blob: f5865c80efd192ea5ffddd64cec02ca3b681fc54 [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
Prathmesh Prabhu46156ff2020-06-20 00:18:52 -0700178 def testShouldSetQsAccountForUnmanagedPool(self):
Xinan Lin4757d6f2020-03-24 22:20:31 -0700179 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
Prathmesh Prabhu46156ff2020-06-20 00:18:52 -0700199 def testShouldSetQsAccountForManagedPool(self):
Xinan Lin4757d6f2020-03-24 22:20:31 -0700200 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'])
Prathmesh Prabhu46156ff2020-06-20 00:18:52 -0700214 self.assertEqual(req.params.scheduling.qs_account, 'qs-foo')
215 # If qs_account is set, priority should be 0, the default value
216 # defined by proto3.
217 self.assertEqual(req.params.scheduling.priority, 0)
Xinan Lin4757d6f2020-03-24 22:20:31 -0700218
Xinan Lin9e4917d2019-11-04 10:58:47 -0800219 def testShouldSetPriority(self):
220 suite_kwargs = _get_suite_params()
221 suite_kwargs['priority'] = '30'
Xinan Linfb63d572019-09-24 15:49:04 -0700222
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700223 task_executor.push(task_executor.SUITES_QUEUE,
224 tag='fake_suite',
225 **suite_kwargs)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800226
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700227 tasks = self.queue.lease_tasks_by_tag(
228 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800229 self.client.multirequest_run(tasks, 'fake_suite')
230 request_map = self._extract_request_map_from_bb_client()
Xinan Linfb63d572019-09-24 15:49:04 -0700231 self._assert_bb_client_called()
Xinan Lin9e4917d2019-11-04 10:58:47 -0800232 self.assertEqual(len(request_map), 1)
233 req = _struct_pb2_to_request_pb2(request_map['fake_board_fake_model'])
234 self.assertEqual(req.params.scheduling.priority, 30)
235
Xinan Linf1df4fc2020-04-22 21:31:48 -0700236 def testIgnorePriorityInPoolSuites(self):
237 suite_kwargs = _get_suite_params()
238 suite_kwargs['priority'] = '100'
239 suite_kwargs['pool'] = 'suites'
240
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700241 task_executor.push(task_executor.SUITES_QUEUE,
242 tag='fake_suite',
243 **suite_kwargs)
Xinan Linf1df4fc2020-04-22 21:31:48 -0700244
245 tasks = self.queue.lease_tasks_by_tag(
246 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
247 self.client.multirequest_run(tasks, 'fake_suite')
248 request_map = self._extract_request_map_from_bb_client()
249 self._assert_bb_client_called()
250 self.assertEqual(len(request_map), 1)
251 req = _struct_pb2_to_request_pb2(request_map['fake_board_fake_model'])
252 # In pool "suites", the priority should be empty or zero, the default value
253 # defined by proto3.
254 self.assertEqual(req.params.scheduling.priority, 0)
255
Xinan Lin9e4917d2019-11-04 10:58:47 -0800256 def testRequestWithInvalidTags(self):
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700257 suite_kwargs = [_get_suite_params(board=b) for b in ['foo', 'foo']]
Xinan Lin9e4917d2019-11-04 10:58:47 -0800258
259 # test request having None String Tag
260 suite_kwargs[0]['model'] = 'None'
261
262 # test request having None Tag
263 suite_kwargs[1]['model'] = None
264
265 for suite in suite_kwargs:
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700266 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800267
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700268 tasks = self.queue.lease_tasks_by_tag(
269 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800270 executed = self.client.multirequest_run(tasks, 'fake_suite')
271 self.assertEqual(len(executed), 2)
272
273 self._assert_bb_client_called()
274 request_map = self._extract_request_map_from_bb_client()
275 self.assertEqual(len(request_map), 2)
Xinan Linfb63d572019-09-24 15:49:04 -0700276 want = {
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700277 'suite:fake_suite',
278 'build:fake_cros_build',
279 'label-pool:DUT_POOL_SUITES',
280 'label-board:foo',
Xinan Linfb63d572019-09-24 15:49:04 -0700281 }
Xinan Lin9e4917d2019-11-04 10:58:47 -0800282 want_bb = {
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700283 'suite:fake_suite',
Xinan Lin9e4917d2019-11-04 10:58:47 -0800284 }
285 for req_name in ['foo', 'foo_1']:
286 req = _struct_pb2_to_request_pb2(request_map[req_name])
287 req_tags = set([t for t in req.params.decorations.tags])
288 self.assertEqual(want, req_tags)
289 bb_tags = set([t for t in self._extract_tags_from_bb_client()])
290 self.assertEqual(want_bb, bb_tags)
Xinan Linfb63d572019-09-24 15:49:04 -0700291
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700292 def testRequestTags(self):
Xinan Lin9e4917d2019-11-04 10:58:47 -0800293 suite_kwargs = _get_suite_params(
294 board='fake_board',
295 model='fake_model',
296 pool='DUT_POOL_SUITES',
297 suite='fake_suite',
298 cros_build='fake_build',
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700299 )
Xinan Lin9e4917d2019-11-04 10:58:47 -0800300 task_executor.push(task_executor.SUITES_QUEUE,
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700301 tag='fake_suite',
302 **suite_kwargs)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800303
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700304 tasks = self.queue.lease_tasks_by_tag(
305 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800306 self.client.multirequest_run(tasks, 'fake_suite')
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700307 self._assert_bb_client_called()
308 want = {
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700309 'label-board:fake_board',
310 'label-model:fake_model',
311 'label-pool:DUT_POOL_SUITES',
312 'suite:fake_suite',
313 'build:fake_build',
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700314 }
Xinan Lin9e4917d2019-11-04 10:58:47 -0800315 want_bb = {
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700316 'suite:fake_suite',
Xinan Lin9e4917d2019-11-04 10:58:47 -0800317 }
318 request_map = self._extract_request_map_from_bb_client()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700319 request_list = [
320 _struct_pb2_to_request_pb2(v) for v in request_map.values()
321 ]
Xinan Lin9e4917d2019-11-04 10:58:47 -0800322 self.assertEqual(len(request_list), 1)
323 req = request_list[0]
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700324 req_tags = set([t for t in req.params.decorations.tags])
325 self.assertEqual(want, req_tags)
326 bb_tags = set([t for t in self._extract_tags_from_bb_client()])
Xinan Lin9e4917d2019-11-04 10:58:47 -0800327 self.assertEqual(want_bb, bb_tags)
Xinan Lin3ba18a02019-08-13 15:44:55 -0700328
Xinan Linba3b9322020-04-24 15:08:12 -0700329 def testRequestUserDefinedDimensions(self):
330 suite_kwargs = _get_suite_params(
331 board='fake_board',
332 model='fake_model',
333 pool='DUT_POOL_SUITES',
334 suite='fake_suite',
335 cros_build='fake_build',
336 )
337 suite_kwargs['dimensions'] = 'label-wifi:foo,label-bt:hoo'
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700338 task_executor.push(task_executor.SUITES_QUEUE,
339 tag='fake_suite',
340 **suite_kwargs)
Xinan Linba3b9322020-04-24 15:08:12 -0700341
342 tasks = self.queue.lease_tasks_by_tag(
343 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
344 self.client.multirequest_run(tasks, 'fake_suite')
345 self._assert_bb_client_called()
346 request_map = self._extract_request_map_from_bb_client()
347 request_list = [
348 _struct_pb2_to_request_pb2(v) for v in request_map.values()
349 ]
350 self.assertEqual(len(request_list), 1)
351 req = request_list[0]
352 free_dimensions = req.params.freeform_attributes.swarming_dimensions
353 self.assertTrue(len(free_dimensions), 2)
354 self.assertTrue('label-wifi:foo' in free_dimensions)
355 self.assertTrue('label-bt:hoo' in free_dimensions)
356
357 def testRequestInvalidUserDefinedDimensions(self):
358 suite_kwargs = _get_suite_params(
359 board='fake_board',
360 model='fake_model',
361 pool='DUT_POOL_SUITES',
362 suite='fake_suite',
363 cros_build='fake_build',
364 )
365 suite_kwargs['dimensions'] = 'invalid-dimension'
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700366 task_executor.push(task_executor.SUITES_QUEUE,
367 tag='fake_suite',
368 **suite_kwargs)
Xinan Linba3b9322020-04-24 15:08:12 -0700369
370 tasks = self.queue.lease_tasks_by_tag(
371 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
372 self.client.multirequest_run(tasks, 'fake_suite')
373 self._assert_bb_client_not_called()
374
Prathmesh Prabhu73801e12019-08-30 14:09:31 -0700375 def _get_client(self, addr):
376 self.assertEqual(ADDR, addr)
377 return self.fake_client
378
Prathmesh Prabhu9a8060a2019-08-30 14:13:23 -0700379 def _assert_bb_client_called(self):
380 self.assertEqual(len(self.fake_client.called_with_requests), 1)
381
382 def _assert_bb_client_not_called(self):
383 self.assertEqual(len(self.fake_client.called_with_requests), 0)
384
Xinan Lin9e4917d2019-11-04 10:58:47 -0800385 def _extract_request_map_from_bb_client(self):
386 return self.fake_client.called_with_requests[0].properties['requests']
Prathmesh Prabhu6348ea82019-08-30 14:15:21 -0700387
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700388 def _extract_tags_from_bb_client(self):
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700389 return [
390 '%s:%s' % (t.key, t.value)
391 for t in self.fake_client.called_with_requests[0].tags
392 ]
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700393
Prathmesh Prabhu9a8060a2019-08-30 14:13:23 -0700394
Xinan Lindf0698a2020-02-05 22:38:11 -0800395class TestTaskExecutions(TestPlatformClientTestCase):
Xinan Lindf0698a2020-02-05 22:38:11 -0800396 def setUp(self):
397 super(TestTaskExecutions, self).setUp()
398 _mock_bq_client = mock.patch('rest_client.BigqueryRestClient')
399 mock_bq_client = _mock_bq_client.start()
400 self.addCleanup(_mock_bq_client.stop)
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700401 self.mock_bq_client = FakeBigqueryRestClient(None,
402 project='proj',
403 dataset='dataset',
404 table='foo')
Xinan Lindf0698a2020-02-05 22:38:11 -0800405 mock_bq_client.return_value = self.mock_bq_client
406
407 def testRecordSuccessfulTaskExecution(self):
Xinan Lin083ba8f2020-02-06 13:55:18 -0800408 suite = _get_suite_params(board='foo')
409 suite['task_id'] = FAKE_UUID
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700410 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
411 tasks = self.queue.lease_tasks_by_tag(
412 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lindf0698a2020-02-05 22:38:11 -0800413 self.client.multirequest_run(tasks, 'fake_suite')
414 self._assert_bb_client_called()
415 task_execution = json_format.Parse(
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700416 json.dumps(self.mock_bq_client.rows[0]['json']),
417 analytics_pb2.ExecutionTask())
Xinan Lindf0698a2020-02-05 22:38:11 -0800418 self.assertEqual(task_execution.queued_task_id, FAKE_UUID)
419 self.assertEqual(task_execution.response.ctp_build_id, str(FAKE_BUILD_ID))
420 self.assertEqual(task_execution.error.error_message, '')
421
422 def testRecordFailedTaskExecution(self):
Xinan Lin083ba8f2020-02-06 13:55:18 -0800423 suite = _get_suite_params(board='foo')
424 suite['task_id'] = FAKE_UUID
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700425 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
Xinan Lindf0698a2020-02-05 22:38:11 -0800426 self.fake_client.error = "cros_test_platform error"
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700427 tasks = self.queue.lease_tasks_by_tag(
428 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lindf0698a2020-02-05 22:38:11 -0800429 self.client.multirequest_run(tasks, 'fake_suite')
430 self._assert_bb_client_called()
431 task_execution = json_format.Parse(
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700432 json.dumps(self.mock_bq_client.rows[0]['json']),
433 analytics_pb2.ExecutionTask())
Xinan Lindf0698a2020-02-05 22:38:11 -0800434 self.assertEqual(task_execution.queued_task_id, FAKE_UUID)
435 self.assertEqual(task_execution.response.ctp_build_id, '')
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700436 self.assertEqual(task_execution.error.error_message,
437 self.fake_client.error)
Xinan Lindf0698a2020-02-05 22:38:11 -0800438
Xinan Lin083ba8f2020-02-06 13:55:18 -0800439 def testIgnoreTaskWithoutTaskID(self):
440 suite = _get_suite_params(board='foo')
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700441 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
Xinan Lin083ba8f2020-02-06 13:55:18 -0800442 self.fake_client.error = "cros_test_platform error"
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700443 tasks = self.queue.lease_tasks_by_tag(
444 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin083ba8f2020-02-06 13:55:18 -0800445 self.client.multirequest_run(tasks, 'fake_suite')
446 self._assert_bb_client_called()
447 self.assertEqual(len(self.mock_bq_client.rows), 0)
448
Xinan Linc54a7462020-04-17 15:39:01 -0700449 def testIgnoreSuiteForUnamanagedPoolInStaging(self):
450 self.mock_application_id.return_value = 'suite-scheduler-staging'
451 suite = _get_suite_params(pool='foo')
452 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
453 self.fake_client.error = "cros_test_platform error"
454 tasks = self.queue.lease_tasks_by_tag(
455 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
456 self.client.multirequest_run(tasks, 'fake_suite')
457 self._assert_bb_client_not_called()
458
Xinan Lindf0698a2020-02-05 22:38:11 -0800459
Xinan Lin3ba18a02019-08-13 15:44:55 -0700460def _struct_pb2_to_request_pb2(struct_pb2):
461 """Transform google struct proto to test_platform_request.
462
463 Args:
464 struct_pb2: A struct_pb2 instance.
465
466 Returns:
467 A request_pb2 instance.
468 """
469 json = json_format.MessageToJson(struct_pb2)
470 return json_format.Parse(json, request_pb2.Request())