blob: 554f44ddaa20a374fc2ab596ba6bc968fbb54385 [file] [log] [blame]
Aviv Keshetc679faf2019-11-27 17:52:50 -08001#!/usr/bin/env python2
Xinan Lin3ba18a02019-08-13 15:44:55 -07002# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
Xinan Lin3ba18a02019-08-13 15:44:55 -07005"""Module for buildbucket unittests."""
6
Xinan Lindf0698a2020-02-05 22:38:11 -08007import json
Xinan Lin3ba18a02019-08-13 15:44:55 -07008import mock
9import os
10import unittest
11
Xinan Lin9e4917d2019-11-04 10:58:47 -080012import constants
Xinan Lin3ba18a02019-08-13 15:44:55 -070013import build_lib
14import buildbucket
15import task_executor
16
17from chromite.api.gen.test_platform import request_pb2
Xinan Lindf0698a2020-02-05 22:38:11 -080018from chromite.api.gen.test_platform.suite_scheduler import analytics_pb2
Xinan Lin9e4917d2019-11-04 10:58:47 -080019from google.appengine.api import taskqueue
Xinan Lin3ba18a02019-08-13 15:44:55 -070020from google.appengine.ext import testbed
21from google.protobuf import json_format
Xinan Lindf0698a2020-02-05 22:38:11 -080022from infra_libs.buildbucket.proto import build_pb2
Xinan Lin3ba18a02019-08-13 15:44:55 -070023
Xinan Lin3ba18a02019-08-13 15:44:55 -070024ADDR = u'http://localhost:1'
Xinan Lindf0698a2020-02-05 22:38:11 -080025FAKE_UUID = 'c78e0bf3-4142-11ea-bc66-88e9fe4c5349'
26FAKE_BUILD_ID = 8890493019851395280
Xinan Lin3ba18a02019-08-13 15:44:55 -070027
28
Xinan Lin9e4917d2019-11-04 10:58:47 -080029def _get_suite_params(board='fake_board',
30 model='fake_model',
31 suite='fake_suite',
32 pool='DUT_POOL_SUITES',
33 cros_build='fake_cros_build'):
Xinan Lin3ba18a02019-08-13 15:44:55 -070034 return {
Prathmesh Prabhu2382a182019-09-07 21:18:10 -070035 'suite': suite,
36 'board': board,
37 'model': model,
38 build_lib.BuildVersionKey.CROS_VERSION: cros_build,
Xinan Lin3ba18a02019-08-13 15:44:55 -070039 build_lib.BuildVersionKey.FW_RW_VERSION: 'fake_firmware_rw_build',
40 build_lib.BuildVersionKey.FW_RO_VERSION: 'fake_firmware_ro_build',
41 build_lib.BuildVersionKey.ANDROID_BUILD_VERSION: 'fake_android_build',
42 build_lib.BuildVersionKey.TESTBED_BUILD_VERSION: 'fake_testbed_build',
43 'num': 1,
Prathmesh Prabhu2382a182019-09-07 21:18:10 -070044 'pool': pool,
Xinan Lin3ba18a02019-08-13 15:44:55 -070045 'priority': 10,
Xinan Lin6e097382019-08-27 18:43:35 -070046 'timeout': 12,
47 'timeout_mins': 4320,
48 'max_runtime_mins': 4320,
Xinan Lin3ba18a02019-08-13 15:44:55 -070049 'no_wait_for_results': True,
50 'test_source_build': 'fake_test_source_build',
51 'job_retry': False,
52 'no_delay': False,
53 'force': False,
54 'run_prod_code': True,
55 'is_skylab': False,
56 }
57
58
59class FakeTestPlatformClient(object):
Xinan Lin3ba18a02019-08-13 15:44:55 -070060 def __init__(self, test):
61 self._test = test
62 self.called_with_requests = []
Xinan Lindf0698a2020-02-05 22:38:11 -080063 self.error = None
Xinan Lin3ba18a02019-08-13 15:44:55 -070064
Xinan Lin57e2d962020-03-30 17:24:53 -070065 def ScheduleBuild(self, req, credentials=None, timeout=10):
Xinan Lin3ba18a02019-08-13 15:44:55 -070066 self.called_with_requests.append(req)
Xinan Lindf0698a2020-02-05 22:38:11 -080067 if self.error:
68 return self.error
69 return build_pb2.Build(id=FAKE_BUILD_ID)
70
71
72class FakeBigqueryRestClient(object):
Xinan Lindf0698a2020-02-05 22:38:11 -080073 def __init__(self, rest_client, project=None, dataset=None, table=None):
74 """Initialize the mock class."""
75 self.table = table
76 self.rows = []
77
78 def insert(self, rows):
79 self.rows = rows
80 return True
Xinan Lin3ba18a02019-08-13 15:44:55 -070081
82
83class TestPlatformClientTestCase(unittest.TestCase):
Xinan Lin3ba18a02019-08-13 15:44:55 -070084 def setUp(self):
85 super(TestPlatformClientTestCase, self).setUp()
86 self.fake_client = FakeTestPlatformClient(self)
87 patcher = mock.patch('buildbucket._get_client',
88 return_value=self._get_client(ADDR))
89 patcher.start()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -070090 self.client = buildbucket.TestPlatformClient(ADDR, 'foo-proj',
91 'foo-bucket', 'foo-builder')
Xinan Lin3ba18a02019-08-13 15:44:55 -070092 self.addCleanup(patcher.stop)
93 self.testbed = testbed.Testbed()
94 self.testbed.activate()
95 self.addCleanup(self.testbed.deactivate)
96 self.testbed.init_taskqueue_stub(
97 root_path=os.path.join(os.path.dirname(__file__)))
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -070098 self.taskqueue_stub = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)
Xinan Lin9e4917d2019-11-04 10:58:47 -080099 self.queue = taskqueue.Queue(task_executor.SUITES_QUEUE)
Xinan Linc54a7462020-04-17 15:39:01 -0700100 _mock_application_id = mock.patch('constants.application_id')
101 self.mock_application_id = _mock_application_id.start()
102 self.addCleanup(_mock_application_id.stop)
103 self.mock_application_id.return_value = constants.AppID.PROD_APP
Xinan Lin3ba18a02019-08-13 15:44:55 -0700104
Xinan Lin9e4917d2019-11-04 10:58:47 -0800105 def testFormTestPlatformMultiRequestSuccessfully(self):
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700106 suite_kwargs = [_get_suite_params(board=b) for b in ['foo', 'goo']]
Xinan Lin9e4917d2019-11-04 10:58:47 -0800107 for suite in suite_kwargs:
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700108 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
Xinan Lin3ba18a02019-08-13 15:44:55 -0700109
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700110 tasks = self.queue.lease_tasks_by_tag(
111 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800112
113 self.client.multirequest_run(tasks, 'fake_suite')
Prathmesh Prabhu9a8060a2019-08-30 14:13:23 -0700114 self._assert_bb_client_called()
Xinan Lin9e4917d2019-11-04 10:58:47 -0800115
116 request_map = self._extract_request_map_from_bb_client()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700117 request_list = [
118 _struct_pb2_to_request_pb2(v) for v in request_map.values()
119 ]
Xinan Lin9e4917d2019-11-04 10:58:47 -0800120 request_list.sort(
121 key=lambda req: req.params.software_attributes.build_target.name)
122 for i, req in enumerate(request_list):
123 self.assertEqual(req.params.scheduling.priority, 10)
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700124 self.assertEqual(
125 req.params.scheduling.managed_pool,
126 request_pb2.Request.Params.Scheduling.MANAGED_POOL_SUITES)
Prathmesh Prabhu9adfa632020-02-21 10:39:24 -0800127 # Don't check for exact max timeout to stay DRY.
128 # But do check that the maximum is sane.
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700129 self.assertLess(req.params.time.maximum_duration.seconds,
130 2 * 24 * 60 * 60)
Aviv Keshetc679faf2019-11-27 17:52:50 -0800131 gs_url = ('gs://chromeos-image-archive/%s' %
132 suite_kwargs[i]['test_source_build'])
133 self.assertEqual(req.params.metadata.test_metadata_url, gs_url)
134 self.assertEqual(req.params.metadata.debug_symbols_archive_url, gs_url)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800135 self.assertEqual(req.test_plan.suite[0].name, suite_kwargs[i]['suite'])
136 self.assertEqual(req.params.software_attributes.build_target.name,
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700137 suite_kwargs[i]['board'])
Xinan Lin9e4917d2019-11-04 10:58:47 -0800138
139 def testPoolName(self):
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700140 suite_kwargs = [_get_suite_params(board=b) for b in ['foo', 'goo', 'hoo']]
Xinan Lin9e4917d2019-11-04 10:58:47 -0800141 suite_kwargs[0]['pool'] = 'cts'
142 suite_kwargs[1]['pool'] = 'wifi'
Xinan Lin9e4917d2019-11-04 10:58:47 -0800143 for suite in suite_kwargs:
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700144 task_executor.push(task_executor.SUITES_QUEUE, tag='some_suite', **suite)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800145
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700146 tasks = self.queue.lease_tasks_by_tag(
147 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800148 self.client.multirequest_run(tasks, 'some_suite')
149 self._assert_bb_client_called()
150 request_map = self._extract_request_map_from_bb_client()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700151 request_list = [
152 _struct_pb2_to_request_pb2(v) for v in request_map.values()
153 ]
Xinan Lin9e4917d2019-11-04 10:58:47 -0800154 self.assertEqual(len(request_list), 3)
155 request_list.sort(
156 key=lambda req: req.params.software_attributes.build_target.name)
157 self.assertEqual(request_list[0].params.scheduling.managed_pool,
Alex Zamorzaevc1935602019-08-28 14:37:35 -0700158 request_pb2.Request.Params.Scheduling.MANAGED_POOL_CTS)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800159 self.assertEqual(request_list[1].params.scheduling.unmanaged_pool,
160 suite_kwargs[1]['pool'])
Prathmesh Prabhu88409f62019-08-30 14:32:28 -0700161
Xinan Lin3ba18a02019-08-13 15:44:55 -0700162 def testNoFinalBuild(self):
Xinan Lin9e4917d2019-11-04 10:58:47 -0800163 suite_kwargs = _get_suite_params()
164 suite_kwargs[build_lib.BuildVersionKey.CROS_VERSION] = None
165 suite_kwargs[build_lib.BuildVersionKey.ANDROID_BUILD_VERSION] = None
166 suite_kwargs[build_lib.BuildVersionKey.TESTBED_BUILD_VERSION] = None
167
168 task_executor.push(task_executor.SUITES_QUEUE,
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700169 tag='fake_suite',
170 **suite_kwargs)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800171
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700172 tasks = self.queue.lease_tasks_by_tag(
173 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800174 executed = self.client.multirequest_run(tasks, 'fake_suite')
175 self.assertEqual(len(executed), 0)
Prathmesh Prabhu9a8060a2019-08-30 14:13:23 -0700176 self._assert_bb_client_not_called()
Xinan Lin3ba18a02019-08-13 15:44:55 -0700177
Xinan Lin4757d6f2020-03-24 22:20:31 -0700178 def testShouldSetQsAccount(self):
179 suite_kwargs = _get_suite_params(pool='foo')
180 suite_kwargs['priority'] = '30'
181 suite_kwargs['qs_account'] = 'qs-foo'
182
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700183 task_executor.push(task_executor.SUITES_QUEUE,
184 tag='fake_suite',
185 **suite_kwargs)
Xinan Lin4757d6f2020-03-24 22:20:31 -0700186
187 tasks = self.queue.lease_tasks_by_tag(
188 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
189 self.client.multirequest_run(tasks, 'fake_suite')
190 request_map = self._extract_request_map_from_bb_client()
191 self._assert_bb_client_called()
192 self.assertEqual(len(request_map), 1)
193 req = _struct_pb2_to_request_pb2(request_map['fake_board_fake_model'])
194 self.assertEqual(req.params.scheduling.qs_account, 'qs-foo')
195 # If qs_account is set, priority should be 0, the default value
196 # defined by proto3.
197 self.assertEqual(req.params.scheduling.priority, 0)
198
199 def testShouldNotSetQsAccountForManagedPool(self):
200 suite_kwargs = _get_suite_params(pool='DUT_POOL_SUITES')
201 suite_kwargs['qs_account'] = 'qs-foo'
202
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700203 task_executor.push(task_executor.SUITES_QUEUE,
204 tag='fake_suite',
205 **suite_kwargs)
Xinan Lin4757d6f2020-03-24 22:20:31 -0700206
207 tasks = self.queue.lease_tasks_by_tag(
208 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
209 self.client.multirequest_run(tasks, 'fake_suite')
210 request_map = self._extract_request_map_from_bb_client()
211 self._assert_bb_client_called()
212 self.assertEqual(len(request_map), 1)
213 req = _struct_pb2_to_request_pb2(request_map['fake_board_fake_model'])
214 self.assertEqual(req.params.scheduling.qs_account, '')
215
Xinan Lin9e4917d2019-11-04 10:58:47 -0800216 def testShouldSetPriority(self):
217 suite_kwargs = _get_suite_params()
218 suite_kwargs['priority'] = '30'
Xinan Linfb63d572019-09-24 15:49:04 -0700219
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700220 task_executor.push(task_executor.SUITES_QUEUE,
221 tag='fake_suite',
222 **suite_kwargs)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800223
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700224 tasks = self.queue.lease_tasks_by_tag(
225 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800226 self.client.multirequest_run(tasks, 'fake_suite')
227 request_map = self._extract_request_map_from_bb_client()
Xinan Linfb63d572019-09-24 15:49:04 -0700228 self._assert_bb_client_called()
Xinan Lin9e4917d2019-11-04 10:58:47 -0800229 self.assertEqual(len(request_map), 1)
230 req = _struct_pb2_to_request_pb2(request_map['fake_board_fake_model'])
231 self.assertEqual(req.params.scheduling.priority, 30)
232
Xinan Linf1df4fc2020-04-22 21:31:48 -0700233 def testIgnorePriorityInPoolSuites(self):
234 suite_kwargs = _get_suite_params()
235 suite_kwargs['priority'] = '100'
236 suite_kwargs['pool'] = 'suites'
237
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700238 task_executor.push(task_executor.SUITES_QUEUE,
239 tag='fake_suite',
240 **suite_kwargs)
Xinan Linf1df4fc2020-04-22 21:31:48 -0700241
242 tasks = self.queue.lease_tasks_by_tag(
243 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
244 self.client.multirequest_run(tasks, 'fake_suite')
245 request_map = self._extract_request_map_from_bb_client()
246 self._assert_bb_client_called()
247 self.assertEqual(len(request_map), 1)
248 req = _struct_pb2_to_request_pb2(request_map['fake_board_fake_model'])
249 # In pool "suites", the priority should be empty or zero, the default value
250 # defined by proto3.
251 self.assertEqual(req.params.scheduling.priority, 0)
252
Xinan Lin9e4917d2019-11-04 10:58:47 -0800253 def testRequestWithInvalidTags(self):
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700254 suite_kwargs = [_get_suite_params(board=b) for b in ['foo', 'foo']]
Xinan Lin9e4917d2019-11-04 10:58:47 -0800255
256 # test request having None String Tag
257 suite_kwargs[0]['model'] = 'None'
258
259 # test request having None Tag
260 suite_kwargs[1]['model'] = None
261
262 for suite in suite_kwargs:
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700263 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800264
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700265 tasks = self.queue.lease_tasks_by_tag(
266 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800267 executed = self.client.multirequest_run(tasks, 'fake_suite')
268 self.assertEqual(len(executed), 2)
269
270 self._assert_bb_client_called()
271 request_map = self._extract_request_map_from_bb_client()
272 self.assertEqual(len(request_map), 2)
Xinan Linfb63d572019-09-24 15:49:04 -0700273 want = {
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700274 'suite:fake_suite',
275 'build:fake_cros_build',
276 'label-pool:DUT_POOL_SUITES',
277 'label-board:foo',
Xinan Linfb63d572019-09-24 15:49:04 -0700278 }
Xinan Lin9e4917d2019-11-04 10:58:47 -0800279 want_bb = {
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700280 'suite:fake_suite',
Xinan Lin9e4917d2019-11-04 10:58:47 -0800281 }
282 for req_name in ['foo', 'foo_1']:
283 req = _struct_pb2_to_request_pb2(request_map[req_name])
284 req_tags = set([t for t in req.params.decorations.tags])
285 self.assertEqual(want, req_tags)
286 bb_tags = set([t for t in self._extract_tags_from_bb_client()])
287 self.assertEqual(want_bb, bb_tags)
Xinan Linfb63d572019-09-24 15:49:04 -0700288
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700289 def testRequestTags(self):
Xinan Lin9e4917d2019-11-04 10:58:47 -0800290 suite_kwargs = _get_suite_params(
291 board='fake_board',
292 model='fake_model',
293 pool='DUT_POOL_SUITES',
294 suite='fake_suite',
295 cros_build='fake_build',
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700296 )
Xinan Lin9e4917d2019-11-04 10:58:47 -0800297 task_executor.push(task_executor.SUITES_QUEUE,
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700298 tag='fake_suite',
299 **suite_kwargs)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800300
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700301 tasks = self.queue.lease_tasks_by_tag(
302 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800303 self.client.multirequest_run(tasks, 'fake_suite')
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700304 self._assert_bb_client_called()
305 want = {
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700306 'label-board:fake_board',
307 'label-model:fake_model',
308 'label-pool:DUT_POOL_SUITES',
309 'suite:fake_suite',
310 'build:fake_build',
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700311 }
Xinan Lin9e4917d2019-11-04 10:58:47 -0800312 want_bb = {
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700313 'suite:fake_suite',
Xinan Lin9e4917d2019-11-04 10:58:47 -0800314 }
315 request_map = self._extract_request_map_from_bb_client()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700316 request_list = [
317 _struct_pb2_to_request_pb2(v) for v in request_map.values()
318 ]
Xinan Lin9e4917d2019-11-04 10:58:47 -0800319 self.assertEqual(len(request_list), 1)
320 req = request_list[0]
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700321 req_tags = set([t for t in req.params.decorations.tags])
322 self.assertEqual(want, req_tags)
323 bb_tags = set([t for t in self._extract_tags_from_bb_client()])
Xinan Lin9e4917d2019-11-04 10:58:47 -0800324 self.assertEqual(want_bb, bb_tags)
Xinan Lin3ba18a02019-08-13 15:44:55 -0700325
Xinan Linba3b9322020-04-24 15:08:12 -0700326 def testRequestUserDefinedDimensions(self):
327 suite_kwargs = _get_suite_params(
328 board='fake_board',
329 model='fake_model',
330 pool='DUT_POOL_SUITES',
331 suite='fake_suite',
332 cros_build='fake_build',
333 )
334 suite_kwargs['dimensions'] = 'label-wifi:foo,label-bt:hoo'
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700335 task_executor.push(task_executor.SUITES_QUEUE,
336 tag='fake_suite',
337 **suite_kwargs)
Xinan Linba3b9322020-04-24 15:08:12 -0700338
339 tasks = self.queue.lease_tasks_by_tag(
340 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
341 self.client.multirequest_run(tasks, 'fake_suite')
342 self._assert_bb_client_called()
343 request_map = self._extract_request_map_from_bb_client()
344 request_list = [
345 _struct_pb2_to_request_pb2(v) for v in request_map.values()
346 ]
347 self.assertEqual(len(request_list), 1)
348 req = request_list[0]
349 free_dimensions = req.params.freeform_attributes.swarming_dimensions
350 self.assertTrue(len(free_dimensions), 2)
351 self.assertTrue('label-wifi:foo' in free_dimensions)
352 self.assertTrue('label-bt:hoo' in free_dimensions)
353
354 def testRequestInvalidUserDefinedDimensions(self):
355 suite_kwargs = _get_suite_params(
356 board='fake_board',
357 model='fake_model',
358 pool='DUT_POOL_SUITES',
359 suite='fake_suite',
360 cros_build='fake_build',
361 )
362 suite_kwargs['dimensions'] = 'invalid-dimension'
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700363 task_executor.push(task_executor.SUITES_QUEUE,
364 tag='fake_suite',
365 **suite_kwargs)
Xinan Linba3b9322020-04-24 15:08:12 -0700366
367 tasks = self.queue.lease_tasks_by_tag(
368 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
369 self.client.multirequest_run(tasks, 'fake_suite')
370 self._assert_bb_client_not_called()
371
Prathmesh Prabhu73801e12019-08-30 14:09:31 -0700372 def _get_client(self, addr):
373 self.assertEqual(ADDR, addr)
374 return self.fake_client
375
Prathmesh Prabhu9a8060a2019-08-30 14:13:23 -0700376 def _assert_bb_client_called(self):
377 self.assertEqual(len(self.fake_client.called_with_requests), 1)
378
379 def _assert_bb_client_not_called(self):
380 self.assertEqual(len(self.fake_client.called_with_requests), 0)
381
Xinan Lin9e4917d2019-11-04 10:58:47 -0800382 def _extract_request_map_from_bb_client(self):
383 return self.fake_client.called_with_requests[0].properties['requests']
Prathmesh Prabhu6348ea82019-08-30 14:15:21 -0700384
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700385 def _extract_tags_from_bb_client(self):
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700386 return [
387 '%s:%s' % (t.key, t.value)
388 for t in self.fake_client.called_with_requests[0].tags
389 ]
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700390
Prathmesh Prabhu9a8060a2019-08-30 14:13:23 -0700391
Xinan Lindf0698a2020-02-05 22:38:11 -0800392class TestTaskExecutions(TestPlatformClientTestCase):
Xinan Lindf0698a2020-02-05 22:38:11 -0800393 def setUp(self):
394 super(TestTaskExecutions, self).setUp()
395 _mock_bq_client = mock.patch('rest_client.BigqueryRestClient')
396 mock_bq_client = _mock_bq_client.start()
397 self.addCleanup(_mock_bq_client.stop)
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700398 self.mock_bq_client = FakeBigqueryRestClient(None,
399 project='proj',
400 dataset='dataset',
401 table='foo')
Xinan Lindf0698a2020-02-05 22:38:11 -0800402 mock_bq_client.return_value = self.mock_bq_client
403
404 def testRecordSuccessfulTaskExecution(self):
Xinan Lin083ba8f2020-02-06 13:55:18 -0800405 suite = _get_suite_params(board='foo')
406 suite['task_id'] = FAKE_UUID
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700407 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
408 tasks = self.queue.lease_tasks_by_tag(
409 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lindf0698a2020-02-05 22:38:11 -0800410 self.client.multirequest_run(tasks, 'fake_suite')
411 self._assert_bb_client_called()
412 task_execution = json_format.Parse(
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700413 json.dumps(self.mock_bq_client.rows[0]['json']),
414 analytics_pb2.ExecutionTask())
Xinan Lindf0698a2020-02-05 22:38:11 -0800415 self.assertEqual(task_execution.queued_task_id, FAKE_UUID)
416 self.assertEqual(task_execution.response.ctp_build_id, str(FAKE_BUILD_ID))
417 self.assertEqual(task_execution.error.error_message, '')
418
419 def testRecordFailedTaskExecution(self):
Xinan Lin083ba8f2020-02-06 13:55:18 -0800420 suite = _get_suite_params(board='foo')
421 suite['task_id'] = FAKE_UUID
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700422 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
Xinan Lindf0698a2020-02-05 22:38:11 -0800423 self.fake_client.error = "cros_test_platform error"
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700424 tasks = self.queue.lease_tasks_by_tag(
425 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lindf0698a2020-02-05 22:38:11 -0800426 self.client.multirequest_run(tasks, 'fake_suite')
427 self._assert_bb_client_called()
428 task_execution = json_format.Parse(
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700429 json.dumps(self.mock_bq_client.rows[0]['json']),
430 analytics_pb2.ExecutionTask())
Xinan Lindf0698a2020-02-05 22:38:11 -0800431 self.assertEqual(task_execution.queued_task_id, FAKE_UUID)
432 self.assertEqual(task_execution.response.ctp_build_id, '')
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700433 self.assertEqual(task_execution.error.error_message,
434 self.fake_client.error)
Xinan Lindf0698a2020-02-05 22:38:11 -0800435
Xinan Lin083ba8f2020-02-06 13:55:18 -0800436 def testIgnoreTaskWithoutTaskID(self):
437 suite = _get_suite_params(board='foo')
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700438 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
Xinan Lin083ba8f2020-02-06 13:55:18 -0800439 self.fake_client.error = "cros_test_platform error"
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700440 tasks = self.queue.lease_tasks_by_tag(
441 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin083ba8f2020-02-06 13:55:18 -0800442 self.client.multirequest_run(tasks, 'fake_suite')
443 self._assert_bb_client_called()
444 self.assertEqual(len(self.mock_bq_client.rows), 0)
445
Xinan Linc54a7462020-04-17 15:39:01 -0700446 def testIgnoreSuiteForUnamanagedPoolInStaging(self):
447 self.mock_application_id.return_value = 'suite-scheduler-staging'
448 suite = _get_suite_params(pool='foo')
449 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
450 self.fake_client.error = "cros_test_platform error"
451 tasks = self.queue.lease_tasks_by_tag(
452 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
453 self.client.multirequest_run(tasks, 'fake_suite')
454 self._assert_bb_client_not_called()
455
Xinan Lindf0698a2020-02-05 22:38:11 -0800456
Xinan Lin3ba18a02019-08-13 15:44:55 -0700457def _struct_pb2_to_request_pb2(struct_pb2):
458 """Transform google struct proto to test_platform_request.
459
460 Args:
461 struct_pb2: A struct_pb2 instance.
462
463 Returns:
464 A request_pb2 instance.
465 """
466 json = json_format.MessageToJson(struct_pb2)
467 return json_format.Parse(json, request_pb2.Request())