Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 1 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 4 | """Module for interacting with Buildbucket.""" |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 5 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 6 | import collections |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 7 | import datetime |
linxinan | e5eb455 | 2019-08-26 05:44:45 +0000 | [diff] [blame] | 8 | import logging |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 9 | import re |
| 10 | import string |
Jacob Kopczynski | e56bf49 | 2020-08-07 13:20:12 -0700 | [diff] [blame] | 11 | import types |
Xinan Lin | 081b5d3 | 2020-03-23 17:37:55 -0700 | [diff] [blame] | 12 | import uuid |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 13 | |
Xinan Lin | df0698a | 2020-02-05 22:38:11 -0800 | [diff] [blame] | 14 | import analytics |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 15 | import build_lib |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 16 | import constants |
| 17 | import file_getter |
| 18 | |
Jacob Kopczynski | 408f3fd | 2020-08-07 13:31:59 -0700 | [diff] [blame] | 19 | from chromite.api.gen.test_platform import request_pb2 as ctp_request |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 20 | from components import auth |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 21 | from components.prpc import client as prpc_client |
Sean McAllister | af32bfb | 2021-07-19 09:20:17 -0600 | [diff] [blame^] | 22 | from chromite.third_party.infra_libs.buildbucket.proto import common_pb2 as bb_common_pb2 |
| 23 | from chromite.third_party.infra_libs.buildbucket.proto import builds_service_pb2, builder_pb2, build_pb2 |
| 24 | from chromite.third_party.infra_libs.buildbucket.proto.builds_service_prpc_pb2 import BuildsServiceDescription |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 25 | |
| 26 | from oauth2client import service_account |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 27 | |
Sean McAllister | 66bf7e9 | 2021-07-16 18:46:04 +0000 | [diff] [blame] | 28 | from google.protobuf import json_format, struct_pb2 |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 29 | |
Xinan Lin | 6e09738 | 2019-08-27 18:43:35 -0700 | [diff] [blame] | 30 | GS_PREFIX = 'gs://chromeos-image-archive/' |
| 31 | |
Xinan Lin | 57e2d96 | 2020-03-30 17:24:53 -0700 | [diff] [blame] | 32 | # 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) |
| 36 | PRPC_TIMEOUT_SEC = 55 |
| 37 | |
| 38 | |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 39 | def _get_client(address): |
| 40 | """Create a prpc client instance for given address.""" |
| 41 | return prpc_client.Client(address, BuildsServiceDescription) |
| 42 | |
| 43 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 44 | class BuildbucketRunError(Exception): |
| 45 | """Raised when interactions with buildbucket server fail.""" |
| 46 | |
| 47 | |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 48 | class TestPlatformClient(object): |
| 49 | """prpc client for cros_test_platform, aka frontdoor.""" |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 50 | def __init__(self, address, project, bucket, builder): |
| 51 | self.client = _get_client(address) |
Sean McAllister | af32bfb | 2021-07-19 09:20:17 -0600 | [diff] [blame^] | 52 | self.builder = builder_pb2.BuilderID(project=project, |
| 53 | bucket=bucket, |
| 54 | builder=builder) |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 55 | self.scope = 'https://www.googleapis.com/auth/userinfo.email' |
| 56 | self.running_env = constants.environment() |
| 57 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 58 | def multirequest_run(self, tasks, suite): |
Xinan Lin | 1e8e791 | 2020-07-31 09:52:16 -0700 | [diff] [blame] | 59 | """Call cros_test_platform Builder to schedule a batch of suite tests. |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 60 | |
| 61 | Args: |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 62 | tasks: The suite tasks to run. |
| 63 | suite: suite name for the batch request. |
| 64 | |
| 65 | Returns: |
| 66 | List of executed tasks. |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 67 | |
| 68 | Raises: |
| 69 | BuildbucketRunError: if failed to get build info from task parameters. |
| 70 | """ |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 71 | requests = [] |
| 72 | executed_tasks = [] |
| 73 | counter = collections.defaultdict(int) |
Xinan Lin | df0698a | 2020-02-05 22:38:11 -0800 | [diff] [blame] | 74 | task_executions = [] |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 75 | for task in tasks: |
| 76 | try: |
| 77 | params = task.extract_params() |
Xinan Lin | c54a746 | 2020-04-17 15:39:01 -0700 | [diff] [blame] | 78 | if _should_skip(params): |
| 79 | continue |
Xinan Lin | 8bb5b4b | 2020-07-21 23:17:55 -0700 | [diff] [blame] | 80 | req = _form_test_platform_request(params) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 81 | req_json = json_format.MessageToJson(req) |
| 82 | counter_key = params['board'] |
Prathmesh Prabhu | af0857f | 2020-06-20 00:16:32 -0700 | [diff] [blame] | 83 | counter_key += '' if params['model'] in [None, 'None' |
| 84 | ] else ('_' + params['model']) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 85 | counter[counter_key] += 1 |
| 86 | req_name = counter_key |
| 87 | if counter[counter_key] > 1: |
| 88 | req_name += '_' + str(counter[counter_key] - 1) |
| 89 | requests.append('"%s": %s' % (req_name, req_json)) |
| 90 | executed_tasks.append(task) |
Xinan Lin | 083ba8f | 2020-02-06 13:55:18 -0800 | [diff] [blame] | 91 | if params.get('task_id'): |
Xinan Lin | 9b17c5b | 2020-08-06 10:43:30 -0700 | [diff] [blame] | 92 | task_executions.append( |
| 93 | analytics.ExecutionTask(params['task_id'], req_name)) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 94 | except (ValueError, BuildbucketRunError): |
Xinan Lin | ba3b932 | 2020-04-24 15:08:12 -0700 | [diff] [blame] | 95 | logging.error('Failed to process task: %r', params) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 96 | if not requests: |
| 97 | return [] |
| 98 | try: |
| 99 | requests_json = '{ "requests": { %s } }' % ', '.join(requests) |
Dhanya Ganesh | edb78cb | 2020-07-20 19:40:50 +0000 | [diff] [blame] | 100 | req_build = self._build_request(requests_json, _bb_tags(suite)) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 101 | logging.debug('Raw request to buildbucket: %r', req_build) |
linxinan | e5eb455 | 2019-08-26 05:44:45 +0000 | [diff] [blame] | 102 | |
Prathmesh Prabhu | af0857f | 2020-06-20 00:16:32 -0700 | [diff] [blame] | 103 | if (self.running_env == constants.RunningEnv.ENV_STANDALONE |
| 104 | or self.running_env == constants.RunningEnv.ENV_DEVELOPMENT_SERVER): |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 105 | # If running locally, use the staging service account. |
| 106 | sa_key = self._gen_service_account_key( |
| 107 | file_getter.STAGING_CLIENT_SECRETS_FILE) |
| 108 | cred = prpc_client.service_account_credentials( |
| 109 | service_account_key=sa_key) |
| 110 | else: |
| 111 | cred = prpc_client.service_account_credentials() |
Prathmesh Prabhu | af0857f | 2020-06-20 00:16:32 -0700 | [diff] [blame] | 112 | resp = self.client.ScheduleBuild(req_build, |
| 113 | credentials=cred, |
| 114 | timeout=PRPC_TIMEOUT_SEC) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 115 | logging.debug('Response from buildbucket: %r', resp) |
Xinan Lin | df0698a | 2020-02-05 22:38:11 -0800 | [diff] [blame] | 116 | for t in task_executions: |
| 117 | t.update_result(resp) |
| 118 | try: |
| 119 | if not t.upload(): |
| 120 | logging.warning('Failed to insert row: %r', t) |
| 121 | # For any exceptions from BQ, only log it. |
Prathmesh Prabhu | af0857f | 2020-06-20 00:16:32 -0700 | [diff] [blame] | 122 | except Exception as e: #pylint: disable=broad-except |
| 123 | logging.exception('Failed to insert row: %r, got error: %s', t, |
| 124 | str(e)) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 125 | return executed_tasks |
| 126 | except Exception as e: |
| 127 | logging.debug('Failed to process tasks: %r', tasks) |
| 128 | logging.exception(str(e)) |
| 129 | return [] |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 130 | |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 131 | def dummy_run(self): |
| 132 | """Perform a dummy run of prpc call to cros_test_platform-dev.""" |
| 133 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 134 | requests_json = '{ "requests": { "dummy": {} } }' |
| 135 | req_build = self._build_request(requests_json, tags=None) |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 136 | # Use the staging service account to authorize the request. |
| 137 | sa_key = self._gen_service_account_key( |
| 138 | file_getter.STAGING_CLIENT_SECRETS_FILE) |
| 139 | cred = prpc_client.service_account_credentials(service_account_key=sa_key) |
Prathmesh Prabhu | af0857f | 2020-06-20 00:16:32 -0700 | [diff] [blame] | 140 | return self.client.ScheduleBuild(req_build, |
| 141 | credentials=cred, |
| 142 | timeout=PRPC_TIMEOUT_SEC) |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 143 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 144 | def _build_request(self, reqs_json, tags): |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 145 | """Generate ScheduleBuildRequest for calling buildbucket. |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 146 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 147 | Args: |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 148 | reqs_json: A json string of requests. |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 149 | tags: A list of tags for the buildbucket build. |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 150 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 151 | Returns: |
| 152 | A ScheduleBuildRequest instance. |
| 153 | """ |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 154 | requests_struct = struct_pb2.Struct() |
| 155 | recipe_struct = json_format.Parse(reqs_json, requests_struct) |
Sean McAllister | af32bfb | 2021-07-19 09:20:17 -0600 | [diff] [blame^] | 156 | return builds_service_pb2.ScheduleBuildRequest(builder=self.builder, |
| 157 | properties=recipe_struct, |
| 158 | request_id=str( |
| 159 | uuid.uuid1()), |
| 160 | tags=tags) |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 161 | |
| 162 | def _gen_service_account_key(self, sa): |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 163 | """Generate credentials to authorize the call. |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 164 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 165 | Args: |
| 166 | sa: A string of the path to the service account json file. |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 167 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 168 | Returns: |
| 169 | A service account key. |
| 170 | """ |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 171 | service_credentials = service_account.ServiceAccountCredentials |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 172 | key = service_credentials.from_json_keyfile_name(sa, self.scope) |
Prathmesh Prabhu | af0857f | 2020-06-20 00:16:32 -0700 | [diff] [blame] | 173 | return auth.ServiceAccountKey(client_email=key.service_account_email, |
| 174 | private_key=key._private_key_pkcs8_pem, |
| 175 | private_key_id=key._private_key_id) |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 176 | |
| 177 | |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 178 | def _form_test_platform_request(task_params): |
Jacob Kopczynski | 408f3fd | 2020-08-07 13:31:59 -0700 | [diff] [blame] | 179 | """Generates test_platform.Request proto to send to buildbucket. |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 180 | |
| 181 | Args: |
Jacob Kopczynski | 408f3fd | 2020-08-07 13:31:59 -0700 | [diff] [blame] | 182 | task_params: dict containing the parameters of a task from the suite queue. |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 183 | |
| 184 | Returns: |
Jacob Kopczynski | 408f3fd | 2020-08-07 13:31:59 -0700 | [diff] [blame] | 185 | A ctp_request.Request instance. |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 186 | """ |
| 187 | build = _infer_build_from_task_params(task_params) |
| 188 | if build == 'None': |
| 189 | raise BuildbucketRunError('No proper build in task params: %r' % |
| 190 | task_params) |
| 191 | pool = _infer_pool_from_task_params(task_params) |
| 192 | timeout = _infer_timeout_from_task_params(task_params) |
| 193 | |
Jacob Kopczynski | 408f3fd | 2020-08-07 13:31:59 -0700 | [diff] [blame] | 194 | request = ctp_request.Request() |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 195 | params = request.params |
| 196 | |
Xinan Lin | 4757d6f | 2020-03-24 22:20:31 -0700 | [diff] [blame] | 197 | params.scheduling.CopyFrom(_scheduling_for_pool(pool)) |
Prathmesh Prabhu | 46156ff | 2020-06-20 00:18:52 -0700 | [diff] [blame] | 198 | if task_params.get('qs_account') not in ['None', None]: |
Xinan Lin | 4757d6f | 2020-03-24 22:20:31 -0700 | [diff] [blame] | 199 | params.scheduling.qs_account = task_params.get('qs_account') |
| 200 | # Quota Scheduler has no concept of priority. |
Xinan Lin | 8bb5b4b | 2020-07-21 23:17:55 -0700 | [diff] [blame] | 201 | if (task_params.get('priority') not in ['None', None] |
| 202 | and not params.scheduling.qs_account): |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 203 | params.scheduling.priority = int(task_params['priority']) |
| 204 | |
| 205 | params.software_dependencies.add().chromeos_build = build |
| 206 | params.software_attributes.build_target.name = task_params['board'] |
Aviv Keshet | c679faf | 2019-11-27 17:52:50 -0800 | [diff] [blame] | 207 | |
| 208 | gs_url = GS_PREFIX + task_params['test_source_build'] |
| 209 | params.metadata.test_metadata_url = gs_url |
| 210 | params.metadata.debug_symbols_archive_url = gs_url |
| 211 | |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 212 | params.time.maximum_duration.FromTimedelta(timeout) |
| 213 | |
| 214 | for key, value in _request_tags(task_params, build, pool).iteritems(): |
| 215 | params.decorations.tags.append('%s:%s' % (key, value)) |
| 216 | |
Xinan Lin | 7bf266a | 2020-06-10 23:54:26 -0700 | [diff] [blame] | 217 | for d in _infer_user_defined_dimensions(task_params): |
| 218 | params.freeform_attributes.swarming_dimensions.append(d) |
Xinan Lin | ba3b932 | 2020-04-24 15:08:12 -0700 | [diff] [blame] | 219 | |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 220 | if task_params['model'] != 'None': |
| 221 | params.hardware_attributes.model = task_params['model'] |
| 222 | |
| 223 | if task_params['job_retry'] == 'True': |
| 224 | params.retry.allow = True |
| 225 | params.retry.max = constants.Buildbucket.MAX_RETRY |
| 226 | |
| 227 | fw_rw_build = task_params.get(build_lib.BuildVersionKey.FW_RW_VERSION) |
| 228 | fw_ro_build = task_params.get(build_lib.BuildVersionKey.FW_RO_VERSION) |
Brigit Rossbach | bb08091 | 2020-11-18 13:52:17 -0700 | [diff] [blame] | 229 | |
| 230 | if task_params.has_key('firmware_ro_version'): |
| 231 | build = params.software_dependencies.add() |
| 232 | build.ro_firmware_build = task_params['firmware_ro_version'] |
| 233 | |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 234 | # Skip firmware field if None(unspecified) or 'None'(no such build). |
| 235 | if fw_ro_build not in (None, 'None'): |
| 236 | build = params.software_dependencies.add() |
| 237 | build.ro_firmware_build = fw_ro_build |
Brigit Rossbach | bb08091 | 2020-11-18 13:52:17 -0700 | [diff] [blame] | 238 | |
| 239 | if task_params.has_key('firmware_rw_version'): |
| 240 | build = params.software_dependencies.add() |
| 241 | build.rw_firmware_build = task_params['firmware_rw_version'] |
| 242 | |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 243 | if fw_rw_build not in (None, 'None'): |
| 244 | build = params.software_dependencies.add() |
| 245 | build.rw_firmware_build = fw_rw_build |
| 246 | |
| 247 | request.test_plan.suite.add().name = task_params['suite'] |
| 248 | return request |
| 249 | |
Sean McAllister | af32bfb | 2021-07-19 09:20:17 -0600 | [diff] [blame^] | 250 | |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 251 | def _scheduling_for_pool(pool): |
| 252 | """Assign appropriate pool name to scheduling instance. |
| 253 | |
| 254 | Args: |
Xinan Lin | 1516edb | 2020-07-05 23:13:54 -0700 | [diff] [blame] | 255 | pool: string pool name (e.g. 'MANAGED_POOL_QUOTA', 'wificell'). |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 256 | |
| 257 | Returns: |
Jacob Kopczynski | 408f3fd | 2020-08-07 13:31:59 -0700 | [diff] [blame] | 258 | scheduling: A ctp_request.Request.Params.Scheduling instance. |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 259 | """ |
Jacob Kopczynski | 408f3fd | 2020-08-07 13:31:59 -0700 | [diff] [blame] | 260 | mp = ctp_request.Request.Params.Scheduling.ManagedPool |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 261 | if mp.DESCRIPTOR.values_by_name.get(pool) is not None: |
Jacob Kopczynski | 408f3fd | 2020-08-07 13:31:59 -0700 | [diff] [blame] | 262 | return ctp_request.Request.Params.Scheduling(managed_pool=mp.Value(pool)) |
| 263 | return ctp_request.Request.Params.Scheduling(unmanaged_pool=pool) |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 264 | |
| 265 | |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 266 | # Also see: A copy of this function in swarming_lib.py. |
| 267 | def _infer_build_from_task_params(task_params): |
| 268 | """Infer the build to install on the DUT for the scheduled task. |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 269 | |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 270 | Args: |
| 271 | task_params: The parameters of a task loaded from suite queue. |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 272 | |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 273 | Returns: |
| 274 | A string representing the build to run. |
| 275 | """ |
| 276 | cros_build = task_params[build_lib.BuildVersionKey.CROS_VERSION] |
| 277 | android_build = task_params[build_lib.BuildVersionKey.ANDROID_BUILD_VERSION] |
| 278 | testbed_build = task_params[build_lib.BuildVersionKey.TESTBED_BUILD_VERSION] |
| 279 | return cros_build or android_build or testbed_build |
| 280 | |
| 281 | |
| 282 | def _infer_pool_from_task_params(task_params): |
| 283 | """Infer the pool to use for the scheduled task. |
| 284 | |
| 285 | Args: |
| 286 | task_params: The parameters of a task loaded from suite queue. |
| 287 | |
| 288 | Returns: |
| 289 | A string pool to schedule task in. |
| 290 | """ |
| 291 | if task_params.get('override_qs_account'): |
| 292 | return 'DUT_POOL_QUOTA' |
| 293 | return task_params.get('override_pool') or task_params['pool'] |
| 294 | |
| 295 | |
| 296 | def _infer_timeout_from_task_params(task_params): |
| 297 | """Infer the timeout for the scheduled task. |
| 298 | |
| 299 | Args: |
| 300 | task_params: The parameters of a task loaded from suite queue. |
| 301 | |
| 302 | Returns: |
| 303 | A datetime.timedelta instance for the timeout. |
| 304 | """ |
| 305 | timeout_mins = int(task_params['timeout_mins']) |
| 306 | # timeout's unit is hour. |
| 307 | if task_params.get('timeout'): |
Sean Abraham | ec0d076 | 2020-09-18 17:19:05 +0000 | [diff] [blame] | 308 | timeout_mins = max(int(task_params['timeout']) * 60, timeout_mins) |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 309 | if timeout_mins > constants.Buildbucket.MAX_BUILDBUCKET_TIMEOUT_MINS: |
| 310 | timeout_mins = constants.Buildbucket.MAX_BUILDBUCKET_TIMEOUT_MINS |
| 311 | return datetime.timedelta(minutes=timeout_mins) |
| 312 | |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 313 | |
Xinan Lin | ba3b932 | 2020-04-24 15:08:12 -0700 | [diff] [blame] | 314 | def _infer_user_defined_dimensions(task_params): |
| 315 | """Infer the dimensions defined by users. |
| 316 | |
| 317 | Args: |
| 318 | task_params: The parameters of a task loaded from suite queue. |
| 319 | |
| 320 | Returns: |
| 321 | A list of strings; an empty list if no dimensions set. |
| 322 | |
| 323 | Raises: |
| 324 | ValueError: if dimension is not valid. |
| 325 | """ |
| 326 | result = [] |
| 327 | if task_params.get('dimensions') in (None, 'None'): |
| 328 | return result |
Patrick Meiring | d4f6077 | 2021-03-04 12:09:28 +1100 | [diff] [blame] | 329 | dims = [d.lstrip() for d in task_params.get('dimensions').split(',')] |
| 330 | for d in dims: |
Xinan Lin | ba3b932 | 2020-04-24 15:08:12 -0700 | [diff] [blame] | 331 | if len(d.split(':')) != 2: |
| 332 | raise ValueError( |
| 333 | 'Job %s has invalid dimensions: %s' % |
| 334 | (task_params.get('name'), task_params.get('dimensions'))) |
| 335 | result.append(d) |
| 336 | return result |
| 337 | |
| 338 | |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 339 | def _request_tags(task_params, build, pool): |
| 340 | """Infer tags to include in cros_test_platform request. |
| 341 | |
| 342 | Args: |
| 343 | task_params: suite task parameters. |
| 344 | build: The build included in the request. Must not be None. |
| 345 | pool: The DUT pool used for the request. Must not be None. |
| 346 | |
| 347 | Returns: |
| 348 | A dict of tags. |
| 349 | """ |
| 350 | tags = { |
Prathmesh Prabhu | af0857f | 2020-06-20 00:16:32 -0700 | [diff] [blame] | 351 | 'build': build, |
| 352 | 'label-pool': pool, |
Taylor Clark | e12ec9a | 2021-02-18 22:22:19 +0000 | [diff] [blame] | 353 | 'ctp-fwd-task-name': task_params.get('name') |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 354 | } |
Xinan Lin | fb63d57 | 2019-09-24 15:49:04 -0700 | [diff] [blame] | 355 | if task_params.get('board') not in (None, 'None'): |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 356 | tags['label-board'] = task_params['board'] |
Xinan Lin | fb63d57 | 2019-09-24 15:49:04 -0700 | [diff] [blame] | 357 | if task_params.get('model') not in (None, 'None'): |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 358 | tags['label-model'] = task_params['model'] |
Xinan Lin | fb63d57 | 2019-09-24 15:49:04 -0700 | [diff] [blame] | 359 | if task_params.get('suite') not in (None, 'None'): |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 360 | tags['suite'] = task_params['suite'] |
Taylor Clark | e12ec9a | 2021-02-18 22:22:19 +0000 | [diff] [blame] | 361 | if task_params.get('analytics_name') not in (None, 'None'): |
| 362 | tags['analytics_name'] = task_params['analytics_name'] |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 363 | return tags |
| 364 | |
| 365 | |
Xinan Lin | c864711 | 2020-02-04 16:45:56 -0800 | [diff] [blame] | 366 | def _get_key_val_from_label(label): |
| 367 | """A helper to get key and value from the label. |
| 368 | |
| 369 | Args: |
| 370 | label: A string of label, should be in the form of |
| 371 | key:value, e.g. 'pool:ChromeOSSkylab'. |
| 372 | """ |
| 373 | res = label.split(':') |
| 374 | if len(res) == 2: |
| 375 | return res[0], res[1] |
| 376 | logging.warning('Failed to parse the label, %s', label) |
| 377 | |
| 378 | |
Dhanya Ganesh | edb78cb | 2020-07-20 19:40:50 +0000 | [diff] [blame] | 379 | def _bb_tags(suite): |
| 380 | """Get all the tags required for Buildbucket. |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 381 | |
| 382 | Args: |
Dhanya Ganesh | edb78cb | 2020-07-20 19:40:50 +0000 | [diff] [blame] | 383 | suite: A string of suite name being scheduled. |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 384 | |
| 385 | Returns: |
| 386 | [bb_common_pb2.StringPair] tags to include the buildbucket request. |
| 387 | """ |
Sean McAllister | af32bfb | 2021-07-19 09:20:17 -0600 | [diff] [blame^] | 388 | return [ |
| 389 | bb_common_pb2.StringPair(key='suite', value=suite), |
| 390 | bb_common_pb2.StringPair(key='user_agent', value='suite_scheduler') |
| 391 | ] |
Xinan Lin | c54a746 | 2020-04-17 15:39:01 -0700 | [diff] [blame] | 392 | |
| 393 | |
| 394 | def _should_skip(params): |
| 395 | """Decide whether to skip a task based on env and pool. |
| 396 | |
| 397 | Suite request from staging may still have a small chance to run |
| 398 | in production. However, for unmanaged pools(e.g. wificell), which |
| 399 | usually are small, dev traffic is unacceptable. |
| 400 | |
| 401 | Args: |
| 402 | params: dict containing the parameters of a task got from suite |
| 403 | queue. |
| 404 | |
| 405 | Returns: |
| 406 | A boolean; true for suite targetting non-default pools from staging |
| 407 | env. |
| 408 | """ |
| 409 | if constants.application_id() == constants.AppID.PROD_APP: |
| 410 | return False |
Xinan Lin | 0d7910d | 2020-07-21 11:06:45 -0700 | [diff] [blame] | 411 | return params['pool'] != 'MANAGED_POOL_QUOTA' |