blob: ee85bd8d5b6fd3f12ba90858a524aadc74450129 [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.
Xixuan Wu0d730582019-04-25 11:20:39 -070061 super(IntegrationTest, cls).setUpClass()
Xixuan Wu865fa282017-09-05 15:23:19 -070062 try:
63 cls.dev_app_proc = _subprocess_wrapper(
64 subprocess.Popen,
Xixuan Wu8c173c32017-11-06 11:23:06 -080065 ['%s %s --port %d --admin_port %d --log_level=debug' %
66 (LOCAL_DEV_APPSERVER, INTEGRATION_TEST_CONFIG,
67 DEV_APPSERVER_PORT, DEV_APPSERVER_ADMIN_PORT)],
Xixuan Wu865fa282017-09-05 15:23:19 -070068 shell=True)
69
70 # Wait for dev_appserver to start locally.
71 utils.wait_for_value(
72 _check_dev_appserver,
73 exception_to_raise=subprocess.CalledProcessError)
74 except:
75 cls.tearDownClass()
76 # Still raise error to indicate that tests fail.
77 raise
78
79 @classmethod
80 def tearDownClass(cls):
Xixuan Wu0d730582019-04-25 11:20:39 -070081 super(IntegrationTest, cls).tearDownClass()
Xixuan Wu865fa282017-09-05 15:23:19 -070082 cls.dev_app_proc.terminate()
83
Xixuan Wua17e3282018-08-21 16:11:11 -070084 def testAFESwarmingDummyRun(self):
Xixuan Wu865fa282017-09-05 15:23:19 -070085 """Swarming test with local configs and dev_appservers."""
86 output = _subprocess_wrapper(
87 subprocess.check_output,
Xixuan Wud9dd4742018-08-23 15:57:03 -070088 ['curl', '%s/test_push/afe_dummy' % DEV_APPSERVER_URL])
Xixuan Wu865fa282017-09-05 15:23:19 -070089 self.assertEqual(output, swarming_lib.DUMMY_TASK_NAME)
90
Xixuan Wua17e3282018-08-21 16:11:11 -070091 def testSkylabSwarmingDummyRun(self):
92 """Swarming test with local configs and dev_appservers."""
93 output = _subprocess_wrapper(
94 subprocess.check_output,
Xixuan Wud9dd4742018-08-23 15:57:03 -070095 ['curl', '%s/test_push/skylab_dummy' % DEV_APPSERVER_URL])
Xixuan Wua17e3282018-08-21 16:11:11 -070096 self.assertEqual(output, swarming_lib.DUMMY_TASK_NAME)
Xixuan Wu8c173c32017-11-06 11:23:06 -080097
Xixuan Wud9dd4742018-08-23 15:57:03 -070098 def testAFESwarmingSanity(self):
99 """Swarming test with local configs and dev_appservers."""
100 output = _subprocess_wrapper(
101 subprocess.check_output,
102 ['curl', '%s/test_push/afe_sanity' % DEV_APPSERVER_URL])
103 self.assertIn('sanity', output)
104
105 def testSkylabSwarmingSanity(self):
106 """Swarming test with local configs and dev_appservers."""
107 output = _subprocess_wrapper(
108 subprocess.check_output,
109 ['curl', '%s/test_push/skylab_sanity' % DEV_APPSERVER_URL])
110 self.assertIn('sanity', output)
111
Xixuan Wu0d730582019-04-25 11:20:39 -0700112 def testSkylabBackwardCompatibilitySwarmingSanity(self):
113 """Swarming test with local configs and dev_appservers."""
114 output = _subprocess_wrapper(
115 subprocess.check_output,
116 ['curl', '%s/test_push/skylab_sanity_bc_test' % DEV_APPSERVER_URL])
117 self.assertIn('sanity', output)
118
Xixuan Wuf856ff12019-05-21 14:09:38 -0700119 def testBigqueryGetPassedFromBB(self):
120 """Test get_passed_builds from Buildbucket Bigquery database."""
121 output = _subprocess_wrapper(
122 subprocess.check_output,
123 ['curl', '%s/test_push/bq_get_passed' % DEV_APPSERVER_URL])
124 self.assertIn('release', output)
125
126 def testBigqueryGetRelaxedPassedFromBB(self):
127 """Test get_relaxed_passed_builds from Buildbucket Bigquery database."""
128 output = _subprocess_wrapper(
129 subprocess.check_output,
130 ['curl', '%s/test_push/bq_get_relaxed_passed' % DEV_APPSERVER_URL])
131 self.assertIn('release', output)
132
Xixuan Wu865fa282017-09-05 15:23:19 -0700133
134if __name__ == '__main__':
135 unittest.main()