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 | |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 7 | import datetime |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 8 | import logging |
| 9 | import os |
| 10 | import subprocess |
| 11 | import unittest |
| 12 | |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 13 | import constants |
| 14 | import stackdriver_lib |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 15 | import swarming_lib |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 16 | import time_converter |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 17 | import utils |
| 18 | |
| 19 | |
| 20 | # The local dev_appserver script, only available when Google App Engine SDK |
| 21 | # is set. |
| 22 | LOCAL_DEV_APPSERVER = 'dev_appserver.py' |
| 23 | |
| 24 | # The configuration file for local integration test. |
| 25 | INTEGRATION_TEST_CONFIG = 'test.yaml' |
| 26 | |
| 27 | # Indicate whether it's in debug. |
| 28 | DEBUG_MODE = logging.getLogger().getEffectiveLevel() <= logging.DEBUG |
| 29 | |
| 30 | # The url to start local dev appserver. |
| 31 | DEV_APPSERVER_PORT = 8888 |
| 32 | DEV_APPSERVER_URL = 'http://localhost:%d' % DEV_APPSERVER_PORT |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 33 | DEV_APPSERVER_ADMIN_PORT = 8001 |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 34 | |
| 35 | |
| 36 | def _subprocess_wrapper(func, cmd, **kwargs): |
| 37 | if DEBUG_MODE: |
| 38 | return func(cmd, **kwargs) |
| 39 | else: |
| 40 | with open(os.devnull, 'w') as devnull: |
| 41 | return func(cmd, stderr=devnull, **kwargs) |
| 42 | |
| 43 | |
| 44 | def _check_dev_appserver(): |
| 45 | """Wait for dev_appserver to start for integration tests. |
| 46 | |
| 47 | Returns: |
| 48 | The output of calling DEV_APPSERVER_URL/check_health. |
| 49 | |
| 50 | Raises: |
| 51 | subprocess.CalledProcessError: if the url is not working. |
| 52 | """ |
| 53 | return _subprocess_wrapper( |
| 54 | subprocess.check_output, |
| 55 | ['curl', '%s/check_health' % DEV_APPSERVER_URL]) |
| 56 | |
| 57 | |
| 58 | class IntegrationTest(unittest.TestCase): |
| 59 | |
| 60 | @classmethod |
| 61 | def setUpClass(cls): |
| 62 | # In order to only start dev_appserver once for all tests, use setUpClass |
| 63 | # here. However there's no corresponding addCleanUp() for this class |
| 64 | # function, so use 'try except' to terminate the dev_appserver process. |
| 65 | try: |
| 66 | cls.dev_app_proc = _subprocess_wrapper( |
| 67 | subprocess.Popen, |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 68 | ['%s %s --port %d --admin_port %d --log_level=debug' % |
| 69 | (LOCAL_DEV_APPSERVER, INTEGRATION_TEST_CONFIG, |
| 70 | DEV_APPSERVER_PORT, DEV_APPSERVER_ADMIN_PORT)], |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 71 | shell=True) |
| 72 | |
| 73 | # Wait for dev_appserver to start locally. |
| 74 | utils.wait_for_value( |
| 75 | _check_dev_appserver, |
| 76 | exception_to_raise=subprocess.CalledProcessError) |
| 77 | except: |
| 78 | cls.tearDownClass() |
| 79 | # Still raise error to indicate that tests fail. |
| 80 | raise |
| 81 | |
| 82 | @classmethod |
| 83 | def tearDownClass(cls): |
| 84 | cls.dev_app_proc.terminate() |
| 85 | |
| 86 | def testSwarmingDummyRun(self): |
| 87 | """Swarming test with local configs and dev_appservers.""" |
| 88 | output = _subprocess_wrapper( |
| 89 | subprocess.check_output, |
| 90 | ['curl', '%s/test_push/swarming' % DEV_APPSERVER_URL]) |
| 91 | self.assertEqual(output, swarming_lib.DUMMY_TASK_NAME) |
| 92 | |
Xixuan Wu | 69a5e78 | 2017-11-06 14:32:05 -0800 | [diff] [blame] | 93 | def testWebLogger(self): |
| 94 | """RPC test for logger.""" |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 95 | current_time = time_converter.utc_now() |
| 96 | start_time = current_time - datetime.timedelta(hours=12) |
Xixuan Wu | 69a5e78 | 2017-11-06 14:32:05 -0800 | [diff] [blame] | 97 | log_format = ('%s/logger?start_time=%s&end_time=%s&' |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 98 | 'resource=%s&app_id=%s') |
| 99 | log_url = log_format % ( |
| 100 | DEV_APPSERVER_URL, |
| 101 | start_time.strftime(time_converter.STACKDRIVER_TIME_FORMAT), |
| 102 | current_time.strftime(time_converter.STACKDRIVER_TIME_FORMAT), |
| 103 | '/cron/test_push', |
| 104 | 'google.com:%s' % constants.AppID.STAGING_APP) |
Xixuan Wu | 8929a30 | 2017-12-07 18:09:31 -0800 | [diff] [blame] | 105 | |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 106 | output = _subprocess_wrapper(subprocess.check_output, ['curl', log_url]) |
| 107 | self.assertEqual(len(output.splitlines()[0].split('|')), |
| 108 | len(stackdriver_lib.LOGGING_FORMAT.split('|'))) |
| 109 | |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 110 | |
| 111 | if __name__ == '__main__': |
| 112 | unittest.main() |