Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 1 | # 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 | |
| 7 | import logging |
| 8 | import os |
| 9 | import subprocess |
| 10 | import unittest |
| 11 | |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 12 | import utils |
| 13 | |
Xinan Lin | c9f0115 | 2020-02-05 22:05:13 -0800 | [diff] [blame] | 14 | import analytics |
| 15 | import task_config_reader |
| 16 | |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 17 | |
| 18 | # The local dev_appserver script, only available when Google App Engine SDK |
| 19 | # is set. |
| 20 | LOCAL_DEV_APPSERVER = 'dev_appserver.py' |
| 21 | |
| 22 | # The configuration file for local integration test. |
| 23 | INTEGRATION_TEST_CONFIG = 'test.yaml' |
| 24 | |
| 25 | # Indicate whether it's in debug. |
| 26 | DEBUG_MODE = logging.getLogger().getEffectiveLevel() <= logging.DEBUG |
| 27 | |
| 28 | # The url to start local dev appserver. |
| 29 | DEV_APPSERVER_PORT = 8888 |
| 30 | DEV_APPSERVER_URL = 'http://localhost:%d' % DEV_APPSERVER_PORT |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 31 | DEV_APPSERVER_ADMIN_PORT = 8001 |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 32 | |
| 33 | |
| 34 | def _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 | |
| 42 | def _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 | |
| 56 | class 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 Wu | 0d73058 | 2019-04-25 11:20:39 -0700 | [diff] [blame] | 63 | super(IntegrationTest, cls).setUpClass() |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 64 | try: |
| 65 | cls.dev_app_proc = _subprocess_wrapper( |
| 66 | subprocess.Popen, |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 67 | ['%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 Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 70 | 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 Wu | 0d73058 | 2019-04-25 11:20:39 -0700 | [diff] [blame] | 83 | super(IntegrationTest, cls).tearDownClass() |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 84 | cls.dev_app_proc.terminate() |
| 85 | |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 86 | 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 Lin | 80a9d93 | 2019-10-17 09:24:43 -0700 | [diff] [blame] | 100 | 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 Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 107 | 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 Lin | c9f0115 | 2020-02-05 22:05:13 -0800 | [diff] [blame] | 115 | 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 Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 123 | |
| 124 | if __name__ == '__main__': |
| 125 | unittest.main() |