blob: 7d57f5e72100eae14e239e2462a27a18069e3418 [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
7import logging
8import os
9import subprocess
10import unittest
11
12import swarming_lib
13import utils
14
15
16# The local dev_appserver script, only available when Google App Engine SDK
17# is set.
18LOCAL_DEV_APPSERVER = 'dev_appserver.py'
19
20# The configuration file for local integration test.
21INTEGRATION_TEST_CONFIG = 'test.yaml'
22
23# Indicate whether it's in debug.
24DEBUG_MODE = logging.getLogger().getEffectiveLevel() <= logging.DEBUG
25
26# The url to start local dev appserver.
27DEV_APPSERVER_PORT = 8888
28DEV_APPSERVER_URL = 'http://localhost:%d' % DEV_APPSERVER_PORT
Xixuan Wu8c173c32017-11-06 11:23:06 -080029DEV_APPSERVER_ADMIN_PORT = 8001
Xixuan Wu865fa282017-09-05 15:23:19 -070030
31
32def _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
40def _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
54class 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 Wu8c173c32017-11-06 11:23:06 -080064 ['%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 Wu865fa282017-09-05 15:23:19 -070067 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 Wua17e3282018-08-21 16:11:11 -070082 def testAFESwarmingDummyRun(self):
Xixuan Wu865fa282017-09-05 15:23:19 -070083 """Swarming test with local configs and dev_appservers."""
84 output = _subprocess_wrapper(
85 subprocess.check_output,
Xixuan Wua17e3282018-08-21 16:11:11 -070086 ['curl', '%s/test_push/afe_swarming' % DEV_APPSERVER_URL])
Xixuan Wu865fa282017-09-05 15:23:19 -070087 self.assertEqual(output, swarming_lib.DUMMY_TASK_NAME)
88
Xixuan Wua17e3282018-08-21 16:11:11 -070089 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 Wu8c173c32017-11-06 11:23:06 -080095
Xixuan Wu865fa282017-09-05 15:23:19 -070096
97if __name__ == '__main__':
98 unittest.main()