blob: 140ccc6de80c15981a66e2555f0f53adea0a88de [file] [log] [blame]
Chris Masone44e4d6c2012-08-15 14:25:53 -07001# Copyright (c) 2012 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
Allen Liee36ab82017-07-07 15:46:40 -07005import abc
Fang Deng443f1952015-01-02 14:51:49 -08006import datetime
7import difflib
Allen Li98a26a42017-02-28 18:43:24 -08008import functools
Fang Deng443f1952015-01-02 14:51:49 -08009import hashlib
10import logging
11import operator
12import os
13import re
Fang Deng443f1952015-01-02 14:51:49 -080014import sys
Allen Li98a26a42017-02-28 18:43:24 -080015import warnings
Chris Masone44e4d6c2012-08-15 14:25:53 -070016
17import common
18
J. Richard Barnetteb592fbc2014-04-02 10:27:33 -070019from autotest_lib.frontend.afe.json_rpc import proxy
Fang Denge3bc24b2014-03-17 15:19:46 -070020from autotest_lib.client.common_lib import enum
Dan Shidfea3682014-08-10 23:38:40 -070021from autotest_lib.client.common_lib import error
Simran Basi5ace6f22016-01-06 17:30:44 -080022from autotest_lib.client.common_lib import global_config
Alex Miller7d658cf2013-09-04 16:00:35 -070023from autotest_lib.client.common_lib import priorities
Dan Shidfea3682014-08-10 23:38:40 -070024from autotest_lib.client.common_lib import time_utils
25from autotest_lib.client.common_lib import utils
Xixuan Wueb1acc42017-11-22 15:46:03 -080026from autotest_lib.frontend.afe import model_attributes
Fang Denge3bc24b2014-03-17 15:19:46 -070027from autotest_lib.frontend.afe.json_rpc import proxy
Dan Shi36cfd832014-10-10 13:38:51 -070028from autotest_lib.server.cros import provision
Chris Masone44e4d6c2012-08-15 14:25:53 -070029from autotest_lib.server.cros.dynamic_suite import constants
30from autotest_lib.server.cros.dynamic_suite import control_file_getter
31from autotest_lib.server.cros.dynamic_suite import frontend_wrappers
Alex Miller3a69adc2012-12-19 13:38:31 -080032from autotest_lib.server.cros.dynamic_suite import job_status
Xixuan Wu7cc10e52018-04-25 17:04:51 -070033from autotest_lib.server.cros.dynamic_suite import suite_common
J. Richard Barnettee7b98bb2013-08-21 16:34:16 -070034from autotest_lib.server.cros.dynamic_suite import tools
35from autotest_lib.server.cros.dynamic_suite.job_status import Status
Chris Masone44e4d6c2012-08-15 14:25:53 -070036
Shuqian Zhaoab468812015-04-08 14:40:38 -070037try:
38 from chromite.lib import boolparse_lib
39 from chromite.lib import cros_logging as logging
40except ImportError:
41 print 'Unable to import chromite.'
42 print 'This script must be either:'
43 print ' - Be run in the chroot.'
44 print ' - (not yet supported) be run after running '
45 print ' ../utils/build_externals.py'
Fang Denge3bc24b2014-03-17 15:19:46 -070046
Shuqian Zhao490f78f2016-01-20 13:18:40 -080047_FILE_BUG_SUITES = ['au', 'bvt', 'bvt-cq', 'bvt-inline', 'paygen_au_beta',
48 'paygen_au_canary', 'paygen_au_dev', 'paygen_au_stable',
49 'sanity', 'push_to_prod']
Simran Basi5ace6f22016-01-06 17:30:44 -080050_AUTOTEST_DIR = global_config.global_config.get_config_value(
51 'SCHEDULER', 'drone_installation_directory')
Xixuan Wu92249042018-04-30 17:17:10 -070052
Shuqian Zhaoe33ba4a2015-09-11 18:51:43 -070053
Fang Denge3bc24b2014-03-17 15:19:46 -070054class RetryHandler(object):
55 """Maintain retry information.
56
57 @var _retry_map: A dictionary that stores retry history.
58 The key is afe job id. The value is a dictionary.
59 {job_id: {'state':RetryHandler.States, 'retry_max':int}}
60 - state:
61 The retry state of a job.
62 NOT_ATTEMPTED:
63 We haven't done anything about the job.
64 ATTEMPTED:
65 We've made an attempt to schedule a retry job. The
66 scheduling may or may not be successful, e.g.
67 it might encounter an rpc error. Note failure
68 in scheduling a retry is different from a retry job failure.
69 For each job, we only attempt to schedule a retry once.
70 For example, assume we have a test with JOB_RETRIES=5 and
71 its second retry job failed. When we attempt to create
72 a third retry job to retry the second, we hit an rpc
73 error. In such case, we will give up on all following
74 retries.
75 RETRIED:
76 A retry job has already been successfully
77 scheduled.
78 - retry_max:
79 The maximum of times the job can still
80 be retried, taking into account retries
81 that have occurred.
82 @var _retry_level: A retry might be triggered only if the result
83 is worse than the level.
Fang Deng443f1952015-01-02 14:51:49 -080084 @var _max_retries: Maximum retry limit at suite level.
85 Regardless how many times each individual test
86 has been retried, the total number of retries happening in
87 the suite can't exceed _max_retries.
Fang Denge3bc24b2014-03-17 15:19:46 -070088 """
89
90 States = enum.Enum('NOT_ATTEMPTED', 'ATTEMPTED', 'RETRIED',
91 start_value=1, step=1)
92
Fang Deng443f1952015-01-02 14:51:49 -080093 def __init__(self, initial_jobs_to_tests, retry_level='WARN',
94 max_retries=None):
Fang Denge3bc24b2014-03-17 15:19:46 -070095 """Initialize RetryHandler.
96
97 @param initial_jobs_to_tests: A dictionary that maps a job id to
98 a ControlData object. This dictionary should contain
99 jobs that are originally scheduled by the suite.
100 @param retry_level: A retry might be triggered only if the result is
101 worse than the level.
Fang Deng443f1952015-01-02 14:51:49 -0800102 @param max_retries: Integer, maxmium total retries allowed
103 for the suite. Default to None, no max.
Fang Denge3bc24b2014-03-17 15:19:46 -0700104 """
105 self._retry_map = {}
106 self._retry_level = retry_level
Fang Deng443f1952015-01-02 14:51:49 -0800107 self._max_retries = (max_retries
108 if max_retries is not None else sys.maxint)
Fang Denge3bc24b2014-03-17 15:19:46 -0700109 for job_id, test in initial_jobs_to_tests.items():
110 if test.job_retries > 0:
Allen Lifb89e2b2017-01-03 12:47:58 -0800111 self._add_job(new_job_id=job_id,
112 retry_max=test.job_retries)
Jacob Kopczynski2cefa1f2018-01-10 17:25:38 -0800113 else:
114 logging.debug("Test %s has no retries", test.name)
Fang Denge3bc24b2014-03-17 15:19:46 -0700115
116
Allen Lifb89e2b2017-01-03 12:47:58 -0800117 def _add_job(self, new_job_id, retry_max):
Fang Denge3bc24b2014-03-17 15:19:46 -0700118 """Add a newly-created job to the retry map.
119
120 @param new_job_id: The afe_job_id of a newly created job.
121 @param retry_max: The maximum of times that we could retry
122 the test if the job fails.
123
124 @raises ValueError if new_job_id is already in retry map.
125
126 """
127 if new_job_id in self._retry_map:
128 raise ValueError('add_job called when job is already in retry map.')
129
130 self._retry_map[new_job_id] = {
131 'state': self.States.NOT_ATTEMPTED,
132 'retry_max': retry_max}
133
134
Allen Li0cd19262017-01-03 12:56:08 -0800135 def _suite_max_reached(self):
Fang Deng443f1952015-01-02 14:51:49 -0800136 """Return whether maximum retry limit for a suite has been reached."""
Fang Denge4326d62015-01-06 13:15:15 -0800137 return self._max_retries <= 0
Fang Deng443f1952015-01-02 14:51:49 -0800138
139
Fang Denge3bc24b2014-03-17 15:19:46 -0700140 def add_retry(self, old_job_id, new_job_id):
141 """Record a retry.
142
143 Update retry map with the retry information.
144
145 @param old_job_id: The afe_job_id of the job that is retried.
146 @param new_job_id: The afe_job_id of the retry job.
147
148 @raises KeyError if old_job_id isn't in the retry map.
149 @raises ValueError if we have already retried or made an attempt
150 to retry the old job.
151
152 """
153 old_record = self._retry_map[old_job_id]
154 if old_record['state'] != self.States.NOT_ATTEMPTED:
155 raise ValueError(
156 'We have already retried or attempted to retry job %d' %
157 old_job_id)
158 old_record['state'] = self.States.RETRIED
Allen Lifb89e2b2017-01-03 12:47:58 -0800159 self._add_job(new_job_id=new_job_id,
160 retry_max=old_record['retry_max'] - 1)
Fang Deng443f1952015-01-02 14:51:49 -0800161 self._max_retries -= 1
Fang Denge3bc24b2014-03-17 15:19:46 -0700162
163
164 def set_attempted(self, job_id):
165 """Set the state of the job to ATTEMPTED.
166
167 @param job_id: afe_job_id of a job.
168
169 @raises KeyError if job_id isn't in the retry map.
170 @raises ValueError if the current state is not NOT_ATTEMPTED.
171
172 """
173 current_state = self._retry_map[job_id]['state']
174 if current_state != self.States.NOT_ATTEMPTED:
175 # We are supposed to retry or attempt to retry each job
176 # only once. Raise an error if this is not the case.
177 raise ValueError('Unexpected state transition: %s -> %s' %
178 (self.States.get_string(current_state),
179 self.States.get_string(self.States.ATTEMPTED)))
180 else:
181 self._retry_map[job_id]['state'] = self.States.ATTEMPTED
182
183
184 def has_following_retry(self, result):
185 """Check whether there will be a following retry.
186
187 We have the following cases for a given job id (result.id),
188 - no retry map entry -> retry not required, no following retry
189 - has retry map entry:
190 - already retried -> has following retry
191 - has not retried
192 (this branch can be handled by checking should_retry(result))
193 - retry_max == 0 --> the last retry job, no more retry
194 - retry_max > 0
195 - attempted, but has failed in scheduling a
196 following retry due to rpc error --> no more retry
197 - has not attempped --> has following retry if test failed.
198
199 @param result: A result, encapsulating the status of the job.
200
201 @returns: True, if there will be a following retry.
202 False otherwise.
203
204 """
Allen Li2ee2a262017-01-03 13:21:10 -0800205 return (result.test_executed
206 and result.id in self._retry_map
207 and (self._retry_map[result.id]['state'] == self.States.RETRIED
208 or self._should_retry(result)))
Allen Li5cb00652017-01-03 13:06:30 -0800209
210
211 def _should_retry(self, result):
212 """Check whether we should retry a job based on its result.
213
214 We will retry the job that corresponds to the result
215 when all of the following are true.
216 a) The test was actually executed, meaning that if
217 a job was aborted before it could ever reach the state
218 of 'Running', the job will not be retried.
219 b) The result is worse than |self._retry_level| which
220 defaults to 'WARN'.
221 c) The test requires retry, i.e. the job has an entry in the retry map.
222 d) We haven't made any retry attempt yet, i.e. state == NOT_ATTEMPTED
223 Note that if a test has JOB_RETRIES=5, and the second time
224 it was retried it hit an rpc error, we will give up on
225 all following retries.
226 e) The job has not reached its retry max, i.e. retry_max > 0
227
228 @param result: A result, encapsulating the status of the job.
229
230 @returns: True if we should retry the job.
231
232 """
233 return (
xixuanbf854f82017-04-20 10:40:15 -0700234 result.test_executed
235 and result.id in self._retry_map
236 and not self._suite_max_reached()
Allen Li5cb00652017-01-03 13:06:30 -0800237 and result.is_worse_than(
238 job_status.Status(self._retry_level, '', 'reason'))
Allen Li5cb00652017-01-03 13:06:30 -0800239 and self._retry_map[result.id]['state'] == self.States.NOT_ATTEMPTED
240 and self._retry_map[result.id]['retry_max'] > 0
241 )
Fang Denge3bc24b2014-03-17 15:19:46 -0700242
Jacob Kopczynski2cefa1f2018-01-10 17:25:38 -0800243 def _should_retry_local_job(self, job_id):
244 """Check whether we should retry a job based on information available
245 for a local job without a Result object.
246
247 We will retry the job that corresponds to the result
248 when all of the following are true.
249 a) The test requires retry, i.e. the job has an entry in the retry map.
250 b) We haven't made any retry attempt yet for this job, i.e.
251 state == NOT_ATTEMPTED
252 If the job is aborted, we will give up on all following retries,
253 regardless of max_retries.
254 c) The job has not reached its retry max, i.e. retry_max > 0
255
256 @param job_id: the id for the job, to look up relevant information.
257
258 @returns: True if we should retry the job.
259
260 """
261 if self._suite_max_reached():
262 logging.debug('suite max_retries reached, not retrying.')
263 return False
264 if job_id not in self._retry_map:
265 logging.debug('job_id not in retry map, not retrying.')
266 return False
267 if self._retry_map[job_id]['state'] != self.States.NOT_ATTEMPTED:
268 logging.debug("job state was %s not 'Not Attempted', not retrying",
269 self._retry_map[job_id]['state'])
270 return False
271 if self._retry_map[job_id]['retry_max'] <= 0:
272 logging.debug('test-level retries exhausted, not retrying')
273 return False
274 return True
275
276
277 def job_present(self, job_id):
278 """Check whether a job id present in the retry map.
279
280 @param job_id: afe_job_id of a job.
281
282 @returns: A True if the job is present, False if not.
283 """
284 return bool(self._retry_map.get(job_id))
285
286
Fang Denge3bc24b2014-03-17 15:19:46 -0700287
288 def get_retry_max(self, job_id):
289 """Get the maximum times the job can still be retried.
290
291 @param job_id: afe_job_id of a job.
292
293 @returns: An int, representing the maximum times the job can still be
294 retried.
295 @raises KeyError if job_id isn't in the retry map.
296
297 """
298 return self._retry_map[job_id]['retry_max']
299
300
Allen Lida198fd2017-03-29 17:22:13 -0700301class _SuiteChildJobCreator(object):
302 """Create test jobs for a suite."""
303
Allen Li010c0412017-03-29 17:31:35 -0700304 def __init__(
305 self,
Allen Li55de3402017-03-29 17:48:46 -0700306 tag,
Allen Li27f72a22017-03-29 17:37:43 -0700307 builds,
Allen Li010c0412017-03-29 17:31:35 -0700308 board,
Allen Li388b7a12017-03-29 17:58:23 -0700309 afe=None,
Allen Li388b7a12017-03-29 17:58:23 -0700310 max_runtime_mins=24*60,
311 timeout_mins=24*60,
Allen Li55de3402017-03-29 17:48:46 -0700312 suite_job_id=None,
Allen Li010c0412017-03-29 17:31:35 -0700313 ignore_deps=False,
Allen Li37e1a292017-02-28 18:28:41 -0800314 extra_deps=(),
Allen Li388b7a12017-03-29 17:58:23 -0700315 priority=priorities.Priority.DEFAULT,
Allen Li55de3402017-03-29 17:48:46 -0700316 offload_failures_only=False,
Shuhei Takahashif8659c62017-06-14 20:02:26 +0900317 test_source_build=None,
Aviv Keshetd5a83f72017-10-30 12:53:01 -0700318 job_keyvals=None,
319 ):
Allen Li010c0412017-03-29 17:31:35 -0700320 """
321 Constructor
322
Allen Li55de3402017-03-29 17:48:46 -0700323 @param tag: a string with which to tag jobs run in this suite.
Allen Li27f72a22017-03-29 17:37:43 -0700324 @param builds: the builds on which we're running this suite.
Allen Li010c0412017-03-29 17:31:35 -0700325 @param board: the board on which we're running this suite.
Allen Li388b7a12017-03-29 17:58:23 -0700326 @param afe: an instance of AFE as defined in server/frontend.py.
Allen Li388b7a12017-03-29 17:58:23 -0700327 @param max_runtime_mins: Maximum suite runtime, in minutes.
328 @param timeout_mins: Maximum job lifetime, in minutes.
Allen Li55de3402017-03-29 17:48:46 -0700329 @param suite_job_id: Job id that will act as parent id to all sub jobs.
330 Default: None
Allen Li010c0412017-03-29 17:31:35 -0700331 @param ignore_deps: True if jobs should ignore the DEPENDENCIES
332 attribute and skip applying of dependency labels.
333 (Default:False)
334 @param extra_deps: A list of strings which are the extra DEPENDENCIES
335 to add to each test being scheduled.
Allen Li388b7a12017-03-29 17:58:23 -0700336 @param priority: Integer priority level. Higher is more important.
Allen Li55de3402017-03-29 17:48:46 -0700337 @param offload_failures_only: Only enable gs_offloading for failed
338 jobs.
339 @param test_source_build: Build that contains the server-side test code.
Shuhei Takahashif8659c62017-06-14 20:02:26 +0900340 @param job_keyvals: General job keyvals to be inserted into keyval file,
341 which will be used by tko/parse later.
Allen Li010c0412017-03-29 17:31:35 -0700342 """
Allen Li55de3402017-03-29 17:48:46 -0700343 self._tag = tag
Allen Li27f72a22017-03-29 17:37:43 -0700344 self._builds = builds
Allen Li010c0412017-03-29 17:31:35 -0700345 self._board = board
Allen Li388b7a12017-03-29 17:58:23 -0700346 self._afe = afe or frontend_wrappers.RetryingAFE(timeout_min=30,
347 delay_sec=10,
348 debug=False)
Allen Li388b7a12017-03-29 17:58:23 -0700349 self._max_runtime_mins = max_runtime_mins
350 self._timeout_mins = timeout_mins
Allen Li55de3402017-03-29 17:48:46 -0700351 self._suite_job_id = suite_job_id
Allen Li010c0412017-03-29 17:31:35 -0700352 self._ignore_deps = ignore_deps
Allen Li37e1a292017-02-28 18:28:41 -0800353 self._extra_deps = tuple(extra_deps)
Allen Li388b7a12017-03-29 17:58:23 -0700354 self._priority = priority
Allen Li55de3402017-03-29 17:48:46 -0700355 self._offload_failures_only = offload_failures_only
356 self._test_source_build = test_source_build
Shuhei Takahashif8659c62017-06-14 20:02:26 +0900357 self._job_keyvals = job_keyvals
Allen Li010c0412017-03-29 17:31:35 -0700358
359
Allen Li27f72a22017-03-29 17:37:43 -0700360 @property
361 def cros_build(self):
362 """Return the CrOS build or the first build in the builds dict."""
363 # TODO(ayatane): Note that the builds dict isn't ordered. I'm not
364 # sure what the implications of this are, but it's probably not a
365 # good thing.
366 return self._builds.get(provision.CROS_VERSION_PREFIX,
367 self._builds.values()[0])
368
369
Allen Li388b7a12017-03-29 17:58:23 -0700370 def create_job(self, test, retry_for=None):
371 """
372 Thin wrapper around frontend.AFE.create_job().
373
374 @param test: ControlData object for a test to run.
375 @param retry_for: If the to-be-created job is a retry for an
376 old job, the afe_job_id of the old job will
377 be passed in as |retry_for|, which will be
378 recorded in the new job's keyvals.
379 @returns: A frontend.Job object with an added test_name member.
380 test_name is used to preserve the higher level TEST_NAME
381 name of the job.
382 """
Keith Haddow782e2a82017-09-26 15:44:51 -0700383 # For a system running multiple suites which share tests, the priority
384 # overridden may lead to unexpected scheduling order that adds extra
385 # provision jobs.
386 test_priority = self._priority
387 if utils.is_moblab():
388 test_priority = max(self._priority, test.priority)
389
Xixuan Wueb1acc42017-11-22 15:46:03 -0800390 reboot_before = (model_attributes.RebootBefore.NEVER if test.fast
391 else None)
392
Allen Li388b7a12017-03-29 17:58:23 -0700393 test_obj = self._afe.create_job(
394 control_file=test.text,
395 name=tools.create_job_name(
396 self._test_source_build or self.cros_build,
397 self._tag,
398 test.name),
399 control_type=test.test_type.capitalize(),
400 meta_hosts=[self._board]*test.sync_count,
401 dependencies=self._create_job_deps(test),
402 keyvals=self._create_keyvals_for_test_job(test, retry_for),
403 max_runtime_mins=self._max_runtime_mins,
404 timeout_mins=self._timeout_mins,
405 parent_job_id=self._suite_job_id,
406 test_retry=test.retries,
Xixuan Wueb1acc42017-11-22 15:46:03 -0800407 reboot_before=reboot_before,
408 run_reset=not test.fast,
Keith Haddow782e2a82017-09-26 15:44:51 -0700409 priority=test_priority,
Allen Li388b7a12017-03-29 17:58:23 -0700410 synch_count=test.sync_count,
411 require_ssp=test.require_ssp)
412
413 test_obj.test_name = test.name
414 return test_obj
415
416
Allen Li010c0412017-03-29 17:31:35 -0700417 def _create_job_deps(self, test):
418 """Create job deps list for a test job.
419
420 @returns: A list of dependency strings.
421 """
422 if self._ignore_deps:
423 job_deps = []
424 else:
425 job_deps = list(test.dependencies)
426 job_deps.extend(self._extra_deps)
Allen Li010c0412017-03-29 17:31:35 -0700427 return job_deps
428
Allen Lida198fd2017-03-29 17:22:13 -0700429
Allen Li55de3402017-03-29 17:48:46 -0700430 def _create_keyvals_for_test_job(self, test, retry_for=None):
431 """Create keyvals dict for creating a test job.
432
433 @param test: ControlData object for a test to run.
434 @param retry_for: If the to-be-created job is a retry for an
435 old job, the afe_job_id of the old job will
436 be passed in as |retry_for|, which will be
437 recorded in the new job's keyvals.
438 @returns: A keyvals dict for creating the test job.
439 """
440 keyvals = {
441 constants.JOB_BUILD_KEY: self.cros_build,
442 constants.JOB_SUITE_KEY: self._tag,
443 constants.JOB_EXPERIMENTAL_KEY: test.experimental,
444 constants.JOB_BUILDS_KEY: self._builds
445 }
446 # test_source_build is saved to job_keyvals so scheduler can retrieve
447 # the build name from database when compiling autoserv commandline.
448 # This avoid a database change to add a new field in afe_jobs.
449 #
450 # Only add `test_source_build` to job keyvals if the build is different
451 # from the CrOS build or the job uses more than one build, e.g., both
452 # firmware and CrOS will be updated in the dut.
453 # This is for backwards compatibility, so the update Autotest code can
454 # compile an autoserv command line to run in a SSP container using
455 # previous builds.
456 if (self._test_source_build and
457 (self.cros_build != self._test_source_build or
458 len(self._builds) > 1)):
459 keyvals[constants.JOB_TEST_SOURCE_BUILD_KEY] = \
460 self._test_source_build
461 for prefix, build in self._builds.iteritems():
462 if prefix == provision.FW_RW_VERSION_PREFIX:
463 keyvals[constants.FWRW_BUILD]= build
464 elif prefix == provision.FW_RO_VERSION_PREFIX:
465 keyvals[constants.FWRO_BUILD] = build
466 # Add suite job id to keyvals so tko parser can read it from keyval
467 # file.
468 if self._suite_job_id:
469 keyvals[constants.PARENT_JOB_ID] = self._suite_job_id
470 # We drop the old job's id in the new job's keyval file so that
471 # later our tko parser can figure out the retry relationship and
472 # invalidate the results of the old job in tko database.
473 if retry_for:
474 keyvals[constants.RETRY_ORIGINAL_JOB_ID] = retry_for
475 if self._offload_failures_only:
476 keyvals[constants.JOB_OFFLOAD_FAILURES_KEY] = True
Shuhei Takahashif8659c62017-06-14 20:02:26 +0900477 if self._job_keyvals:
478 for key in constants.INHERITED_KEYVALS:
479 if key in self._job_keyvals:
480 keyvals[key] = self._job_keyvals[key]
Allen Li55de3402017-03-29 17:48:46 -0700481 return keyvals
482
483
Allen Li574fe4d2017-03-10 16:11:53 -0800484class _ControlFileRetriever(object):
485 """Retrieves control files.
486
487 This returns control data instances, unlike control file getters
488 which simply return the control file text contents.
Allen Li066f5872017-02-28 13:30:44 -0800489 """
Allen Li066f5872017-02-28 13:30:44 -0800490
Allen Liaed93492017-03-14 13:36:26 -0700491 def __init__(self, cf_getter, forgiving_parser=True, run_prod_code=False,
492 test_args=None):
Allen Li36746972017-03-10 16:17:46 -0800493 """Initialize instance.
494
495 @param cf_getter: a control_file_getter.ControlFileGetter used to list
496 and fetch the content of control files
Allen Li574fe4d2017-03-10 16:11:53 -0800497 @param forgiving_parser: If False, will raise ControlVariableExceptions
498 if any are encountered when parsing control
499 files. Note that this can raise an exception
500 for syntax errors in unrelated files, because
501 we parse them before applying the predicate.
Allen Liaed93492017-03-14 13:36:26 -0700502 @param run_prod_code: If true, the retrieved tests will run the test
503 code that lives in prod aka the test code
504 currently on the lab servers by disabling
505 SSP for the discovered tests.
Allen Li574fe4d2017-03-10 16:11:53 -0800506 @param test_args: A dict of args to be seeded in test control file under
507 the name |args_dict|.
Allen Liaed93492017-03-14 13:36:26 -0700508 """
509 self._cf_getter = cf_getter
510 self._forgiving_parser = forgiving_parser
511 self._run_prod_code = run_prod_code
512 self._test_args = test_args
513
514
Xixuan Wub2cf7fc2018-05-04 17:37:24 -0700515 def retrieve_for_test(self, test_name):
Allen Lib47f59a2017-03-10 17:50:45 -0800516 """Retrieve a test's control data.
517
518 This ignores forgiving_parser because we cannot return a
519 forgiving value.
520
521 @param test_name: Name of test to retrieve.
522
523 @raises ControlVariableException: There is a syntax error in a
524 control file.
525
526 @returns a ControlData object
527 """
Xixuan Wu9af22652018-05-14 10:50:54 -0700528 return suite_common.retrieve_control_data_for_test(
529 self._cf_getter, test_name)
Allen Lib47f59a2017-03-10 17:50:45 -0800530
531
Allen Liaed93492017-03-14 13:36:26 -0700532 def retrieve_for_suite(self, suite_name=''):
533 """Scan through all tests and find all tests.
534
535 @param suite_name: If specified, this method will attempt to restrain
536 the search space to just this suite's control files.
Allen Li066f5872017-02-28 13:30:44 -0800537
Allen Li574fe4d2017-03-10 16:11:53 -0800538 @raises ControlVariableException: If forgiving_parser is False and there
539 is a syntax error in a control file.
540
541 @returns a dictionary of ControlData objects that based on given
542 parameters.
543 """
Xixuan Wu92249042018-04-30 17:17:10 -0700544 tests = suite_common.retrieve_for_suite(
545 self._cf_getter, suite_name, self._forgiving_parser,
546 self._test_args)
Allen Lif8b0b702017-03-10 17:42:11 -0800547 if self._run_prod_code:
Xixuan Wu92249042018-04-30 17:17:10 -0700548 for test in tests.itervalues():
549 test.require_ssp = False
Allen Lif8b0b702017-03-10 17:42:11 -0800550
Xixuan Wu92249042018-04-30 17:17:10 -0700551 return tests
Allen Li1abded52017-03-10 16:37:57 -0800552
553
Allen Li122cd092017-03-09 15:56:46 -0800554def list_all_suites(build, devserver, cf_getter=None):
555 """
556 Parses all ControlData objects with a SUITE tag and extracts all
557 defined suite names.
558
559 @param build: the build on which we're running this suite.
560 @param devserver: the devserver which contains the build.
561 @param cf_getter: control_file_getter.ControlFileGetter. Defaults to
562 using DevServerGetter.
563
564 @return list of suites
565 """
566 if cf_getter is None:
567 cf_getter = _create_ds_getter(build, devserver)
568
569 suites = set()
570 predicate = lambda t: True
Allen Lif487fa12017-05-17 16:46:32 -0700571 for test in find_and_parse_tests(cf_getter, predicate):
Allen Li122cd092017-03-09 15:56:46 -0800572 suites.update(test.suite_tag_parts)
573 return list(suites)
574
575
Allen Lid1806ac2017-03-09 15:52:33 -0800576def test_file_similarity_predicate(test_file_pattern):
577 """Returns predicate that gets the similarity based on a test's file
578 name pattern.
579
580 Builds a predicate that takes in a parsed control file (a ControlData)
581 and returns a tuple of (file path, ratio), where ratio is the
582 similarity between the test file name and the given test_file_pattern.
583
584 @param test_file_pattern: regular expression (string) to match against
585 control file names.
586 @return a callable that takes a ControlData and and returns a tuple of
587 (file path, ratio), where ratio is the similarity between the
588 test file name and the given test_file_pattern.
589 """
590 return lambda t: ((None, 0) if not hasattr(t, 'path') else
591 (t.path, difflib.SequenceMatcher(a=t.path,
592 b=test_file_pattern).ratio()))
593
594
Allen Lib5b4a7a2017-03-09 15:50:09 -0800595def test_name_similarity_predicate(test_name):
596 """Returns predicate that matched based on a test's name.
597
598 Builds a predicate that takes in a parsed control file (a ControlData)
599 and returns a tuple of (test name, ratio), where ratio is the similarity
600 between the test name and the given test_name.
601
602 @param test_name: the test name to base the predicate on.
603 @return a callable that takes a ControlData and returns a tuple of
604 (test name, ratio), where ratio is the similarity between the
605 test name and the given test_name.
606 """
607 return lambda t: ((None, 0) if not hasattr(t, 'name') else
608 (t.name,
609 difflib.SequenceMatcher(a=t.name, b=test_name).ratio()))
610
611
Allen Lie37d6ba2017-03-09 15:49:25 -0800612def matches_attribute_expression_predicate(test_attr_boolstr):
613 """Returns predicate that matches based on boolean expression of
614 attributes.
615
616 Builds a predicate that takes in a parsed control file (a ControlData)
617 ans returns True if the test attributes satisfy the given attribute
618 boolean expression.
619
620 @param test_attr_boolstr: boolean expression of the attributes to be
621 test, like 'system:all and interval:daily'.
622
623 @return a callable that takes a ControlData and returns True if the test
624 attributes satisfy the given boolean expression.
625 """
626 return lambda t: boolparse_lib.BoolstrResult(
627 test_attr_boolstr, t.attributes)
628
629
Allen Lif29b48a2017-03-09 15:48:41 -0800630def test_file_matches_pattern_predicate(test_file_pattern):
631 """Returns predicate that matches based on a test's file name pattern.
632
633 Builds a predicate that takes in a parsed control file (a ControlData)
634 and returns True if the test's control file name matches the given
635 regular expression.
636
637 @param test_file_pattern: regular expression (string) to match against
638 control file names.
639 @return a callable that takes a ControlData and and returns
640 True if control file name matches the pattern.
641 """
642 return lambda t: hasattr(t, 'path') and re.match(test_file_pattern,
643 t.path)
644
645
Allen Li1819f522017-03-09 15:47:25 -0800646def test_name_matches_pattern_predicate(test_name_pattern):
647 """Returns predicate that matches based on a test's name pattern.
648
649 Builds a predicate that takes in a parsed control file (a ControlData)
650 and returns True if the test name matches the given regular expression.
651
652 @param test_name_pattern: regular expression (string) to match against
653 test names.
654 @return a callable that takes a ControlData and returns
655 True if the name fields matches the pattern.
656 """
657 return lambda t: hasattr(t, 'name') and re.match(test_name_pattern,
658 t.name)
659
660
Allen Lif8441c82017-03-09 15:46:32 -0800661def test_name_equals_predicate(test_name):
662 """Returns predicate that matched based on a test's name.
663
664 Builds a predicate that takes in a parsed control file (a ControlData)
665 and returns True if the test name is equal to |test_name|.
666
667 @param test_name: the test name to base the predicate on.
668 @return a callable that takes a ControlData and looks for |test_name|
669 in that ControlData's name.
670 """
671 return lambda t: hasattr(t, 'name') and test_name == t.name
672
673
Allen Li6e2fa4f2017-03-09 15:45:43 -0800674def name_in_tag_similarity_predicate(name):
675 """Returns predicate that takes a control file and gets the similarity
676 of the suites in the control file and the given name.
677
678 Builds a predicate that takes in a parsed control file (a ControlData)
679 and returns a list of tuples of (suite name, ratio), where suite name
680 is each suite listed in the control file, and ratio is the similarity
681 between each suite and the given name.
682
683 @param name: the suite name to base the predicate on.
684 @return a callable that takes a ControlData and returns a list of tuples
685 of (suite name, ratio), where suite name is each suite listed in
686 the control file, and ratio is the similarity between each suite
687 and the given name.
688 """
689 return lambda t: [(suite,
690 difflib.SequenceMatcher(a=suite, b=name).ratio())
691 for suite in t.suite_tag_parts] or [(None, 0)]
692
693
Allen Li398ddbd2017-03-09 15:44:25 -0800694def name_in_tag_predicate(name):
695 """Returns predicate that takes a control file and looks for |name|.
696
697 Builds a predicate that takes in a parsed control file (a ControlData)
698 and returns True if the SUITE tag is present and contains |name|.
699
700 @param name: the suite name to base the predicate on.
701 @return a callable that takes a ControlData and looks for |name| in that
702 ControlData object's suite member.
703 """
704 return lambda t: name in t.suite_tag_parts
705
706
Allen Lia640d6d2017-03-09 15:41:35 -0800707def create_fs_getter(autotest_dir):
708 """
709 @param autotest_dir: the place to find autotests.
710 @return a FileSystemGetter instance that looks under |autotest_dir|.
711 """
712 # currently hard-coded places to look for tests.
713 subpaths = ['server/site_tests', 'client/site_tests',
714 'server/tests', 'client/tests']
715 directories = [os.path.join(autotest_dir, p) for p in subpaths]
716 return control_file_getter.FileSystemGetter(directories)
717
718
Allen Li0f915872017-02-28 18:51:04 -0800719def _create_ds_getter(build, devserver):
720 """
721 @param build: the build on which we're running this suite.
722 @param devserver: the devserver which contains the build.
723 @return a FileSystemGetter instance that looks under |autotest_dir|.
724 """
725 return control_file_getter.DevServerGetter(build, devserver)
726
727
Allen Li3adae952017-03-10 17:18:12 -0800728def _non_experimental_tests_predicate(test_data):
729 """Test predicate for non-experimental tests."""
730 return not test_data.experimental
731
732
Allen Li0b1fa382017-02-28 18:47:16 -0800733def find_and_parse_tests(cf_getter, predicate, suite_name='',
734 add_experimental=False, forgiving_parser=True,
735 run_prod_code=False, test_args=None):
736 """
737 Function to scan through all tests and find eligible tests.
738
739 Search through all tests based on given cf_getter, suite_name,
740 add_experimental and forgiving_parser, return the tests that match
741 given predicate.
742
743 @param cf_getter: a control_file_getter.ControlFileGetter used to list
744 and fetch the content of control files
745 @param predicate: a function that should return True when run over a
746 ControlData representation of a control file that should be in
747 this Suite.
748 @param suite_name: If specified, this method will attempt to restrain
749 the search space to just this suite's control files.
750 @param add_experimental: add tests with experimental attribute set.
751 @param forgiving_parser: If False, will raise ControlVariableExceptions
752 if any are encountered when parsing control
753 files. Note that this can raise an exception
754 for syntax errors in unrelated files, because
755 we parse them before applying the predicate.
756 @param run_prod_code: If true, the suite will run the test code that
757 lives in prod aka the test code currently on the
758 lab servers by disabling SSP for the discovered
759 tests.
760 @param test_args: A dict of args to be seeded in test control file.
761
762 @raises ControlVariableException: If forgiving_parser is False and there
763 is a syntax error in a control file.
764
765 @return list of ControlData objects that should be run, with control
766 file text added in |text| attribute. Results are sorted based
767 on the TIME setting in control file, slowest test comes first.
768 """
Allen Libb60f442017-03-14 12:18:57 -0700769 logging.debug('Getting control file list for suite: %s', suite_name)
Xixuan Wu92249042018-04-30 17:17:10 -0700770 retriever = _ControlFileRetriever(cf_getter,
771 forgiving_parser=forgiving_parser,
772 run_prod_code=run_prod_code,
773 test_args=test_args)
Allen Liaed93492017-03-14 13:36:26 -0700774 tests = retriever.retrieve_for_suite(suite_name)
Allen Li3adae952017-03-10 17:18:12 -0800775 if not add_experimental:
776 predicate = _ComposedPredicate([predicate,
777 _non_experimental_tests_predicate])
Xixuan Wud9648532018-05-04 18:06:53 -0700778 return suite_common.filter_tests(tests, predicate)
Allen Li0b1fa382017-02-28 18:47:16 -0800779
780
Allen Lida012192017-02-28 18:37:52 -0800781def find_possible_tests(cf_getter, predicate, suite_name='', count=10):
782 """
783 Function to scan through all tests and find possible tests.
784
785 Search through all tests based on given cf_getter, suite_name,
786 add_experimental and forgiving_parser. Use the given predicate to
787 calculate the similarity and return the top 10 matches.
788
789 @param cf_getter: a control_file_getter.ControlFileGetter used to list
790 and fetch the content of control files
791 @param predicate: a function that should return a tuple of (name, ratio)
792 when run over a ControlData representation of a control file that
793 should be in this Suite. `name` is the key to be compared, e.g.,
794 a suite name or test name. `ratio` is a value between [0,1]
795 indicating the similarity of `name` and the value to be compared.
796 @param suite_name: If specified, this method will attempt to restrain
797 the search space to just this suite's control files.
798 @param count: Number of suggestions to return, default to 10.
799
800 @return list of top names that similar to the given test, sorted by
801 match ratio.
802 """
Allen Libb60f442017-03-14 12:18:57 -0700803 logging.debug('Getting control file list for suite: %s', suite_name)
Xixuan Wu92249042018-04-30 17:17:10 -0700804 tests = _ControlFileRetriever(cf_getter).retrieve_for_suite(suite_name)
Allen Lida012192017-02-28 18:37:52 -0800805 logging.debug('Parsed %s control files.', len(tests))
806 similarities = {}
807 for test in tests.itervalues():
808 ratios = predicate(test)
809 # Some predicates may return a list of tuples, e.g.,
810 # name_in_tag_similarity_predicate. Convert all returns to a list.
811 if not isinstance(ratios, list):
812 ratios = [ratios]
813 for name, ratio in ratios:
814 similarities[name] = ratio
815 return [s[0] for s in
816 sorted(similarities.items(), key=operator.itemgetter(1),
817 reverse=True)][:count]
818
819
Allen Li98a26a42017-02-28 18:43:24 -0800820def _deprecated_suite_method(func):
821 """Decorator for deprecated Suite static methods.
822
823 TODO(ayatane): This is used to decorate functions that are called as
824 static methods on Suite.
825 """
826 @functools.wraps(func)
827 def wrapper(*args, **kwargs):
Shuhei Takahashif8659c62017-06-14 20:02:26 +0900828 """Wraps |func| for warning."""
Brian Norris32add5d2017-06-19 11:09:54 -0700829 warnings.warn('Calling method "%s" from Suite is deprecated' %
830 func.__name__)
Allen Li98a26a42017-02-28 18:43:24 -0800831 return func(*args, **kwargs)
832 return staticmethod(wrapper)
833
834
Allen Li4b5a24f2017-03-09 16:01:35 -0800835class _BaseSuite(object):
Chris Masone44e4d6c2012-08-15 14:25:53 -0700836 """
837 A suite of tests, defined by some predicate over control file variables.
838
839 Given a place to search for control files a predicate to match the desired
840 tests, can gather tests and fire off jobs to run them, and then wait for
841 results.
842
843 @var _predicate: a function that should return True when run over a
844 ControlData representation of a control file that should be in
845 this Suite.
846 @var _tag: a string with which to tag jobs run in this suite.
Dan Shi36cfd832014-10-10 13:38:51 -0700847 @var _builds: the builds on which we're running this suite.
Chris Masone44e4d6c2012-08-15 14:25:53 -0700848 @var _afe: an instance of AFE as defined in server/frontend.py.
849 @var _tko: an instance of TKO as defined in server/frontend.py.
850 @var _jobs: currently scheduled jobs, if any.
Fang Denge3bc24b2014-03-17 15:19:46 -0700851 @var _jobs_to_tests: a dictionary that maps job ids to tests represented
852 ControlData objects.
Fang Denge3bc24b2014-03-17 15:19:46 -0700853 @var _retry: a bool value indicating whether jobs should be retried on
854 failure.
855 @var _retry_handler: a RetryHandler object.
856
Chris Masone44e4d6c2012-08-15 14:25:53 -0700857 """
858
Dan Shi36cfd832014-10-10 13:38:51 -0700859
Allen Li6fff5502016-12-09 18:04:26 -0800860 def __init__(
861 self,
Allen Li00bbe5b2017-03-09 16:44:30 -0800862 tests,
Allen Li6fff5502016-12-09 18:04:26 -0800863 tag,
864 builds,
865 board,
Allen Li6fff5502016-12-09 18:04:26 -0800866 afe=None,
867 tko=None,
868 pool=None,
869 results_dir=None,
870 max_runtime_mins=24*60,
871 timeout_mins=24*60,
872 file_bugs=False,
Allen Li6fff5502016-12-09 18:04:26 -0800873 suite_job_id=None,
874 ignore_deps=False,
Allen Li493eefa2016-12-09 18:05:35 -0800875 extra_deps=None,
Allen Li6fff5502016-12-09 18:04:26 -0800876 priority=priorities.Priority.DEFAULT,
Allen Li6fff5502016-12-09 18:04:26 -0800877 wait_for_results=True,
878 job_retry=False,
879 max_retries=sys.maxint,
880 offload_failures_only=False,
Shuqian Zhaoda1118d2017-02-13 16:22:58 -0800881 test_source_build=None,
Prathmesh Prabhu013afa52017-09-07 17:54:23 +0000882 job_keyvals=None,
Aviv Keshetd5a83f72017-10-30 12:53:01 -0700883 child_dependencies=(),
Prathmesh Prabhu013afa52017-09-07 17:54:23 +0000884 result_reporter=None,
Allen Li6fff5502016-12-09 18:04:26 -0800885 ):
Allen Li7f43ef92017-03-09 16:29:48 -0800886 """Initialize instance.
Chris Masone44e4d6c2012-08-15 14:25:53 -0700887
Allen Li00bbe5b2017-03-09 16:44:30 -0800888 @param tests: Iterable of tests to run.
Chris Masone44e4d6c2012-08-15 14:25:53 -0700889 @param tag: a string with which to tag jobs run in this suite.
Dan Shi36cfd832014-10-10 13:38:51 -0700890 @param builds: the builds on which we're running this suite.
Alex Millera0913072013-06-12 10:01:51 -0700891 @param board: the board on which we're running this suite.
Chris Masone44e4d6c2012-08-15 14:25:53 -0700892 @param afe: an instance of AFE as defined in server/frontend.py.
893 @param tko: an instance of TKO as defined in server/frontend.py.
894 @param pool: Specify the pool of machines to use for scheduling
895 purposes.
896 @param results_dir: The directory where the job can write results to.
897 This must be set if you want job_id of sub-jobs
898 list in the job keyvals.
Aviv Keshet18308922013-02-19 17:49:49 -0800899 @param max_runtime_mins: Maximum suite runtime, in minutes.
Alex Miller028b0312013-09-07 15:25:45 -0700900 @param timeout: Maximum job lifetime, in hours.
Aviv Keshet18308922013-02-19 17:49:49 -0800901 @param suite_job_id: Job id that will act as parent id to all sub jobs.
902 Default: None
Aviv Keshetd7959f32013-05-17 15:58:43 -0700903 @param ignore_deps: True if jobs should ignore the DEPENDENCIES
904 attribute and skip applying of dependency labels.
905 (Default:False)
Alex Miller47a03672013-08-27 09:09:53 -0700906 @param extra_deps: A list of strings which are the extra DEPENDENCIES
907 to add to each test being scheduled.
Alex Miller7d658cf2013-09-04 16:00:35 -0700908 @param priority: Integer priority level. Higher is more important.
Dan Shi95122412013-11-12 16:20:33 -0800909 @param wait_for_results: Set to False to run the suite job without
910 waiting for test jobs to finish. Default is
911 True.
Jacob Kopczynski2cefa1f2018-01-10 17:25:38 -0800912 @param job_retry: A bool value indicating whether jobs should be retried
Fang Denge3bc24b2014-03-17 15:19:46 -0700913 on failure. If True, the field 'JOB_RETRIES' in
914 control files will be respected. If False, do not
915 retry.
Fang Deng443f1952015-01-02 14:51:49 -0800916 @param max_retries: Maximum retry limit at suite level.
917 Regardless how many times each individual test
918 has been retried, the total number of retries
919 happening in the suite can't exceed _max_retries.
920 Default to sys.maxint.
Simran Basi1e10e922015-04-16 15:09:56 -0700921 @param offload_failures_only: Only enable gs_offloading for failed
922 jobs.
Dan Shi36cfd832014-10-10 13:38:51 -0700923 @param test_source_build: Build that contains the server-side test code.
Shuqian Zhaoda1118d2017-02-13 16:22:58 -0800924 @param job_keyvals: General job keyvals to be inserted into keyval file,
925 which will be used by tko/parse later.
Aviv Keshetd5a83f72017-10-30 12:53:01 -0700926 @param child_dependencies: (optional) list of dependency strings
927 to be added as dependencies to child jobs.
Prathmesh Prabhu013afa52017-09-07 17:54:23 +0000928 @param result_reporter: A _ResultReporter instance to report results. If
929 None, an _EmailReporter will be created.
Chris Masone44e4d6c2012-08-15 14:25:53 -0700930 """
Allen Li493eefa2016-12-09 18:05:35 -0800931
Allen Li00bbe5b2017-03-09 16:44:30 -0800932 self.tests = list(tests)
Chris Masone44e4d6c2012-08-15 14:25:53 -0700933 self._tag = tag
Dan Shi36cfd832014-10-10 13:38:51 -0700934 self._builds = builds
Chris Masone44e4d6c2012-08-15 14:25:53 -0700935 self._results_dir = results_dir
936 self._afe = afe or frontend_wrappers.RetryingAFE(timeout_min=30,
937 delay_sec=10,
938 debug=False)
939 self._tko = tko or frontend_wrappers.RetryingTKO(timeout_min=30,
940 delay_sec=10,
941 debug=False)
Chris Masone44e4d6c2012-08-15 14:25:53 -0700942 self._jobs = []
Fang Denge3bc24b2014-03-17 15:19:46 -0700943 self._jobs_to_tests = {}
beeps89f1e062013-09-18 12:00:17 -0700944
Alex Millera3a4fe72013-01-22 09:57:47 -0800945 self._file_bugs = file_bugs
Aviv Keshet18308922013-02-19 17:49:49 -0800946 self._suite_job_id = suite_job_id
Fang Denge3bc24b2014-03-17 15:19:46 -0700947 self._job_retry=job_retry
Fang Deng443f1952015-01-02 14:51:49 -0800948 self._max_retries = max_retries
Fang Denge3bc24b2014-03-17 15:19:46 -0700949 # RetryHandler to be initialized in schedule()
950 self._retry_handler = None
Dan Shi95122412013-11-12 16:20:33 -0800951 self.wait_for_results = wait_for_results
Shuqian Zhaoda1118d2017-02-13 16:22:58 -0800952 self._job_keyvals = job_keyvals
Prathmesh Prabhu013afa52017-09-07 17:54:23 +0000953 if result_reporter is None:
954 self._result_reporter = _EmailReporter(self)
955 else:
956 self._result_reporter = result_reporter
Alex Millera3a4fe72013-01-22 09:57:47 -0800957
Allen Li80dc02c2017-02-28 18:22:16 -0800958 if extra_deps is None:
959 extra_deps = []
Allen Li3a83fe62017-02-28 18:27:09 -0800960 extra_deps.append(board)
Allen Licceb1832017-02-28 18:25:06 -0800961 if pool:
962 extra_deps.append(pool)
Aviv Keshetd5a83f72017-10-30 12:53:01 -0700963 extra_deps.extend(child_dependencies)
Allen Lide5ecce2017-11-06 17:48:09 -0800964 self._dependencies = tuple(extra_deps)
Aviv Keshetd5a83f72017-10-30 12:53:01 -0700965
Allen Li010c0412017-03-29 17:31:35 -0700966 self._job_creator = _SuiteChildJobCreator(
Allen Li55de3402017-03-29 17:48:46 -0700967 tag=tag,
Allen Li27f72a22017-03-29 17:37:43 -0700968 builds=builds,
Allen Li010c0412017-03-29 17:31:35 -0700969 board=board,
Allen Li388b7a12017-03-29 17:58:23 -0700970 afe=afe,
Allen Li388b7a12017-03-29 17:58:23 -0700971 max_runtime_mins=max_runtime_mins,
972 timeout_mins=timeout_mins,
Allen Li55de3402017-03-29 17:48:46 -0700973 suite_job_id=suite_job_id,
Allen Li010c0412017-03-29 17:31:35 -0700974 ignore_deps=ignore_deps,
975 extra_deps=extra_deps,
Allen Li388b7a12017-03-29 17:58:23 -0700976 priority=priority,
Allen Li55de3402017-03-29 17:48:46 -0700977 offload_failures_only=offload_failures_only,
978 test_source_build=test_source_build,
Shuhei Takahashif8659c62017-06-14 20:02:26 +0900979 job_keyvals=job_keyvals,
Allen Li010c0412017-03-29 17:31:35 -0700980 )
Allen Lida198fd2017-03-29 17:22:13 -0700981
Chris Masone44e4d6c2012-08-15 14:25:53 -0700982
Allen Liad281cf2017-07-07 16:50:38 -0700983 def _schedule_test(self, record, test, retry_for=None):
Fang Denge3bc24b2014-03-17 15:19:46 -0700984 """Schedule a single test and return the job.
985
Allen Lie79b3cb2016-12-12 18:24:17 -0800986 Schedule a single test by creating a job, and then update relevant
987 data structures that are used to keep track of all running jobs.
Fang Denge3bc24b2014-03-17 15:19:46 -0700988
Allen Lie79b3cb2016-12-12 18:24:17 -0800989 Emits a TEST_NA status log entry if it failed to schedule the test due
990 to NoEligibleHostException or a non-existent board label.
991
992 Returns a frontend.Job object if the test is successfully scheduled.
993 If scheduling failed due to NoEligibleHostException or a non-existent
Allen Liad281cf2017-07-07 16:50:38 -0700994 board label, returns None.
Fang Denge3bc24b2014-03-17 15:19:46 -0700995
996 @param record: A callable to use for logging.
997 prototype: record(base_job.status_log_entry)
998 @param test: ControlData for a test to run.
999 @param retry_for: If we are scheduling a test to retry an
1000 old job, the afe_job_id of the old job
1001 will be passed in as |retry_for|.
Fang Denge3bc24b2014-03-17 15:19:46 -07001002
Allen Lie79b3cb2016-12-12 18:24:17 -08001003 @returns: A frontend.Job object or None
Fang Denge3bc24b2014-03-17 15:19:46 -07001004 """
1005 msg = 'Scheduling %s' % test.name
1006 if retry_for:
1007 msg = msg + ', to retry afe job %d' % retry_for
1008 logging.debug(msg)
Dan Shidfea3682014-08-10 23:38:40 -07001009 begin_time_str = datetime.datetime.now().strftime(time_utils.TIME_FMT)
Fang Denge3bc24b2014-03-17 15:19:46 -07001010 try:
Allen Li388b7a12017-03-29 17:58:23 -07001011 job = self._job_creator.create_job(test, retry_for=retry_for)
Allen Li6fd440f2016-12-12 18:40:05 -08001012 except (error.NoEligibleHostException, proxy.ValidationError) as e:
1013 if (isinstance(e, error.NoEligibleHostException)
1014 or (isinstance(e, proxy.ValidationError)
1015 and _is_nonexistent_board_error(e))):
1016 # Treat a dependency on a non-existent board label the same as
1017 # a dependency on a board that exists, but for which there's no
1018 # hardware.
1019 logging.debug('%s not applicable for this board/pool. '
1020 'Emitting TEST_NA.', test.name)
1021 Status('TEST_NA', test.name,
1022 'Skipping: test not supported on this board/pool.',
Allen Li9fcd4b42016-12-12 16:15:14 -08001023 begin_time_str=begin_time_str).record_all(record)
1024 return None
1025 else:
Fang Denge3bc24b2014-03-17 15:19:46 -07001026 raise e
Allen Liad281cf2017-07-07 16:50:38 -07001027 except (error.RPCException, proxy.JSONRPCException):
Fang Denge3bc24b2014-03-17 15:19:46 -07001028 if retry_for:
1029 # Mark that we've attempted to retry the old job.
Jacob Kopczynski2cefa1f2018-01-10 17:25:38 -08001030 logging.debug("RPC exception occurred")
Fang Denge3bc24b2014-03-17 15:19:46 -07001031 self._retry_handler.set_attempted(job_id=retry_for)
Allen Liad281cf2017-07-07 16:50:38 -07001032 raise
Fang Denge3bc24b2014-03-17 15:19:46 -07001033 else:
1034 self._jobs.append(job)
1035 self._jobs_to_tests[job.id] = test
1036 if retry_for:
1037 # A retry job was just created, record it.
1038 self._retry_handler.add_retry(
1039 old_job_id=retry_for, new_job_id=job.id)
1040 retry_count = (test.job_retries -
1041 self._retry_handler.get_retry_max(job.id))
1042 logging.debug('Job %d created to retry job %d. '
1043 'Have retried for %d time(s)',
1044 job.id, retry_for, retry_count)
Allen Li4df053e2016-12-29 16:05:41 -08001045 self._remember_job_keyval(job)
Fang Denge3bc24b2014-03-17 15:19:46 -07001046 return job
Fang Denge3bc24b2014-03-17 15:19:46 -07001047
Allen Li5b5642f2017-05-17 17:02:56 -07001048 def schedule(self, record):
Chris Masone44e4d6c2012-08-15 14:25:53 -07001049 """
1050 Schedule jobs using |self._afe|.
1051
1052 frontend.Job objects representing each scheduled job will be put in
1053 |self._jobs|.
1054
Fang Denge3bc24b2014-03-17 15:19:46 -07001055 @param record: A callable to use for logging.
1056 prototype: record(base_job.status_log_entry)
Aviv Keshete9170d92013-07-19 11:20:45 -07001057 @returns: The number of tests that were scheduled.
Chris Masone44e4d6c2012-08-15 14:25:53 -07001058 """
Allen Lif4cb5ec2017-01-03 16:58:12 -08001059 scheduled_test_names = []
Allen Li5b5642f2017-05-17 17:02:56 -07001060 logging.debug('Discovered %d tests.', len(self.tests))
Chris Masone44e4d6c2012-08-15 14:25:53 -07001061
Alex Miller3a69adc2012-12-19 13:38:31 -08001062 Status('INFO', 'Start %s' % self._tag).record_result(record)
1063 try:
Shuqian Zhaoda1118d2017-02-13 16:22:58 -08001064 # Write job_keyvals into keyval file.
1065 if self._job_keyvals:
1066 utils.write_keyval(self._results_dir, self._job_keyvals)
1067
Prathmesh Prabhu7295bf32017-06-08 10:44:52 -07001068 # TODO(crbug.com/730885): This is a hack to protect tests that are
1069 # not usually retried from getting hit by a provision error when run
1070 # as part of a suite. Remove this hack once provision is separated
1071 # out in its own suite.
Allen Li5b5642f2017-05-17 17:02:56 -07001072 self._bump_up_test_retries(self.tests)
1073 for test in self.tests:
Allen Lida905732016-12-12 15:49:16 -08001074 scheduled_job = self._schedule_test(record, test)
1075 if scheduled_job is not None:
Shuqian Zhaocd866f32016-11-29 20:14:34 -08001076 scheduled_test_names.append(test.name)
1077
1078 # Write the num of scheduled tests and name of them to keyval file.
Shuqian Zhaocd866f32016-11-29 20:14:34 -08001079 logging.debug('Scheduled %d tests, writing the total to keyval.',
Allen Lia4d35022016-12-12 15:42:10 -08001080 len(scheduled_test_names))
Allen Lid4d5dda2016-12-12 15:39:11 -08001081 utils.write_keyval(
1082 self._results_dir,
Allen Lidda59b82016-12-12 18:20:04 -08001083 self._make_scheduled_tests_keyvals(scheduled_test_names))
Aviv Keshetff7bd292017-07-27 11:14:41 -07001084 except Exception:
Allen Lib892d9f2016-12-29 15:50:11 -08001085 logging.exception('Exception while scheduling suite')
Alex Miller3a69adc2012-12-19 13:38:31 -08001086 Status('FAIL', self._tag,
1087 'Exception while scheduling suite').record_result(record)
1088
Fang Deng7e655a92014-05-23 13:48:11 -07001089 if self._job_retry:
Jacob Kopczynski2cefa1f2018-01-10 17:25:38 -08001090 logging.debug("Initializing RetryHandler for suite %s.", self._tag)
Fang Deng7e655a92014-05-23 13:48:11 -07001091 self._retry_handler = RetryHandler(
Fang Deng443f1952015-01-02 14:51:49 -08001092 initial_jobs_to_tests=self._jobs_to_tests,
1093 max_retries=self._max_retries)
Jacob Kopczynski2cefa1f2018-01-10 17:25:38 -08001094 logging.debug("retry map created: %s ",
1095 self._retry_handler._retry_map)
1096 else:
Jacob Kopczynski61a2d372018-06-13 11:51:46 -07001097 logging.info("Will not retry jobs from suite %s.", self._tag)
Allen Lia4d35022016-12-12 15:42:10 -08001098 return len(scheduled_test_names)
Aviv Keshete9170d92013-07-19 11:20:45 -07001099
Alex Miller3a69adc2012-12-19 13:38:31 -08001100
Prathmesh Prabhu7295bf32017-06-08 10:44:52 -07001101 def _bump_up_test_retries(self, tests):
1102 """Bump up individual test retries to match suite retry options."""
1103 if not self._job_retry:
1104 return
1105
1106 for test in tests:
Ilja H. Friedeldd98c2b2017-08-31 23:55:40 -07001107 # We do honor if a test insists on JOB_RETRIES = 0.
1108 if test.job_retries is None:
Prathmesh Prabhu7295bf32017-06-08 10:44:52 -07001109 logging.debug(
Ilja H. Friedeldd98c2b2017-08-31 23:55:40 -07001110 'Test %s did not request retries, but suite requires '
Prathmesh Prabhu7295bf32017-06-08 10:44:52 -07001111 'retries. Bumping retries up to 1. '
1112 '(See crbug.com/730885)',
1113 test.name)
1114 test.job_retries = 1
1115
1116
Allen Lidda59b82016-12-12 18:20:04 -08001117 def _make_scheduled_tests_keyvals(self, scheduled_test_names):
1118 """Make a keyvals dict to write for scheduled test names.
1119
1120 @param scheduled_test_names: A list of scheduled test name strings.
1121
1122 @returns: A keyvals dict.
1123 """
1124 return {
1125 constants.SCHEDULED_TEST_COUNT_KEY: len(scheduled_test_names),
1126 constants.SCHEDULED_TEST_NAMES_KEY: repr(scheduled_test_names),
1127 }
1128
1129
Allen Lid1cbccf2016-12-29 15:12:39 -08001130 def _should_report(self, result):
beepsda5b7112013-05-30 11:34:14 -07001131 """
Shuqian Zhaoe33ba4a2015-09-11 18:51:43 -07001132 Returns True if this failure requires to be reported.
beepsda5b7112013-05-30 11:34:14 -07001133
1134 @param result: A result, encapsulating the status of the failed job.
Shuqian Zhaoe33ba4a2015-09-11 18:51:43 -07001135 @return: True if we should report this failure.
beepsda5b7112013-05-30 11:34:14 -07001136 """
Alex Millerfcc119b2014-01-15 13:54:58 -08001137 return (self._file_bugs and result.test_executed and
Fang Dengd82c1c72014-07-29 10:43:01 -07001138 not result.is_testna() and
beeps32fa6772014-01-28 13:19:53 -08001139 result.is_worse_than(job_status.Status('GOOD', '', 'reason')))
beepsda5b7112013-05-30 11:34:14 -07001140
1141
Allen Licc752292017-01-03 12:44:39 -08001142 def _has_retry(self, result):
1143 """
1144 Return True if this result gets to retry.
1145
1146 @param result: A result, encapsulating the status of the failed job.
1147 @return: bool
1148 """
1149 return (self._job_retry
1150 and self._retry_handler.has_following_retry(result))
1151
1152
Aviv Keshet3e5ff4a2017-08-04 14:11:37 -07001153 def wait(self, record):
Alex Miller3a69adc2012-12-19 13:38:31 -08001154 """
1155 Polls for the job statuses, using |record| to print status when each
1156 completes.
1157
1158 @param record: callable that records job status.
1159 prototype:
1160 record(base_job.status_log_entry)
1161 """
Allen Li4a993a62017-07-10 11:44:54 -07001162 waiter = job_status.JobResultWaiter(self._afe, self._tko)
Alex Miller3a69adc2012-12-19 13:38:31 -08001163 try:
Aviv Keshet133beb12013-08-20 14:37:13 -07001164 if self._suite_job_id:
Allen Li4a993a62017-07-10 11:44:54 -07001165 jobs = self._afe.get_jobs(parent_job_id=self._suite_job_id)
Aviv Keshet133beb12013-08-20 14:37:13 -07001166 else:
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -07001167 logging.warning('Unknown suite_job_id, falling back to less '
Dan Shi08ff1282016-02-18 19:51:16 -08001168 'efficient results_generator.')
Allen Li4a993a62017-07-10 11:44:54 -07001169 jobs = self._jobs
1170 waiter.add_jobs(jobs)
1171 for result in waiter.wait_for_results():
Prathmesh Prabhu013afa52017-09-07 17:54:23 +00001172 self._handle_result(result=result, record=record, waiter=waiter)
Allen Li337e1242017-07-10 13:00:31 -07001173 if self._finished_waiting():
1174 break
1175 except Exception: # pylint: disable=W0703
Allen Lib892d9f2016-12-29 15:50:11 -08001176 logging.exception('Exception waiting for results')
Alex Miller3a69adc2012-12-19 13:38:31 -08001177 Status('FAIL', self._tag,
1178 'Exception waiting for results').record_result(record)
1179
1180
Allen Li337e1242017-07-10 13:00:31 -07001181 def _finished_waiting(self):
1182 """Return whether the suite is finished waiting for child jobs."""
1183 return False
1184
1185
Prathmesh Prabhu013afa52017-09-07 17:54:23 +00001186 def _handle_result(self, result, record, waiter):
Allen Li26b340d2016-12-29 15:23:01 -08001187 """
Allen Lie406a5e2017-07-10 12:53:06 -07001188 Handle a test job result.
Allen Li26b340d2016-12-29 15:23:01 -08001189
1190 @param result: Status instance for job.
1191 @param record: callable that records job status.
1192 prototype:
1193 record(base_job.status_log_entry)
Allen Li4a993a62017-07-10 11:44:54 -07001194 @param waiter: JobResultsWaiter instance.
Jacob Kopczynski2cefa1f2018-01-10 17:25:38 -08001195
1196 @instance_param _result_reporter: _ResultReporter instance.
Allen Li26b340d2016-12-29 15:23:01 -08001197 """
Allen Lie406a5e2017-07-10 12:53:06 -07001198 self._record_result(result, record)
Prathmesh Prabhu69fdb0d2017-09-07 12:30:03 -07001199 rescheduled = False
xixuanbf854f82017-04-20 10:40:15 -07001200 if self._job_retry and self._retry_handler._should_retry(result):
Prathmesh Prabhu69fdb0d2017-09-07 12:30:03 -07001201 rescheduled = self._retry_result(result, record, waiter)
1202 # TODO (crbug.com/751428): If the suite times out before a retry could
1203 # finish, we would lose the chance to report errors from the original
1204 # job.
1205 if self._has_retry(result) and rescheduled:
1206 return
1207
Prathmesh Prabhu013afa52017-09-07 17:54:23 +00001208 if self._should_report(result):
1209 self._result_reporter.report(result)
Allen Li26b340d2016-12-29 15:23:01 -08001210
Allen Lie406a5e2017-07-10 12:53:06 -07001211 def _record_result(self, result, record):
1212 """
1213 Record a test job result.
1214
1215 @param result: Status instance for job.
1216 @param record: callable that records job status.
1217 prototype:
1218 record(base_job.status_log_entry)
1219 """
1220 result.record_all(record)
1221 self._remember_job_keyval(result)
1222
1223
1224 def _retry_result(self, result, record, waiter):
1225 """
1226 Retry a test job result.
1227
1228 @param result: Status instance for job.
1229 @param record: callable that records job status.
1230 prototype:
1231 record(base_job.status_log_entry)
1232 @param waiter: JobResultsWaiter instance.
Prathmesh Prabhu69fdb0d2017-09-07 12:30:03 -07001233 @returns: True if a job was scheduled for retry, False otherwise.
Allen Lie406a5e2017-07-10 12:53:06 -07001234 """
1235 test = self._jobs_to_tests[result.id]
1236 try:
Xixuan Wu163ba1f2017-12-05 11:03:47 -08001237 # It only takes effect for CQ retriable job:
1238 # 1) in first try, test.fast=True.
1239 # 2) in second try, test will be run in normal mode, so reset
1240 # test.fast=False.
1241 test.fast = False
Allen Lie406a5e2017-07-10 12:53:06 -07001242 new_job = self._schedule_test(
1243 record=record, test=test, retry_for=result.id)
1244 except (error.RPCException, proxy.JSONRPCException) as e:
1245 logging.error('Failed to schedule test: %s, Reason: %s',
1246 test.name, e)
Prathmesh Prabhu69fdb0d2017-09-07 12:30:03 -07001247 return False
Allen Lie406a5e2017-07-10 12:53:06 -07001248 else:
1249 waiter.add_job(new_job)
Prathmesh Prabhu69fdb0d2017-09-07 12:30:03 -07001250 return bool(new_job)
Allen Lie406a5e2017-07-10 12:53:06 -07001251
Jacob Kopczynski2cefa1f2018-01-10 17:25:38 -08001252 @property
1253 def jobs(self):
1254 """Give a copy of the associated jobs
1255
1256 @returns: array of jobs"""
1257 return [job for job in self._jobs]
1258
Allen Lie406a5e2017-07-10 12:53:06 -07001259
Allen Li11308982016-12-29 16:19:55 -08001260 @property
1261 def _should_file_bugs(self):
1262 """Return whether bugs should be filed.
1263
1264 @returns: bool
1265 """
1266 # File bug when failure is one of the _FILE_BUG_SUITES,
1267 # otherwise send an email to the owner anc cc.
1268 return self._tag in _FILE_BUG_SUITES
1269
1270
Alex Miller3a69adc2012-12-19 13:38:31 -08001271 def abort(self):
1272 """
1273 Abort all scheduled test jobs.
1274 """
1275 if self._jobs:
1276 job_ids = [job.id for job in self._jobs]
1277 self._afe.run('abort_host_queue_entries', job__id__in=job_ids)
Chris Masone44e4d6c2012-08-15 14:25:53 -07001278
1279
Allen Li4df053e2016-12-29 16:05:41 -08001280 def _remember_job_keyval(self, job):
Chris Masoned9f13c52012-08-29 10:37:08 -07001281 """
1282 Record provided job as a suite job keyval, for later referencing.
1283
Allen Li4df053e2016-12-29 16:05:41 -08001284 @param job: some representation of a job that has the attributes:
1285 id, test_name, and owner
Chris Masoned9f13c52012-08-29 10:37:08 -07001286 """
Allen Li3cc73cd2016-12-12 16:02:21 -08001287 if self._results_dir and job.id and job.owner and job.test_name:
Chris Masone44e4d6c2012-08-15 14:25:53 -07001288 job_id_owner = '%s-%s' % (job.id, job.owner)
Chris Masoned9f13c52012-08-29 10:37:08 -07001289 logging.debug('Adding job keyval for %s=%s',
Chris Sosaaccb5ce2012-08-30 17:29:15 -07001290 job.test_name, job_id_owner)
Chris Masone44e4d6c2012-08-15 14:25:53 -07001291 utils.write_keyval(
1292 self._results_dir,
1293 {hashlib.md5(job.test_name).hexdigest(): job_id_owner})
1294
Dan Shid1521802013-05-24 13:08:37 -07001295
Allen Li4b5a24f2017-03-09 16:01:35 -08001296class Suite(_BaseSuite):
1297 """
1298 A suite of tests, defined by some predicate over control file variables.
1299
1300 Given a place to search for control files a predicate to match the desired
1301 tests, can gather tests and fire off jobs to run them, and then wait for
1302 results.
1303
1304 @var _predicate: a function that should return True when run over a
1305 ControlData representation of a control file that should be in
1306 this Suite.
1307 @var _tag: a string with which to tag jobs run in this suite.
1308 @var _builds: the builds on which we're running this suite.
1309 @var _afe: an instance of AFE as defined in server/frontend.py.
1310 @var _tko: an instance of TKO as defined in server/frontend.py.
1311 @var _jobs: currently scheduled jobs, if any.
1312 @var _jobs_to_tests: a dictionary that maps job ids to tests represented
1313 ControlData objects.
1314 @var _cf_getter: a control_file_getter.ControlFileGetter
1315 @var _retry: a bool value indicating whether jobs should be retried on
1316 failure.
1317 @var _retry_handler: a RetryHandler object.
1318
1319 """
1320
1321 # TODO(ayatane): These methods are kept on the Suite class for
1322 # backward compatibility.
1323 find_and_parse_tests = _deprecated_suite_method(find_and_parse_tests)
1324 find_possible_tests = _deprecated_suite_method(find_possible_tests)
1325 create_fs_getter = _deprecated_suite_method(create_fs_getter)
1326 name_in_tag_predicate = _deprecated_suite_method(name_in_tag_predicate)
1327 name_in_tag_similarity_predicate = _deprecated_suite_method(
1328 name_in_tag_similarity_predicate)
1329 test_name_equals_predicate = _deprecated_suite_method(
1330 test_name_equals_predicate)
1331 test_name_matches_pattern_predicate = _deprecated_suite_method(
1332 test_name_matches_pattern_predicate)
1333 test_file_matches_pattern_predicate = _deprecated_suite_method(
1334 test_file_matches_pattern_predicate)
1335 matches_attribute_expression_predicate = _deprecated_suite_method(
1336 matches_attribute_expression_predicate)
1337 test_name_similarity_predicate = _deprecated_suite_method(
1338 test_name_similarity_predicate)
1339 test_file_similarity_predicate = _deprecated_suite_method(
1340 test_file_similarity_predicate)
1341 list_all_suites = _deprecated_suite_method(list_all_suites)
Xixuan Wu7cc10e52018-04-25 17:04:51 -07001342 get_test_source_build = _deprecated_suite_method(
1343 suite_common.get_test_source_build)
Allen Li4b5a24f2017-03-09 16:01:35 -08001344
1345
Allen Li25bb1c62017-03-09 16:27:00 -08001346 @classmethod
1347 def create_from_predicates(cls, predicates, builds, board, devserver,
1348 cf_getter=None, name='ad_hoc_suite',
1349 run_prod_code=False, **dargs):
1350 """
1351 Create a Suite using a given predicate test filters.
1352
1353 Uses supplied predicate(s) to instantiate a Suite. Looks for tests in
1354 |autotest_dir| and will schedule them using |afe|. Pulls control files
1355 from the default dev server. Results will be pulled from |tko| upon
1356 completion.
1357
1358 @param predicates: A list of callables that accept ControlData
1359 representations of control files. A test will be
1360 included in suite if all callables in this list
1361 return True on the given control file.
1362 @param builds: the builds on which we're running this suite. It's a
1363 dictionary of version_prefix:build.
1364 @param board: the board on which we're running this suite.
1365 @param devserver: the devserver which contains the build.
1366 @param cf_getter: control_file_getter.ControlFileGetter. Defaults to
1367 using DevServerGetter.
1368 @param name: name of suite. Defaults to 'ad_hoc_suite'
1369 @param run_prod_code: If true, the suite will run the tests that
1370 lives in prod aka the test code currently on the
1371 lab servers.
1372 @param **dargs: Any other Suite constructor parameters, as described
1373 in Suite.__init__ docstring.
1374 @return a Suite instance.
1375 """
1376 if cf_getter is None:
1377 if run_prod_code:
1378 cf_getter = create_fs_getter(_AUTOTEST_DIR)
1379 else:
Xixuan Wu7cc10e52018-04-25 17:04:51 -07001380 build = suite_common.get_test_source_build(builds, **dargs)
Allen Li25bb1c62017-03-09 16:27:00 -08001381 cf_getter = _create_ds_getter(build, devserver)
1382
1383 return cls(predicates,
1384 name, builds, board, cf_getter, run_prod_code, **dargs)
1385
1386
1387 @classmethod
1388 def create_from_name(cls, name, builds, board, devserver, cf_getter=None,
1389 **dargs):
1390 """
1391 Create a Suite using a predicate based on the SUITE control file var.
1392
1393 Makes a predicate based on |name| and uses it to instantiate a Suite
1394 that looks for tests in |autotest_dir| and will schedule them using
1395 |afe|. Pulls control files from the default dev server.
1396 Results will be pulled from |tko| upon completion.
1397
1398 @param name: a value of the SUITE control file variable to search for.
1399 @param builds: the builds on which we're running this suite. It's a
1400 dictionary of version_prefix:build.
1401 @param board: the board on which we're running this suite.
1402 @param devserver: the devserver which contains the build.
1403 @param cf_getter: control_file_getter.ControlFileGetter. Defaults to
1404 using DevServerGetter.
1405 @param **dargs: Any other Suite constructor parameters, as described
1406 in Suite.__init__ docstring.
1407 @return a Suite instance.
1408 """
1409 if cf_getter is None:
Xixuan Wu7cc10e52018-04-25 17:04:51 -07001410 build = suite_common.get_test_source_build(builds, **dargs)
Allen Li25bb1c62017-03-09 16:27:00 -08001411 cf_getter = _create_ds_getter(build, devserver)
1412
1413 return cls([name_in_tag_predicate(name)],
1414 name, builds, board, cf_getter, **dargs)
1415
1416
Allen Li3b1d4e52017-03-09 16:23:06 -08001417 def __init__(
1418 self,
1419 predicates,
1420 tag,
1421 builds,
1422 board,
1423 cf_getter,
1424 run_prod_code=False,
1425 afe=None,
1426 tko=None,
1427 pool=None,
1428 results_dir=None,
1429 max_runtime_mins=24*60,
1430 timeout_mins=24*60,
1431 file_bugs=False,
Allen Li3b1d4e52017-03-09 16:23:06 -08001432 suite_job_id=None,
1433 ignore_deps=False,
1434 extra_deps=None,
1435 priority=priorities.Priority.DEFAULT,
1436 forgiving_parser=True,
1437 wait_for_results=True,
1438 job_retry=False,
1439 max_retries=sys.maxint,
1440 offload_failures_only=False,
1441 test_source_build=None,
Allen Li7f43ef92017-03-09 16:29:48 -08001442 job_keyvals=None,
Prathmesh Prabhu013afa52017-09-07 17:54:23 +00001443 test_args=None,
Aviv Keshetd5a83f72017-10-30 12:53:01 -07001444 child_dependencies=(),
Prathmesh Prabhu013afa52017-09-07 17:54:23 +00001445 result_reporter=None,
Allen Li3b1d4e52017-03-09 16:23:06 -08001446 ):
1447 """
1448 Constructor
1449
1450 @param predicates: A list of callables that accept ControlData
1451 representations of control files. A test will be
Allen Li2887e332017-03-09 16:30:36 -08001452 included in suite if all callables in this list
Allen Li3b1d4e52017-03-09 16:23:06 -08001453 return True on the given control file.
1454 @param tag: a string with which to tag jobs run in this suite.
1455 @param builds: the builds on which we're running this suite.
1456 @param board: the board on which we're running this suite.
1457 @param cf_getter: a control_file_getter.ControlFileGetter
1458 @param afe: an instance of AFE as defined in server/frontend.py.
1459 @param tko: an instance of TKO as defined in server/frontend.py.
1460 @param pool: Specify the pool of machines to use for scheduling
1461 purposes.
1462 @param run_prod_code: If true, the suite will run the test code that
1463 lives in prod aka the test code currently on the
1464 lab servers.
1465 @param results_dir: The directory where the job can write results to.
1466 This must be set if you want job_id of sub-jobs
1467 list in the job keyvals.
1468 @param max_runtime_mins: Maximum suite runtime, in minutes.
1469 @param timeout: Maximum job lifetime, in hours.
1470 @param suite_job_id: Job id that will act as parent id to all sub jobs.
1471 Default: None
1472 @param ignore_deps: True if jobs should ignore the DEPENDENCIES
1473 attribute and skip applying of dependency labels.
1474 (Default:False)
1475 @param extra_deps: A list of strings which are the extra DEPENDENCIES
1476 to add to each test being scheduled.
1477 @param priority: Integer priority level. Higher is more important.
1478 @param wait_for_results: Set to False to run the suite job without
1479 waiting for test jobs to finish. Default is
1480 True.
Jacob Kopczynski2cefa1f2018-01-10 17:25:38 -08001481 @param job_retry: A bool value indicating whether jobs should be retried
Allen Li3b1d4e52017-03-09 16:23:06 -08001482 on failure. If True, the field 'JOB_RETRIES' in
1483 control files will be respected. If False, do not
1484 retry.
1485 @param max_retries: Maximum retry limit at suite level.
1486 Regardless how many times each individual test
1487 has been retried, the total number of retries
1488 happening in the suite can't exceed _max_retries.
1489 Default to sys.maxint.
1490 @param offload_failures_only: Only enable gs_offloading for failed
1491 jobs.
1492 @param test_source_build: Build that contains the server-side test code.
1493 @param job_keyvals: General job keyvals to be inserted into keyval file,
1494 which will be used by tko/parse later.
Allen Li7f43ef92017-03-09 16:29:48 -08001495 @param test_args: A dict of args passed all the way to each individual
1496 test that will be actually ran.
Aviv Keshetd5a83f72017-10-30 12:53:01 -07001497 @param child_dependencies: (optional) list of dependency strings
1498 to be added as dependencies to child jobs.
Prathmesh Prabhu013afa52017-09-07 17:54:23 +00001499 @param result_reporter: A _ResultReporter instance to report results. If
1500 None, an _EmailReporter will be created.
Allen Li3b1d4e52017-03-09 16:23:06 -08001501 """
Allen Li00bbe5b2017-03-09 16:44:30 -08001502 tests = find_and_parse_tests(
1503 cf_getter,
1504 _ComposedPredicate(predicates),
1505 tag,
Allen Li00bbe5b2017-03-09 16:44:30 -08001506 forgiving_parser=forgiving_parser,
1507 run_prod_code=run_prod_code,
1508 test_args=test_args,
1509 )
Allen Li3b1d4e52017-03-09 16:23:06 -08001510 super(Suite, self).__init__(
Allen Li00bbe5b2017-03-09 16:44:30 -08001511 tests=tests,
Allen Li3b1d4e52017-03-09 16:23:06 -08001512 tag=tag,
1513 builds=builds,
1514 board=board,
Allen Li3b1d4e52017-03-09 16:23:06 -08001515 afe=afe,
1516 tko=tko,
1517 pool=pool,
1518 results_dir=results_dir,
1519 max_runtime_mins=max_runtime_mins,
1520 timeout_mins=timeout_mins,
1521 file_bugs=file_bugs,
Allen Li3b1d4e52017-03-09 16:23:06 -08001522 suite_job_id=suite_job_id,
1523 ignore_deps=ignore_deps,
1524 extra_deps=extra_deps,
1525 priority=priority,
Allen Li3b1d4e52017-03-09 16:23:06 -08001526 wait_for_results=wait_for_results,
1527 job_retry=job_retry,
1528 max_retries=max_retries,
1529 offload_failures_only=offload_failures_only,
1530 test_source_build=test_source_build,
Prathmesh Prabhu013afa52017-09-07 17:54:23 +00001531 job_keyvals=job_keyvals,
Aviv Keshetd5a83f72017-10-30 12:53:01 -07001532 child_dependencies=child_dependencies,
Prathmesh Prabhu013afa52017-09-07 17:54:23 +00001533 result_reporter=result_reporter,
1534 )
Allen Li3b1d4e52017-03-09 16:23:06 -08001535
Allen Li4b5a24f2017-03-09 16:01:35 -08001536
Allen Li44969e32017-05-24 16:47:37 -07001537class ProvisionSuite(_BaseSuite):
1538 """
1539 A suite for provisioning DUTs.
1540
1541 This is done by creating dummy_Pass tests.
1542 """
1543
1544
1545 def __init__(
1546 self,
1547 tag,
1548 builds,
1549 board,
Allen Li44969e32017-05-24 16:47:37 -07001550 devserver,
Allen Li337e1242017-07-10 13:00:31 -07001551 num_required,
Allen Lide5ecce2017-11-06 17:48:09 -08001552 num_max=float('inf'),
Allen Li44969e32017-05-24 16:47:37 -07001553 cf_getter=None,
1554 run_prod_code=False,
1555 test_args=None,
1556 test_source_build=None,
Allen Liaa7f2842017-07-06 16:06:32 -07001557 **kwargs):
Allen Li44969e32017-05-24 16:47:37 -07001558 """
1559 Constructor
1560
1561 @param tag: a string with which to tag jobs run in this suite.
1562 @param builds: the builds on which we're running this suite.
1563 @param board: the board on which we're running this suite.
Allen Li44969e32017-05-24 16:47:37 -07001564 @param devserver: the devserver which contains the build.
Allen Lide5ecce2017-11-06 17:48:09 -08001565 @param num_required: number of tests that must pass. This is
1566 capped by the number of tests that are run.
1567 @param num_max: max number of tests to make. By default there
1568 is no cap, a test is created for each eligible host.
Allen Li44969e32017-05-24 16:47:37 -07001569 @param cf_getter: a control_file_getter.ControlFileGetter.
1570 @param test_args: A dict of args passed all the way to each individual
1571 test that will be actually ran.
1572 @param test_source_build: Build that contains the server-side test code.
Allen Liaa7f2842017-07-06 16:06:32 -07001573 @param kwargs: Various keyword arguments passed to
1574 _BaseSuite constructor.
Allen Li44969e32017-05-24 16:47:37 -07001575 """
Allen Li44969e32017-05-24 16:47:37 -07001576 super(ProvisionSuite, self).__init__(
Allen Lide5ecce2017-11-06 17:48:09 -08001577 tests=[],
Allen Li44969e32017-05-24 16:47:37 -07001578 tag=tag,
1579 builds=builds,
1580 board=board,
Allen Liaa7f2842017-07-06 16:06:32 -07001581 **kwargs)
Allen Li4f09a6d2018-01-23 15:00:17 -08001582 self._num_successful = 0
1583 self._num_required = 0
1584 self.tests = []
1585
Allen Li0c0e7762017-11-17 14:07:43 -08001586 static_deps = [dep for dep in self._dependencies
1587 if not provision.Provision.acts_on(dep)]
Allen Li4f09a6d2018-01-23 15:00:17 -08001588 if 'pool:suites' in static_deps:
1589 logging.info('Provision suite is disabled on suites pool')
1590 return
1591 logging.debug('Looking for hosts matching %r', static_deps)
Allen Lide5ecce2017-11-06 17:48:09 -08001592 hosts = self._afe.get_hosts(
Allen Li0c0e7762017-11-17 14:07:43 -08001593 invalid=False, multiple_labels=static_deps)
Allen Li42e511e2017-11-13 18:36:34 -08001594 logging.debug('Found %d matching hosts for ProvisionSuite', len(hosts))
Allen Lide5ecce2017-11-06 17:48:09 -08001595 available_hosts = [h for h in hosts if h.is_available()]
Allen Li42e511e2017-11-13 18:36:34 -08001596 logging.debug('Found %d available hosts for ProvisionSuite',
1597 len(available_hosts))
Allen Li4f09a6d2018-01-23 15:00:17 -08001598 dummy_test = _load_dummy_test(
1599 builds, devserver, cf_getter,
1600 run_prod_code, test_args, test_source_build)
Allen Lide5ecce2017-11-06 17:48:09 -08001601 self.tests = [dummy_test] * min(len(available_hosts), num_max)
1602 logging.debug('Made %d tests for ProvisionSuite', len(self.tests))
1603 self._num_required = min(num_required, len(self.tests))
1604 logging.debug('Expecting %d tests to pass for ProvisionSuite',
1605 self._num_required)
Allen Li337e1242017-07-10 13:00:31 -07001606
Prathmesh Prabhu013afa52017-09-07 17:54:23 +00001607 def _handle_result(self, result, record, waiter):
1608 super(ProvisionSuite, self)._handle_result(result, record, waiter)
Allen Li337e1242017-07-10 13:00:31 -07001609 if result.is_good():
1610 self._num_successful += 1
1611
1612 def _finished_waiting(self):
1613 return self._num_successful >= self._num_required
Allen Li44969e32017-05-24 16:47:37 -07001614
1615
1616def _load_dummy_test(
1617 builds,
1618 devserver,
1619 cf_getter=None,
1620 run_prod_code=False,
1621 test_args=None,
1622 test_source_build=None):
1623 """
1624 Load and return the dummy pass test.
1625
1626 @param builds: the builds on which we're running this suite.
1627 @param devserver: the devserver which contains the build.
1628 @param cf_getter: a control_file_getter.ControlFileGetter.
1629 @param test_args: A dict of args passed all the way to each individual
1630 test that will be actually ran.
1631 @param test_source_build: Build that contains the server-side test code.
Allen Li44969e32017-05-24 16:47:37 -07001632 """
1633 if cf_getter is None:
1634 if run_prod_code:
1635 cf_getter = create_fs_getter(_AUTOTEST_DIR)
1636 else:
Xixuan Wu7cc10e52018-04-25 17:04:51 -07001637 build = suite_common.get_test_source_build(
Allen Li44969e32017-05-24 16:47:37 -07001638 builds, test_source_build=test_source_build)
Richard Barnetteadf05862018-06-04 17:37:02 -07001639 devserver.stage_artifacts(image=build,
1640 artifacts=['control_files'])
Allen Li44969e32017-05-24 16:47:37 -07001641 cf_getter = _create_ds_getter(build, devserver)
Xixuan Wu92249042018-04-30 17:17:10 -07001642 retriever = _ControlFileRetriever(cf_getter,
1643 run_prod_code=run_prod_code,
1644 test_args=test_args)
Xixuan Wub2cf7fc2018-05-04 17:37:24 -07001645 return retriever.retrieve_for_test('dummy_Pass')
Allen Li44969e32017-05-24 16:47:37 -07001646
1647
Allen Licec26f72017-03-09 16:39:09 -08001648class _ComposedPredicate(object):
1649 """Return the composition of the predicates.
1650
1651 Predicates are functions that take a test control data object and
1652 return True of that test is to be included. The returned
1653 predicate's set is the intersection of all of the input predicates'
1654 sets (it returns True if all predicates return True).
1655 """
1656
1657 def __init__(self, predicates):
1658 """Initialize instance.
1659
1660 @param predicates: Iterable of predicates.
1661 """
1662 self._predicates = list(predicates)
1663
1664 def __repr__(self):
1665 return '{cls}({this._predicates!r})'.format(
Allen Li5511bd32017-05-17 16:57:26 -07001666 cls=type(self).__name__,
Allen Licec26f72017-03-09 16:39:09 -08001667 this=self,
1668 )
1669
1670 def __call__(self, control_data_):
1671 return all(f(control_data_) for f in self._predicates)
1672
1673
Allen Li9fcd4b42016-12-12 16:15:14 -08001674def _is_nonexistent_board_error(e):
1675 """Return True if error is caused by nonexistent board label.
1676
1677 As of this writing, the particular case we want looks like this:
1678
1679 1) e.problem_keys is a dictionary
1680 2) e.problem_keys['meta_hosts'] exists as the only key
1681 in the dictionary.
1682 3) e.problem_keys['meta_hosts'] matches this pattern:
1683 "Label "board:.*" not found"
1684
1685 We check for conditions 1) and 2) on the
1686 theory that they're relatively immutable.
1687 We don't check condition 3) because it seems
1688 likely to be a maintenance burden, and for the
1689 times when we're wrong, being right shouldn't
1690 matter enough (we _hope_).
1691
1692 @param e: proxy.ValidationError instance
1693 @returns: boolean
1694 """
1695 return (isinstance(e.problem_keys, dict)
1696 and len(e.problem_keys) == 1
1697 and 'meta_hosts' in e.problem_keys)
Allen Liee36ab82017-07-07 15:46:40 -07001698
1699
1700class _ResultReporter(object):
1701 """Abstract base class for reporting test results.
1702
1703 Usually, this is used to report test failures.
1704 """
1705
1706 __metaclass__ = abc.ABCMeta
1707
1708 @abc.abstractmethod
1709 def report(self, result):
1710 """Report test result.
1711
1712 @param result: Status instance for job.
1713 """
1714
1715
Aviv Keshetf93775c2017-08-14 11:07:51 -07001716class _EmailReporter(_ResultReporter):
1717 """Class that emails based on test failures."""
Allen Lic1ce0342017-07-10 12:45:29 -07001718
Aviv Keshetf93775c2017-08-14 11:07:51 -07001719 # TODO(akeshet): Document what |bug_template| is actually supposed to come
1720 # from, and rename it to something unrelated to "bugs" which are no longer
1721 # relevant now that this is purely an email sender.
1722 def __init__(self, suite, bug_template=None):
Allen Lic1ce0342017-07-10 12:45:29 -07001723 self._suite = suite
Aviv Keshetf93775c2017-08-14 11:07:51 -07001724 self._bug_template = bug_template or {}
Allen Lic1ce0342017-07-10 12:45:29 -07001725
1726 def _get_test_bug(self, result):
1727 """Get TestBug for the given result.
1728
1729 @param result: Status instance for a test job.
1730 @returns: TestBug instance.
1731 """
1732 # reporting modules have dependency on external packages, e.g., httplib2
1733 # Such dependency can cause issue to any module tries to import suite.py
1734 # without building site-packages first. Since the reporting modules are
1735 # only used in this function, move the imports here avoid the
1736 # requirement of building site packages to use other functions in this
1737 # module.
1738 from autotest_lib.server.cros.dynamic_suite import reporting
1739
1740 job_views = self._suite._tko.run('get_detailed_test_views',
1741 afe_job_id=result.id)
1742 return reporting.TestBug(self._suite._job_creator.cros_build,
1743 utils.get_chrome_version(job_views),
1744 self._suite._tag,
1745 result)
1746
1747 def _get_bug_template(self, result):
1748 """Get BugTemplate for test job.
1749
1750 @param result: Status instance for job.
1751 @param bug_template: A template dictionary specifying the default bug
1752 filing options for failures in this suite.
1753 @returns: BugTemplate instance
1754 """
1755 # reporting modules have dependency on external packages, e.g., httplib2
1756 # Such dependency can cause issue to any module tries to import suite.py
1757 # without building site-packages first. Since the reporting modules are
1758 # only used in this function, move the imports here avoid the
1759 # requirement of building site packages to use other functions in this
1760 # module.
1761 from autotest_lib.server.cros.dynamic_suite import reporting_utils
1762
1763 # Try to merge with bug template in test control file.
1764 template = reporting_utils.BugTemplate(self._bug_template)
1765 try:
1766 test_data = self._suite._jobs_to_tests[result.id]
1767 return template.finalize_bug_template(
1768 test_data.bug_template)
1769 except AttributeError:
1770 # Test control file does not have bug template defined.
1771 return template.bug_template
1772 except reporting_utils.InvalidBugTemplateException as e:
1773 logging.error('Merging bug templates failed with '
1774 'error: %s An empty bug template will '
1775 'be used.', e)
1776 return {}
1777
Allen Liee36ab82017-07-07 15:46:40 -07001778 def report(self, result):
1779 # reporting modules have dependency on external
1780 # packages, e.g., httplib2 Such dependency can cause
1781 # issue to any module tries to import suite.py without
1782 # building site-packages first. Since the reporting
1783 # modules are only used in this function, move the
1784 # imports here avoid the requirement of building site
1785 # packages to use other functions in this module.
1786 from autotest_lib.server.cros.dynamic_suite import reporting
1787
1788 reporting.send_email(
Aviv Keshetf93775c2017-08-14 11:07:51 -07001789 self._get_test_bug(result),
1790 self._get_bug_template(result))