blob: 7c176f254426c16f001e2aa69ca5d8c9e0e26a53 [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
29
30
31def _subprocess_wrapper(func, cmd, **kwargs):
32 if DEBUG_MODE:
33 return func(cmd, **kwargs)
34 else:
35 with open(os.devnull, 'w') as devnull:
36 return func(cmd, stderr=devnull, **kwargs)
37
38
39def _check_dev_appserver():
40 """Wait for dev_appserver to start for integration tests.
41
42 Returns:
43 The output of calling DEV_APPSERVER_URL/check_health.
44
45 Raises:
46 subprocess.CalledProcessError: if the url is not working.
47 """
48 return _subprocess_wrapper(
49 subprocess.check_output,
50 ['curl', '%s/check_health' % DEV_APPSERVER_URL])
51
52
53class IntegrationTest(unittest.TestCase):
54
55 @classmethod
56 def setUpClass(cls):
57 # In order to only start dev_appserver once for all tests, use setUpClass
58 # here. However there's no corresponding addCleanUp() for this class
59 # function, so use 'try except' to terminate the dev_appserver process.
60 try:
61 cls.dev_app_proc = _subprocess_wrapper(
62 subprocess.Popen,
63 ['%s %s --port %d --log_level=debug' %
64 (LOCAL_DEV_APPSERVER, INTEGRATION_TEST_CONFIG, DEV_APPSERVER_PORT)],
65 shell=True)
66
67 # Wait for dev_appserver to start locally.
68 utils.wait_for_value(
69 _check_dev_appserver,
70 exception_to_raise=subprocess.CalledProcessError)
71 except:
72 cls.tearDownClass()
73 # Still raise error to indicate that tests fail.
74 raise
75
76 @classmethod
77 def tearDownClass(cls):
78 cls.dev_app_proc.terminate()
79
80 def testSwarmingDummyRun(self):
81 """Swarming test with local configs and dev_appservers."""
82 output = _subprocess_wrapper(
83 subprocess.check_output,
84 ['curl', '%s/test_push/swarming' % DEV_APPSERVER_URL])
85 self.assertEqual(output, swarming_lib.DUMMY_TASK_NAME)
86
87
88if __name__ == '__main__':
89 unittest.main()