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