blob: d518f5429efb00a130f26996637a84ac61dff36c [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
Xixuan Wu865fa282017-09-05 15:23:19 -070012import utils
13
14
15# The local dev_appserver script, only available when Google App Engine SDK
16# is set.
17LOCAL_DEV_APPSERVER = 'dev_appserver.py'
18
19# The configuration file for local integration test.
20INTEGRATION_TEST_CONFIG = 'test.yaml'
21
22# Indicate whether it's in debug.
23DEBUG_MODE = logging.getLogger().getEffectiveLevel() <= logging.DEBUG
24
25# The url to start local dev appserver.
26DEV_APPSERVER_PORT = 8888
27DEV_APPSERVER_URL = 'http://localhost:%d' % DEV_APPSERVER_PORT
Xixuan Wu8c173c32017-11-06 11:23:06 -080028DEV_APPSERVER_ADMIN_PORT = 8001
Xixuan Wu865fa282017-09-05 15:23:19 -070029
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.
Xixuan Wu0d730582019-04-25 11:20:39 -070060 super(IntegrationTest, cls).setUpClass()
Xixuan Wu865fa282017-09-05 15:23:19 -070061 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):
Xixuan Wu0d730582019-04-25 11:20:39 -070080 super(IntegrationTest, cls).tearDownClass()
Xixuan Wu865fa282017-09-05 15:23:19 -070081 cls.dev_app_proc.terminate()
82
Xixuan Wuf856ff12019-05-21 14:09:38 -070083 def testBigqueryGetPassedFromBB(self):
84 """Test get_passed_builds from Buildbucket Bigquery database."""
85 output = _subprocess_wrapper(
86 subprocess.check_output,
87 ['curl', '%s/test_push/bq_get_passed' % DEV_APPSERVER_URL])
88 self.assertIn('release', output)
89
90 def testBigqueryGetRelaxedPassedFromBB(self):
91 """Test get_relaxed_passed_builds from Buildbucket Bigquery database."""
92 output = _subprocess_wrapper(
93 subprocess.check_output,
94 ['curl', '%s/test_push/bq_get_relaxed_passed' % DEV_APPSERVER_URL])
95 self.assertIn('release', output)
96
Xinan Lin80a9d932019-10-17 09:24:43 -070097 def testBigqueryGetJobsLastHours(self):
98 """Test get_past_buildbucket_job_nums from cros_test_platform BQ database"""
99 output = _subprocess_wrapper(
100 subprocess.check_output,
101 ['curl', '%s/test_push/bq_get_last_hours' % DEV_APPSERVER_URL])
102 self.assertIn('ok', output)
103
Xinan Linc61196b2019-08-13 10:37:30 -0700104 def testBuildbucketClient(self):
105 """Test buildbucket client by calling test-platform's dev builder."""
106 output = _subprocess_wrapper(
107 subprocess.check_output,
108 ['curl', '%s/test_push/buildbucket_lib_compatibility'
109 % DEV_APPSERVER_URL])
110 self.assertIn('status: SCHEDULED', output)
111
Xixuan Wu865fa282017-09-05 15:23:19 -0700112
113if __name__ == '__main__':
114 unittest.main()