blob: e39916357ccfd358362e8a068f829deb30c8566b [file] [log] [blame]
Xixuan Wu865fa282017-09-05 15:23:19 -07001# Copyright 2017 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Module for local integration tests."""
6
7import logging
8import os
9import subprocess
10import unittest
11
Xixuan Wu865fa282017-09-05 15:23:19 -070012import utils
13
Xinan Linc9f01152020-02-05 22:05:13 -080014import analytics
15import task_config_reader
16
Xixuan Wu865fa282017-09-05 15:23:19 -070017
18# The local dev_appserver script, only available when Google App Engine SDK
19# is set.
20LOCAL_DEV_APPSERVER = 'dev_appserver.py'
21
22# The configuration file for local integration test.
23INTEGRATION_TEST_CONFIG = 'test.yaml'
24
25# Indicate whether it's in debug.
26DEBUG_MODE = logging.getLogger().getEffectiveLevel() <= logging.DEBUG
27
28# The url to start local dev appserver.
29DEV_APPSERVER_PORT = 8888
30DEV_APPSERVER_URL = 'http://localhost:%d' % DEV_APPSERVER_PORT
Xixuan Wu8c173c32017-11-06 11:23:06 -080031DEV_APPSERVER_ADMIN_PORT = 8001
Xixuan Wu865fa282017-09-05 15:23:19 -070032
33
34def _subprocess_wrapper(func, cmd, **kwargs):
35 if DEBUG_MODE:
36 return func(cmd, **kwargs)
37 else:
38 with open(os.devnull, 'w') as devnull:
39 return func(cmd, stderr=devnull, **kwargs)
40
41
42def _check_dev_appserver():
43 """Wait for dev_appserver to start for integration tests.
44
45 Returns:
46 The output of calling DEV_APPSERVER_URL/check_health.
47
48 Raises:
49 subprocess.CalledProcessError: if the url is not working.
50 """
51 return _subprocess_wrapper(
52 subprocess.check_output,
53 ['curl', '%s/check_health' % DEV_APPSERVER_URL])
54
55
56class IntegrationTest(unittest.TestCase):
57
58 @classmethod
59 def setUpClass(cls):
60 # In order to only start dev_appserver once for all tests, use setUpClass
61 # here. However there's no corresponding addCleanUp() for this class
62 # function, so use 'try except' to terminate the dev_appserver process.
Xixuan Wu0d730582019-04-25 11:20:39 -070063 super(IntegrationTest, cls).setUpClass()
Xixuan Wu865fa282017-09-05 15:23:19 -070064 try:
65 cls.dev_app_proc = _subprocess_wrapper(
66 subprocess.Popen,
Xixuan Wu8c173c32017-11-06 11:23:06 -080067 ['%s %s --port %d --admin_port %d --log_level=debug' %
68 (LOCAL_DEV_APPSERVER, INTEGRATION_TEST_CONFIG,
69 DEV_APPSERVER_PORT, DEV_APPSERVER_ADMIN_PORT)],
Xixuan Wu865fa282017-09-05 15:23:19 -070070 shell=True)
71
72 # Wait for dev_appserver to start locally.
73 utils.wait_for_value(
74 _check_dev_appserver,
75 exception_to_raise=subprocess.CalledProcessError)
76 except:
77 cls.tearDownClass()
78 # Still raise error to indicate that tests fail.
79 raise
80
81 @classmethod
82 def tearDownClass(cls):
Xixuan Wu0d730582019-04-25 11:20:39 -070083 super(IntegrationTest, cls).tearDownClass()
Xixuan Wu865fa282017-09-05 15:23:19 -070084 cls.dev_app_proc.terminate()
85
Xixuan Wuf856ff12019-05-21 14:09:38 -070086 def testBigqueryGetPassedFromBB(self):
87 """Test get_passed_builds from Buildbucket Bigquery database."""
88 output = _subprocess_wrapper(
89 subprocess.check_output,
90 ['curl', '%s/test_push/bq_get_passed' % DEV_APPSERVER_URL])
91 self.assertIn('release', output)
92
93 def testBigqueryGetRelaxedPassedFromBB(self):
94 """Test get_relaxed_passed_builds from Buildbucket Bigquery database."""
95 output = _subprocess_wrapper(
96 subprocess.check_output,
97 ['curl', '%s/test_push/bq_get_relaxed_passed' % DEV_APPSERVER_URL])
98 self.assertIn('release', output)
99
Xinan Lin80a9d932019-10-17 09:24:43 -0700100 def testBigqueryGetJobsLastHours(self):
101 """Test get_past_buildbucket_job_nums from cros_test_platform BQ database"""
102 output = _subprocess_wrapper(
103 subprocess.check_output,
104 ['curl', '%s/test_push/bq_get_last_hours' % DEV_APPSERVER_URL])
105 self.assertIn('ok', output)
106
Xinan Linc61196b2019-08-13 10:37:30 -0700107 def testBuildbucketClient(self):
108 """Test buildbucket client by calling test-platform's dev builder."""
109 output = _subprocess_wrapper(
110 subprocess.check_output,
111 ['curl', '%s/test_push/buildbucket_lib_compatibility'
112 % DEV_APPSERVER_URL])
113 self.assertIn('status: SCHEDULED', output)
114
Xinan Linc9f01152020-02-05 22:05:13 -0800115 def testSchedulerSnapshot(self):
116 """Test SchedulerJobSection could be inserted to BQ."""
117 output = _subprocess_wrapper(
118 subprocess.check_output,
119 ['curl', '%s/test_push/fake_scheduler_job_section'
120 % DEV_APPSERVER_URL])
121 self.assertIn('ok', output)
122
Xixuan Wu865fa282017-09-05 15:23:19 -0700123
124if __name__ == '__main__':
125 unittest.main()