blob: 2fbd21866fcebbf18e3e4068bc49fe21342c13da [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
Jacob Kopczynskicefd50c2020-07-31 10:05:10 -070017from chromite.api.gen.test_platform import request_pb2 as ctp_request
Xinan Lindf0698a2020-02-05 22:38:11 -080018from chromite.api.gen.test_platform.suite_scheduler import analytics_pb2
Sean McAllisteraf32bfb2021-07-19 09:20:17 -060019from chromite.third_party.infra_libs.buildbucket.proto import build_pb2
Xinan Lin9e4917d2019-11-04 10:58:47 -080020from google.appengine.api import taskqueue
Xinan Lin3ba18a02019-08-13 15:44:55 -070021from google.appengine.ext import testbed
Sean McAllister66bf7e92021-07-16 18:46:04 +000022from google.protobuf import json_format
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',
Xinan Lin1516edb2020-07-05 23:13:54 -070032 pool='MANAGED_POOL_QUOTA',
Taylor Clarke12ec9a2021-02-18 22:22:19 +000033 cros_build='fake_cros_build',
34 analytics_name=None):
Xinan Lin3ba18a02019-08-13 15:44:55 -070035 return {
Prathmesh Prabhu2382a182019-09-07 21:18:10 -070036 'suite': suite,
37 'board': board,
38 'model': model,
39 build_lib.BuildVersionKey.CROS_VERSION: cros_build,
Xinan Lin3ba18a02019-08-13 15:44:55 -070040 build_lib.BuildVersionKey.FW_RW_VERSION: 'fake_firmware_rw_build',
41 build_lib.BuildVersionKey.FW_RO_VERSION: 'fake_firmware_ro_build',
42 build_lib.BuildVersionKey.ANDROID_BUILD_VERSION: 'fake_android_build',
43 build_lib.BuildVersionKey.TESTBED_BUILD_VERSION: 'fake_testbed_build',
Brigit Rossbachbb080912020-11-18 13:52:17 -070044 'firmware_ro_version': 'fake_firmware_ro_build',
Xinan Lin3ba18a02019-08-13 15:44:55 -070045 'num': 1,
Prathmesh Prabhu2382a182019-09-07 21:18:10 -070046 'pool': pool,
Xinan Lin3ba18a02019-08-13 15:44:55 -070047 'priority': 10,
Xinan Lin6e097382019-08-27 18:43:35 -070048 'timeout': 12,
49 'timeout_mins': 4320,
50 'max_runtime_mins': 4320,
Xinan Lin3ba18a02019-08-13 15:44:55 -070051 'no_wait_for_results': True,
52 'test_source_build': 'fake_test_source_build',
53 'job_retry': False,
54 'no_delay': False,
55 'force': False,
56 'run_prod_code': True,
57 'is_skylab': False,
Taylor Clarke12ec9a2021-02-18 22:22:19 +000058 'analytics_name': analytics_name,
Garry Wang111a26f2021-07-23 15:25:14 -070059 'secondary_targets': ''
Xinan Lin3ba18a02019-08-13 15:44:55 -070060 }
61
62
63class FakeTestPlatformClient(object):
Xinan Lin3ba18a02019-08-13 15:44:55 -070064 def __init__(self, test):
65 self._test = test
66 self.called_with_requests = []
Xinan Lindf0698a2020-02-05 22:38:11 -080067 self.error = None
Xinan Lin3ba18a02019-08-13 15:44:55 -070068
Xinan Lin57e2d962020-03-30 17:24:53 -070069 def ScheduleBuild(self, req, credentials=None, timeout=10):
Xinan Lin3ba18a02019-08-13 15:44:55 -070070 self.called_with_requests.append(req)
Xinan Lindf0698a2020-02-05 22:38:11 -080071 if self.error:
72 return self.error
73 return build_pb2.Build(id=FAKE_BUILD_ID)
74
75
76class FakeBigqueryRestClient(object):
Xinan Lindf0698a2020-02-05 22:38:11 -080077 def __init__(self, rest_client, project=None, dataset=None, table=None):
78 """Initialize the mock class."""
79 self.table = table
80 self.rows = []
81
82 def insert(self, rows):
83 self.rows = rows
84 return True
Xinan Lin3ba18a02019-08-13 15:44:55 -070085
86
87class TestPlatformClientTestCase(unittest.TestCase):
Xinan Lin3ba18a02019-08-13 15:44:55 -070088 def setUp(self):
89 super(TestPlatformClientTestCase, self).setUp()
90 self.fake_client = FakeTestPlatformClient(self)
91 patcher = mock.patch('buildbucket._get_client',
92 return_value=self._get_client(ADDR))
93 patcher.start()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -070094 self.client = buildbucket.TestPlatformClient(ADDR, 'foo-proj',
95 'foo-bucket', 'foo-builder')
Xinan Lin3ba18a02019-08-13 15:44:55 -070096 self.addCleanup(patcher.stop)
97 self.testbed = testbed.Testbed()
98 self.testbed.activate()
99 self.addCleanup(self.testbed.deactivate)
100 self.testbed.init_taskqueue_stub(
101 root_path=os.path.join(os.path.dirname(__file__)))
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700102 self.taskqueue_stub = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800103 self.queue = taskqueue.Queue(task_executor.SUITES_QUEUE)
Xinan Linc54a7462020-04-17 15:39:01 -0700104 _mock_application_id = mock.patch('constants.application_id')
105 self.mock_application_id = _mock_application_id.start()
106 self.addCleanup(_mock_application_id.stop)
107 self.mock_application_id.return_value = constants.AppID.PROD_APP
Xinan Lin3ba18a02019-08-13 15:44:55 -0700108
Xinan Lin9e4917d2019-11-04 10:58:47 -0800109 def testFormTestPlatformMultiRequestSuccessfully(self):
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700110 suite_kwargs = [_get_suite_params(board=b) for b in ['foo', 'goo']]
Xinan Lin9e4917d2019-11-04 10:58:47 -0800111 for suite in suite_kwargs:
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700112 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
Xinan Lin3ba18a02019-08-13 15:44:55 -0700113
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700114 tasks = self.queue.lease_tasks_by_tag(
115 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800116
117 self.client.multirequest_run(tasks, 'fake_suite')
Prathmesh Prabhu9a8060a2019-08-30 14:13:23 -0700118 self._assert_bb_client_called()
Xinan Lin9e4917d2019-11-04 10:58:47 -0800119
120 request_map = self._extract_request_map_from_bb_client()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700121 request_list = [
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700122 _struct_to_ctp_request(v) for v in request_map.values()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700123 ]
Xinan Lin9e4917d2019-11-04 10:58:47 -0800124 request_list.sort(
125 key=lambda req: req.params.software_attributes.build_target.name)
126 for i, req in enumerate(request_list):
127 self.assertEqual(req.params.scheduling.priority, 10)
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700128 self.assertEqual(
129 req.params.scheduling.managed_pool,
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700130 ctp_request.Request.Params.Scheduling.MANAGED_POOL_QUOTA)
Prathmesh Prabhu9adfa632020-02-21 10:39:24 -0800131 # Don't check for exact max timeout to stay DRY.
132 # But do check that the maximum is sane.
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700133 self.assertLess(req.params.time.maximum_duration.seconds,
134 2 * 24 * 60 * 60)
Aviv Keshetc679faf2019-11-27 17:52:50 -0800135 gs_url = ('gs://chromeos-image-archive/%s' %
136 suite_kwargs[i]['test_source_build'])
137 self.assertEqual(req.params.metadata.test_metadata_url, gs_url)
138 self.assertEqual(req.params.metadata.debug_symbols_archive_url, gs_url)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800139 self.assertEqual(req.test_plan.suite[0].name, suite_kwargs[i]['suite'])
140 self.assertEqual(req.params.software_attributes.build_target.name,
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700141 suite_kwargs[i]['board'])
Xinan Lin9e4917d2019-11-04 10:58:47 -0800142
143 def testPoolName(self):
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700144 suite_kwargs = [_get_suite_params(board=b) for b in ['foo', 'goo', 'hoo']]
Xinan Lin1516edb2020-07-05 23:13:54 -0700145 suite_kwargs[0]['pool'] = 'MANAGED_POOL_CTS'
Xinan Lin9e4917d2019-11-04 10:58:47 -0800146 suite_kwargs[1]['pool'] = 'wifi'
Xinan Lin9e4917d2019-11-04 10:58:47 -0800147 for suite in suite_kwargs:
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700148 task_executor.push(task_executor.SUITES_QUEUE, tag='some_suite', **suite)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800149
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700150 tasks = self.queue.lease_tasks_by_tag(
151 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800152 self.client.multirequest_run(tasks, 'some_suite')
153 self._assert_bb_client_called()
154 request_map = self._extract_request_map_from_bb_client()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700155 request_list = [
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700156 _struct_to_ctp_request(v) for v in request_map.values()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700157 ]
Xinan Lin9e4917d2019-11-04 10:58:47 -0800158 self.assertEqual(len(request_list), 3)
159 request_list.sort(
160 key=lambda req: req.params.software_attributes.build_target.name)
161 self.assertEqual(request_list[0].params.scheduling.managed_pool,
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700162 ctp_request.Request.Params.Scheduling.MANAGED_POOL_CTS)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800163 self.assertEqual(request_list[1].params.scheduling.unmanaged_pool,
164 suite_kwargs[1]['pool'])
Prathmesh Prabhu88409f62019-08-30 14:32:28 -0700165
Xinan Lin3ba18a02019-08-13 15:44:55 -0700166 def testNoFinalBuild(self):
Xinan Lin9e4917d2019-11-04 10:58:47 -0800167 suite_kwargs = _get_suite_params()
168 suite_kwargs[build_lib.BuildVersionKey.CROS_VERSION] = None
169 suite_kwargs[build_lib.BuildVersionKey.ANDROID_BUILD_VERSION] = None
170 suite_kwargs[build_lib.BuildVersionKey.TESTBED_BUILD_VERSION] = None
171
172 task_executor.push(task_executor.SUITES_QUEUE,
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700173 tag='fake_suite',
174 **suite_kwargs)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800175
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700176 tasks = self.queue.lease_tasks_by_tag(
177 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800178 executed = self.client.multirequest_run(tasks, 'fake_suite')
179 self.assertEqual(len(executed), 0)
Prathmesh Prabhu9a8060a2019-08-30 14:13:23 -0700180 self._assert_bb_client_not_called()
Xinan Lin3ba18a02019-08-13 15:44:55 -0700181
Prathmesh Prabhu46156ff2020-06-20 00:18:52 -0700182 def testShouldSetQsAccountForUnmanagedPool(self):
Xinan Lin4757d6f2020-03-24 22:20:31 -0700183 suite_kwargs = _get_suite_params(pool='foo')
184 suite_kwargs['priority'] = '30'
185 suite_kwargs['qs_account'] = 'qs-foo'
186
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700187 task_executor.push(task_executor.SUITES_QUEUE,
188 tag='fake_suite',
189 **suite_kwargs)
Xinan Lin4757d6f2020-03-24 22:20:31 -0700190
191 tasks = self.queue.lease_tasks_by_tag(
192 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
193 self.client.multirequest_run(tasks, 'fake_suite')
Xinan Lin4757d6f2020-03-24 22:20:31 -0700194 self._assert_bb_client_called()
Jacob Kopczynskif464c8f2020-08-20 17:54:35 +0000195 request_map = self._extract_request_map_from_bb_client()
Xinan Lin4757d6f2020-03-24 22:20:31 -0700196 self.assertEqual(len(request_map), 1)
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700197 req = _struct_to_ctp_request(request_map['fake_board_fake_model'])
Xinan Lin4757d6f2020-03-24 22:20:31 -0700198 self.assertEqual(req.params.scheduling.qs_account, 'qs-foo')
199 # If qs_account is set, priority should be 0, the default value
200 # defined by proto3.
201 self.assertEqual(req.params.scheduling.priority, 0)
202
Prathmesh Prabhu46156ff2020-06-20 00:18:52 -0700203 def testShouldSetQsAccountForManagedPool(self):
Xinan Lin1516edb2020-07-05 23:13:54 -0700204 suite_kwargs = _get_suite_params(pool='MANAGED_POOL_QUOTA')
Xinan Lin4757d6f2020-03-24 22:20:31 -0700205 suite_kwargs['qs_account'] = 'qs-foo'
206
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700207 task_executor.push(task_executor.SUITES_QUEUE,
208 tag='fake_suite',
209 **suite_kwargs)
Xinan Lin4757d6f2020-03-24 22:20:31 -0700210
211 tasks = self.queue.lease_tasks_by_tag(
212 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
213 self.client.multirequest_run(tasks, 'fake_suite')
Xinan Lin4757d6f2020-03-24 22:20:31 -0700214 self._assert_bb_client_called()
Jacob Kopczynskif464c8f2020-08-20 17:54:35 +0000215 request_map = self._extract_request_map_from_bb_client()
Xinan Lin4757d6f2020-03-24 22:20:31 -0700216 self.assertEqual(len(request_map), 1)
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700217 req = _struct_to_ctp_request(request_map['fake_board_fake_model'])
Prathmesh Prabhu46156ff2020-06-20 00:18:52 -0700218 self.assertEqual(req.params.scheduling.qs_account, 'qs-foo')
219 # If qs_account is set, priority should be 0, the default value
220 # defined by proto3.
221 self.assertEqual(req.params.scheduling.priority, 0)
Xinan Lin4757d6f2020-03-24 22:20:31 -0700222
Xinan Lin9e4917d2019-11-04 10:58:47 -0800223 def testShouldSetPriority(self):
224 suite_kwargs = _get_suite_params()
225 suite_kwargs['priority'] = '30'
Xinan Linfb63d572019-09-24 15:49:04 -0700226
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700227 task_executor.push(task_executor.SUITES_QUEUE,
228 tag='fake_suite',
229 **suite_kwargs)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800230
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700231 tasks = self.queue.lease_tasks_by_tag(
232 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800233 self.client.multirequest_run(tasks, 'fake_suite')
234 request_map = self._extract_request_map_from_bb_client()
Xinan Linfb63d572019-09-24 15:49:04 -0700235 self._assert_bb_client_called()
Xinan Lin9e4917d2019-11-04 10:58:47 -0800236 self.assertEqual(len(request_map), 1)
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700237 req = _struct_to_ctp_request(request_map['fake_board_fake_model'])
Xinan Lin9e4917d2019-11-04 10:58:47 -0800238 self.assertEqual(req.params.scheduling.priority, 30)
239
240 def testRequestWithInvalidTags(self):
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700241 suite_kwargs = [_get_suite_params(board=b) for b in ['foo', 'foo']]
Xinan Lin9e4917d2019-11-04 10:58:47 -0800242
243 # test request having None String Tag
244 suite_kwargs[0]['model'] = 'None'
245
246 # test request having None Tag
247 suite_kwargs[1]['model'] = None
248
249 for suite in suite_kwargs:
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700250 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800251
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700252 tasks = self.queue.lease_tasks_by_tag(
253 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800254 executed = self.client.multirequest_run(tasks, 'fake_suite')
Jacob Kopczynskif464c8f2020-08-20 17:54:35 +0000255 self._assert_bb_client_called()
Xinan Lin9e4917d2019-11-04 10:58:47 -0800256 self.assertEqual(len(executed), 2)
257
Xinan Lin9e4917d2019-11-04 10:58:47 -0800258 request_map = self._extract_request_map_from_bb_client()
259 self.assertEqual(len(request_map), 2)
Xinan Linfb63d572019-09-24 15:49:04 -0700260 want = {
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700261 'suite:fake_suite',
262 'build:fake_cros_build',
Xinan Lin1516edb2020-07-05 23:13:54 -0700263 'label-pool:MANAGED_POOL_QUOTA',
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700264 'label-board:foo',
Taylor Clarke12ec9a2021-02-18 22:22:19 +0000265 'ctp-fwd-task-name:None',
Xinan Linfb63d572019-09-24 15:49:04 -0700266 }
Xinan Lin9e4917d2019-11-04 10:58:47 -0800267 want_bb = {
Jared Loucks438bf3a2022-04-06 13:43:15 -0600268 'label-image:fake_cros_build',
269 'label-suite:fake_suite',
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700270 'suite:fake_suite',
Dhanya Ganeshedb78cb2020-07-20 19:40:50 +0000271 'user_agent:suite_scheduler',
Xinan Lin9e4917d2019-11-04 10:58:47 -0800272 }
273 for req_name in ['foo', 'foo_1']:
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700274 req = _struct_to_ctp_request(request_map[req_name])
Xinan Lin9e4917d2019-11-04 10:58:47 -0800275 req_tags = set([t for t in req.params.decorations.tags])
276 self.assertEqual(want, req_tags)
277 bb_tags = set([t for t in self._extract_tags_from_bb_client()])
278 self.assertEqual(want_bb, bb_tags)
Xinan Linfb63d572019-09-24 15:49:04 -0700279
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700280 def testRequestTags(self):
Xinan Lin9e4917d2019-11-04 10:58:47 -0800281 suite_kwargs = _get_suite_params(
282 board='fake_board',
283 model='fake_model',
Xinan Lin1516edb2020-07-05 23:13:54 -0700284 pool='DUT_POOL_QUOTA',
Xinan Lin9e4917d2019-11-04 10:58:47 -0800285 suite='fake_suite',
286 cros_build='fake_build',
Taylor Clarke12ec9a2021-02-18 22:22:19 +0000287 analytics_name='fake_analytics_name',
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700288 )
Xinan Lin9e4917d2019-11-04 10:58:47 -0800289 task_executor.push(task_executor.SUITES_QUEUE,
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700290 tag='fake_suite',
291 **suite_kwargs)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800292
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700293 tasks = self.queue.lease_tasks_by_tag(
294 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800295 self.client.multirequest_run(tasks, 'fake_suite')
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700296 self._assert_bb_client_called()
297 want = {
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700298 'label-board:fake_board',
299 'label-model:fake_model',
Xinan Lin1516edb2020-07-05 23:13:54 -0700300 'label-pool:DUT_POOL_QUOTA',
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700301 'suite:fake_suite',
302 'build:fake_build',
Taylor Clarke12ec9a2021-02-18 22:22:19 +0000303 'analytics_name:fake_analytics_name',
304 'ctp-fwd-task-name:None',
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700305 }
Xinan Lin9e4917d2019-11-04 10:58:47 -0800306 want_bb = {
Jared Loucks438bf3a2022-04-06 13:43:15 -0600307 'label-suite:fake_suite',
308 'label-image:fake_build',
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700309 'suite:fake_suite',
Dhanya Ganeshedb78cb2020-07-20 19:40:50 +0000310 'user_agent:suite_scheduler',
Xinan Lin9e4917d2019-11-04 10:58:47 -0800311 }
312 request_map = self._extract_request_map_from_bb_client()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700313 request_list = [
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700314 _struct_to_ctp_request(v) for v in request_map.values()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700315 ]
Xinan Lin9e4917d2019-11-04 10:58:47 -0800316 self.assertEqual(len(request_list), 1)
317 req = request_list[0]
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700318 req_tags = set([t for t in req.params.decorations.tags])
319 self.assertEqual(want, req_tags)
320 bb_tags = set([t for t in self._extract_tags_from_bb_client()])
Xinan Lin9e4917d2019-11-04 10:58:47 -0800321 self.assertEqual(want_bb, bb_tags)
Xinan Lin3ba18a02019-08-13 15:44:55 -0700322
Xinan Linba3b9322020-04-24 15:08:12 -0700323 def testRequestUserDefinedDimensions(self):
324 suite_kwargs = _get_suite_params(
325 board='fake_board',
326 model='fake_model',
Xinan Lin1516edb2020-07-05 23:13:54 -0700327 pool='MANAGED_POOL_QUOTA',
Xinan Linba3b9322020-04-24 15:08:12 -0700328 suite='fake_suite',
329 cros_build='fake_build',
330 )
Patrick Meiringd4f60772021-03-04 12:09:28 +1100331 suite_kwargs['dimensions'] = 'label-wifi:foo, label-bt:hoo'
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700332 task_executor.push(task_executor.SUITES_QUEUE,
333 tag='fake_suite',
334 **suite_kwargs)
Xinan Linba3b9322020-04-24 15:08:12 -0700335
336 tasks = self.queue.lease_tasks_by_tag(
337 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
338 self.client.multirequest_run(tasks, 'fake_suite')
339 self._assert_bb_client_called()
340 request_map = self._extract_request_map_from_bb_client()
341 request_list = [
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700342 _struct_to_ctp_request(v) for v in request_map.values()
Xinan Linba3b9322020-04-24 15:08:12 -0700343 ]
344 self.assertEqual(len(request_list), 1)
345 req = request_list[0]
346 free_dimensions = req.params.freeform_attributes.swarming_dimensions
347 self.assertTrue(len(free_dimensions), 2)
348 self.assertTrue('label-wifi:foo' in free_dimensions)
349 self.assertTrue('label-bt:hoo' in free_dimensions)
350
351 def testRequestInvalidUserDefinedDimensions(self):
352 suite_kwargs = _get_suite_params(
353 board='fake_board',
354 model='fake_model',
Xinan Lin1516edb2020-07-05 23:13:54 -0700355 pool='MANAGED_POOL_QUOTA',
Xinan Linba3b9322020-04-24 15:08:12 -0700356 suite='fake_suite',
357 cros_build='fake_build',
358 )
359 suite_kwargs['dimensions'] = 'invalid-dimension'
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700360 task_executor.push(task_executor.SUITES_QUEUE,
361 tag='fake_suite',
362 **suite_kwargs)
Xinan Linba3b9322020-04-24 15:08:12 -0700363
364 tasks = self.queue.lease_tasks_by_tag(
365 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
366 self.client.multirequest_run(tasks, 'fake_suite')
367 self._assert_bb_client_not_called()
368
Prathmesh Prabhu73801e12019-08-30 14:09:31 -0700369 def _get_client(self, addr):
370 self.assertEqual(ADDR, addr)
371 return self.fake_client
372
Jacob Kopczynskif464c8f2020-08-20 17:54:35 +0000373 def _assert_bb_client_called(self, times=1):
374 actual = len(self.fake_client.called_with_requests)
375 self.assertEqual(actual, times,
376 "BB client called %s times, expected %s" % (actual, times))
Prathmesh Prabhu9a8060a2019-08-30 14:13:23 -0700377
378 def _assert_bb_client_not_called(self):
Jacob Kopczynskif464c8f2020-08-20 17:54:35 +0000379 actual = len(self.fake_client.called_with_requests)
380 self.assertEqual(actual, 0,
381 "BB client called %s times, expected 0"% actual)
Prathmesh Prabhu9a8060a2019-08-30 14:13:23 -0700382
Xinan Lin9e4917d2019-11-04 10:58:47 -0800383 def _extract_request_map_from_bb_client(self):
384 return self.fake_client.called_with_requests[0].properties['requests']
Prathmesh Prabhu6348ea82019-08-30 14:15:21 -0700385
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700386 def _extract_tags_from_bb_client(self):
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700387 return [
388 '%s:%s' % (t.key, t.value)
389 for t in self.fake_client.called_with_requests[0].tags
390 ]
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700391
Prathmesh Prabhu9a8060a2019-08-30 14:13:23 -0700392
Xinan Lindf0698a2020-02-05 22:38:11 -0800393class TestTaskExecutions(TestPlatformClientTestCase):
Xinan Lindf0698a2020-02-05 22:38:11 -0800394 def setUp(self):
395 super(TestTaskExecutions, self).setUp()
396 _mock_bq_client = mock.patch('rest_client.BigqueryRestClient')
397 mock_bq_client = _mock_bq_client.start()
398 self.addCleanup(_mock_bq_client.stop)
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700399 self.mock_bq_client = FakeBigqueryRestClient(None,
400 project='proj',
401 dataset='dataset',
402 table='foo')
Xinan Lindf0698a2020-02-05 22:38:11 -0800403 mock_bq_client.return_value = self.mock_bq_client
404
405 def testRecordSuccessfulTaskExecution(self):
Xinan Lin9b17c5b2020-08-06 10:43:30 -0700406 suite = _get_suite_params(board='fake_build', model='fake_model')
Xinan Lin083ba8f2020-02-06 13:55:18 -0800407 suite['task_id'] = FAKE_UUID
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700408 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
409 tasks = self.queue.lease_tasks_by_tag(
410 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lindf0698a2020-02-05 22:38:11 -0800411 self.client.multirequest_run(tasks, 'fake_suite')
412 self._assert_bb_client_called()
413 task_execution = json_format.Parse(
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700414 json.dumps(self.mock_bq_client.rows[0]['json']),
415 analytics_pb2.ExecutionTask())
Xinan Lindf0698a2020-02-05 22:38:11 -0800416 self.assertEqual(task_execution.queued_task_id, FAKE_UUID)
Xinan Lin9b17c5b2020-08-06 10:43:30 -0700417 self.assertEqual(task_execution.request_tag, 'fake_build_fake_model')
Xinan Lindf0698a2020-02-05 22:38:11 -0800418 self.assertEqual(task_execution.response.ctp_build_id, str(FAKE_BUILD_ID))
419 self.assertEqual(task_execution.error.error_message, '')
420
421 def testRecordFailedTaskExecution(self):
Xinan Lin9b17c5b2020-08-06 10:43:30 -0700422 suite = _get_suite_params(board='fake_build', model='fake_model')
Xinan Lin083ba8f2020-02-06 13:55:18 -0800423 suite['task_id'] = FAKE_UUID
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700424 task_executor.push(task_executor.SUITES_QUEUE, tag='fake_suite', **suite)
Xinan Lindf0698a2020-02-05 22:38:11 -0800425 self.fake_client.error = "cros_test_platform error"
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700426 tasks = self.queue.lease_tasks_by_tag(
427 3600, constants.Buildbucket.MULTIREQUEST_SIZE, deadline=60)
Xinan Lindf0698a2020-02-05 22:38:11 -0800428 self.client.multirequest_run(tasks, 'fake_suite')
429 self._assert_bb_client_called()
430 task_execution = json_format.Parse(
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700431 json.dumps(self.mock_bq_client.rows[0]['json']),
432 analytics_pb2.ExecutionTask())
Xinan Lindf0698a2020-02-05 22:38:11 -0800433 self.assertEqual(task_execution.queued_task_id, FAKE_UUID)
Xinan Lin9b17c5b2020-08-06 10:43:30 -0700434 self.assertEqual(task_execution.request_tag, 'fake_build_fake_model')
Xinan Lindf0698a2020-02-05 22:38:11 -0800435 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
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700449 def testIgnoreSuiteForUnmanagedPoolInStaging(self):
Xinan Linc54a7462020-04-17 15:39:01 -0700450 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
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700460def _struct_to_ctp_request(struct_pb2):
Xinan Lin3ba18a02019-08-13 15:44:55 -0700461 """Transform google struct proto to test_platform_request.
462
463 Args:
464 struct_pb2: A struct_pb2 instance.
465
466 Returns:
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700467 A ctp_request instance.
Xinan Lin3ba18a02019-08-13 15:44:55 -0700468 """
469 json = json_format.MessageToJson(struct_pb2)
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700470 return json_format.Parse(json, ctp_request.Request())