blob: 8bea636ae01753a05018d2219330a04620ace4d8 [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
Xinan Linc61196b2019-08-13 10:37:30 -070024
25from oauth2client import service_account
Xinan Lin3ba18a02019-08-13 15:44:55 -070026
Sean McAllister66bf7e92021-07-16 18:46:04 +000027from google.protobuf import json_format, struct_pb2
Xinan Linc61196b2019-08-13 10:37:30 -070028
Xinan Lin6e097382019-08-27 18:43:35 -070029GS_PREFIX = 'gs://chromeos-image-archive/'
Azizur Rahmanb720e512022-05-13 18:30:40 +000030CONTAINER_METADATA_LOC = 'metadata/containers.jsonpb'
Xinan Lin6e097382019-08-27 18:43:35 -070031
Xinan Lin57e2d962020-03-30 17:24:53 -070032# The default prpc timeout(10 sec) is too short for the request_id
33# to propgate in buildbucket, and could not fully dedup the
34# ScheduleBuild request. Increase it while not hitting the default
35# GAE deadline(60 sec)
36PRPC_TIMEOUT_SEC = 55
37
38
Xinan Linc61196b2019-08-13 10:37:30 -070039def _get_client(address):
40 """Create a prpc client instance for given address."""
41 return prpc_client.Client(address, BuildsServiceDescription)
42
43
Xinan Lin3ba18a02019-08-13 15:44:55 -070044class BuildbucketRunError(Exception):
45 """Raised when interactions with buildbucket server fail."""
46
47
Xinan Linc61196b2019-08-13 10:37:30 -070048class TestPlatformClient(object):
49 """prpc client for cros_test_platform, aka frontdoor."""
Xinan Linc61196b2019-08-13 10:37:30 -070050 def __init__(self, address, project, bucket, builder):
51 self.client = _get_client(address)
Azizur Rahmanb720e512022-05-13 18:30:40 +000052 self.builder = builder_common_pb2.BuilderID(project=project,
Sean McAllisteraf32bfb2021-07-19 09:20:17 -060053 bucket=bucket,
54 builder=builder)
Xinan Linc61196b2019-08-13 10:37:30 -070055 self.scope = 'https://www.googleapis.com/auth/userinfo.email'
56 self.running_env = constants.environment()
57
Xinan Lin9e4917d2019-11-04 10:58:47 -080058 def multirequest_run(self, tasks, suite):
Xinan Lin1e8e7912020-07-31 09:52:16 -070059 """Call cros_test_platform Builder to schedule a batch of suite tests.
Xinan Lin3ba18a02019-08-13 15:44:55 -070060
61 Args:
Xinan Lin9e4917d2019-11-04 10:58:47 -080062 tasks: The suite tasks to run.
63 suite: suite name for the batch request.
64
65 Returns:
66 List of executed tasks.
Xinan Lin3ba18a02019-08-13 15:44:55 -070067
68 Raises:
69 BuildbucketRunError: if failed to get build info from task parameters.
70 """
Xinan Lin9e4917d2019-11-04 10:58:47 -080071 requests = []
72 executed_tasks = []
73 counter = collections.defaultdict(int)
Xinan Lindf0698a2020-02-05 22:38:11 -080074 task_executions = []
Jared Loucks438bf3a2022-04-06 13:43:15 -060075 req_tags = _bb_tags(suite)
Xinan Lin9e4917d2019-11-04 10:58:47 -080076 for task in tasks:
77 try:
78 params = task.extract_params()
Xinan Linc54a7462020-04-17 15:39:01 -070079 if _should_skip(params):
80 continue
Xinan Lin8bb5b4b2020-07-21 23:17:55 -070081 req = _form_test_platform_request(params)
Xinan Lin9e4917d2019-11-04 10:58:47 -080082 req_json = json_format.MessageToJson(req)
83 counter_key = params['board']
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -070084 counter_key += '' if params['model'] in [None, 'None'
85 ] else ('_' + params['model'])
Xinan Lin9e4917d2019-11-04 10:58:47 -080086 counter[counter_key] += 1
87 req_name = counter_key
88 if counter[counter_key] > 1:
89 req_name += '_' + str(counter[counter_key] - 1)
90 requests.append('"%s": %s' % (req_name, req_json))
Jared Loucks438bf3a2022-04-06 13:43:15 -060091 req_tags.append(
92 bb_common_pb2.StringPair(
93 key='label-image',
94 value=_infer_build_from_task_params(params)))
Xinan Lin9e4917d2019-11-04 10:58:47 -080095 executed_tasks.append(task)
Xinan Lin083ba8f2020-02-06 13:55:18 -080096 if params.get('task_id'):
Xinan Lin9b17c5b2020-08-06 10:43:30 -070097 task_executions.append(
98 analytics.ExecutionTask(params['task_id'], req_name))
Xinan Lin9e4917d2019-11-04 10:58:47 -080099 except (ValueError, BuildbucketRunError):
Xinan Linba3b9322020-04-24 15:08:12 -0700100 logging.error('Failed to process task: %r', params)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800101 if not requests:
102 return []
103 try:
104 requests_json = '{ "requests": { %s } }' % ', '.join(requests)
Jared Loucks438bf3a2022-04-06 13:43:15 -0600105 req_build = self._build_request(requests_json, req_tags)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800106 logging.debug('Raw request to buildbucket: %r', req_build)
linxinane5eb4552019-08-26 05:44:45 +0000107
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700108 if (self.running_env == constants.RunningEnv.ENV_STANDALONE
109 or self.running_env == constants.RunningEnv.ENV_DEVELOPMENT_SERVER):
Xinan Lin9e4917d2019-11-04 10:58:47 -0800110 # If running locally, use the staging service account.
111 sa_key = self._gen_service_account_key(
112 file_getter.STAGING_CLIENT_SECRETS_FILE)
113 cred = prpc_client.service_account_credentials(
114 service_account_key=sa_key)
115 else:
116 cred = prpc_client.service_account_credentials()
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700117 resp = self.client.ScheduleBuild(req_build,
118 credentials=cred,
119 timeout=PRPC_TIMEOUT_SEC)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800120 logging.debug('Response from buildbucket: %r', resp)
Xinan Lindf0698a2020-02-05 22:38:11 -0800121 for t in task_executions:
122 t.update_result(resp)
123 try:
124 if not t.upload():
125 logging.warning('Failed to insert row: %r', t)
126 # For any exceptions from BQ, only log it.
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700127 except Exception as e: #pylint: disable=broad-except
128 logging.exception('Failed to insert row: %r, got error: %s', t,
129 str(e))
Xinan Lin9e4917d2019-11-04 10:58:47 -0800130 return executed_tasks
131 except Exception as e:
132 logging.debug('Failed to process tasks: %r', tasks)
133 logging.exception(str(e))
134 return []
Xinan Lin3ba18a02019-08-13 15:44:55 -0700135
Xinan Linc61196b2019-08-13 10:37:30 -0700136 def dummy_run(self):
137 """Perform a dummy run of prpc call to cros_test_platform-dev."""
138
Xinan Lin9e4917d2019-11-04 10:58:47 -0800139 requests_json = '{ "requests": { "dummy": {} } }'
140 req_build = self._build_request(requests_json, tags=None)
Xinan Linc61196b2019-08-13 10:37:30 -0700141 # Use the staging service account to authorize the request.
142 sa_key = self._gen_service_account_key(
143 file_getter.STAGING_CLIENT_SECRETS_FILE)
144 cred = prpc_client.service_account_credentials(service_account_key=sa_key)
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700145 return self.client.ScheduleBuild(req_build,
146 credentials=cred,
147 timeout=PRPC_TIMEOUT_SEC)
Xinan Linc61196b2019-08-13 10:37:30 -0700148
Xinan Lin9e4917d2019-11-04 10:58:47 -0800149 def _build_request(self, reqs_json, tags):
Xinan Lin3ba18a02019-08-13 15:44:55 -0700150 """Generate ScheduleBuildRequest for calling buildbucket.
Xinan Linc61196b2019-08-13 10:37:30 -0700151
Xinan Lin3ba18a02019-08-13 15:44:55 -0700152 Args:
Xinan Lin9e4917d2019-11-04 10:58:47 -0800153 reqs_json: A json string of requests.
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700154 tags: A list of tags for the buildbucket build.
Xinan Linc61196b2019-08-13 10:37:30 -0700155
Xinan Lin3ba18a02019-08-13 15:44:55 -0700156 Returns:
157 A ScheduleBuildRequest instance.
158 """
Xinan Lin9e4917d2019-11-04 10:58:47 -0800159 requests_struct = struct_pb2.Struct()
160 recipe_struct = json_format.Parse(reqs_json, requests_struct)
Sean McAllisteraf32bfb2021-07-19 09:20:17 -0600161 return builds_service_pb2.ScheduleBuildRequest(builder=self.builder,
162 properties=recipe_struct,
163 request_id=str(
164 uuid.uuid1()),
165 tags=tags)
Xinan Linc61196b2019-08-13 10:37:30 -0700166
167 def _gen_service_account_key(self, sa):
Xinan Lin3ba18a02019-08-13 15:44:55 -0700168 """Generate credentials to authorize the call.
Xinan Linc61196b2019-08-13 10:37:30 -0700169
Xinan Lin3ba18a02019-08-13 15:44:55 -0700170 Args:
171 sa: A string of the path to the service account json file.
Xinan Linc61196b2019-08-13 10:37:30 -0700172
Xinan Lin3ba18a02019-08-13 15:44:55 -0700173 Returns:
174 A service account key.
175 """
Xinan Linc61196b2019-08-13 10:37:30 -0700176 service_credentials = service_account.ServiceAccountCredentials
Xinan Lin3ba18a02019-08-13 15:44:55 -0700177 key = service_credentials.from_json_keyfile_name(sa, self.scope)
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700178 return auth.ServiceAccountKey(client_email=key.service_account_email,
179 private_key=key._private_key_pkcs8_pem,
180 private_key_id=key._private_key_id)
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700181
182
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700183def _form_test_platform_request(task_params):
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700184 """Generates test_platform.Request proto to send to buildbucket.
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700185
186 Args:
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700187 task_params: dict containing the parameters of a task from the suite queue.
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700188
189 Returns:
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700190 A ctp_request.Request instance.
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700191 """
192 build = _infer_build_from_task_params(task_params)
193 if build == 'None':
194 raise BuildbucketRunError('No proper build in task params: %r' %
195 task_params)
196 pool = _infer_pool_from_task_params(task_params)
197 timeout = _infer_timeout_from_task_params(task_params)
198
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700199 request = ctp_request.Request()
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700200 params = request.params
201
Xinan Lin4757d6f2020-03-24 22:20:31 -0700202 params.scheduling.CopyFrom(_scheduling_for_pool(pool))
Prathmesh Prabhu46156ff2020-06-20 00:18:52 -0700203 if task_params.get('qs_account') not in ['None', None]:
Xinan Lin4757d6f2020-03-24 22:20:31 -0700204 params.scheduling.qs_account = task_params.get('qs_account')
205 # Quota Scheduler has no concept of priority.
Xinan Lin8bb5b4b2020-07-21 23:17:55 -0700206 if (task_params.get('priority') not in ['None', None]
207 and not params.scheduling.qs_account):
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700208 params.scheduling.priority = int(task_params['priority'])
209
210 params.software_dependencies.add().chromeos_build = build
211 params.software_attributes.build_target.name = task_params['board']
Aviv Keshetc679faf2019-11-27 17:52:50 -0800212
213 gs_url = GS_PREFIX + task_params['test_source_build']
214 params.metadata.test_metadata_url = gs_url
215 params.metadata.debug_symbols_archive_url = gs_url
Azizur Rahmanb720e512022-05-13 18:30:40 +0000216 params.metadata.container_metadata_url = os.path.join(gs_url, CONTAINER_METADATA_LOC)
Aviv Keshetc679faf2019-11-27 17:52:50 -0800217
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700218 params.time.maximum_duration.FromTimedelta(timeout)
219
220 for key, value in _request_tags(task_params, build, pool).iteritems():
221 params.decorations.tags.append('%s:%s' % (key, value))
222
Xinan Lin7bf266a2020-06-10 23:54:26 -0700223 for d in _infer_user_defined_dimensions(task_params):
224 params.freeform_attributes.swarming_dimensions.append(d)
Xinan Linba3b9322020-04-24 15:08:12 -0700225
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700226 if task_params['model'] != 'None':
227 params.hardware_attributes.model = task_params['model']
228
229 if task_params['job_retry'] == 'True':
230 params.retry.allow = True
231 params.retry.max = constants.Buildbucket.MAX_RETRY
232
Azizur Rahmanb720e512022-05-13 18:30:40 +0000233 params.run_via_cft = (task_params['run_via_cft'] == 'True')
234
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700235 fw_rw_build = task_params.get(build_lib.BuildVersionKey.FW_RW_VERSION)
236 fw_ro_build = task_params.get(build_lib.BuildVersionKey.FW_RO_VERSION)
Brigit Rossbachbb080912020-11-18 13:52:17 -0700237
238 if task_params.has_key('firmware_ro_version'):
239 build = params.software_dependencies.add()
240 build.ro_firmware_build = task_params['firmware_ro_version']
241
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700242 # Skip firmware field if None(unspecified) or 'None'(no such build).
243 if fw_ro_build not in (None, 'None'):
244 build = params.software_dependencies.add()
245 build.ro_firmware_build = fw_ro_build
Brigit Rossbachbb080912020-11-18 13:52:17 -0700246
247 if task_params.has_key('firmware_rw_version'):
248 build = params.software_dependencies.add()
249 build.rw_firmware_build = task_params['firmware_rw_version']
250
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700251 if fw_rw_build not in (None, 'None'):
252 build = params.software_dependencies.add()
253 build.rw_firmware_build = fw_rw_build
254
Garry Wang2104bf52021-07-19 18:13:12 -0700255 secondary_targets = task_params.get('secondary_targets')
Garry Wang111a26f2021-07-23 15:25:14 -0700256 secondary_targets = restruct_secondary_targets_from_string(secondary_targets)
Garry Wang2104bf52021-07-19 18:13:12 -0700257 if secondary_targets:
258 for s_target in secondary_targets:
259 s_device = params.secondary_devices.add()
260 s_device.software_attributes.build_target.name = s_target.board
261 if s_target.model:
262 s_device.hardware_attributes.model = s_target.model
263 if s_target.cros_build:
264 s_build = s_device.software_dependencies.add()
265 s_build.chromeos_build = s_target.cros_build
266
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700267 request.test_plan.suite.add().name = task_params['suite']
268 return request
269
Sean McAllisteraf32bfb2021-07-19 09:20:17 -0600270
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700271def _scheduling_for_pool(pool):
272 """Assign appropriate pool name to scheduling instance.
273
274 Args:
Xinan Lin1516edb2020-07-05 23:13:54 -0700275 pool: string pool name (e.g. 'MANAGED_POOL_QUOTA', 'wificell').
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700276
277 Returns:
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700278 scheduling: A ctp_request.Request.Params.Scheduling instance.
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700279 """
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700280 mp = ctp_request.Request.Params.Scheduling.ManagedPool
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700281 if mp.DESCRIPTOR.values_by_name.get(pool) is not None:
Jacob Kopczynski408f3fd2020-08-07 13:31:59 -0700282 return ctp_request.Request.Params.Scheduling(managed_pool=mp.Value(pool))
283 return ctp_request.Request.Params.Scheduling(unmanaged_pool=pool)
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700284
285
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700286# Also see: A copy of this function in swarming_lib.py.
287def _infer_build_from_task_params(task_params):
288 """Infer the build to install on the DUT for the scheduled task.
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700289
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700290 Args:
291 task_params: The parameters of a task loaded from suite queue.
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700292
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700293 Returns:
294 A string representing the build to run.
295 """
296 cros_build = task_params[build_lib.BuildVersionKey.CROS_VERSION]
297 android_build = task_params[build_lib.BuildVersionKey.ANDROID_BUILD_VERSION]
298 testbed_build = task_params[build_lib.BuildVersionKey.TESTBED_BUILD_VERSION]
299 return cros_build or android_build or testbed_build
300
301
302def _infer_pool_from_task_params(task_params):
303 """Infer the pool to use for the scheduled task.
304
305 Args:
306 task_params: The parameters of a task loaded from suite queue.
307
308 Returns:
309 A string pool to schedule task in.
310 """
311 if task_params.get('override_qs_account'):
312 return 'DUT_POOL_QUOTA'
313 return task_params.get('override_pool') or task_params['pool']
314
315
316def _infer_timeout_from_task_params(task_params):
317 """Infer the timeout for the scheduled task.
318
319 Args:
320 task_params: The parameters of a task loaded from suite queue.
321
322 Returns:
323 A datetime.timedelta instance for the timeout.
324 """
325 timeout_mins = int(task_params['timeout_mins'])
326 # timeout's unit is hour.
327 if task_params.get('timeout'):
Sean Abrahamec0d0762020-09-18 17:19:05 +0000328 timeout_mins = max(int(task_params['timeout']) * 60, timeout_mins)
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700329 if timeout_mins > constants.Buildbucket.MAX_BUILDBUCKET_TIMEOUT_MINS:
330 timeout_mins = constants.Buildbucket.MAX_BUILDBUCKET_TIMEOUT_MINS
331 return datetime.timedelta(minutes=timeout_mins)
332
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700333
Xinan Linba3b9322020-04-24 15:08:12 -0700334def _infer_user_defined_dimensions(task_params):
335 """Infer the dimensions defined by users.
336
337 Args:
338 task_params: The parameters of a task loaded from suite queue.
339
340 Returns:
341 A list of strings; an empty list if no dimensions set.
342
343 Raises:
344 ValueError: if dimension is not valid.
345 """
346 result = []
347 if task_params.get('dimensions') in (None, 'None'):
348 return result
Patrick Meiringd4f60772021-03-04 12:09:28 +1100349 dims = [d.lstrip() for d in task_params.get('dimensions').split(',')]
350 for d in dims:
Xinan Linba3b9322020-04-24 15:08:12 -0700351 if len(d.split(':')) != 2:
352 raise ValueError(
353 'Job %s has invalid dimensions: %s' %
354 (task_params.get('name'), task_params.get('dimensions')))
355 result.append(d)
356 return result
357
358
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700359def _request_tags(task_params, build, pool):
360 """Infer tags to include in cros_test_platform request.
361
362 Args:
363 task_params: suite task parameters.
364 build: The build included in the request. Must not be None.
365 pool: The DUT pool used for the request. Must not be None.
366
367 Returns:
368 A dict of tags.
369 """
370 tags = {
Prathmesh Prabhuaf0857f2020-06-20 00:16:32 -0700371 'build': build,
372 'label-pool': pool,
Taylor Clarke12ec9a2021-02-18 22:22:19 +0000373 'ctp-fwd-task-name': task_params.get('name')
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700374 }
Xinan Linfb63d572019-09-24 15:49:04 -0700375 if task_params.get('board') not in (None, 'None'):
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700376 tags['label-board'] = task_params['board']
Xinan Linfb63d572019-09-24 15:49:04 -0700377 if task_params.get('model') not in (None, 'None'):
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700378 tags['label-model'] = task_params['model']
Xinan Linfb63d572019-09-24 15:49:04 -0700379 if task_params.get('suite') not in (None, 'None'):
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700380 tags['suite'] = task_params['suite']
Taylor Clarke12ec9a2021-02-18 22:22:19 +0000381 if task_params.get('analytics_name') not in (None, 'None'):
382 tags['analytics_name'] = task_params['analytics_name']
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700383 return tags
384
385
Xinan Linc8647112020-02-04 16:45:56 -0800386def _get_key_val_from_label(label):
387 """A helper to get key and value from the label.
388
389 Args:
390 label: A string of label, should be in the form of
391 key:value, e.g. 'pool:ChromeOSSkylab'.
392 """
393 res = label.split(':')
394 if len(res) == 2:
395 return res[0], res[1]
396 logging.warning('Failed to parse the label, %s', label)
397
398
Dhanya Ganeshedb78cb2020-07-20 19:40:50 +0000399def _bb_tags(suite):
400 """Get all the tags required for Buildbucket.
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700401
402 Args:
Dhanya Ganeshedb78cb2020-07-20 19:40:50 +0000403 suite: A string of suite name being scheduled.
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700404
405 Returns:
406 [bb_common_pb2.StringPair] tags to include the buildbucket request.
407 """
Sean McAllisteraf32bfb2021-07-19 09:20:17 -0600408 return [
Jared Loucks438bf3a2022-04-06 13:43:15 -0600409 bb_common_pb2.StringPair(key='label-suite', value=suite),
Sean McAllisteraf32bfb2021-07-19 09:20:17 -0600410 bb_common_pb2.StringPair(key='suite', value=suite),
411 bb_common_pb2.StringPair(key='user_agent', value='suite_scheduler')
412 ]
Xinan Linc54a7462020-04-17 15:39:01 -0700413
414
415def _should_skip(params):
416 """Decide whether to skip a task based on env and pool.
417
418 Suite request from staging may still have a small chance to run
419 in production. However, for unmanaged pools(e.g. wificell), which
420 usually are small, dev traffic is unacceptable.
421
422 Args:
423 params: dict containing the parameters of a task got from suite
424 queue.
425
426 Returns:
427 A boolean; true for suite targetting non-default pools from staging
428 env.
429 """
430 if constants.application_id() == constants.AppID.PROD_APP:
431 return False
Xinan Lin0d7910d2020-07-21 11:06:45 -0700432 return params['pool'] != 'MANAGED_POOL_QUOTA'