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. |
| 61 | try: |
| 62 | cls.dev_app_proc = _subprocess_wrapper( |
| 63 | subprocess.Popen, |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 64 | ['%s %s --port %d --admin_port %d --log_level=debug' % |
| 65 | (LOCAL_DEV_APPSERVER, INTEGRATION_TEST_CONFIG, |
| 66 | DEV_APPSERVER_PORT, DEV_APPSERVER_ADMIN_PORT)], |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 67 | shell=True) |
| 68 | |
| 69 | # Wait for dev_appserver to start locally. |
| 70 | utils.wait_for_value( |
| 71 | _check_dev_appserver, |
| 72 | exception_to_raise=subprocess.CalledProcessError) |
| 73 | except: |
| 74 | cls.tearDownClass() |
| 75 | # Still raise error to indicate that tests fail. |
| 76 | raise |
| 77 | |
| 78 | @classmethod |
| 79 | def tearDownClass(cls): |
| 80 | cls.dev_app_proc.terminate() |
| 81 | |
Xixuan Wu | a17e328 | 2018-08-21 16:11:11 -0700 | [diff] [blame] | 82 | def testAFESwarmingDummyRun(self): |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 83 | """Swarming test with local configs and dev_appservers.""" |
| 84 | output = _subprocess_wrapper( |
| 85 | subprocess.check_output, |
Xixuan Wu | a17e328 | 2018-08-21 16:11:11 -0700 | [diff] [blame] | 86 | ['curl', '%s/test_push/afe_swarming' % DEV_APPSERVER_URL]) |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 87 | self.assertEqual(output, swarming_lib.DUMMY_TASK_NAME) |
| 88 | |
Xixuan Wu | a17e328 | 2018-08-21 16:11:11 -0700 | [diff] [blame] | 89 | def testSkylabSwarmingDummyRun(self): |
| 90 | """Swarming test with local configs and dev_appservers.""" |
| 91 | output = _subprocess_wrapper( |
| 92 | subprocess.check_output, |
| 93 | ['curl', '%s/test_push/skylab_swarming' % DEV_APPSERVER_URL]) |
| 94 | self.assertEqual(output, swarming_lib.DUMMY_TASK_NAME) |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 95 | |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 96 | |
| 97 | if __name__ == '__main__': |
| 98 | unittest.main() |