Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 1 | # Copyright 2019 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 buildbucket unittests.""" |
| 6 | |
| 7 | import mock |
| 8 | import os |
| 9 | import unittest |
| 10 | |
| 11 | import build_lib |
| 12 | import buildbucket |
| 13 | import task_executor |
| 14 | |
| 15 | from chromite.api.gen.test_platform import request_pb2 |
| 16 | from google.appengine.ext import testbed |
| 17 | from google.protobuf import json_format |
| 18 | |
| 19 | |
| 20 | ADDR = u'http://localhost:1' |
| 21 | |
| 22 | |
| 23 | def _get_suite_job_parameters(): |
| 24 | return { |
| 25 | 'suite': 'fake_suite', |
| 26 | 'board': 'fake_board', |
| 27 | 'model': 'fake_model', |
| 28 | build_lib.BuildVersionKey.CROS_VERSION: 'fake_cros_build', |
| 29 | build_lib.BuildVersionKey.FW_RW_VERSION: 'fake_firmware_rw_build', |
| 30 | build_lib.BuildVersionKey.FW_RO_VERSION: 'fake_firmware_ro_build', |
| 31 | build_lib.BuildVersionKey.ANDROID_BUILD_VERSION: 'fake_android_build', |
| 32 | build_lib.BuildVersionKey.TESTBED_BUILD_VERSION: 'fake_testbed_build', |
| 33 | 'num': 1, |
| 34 | 'pool': 'DUT_POOL_SUITES', |
| 35 | 'priority': 10, |
Xinan Lin | 6e09738 | 2019-08-27 18:43:35 -0700 | [diff] [blame] | 36 | 'timeout': 12, |
| 37 | 'timeout_mins': 4320, |
| 38 | 'max_runtime_mins': 4320, |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 39 | 'no_wait_for_results': True, |
| 40 | 'test_source_build': 'fake_test_source_build', |
| 41 | 'job_retry': False, |
| 42 | 'no_delay': False, |
| 43 | 'force': False, |
| 44 | 'run_prod_code': True, |
| 45 | 'is_skylab': False, |
| 46 | } |
| 47 | |
| 48 | |
| 49 | class FakeTestPlatformClient(object): |
| 50 | |
| 51 | def __init__(self, test): |
| 52 | self._test = test |
| 53 | self.called_with_requests = [] |
| 54 | |
| 55 | def ScheduleBuild(self, req, credentials=None): |
| 56 | self.called_with_requests.append(req) |
| 57 | # No test_platform_response proto. Simply return '200, ok'. |
| 58 | return '200 ok' |
| 59 | |
| 60 | |
| 61 | class TestPlatformClientTestCase(unittest.TestCase): |
| 62 | |
| 63 | def setUp(self): |
| 64 | super(TestPlatformClientTestCase, self).setUp() |
| 65 | self.fake_client = FakeTestPlatformClient(self) |
| 66 | patcher = mock.patch('buildbucket._get_client', |
| 67 | return_value=self._get_client(ADDR)) |
| 68 | patcher.start() |
| 69 | self.client = buildbucket.TestPlatformClient(ADDR, |
| 70 | 'foo-proj', |
| 71 | 'foo-bucket', |
| 72 | 'foo-builder') |
| 73 | self.addCleanup(patcher.stop) |
| 74 | self.testbed = testbed.Testbed() |
| 75 | self.testbed.activate() |
| 76 | self.addCleanup(self.testbed.deactivate) |
| 77 | self.testbed.init_taskqueue_stub( |
| 78 | root_path=os.path.join(os.path.dirname(__file__))) |
| 79 | self.taskqueue_stub = self.testbed.get_stub( |
| 80 | testbed.TASKQUEUE_SERVICE_NAME) |
| 81 | |
| 82 | def _get_client(self, addr): |
| 83 | self.assertEqual(ADDR, addr) |
| 84 | return self.fake_client |
| 85 | |
| 86 | def testFormTestPlatformRequestSuccessfully(self): |
| 87 | tp = _get_suite_job_parameters() |
| 88 | self.assertEqual(len(self.fake_client.called_with_requests), 0) |
| 89 | task_executor.push(task_executor.SUITES_QUEUE, **tp) |
| 90 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 91 | params = tasks[0].extract_params() |
| 92 | self.client.run(**params) |
| 93 | self.assertEqual(len(self.fake_client.called_with_requests), 1) |
| 94 | req = _struct_pb2_to_request_pb2( |
| 95 | self.fake_client.called_with_requests[0].properties['request']) |
| 96 | |
| 97 | self.assertEqual(req.params.scheduling.managed_pool, |
| 98 | request_pb2.Request.Params.Scheduling.MANAGED_POOL_SUITES) |
Xinan Lin | 6e09738 | 2019-08-27 18:43:35 -0700 | [diff] [blame] | 99 | self.assertEqual(req.params.time.maximum_duration.seconds, 165600) |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 100 | self.assertEqual(req.params.metadata.test_metadata_url, |
Xinan Lin | 6e09738 | 2019-08-27 18:43:35 -0700 | [diff] [blame] | 101 | 'gs://chromeos-image-archive/' + tp['test_source_build']) |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 102 | self.assertEqual(req.test_plan.suite[0].name, tp['suite']) |
| 103 | |
Alex Zamorzaev | c193560 | 2019-08-28 14:37:35 -0700 | [diff] [blame] | 104 | def testNonStandardPoolName(self): |
| 105 | tp = _get_suite_job_parameters() |
| 106 | self.assertEqual(len(self.fake_client.called_with_requests), 0) |
| 107 | tp['pool'] = 'cts' |
| 108 | task_executor.push(task_executor.SUITES_QUEUE, **tp) |
| 109 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 110 | params = tasks[0].extract_params() |
| 111 | self.client.run(**params) |
| 112 | req = _struct_pb2_to_request_pb2( |
| 113 | self.fake_client.called_with_requests[0].properties['request']) |
| 114 | |
| 115 | self.assertEqual(req.params.scheduling.managed_pool, |
| 116 | request_pb2.Request.Params.Scheduling.MANAGED_POOL_CTS) |
| 117 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 118 | def testShouldGoUnmanagedPool(self): |
| 119 | tp = _get_suite_job_parameters() |
| 120 | self.assertEqual(len(self.fake_client.called_with_requests), 0) |
| 121 | tp['pool'] = 'wifi' |
| 122 | task_executor.push(task_executor.SUITES_QUEUE, **tp) |
| 123 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 124 | params = tasks[0].extract_params() |
| 125 | self.client.run(**params) |
| 126 | req = _struct_pb2_to_request_pb2( |
| 127 | self.fake_client.called_with_requests[0].properties['request']) |
| 128 | |
| 129 | self.assertEqual(req.params.scheduling.unmanaged_pool, tp['pool']) |
| 130 | |
| 131 | def testShouldSchedulewithQuotaAccount(self): |
| 132 | tp = _get_suite_job_parameters() |
| 133 | self.assertEqual(len(self.fake_client.called_with_requests), 0) |
| 134 | tp['override_qs_account'] = 'dummy@foo.com' |
| 135 | task_executor.push(task_executor.SUITES_QUEUE, **tp) |
| 136 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 137 | params = tasks[0].extract_params() |
| 138 | self.client.run(**params) |
| 139 | req = _struct_pb2_to_request_pb2( |
| 140 | self.fake_client.called_with_requests[0].properties['request']) |
| 141 | |
| 142 | self.assertEqual(req.params.scheduling.quota_account, |
| 143 | tp['override_qs_account']) |
| 144 | |
| 145 | def testNoFinalBuild(self): |
| 146 | tp = _get_suite_job_parameters() |
| 147 | tp[build_lib.BuildVersionKey.CROS_VERSION] = None |
| 148 | tp[build_lib.BuildVersionKey.ANDROID_BUILD_VERSION] = None |
| 149 | tp[build_lib.BuildVersionKey.TESTBED_BUILD_VERSION] = None |
| 150 | task_executor.push(task_executor.SUITES_QUEUE, **tp) |
| 151 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 152 | params = tasks[0].extract_params() |
| 153 | self.assertRaises(buildbucket.BuildbucketRunError, |
| 154 | self.client.run, **params) |
| 155 | |
| 156 | |
| 157 | def _struct_pb2_to_request_pb2(struct_pb2): |
| 158 | """Transform google struct proto to test_platform_request. |
| 159 | |
| 160 | Args: |
| 161 | struct_pb2: A struct_pb2 instance. |
| 162 | |
| 163 | Returns: |
| 164 | A request_pb2 instance. |
| 165 | """ |
| 166 | json = json_format.MessageToJson(struct_pb2) |
| 167 | return json_format.Parse(json, request_pb2.Request()) |