blob: 3375287dd19caa93ed8b03924dfce896b10a882b [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.
4
5"""Module for interacting with Buildbucket."""
Xinan Linc61196b2019-08-13 10:37:30 -07006
Xinan Lin9e4917d2019-11-04 10:58:47 -08007import collections
Xinan Lin3ba18a02019-08-13 15:44:55 -07008import datetime
linxinane5eb4552019-08-26 05:44:45 +00009import logging
Xinan Lin3ba18a02019-08-13 15:44:55 -070010import re
11import string
12
13import build_lib
Xinan Linc61196b2019-08-13 10:37:30 -070014import constants
15import file_getter
16
17from chromite.api.gen.test_platform import request_pb2
Xinan Linc61196b2019-08-13 10:37:30 -070018from components import auth
Xinan Lin3ba18a02019-08-13 15:44:55 -070019from components.prpc import client as prpc_client
Prathmesh Prabhu2382a182019-09-07 21:18:10 -070020from infra_libs.buildbucket.proto import common_pb2 as bb_common_pb2
Xinan Lin3ba18a02019-08-13 15:44:55 -070021from infra_libs.buildbucket.proto import rpc_pb2, build_pb2
Xinan Linc61196b2019-08-13 10:37:30 -070022from infra_libs.buildbucket.proto.rpc_prpc_pb2 import BuildsServiceDescription
23
24from oauth2client import service_account
Xinan Lin3ba18a02019-08-13 15:44:55 -070025
26from google.protobuf import json_format, struct_pb2
Xinan Linc61196b2019-08-13 10:37:30 -070027
28
Xinan Lin3ba18a02019-08-13 15:44:55 -070029_enum = request_pb2.Request.Params.Scheduling
Xinan Linc61196b2019-08-13 10:37:30 -070030NONSTANDARD_POOL_NAMES = {
Xinan Lin3ba18a02019-08-13 15:44:55 -070031 '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 Linc61196b2019-08-13 10:37:30 -070039}
40
Xinan Lin3ba18a02019-08-13 15:44:55 -070041
Xinan Lin6e097382019-08-27 18:43:35 -070042GS_PREFIX = 'gs://chromeos-image-archive/'
43
44
Xinan Linc61196b2019-08-13 10:37:30 -070045def _get_client(address):
46 """Create a prpc client instance for given address."""
47 return prpc_client.Client(address, BuildsServiceDescription)
48
49
Xinan Lin3ba18a02019-08-13 15:44:55 -070050class BuildbucketRunError(Exception):
51 """Raised when interactions with buildbucket server fail."""
52
53
Xinan Linc61196b2019-08-13 10:37:30 -070054class 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 Lin9e4917d2019-11-04 10:58:47 -080065 def multirequest_run(self, tasks, suite):
66 """Call TestPlatform Builder to schedule a batch of tests.
Xinan Lin3ba18a02019-08-13 15:44:55 -070067
68 Args:
Xinan Lin9e4917d2019-11-04 10:58:47 -080069 tasks: The suite tasks to run.
70 suite: suite name for the batch request.
71
72 Returns:
73 List of executed tasks.
Xinan Lin3ba18a02019-08-13 15:44:55 -070074
75 Raises:
76 BuildbucketRunError: if failed to get build info from task parameters.
77 """
Xinan Lin9e4917d2019-11-04 10:58:47 -080078 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)
linxinane5eb4552019-08-26 05:44:45 +0000104
Xinan Lin9e4917d2019-11-04 10:58:47 -0800105 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 Lin3ba18a02019-08-13 15:44:55 -0700122
Xinan Linc61196b2019-08-13 10:37:30 -0700123 def dummy_run(self):
124 """Perform a dummy run of prpc call to cros_test_platform-dev."""
125
Xinan Lin9e4917d2019-11-04 10:58:47 -0800126 requests_json = '{ "requests": { "dummy": {} } }'
127 req_build = self._build_request(requests_json, tags=None)
Xinan Linc61196b2019-08-13 10:37:30 -0700128 # 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 Lin9e4917d2019-11-04 10:58:47 -0800132 return self.client.ScheduleBuild(req_build, credentials=cred)
Xinan Linc61196b2019-08-13 10:37:30 -0700133
Xinan Lin9e4917d2019-11-04 10:58:47 -0800134 def _build_request(self, reqs_json, tags):
Xinan Lin3ba18a02019-08-13 15:44:55 -0700135 """Generate ScheduleBuildRequest for calling buildbucket.
Xinan Linc61196b2019-08-13 10:37:30 -0700136
Xinan Lin3ba18a02019-08-13 15:44:55 -0700137 Args:
Xinan Lin9e4917d2019-11-04 10:58:47 -0800138 reqs_json: A json string of requests.
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700139 tags: A list of tags for the buildbucket build.
Xinan Linc61196b2019-08-13 10:37:30 -0700140
Xinan Lin3ba18a02019-08-13 15:44:55 -0700141 Returns:
142 A ScheduleBuildRequest instance.
143 """
Xinan Lin9e4917d2019-11-04 10:58:47 -0800144 requests_struct = struct_pb2.Struct()
145 recipe_struct = json_format.Parse(reqs_json, requests_struct)
Xinan Linc61196b2019-08-13 10:37:30 -0700146 return rpc_pb2.ScheduleBuildRequest(builder=self.builder,
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700147 properties=recipe_struct,
148 tags=tags)
Xinan Linc61196b2019-08-13 10:37:30 -0700149
150 def _gen_service_account_key(self, sa):
Xinan Lin3ba18a02019-08-13 15:44:55 -0700151 """Generate credentials to authorize the call.
Xinan Linc61196b2019-08-13 10:37:30 -0700152
Xinan Lin3ba18a02019-08-13 15:44:55 -0700153 Args:
154 sa: A string of the path to the service account json file.
Xinan Linc61196b2019-08-13 10:37:30 -0700155
Xinan Lin3ba18a02019-08-13 15:44:55 -0700156 Returns:
157 A service account key.
158 """
Xinan Linc61196b2019-08-13 10:37:30 -0700159 service_credentials = service_account.ServiceAccountCredentials
Xinan Lin3ba18a02019-08-13 15:44:55 -0700160 key = service_credentials.from_json_keyfile_name(sa, self.scope)
Xinan Linc61196b2019-08-13 10:37:30 -0700161 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 Prabhu1918a8f2019-09-07 21:37:37 -0700165
166
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700167def _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 Lin2e196412019-10-24 14:31:13 -0700187 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 Prabhu06852fe2019-09-09 07:58:43 -0700191 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 Prabhu1918a8f2019-09-07 21:37:37 -0700230def _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 Lin9e4917d2019-11-04 10:58:47 -0800248 return request_pb2.Request.Params.Scheduling(
249 managed_pool=NONSTANDARD_POOL_NAMES.get(pool))
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700250 return request_pb2.Request.Params.Scheduling(unmanaged_pool=pool)
251
252
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700253# Also see: A copy of this function in swarming_lib.py.
254def _infer_build_from_task_params(task_params):
255 """Infer the build to install on the DUT for the scheduled task.
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700256
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700257 Args:
258 task_params: The parameters of a task loaded from suite queue.
Prathmesh Prabhu1918a8f2019-09-07 21:37:37 -0700259
Prathmesh Prabhu06852fe2019-09-09 07:58:43 -0700260 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
269def _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
283def _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 Prabhu1918a8f2019-09-07 21:37:37 -0700300
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700301def _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 Linfb63d572019-09-24 15:49:04 -0700316 if task_params.get('board') not in (None, 'None'):
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700317 tags['label-board'] = task_params['board']
Xinan Linfb63d572019-09-24 15:49:04 -0700318 if task_params.get('model') not in (None, 'None'):
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700319 tags['label-model'] = task_params['model']
Xinan Linfb63d572019-09-24 15:49:04 -0700320 if task_params.get('suite') not in (None, 'None'):
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700321 tags['suite'] = task_params['suite']
322 return tags
323
324
Xinan Lin9e4917d2019-11-04 10:58:47 -0800325def _suite_bb_tag(suite):
326 """Convert suite name to a buildbucket tag.
Prathmesh Prabhu2382a182019-09-07 21:18:10 -0700327
328 Args:
329 request: A request_pb2.Request.
330
331 Returns:
332 [bb_common_pb2.StringPair] tags to include the buildbucket request.
333 """
Xinan Lin9e4917d2019-11-04 10:58:47 -0800334 return [bb_common_pb2.StringPair(key='suite', value=suite)]