blob: 50076430d15885a8af8be001ff7c622dcd16ed60 [file] [log] [blame]
Xinan Lin3ba18a02019-08-13 15:44:55 -07001# Copyright 2019 The Chromium OS Authors. All rights reserved.
Xinan Linc61196b2019-08-13 10:37:30 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Xinan Linc61196b2019-08-13 10:37:30 -07004"""Module for interacting with Buildbucket."""
Xinan Linc61196b2019-08-13 10:37:30 -07005
Xinan Lin9e4917d2019-11-04 10:58:47 -08006import collections
Xinan Lin3ba18a02019-08-13 15:44:55 -07007import datetime
linxinane5eb4552019-08-26 05:44:45 +00008import logging
Azizur Rahmanb720e512022-05-13 18:30:40 +00009import os
Xinan Lin081b5d32020-03-23 17:37:55 -070010import uuid
Xinan Lin3ba18a02019-08-13 15:44:55 -070011
Xinan Lindf0698a2020-02-05 22:38:11 -080012import analytics
Xinan Lin3ba18a02019-08-13 15:44:55 -070013import build_lib
Xinan Linc61196b2019-08-13 10:37:30 -070014import constants
15import file_getter
Garry Wang111a26f2021-07-23 15:25:14 -070016from multi_duts_lib import restruct_secondary_targets_from_string
Xinan Linc61196b2019-08-13 10:37:30 -070017
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -070018from chromite.api.gen.test_platform import request_pb2 as ctp_request
Xinan Linc61196b2019-08-13 10:37:30 -070019from components import auth
Xinan Lin3ba18a02019-08-13 15:44:55 -070020from components.prpc import client as prpc_client
Sean McAllisteraf32bfb2021-07-19 09:20:17 -060021from chromite.third_party.infra_libs.buildbucket.proto import common_pb2 as bb_common_pb2
Azizur Rahmanb720e512022-05-13 18:30:40 +000022from chromite.third_party.infra_libs.buildbucket.proto import builds_service_pb2, builder_common_pb2
Sean McAllisteraf32bfb2021-07-19 09:20:17 -060023from chromite.third_party.infra_libs.buildbucket.proto.builds_service_prpc_pb2 import BuildsServiceDescription
Azizur Rahmana9a189f2022-07-29 20:28:28 +000024from chromite.api.gen.chromiumos.test.api import test_suite_pb2
Xinan Linc61196b2019-08-13 10:37:30 -070025
26from oauth2client import service_account
Xinan Lin3ba18a02019-08-13 15:44:55 -070027
Sean McAllister66bf7e92021-07-16 18:46:04 +000028from google.protobuf import json_format, struct_pb2
Xinan Linc61196b2019-08-13 10:37:30 -070029
Xinan Lin6e097382019-08-27 18:43:35 -070030GS_PREFIX = 'gs://chromeos-image-archive/'
Azizur Rahmanb720e512022-05-13 18:30:40 +000031CONTAINER_METADATA_LOC = 'metadata/containers.jsonpb'
Xinan Lin6e097382019-08-27 18:43:35 -070032
Xinan Lin57e2d962020-03-30 17:24:53 -070033# The default prpc timeout(10 sec) is too short for the request_id
34# to propgate in buildbucket, and could not fully dedup the
35# ScheduleBuild request. Increase it while not hitting the default
36# GAE deadline(60 sec)
37PRPC_TIMEOUT_SEC = 55
38
39
Xinan Linc61196b2019-08-13 10:37:30 -070040def _get_client(address):
41 """Create a prpc client instance for given address."""
42 return prpc_client.Client(address, BuildsServiceDescription)
43
44
Xinan Lin3ba18a02019-08-13 15:44:55 -070045class BuildbucketRunError(Exception):
46 """Raised when interactions with buildbucket server fail."""
47
48
Xinan Linc61196b2019-08-13 10:37:30 -070049class TestPlatformClient(object):
50 """prpc client for cros_test_platform, aka frontdoor."""
Xinan Linc61196b2019-08-13 10:37:30 -070051 def __init__(self, address, project, bucket, builder):
52 self.client = _get_client(address)
Azizur Rahmanb720e512022-05-13 18:30:40 +000053 self.builder = builder_common_pb2.BuilderID(project=project,
Sean McAllisteraf32bfb2021-07-19 09:20:17 -060054 bucket=bucket,
55 builder=builder)
Xinan Linc61196b2019-08-13 10:37:30 -070056 self.scope = 'https://www.googleapis.com/auth/userinfo.email'
57 self.running_env = constants.environment()
58
Xinan Lin9e4917d2019-11-04 10:58:47 -080059 def multirequest_run(self, tasks, suite):
Xinan Lin1e8e7912020-07-31 09:52:16 -070060 """Call cros_test_platform Builder to schedule a batch of suite tests.
Xinan Lin3ba18a02019-08-13 15:44:55 -070061
62 Args:
Xinan Lin9e4917d2019-11-04 10:58:47 -080063 tasks: The suite tasks to run.
64 suite: suite name for the batch request.
65
66 Returns:
67 List of executed tasks.
Xinan Lin3ba18a02019-08-13 15:44:55 -070068
69 Raises:
70 BuildbucketRunError: if failed to get build info from task parameters.
71 """
Xinan Lin9e4917d2019-11-04 10:58:47 -080072 requests = []
73 executed_tasks = []
74 counter = collections.defaultdict(int)
Xinan Lindf0698a2020-02-05 22:38:11 -080075 task_executions = []
Jared Loucks438bf3a2022-04-06 13:43:15 -060076 req_tags = _bb_tags(suite)
Xinan Lin9e4917d2019-11-04 10:58:47 -080077 for task in tasks:
78 try:
79 params = task.extract_params()
Xinan Linc54a7462020-04-17 15:39:01 -070080 if _should_skip(params):
81 continue
Xinan Lin8bb5b4b2020-07-21 23:17:55 -070082 req = _form_test_platform_request(params)
Xinan Lin9e4917d2019-11-04 10:58:47 -080083 req_json = json_format.MessageToJson(req)
84 counter_key = params['board']
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -070085 counter_key += '' if params['model'] in [None, 'None'
86 ] else ('_' + params['model'])
Xinan Lin9e4917d2019-11-04 10:58:47 -080087 counter[counter_key] += 1
88 req_name = counter_key
89 if counter[counter_key] > 1:
90 req_name += '_' + str(counter[counter_key] - 1)
91 requests.append('"%s": %s' % (req_name, req_json))
Jared Loucks438bf3a2022-04-06 13:43:15 -060092 req_tags.append(
93 bb_common_pb2.StringPair(
94 key='label-image',
95 value=_infer_build_from_task_params(params)))
Xinan Lin9e4917d2019-11-04 10:58:47 -080096 executed_tasks.append(task)
Xinan Lin083ba8f2020-02-06 13:55:18 -080097 if params.get('task_id'):
Xinan Lin9b17c5b2020-08-06 10:43:30 -070098 task_executions.append(
99 analytics.ExecutionTask(params['task_id'], req_name))
Xinan Lin9e4917d2019-11-04 10:58:47 -0800100 except (ValueError, BuildbucketRunError):
Xinan Linba3b9322020-04-24 15:08:12 -0700101 logging.error('Failed to process task: %r', params)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800102 if not requests:
103 return []
104 try:
105 requests_json = '{ "requests": { %s } }' % ', '.join(requests)
Jared Loucks438bf3a2022-04-06 13:43:15 -0600106 req_build = self._build_request(requests_json, req_tags)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800107 logging.debug('Raw request to buildbucket: %r', req_build)
linxinane5eb4552019-08-26 05:44:45 +0000108
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700109 if (self.running_env == constants.RunningEnv.ENV_STANDALONE
110 or self.running_env == constants.RunningEnv.ENV_DEVELOPMENT_SERVER):
Xinan Lin9e4917d2019-11-04 10:58:47 -0800111 # If running locally, use the staging service account.
112 sa_key = self._gen_service_account_key(
113 file_getter.STAGING_CLIENT_SECRETS_FILE)
114 cred = prpc_client.service_account_credentials(
115 service_account_key=sa_key)
116 else:
117 cred = prpc_client.service_account_credentials()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700118 resp = self.client.ScheduleBuild(req_build,
119 credentials=cred,
120 timeout=PRPC_TIMEOUT_SEC)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800121 logging.debug('Response from buildbucket: %r', resp)
Xinan Lindf0698a2020-02-05 22:38:11 -0800122 for t in task_executions:
123 t.update_result(resp)
124 try:
125 if not t.upload():
126 logging.warning('Failed to insert row: %r', t)
127 # For any exceptions from BQ, only log it.
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700128 except Exception as e: #pylint: disable=broad-except
129 logging.exception('Failed to insert row: %r, got error: %s', t,
130 str(e))
Xinan Lin9e4917d2019-11-04 10:58:47 -0800131 return executed_tasks
132 except Exception as e:
133 logging.debug('Failed to process tasks: %r', tasks)
134 logging.exception(str(e))
135 return []
Xinan Lin3ba18a02019-08-13 15:44:55 -0700136
Xinan Linc61196b2019-08-13 10:37:30 -0700137 def dummy_run(self):
138 """Perform a dummy run of prpc call to cros_test_platform-dev."""
139
Xinan Lin9e4917d2019-11-04 10:58:47 -0800140 requests_json = '{ "requests": { "dummy": {} } }'
141 req_build = self._build_request(requests_json, tags=None)
Xinan Linc61196b2019-08-13 10:37:30 -0700142 # Use the staging service account to authorize the request.
143 sa_key = self._gen_service_account_key(
144 file_getter.STAGING_CLIENT_SECRETS_FILE)
145 cred = prpc_client.service_account_credentials(service_account_key=sa_key)
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700146 return self.client.ScheduleBuild(req_build,
147 credentials=cred,
148 timeout=PRPC_TIMEOUT_SEC)
Xinan Linc61196b2019-08-13 10:37:30 -0700149
Xinan Lin9e4917d2019-11-04 10:58:47 -0800150 def _build_request(self, reqs_json, tags):
Xinan Lin3ba18a02019-08-13 15:44:55 -0700151 """Generate ScheduleBuildRequest for calling buildbucket.
Xinan Linc61196b2019-08-13 10:37:30 -0700152
Xinan Lin3ba18a02019-08-13 15:44:55 -0700153 Args:
Xinan Lin9e4917d2019-11-04 10:58:47 -0800154 reqs_json: A json string of requests.
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700155 tags: A list of tags for the buildbucket build.
Xinan Linc61196b2019-08-13 10:37:30 -0700156
Xinan Lin3ba18a02019-08-13 15:44:55 -0700157 Returns:
158 A ScheduleBuildRequest instance.
159 """
Xinan Lin9e4917d2019-11-04 10:58:47 -0800160 requests_struct = struct_pb2.Struct()
161 recipe_struct = json_format.Parse(reqs_json, requests_struct)
Sean McAllisteraf32bfb2021-07-19 09:20:17 -0600162 return builds_service_pb2.ScheduleBuildRequest(builder=self.builder,
163 properties=recipe_struct,
164 request_id=str(
165 uuid.uuid1()),
166 tags=tags)
Xinan Linc61196b2019-08-13 10:37:30 -0700167
168 def _gen_service_account_key(self, sa):
Xinan Lin3ba18a02019-08-13 15:44:55 -0700169 """Generate credentials to authorize the call.
Xinan Linc61196b2019-08-13 10:37:30 -0700170
Xinan Lin3ba18a02019-08-13 15:44:55 -0700171 Args:
172 sa: A string of the path to the service account json file.
Xinan Linc61196b2019-08-13 10:37:30 -0700173
Xinan Lin3ba18a02019-08-13 15:44:55 -0700174 Returns:
175 A service account key.
176 """
Xinan Linc61196b2019-08-13 10:37:30 -0700177 service_credentials = service_account.ServiceAccountCredentials
Xinan Lin3ba18a02019-08-13 15:44:55 -0700178 key = service_credentials.from_json_keyfile_name(sa, self.scope)
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700179 return auth.ServiceAccountKey(client_email=key.service_account_email,
180 private_key=key._private_key_pkcs8_pem,
181 private_key_id=key._private_key_id)
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700182
183
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700184def _form_test_platform_request(task_params):
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700185 """Generates test_platform.Request proto to send to buildbucket.
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700186
187 Args:
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700188 task_params: dict containing the parameters of a task from the suite queue.
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700189
190 Returns:
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700191 A ctp_request.Request instance.
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700192 """
193 build = _infer_build_from_task_params(task_params)
194 if build == 'None':
195 raise BuildbucketRunError('No proper build in task params: %r' %
196 task_params)
197 pool = _infer_pool_from_task_params(task_params)
198 timeout = _infer_timeout_from_task_params(task_params)
199
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700200 request = ctp_request.Request()
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700201 params = request.params
202
Xinan Lin4757d6f2020-03-24 22:20:31 -0700203 params.scheduling.CopyFrom(_scheduling_for_pool(pool))
Prathmesh Prabhu46156ff2020-06-20 00:18:52 -0700204 if task_params.get('qs_account') not in ['None', None]:
Xinan Lin4757d6f2020-03-24 22:20:31 -0700205 params.scheduling.qs_account = task_params.get('qs_account')
206 # Quota Scheduler has no concept of priority.
Xinan Lin8bb5b4b2020-07-21 23:17:55 -0700207 if (task_params.get('priority') not in ['None', None]
208 and not params.scheduling.qs_account):
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700209 params.scheduling.priority = int(task_params['priority'])
210
211 params.software_dependencies.add().chromeos_build = build
212 params.software_attributes.build_target.name = task_params['board']
Aviv Keshetc679faf2019-11-27 17:52:50 -0800213
214 gs_url = GS_PREFIX + task_params['test_source_build']
215 params.metadata.test_metadata_url = gs_url
216 params.metadata.debug_symbols_archive_url = gs_url
Azizur Rahmanb720e512022-05-13 18:30:40 +0000217 params.metadata.container_metadata_url = os.path.join(gs_url, CONTAINER_METADATA_LOC)
Aviv Keshetc679faf2019-11-27 17:52:50 -0800218
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700219 params.time.maximum_duration.FromTimedelta(timeout)
220
221 for key, value in _request_tags(task_params, build, pool).iteritems():
222 params.decorations.tags.append('%s:%s' % (key, value))
223
Xinan Lin7bf266a2020-06-10 23:54:26 -0700224 for d in _infer_user_defined_dimensions(task_params):
225 params.freeform_attributes.swarming_dimensions.append(d)
Xinan Linba3b9322020-04-24 15:08:12 -0700226
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700227 if task_params['model'] != 'None':
228 params.hardware_attributes.model = task_params['model']
229
Azizur Rahmanee62a662022-06-02 18:53:15 +0000230 if task_params.has_key('job_retry') and task_params['job_retry'] == 'True':
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700231 params.retry.allow = True
232 params.retry.max = constants.Buildbucket.MAX_RETRY
233
Azizur Rahmanee62a662022-06-02 18:53:15 +0000234 params.run_via_cft = (task_params.has_key('run_via_cft') and task_params['run_via_cft'] == 'True')
Azizur Rahmanb720e512022-05-13 18:30:40 +0000235
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700236 fw_rw_build = task_params.get(build_lib.BuildVersionKey.FW_RW_VERSION)
237 fw_ro_build = task_params.get(build_lib.BuildVersionKey.FW_RO_VERSION)
Brigit Rossbachbb080912020-11-18 13:52:17 -0700238
239 if task_params.has_key('firmware_ro_version'):
240 build = params.software_dependencies.add()
241 build.ro_firmware_build = task_params['firmware_ro_version']
242
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700243 # Skip firmware field if None(unspecified) or 'None'(no such build).
244 if fw_ro_build not in (None, 'None'):
245 build = params.software_dependencies.add()
246 build.ro_firmware_build = fw_ro_build
Brigit Rossbachbb080912020-11-18 13:52:17 -0700247
248 if task_params.has_key('firmware_rw_version'):
249 build = params.software_dependencies.add()
250 build.rw_firmware_build = task_params['firmware_rw_version']
251
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700252 if fw_rw_build not in (None, 'None'):
253 build = params.software_dependencies.add()
254 build.rw_firmware_build = fw_rw_build
255
Garry Wang2104bf52021-07-19 18:13:12 -0700256 secondary_targets = task_params.get('secondary_targets')
Garry Wang111a26f2021-07-23 15:25:14 -0700257 secondary_targets = restruct_secondary_targets_from_string(secondary_targets)
Garry Wang2104bf52021-07-19 18:13:12 -0700258 if secondary_targets:
259 for s_target in secondary_targets:
260 s_device = params.secondary_devices.add()
261 s_device.software_attributes.build_target.name = s_target.board
262 if s_target.model:
263 s_device.hardware_attributes.model = s_target.model
264 if s_target.cros_build:
265 s_build = s_device.software_dependencies.add()
266 s_build.chromeos_build = s_target.cros_build
267
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700268 request.test_plan.suite.add().name = task_params['suite']
Azizur Rahmana9a189f2022-07-29 20:28:28 +0000269 if params.run_via_cft:
270 include_tags = task_params.get('include_tags', None)
271 exclude_tags = task_params.get('exclude_tags', None)
272 if include_tags or exclude_tags:
273 request.test_plan.tag_criteria.CopyFrom(
274 test_suite_pb2.TestSuite.TestCaseTagCriteria(
275 tags=_get_test_tags(include_tags),
276 tag_excludes=_get_test_tags(exclude_tags)))
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700277 return request
278
Sean McAllisteraf32bfb2021-07-19 09:20:17 -0600279
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700280def _scheduling_for_pool(pool):
281 """Assign appropriate pool name to scheduling instance.
282
283 Args:
Xinan Lin1516edb2020-07-05 23:13:54 -0700284 pool: string pool name (e.g. 'MANAGED_POOL_QUOTA', 'wificell').
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700285
286 Returns:
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700287 scheduling: A ctp_request.Request.Params.Scheduling instance.
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700288 """
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700289 mp = ctp_request.Request.Params.Scheduling.ManagedPool
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700290 if mp.DESCRIPTOR.values_by_name.get(pool) is not None:
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700291 return ctp_request.Request.Params.Scheduling(managed_pool=mp.Value(pool))
292 return ctp_request.Request.Params.Scheduling(unmanaged_pool=pool)
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700293
294
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700295# Also see: A copy of this function in swarming_lib.py.
296def _infer_build_from_task_params(task_params):
297 """Infer the build to install on the DUT for the scheduled task.
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700298
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700299 Args:
300 task_params: The parameters of a task loaded from suite queue.
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700301
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700302 Returns:
303 A string representing the build to run.
304 """
305 cros_build = task_params[build_lib.BuildVersionKey.CROS_VERSION]
306 android_build = task_params[build_lib.BuildVersionKey.ANDROID_BUILD_VERSION]
307 testbed_build = task_params[build_lib.BuildVersionKey.TESTBED_BUILD_VERSION]
308 return cros_build or android_build or testbed_build
309
310
311def _infer_pool_from_task_params(task_params):
312 """Infer the pool to use for the scheduled task.
313
314 Args:
315 task_params: The parameters of a task loaded from suite queue.
316
317 Returns:
318 A string pool to schedule task in.
319 """
320 if task_params.get('override_qs_account'):
321 return 'DUT_POOL_QUOTA'
322 return task_params.get('override_pool') or task_params['pool']
323
324
325def _infer_timeout_from_task_params(task_params):
326 """Infer the timeout for the scheduled task.
327
328 Args:
329 task_params: The parameters of a task loaded from suite queue.
330
331 Returns:
332 A datetime.timedelta instance for the timeout.
333 """
334 timeout_mins = int(task_params['timeout_mins'])
335 # timeout's unit is hour.
336 if task_params.get('timeout'):
Sean Abrahamec0d0762020-09-18 17:19:05 +0000337 timeout_mins = max(int(task_params['timeout']) * 60, timeout_mins)
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700338 if timeout_mins > constants.Buildbucket.MAX_BUILDBUCKET_TIMEOUT_MINS:
339 timeout_mins = constants.Buildbucket.MAX_BUILDBUCKET_TIMEOUT_MINS
340 return datetime.timedelta(minutes=timeout_mins)
341
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700342
Xinan Linba3b9322020-04-24 15:08:12 -0700343def _infer_user_defined_dimensions(task_params):
344 """Infer the dimensions defined by users.
345
346 Args:
347 task_params: The parameters of a task loaded from suite queue.
348
349 Returns:
350 A list of strings; an empty list if no dimensions set.
351
352 Raises:
353 ValueError: if dimension is not valid.
354 """
355 result = []
356 if task_params.get('dimensions') in (None, 'None'):
357 return result
Patrick Meiringd4f60772021-03-04 12:09:28 +1100358 dims = [d.lstrip() for d in task_params.get('dimensions').split(',')]
359 for d in dims:
Xinan Linba3b9322020-04-24 15:08:12 -0700360 if len(d.split(':')) != 2:
361 raise ValueError(
362 'Job %s has invalid dimensions: %s' %
363 (task_params.get('name'), task_params.get('dimensions')))
364 result.append(d)
365 return result
366
367
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700368def _request_tags(task_params, build, pool):
369 """Infer tags to include in cros_test_platform request.
370
371 Args:
372 task_params: suite task parameters.
373 build: The build included in the request. Must not be None.
374 pool: The DUT pool used for the request. Must not be None.
375
376 Returns:
377 A dict of tags.
378 """
379 tags = {
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700380 'build': build,
381 'label-pool': pool,
Taylor Clarke12ec9a2021-02-18 22:22:19 +0000382 'ctp-fwd-task-name': task_params.get('name')
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700383 }
Xinan Linfb63d572019-09-24 15:49:04 -0700384 if task_params.get('board') not in (None, 'None'):
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700385 tags['label-board'] = task_params['board']
Xinan Linfb63d572019-09-24 15:49:04 -0700386 if task_params.get('model') not in (None, 'None'):
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700387 tags['label-model'] = task_params['model']
Xinan Linfb63d572019-09-24 15:49:04 -0700388 if task_params.get('suite') not in (None, 'None'):
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700389 tags['suite'] = task_params['suite']
Taylor Clarke12ec9a2021-02-18 22:22:19 +0000390 if task_params.get('analytics_name') not in (None, 'None'):
391 tags['analytics_name'] = task_params['analytics_name']
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700392 return tags
393
394
Azizur Rahmana9a189f2022-07-29 20:28:28 +0000395def _get_test_tags(tags_list_str):
396 """parse test tags properly and return them.
397
398 Args:
399 tags_list_str: comma separated list of tags.
400
401 Returns:
402 A list of strings; an empty list if no tags set.
403 """
404 if tags_list_str not in (None, '', 'None'):
405 return [d.lstrip() for d in tags_list_str.split(',')]
406 return []
407
408
Xinan Linc8647112020-02-04 16:45:56 -0800409def _get_key_val_from_label(label):
410 """A helper to get key and value from the label.
411
412 Args:
413 label: A string of label, should be in the form of
414 key:value, e.g. 'pool:ChromeOSSkylab'.
415 """
416 res = label.split(':')
417 if len(res) == 2:
418 return res[0], res[1]
419 logging.warning('Failed to parse the label, %s', label)
420
421
Dhanya Ganeshedb78cb2020-07-20 19:40:50 +0000422def _bb_tags(suite):
423 """Get all the tags required for Buildbucket.
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700424
425 Args:
Dhanya Ganeshedb78cb2020-07-20 19:40:50 +0000426 suite: A string of suite name being scheduled.
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700427
428 Returns:
429 [bb_common_pb2.StringPair] tags to include the buildbucket request.
430 """
Sean McAllisteraf32bfb2021-07-19 09:20:17 -0600431 return [
Jared Loucks438bf3a2022-04-06 13:43:15 -0600432 bb_common_pb2.StringPair(key='label-suite', value=suite),
Sean McAllisteraf32bfb2021-07-19 09:20:17 -0600433 bb_common_pb2.StringPair(key='suite', value=suite),
434 bb_common_pb2.StringPair(key='user_agent', value='suite_scheduler')
435 ]
Xinan Linc54a7462020-04-17 15:39:01 -0700436
437
438def _should_skip(params):
439 """Decide whether to skip a task based on env and pool.
440
441 Suite request from staging may still have a small chance to run
442 in production. However, for unmanaged pools(e.g. wificell), which
443 usually are small, dev traffic is unacceptable.
444
445 Args:
446 params: dict containing the parameters of a task got from suite
447 queue.
448
449 Returns:
450 A boolean; true for suite targetting non-default pools from staging
451 env.
452 """
453 if constants.application_id() == constants.AppID.PROD_APP:
454 return False
Xinan Lin0d7910d2020-07-21 11:06:45 -0700455 return params['pool'] != 'MANAGED_POOL_QUOTA'