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 | |
| 12 | import swarming_lib |
| 13 | import utils |
| 14 | |
| 15 | |
| 16 | # The local dev_appserver script, only available when Google App Engine SDK |
| 17 | # is set. |
| 18 | LOCAL_DEV_APPSERVER = 'dev_appserver.py' |
| 19 | |
| 20 | # The configuration file for local integration test. |
| 21 | INTEGRATION_TEST_CONFIG = 'test.yaml' |
| 22 | |
| 23 | # Indicate whether it's in debug. |
| 24 | DEBUG_MODE = logging.getLogger().getEffectiveLevel() <= logging.DEBUG |
| 25 | |
| 26 | # The url to start local dev appserver. |
| 27 | DEV_APPSERVER_PORT = 8888 |
| 28 | DEV_APPSERVER_URL = 'http://localhost:%d' % DEV_APPSERVER_PORT |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 29 | DEV_APPSERVER_ADMIN_PORT = 8001 |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 30 | |
| 31 | |
| 32 | def _subprocess_wrapper(func, cmd, **kwargs): |
| 33 | if DEBUG_MODE: |
| 34 | return func(cmd, **kwargs) |
| 35 | else: |
| 36 | with open(os.devnull, 'w') as devnull: |
| 37 | return func(cmd, stderr=devnull, **kwargs) |
| 38 | |
| 39 | |
| 40 | def _check_dev_appserver(): |
| 41 | """Wait for dev_appserver to start for integration tests. |
| 42 | |
| 43 | Returns: |
| 44 | The output of calling DEV_APPSERVER_URL/check_health. |
| 45 | |
| 46 | Raises: |
| 47 | subprocess.CalledProcessError: if the url is not working. |
| 48 | """ |
| 49 | return _subprocess_wrapper( |
| 50 | subprocess.check_output, |
| 51 | ['curl', '%s/check_health' % DEV_APPSERVER_URL]) |
| 52 | |
| 53 | |
| 54 | class IntegrationTest(unittest.TestCase): |
| 55 | |
| 56 | @classmethod |
| 57 | def setUpClass(cls): |
| 58 | # In order to only start dev_appserver once for all tests, use setUpClass |
| 59 | # here. However there's no corresponding addCleanUp() for this class |
| 60 | # function, so use 'try except' to terminate the dev_appserver process. |
Xixuan Wu | 0d73058 | 2019-04-25 11:20:39 -0700 | [diff] [blame] | 61 | super(IntegrationTest, cls).setUpClass() |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 62 | try: |
| 63 | cls.dev_app_proc = _subprocess_wrapper( |
| 64 | subprocess.Popen, |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 65 | ['%s %s --port %d --admin_port %d --log_level=debug' % |
| 66 | (LOCAL_DEV_APPSERVER, INTEGRATION_TEST_CONFIG, |
| 67 | DEV_APPSERVER_PORT, DEV_APPSERVER_ADMIN_PORT)], |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 68 | shell=True) |
| 69 | |
| 70 | # Wait for dev_appserver to start locally. |
| 71 | utils.wait_for_value( |
| 72 | _check_dev_appserver, |
| 73 | exception_to_raise=subprocess.CalledProcessError) |
| 74 | except: |
| 75 | cls.tearDownClass() |
| 76 | # Still raise error to indicate that tests fail. |
| 77 | raise |
| 78 | |
| 79 | @classmethod |
| 80 | def tearDownClass(cls): |
Xixuan Wu | 0d73058 | 2019-04-25 11:20:39 -0700 | [diff] [blame] | 81 | super(IntegrationTest, cls).tearDownClass() |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 82 | cls.dev_app_proc.terminate() |
| 83 | |
Xixuan Wu | a17e328 | 2018-08-21 16:11:11 -0700 | [diff] [blame] | 84 | def testAFESwarmingDummyRun(self): |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 85 | """Swarming test with local configs and dev_appservers.""" |
| 86 | output = _subprocess_wrapper( |
| 87 | subprocess.check_output, |
Xixuan Wu | d9dd474 | 2018-08-23 15:57:03 -0700 | [diff] [blame] | 88 | ['curl', '%s/test_push/afe_dummy' % DEV_APPSERVER_URL]) |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 89 | self.assertEqual(output, swarming_lib.DUMMY_TASK_NAME) |
| 90 | |
Xixuan Wu | a17e328 | 2018-08-21 16:11:11 -0700 | [diff] [blame] | 91 | def testSkylabSwarmingDummyRun(self): |
| 92 | """Swarming test with local configs and dev_appservers.""" |
| 93 | output = _subprocess_wrapper( |
| 94 | subprocess.check_output, |
Xixuan Wu | d9dd474 | 2018-08-23 15:57:03 -0700 | [diff] [blame] | 95 | ['curl', '%s/test_push/skylab_dummy' % DEV_APPSERVER_URL]) |
Xixuan Wu | a17e328 | 2018-08-21 16:11:11 -0700 | [diff] [blame] | 96 | self.assertEqual(output, swarming_lib.DUMMY_TASK_NAME) |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 97 | |
Xixuan Wu | d9dd474 | 2018-08-23 15:57:03 -0700 | [diff] [blame] | 98 | def testAFESwarmingSanity(self): |
| 99 | """Swarming test with local configs and dev_appservers.""" |
| 100 | output = _subprocess_wrapper( |
| 101 | subprocess.check_output, |
| 102 | ['curl', '%s/test_push/afe_sanity' % DEV_APPSERVER_URL]) |
| 103 | self.assertIn('sanity', output) |
| 104 | |
| 105 | def testSkylabSwarmingSanity(self): |
| 106 | """Swarming test with local configs and dev_appservers.""" |
| 107 | output = _subprocess_wrapper( |
| 108 | subprocess.check_output, |
| 109 | ['curl', '%s/test_push/skylab_sanity' % DEV_APPSERVER_URL]) |
| 110 | self.assertIn('sanity', output) |
| 111 | |
Xixuan Wu | 0d73058 | 2019-04-25 11:20:39 -0700 | [diff] [blame] | 112 | def testSkylabBackwardCompatibilitySwarmingSanity(self): |
| 113 | """Swarming test with local configs and dev_appservers.""" |
| 114 | output = _subprocess_wrapper( |
| 115 | subprocess.check_output, |
| 116 | ['curl', '%s/test_push/skylab_sanity_bc_test' % DEV_APPSERVER_URL]) |
| 117 | self.assertIn('sanity', output) |
| 118 | |
Xixuan Wu | f856ff1 | 2019-05-21 14:09:38 -0700 | [diff] [blame] | 119 | def testBigqueryGetPassedFromBB(self): |
| 120 | """Test get_passed_builds from Buildbucket Bigquery database.""" |
| 121 | output = _subprocess_wrapper( |
| 122 | subprocess.check_output, |
| 123 | ['curl', '%s/test_push/bq_get_passed' % DEV_APPSERVER_URL]) |
| 124 | self.assertIn('release', output) |
| 125 | |
| 126 | def testBigqueryGetRelaxedPassedFromBB(self): |
| 127 | """Test get_relaxed_passed_builds from Buildbucket Bigquery database.""" |
| 128 | output = _subprocess_wrapper( |
| 129 | subprocess.check_output, |
| 130 | ['curl', '%s/test_push/bq_get_relaxed_passed' % DEV_APPSERVER_URL]) |
| 131 | self.assertIn('release', output) |
| 132 | |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 133 | |
| 134 | if __name__ == '__main__': |
| 135 | unittest.main() |