blob: d13554b40f093bad47c68a3f1576e69f9448a8f7 [file] [log] [blame]
Xixuan Wuc7bf77c2018-04-24 12:05:40 -07001# Copyright 2018 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Definition of a CrOS suite in skylab.
6
7This file is a simplicication of dynamic_suite.suite without any useless
8features for skylab suite.
9
10Suite class in this file mainly has 2 features:
11 1. Integrate parameters from control file & passed in arguments.
12 2. Find proper child tests for a given suite.
13
14Use case:
15 See _run_suite() in skylab_suite.run_suite_skylab.
16"""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import print_function
21
Xixuan Wu7cc10e52018-04-25 17:04:51 -070022import collections
Xixuan Wu2406be32018-05-14 13:51:30 -070023import logging
Xixuan Wu8873a0e2018-07-20 17:51:33 -070024import os
Xixuan Wu7cc10e52018-04-25 17:04:51 -070025
Xixuan Wu48c45b92018-04-26 11:09:35 -070026from lucifer import autotest
Xixuan Wu89803382018-07-27 12:33:33 -070027from skylab_suite import errors
Xixuan Wu2406be32018-05-14 13:51:30 -070028from skylab_suite import swarming_lib
Xixuan Wu48c45b92018-04-26 11:09:35 -070029
Xixuan Wu7cc10e52018-04-25 17:04:51 -070030
Xixuan Wu9287fda2018-07-12 16:22:06 -070031SuiteSpec = collections.namedtuple(
32 'SuiteSpec',
Xixuan Wu7cc10e52018-04-25 17:04:51 -070033 [
34 'builds',
Xixuan Wu6c041332018-05-07 16:04:36 -070035 'suite_name',
Xixuan Wue3e362f2018-04-26 16:34:28 -070036 'suite_file_name',
Xixuan Wu7cc10e52018-04-25 17:04:51 -070037 'test_source_build',
Xixuan Wu2406be32018-05-14 13:51:30 -070038 'suite_args',
Xixuan Wu70217a92018-06-04 16:43:42 -070039 'priority',
Xixuan Wu77d4a592018-06-08 10:40:57 -070040 'board',
Xixuan Wu3dea7cf2018-12-10 17:50:45 -080041 'model',
Xixuan Wu77d4a592018-06-08 10:40:57 -070042 'pool',
Xixuan Wub2795662018-06-28 16:02:53 -070043 'job_keyvals',
Xixuan Wu0956b632018-08-06 14:40:23 -070044 'minimum_duts',
Xixuan Wuba0276c2018-08-10 09:36:23 -070045 'timeout_mins',
Aviv Keshetbe570b32018-12-06 14:46:19 -080046 'quota_account',
Xixuan Wu56424bc2018-05-15 11:03:27 -070047 ])
48
Xixuan Wu834cb4b2018-07-12 16:33:49 -070049SuiteHandlerSpec = collections.namedtuple(
50 'SuiteHandlerSpec',
Xixuan Wu56424bc2018-05-15 11:03:27 -070051 [
Xixuan Wu08354a02018-08-01 09:15:26 -070052 'suite_name',
Xixuan Wuf2da1952018-07-10 10:19:42 -070053 'wait',
Xixuan Wuc7430712018-07-10 12:04:34 -070054 'suite_id',
Xixuan Wu2406be32018-05-14 13:51:30 -070055 'timeout_mins',
Xixuan Wuc7dce5ef2018-08-19 21:09:53 -070056 'passed_mins',
Xixuan Wu56424bc2018-05-15 11:03:27 -070057 'test_retry',
58 'max_retries',
59 'provision_num_required',
Xixuan Wu7cc10e52018-04-25 17:04:51 -070060 ])
61
Xixuan Wu9d5d7032018-07-12 16:44:02 -070062TestHandlerSpec = collections.namedtuple(
63 'TestHandlerSpec',
Xixuan Wu5cb5a402018-06-04 16:37:23 -070064 [
Xixuan Wu5811e832018-07-12 11:56:24 -070065 'test_spec',
Xixuan Wu5cb5a402018-06-04 16:37:23 -070066 'remaining_retries',
67 'previous_retried_ids',
68 ])
69
Xixuan Wu9d5d7032018-07-12 16:44:02 -070070TestSpec = collections.namedtuple(
Xixuan Wu5811e832018-07-12 11:56:24 -070071 'TestSpec',
Xixuan Wu9af95a22018-05-18 10:46:42 -070072 [
73 'test',
Xixuan Wu70217a92018-06-04 16:43:42 -070074 'priority',
Xixuan Wu77d4a592018-06-08 10:40:57 -070075 'board',
Xixuan Wu3dea7cf2018-12-10 17:50:45 -080076 'model',
Xixuan Wu77d4a592018-06-08 10:40:57 -070077 'pool',
Xixuan Wu5cb5a402018-06-04 16:37:23 -070078 'build',
Xixuan Wub2795662018-06-28 16:02:53 -070079 'keyvals',
Xixuan Wu0c01b092018-06-13 14:12:55 -070080 'bot_id',
Xixuan Wuff19abe2018-06-20 10:44:45 -070081 'dut_name',
Xixuan Wu5cb5a402018-06-04 16:37:23 -070082 'expiration_secs',
83 'grace_period_secs',
84 'execution_timeout_secs',
85 'io_timeout_secs',
Aviv Keshetbe570b32018-12-06 14:46:19 -080086 'quota_account',
Xixuan Wu9af95a22018-05-18 10:46:42 -070087 ])
88
Xixuan Wu7cc10e52018-04-25 17:04:51 -070089
Xixuan Wu9af95a22018-05-18 10:46:42 -070090class SuiteHandler(object):
91 """The class for handling a CrOS suite run.
92
93 Its responsibility includes handling retries for child tests.
94 """
Xixuan Wu2406be32018-05-14 13:51:30 -070095
Xixuan Wu56424bc2018-05-15 11:03:27 -070096 def __init__(self, specs):
Xixuan Wu08354a02018-08-01 09:15:26 -070097 self._suite_name = specs.suite_name
Xixuan Wuf2da1952018-07-10 10:19:42 -070098 self._wait = specs.wait
Xixuan Wu9af95a22018-05-18 10:46:42 -070099 self._timeout_mins = specs.timeout_mins
100 self._provision_num_required = specs.provision_num_required
101 self._test_retry = specs.test_retry
102 self._max_retries = specs.max_retries
Xixuan Wuc7dce5ef2018-08-19 21:09:53 -0700103 self.passed_mins = specs.passed_mins
Xixuan Wu56424bc2018-05-15 11:03:27 -0700104
Xixuan Wu8873a0e2018-07-20 17:51:33 -0700105 # The swarming task id of the suite that this suite_handler is handling.
Xixuan Wuc7430712018-07-10 12:04:34 -0700106 self._suite_id = specs.suite_id
Xixuan Wu8873a0e2018-07-20 17:51:33 -0700107 # The swarming task id of current run_suite_skylab process. It could be
108 # different from self._suite_id if a suite_id is passed in.
109 self._task_id = os.environ.get('SWARMING_TASK_ID')
Xixuan Wu9af95a22018-05-18 10:46:42 -0700110 self._task_to_test_maps = {}
Xixuan Wu415e8212018-06-04 17:01:12 -0700111 self.successfully_provisioned_duts = set()
Xixuan Wu9af95a22018-05-18 10:46:42 -0700112
113 # It only maintains the swarming task of the final run of each
114 # child task, i.e. it doesn't include failed swarming tasks of
115 # each child task which will get retried later.
Xixuan Wu2406be32018-05-14 13:51:30 -0700116 self._active_child_tasks = []
117
Xixuan Wu9af95a22018-05-18 10:46:42 -0700118 def should_wait(self):
119 """Return whether to wait for a suite's result."""
120 return self._wait
121
Xixuan Wu415e8212018-06-04 17:01:12 -0700122 def is_provision(self):
123 """Return whether the suite handler is for provision suite."""
Xixuan Wu08354a02018-08-01 09:15:26 -0700124 return self._suite_name == 'provision'
Xixuan Wu415e8212018-06-04 17:01:12 -0700125
Xixuan Wu9af95a22018-05-18 10:46:42 -0700126 def set_suite_id(self, suite_id):
127 """Set swarming task id for a suite.
128
129 @param suite_id: The swarming task id of this suite.
130 """
131 self._suite_id = suite_id
132
Xixuan Wu9d5d7032018-07-12 16:44:02 -0700133 def add_test_by_task_id(self, task_id, test_handler_spec):
Xixuan Wu9af95a22018-05-18 10:46:42 -0700134 """Record a child test and its swarming task id.
135
136 @param task_id: the swarming task id of a child test.
Xixuan Wu9d5d7032018-07-12 16:44:02 -0700137 @param test_handler_spec: a TestHandlerSpec object.
Xixuan Wu9af95a22018-05-18 10:46:42 -0700138 """
Xixuan Wu9d5d7032018-07-12 16:44:02 -0700139 self._task_to_test_maps[task_id] = test_handler_spec
Xixuan Wu9af95a22018-05-18 10:46:42 -0700140
141 def get_test_by_task_id(self, task_id):
142 """Get a child test by its swarming task id.
143
144 @param task_id: the swarming task id of a child test.
145 """
146 return self._task_to_test_maps[task_id]
147
148 def remove_test_by_task_id(self, task_id):
149 """Delete a child test by its swarming task id.
150
151 @param task_id: the swarming task id of a child test.
152 """
153 self._task_to_test_maps.pop(task_id, None)
154
155 def set_max_retries(self, max_retries):
156 """Set the max retries for a suite.
157
158 @param max_retries: The current maximum retries to set.
159 """
160 self._max_retries = max_retries
161
162 @property
Xixuan Wu61d62832018-07-20 17:10:19 -0700163 def task_to_test_maps(self):
164 """Get the task_to_test_maps of a suite."""
165 return self._task_to_test_maps
166
167 @property
Xixuan Wu9af95a22018-05-18 10:46:42 -0700168 def timeout_mins(self):
169 """Get the timeout minutes of a suite."""
170 return self._timeout_mins
171
172 @property
173 def suite_id(self):
174 """Get the swarming task id of a suite."""
175 return self._suite_id
176
177 @property
Xixuan Wu8873a0e2018-07-20 17:51:33 -0700178 def task_id(self):
179 """Get swarming task id of current process."""
180 return self._task_id
181
182 @property
Xixuan Wu9af95a22018-05-18 10:46:42 -0700183 def max_retries(self):
184 """Get the max num of retries of a suite."""
185 return self._max_retries
186
Xixuan Wuc6e28d32018-08-27 14:48:14 -0700187 def get_active_child_tasks(self, suite_id):
Xixuan Wu9af95a22018-05-18 10:46:42 -0700188 """Get the child tasks which is actively monitored by a suite.
189
190 The active child tasks list includes tasks which are currently running
191 or finished without following retries. E.g.
192 Suite task X:
193 child task 1: x1 (first try x1_1, second try x1_2)
194 child task 2: x2 (first try: x2_1)
195 The final active child task list will include task x1_2 and x2_1, won't
196 include x1_1 since it's a task which is finished but get retried later.
197 """
Xixuan Wuc6e28d32018-08-27 14:48:14 -0700198 all_tasks = swarming_lib.get_child_tasks(suite_id)
199 return [t for t in all_tasks if t['task_id'] in self._task_to_test_maps]
Xixuan Wu2406be32018-05-14 13:51:30 -0700200
Xixuan Wuc6e28d32018-08-27 14:48:14 -0700201 def handle_results(self, suite_id):
Xixuan Wu2406be32018-05-14 13:51:30 -0700202 """Handle child tasks' results."""
Xixuan Wuc6e28d32018-08-27 14:48:14 -0700203 self._active_child_tasks = self.get_active_child_tasks(suite_id)
204 self.retried_tasks = [t for t in self._active_child_tasks
205 if self._should_retry(t)]
Xixuan Wu56424bc2018-05-15 11:03:27 -0700206 logging.info('Found %d tests to be retried.', len(self.retried_tasks))
Xixuan Wu2406be32018-05-14 13:51:30 -0700207
Xixuan Wu415e8212018-06-04 17:01:12 -0700208 def _check_all_tasks_finished(self):
209 """Check whether all tasks are finished, including retried tasks."""
Xixuan Wu56424bc2018-05-15 11:03:27 -0700210 finished_tasks = [t for t in self._active_child_tasks if
211 t['state'] in swarming_lib.TASK_FINISHED_STATUS]
212 logging.info('%d/%d child tasks finished, %d got retried.',
213 len(finished_tasks), len(self._active_child_tasks),
214 len(self.retried_tasks))
215 return (len(finished_tasks) == len(self._active_child_tasks)
216 and not self.retried_tasks)
217
Xixuan Wu415e8212018-06-04 17:01:12 -0700218 def _set_successful_provisioned_duts(self):
219 """Set successfully provisioned duts."""
220 for t in self._active_child_tasks:
221 if (swarming_lib.get_task_final_state(t) ==
222 swarming_lib.TASK_COMPLETED_SUCCESS):
Xixuan Wuff19abe2018-06-20 10:44:45 -0700223 dut_name = self.get_test_by_task_id(
Xixuan Wu5811e832018-07-12 11:56:24 -0700224 t['task_id']).test_spec.dut_name
Xixuan Wuff19abe2018-06-20 10:44:45 -0700225 if dut_name:
Xixuan Wu415e8212018-06-04 17:01:12 -0700226 self.successfully_provisioned_duts.add(dut_name)
227
228 def is_provision_successfully_finished(self):
229 """Check whether provision succeeds."""
230 logging.info('Found %d successfully provisioned duts, '
231 'the minimum requirement is %d',
232 len(self.successfully_provisioned_duts),
233 self._provision_num_required)
234 return (len(self.successfully_provisioned_duts) >=
235 self._provision_num_required)
236
237 def is_finished_waiting(self):
238 """Check whether the suite should finish its waiting."""
239 if self.is_provision():
240 self._set_successful_provisioned_duts()
241 return (self.is_provision_successfully_finished() or
242 self._check_all_tasks_finished())
243
244 return self._check_all_tasks_finished()
245
Xixuan Wu56424bc2018-05-15 11:03:27 -0700246 def _should_retry(self, test_result):
247 """Check whether a test should be retried.
248
249 We will retry a test if:
250 1. The test-level retry is enabled for this suite.
251 2. The test fails.
252 3. The test is currently monitored by the suite, i.e.
253 it's not a previous retried test.
254 4. The test has remaining retries based on JOB_RETRIES in
255 its control file.
256 5. The suite-level max retries isn't hit.
257
258 @param test_result: A json test result from swarming API.
259
260 @return True if we should retry the test.
261 """
262 task_id = test_result['task_id']
263 state = test_result['state']
264 is_failure = test_result['failure']
Xixuan Wu9af95a22018-05-18 10:46:42 -0700265 return (self._test_retry and
Xixuan Wu56424bc2018-05-15 11:03:27 -0700266 ((state == swarming_lib.TASK_COMPLETED and is_failure)
Xixuan Wuaff23c72018-06-14 12:10:44 -0700267 or (state in swarming_lib.TASK_STATUS_TO_RETRY))
Xixuan Wu9af95a22018-05-18 10:46:42 -0700268 and (task_id in self._task_to_test_maps)
269 and (self._task_to_test_maps[task_id].remaining_retries > 0)
270 and (self._max_retries > 0))
Xixuan Wu2406be32018-05-14 13:51:30 -0700271
272
Xixuan Wuc7bf77c2018-04-24 12:05:40 -0700273class Suite(object):
274 """The class for a CrOS suite."""
Xixuan Wu606e2182018-06-14 11:30:35 -0700275 EXPIRATION_SECS = swarming_lib.DEFAULT_EXPIRATION_SECS
Xixuan Wuc7bf77c2018-04-24 12:05:40 -0700276
Xixuan Wu9287fda2018-07-12 16:22:06 -0700277 def __init__(self, spec):
Xixuan Wu7cc10e52018-04-25 17:04:51 -0700278 """Initialize a suite.
279
Xixuan Wu9287fda2018-07-12 16:22:06 -0700280 @param spec: A SuiteSpec object.
Xixuan Wu7cc10e52018-04-25 17:04:51 -0700281 """
Xixuan Wue3e362f2018-04-26 16:34:28 -0700282 self._ds = None
283
284 self.control_file = ''
Xixuan Wu5811e832018-07-12 11:56:24 -0700285 self.test_specs = []
Xixuan Wu9287fda2018-07-12 16:22:06 -0700286 self.builds = spec.builds
287 self.test_source_build = spec.test_source_build
288 self.suite_name = spec.suite_name
289 self.suite_file_name = spec.suite_file_name
290 self.priority = spec.priority
291 self.board = spec.board
Xixuan Wu3dea7cf2018-12-10 17:50:45 -0800292 self.model = spec.model
Xixuan Wu9287fda2018-07-12 16:22:06 -0700293 self.pool = spec.pool
294 self.job_keyvals = spec.job_keyvals
Xixuan Wu0956b632018-08-06 14:40:23 -0700295 self.minimum_duts = spec.minimum_duts
Xixuan Wuba0276c2018-08-10 09:36:23 -0700296 self.timeout_mins = spec.timeout_mins
Aviv Keshetbe570b32018-12-06 14:46:19 -0800297 self.quota_account = spec.quota_account
Xixuan Wu48c45b92018-04-26 11:09:35 -0700298
Xixuan Wue3e362f2018-04-26 16:34:28 -0700299 @property
300 def ds(self):
301 """Getter for private |self._ds| property.
302
303 This ensures that once self.ds is called, there's a devserver ready
304 for it.
305 """
306 if self._ds is None:
Xixuan Wu89803382018-07-27 12:33:33 -0700307 raise errors.InValidPropertyError(
Xixuan Wue3e362f2018-04-26 16:34:28 -0700308 'Property self.ds is None. Please call stage_suite_artifacts() '
309 'before calling it.')
310
311 return self._ds
312
Xixuan Wub2795662018-06-28 16:02:53 -0700313 def _get_cros_build(self):
314 provision = autotest.load('server.cros.provision')
315 return self.builds.get(provision.CROS_VERSION_PREFIX,
316 self.builds.values()[0])
317
318 def _create_suite_keyvals(self):
319 constants = autotest.load('server.cros.dynamic_suite.constants')
320 provision = autotest.load('server.cros.provision')
321 cros_build = self._get_cros_build()
322 keyvals = {
323 constants.JOB_BUILD_KEY: cros_build,
324 constants.JOB_SUITE_KEY: self.suite_name,
325 constants.JOB_BUILDS_KEY: self.builds
326 }
327 if (cros_build != self.test_source_build or
328 len(self.builds) > 1):
329 keyvals[constants.JOB_TEST_SOURCE_BUILD_KEY] = (
330 self.test_source_build)
331 for prefix, build in self.builds.iteritems():
332 if prefix == provision.FW_RW_VERSION_PREFIX:
333 keyvals[constants.FWRW_BUILD]= build
334 elif prefix == provision.FW_RO_VERSION_PREFIX:
335 keyvals[constants.FWRO_BUILD] = build
336
337 for key in self.job_keyvals:
338 if key in constants.INHERITED_KEYVALS:
339 keyvals[key] = self.job_keyvals[key]
340
341 return keyvals
342
Xixuan Wue3e362f2018-04-26 16:34:28 -0700343 def prepare(self):
344 """Prepare a suite job for execution."""
345 self._stage_suite_artifacts()
346 self._parse_suite_args()
Xixuan Wub2795662018-06-28 16:02:53 -0700347 keyvals = self._create_suite_keyvals()
Xixuan Wu0c01b092018-06-13 14:12:55 -0700348 available_bots = self._get_available_bots()
Xixuan Wu0956b632018-08-06 14:40:23 -0700349 if len(available_bots) < self.minimum_duts:
350 raise errors.NoAvailableDUTsError(
Xixuan Wuefa94522018-08-07 17:51:34 -0700351 self.board, self.pool, len(available_bots),
352 self.minimum_duts)
353
Xixuan Wu0c01b092018-06-13 14:12:55 -0700354 tests = self._find_tests(available_bots_num=len(available_bots))
Xixuan Wu5811e832018-07-12 11:56:24 -0700355 self.test_specs = self._get_test_specs(tests, available_bots, keyvals)
Xixuan Wue3e362f2018-04-26 16:34:28 -0700356
Xixuan Wu19df5f32018-08-07 16:19:44 -0700357 def _create_test_spec(self, test, keyvals, bot_id='', dut_name=''):
358 return TestSpec(
359 test=test,
360 priority=self.priority,
361 board=self.board,
Xixuan Wu3dea7cf2018-12-10 17:50:45 -0800362 model=self.model,
Xixuan Wu19df5f32018-08-07 16:19:44 -0700363 pool=self.pool,
364 build=self.test_source_build,
365 bot_id=bot_id,
366 dut_name=dut_name,
367 keyvals=keyvals,
Xixuan Wuba0276c2018-08-10 09:36:23 -0700368 expiration_secs=self.timeout_mins * 60,
Xixuan Wu19df5f32018-08-07 16:19:44 -0700369 grace_period_secs=swarming_lib.DEFAULT_TIMEOUT_SECS,
370 execution_timeout_secs=swarming_lib.DEFAULT_TIMEOUT_SECS,
371 io_timeout_secs=swarming_lib.DEFAULT_TIMEOUT_SECS,
Aviv Keshetbe570b32018-12-06 14:46:19 -0800372 quota_account=self.quota_account,
Xixuan Wu19df5f32018-08-07 16:19:44 -0700373 )
Xixuan Wue3e362f2018-04-26 16:34:28 -0700374
Xixuan Wu19df5f32018-08-07 16:19:44 -0700375 def _get_test_specs(self, tests, available_bots, keyvals):
376 return [self._create_test_spec(test, keyvals) for test in tests]
Xixuan Wu0c01b092018-06-13 14:12:55 -0700377
Xixuan Wue3e362f2018-04-26 16:34:28 -0700378 def _stage_suite_artifacts(self):
Xixuan Wu48c45b92018-04-26 11:09:35 -0700379 """Stage suite control files and suite-to-tests mapping file.
380
381 @param build: The build to stage artifacts.
382 """
383 suite_common = autotest.load('server.cros.dynamic_suite.suite_common')
384 ds, _ = suite_common.stage_build_artifacts(self.test_source_build)
Xixuan Wue3e362f2018-04-26 16:34:28 -0700385 self._ds = ds
Xixuan Wu48c45b92018-04-26 11:09:35 -0700386
Xixuan Wue3e362f2018-04-26 16:34:28 -0700387 def _parse_suite_args(self):
Xixuan Wu48c45b92018-04-26 11:09:35 -0700388 """Get the suite args.
389
390 The suite args includes:
391 a. suite args in suite control file.
392 b. passed-in suite args by user.
393 """
Xixuan Wue3e362f2018-04-26 16:34:28 -0700394 suite_common = autotest.load('server.cros.dynamic_suite.suite_common')
395 self.control_file = suite_common.get_control_file_by_build(
396 self.test_source_build, self.ds, self.suite_file_name)
Xixuan Wu6c041332018-05-07 16:04:36 -0700397
Xixuan Wu0c01b092018-06-13 14:12:55 -0700398 def _find_tests(self, available_bots_num=0):
Xixuan Wu6c041332018-05-07 16:04:36 -0700399 """Fetch the child tests."""
400 control_file_getter = autotest.load(
401 'server.cros.dynamic_suite.control_file_getter')
402 suite_common = autotest.load('server.cros.dynamic_suite.suite_common')
Xixuan Wu6c041332018-05-07 16:04:36 -0700403 cf_getter = control_file_getter.DevServerGetter(
404 self.test_source_build, self.ds)
405 tests = suite_common.retrieve_for_suite(
406 cf_getter, self.suite_name)
Xixuan Wu81b71cb2019-01-10 16:00:30 -0800407 return suite_common.filter_tests(
408 tests, suite_common.name_in_tag_predicate(self.suite_name))
Xixuan Wu2406be32018-05-14 13:51:30 -0700409
Xixuan Wu0c01b092018-06-13 14:12:55 -0700410 def _get_available_bots(self):
Xixuan Wu0956b632018-08-06 14:40:23 -0700411 """Get available bots for suites."""
412 bots = swarming_lib.query_bots_list({
413 'pool': swarming_lib.SKYLAB_DRONE_POOL,
Xixuan Wu6a004562018-12-10 11:56:17 -0800414 'label-pool': swarming_lib.to_swarming_pool_label(self.pool),
Xixuan Wu0956b632018-08-06 14:40:23 -0700415 'label-board': self.board})
416 return [bot for bot in bots if swarming_lib.bot_available(bot)]
Xixuan Wu0c01b092018-06-13 14:12:55 -0700417
Xixuan Wu2406be32018-05-14 13:51:30 -0700418
419class ProvisionSuite(Suite):
420 """The class for a CrOS provision suite."""
Xixuan Wu6bd67ea2018-08-01 09:24:59 -0700421 EXPIRATION_SECS = swarming_lib.DEFAULT_EXPIRATION_SECS
Xixuan Wu2406be32018-05-14 13:51:30 -0700422
Xixuan Wu9287fda2018-07-12 16:22:06 -0700423 def __init__(self, spec):
424 super(ProvisionSuite, self).__init__(spec)
425 self._num_required = spec.suite_args['num_required']
Xixuan Wu2406be32018-05-14 13:51:30 -0700426
Xixuan Wu0c01b092018-06-13 14:12:55 -0700427 def _find_tests(self, available_bots_num=0):
Xixuan Wu2406be32018-05-14 13:51:30 -0700428 """Fetch the child tests for provision suite."""
429 control_file_getter = autotest.load(
430 'server.cros.dynamic_suite.control_file_getter')
431 suite_common = autotest.load('server.cros.dynamic_suite.suite_common')
Xixuan Wu2406be32018-05-14 13:51:30 -0700432 cf_getter = control_file_getter.DevServerGetter(
433 self.test_source_build, self.ds)
434 dummy_test = suite_common.retrieve_control_data_for_test(
435 cf_getter, 'dummy_Pass')
Xixuan Wu0c01b092018-06-13 14:12:55 -0700436 logging.info('Get %d available DUTs for provision.', available_bots_num)
Xixuan Wu89803382018-07-27 12:33:33 -0700437 if available_bots_num < self._num_required:
438 logging.warning('Not enough available DUTs for provision.')
439 raise errors.NoAvailableDUTsError(
Xixuan Wuefa94522018-08-07 17:51:34 -0700440 self.board, self.pool, available_bots_num,
441 self._num_required)
Xixuan Wu89803382018-07-27 12:33:33 -0700442
Xixuan Wu0c01b092018-06-13 14:12:55 -0700443 return [dummy_test] * max(self._num_required, available_bots_num)
Xixuan Wucb469512018-06-08 15:17:23 -0700444
Xixuan Wu19df5f32018-08-07 16:19:44 -0700445 def _get_test_specs(self, tests, available_bots, keyvals):
446 test_specs = []
447 for idx, test in enumerate(tests):
448 if idx < len(available_bots):
449 bot = available_bots[idx]
450 test_specs.append(self._create_test_spec(
451 test, keyvals, bot_id=bot['bot_id'],
452 dut_name=swarming_lib.get_task_dut_name(
453 bot['dimensions'])))
454 else:
455 test_specs.append(self._create_test_spec(test, keyvals))
456
457 return test_specs