blob: 3e856d1de5f2e5da89b14ad5e14ddd32440d81e6 [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
Xixuan Wu8c173c32017-11-06 11:23:06 -08007import datetime
Xixuan Wu865fa282017-09-05 15:23:19 -07008import logging
9import os
10import subprocess
11import unittest
12
Xixuan Wu8c173c32017-11-06 11:23:06 -080013import constants
14import stackdriver_lib
Xixuan Wu865fa282017-09-05 15:23:19 -070015import swarming_lib
Xixuan Wu8c173c32017-11-06 11:23:06 -080016import time_converter
Xixuan Wu865fa282017-09-05 15:23:19 -070017import utils
18
19
20# The local dev_appserver script, only available when Google App Engine SDK
21# is set.
22LOCAL_DEV_APPSERVER = 'dev_appserver.py'
23
24# The configuration file for local integration test.
25INTEGRATION_TEST_CONFIG = 'test.yaml'
26
27# Indicate whether it's in debug.
28DEBUG_MODE = logging.getLogger().getEffectiveLevel() <= logging.DEBUG
29
30# The url to start local dev appserver.
31DEV_APPSERVER_PORT = 8888
32DEV_APPSERVER_URL = 'http://localhost:%d' % DEV_APPSERVER_PORT
Xixuan Wu8c173c32017-11-06 11:23:06 -080033DEV_APPSERVER_ADMIN_PORT = 8001
Xixuan Wu865fa282017-09-05 15:23:19 -070034
35
36def _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
44def _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
58class 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 Wu8c173c32017-11-06 11:23:06 -080068 ['%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 Wu865fa282017-09-05 15:23:19 -070071 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 Wu69a5e782017-11-06 14:32:05 -080093 def testWebLogger(self):
94 """RPC test for logger."""
Xixuan Wu8c173c32017-11-06 11:23:06 -080095 current_time = time_converter.utc_now()
96 start_time = current_time - datetime.timedelta(hours=12)
Xixuan Wu69a5e782017-11-06 14:32:05 -080097 log_format = ('%s/logger?start_time=%s&end_time=%s&'
Xixuan Wu8c173c32017-11-06 11:23:06 -080098 '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 Wu8929a302017-12-07 18:09:31 -0800105
Xixuan Wu8c173c32017-11-06 11:23:06 -0800106 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 Wu865fa282017-09-05 15:23:19 -0700110
111if __name__ == '__main__':
112 unittest.main()