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. |
| 4 | |
| 5 | """Module for interacting with Buildbucket.""" |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 6 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 7 | import collections |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 8 | import datetime |
linxinan | e5eb455 | 2019-08-26 05:44:45 +0000 | [diff] [blame] | 9 | import logging |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 10 | import re |
| 11 | import string |
| 12 | |
| 13 | import build_lib |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 14 | import constants |
| 15 | import file_getter |
| 16 | |
| 17 | from chromite.api.gen.test_platform import request_pb2 |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 18 | from components import auth |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 19 | from components.prpc import client as prpc_client |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 20 | from infra_libs.buildbucket.proto import common_pb2 as bb_common_pb2 |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 21 | from infra_libs.buildbucket.proto import rpc_pb2, build_pb2 |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 22 | from infra_libs.buildbucket.proto.rpc_prpc_pb2 import BuildsServiceDescription |
| 23 | |
| 24 | from oauth2client import service_account |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 25 | |
| 26 | from google.protobuf import json_format, struct_pb2 |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 27 | |
| 28 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 29 | _enum = request_pb2.Request.Params.Scheduling |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 30 | NONSTANDARD_POOL_NAMES = { |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 31 | 'cq': _enum.MANAGED_POOL_CQ, |
| 32 | 'bvt': _enum.MANAGED_POOL_BVT, |
| 33 | 'suites': _enum.MANAGED_POOL_SUITES, |
| 34 | 'cts': _enum.MANAGED_POOL_CTS, |
| 35 | 'cts-perbuild': _enum.MANAGED_POOL_CTS_PERBUILD, |
| 36 | 'continuous': _enum.MANAGED_POOL_CONTINUOUS, |
| 37 | 'arc-presubmit': _enum.MANAGED_POOL_ARC_PRESUBMIT, |
| 38 | 'quota': _enum.MANAGED_POOL_QUOTA, |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 41 | |
Xinan Lin | 6e09738 | 2019-08-27 18:43:35 -0700 | [diff] [blame] | 42 | GS_PREFIX = 'gs://chromeos-image-archive/' |
| 43 | |
| 44 | |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 45 | def _get_client(address): |
| 46 | """Create a prpc client instance for given address.""" |
| 47 | return prpc_client.Client(address, BuildsServiceDescription) |
| 48 | |
| 49 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 50 | class BuildbucketRunError(Exception): |
| 51 | """Raised when interactions with buildbucket server fail.""" |
| 52 | |
| 53 | |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 54 | class TestPlatformClient(object): |
| 55 | """prpc client for cros_test_platform, aka frontdoor.""" |
| 56 | |
| 57 | def __init__(self, address, project, bucket, builder): |
| 58 | self.client = _get_client(address) |
| 59 | self.builder = build_pb2.BuilderID(project=project, |
| 60 | bucket=bucket, |
| 61 | builder=builder) |
| 62 | self.scope = 'https://www.googleapis.com/auth/userinfo.email' |
| 63 | self.running_env = constants.environment() |
| 64 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 65 | def multirequest_run(self, tasks, suite): |
| 66 | """Call TestPlatform Builder to schedule a batch of tests. |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 67 | |
| 68 | Args: |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 69 | tasks: The suite tasks to run. |
| 70 | suite: suite name for the batch request. |
| 71 | |
| 72 | Returns: |
| 73 | List of executed tasks. |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 74 | |
| 75 | Raises: |
| 76 | BuildbucketRunError: if failed to get build info from task parameters. |
| 77 | """ |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 78 | requests = [] |
| 79 | executed_tasks = [] |
| 80 | counter = collections.defaultdict(int) |
| 81 | for task in tasks: |
| 82 | try: |
| 83 | params = task.extract_params() |
| 84 | req = _form_test_platform_request(params) |
| 85 | req_json = json_format.MessageToJson(req) |
| 86 | counter_key = params['board'] |
| 87 | counter_key += '' if params['model'] in [None, 'None'] else ( |
| 88 | '_' + params['model']) |
| 89 | counter[counter_key] += 1 |
| 90 | req_name = counter_key |
| 91 | if counter[counter_key] > 1: |
| 92 | req_name += '_' + str(counter[counter_key] - 1) |
| 93 | requests.append('"%s": %s' % (req_name, req_json)) |
| 94 | executed_tasks.append(task) |
| 95 | except (ValueError, BuildbucketRunError): |
| 96 | logging.debug('Failed to process task: %r', params) |
| 97 | if not requests: |
| 98 | return [] |
| 99 | try: |
| 100 | requests_json = '{ "requests": { %s } }' % ', '.join(requests) |
| 101 | req_build = self._build_request(requests_json, |
| 102 | _suite_bb_tag(suite)) |
| 103 | logging.debug('Raw request to buildbucket: %r', req_build) |
linxinan | e5eb455 | 2019-08-26 05:44:45 +0000 | [diff] [blame] | 104 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 105 | if (self.running_env == constants.RunningEnv.ENV_STANDALONE or |
| 106 | self.running_env == constants.RunningEnv.ENV_DEVELOPMENT_SERVER): |
| 107 | # If running locally, use the staging service account. |
| 108 | sa_key = self._gen_service_account_key( |
| 109 | file_getter.STAGING_CLIENT_SECRETS_FILE) |
| 110 | cred = prpc_client.service_account_credentials( |
| 111 | service_account_key=sa_key) |
| 112 | else: |
| 113 | cred = prpc_client.service_account_credentials() |
| 114 | #TODO(linxinan): only add the tasks returned from bb to executed_tasks. |
| 115 | resp = self.client.ScheduleBuild(req_build, credentials=cred) |
| 116 | logging.debug('Response from buildbucket: %r', resp) |
| 117 | return executed_tasks |
| 118 | except Exception as e: |
| 119 | logging.debug('Failed to process tasks: %r', tasks) |
| 120 | logging.exception(str(e)) |
| 121 | return [] |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 122 | |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 123 | def dummy_run(self): |
| 124 | """Perform a dummy run of prpc call to cros_test_platform-dev.""" |
| 125 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 126 | requests_json = '{ "requests": { "dummy": {} } }' |
| 127 | req_build = self._build_request(requests_json, tags=None) |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 128 | # Use the staging service account to authorize the request. |
| 129 | sa_key = self._gen_service_account_key( |
| 130 | file_getter.STAGING_CLIENT_SECRETS_FILE) |
| 131 | cred = prpc_client.service_account_credentials(service_account_key=sa_key) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 132 | return self.client.ScheduleBuild(req_build, credentials=cred) |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 133 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 134 | def _build_request(self, reqs_json, tags): |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 135 | """Generate ScheduleBuildRequest for calling buildbucket. |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 136 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 137 | Args: |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 138 | reqs_json: A json string of requests. |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 139 | tags: A list of tags for the buildbucket build. |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 140 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 141 | Returns: |
| 142 | A ScheduleBuildRequest instance. |
| 143 | """ |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 144 | requests_struct = struct_pb2.Struct() |
| 145 | recipe_struct = json_format.Parse(reqs_json, requests_struct) |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 146 | return rpc_pb2.ScheduleBuildRequest(builder=self.builder, |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 147 | properties=recipe_struct, |
| 148 | tags=tags) |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 149 | |
| 150 | def _gen_service_account_key(self, sa): |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 151 | """Generate credentials to authorize the call. |
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: |
| 154 | sa: A string of the path to the service account json file. |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 155 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 156 | Returns: |
| 157 | A service account key. |
| 158 | """ |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 159 | service_credentials = service_account.ServiceAccountCredentials |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 160 | key = service_credentials.from_json_keyfile_name(sa, self.scope) |
Xinan Lin | c61196b | 2019-08-13 10:37:30 -0700 | [diff] [blame] | 161 | return auth.ServiceAccountKey( |
| 162 | client_email=key.service_account_email, |
| 163 | private_key=key._private_key_pkcs8_pem, |
| 164 | private_key_id=key._private_key_id) |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 165 | |
| 166 | |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 167 | def _form_test_platform_request(task_params): |
| 168 | """Generate ScheduleBuildRequest for calling buildbucket. |
| 169 | |
| 170 | Args: |
| 171 | task_params: dict containing the parameters of a task got from suite |
| 172 | queue. |
| 173 | |
| 174 | Returns: |
| 175 | A request_pb2 instance. |
| 176 | """ |
| 177 | build = _infer_build_from_task_params(task_params) |
| 178 | if build == 'None': |
| 179 | raise BuildbucketRunError('No proper build in task params: %r' % |
| 180 | task_params) |
| 181 | pool = _infer_pool_from_task_params(task_params) |
| 182 | timeout = _infer_timeout_from_task_params(task_params) |
| 183 | |
| 184 | request = request_pb2.Request() |
| 185 | params = request.params |
| 186 | |
Xinan Lin | 2e19641 | 2019-10-24 14:31:13 -0700 | [diff] [blame] | 187 | if task_params.get('override_qs_account') or task_params.get('override_pool'): |
| 188 | logging.debug('Override qs account: %s. Override pool: %s.' % |
| 189 | (task_params.get('override_qs_account'), |
| 190 | task_params.get('override_pool'))) |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 191 | quota = task_params.get('override_qs_account') |
| 192 | if quota: |
| 193 | params.scheduling.quota_account = quota |
| 194 | else: |
| 195 | params.scheduling.CopyFrom(_scheduling_for_pool(pool)) |
| 196 | if 'priority' in task_params: |
| 197 | params.scheduling.priority = int(task_params['priority']) |
| 198 | |
| 199 | params.software_dependencies.add().chromeos_build = build |
| 200 | params.software_attributes.build_target.name = task_params['board'] |
| 201 | params.metadata.test_metadata_url = ( |
| 202 | GS_PREFIX + task_params['test_source_build'] |
| 203 | ) |
| 204 | params.time.maximum_duration.FromTimedelta(timeout) |
| 205 | |
| 206 | for key, value in _request_tags(task_params, build, pool).iteritems(): |
| 207 | params.decorations.tags.append('%s:%s' % (key, value)) |
| 208 | |
| 209 | if task_params['model'] != 'None': |
| 210 | params.hardware_attributes.model = task_params['model'] |
| 211 | |
| 212 | if task_params['job_retry'] == 'True': |
| 213 | params.retry.allow = True |
| 214 | params.retry.max = constants.Buildbucket.MAX_RETRY |
| 215 | |
| 216 | fw_rw_build = task_params.get(build_lib.BuildVersionKey.FW_RW_VERSION) |
| 217 | fw_ro_build = task_params.get(build_lib.BuildVersionKey.FW_RO_VERSION) |
| 218 | # Skip firmware field if None(unspecified) or 'None'(no such build). |
| 219 | if fw_ro_build not in (None, 'None'): |
| 220 | build = params.software_dependencies.add() |
| 221 | build.ro_firmware_build = fw_ro_build |
| 222 | if fw_rw_build not in (None, 'None'): |
| 223 | build = params.software_dependencies.add() |
| 224 | build.rw_firmware_build = fw_rw_build |
| 225 | |
| 226 | request.test_plan.suite.add().name = task_params['suite'] |
| 227 | return request |
| 228 | |
| 229 | |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 230 | def _scheduling_for_pool(pool): |
| 231 | """Assign appropriate pool name to scheduling instance. |
| 232 | |
| 233 | Args: |
| 234 | pool: string pool name (e.g. 'bvt', 'quota'). |
| 235 | |
| 236 | Returns: |
| 237 | scheduling: A request_pb2.Request.Params.Scheduling instance. |
| 238 | """ |
| 239 | mp = request_pb2.Request.Params.Scheduling.ManagedPool |
| 240 | if mp.DESCRIPTOR.values_by_name.get(pool) is not None: |
| 241 | return request_pb2.Request.Params.Scheduling(managed_pool=mp.Value(pool)) |
| 242 | |
| 243 | DUT_POOL_PREFIX = r'DUT_POOL_(?P<munged_pool>.+)' |
| 244 | match = re.match(DUT_POOL_PREFIX, pool) |
| 245 | if match: |
| 246 | pool = string.lower(match.group('munged_pool')) |
| 247 | if NONSTANDARD_POOL_NAMES.get(pool): |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 248 | return request_pb2.Request.Params.Scheduling( |
| 249 | managed_pool=NONSTANDARD_POOL_NAMES.get(pool)) |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 250 | return request_pb2.Request.Params.Scheduling(unmanaged_pool=pool) |
| 251 | |
| 252 | |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 253 | # Also see: A copy of this function in swarming_lib.py. |
| 254 | def _infer_build_from_task_params(task_params): |
| 255 | """Infer the build to install on the DUT for the scheduled task. |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 256 | |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 257 | Args: |
| 258 | task_params: The parameters of a task loaded from suite queue. |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 259 | |
Prathmesh Prabhu | 06852fe | 2019-09-09 07:58:43 -0700 | [diff] [blame] | 260 | Returns: |
| 261 | A string representing the build to run. |
| 262 | """ |
| 263 | cros_build = task_params[build_lib.BuildVersionKey.CROS_VERSION] |
| 264 | android_build = task_params[build_lib.BuildVersionKey.ANDROID_BUILD_VERSION] |
| 265 | testbed_build = task_params[build_lib.BuildVersionKey.TESTBED_BUILD_VERSION] |
| 266 | return cros_build or android_build or testbed_build |
| 267 | |
| 268 | |
| 269 | def _infer_pool_from_task_params(task_params): |
| 270 | """Infer the pool to use for the scheduled task. |
| 271 | |
| 272 | Args: |
| 273 | task_params: The parameters of a task loaded from suite queue. |
| 274 | |
| 275 | Returns: |
| 276 | A string pool to schedule task in. |
| 277 | """ |
| 278 | if task_params.get('override_qs_account'): |
| 279 | return 'DUT_POOL_QUOTA' |
| 280 | return task_params.get('override_pool') or task_params['pool'] |
| 281 | |
| 282 | |
| 283 | def _infer_timeout_from_task_params(task_params): |
| 284 | """Infer the timeout for the scheduled task. |
| 285 | |
| 286 | Args: |
| 287 | task_params: The parameters of a task loaded from suite queue. |
| 288 | |
| 289 | Returns: |
| 290 | A datetime.timedelta instance for the timeout. |
| 291 | """ |
| 292 | timeout_mins = int(task_params['timeout_mins']) |
| 293 | # timeout's unit is hour. |
| 294 | if task_params.get('timeout'): |
| 295 | timeout_mins = max(int(task_params['timeout'])*60, timeout_mins) |
| 296 | if timeout_mins > constants.Buildbucket.MAX_BUILDBUCKET_TIMEOUT_MINS: |
| 297 | timeout_mins = constants.Buildbucket.MAX_BUILDBUCKET_TIMEOUT_MINS |
| 298 | return datetime.timedelta(minutes=timeout_mins) |
| 299 | |
Prathmesh Prabhu | 1918a8f | 2019-09-07 21:37:37 -0700 | [diff] [blame] | 300 | |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 301 | def _request_tags(task_params, build, pool): |
| 302 | """Infer tags to include in cros_test_platform request. |
| 303 | |
| 304 | Args: |
| 305 | task_params: suite task parameters. |
| 306 | build: The build included in the request. Must not be None. |
| 307 | pool: The DUT pool used for the request. Must not be None. |
| 308 | |
| 309 | Returns: |
| 310 | A dict of tags. |
| 311 | """ |
| 312 | tags = { |
| 313 | 'build': build, |
| 314 | 'label-pool': pool, |
| 315 | } |
Xinan Lin | fb63d57 | 2019-09-24 15:49:04 -0700 | [diff] [blame] | 316 | if task_params.get('board') not in (None, 'None'): |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 317 | tags['label-board'] = task_params['board'] |
Xinan Lin | fb63d57 | 2019-09-24 15:49:04 -0700 | [diff] [blame] | 318 | if task_params.get('model') not in (None, 'None'): |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 319 | tags['label-model'] = task_params['model'] |
Xinan Lin | fb63d57 | 2019-09-24 15:49:04 -0700 | [diff] [blame] | 320 | if task_params.get('suite') not in (None, 'None'): |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 321 | tags['suite'] = task_params['suite'] |
| 322 | return tags |
| 323 | |
| 324 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 325 | def _suite_bb_tag(suite): |
| 326 | """Convert suite name to a buildbucket tag. |
Prathmesh Prabhu | 2382a18 | 2019-09-07 21:18:10 -0700 | [diff] [blame] | 327 | |
| 328 | Args: |
| 329 | request: A request_pb2.Request. |
| 330 | |
| 331 | Returns: |
| 332 | [bb_common_pb2.StringPair] tags to include the buildbucket request. |
| 333 | """ |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 334 | return [bb_common_pb2.StringPair(key='suite', value=suite)] |