Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 1 | # 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 | |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 5 | import datetime |
| 6 | import difflib |
| 7 | import hashlib |
| 8 | import logging |
| 9 | import operator |
| 10 | import os |
| 11 | import re |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 12 | import sys |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 13 | |
| 14 | import common |
| 15 | |
J. Richard Barnette | b592fbc | 2014-04-02 10:27:33 -0700 | [diff] [blame] | 16 | from autotest_lib.frontend.afe.json_rpc import proxy |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 17 | from autotest_lib.client.common_lib import control_data |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 18 | from autotest_lib.client.common_lib import enum |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 19 | from autotest_lib.client.common_lib import error |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 20 | from autotest_lib.client.common_lib import global_config |
Alex Miller | 7d658cf | 2013-09-04 16:00:35 -0700 | [diff] [blame] | 21 | from autotest_lib.client.common_lib import priorities |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 22 | from autotest_lib.client.common_lib import site_utils |
| 23 | from autotest_lib.client.common_lib import time_utils |
| 24 | from autotest_lib.client.common_lib import utils |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 25 | from autotest_lib.frontend.afe.json_rpc import proxy |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 26 | from autotest_lib.server.cros import provision |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 27 | from autotest_lib.server.cros.dynamic_suite import constants |
| 28 | from autotest_lib.server.cros.dynamic_suite import control_file_getter |
| 29 | from autotest_lib.server.cros.dynamic_suite import frontend_wrappers |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 30 | from autotest_lib.server.cros.dynamic_suite import job_status |
J. Richard Barnette | e7b98bb | 2013-08-21 16:34:16 -0700 | [diff] [blame] | 31 | from autotest_lib.server.cros.dynamic_suite import tools |
| 32 | from autotest_lib.server.cros.dynamic_suite.job_status import Status |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 33 | |
Shuqian Zhao | ab46881 | 2015-04-08 14:40:38 -0700 | [diff] [blame] | 34 | try: |
| 35 | from chromite.lib import boolparse_lib |
| 36 | from chromite.lib import cros_logging as logging |
| 37 | except ImportError: |
| 38 | print 'Unable to import chromite.' |
| 39 | print 'This script must be either:' |
| 40 | print ' - Be run in the chroot.' |
| 41 | print ' - (not yet supported) be run after running ' |
| 42 | print ' ../utils/build_externals.py' |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 43 | |
Shuqian Zhao | 490f78f | 2016-01-20 13:18:40 -0800 | [diff] [blame] | 44 | _FILE_BUG_SUITES = ['au', 'bvt', 'bvt-cq', 'bvt-inline', 'paygen_au_beta', |
| 45 | 'paygen_au_canary', 'paygen_au_dev', 'paygen_au_stable', |
| 46 | 'sanity', 'push_to_prod'] |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 47 | _AUTOTEST_DIR = global_config.global_config.get_config_value( |
| 48 | 'SCHEDULER', 'drone_installation_directory') |
xixuan | 0f7755d | 2016-04-18 14:49:12 -0700 | [diff] [blame] | 49 | ENABLE_CONTROLS_IN_BATCH = global_config.global_config.get_config_value( |
| 50 | 'CROS', 'enable_getting_controls_in_batch', type=bool, default=False) |
Shuqian Zhao | e33ba4a | 2015-09-11 18:51:43 -0700 | [diff] [blame] | 51 | |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 52 | class RetryHandler(object): |
| 53 | """Maintain retry information. |
| 54 | |
| 55 | @var _retry_map: A dictionary that stores retry history. |
| 56 | The key is afe job id. The value is a dictionary. |
| 57 | {job_id: {'state':RetryHandler.States, 'retry_max':int}} |
| 58 | - state: |
| 59 | The retry state of a job. |
| 60 | NOT_ATTEMPTED: |
| 61 | We haven't done anything about the job. |
| 62 | ATTEMPTED: |
| 63 | We've made an attempt to schedule a retry job. The |
| 64 | scheduling may or may not be successful, e.g. |
| 65 | it might encounter an rpc error. Note failure |
| 66 | in scheduling a retry is different from a retry job failure. |
| 67 | For each job, we only attempt to schedule a retry once. |
| 68 | For example, assume we have a test with JOB_RETRIES=5 and |
| 69 | its second retry job failed. When we attempt to create |
| 70 | a third retry job to retry the second, we hit an rpc |
| 71 | error. In such case, we will give up on all following |
| 72 | retries. |
| 73 | RETRIED: |
| 74 | A retry job has already been successfully |
| 75 | scheduled. |
| 76 | - retry_max: |
| 77 | The maximum of times the job can still |
| 78 | be retried, taking into account retries |
| 79 | that have occurred. |
| 80 | @var _retry_level: A retry might be triggered only if the result |
| 81 | is worse than the level. |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 82 | @var _max_retries: Maximum retry limit at suite level. |
| 83 | Regardless how many times each individual test |
| 84 | has been retried, the total number of retries happening in |
| 85 | the suite can't exceed _max_retries. |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 86 | """ |
| 87 | |
| 88 | States = enum.Enum('NOT_ATTEMPTED', 'ATTEMPTED', 'RETRIED', |
| 89 | start_value=1, step=1) |
| 90 | |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 91 | def __init__(self, initial_jobs_to_tests, retry_level='WARN', |
| 92 | max_retries=None): |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 93 | """Initialize RetryHandler. |
| 94 | |
| 95 | @param initial_jobs_to_tests: A dictionary that maps a job id to |
| 96 | a ControlData object. This dictionary should contain |
| 97 | jobs that are originally scheduled by the suite. |
| 98 | @param retry_level: A retry might be triggered only if the result is |
| 99 | worse than the level. |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 100 | @param max_retries: Integer, maxmium total retries allowed |
| 101 | for the suite. Default to None, no max. |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 102 | """ |
| 103 | self._retry_map = {} |
| 104 | self._retry_level = retry_level |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 105 | self._max_retries = (max_retries |
| 106 | if max_retries is not None else sys.maxint) |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 107 | for job_id, test in initial_jobs_to_tests.items(): |
| 108 | if test.job_retries > 0: |
Allen Li | fb89e2b | 2017-01-03 12:47:58 -0800 | [diff] [blame] | 109 | self._add_job(new_job_id=job_id, |
| 110 | retry_max=test.job_retries) |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 111 | |
| 112 | |
Allen Li | fb89e2b | 2017-01-03 12:47:58 -0800 | [diff] [blame] | 113 | def _add_job(self, new_job_id, retry_max): |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 114 | """Add a newly-created job to the retry map. |
| 115 | |
| 116 | @param new_job_id: The afe_job_id of a newly created job. |
| 117 | @param retry_max: The maximum of times that we could retry |
| 118 | the test if the job fails. |
| 119 | |
| 120 | @raises ValueError if new_job_id is already in retry map. |
| 121 | |
| 122 | """ |
| 123 | if new_job_id in self._retry_map: |
| 124 | raise ValueError('add_job called when job is already in retry map.') |
| 125 | |
| 126 | self._retry_map[new_job_id] = { |
| 127 | 'state': self.States.NOT_ATTEMPTED, |
| 128 | 'retry_max': retry_max} |
| 129 | |
| 130 | |
Allen Li | 0cd1926 | 2017-01-03 12:56:08 -0800 | [diff] [blame] | 131 | def _suite_max_reached(self): |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 132 | """Return whether maximum retry limit for a suite has been reached.""" |
Fang Deng | e4326d6 | 2015-01-06 13:15:15 -0800 | [diff] [blame] | 133 | return self._max_retries <= 0 |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 134 | |
| 135 | |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 136 | def add_retry(self, old_job_id, new_job_id): |
| 137 | """Record a retry. |
| 138 | |
| 139 | Update retry map with the retry information. |
| 140 | |
| 141 | @param old_job_id: The afe_job_id of the job that is retried. |
| 142 | @param new_job_id: The afe_job_id of the retry job. |
| 143 | |
| 144 | @raises KeyError if old_job_id isn't in the retry map. |
| 145 | @raises ValueError if we have already retried or made an attempt |
| 146 | to retry the old job. |
| 147 | |
| 148 | """ |
| 149 | old_record = self._retry_map[old_job_id] |
| 150 | if old_record['state'] != self.States.NOT_ATTEMPTED: |
| 151 | raise ValueError( |
| 152 | 'We have already retried or attempted to retry job %d' % |
| 153 | old_job_id) |
| 154 | old_record['state'] = self.States.RETRIED |
Allen Li | fb89e2b | 2017-01-03 12:47:58 -0800 | [diff] [blame] | 155 | self._add_job(new_job_id=new_job_id, |
| 156 | retry_max=old_record['retry_max'] - 1) |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 157 | self._max_retries -= 1 |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 158 | |
| 159 | |
| 160 | def set_attempted(self, job_id): |
| 161 | """Set the state of the job to ATTEMPTED. |
| 162 | |
| 163 | @param job_id: afe_job_id of a job. |
| 164 | |
| 165 | @raises KeyError if job_id isn't in the retry map. |
| 166 | @raises ValueError if the current state is not NOT_ATTEMPTED. |
| 167 | |
| 168 | """ |
| 169 | current_state = self._retry_map[job_id]['state'] |
| 170 | if current_state != self.States.NOT_ATTEMPTED: |
| 171 | # We are supposed to retry or attempt to retry each job |
| 172 | # only once. Raise an error if this is not the case. |
| 173 | raise ValueError('Unexpected state transition: %s -> %s' % |
| 174 | (self.States.get_string(current_state), |
| 175 | self.States.get_string(self.States.ATTEMPTED))) |
| 176 | else: |
| 177 | self._retry_map[job_id]['state'] = self.States.ATTEMPTED |
| 178 | |
| 179 | |
| 180 | def has_following_retry(self, result): |
| 181 | """Check whether there will be a following retry. |
| 182 | |
| 183 | We have the following cases for a given job id (result.id), |
| 184 | - no retry map entry -> retry not required, no following retry |
| 185 | - has retry map entry: |
| 186 | - already retried -> has following retry |
| 187 | - has not retried |
| 188 | (this branch can be handled by checking should_retry(result)) |
| 189 | - retry_max == 0 --> the last retry job, no more retry |
| 190 | - retry_max > 0 |
| 191 | - attempted, but has failed in scheduling a |
| 192 | following retry due to rpc error --> no more retry |
| 193 | - has not attempped --> has following retry if test failed. |
| 194 | |
| 195 | @param result: A result, encapsulating the status of the job. |
| 196 | |
| 197 | @returns: True, if there will be a following retry. |
| 198 | False otherwise. |
| 199 | |
| 200 | """ |
Allen Li | 2ee2a26 | 2017-01-03 13:21:10 -0800 | [diff] [blame] | 201 | return (result.test_executed |
| 202 | and result.id in self._retry_map |
| 203 | and (self._retry_map[result.id]['state'] == self.States.RETRIED |
| 204 | or self._should_retry(result))) |
Allen Li | 5cb0065 | 2017-01-03 13:06:30 -0800 | [diff] [blame] | 205 | |
| 206 | |
| 207 | def _should_retry(self, result): |
| 208 | """Check whether we should retry a job based on its result. |
| 209 | |
Allen Li | 2ee2a26 | 2017-01-03 13:21:10 -0800 | [diff] [blame] | 210 | This method only makes sense when called by has_following_retry(). |
| 211 | |
Allen Li | 5cb0065 | 2017-01-03 13:06:30 -0800 | [diff] [blame] | 212 | We will retry the job that corresponds to the result |
| 213 | when all of the following are true. |
| 214 | a) The test was actually executed, meaning that if |
| 215 | a job was aborted before it could ever reach the state |
| 216 | of 'Running', the job will not be retried. |
| 217 | b) The result is worse than |self._retry_level| which |
| 218 | defaults to 'WARN'. |
| 219 | c) The test requires retry, i.e. the job has an entry in the retry map. |
| 220 | d) We haven't made any retry attempt yet, i.e. state == NOT_ATTEMPTED |
| 221 | Note that if a test has JOB_RETRIES=5, and the second time |
| 222 | it was retried it hit an rpc error, we will give up on |
| 223 | all following retries. |
| 224 | e) The job has not reached its retry max, i.e. retry_max > 0 |
| 225 | |
| 226 | @param result: A result, encapsulating the status of the job. |
| 227 | |
| 228 | @returns: True if we should retry the job. |
| 229 | |
| 230 | """ |
Allen Li | 2ee2a26 | 2017-01-03 13:21:10 -0800 | [diff] [blame] | 231 | assert result.test_executed |
| 232 | assert result.id in self._retry_map |
Allen Li | 5cb0065 | 2017-01-03 13:06:30 -0800 | [diff] [blame] | 233 | return ( |
| 234 | not self._suite_max_reached() |
Allen Li | 5cb0065 | 2017-01-03 13:06:30 -0800 | [diff] [blame] | 235 | and result.is_worse_than( |
| 236 | job_status.Status(self._retry_level, '', 'reason')) |
Allen Li | 5cb0065 | 2017-01-03 13:06:30 -0800 | [diff] [blame] | 237 | and self._retry_map[result.id]['state'] == self.States.NOT_ATTEMPTED |
| 238 | and self._retry_map[result.id]['retry_max'] > 0 |
| 239 | ) |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 240 | |
| 241 | |
| 242 | def get_retry_max(self, job_id): |
| 243 | """Get the maximum times the job can still be retried. |
| 244 | |
| 245 | @param job_id: afe_job_id of a job. |
| 246 | |
| 247 | @returns: An int, representing the maximum times the job can still be |
| 248 | retried. |
| 249 | @raises KeyError if job_id isn't in the retry map. |
| 250 | |
| 251 | """ |
| 252 | return self._retry_map[job_id]['retry_max'] |
| 253 | |
| 254 | |
Allen Li | 86f8c28 | 2017-02-28 13:09:40 -0800 | [diff] [blame] | 255 | class _ExperimentalTestFilter(object): |
| 256 | """Filter experimental tests.""" |
Allen Li | 6b161c6 | 2017-02-28 13:08:54 -0800 | [diff] [blame] | 257 | |
| 258 | |
| 259 | def __init__(self, tests, add_experimental=True): |
| 260 | """Initialize instance. |
| 261 | |
| 262 | @param tests: iterable of tests (ControlData objects) |
| 263 | @param add_experimental: schedule experimental tests as well, or not. |
| 264 | """ |
| 265 | self._tests = list(tests) |
| 266 | self._add_experimental = add_experimental |
| 267 | |
| 268 | |
Allen Li | 86f8c28 | 2017-02-28 13:09:40 -0800 | [diff] [blame] | 269 | def get_tests_to_schedule(self): |
Allen Li | 6b161c6 | 2017-02-28 13:08:54 -0800 | [diff] [blame] | 270 | """Return a list of tests to be scheduled for this suite. |
| 271 | |
| 272 | @returns: list of tests (ControlData objects) |
| 273 | """ |
| 274 | tests = self.stable_tests |
| 275 | if self._add_experimental: |
| 276 | for test in self.unstable_tests: |
| 277 | if not test.name.startswith(constants.EXPERIMENTAL_PREFIX): |
| 278 | test.name = constants.EXPERIMENTAL_PREFIX + test.name |
| 279 | tests.append(test) |
| 280 | return tests |
| 281 | |
| 282 | |
| 283 | @property |
| 284 | def stable_tests(self): |
| 285 | """Non-experimental tests. |
| 286 | |
| 287 | @returns: list |
| 288 | """ |
| 289 | return filter(lambda t: not t.experimental, self._tests) |
| 290 | |
| 291 | |
| 292 | @property |
| 293 | def unstable_tests(self): |
| 294 | """Experimental tests. |
| 295 | |
| 296 | @returns: list |
| 297 | """ |
| 298 | return filter(lambda t: t.experimental, self._tests) |
| 299 | |
| 300 | |
Allen Li | 9ea208e | 2017-02-28 13:43:11 -0800 | [diff] [blame] | 301 | def _find_test_control_data_for_suite( |
Allen Li | 066f587 | 2017-02-28 13:30:44 -0800 | [diff] [blame] | 302 | cf_getter, suite_name='', add_experimental=False, |
| 303 | forgiving_parser=True, run_prod_code=False, |
| 304 | test_args=None): |
| 305 | """ |
| 306 | Function to scan through all tests and find all tests. |
| 307 | |
| 308 | When this method is called with a file system ControlFileGetter, or |
| 309 | enable_controls_in_batch is set as false, this function will looks at |
| 310 | control files returned by cf_getter.get_control_file_list() for tests. |
| 311 | |
| 312 | If cf_getter is a File system ControlFileGetter, it performs a full |
| 313 | parse of the root directory associated with the getter. This is the |
| 314 | case when it's invoked from suite_preprocessor. |
| 315 | |
| 316 | If cf_getter is a devserver getter it looks up the suite_name in a |
| 317 | suite to control file map generated at build time, and parses the |
| 318 | relevant control files alone. This lookup happens on the devserver, |
| 319 | so as far as this method is concerned, both cases are equivalent. If |
| 320 | enable_controls_in_batch is switched on, this function will call |
| 321 | cf_getter.get_suite_info() to get a dict of control files and contents |
| 322 | in batch. |
| 323 | |
| 324 | @param cf_getter: a control_file_getter.ControlFileGetter used to list |
| 325 | and fetch the content of control files |
| 326 | @param suite_name: If specified, this method will attempt to restrain |
| 327 | the search space to just this suite's control files. |
| 328 | @param add_experimental: add tests with experimental attribute set. |
| 329 | @param forgiving_parser: If False, will raise ControlVariableExceptions |
| 330 | if any are encountered when parsing control |
| 331 | files. Note that this can raise an exception |
| 332 | for syntax errors in unrelated files, because |
| 333 | we parse them before applying the predicate. |
| 334 | @param run_prod_code: If true, the suite will run the test code that |
| 335 | lives in prod aka the test code currently on the |
| 336 | lab servers by disabling SSP for the discovered |
| 337 | tests. |
| 338 | @param test_args: A dict of args to be seeded in test control file under |
| 339 | the name |args_dict|. |
| 340 | |
| 341 | @raises ControlVariableException: If forgiving_parser is False and there |
| 342 | is a syntax error in a control file. |
| 343 | |
| 344 | @returns a dictionary of ControlData objects that based on given |
| 345 | parameters. |
| 346 | """ |
| 347 | logging.debug('Getting control file list for suite: %s', suite_name) |
Allen Li | 1161bab | 2017-02-28 13:49:16 -0800 | [diff] [blame] | 348 | if _should_batch_with(cf_getter): |
Allen Li | 066f587 | 2017-02-28 13:30:44 -0800 | [diff] [blame] | 349 | suite_info = cf_getter.get_suite_info(suite_name=suite_name) |
| 350 | files = suite_info.keys() |
| 351 | else: |
| 352 | files = cf_getter.get_control_file_list(suite_name=suite_name) |
| 353 | |
| 354 | |
| 355 | logging.debug('Parsing control files ...') |
| 356 | matcher = re.compile(r'[^/]+/(deps|profilers)/.+') |
Allen Li | e911e04 | 2017-02-28 14:03:25 -0800 | [diff] [blame] | 357 | filtered_files = (path for path in files if not matcher.match(path)) |
Allen Li | 59664f9 | 2017-02-28 14:08:55 -0800 | [diff] [blame] | 358 | if _should_batch_with(cf_getter): |
| 359 | control_file_texts = _batch_get_control_file_texts( |
| 360 | cf_getter, suite_name, filtered_files) |
| 361 | else: |
| 362 | control_file_texts = _get_control_file_texts( |
| 363 | cf_getter, filtered_files) |
Allen Li | 9d0be12 | 2017-02-28 14:13:04 -0800 | [diff] [blame^] | 364 | return _parse_control_file_texts( |
| 365 | control_file_texts=control_file_texts, |
| 366 | add_experimental=add_experimental, |
| 367 | forgiving_parser=forgiving_parser, |
| 368 | run_prod_code=run_prod_code, |
| 369 | test_args=test_args) |
| 370 | |
| 371 | |
| 372 | def _parse_control_file_texts(control_file_texts, add_experimental=False, |
| 373 | forgiving_parser=True, run_prod_code=False, |
| 374 | test_args=None): |
| 375 | """Parse control file texts. |
| 376 | |
| 377 | @param control_file_texts: iterable of (path, text) pairs |
| 378 | @param add_experimental: add tests with experimental attribute set. |
| 379 | @param forgiving_parser: If False, will raise ControlVariableExceptions |
| 380 | if any are encountered when parsing control |
| 381 | files. Note that this can raise an exception |
| 382 | for syntax errors in unrelated files, because |
| 383 | we parse them before applying the predicate. |
| 384 | @param run_prod_code: If true, the suite will run the test code that |
| 385 | lives in prod aka the test code currently on the |
| 386 | lab servers by disabling SSP for the discovered |
| 387 | tests. |
| 388 | @param test_args: A dict of args to be seeded in test control file under |
| 389 | the name |args_dict|. |
| 390 | |
| 391 | @returns: a dictionary of ControlData objects |
| 392 | """ |
| 393 | tests = {} |
Allen Li | 59664f9 | 2017-02-28 14:08:55 -0800 | [diff] [blame] | 394 | for file, text in control_file_texts: |
Allen Li | 066f587 | 2017-02-28 13:30:44 -0800 | [diff] [blame] | 395 | # Seed test_args into the control file. |
| 396 | if test_args: |
| 397 | text = tools.inject_vars(test_args, text) |
| 398 | try: |
| 399 | found_test = control_data.parse_control_string( |
| 400 | text, raise_warnings=True, path=file) |
| 401 | if not add_experimental and found_test.experimental: |
| 402 | continue |
| 403 | found_test.text = text |
| 404 | if run_prod_code: |
| 405 | found_test.require_ssp = False |
| 406 | tests[file] = found_test |
| 407 | except control_data.ControlVariableException, e: |
| 408 | if not forgiving_parser: |
| 409 | msg = "Failed parsing %s\n%s" % (file, e) |
| 410 | raise control_data.ControlVariableException(msg) |
| 411 | logging.warning("Skipping %s\n%s", file, e) |
| 412 | except Exception, e: |
| 413 | logging.error("Bad %s\n%s", file, e) |
| 414 | return tests |
| 415 | |
| 416 | |
Allen Li | 59664f9 | 2017-02-28 14:08:55 -0800 | [diff] [blame] | 417 | def _batch_get_control_file_texts(cf_getter, suite_name, paths): |
| 418 | """Get control file content for given files. |
| 419 | |
| 420 | @param cf_getter: a control_file_getter.ControlFileGetter used to list |
| 421 | and fetch the content of control files |
| 422 | @param suite_name: suite name |
| 423 | @param paths: iterable of control file paths |
| 424 | @returns: generator yielding (path, text) tuples |
| 425 | """ |
| 426 | suite_info = cf_getter.get_suite_info(suite_name=suite_name) |
| 427 | for path in paths: |
| 428 | yield path, suite_info[path] |
| 429 | |
| 430 | |
| 431 | def _get_control_file_texts(cf_getter, paths): |
| 432 | """Get control file content for given files. |
| 433 | |
| 434 | @param cf_getter: a control_file_getter.ControlFileGetter used to list |
| 435 | and fetch the content of control files |
| 436 | @param paths: iterable of control file paths |
| 437 | @returns: generator yielding (path, text) tuples |
| 438 | """ |
| 439 | for path in paths: |
| 440 | yield path, cf_getter.get_control_file_contents(path) |
| 441 | |
| 442 | |
Allen Li | 1161bab | 2017-02-28 13:49:16 -0800 | [diff] [blame] | 443 | def _should_batch_with(cf_getter): |
| 444 | """Return whether control files should be fetched in batch. |
| 445 | |
| 446 | This depends on the control file getter and configuration options. |
| 447 | |
| 448 | @param cf_getter: a control_file_getter.ControlFileGetter used to list |
| 449 | and fetch the content of control files |
| 450 | """ |
| 451 | return (ENABLE_CONTROLS_IN_BATCH |
| 452 | and isinstance(cf_getter, control_file_getter.DevServerGetter)) |
| 453 | |
| 454 | |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 455 | class Suite(object): |
| 456 | """ |
| 457 | A suite of tests, defined by some predicate over control file variables. |
| 458 | |
| 459 | Given a place to search for control files a predicate to match the desired |
| 460 | tests, can gather tests and fire off jobs to run them, and then wait for |
| 461 | results. |
| 462 | |
| 463 | @var _predicate: a function that should return True when run over a |
| 464 | ControlData representation of a control file that should be in |
| 465 | this Suite. |
| 466 | @var _tag: a string with which to tag jobs run in this suite. |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 467 | @var _builds: the builds on which we're running this suite. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 468 | @var _afe: an instance of AFE as defined in server/frontend.py. |
| 469 | @var _tko: an instance of TKO as defined in server/frontend.py. |
| 470 | @var _jobs: currently scheduled jobs, if any. |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 471 | @var _jobs_to_tests: a dictionary that maps job ids to tests represented |
| 472 | ControlData objects. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 473 | @var _cf_getter: a control_file_getter.ControlFileGetter |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 474 | @var _retry: a bool value indicating whether jobs should be retried on |
| 475 | failure. |
| 476 | @var _retry_handler: a RetryHandler object. |
| 477 | |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 478 | """ |
| 479 | |
| 480 | |
| 481 | @staticmethod |
Allen Li | 9864ed6 | 2016-12-29 16:30:53 -0800 | [diff] [blame] | 482 | def _create_ds_getter(build, devserver): |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 483 | """ |
| 484 | @param build: the build on which we're running this suite. |
Chris Sosa | accb5ce | 2012-08-30 17:29:15 -0700 | [diff] [blame] | 485 | @param devserver: the devserver which contains the build. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 486 | @return a FileSystemGetter instance that looks under |autotest_dir|. |
| 487 | """ |
Chris Sosa | accb5ce | 2012-08-30 17:29:15 -0700 | [diff] [blame] | 488 | return control_file_getter.DevServerGetter(build, devserver) |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 489 | |
| 490 | |
| 491 | @staticmethod |
| 492 | def create_fs_getter(autotest_dir): |
| 493 | """ |
| 494 | @param autotest_dir: the place to find autotests. |
| 495 | @return a FileSystemGetter instance that looks under |autotest_dir|. |
| 496 | """ |
| 497 | # currently hard-coded places to look for tests. |
| 498 | subpaths = ['server/site_tests', 'client/site_tests', |
| 499 | 'server/tests', 'client/tests'] |
| 500 | directories = [os.path.join(autotest_dir, p) for p in subpaths] |
| 501 | return control_file_getter.FileSystemGetter(directories) |
| 502 | |
| 503 | |
| 504 | @staticmethod |
Allen Li | f20e17d | 2017-01-03 18:24:19 -0800 | [diff] [blame] | 505 | def name_in_tag_predicate(name): |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 506 | """Returns predicate that takes a control file and looks for |name|. |
| 507 | |
| 508 | Builds a predicate that takes in a parsed control file (a ControlData) |
| 509 | and returns True if the SUITE tag is present and contains |name|. |
| 510 | |
| 511 | @param name: the suite name to base the predicate on. |
| 512 | @return a callable that takes a ControlData and looks for |name| in that |
| 513 | ControlData object's suite member. |
| 514 | """ |
Allen Li | 3083370 | 2017-01-03 18:34:15 -0800 | [diff] [blame] | 515 | return lambda t: name in t.suite_tag_parts |
Dan Shi | 5783f8a | 2014-12-22 14:34:45 -0800 | [diff] [blame] | 516 | |
| 517 | |
Allen Li | f20e17d | 2017-01-03 18:24:19 -0800 | [diff] [blame] | 518 | @staticmethod |
| 519 | def name_in_tag_similarity_predicate(name): |
Dan Shi | 5783f8a | 2014-12-22 14:34:45 -0800 | [diff] [blame] | 520 | """Returns predicate that takes a control file and gets the similarity |
| 521 | of the suites in the control file and the given name. |
| 522 | |
| 523 | Builds a predicate that takes in a parsed control file (a ControlData) |
| 524 | and returns a list of tuples of (suite name, ratio), where suite name |
| 525 | is each suite listed in the control file, and ratio is the similarity |
| 526 | between each suite and the given name. |
| 527 | |
| 528 | @param name: the suite name to base the predicate on. |
| 529 | @return a callable that takes a ControlData and returns a list of tuples |
| 530 | of (suite name, ratio), where suite name is each suite listed in |
| 531 | the control file, and ratio is the similarity between each suite |
| 532 | and the given name. |
| 533 | """ |
Allen Li | 3083370 | 2017-01-03 18:34:15 -0800 | [diff] [blame] | 534 | return lambda t: [(suite, |
| 535 | difflib.SequenceMatcher(a=suite, b=name).ratio()) |
| 536 | for suite in t.suite_tag_parts] or [(None, 0)] |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 537 | |
| 538 | |
| 539 | @staticmethod |
Aviv Keshet | 40222a4 | 2013-06-04 16:25:49 -0700 | [diff] [blame] | 540 | def test_name_equals_predicate(test_name): |
| 541 | """Returns predicate that matched based on a test's name. |
| 542 | |
| 543 | Builds a predicate that takes in a parsed control file (a ControlData) |
| 544 | and returns True if the test name is equal to |test_name|. |
| 545 | |
| 546 | @param test_name: the test name to base the predicate on. |
| 547 | @return a callable that takes a ControlData and looks for |test_name| |
| 548 | in that ControlData's name. |
| 549 | """ |
| 550 | return lambda t: hasattr(t, 'name') and test_name == t.name |
| 551 | |
| 552 | |
| 553 | @staticmethod |
Aviv Keshet | a6adc7a | 2013-08-30 11:13:38 -0700 | [diff] [blame] | 554 | def test_name_matches_pattern_predicate(test_name_pattern): |
| 555 | """Returns predicate that matches based on a test's name pattern. |
| 556 | |
| 557 | Builds a predicate that takes in a parsed control file (a ControlData) |
| 558 | and returns True if the test name matches the given regular expression. |
| 559 | |
| 560 | @param test_name_pattern: regular expression (string) to match against |
| 561 | test names. |
| 562 | @return a callable that takes a ControlData and returns |
| 563 | True if the name fields matches the pattern. |
| 564 | """ |
| 565 | return lambda t: hasattr(t, 'name') and re.match(test_name_pattern, |
| 566 | t.name) |
| 567 | |
| 568 | |
| 569 | @staticmethod |
| 570 | def test_file_matches_pattern_predicate(test_file_pattern): |
| 571 | """Returns predicate that matches based on a test's file name pattern. |
| 572 | |
| 573 | Builds a predicate that takes in a parsed control file (a ControlData) |
| 574 | and returns True if the test's control file name matches the given |
| 575 | regular expression. |
| 576 | |
| 577 | @param test_file_pattern: regular expression (string) to match against |
| 578 | control file names. |
| 579 | @return a callable that takes a ControlData and and returns |
| 580 | True if control file name matches the pattern. |
| 581 | """ |
| 582 | return lambda t: hasattr(t, 'path') and re.match(test_file_pattern, |
| 583 | t.path) |
| 584 | |
| 585 | |
| 586 | @staticmethod |
Shuqian Zhao | ab46881 | 2015-04-08 14:40:38 -0700 | [diff] [blame] | 587 | def matches_attribute_expression_predicate(test_attr_boolstr): |
| 588 | """Returns predicate that matches based on boolean expression of |
| 589 | attributes. |
| 590 | |
| 591 | Builds a predicate that takes in a parsed control file (a ControlData) |
| 592 | ans returns True if the test attributes satisfy the given attribute |
| 593 | boolean expression. |
| 594 | |
| 595 | @param test_attr_boolstr: boolean expression of the attributes to be |
| 596 | test, like 'system:all and interval:daily'. |
| 597 | |
| 598 | @return a callable that takes a ControlData and returns True if the test |
| 599 | attributes satisfy the given boolean expression. |
| 600 | """ |
| 601 | return lambda t: boolparse_lib.BoolstrResult( |
| 602 | test_attr_boolstr, t.attributes) |
| 603 | |
| 604 | @staticmethod |
Dan Shi | 5783f8a | 2014-12-22 14:34:45 -0800 | [diff] [blame] | 605 | def test_name_similarity_predicate(test_name): |
| 606 | """Returns predicate that matched based on a test's name. |
| 607 | |
| 608 | Builds a predicate that takes in a parsed control file (a ControlData) |
| 609 | and returns a tuple of (test name, ratio), where ratio is the similarity |
| 610 | between the test name and the given test_name. |
| 611 | |
| 612 | @param test_name: the test name to base the predicate on. |
| 613 | @return a callable that takes a ControlData and returns a tuple of |
| 614 | (test name, ratio), where ratio is the similarity between the |
| 615 | test name and the given test_name. |
| 616 | """ |
| 617 | return lambda t: ((None, 0) if not hasattr(t, 'name') else |
| 618 | (t.name, |
| 619 | difflib.SequenceMatcher(a=t.name, b=test_name).ratio())) |
| 620 | |
| 621 | |
| 622 | @staticmethod |
| 623 | def test_file_similarity_predicate(test_file_pattern): |
| 624 | """Returns predicate that gets the similarity based on a test's file |
| 625 | name pattern. |
| 626 | |
| 627 | Builds a predicate that takes in a parsed control file (a ControlData) |
| 628 | and returns a tuple of (file path, ratio), where ratio is the |
| 629 | similarity between the test file name and the given test_file_pattern. |
| 630 | |
| 631 | @param test_file_pattern: regular expression (string) to match against |
| 632 | control file names. |
| 633 | @return a callable that takes a ControlData and and returns a tuple of |
| 634 | (file path, ratio), where ratio is the similarity between the |
| 635 | test file name and the given test_file_pattern. |
| 636 | """ |
| 637 | return lambda t: ((None, 0) if not hasattr(t, 'path') else |
| 638 | (t.path, difflib.SequenceMatcher(a=t.path, |
| 639 | b=test_file_pattern).ratio())) |
| 640 | |
| 641 | |
Allen Li | 9864ed6 | 2016-12-29 16:30:53 -0800 | [diff] [blame] | 642 | @classmethod |
| 643 | def list_all_suites(cls, build, devserver, cf_getter=None): |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 644 | """ |
| 645 | Parses all ControlData objects with a SUITE tag and extracts all |
| 646 | defined suite names. |
| 647 | |
Chris Sosa | accb5ce | 2012-08-30 17:29:15 -0700 | [diff] [blame] | 648 | @param build: the build on which we're running this suite. |
| 649 | @param devserver: the devserver which contains the build. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 650 | @param cf_getter: control_file_getter.ControlFileGetter. Defaults to |
| 651 | using DevServerGetter. |
| 652 | |
| 653 | @return list of suites |
| 654 | """ |
| 655 | if cf_getter is None: |
Allen Li | 9864ed6 | 2016-12-29 16:30:53 -0800 | [diff] [blame] | 656 | cf_getter = cls._create_ds_getter(build, devserver) |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 657 | |
| 658 | suites = set() |
Allen Li | 3083370 | 2017-01-03 18:34:15 -0800 | [diff] [blame] | 659 | predicate = lambda t: True |
Allen Li | c9be366 | 2017-01-03 17:56:26 -0800 | [diff] [blame] | 660 | for test in cls.find_and_parse_tests(cf_getter, predicate, |
Allen Li | 3083370 | 2017-01-03 18:34:15 -0800 | [diff] [blame] | 661 | add_experimental=True): |
Allen Li | f20e17d | 2017-01-03 18:24:19 -0800 | [diff] [blame] | 662 | suites.update(test.suite_tag_parts) |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 663 | return list(suites) |
| 664 | |
| 665 | |
| 666 | @staticmethod |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 667 | def get_test_source_build(builds, **dargs): |
| 668 | """Get the build of test code. |
| 669 | |
| 670 | Get the test source build from arguments. If parameter |
| 671 | `test_source_build` is set and has a value, return its value. Otherwise |
| 672 | returns the ChromeOS build name if it exists. If ChromeOS build is not |
| 673 | specified either, raise SuiteArgumentException. |
| 674 | |
| 675 | @param builds: the builds on which we're running this suite. It's a |
| 676 | dictionary of version_prefix:build. |
| 677 | @param **dargs: Any other Suite constructor parameters, as described |
| 678 | in Suite.__init__ docstring. |
| 679 | |
| 680 | @return: The build contains the test code. |
| 681 | @raise: SuiteArgumentException if both test_source_build and ChromeOS |
| 682 | build are not specified. |
| 683 | |
| 684 | """ |
| 685 | if dargs.get('test_source_build', None): |
| 686 | return dargs['test_source_build'] |
| 687 | test_source_build = builds.get(provision.CROS_VERSION_PREFIX, None) |
| 688 | if not test_source_build: |
| 689 | raise error.SuiteArgumentException( |
| 690 | 'test_source_build must be specified if CrOS build is not ' |
| 691 | 'specified.') |
| 692 | return test_source_build |
| 693 | |
| 694 | |
Allen Li | 9864ed6 | 2016-12-29 16:30:53 -0800 | [diff] [blame] | 695 | @classmethod |
| 696 | def create_from_predicates(cls, predicates, builds, board, devserver, |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 697 | cf_getter=None, name='ad_hoc_suite', |
| 698 | run_prod_code=False, **dargs): |
Aviv Keshet | 69ebb6c | 2013-06-11 13:58:44 -0700 | [diff] [blame] | 699 | """ |
| 700 | Create a Suite using a given predicate test filters. |
| 701 | |
| 702 | Uses supplied predicate(s) to instantiate a Suite. Looks for tests in |
| 703 | |autotest_dir| and will schedule them using |afe|. Pulls control files |
| 704 | from the default dev server. Results will be pulled from |tko| upon |
| 705 | completion. |
| 706 | |
| 707 | @param predicates: A list of callables that accept ControlData |
| 708 | representations of control files. A test will be |
Aviv Keshet | 938a677 | 2013-07-25 14:05:45 -0700 | [diff] [blame] | 709 | included in suite if all callables in this list |
Aviv Keshet | 69ebb6c | 2013-06-11 13:58:44 -0700 | [diff] [blame] | 710 | return True on the given control file. |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 711 | @param builds: the builds on which we're running this suite. It's a |
| 712 | dictionary of version_prefix:build. |
Alex Miller | a091307 | 2013-06-12 10:01:51 -0700 | [diff] [blame] | 713 | @param board: the board on which we're running this suite. |
Aviv Keshet | 69ebb6c | 2013-06-11 13:58:44 -0700 | [diff] [blame] | 714 | @param devserver: the devserver which contains the build. |
| 715 | @param cf_getter: control_file_getter.ControlFileGetter. Defaults to |
| 716 | using DevServerGetter. |
| 717 | @param name: name of suite. Defaults to 'ad_hoc_suite' |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 718 | @param run_prod_code: If true, the suite will run the tests that |
| 719 | lives in prod aka the test code currently on the |
| 720 | lab servers. |
Aviv Keshet | 69ebb6c | 2013-06-11 13:58:44 -0700 | [diff] [blame] | 721 | @param **dargs: Any other Suite constructor parameters, as described |
| 722 | in Suite.__init__ docstring. |
| 723 | @return a Suite instance. |
| 724 | """ |
| 725 | if cf_getter is None: |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 726 | if run_prod_code: |
Allen Li | 9864ed6 | 2016-12-29 16:30:53 -0800 | [diff] [blame] | 727 | cf_getter = cls.create_fs_getter(_AUTOTEST_DIR) |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 728 | else: |
Allen Li | c9be366 | 2017-01-03 17:56:26 -0800 | [diff] [blame] | 729 | build = cls.get_test_source_build(builds, **dargs) |
Allen Li | 9864ed6 | 2016-12-29 16:30:53 -0800 | [diff] [blame] | 730 | cf_getter = cls._create_ds_getter(build, devserver) |
Aviv Keshet | 69ebb6c | 2013-06-11 13:58:44 -0700 | [diff] [blame] | 731 | |
Allen Li | c9be366 | 2017-01-03 17:56:26 -0800 | [diff] [blame] | 732 | return cls(predicates, |
| 733 | name, builds, board, cf_getter, run_prod_code, **dargs) |
Aviv Keshet | 69ebb6c | 2013-06-11 13:58:44 -0700 | [diff] [blame] | 734 | |
| 735 | |
Allen Li | 9864ed6 | 2016-12-29 16:30:53 -0800 | [diff] [blame] | 736 | @classmethod |
| 737 | def create_from_name(cls, name, builds, board, devserver, cf_getter=None, |
Alex Miller | a091307 | 2013-06-12 10:01:51 -0700 | [diff] [blame] | 738 | **dargs): |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 739 | """ |
| 740 | Create a Suite using a predicate based on the SUITE control file var. |
| 741 | |
| 742 | Makes a predicate based on |name| and uses it to instantiate a Suite |
| 743 | that looks for tests in |autotest_dir| and will schedule them using |
| 744 | |afe|. Pulls control files from the default dev server. |
| 745 | Results will be pulled from |tko| upon completion. |
| 746 | |
| 747 | @param name: a value of the SUITE control file variable to search for. |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 748 | @param builds: the builds on which we're running this suite. It's a |
| 749 | dictionary of version_prefix:build. |
Alex Miller | a091307 | 2013-06-12 10:01:51 -0700 | [diff] [blame] | 750 | @param board: the board on which we're running this suite. |
Chris Sosa | accb5ce | 2012-08-30 17:29:15 -0700 | [diff] [blame] | 751 | @param devserver: the devserver which contains the build. |
Aviv Keshet | 813d678 | 2013-06-04 17:11:03 -0700 | [diff] [blame] | 752 | @param cf_getter: control_file_getter.ControlFileGetter. Defaults to |
| 753 | using DevServerGetter. |
| 754 | @param **dargs: Any other Suite constructor parameters, as described |
| 755 | in Suite.__init__ docstring. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 756 | @return a Suite instance. |
| 757 | """ |
| 758 | if cf_getter is None: |
Allen Li | 9864ed6 | 2016-12-29 16:30:53 -0800 | [diff] [blame] | 759 | build = cls.get_test_source_build(builds, **dargs) |
| 760 | cf_getter = cls._create_ds_getter(build, devserver) |
Chris Sosa | accb5ce | 2012-08-30 17:29:15 -0700 | [diff] [blame] | 761 | |
Allen Li | c9be366 | 2017-01-03 17:56:26 -0800 | [diff] [blame] | 762 | return cls([cls.name_in_tag_predicate(name)], |
| 763 | name, builds, board, cf_getter, **dargs) |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 764 | |
| 765 | |
Allen Li | 6fff550 | 2016-12-09 18:04:26 -0800 | [diff] [blame] | 766 | def __init__( |
| 767 | self, |
| 768 | predicates, |
| 769 | tag, |
| 770 | builds, |
| 771 | board, |
| 772 | cf_getter, |
| 773 | run_prod_code=False, |
| 774 | afe=None, |
| 775 | tko=None, |
| 776 | pool=None, |
| 777 | results_dir=None, |
| 778 | max_runtime_mins=24*60, |
| 779 | timeout_mins=24*60, |
| 780 | file_bugs=False, |
| 781 | file_experimental_bugs=False, |
| 782 | suite_job_id=None, |
| 783 | ignore_deps=False, |
Allen Li | 493eefa | 2016-12-09 18:05:35 -0800 | [diff] [blame] | 784 | extra_deps=None, |
Allen Li | 6fff550 | 2016-12-09 18:04:26 -0800 | [diff] [blame] | 785 | priority=priorities.Priority.DEFAULT, |
| 786 | forgiving_parser=True, |
| 787 | wait_for_results=True, |
| 788 | job_retry=False, |
| 789 | max_retries=sys.maxint, |
| 790 | offload_failures_only=False, |
Shuqian Zhao | da1118d | 2017-02-13 16:22:58 -0800 | [diff] [blame] | 791 | test_source_build=None, |
Shuqian Zhao | ed0da86 | 2017-03-06 14:47:13 -0800 | [diff] [blame] | 792 | job_keyvals=None, |
| 793 | test_args=None |
Allen Li | 6fff550 | 2016-12-09 18:04:26 -0800 | [diff] [blame] | 794 | ): |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 795 | """ |
| 796 | Constructor |
| 797 | |
Aviv Keshet | 40222a4 | 2013-06-04 16:25:49 -0700 | [diff] [blame] | 798 | @param predicates: A list of callables that accept ControlData |
| 799 | representations of control files. A test will be |
| 800 | included in suite is all callables in this list |
| 801 | return True on the given control file. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 802 | @param tag: a string with which to tag jobs run in this suite. |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 803 | @param builds: the builds on which we're running this suite. |
Alex Miller | a091307 | 2013-06-12 10:01:51 -0700 | [diff] [blame] | 804 | @param board: the board on which we're running this suite. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 805 | @param cf_getter: a control_file_getter.ControlFileGetter |
| 806 | @param afe: an instance of AFE as defined in server/frontend.py. |
| 807 | @param tko: an instance of TKO as defined in server/frontend.py. |
| 808 | @param pool: Specify the pool of machines to use for scheduling |
| 809 | purposes. |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 810 | @param run_prod_code: If true, the suite will run the test code that |
| 811 | lives in prod aka the test code currently on the |
| 812 | lab servers. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 813 | @param results_dir: The directory where the job can write results to. |
| 814 | This must be set if you want job_id of sub-jobs |
| 815 | list in the job keyvals. |
Aviv Keshet | 1830892 | 2013-02-19 17:49:49 -0800 | [diff] [blame] | 816 | @param max_runtime_mins: Maximum suite runtime, in minutes. |
Alex Miller | 028b031 | 2013-09-07 15:25:45 -0700 | [diff] [blame] | 817 | @param timeout: Maximum job lifetime, in hours. |
Aviv Keshet | 1830892 | 2013-02-19 17:49:49 -0800 | [diff] [blame] | 818 | @param suite_job_id: Job id that will act as parent id to all sub jobs. |
| 819 | Default: None |
Aviv Keshet | d7959f3 | 2013-05-17 15:58:43 -0700 | [diff] [blame] | 820 | @param ignore_deps: True if jobs should ignore the DEPENDENCIES |
| 821 | attribute and skip applying of dependency labels. |
| 822 | (Default:False) |
Alex Miller | 47a0367 | 2013-08-27 09:09:53 -0700 | [diff] [blame] | 823 | @param extra_deps: A list of strings which are the extra DEPENDENCIES |
| 824 | to add to each test being scheduled. |
Alex Miller | 7d658cf | 2013-09-04 16:00:35 -0700 | [diff] [blame] | 825 | @param priority: Integer priority level. Higher is more important. |
Dan Shi | 9512241 | 2013-11-12 16:20:33 -0800 | [diff] [blame] | 826 | @param wait_for_results: Set to False to run the suite job without |
| 827 | waiting for test jobs to finish. Default is |
| 828 | True. |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 829 | @param job_retry: A bool value indicating whether jobs should be retired |
| 830 | on failure. If True, the field 'JOB_RETRIES' in |
| 831 | control files will be respected. If False, do not |
| 832 | retry. |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 833 | @param max_retries: Maximum retry limit at suite level. |
| 834 | Regardless how many times each individual test |
| 835 | has been retried, the total number of retries |
| 836 | happening in the suite can't exceed _max_retries. |
| 837 | Default to sys.maxint. |
Simran Basi | 1e10e92 | 2015-04-16 15:09:56 -0700 | [diff] [blame] | 838 | @param offload_failures_only: Only enable gs_offloading for failed |
| 839 | jobs. |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 840 | @param test_source_build: Build that contains the server-side test code. |
Shuqian Zhao | da1118d | 2017-02-13 16:22:58 -0800 | [diff] [blame] | 841 | @param job_keyvals: General job keyvals to be inserted into keyval file, |
| 842 | which will be used by tko/parse later. |
Shuqian Zhao | ed0da86 | 2017-03-06 14:47:13 -0800 | [diff] [blame] | 843 | @param test_args: A dict of args passed all the way to each individual |
| 844 | test that will be actually ran. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 845 | """ |
Allen Li | 493eefa | 2016-12-09 18:05:35 -0800 | [diff] [blame] | 846 | if extra_deps is None: |
| 847 | extra_deps = [] |
| 848 | |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 849 | self._tag = tag |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 850 | self._builds = builds |
Alex Miller | a091307 | 2013-06-12 10:01:51 -0700 | [diff] [blame] | 851 | self._board = board |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 852 | self._cf_getter = cf_getter |
| 853 | self._results_dir = results_dir |
| 854 | self._afe = afe or frontend_wrappers.RetryingAFE(timeout_min=30, |
| 855 | delay_sec=10, |
| 856 | debug=False) |
| 857 | self._tko = tko or frontend_wrappers.RetryingTKO(timeout_min=30, |
| 858 | delay_sec=10, |
| 859 | debug=False) |
| 860 | self._pool = pool |
| 861 | self._jobs = [] |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 862 | self._jobs_to_tests = {} |
Allen Li | 7947f73 | 2016-12-29 16:44:28 -0800 | [diff] [blame] | 863 | self.tests = self.find_and_parse_tests( |
Allen Li | 8a64909 | 2016-12-09 18:07:39 -0800 | [diff] [blame] | 864 | self._cf_getter, |
Allen Li | d69b9f0 | 2016-12-09 18:15:59 -0800 | [diff] [blame] | 865 | lambda control_data: all(f(control_data) for f in predicates), |
Allen Li | 8a64909 | 2016-12-09 18:07:39 -0800 | [diff] [blame] | 866 | self._tag, |
| 867 | add_experimental=True, |
| 868 | forgiving_parser=forgiving_parser, |
| 869 | run_prod_code=run_prod_code, |
Shuqian Zhao | ed0da86 | 2017-03-06 14:47:13 -0800 | [diff] [blame] | 870 | test_args=test_args, |
Allen Li | 8a64909 | 2016-12-09 18:07:39 -0800 | [diff] [blame] | 871 | ) |
beeps | 89f1e06 | 2013-09-18 12:00:17 -0700 | [diff] [blame] | 872 | |
Simran Basi | c68cda4 | 2012-11-19 17:03:18 -0800 | [diff] [blame] | 873 | self._max_runtime_mins = max_runtime_mins |
Simran Basi | 8705d67 | 2013-11-19 15:56:58 -0800 | [diff] [blame] | 874 | self._timeout_mins = timeout_mins |
Alex Miller | a3a4fe7 | 2013-01-22 09:57:47 -0800 | [diff] [blame] | 875 | self._file_bugs = file_bugs |
beeps | da5b711 | 2013-05-30 11:34:14 -0700 | [diff] [blame] | 876 | self._file_experimental_bugs = file_experimental_bugs |
Aviv Keshet | 1830892 | 2013-02-19 17:49:49 -0800 | [diff] [blame] | 877 | self._suite_job_id = suite_job_id |
Aviv Keshet | d7959f3 | 2013-05-17 15:58:43 -0700 | [diff] [blame] | 878 | self._ignore_deps = ignore_deps |
Alex Miller | 47a0367 | 2013-08-27 09:09:53 -0700 | [diff] [blame] | 879 | self._extra_deps = extra_deps |
Alex Miller | 7d658cf | 2013-09-04 16:00:35 -0700 | [diff] [blame] | 880 | self._priority = priority |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 881 | self._job_retry=job_retry |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 882 | self._max_retries = max_retries |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 883 | # RetryHandler to be initialized in schedule() |
| 884 | self._retry_handler = None |
Dan Shi | 9512241 | 2013-11-12 16:20:33 -0800 | [diff] [blame] | 885 | self.wait_for_results = wait_for_results |
Simran Basi | 1e10e92 | 2015-04-16 15:09:56 -0700 | [diff] [blame] | 886 | self._offload_failures_only = offload_failures_only |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 887 | self._test_source_build = test_source_build |
Shuqian Zhao | da1118d | 2017-02-13 16:22:58 -0800 | [diff] [blame] | 888 | self._job_keyvals = job_keyvals |
Shuqian Zhao | ed0da86 | 2017-03-06 14:47:13 -0800 | [diff] [blame] | 889 | self._test_args = test_args |
Alex Miller | a3a4fe7 | 2013-01-22 09:57:47 -0800 | [diff] [blame] | 890 | |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 891 | |
| 892 | @property |
Allen Li | db8eafe | 2016-12-12 16:33:58 -0800 | [diff] [blame] | 893 | def _cros_build(self): |
| 894 | """Return the CrOS build or the first build in the builds dict.""" |
| 895 | # TODO(ayatane): Note that the builds dict isn't ordered. I'm not |
| 896 | # sure what the implications of this are, but it's probably not a |
| 897 | # good thing. |
| 898 | return self._builds.get(provision.CROS_VERSION_PREFIX, |
| 899 | self._builds.values()[0]) |
| 900 | |
| 901 | |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 902 | def _create_job(self, test, retry_for=None): |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 903 | """ |
| 904 | Thin wrapper around frontend.AFE.create_job(). |
| 905 | |
| 906 | @param test: ControlData object for a test to run. |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 907 | @param retry_for: If the to-be-created job is a retry for an |
| 908 | old job, the afe_job_id of the old job will |
| 909 | be passed in as |retry_for|, which will be |
| 910 | recorded in the new job's keyvals. |
| 911 | @returns: A frontend.Job object with an added test_name member. |
| 912 | test_name is used to preserve the higher level TEST_NAME |
| 913 | name of the job. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 914 | """ |
Allen Li | 069fc25 | 2016-12-12 16:26:21 -0800 | [diff] [blame] | 915 | test_obj = self._afe.create_job( |
| 916 | control_file=test.text, |
Allen Li | 468d615 | 2016-12-12 16:35:01 -0800 | [diff] [blame] | 917 | name=tools.create_job_name( |
| 918 | self._test_source_build or self._cros_build, |
| 919 | self._tag, |
| 920 | test.name), |
Allen Li | 069fc25 | 2016-12-12 16:26:21 -0800 | [diff] [blame] | 921 | control_type=test.test_type.capitalize(), |
| 922 | meta_hosts=[self._board]*test.sync_count, |
Allen Li | c68ca4a | 2016-12-12 17:28:36 -0800 | [diff] [blame] | 923 | dependencies=self._create_job_deps(test), |
Allen Li | a4ae135 | 2016-12-12 16:26:57 -0800 | [diff] [blame] | 924 | keyvals=self._create_keyvals_for_test_job(test, retry_for), |
Allen Li | 069fc25 | 2016-12-12 16:26:21 -0800 | [diff] [blame] | 925 | max_runtime_mins=self._max_runtime_mins, |
| 926 | timeout_mins=self._timeout_mins, |
| 927 | parent_job_id=self._suite_job_id, |
| 928 | test_retry=test.retries, |
| 929 | priority=self._priority, |
| 930 | synch_count=test.sync_count, |
| 931 | require_ssp=test.require_ssp) |
| 932 | |
| 933 | test_obj.test_name = test.name |
| 934 | return test_obj |
| 935 | |
| 936 | |
Allen Li | c68ca4a | 2016-12-12 17:28:36 -0800 | [diff] [blame] | 937 | def _create_job_deps(self, test): |
| 938 | """Create job deps list for a test job. |
| 939 | |
| 940 | @returns: A list of dependency strings. |
| 941 | """ |
| 942 | if self._ignore_deps: |
| 943 | job_deps = [] |
| 944 | else: |
| 945 | job_deps = list(test.dependencies) |
| 946 | job_deps.extend(self._extra_deps) |
| 947 | if self._pool: |
| 948 | job_deps.append(self._pool) |
| 949 | job_deps.append(self._board) |
| 950 | return job_deps |
| 951 | |
| 952 | |
Allen Li | 069fc25 | 2016-12-12 16:26:21 -0800 | [diff] [blame] | 953 | def _create_keyvals_for_test_job(self, test, retry_for=None): |
| 954 | """Create keyvals dict for creating a test job. |
| 955 | |
| 956 | @param test: ControlData object for a test to run. |
| 957 | @param retry_for: If the to-be-created job is a retry for an |
| 958 | old job, the afe_job_id of the old job will |
| 959 | be passed in as |retry_for|, which will be |
| 960 | recorded in the new job's keyvals. |
| 961 | @returns: A keyvals dict for creating the test job. |
| 962 | """ |
Allen Li | 015e71b | 2016-12-12 16:37:25 -0800 | [diff] [blame] | 963 | keyvals = { |
| 964 | constants.JOB_BUILD_KEY: self._cros_build, |
| 965 | constants.JOB_SUITE_KEY: self._tag, |
| 966 | constants.JOB_EXPERIMENTAL_KEY: test.experimental, |
| 967 | constants.JOB_BUILDS_KEY: self._builds |
| 968 | } |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 969 | # test_source_build is saved to job_keyvals so scheduler can retrieve |
| 970 | # the build name from database when compiling autoserv commandline. |
| 971 | # This avoid a database change to add a new field in afe_jobs. |
Allen Li | 015e71b | 2016-12-12 16:37:25 -0800 | [diff] [blame] | 972 | # |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 973 | # Only add `test_source_build` to job keyvals if the build is different |
| 974 | # from the CrOS build or the job uses more than one build, e.g., both |
| 975 | # firmware and CrOS will be updated in the dut. |
| 976 | # This is for backwards compatibility, so the update Autotest code can |
| 977 | # compile an autoserv command line to run in a SSP container using |
| 978 | # previous builds. |
| 979 | if (self._test_source_build and |
Allen Li | 015e71b | 2016-12-12 16:37:25 -0800 | [diff] [blame] | 980 | (self._cros_build != self._test_source_build or |
| 981 | len(self._builds) > 1)): |
| 982 | keyvals[constants.JOB_TEST_SOURCE_BUILD_KEY] = \ |
| 983 | self._test_source_build |
Dan Shi | dac462f | 2015-08-14 11:07:32 -0700 | [diff] [blame] | 984 | for prefix, build in self._builds.iteritems(): |
| 985 | if prefix == provision.FW_RW_VERSION_PREFIX: |
| 986 | keyvals[constants.FWRW_BUILD]= build |
| 987 | elif prefix == provision.FW_RO_VERSION_PREFIX: |
| 988 | keyvals[constants.FWRO_BUILD] = build |
Allen Li | 015e71b | 2016-12-12 16:37:25 -0800 | [diff] [blame] | 989 | # Add suite job id to keyvals so tko parser can read it from keyval |
| 990 | # file. |
Dan Shi | dac462f | 2015-08-14 11:07:32 -0700 | [diff] [blame] | 991 | if self._suite_job_id: |
| 992 | keyvals[constants.PARENT_JOB_ID] = self._suite_job_id |
Allen Li | 015e71b | 2016-12-12 16:37:25 -0800 | [diff] [blame] | 993 | # We drop the old job's id in the new job's keyval file so that |
| 994 | # later our tko parser can figure out the retry relationship and |
| 995 | # invalidate the results of the old job in tko database. |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 996 | if retry_for: |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 997 | keyvals[constants.RETRY_ORIGINAL_JOB_ID] = retry_for |
Simran Basi | 1e10e92 | 2015-04-16 15:09:56 -0700 | [diff] [blame] | 998 | if self._offload_failures_only: |
| 999 | keyvals[constants.JOB_OFFLOAD_FAILURES_KEY] = True |
Allen Li | 069fc25 | 2016-12-12 16:26:21 -0800 | [diff] [blame] | 1000 | return keyvals |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 1001 | |
| 1002 | |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 1003 | def _schedule_test(self, record, test, retry_for=None, ignore_errors=False): |
| 1004 | """Schedule a single test and return the job. |
| 1005 | |
Allen Li | e79b3cb | 2016-12-12 18:24:17 -0800 | [diff] [blame] | 1006 | Schedule a single test by creating a job, and then update relevant |
| 1007 | data structures that are used to keep track of all running jobs. |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 1008 | |
Allen Li | e79b3cb | 2016-12-12 18:24:17 -0800 | [diff] [blame] | 1009 | Emits a TEST_NA status log entry if it failed to schedule the test due |
| 1010 | to NoEligibleHostException or a non-existent board label. |
| 1011 | |
| 1012 | Returns a frontend.Job object if the test is successfully scheduled. |
| 1013 | If scheduling failed due to NoEligibleHostException or a non-existent |
| 1014 | board label, returns None. If ignore_errors is True, all unknown |
| 1015 | errors return None, otherwise the errors are raised as-is. |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 1016 | |
| 1017 | @param record: A callable to use for logging. |
| 1018 | prototype: record(base_job.status_log_entry) |
| 1019 | @param test: ControlData for a test to run. |
| 1020 | @param retry_for: If we are scheduling a test to retry an |
| 1021 | old job, the afe_job_id of the old job |
| 1022 | will be passed in as |retry_for|. |
| 1023 | @param ignore_errors: If True, when an rpc error occur, ignore |
| 1024 | the error and will return None. |
| 1025 | If False, rpc errors will be raised. |
| 1026 | |
Allen Li | e79b3cb | 2016-12-12 18:24:17 -0800 | [diff] [blame] | 1027 | @returns: A frontend.Job object or None |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 1028 | """ |
| 1029 | msg = 'Scheduling %s' % test.name |
| 1030 | if retry_for: |
| 1031 | msg = msg + ', to retry afe job %d' % retry_for |
| 1032 | logging.debug(msg) |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 1033 | begin_time_str = datetime.datetime.now().strftime(time_utils.TIME_FMT) |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 1034 | try: |
| 1035 | job = self._create_job(test, retry_for=retry_for) |
Allen Li | 6fd440f | 2016-12-12 18:40:05 -0800 | [diff] [blame] | 1036 | except (error.NoEligibleHostException, proxy.ValidationError) as e: |
| 1037 | if (isinstance(e, error.NoEligibleHostException) |
| 1038 | or (isinstance(e, proxy.ValidationError) |
| 1039 | and _is_nonexistent_board_error(e))): |
| 1040 | # Treat a dependency on a non-existent board label the same as |
| 1041 | # a dependency on a board that exists, but for which there's no |
| 1042 | # hardware. |
| 1043 | logging.debug('%s not applicable for this board/pool. ' |
| 1044 | 'Emitting TEST_NA.', test.name) |
| 1045 | Status('TEST_NA', test.name, |
| 1046 | 'Skipping: test not supported on this board/pool.', |
Allen Li | 9fcd4b4 | 2016-12-12 16:15:14 -0800 | [diff] [blame] | 1047 | begin_time_str=begin_time_str).record_all(record) |
| 1048 | return None |
| 1049 | else: |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 1050 | raise e |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 1051 | except (error.RPCException, proxy.JSONRPCException) as e: |
| 1052 | if retry_for: |
| 1053 | # Mark that we've attempted to retry the old job. |
| 1054 | self._retry_handler.set_attempted(job_id=retry_for) |
Allen Li | 0ba5934 | 2016-12-12 15:57:02 -0800 | [diff] [blame] | 1055 | |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 1056 | if ignore_errors: |
| 1057 | logging.error('Failed to schedule test: %s, Reason: %s', |
| 1058 | test.name, e) |
Allen Li | 0ba5934 | 2016-12-12 15:57:02 -0800 | [diff] [blame] | 1059 | return None |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 1060 | else: |
| 1061 | raise e |
| 1062 | else: |
| 1063 | self._jobs.append(job) |
| 1064 | self._jobs_to_tests[job.id] = test |
| 1065 | if retry_for: |
| 1066 | # A retry job was just created, record it. |
| 1067 | self._retry_handler.add_retry( |
| 1068 | old_job_id=retry_for, new_job_id=job.id) |
| 1069 | retry_count = (test.job_retries - |
| 1070 | self._retry_handler.get_retry_max(job.id)) |
| 1071 | logging.debug('Job %d created to retry job %d. ' |
| 1072 | 'Have retried for %d time(s)', |
| 1073 | job.id, retry_for, retry_count) |
Allen Li | 4df053e | 2016-12-29 16:05:41 -0800 | [diff] [blame] | 1074 | self._remember_job_keyval(job) |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 1075 | return job |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 1076 | |
| 1077 | |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 1078 | def schedule(self, record, add_experimental=True): |
Aviv Keshet | 1830892 | 2013-02-19 17:49:49 -0800 | [diff] [blame] | 1079 | #pylint: disable-msg=C0111 |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 1080 | """ |
| 1081 | Schedule jobs using |self._afe|. |
| 1082 | |
| 1083 | frontend.Job objects representing each scheduled job will be put in |
| 1084 | |self._jobs|. |
| 1085 | |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 1086 | @param record: A callable to use for logging. |
| 1087 | prototype: record(base_job.status_log_entry) |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 1088 | @param add_experimental: schedule experimental tests as well, or not. |
Aviv Keshet | e9170d9 | 2013-07-19 11:20:45 -0700 | [diff] [blame] | 1089 | @returns: The number of tests that were scheduled. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 1090 | """ |
Allen Li | f4cb5ec | 2017-01-03 16:58:12 -0800 | [diff] [blame] | 1091 | scheduled_test_names = [] |
Allen Li | 86f8c28 | 2017-02-28 13:09:40 -0800 | [diff] [blame] | 1092 | test_filter = _ExperimentalTestFilter( |
Allen Li | f4cb5ec | 2017-01-03 16:58:12 -0800 | [diff] [blame] | 1093 | tests=self.tests, |
| 1094 | add_experimental=add_experimental) |
| 1095 | logging.debug('Discovered %d stable tests.', |
Allen Li | 86f8c28 | 2017-02-28 13:09:40 -0800 | [diff] [blame] | 1096 | len(test_filter.stable_tests)) |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 1097 | logging.debug('Discovered %d unstable tests.', |
Allen Li | 86f8c28 | 2017-02-28 13:09:40 -0800 | [diff] [blame] | 1098 | len(test_filter.unstable_tests)) |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 1099 | |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 1100 | Status('INFO', 'Start %s' % self._tag).record_result(record) |
| 1101 | try: |
Shuqian Zhao | da1118d | 2017-02-13 16:22:58 -0800 | [diff] [blame] | 1102 | # Write job_keyvals into keyval file. |
| 1103 | if self._job_keyvals: |
| 1104 | utils.write_keyval(self._results_dir, self._job_keyvals) |
| 1105 | |
Allen Li | 86f8c28 | 2017-02-28 13:09:40 -0800 | [diff] [blame] | 1106 | for test in test_filter.get_tests_to_schedule(): |
Allen Li | da90573 | 2016-12-12 15:49:16 -0800 | [diff] [blame] | 1107 | scheduled_job = self._schedule_test(record, test) |
| 1108 | if scheduled_job is not None: |
Shuqian Zhao | cd866f3 | 2016-11-29 20:14:34 -0800 | [diff] [blame] | 1109 | scheduled_test_names.append(test.name) |
| 1110 | |
| 1111 | # Write the num of scheduled tests and name of them to keyval file. |
Shuqian Zhao | cd866f3 | 2016-11-29 20:14:34 -0800 | [diff] [blame] | 1112 | logging.debug('Scheduled %d tests, writing the total to keyval.', |
Allen Li | a4d3502 | 2016-12-12 15:42:10 -0800 | [diff] [blame] | 1113 | len(scheduled_test_names)) |
Allen Li | d4d5dda | 2016-12-12 15:39:11 -0800 | [diff] [blame] | 1114 | utils.write_keyval( |
| 1115 | self._results_dir, |
Allen Li | dda59b8 | 2016-12-12 18:20:04 -0800 | [diff] [blame] | 1116 | self._make_scheduled_tests_keyvals(scheduled_test_names)) |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 1117 | except Exception: # pylint: disable=W0703 |
Allen Li | b892d9f | 2016-12-29 15:50:11 -0800 | [diff] [blame] | 1118 | logging.exception('Exception while scheduling suite') |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 1119 | Status('FAIL', self._tag, |
| 1120 | 'Exception while scheduling suite').record_result(record) |
| 1121 | |
Fang Deng | 7e655a9 | 2014-05-23 13:48:11 -0700 | [diff] [blame] | 1122 | if self._job_retry: |
| 1123 | self._retry_handler = RetryHandler( |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 1124 | initial_jobs_to_tests=self._jobs_to_tests, |
| 1125 | max_retries=self._max_retries) |
Allen Li | a4d3502 | 2016-12-12 15:42:10 -0800 | [diff] [blame] | 1126 | return len(scheduled_test_names) |
Aviv Keshet | e9170d9 | 2013-07-19 11:20:45 -0700 | [diff] [blame] | 1127 | |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 1128 | |
Allen Li | dda59b8 | 2016-12-12 18:20:04 -0800 | [diff] [blame] | 1129 | def _make_scheduled_tests_keyvals(self, scheduled_test_names): |
| 1130 | """Make a keyvals dict to write for scheduled test names. |
| 1131 | |
| 1132 | @param scheduled_test_names: A list of scheduled test name strings. |
| 1133 | |
| 1134 | @returns: A keyvals dict. |
| 1135 | """ |
| 1136 | return { |
| 1137 | constants.SCHEDULED_TEST_COUNT_KEY: len(scheduled_test_names), |
| 1138 | constants.SCHEDULED_TEST_NAMES_KEY: repr(scheduled_test_names), |
| 1139 | } |
| 1140 | |
| 1141 | |
Allen Li | d1cbccf | 2016-12-29 15:12:39 -0800 | [diff] [blame] | 1142 | def _should_report(self, result): |
beeps | da5b711 | 2013-05-30 11:34:14 -0700 | [diff] [blame] | 1143 | """ |
Shuqian Zhao | e33ba4a | 2015-09-11 18:51:43 -0700 | [diff] [blame] | 1144 | Returns True if this failure requires to be reported. |
beeps | da5b711 | 2013-05-30 11:34:14 -0700 | [diff] [blame] | 1145 | |
| 1146 | @param result: A result, encapsulating the status of the failed job. |
Shuqian Zhao | e33ba4a | 2015-09-11 18:51:43 -0700 | [diff] [blame] | 1147 | @return: True if we should report this failure. |
beeps | da5b711 | 2013-05-30 11:34:14 -0700 | [diff] [blame] | 1148 | """ |
Allen Li | cc75229 | 2017-01-03 12:44:39 -0800 | [diff] [blame] | 1149 | if self._has_retry(result): |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 1150 | return False |
| 1151 | |
beeps | beefc06 | 2013-08-02 11:17:09 -0700 | [diff] [blame] | 1152 | is_not_experimental = ( |
| 1153 | constants.EXPERIMENTAL_PREFIX not in result._test_name and |
| 1154 | constants.EXPERIMENTAL_PREFIX not in result._job_name) |
| 1155 | |
Alex Miller | fcc119b | 2014-01-15 13:54:58 -0800 | [diff] [blame] | 1156 | return (self._file_bugs and result.test_executed and |
beeps | beefc06 | 2013-08-02 11:17:09 -0700 | [diff] [blame] | 1157 | (is_not_experimental or self._file_experimental_bugs) and |
Fang Deng | d82c1c7 | 2014-07-29 10:43:01 -0700 | [diff] [blame] | 1158 | not result.is_testna() and |
beeps | 32fa677 | 2014-01-28 13:19:53 -0800 | [diff] [blame] | 1159 | result.is_worse_than(job_status.Status('GOOD', '', 'reason'))) |
beeps | da5b711 | 2013-05-30 11:34:14 -0700 | [diff] [blame] | 1160 | |
| 1161 | |
Allen Li | cc75229 | 2017-01-03 12:44:39 -0800 | [diff] [blame] | 1162 | def _has_retry(self, result): |
| 1163 | """ |
| 1164 | Return True if this result gets to retry. |
| 1165 | |
| 1166 | @param result: A result, encapsulating the status of the failed job. |
| 1167 | @return: bool |
| 1168 | """ |
| 1169 | return (self._job_retry |
| 1170 | and self._retry_handler.has_following_retry(result)) |
| 1171 | |
| 1172 | |
Allen Li | 1850345 | 2016-12-29 14:56:48 -0800 | [diff] [blame] | 1173 | def wait(self, record, bug_template=None): |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 1174 | """ |
| 1175 | Polls for the job statuses, using |record| to print status when each |
| 1176 | completes. |
| 1177 | |
| 1178 | @param record: callable that records job status. |
| 1179 | prototype: |
| 1180 | record(base_job.status_log_entry) |
beeps | c8a875b | 2013-03-25 10:20:38 -0700 | [diff] [blame] | 1181 | @param bug_template: A template dictionary specifying the default bug |
| 1182 | filing options for failures in this suite. |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 1183 | """ |
Dan Shi | e67bd6a | 2016-02-17 14:44:07 -0800 | [diff] [blame] | 1184 | # reporting modules have dependency on external packages, e.g., httplib2 |
| 1185 | # Such dependency can cause issue to any module tries to import suite.py |
| 1186 | # without building site-packages first. Since the reporting modules are |
| 1187 | # only used in this function, move the imports here avoid the |
| 1188 | # requirement of building site packages to use other functions in this |
| 1189 | # module. |
| 1190 | from autotest_lib.server.cros.dynamic_suite import reporting |
Dan Shi | e67bd6a | 2016-02-17 14:44:07 -0800 | [diff] [blame] | 1191 | |
Allen Li | 1850345 | 2016-12-29 14:56:48 -0800 | [diff] [blame] | 1192 | if bug_template is None: |
| 1193 | bug_template = {} |
| 1194 | |
Alex Miller | a3a4fe7 | 2013-01-22 09:57:47 -0800 | [diff] [blame] | 1195 | if self._file_bugs: |
| 1196 | bug_reporter = reporting.Reporter() |
Allen Li | 733dab9 | 2016-12-29 15:07:50 -0800 | [diff] [blame] | 1197 | else: |
| 1198 | bug_reporter = reporting.NullReporter() |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 1199 | try: |
Aviv Keshet | 133beb1 | 2013-08-20 14:37:13 -0700 | [diff] [blame] | 1200 | if self._suite_job_id: |
| 1201 | results_generator = job_status.wait_for_child_results( |
| 1202 | self._afe, self._tko, self._suite_job_id) |
| 1203 | else: |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 1204 | logging.warning('Unknown suite_job_id, falling back to less ' |
Dan Shi | 08ff128 | 2016-02-18 19:51:16 -0800 | [diff] [blame] | 1205 | 'efficient results_generator.') |
Aviv Keshet | 133beb1 | 2013-08-20 14:37:13 -0700 | [diff] [blame] | 1206 | results_generator = job_status.wait_for_results(self._afe, |
| 1207 | self._tko, |
| 1208 | self._jobs) |
| 1209 | for result in results_generator: |
Allen Li | 26b340d | 2016-12-29 15:23:01 -0800 | [diff] [blame] | 1210 | self._record_result( |
| 1211 | result=result, |
| 1212 | record=record, |
| 1213 | results_generator=results_generator, |
| 1214 | bug_reporter=bug_reporter, |
| 1215 | bug_template=bug_template) |
beeps | 8ead53c | 2013-04-26 19:12:46 -0700 | [diff] [blame] | 1216 | |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 1217 | except Exception: # pylint: disable=W0703 |
Allen Li | b892d9f | 2016-12-29 15:50:11 -0800 | [diff] [blame] | 1218 | logging.exception('Exception waiting for results') |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 1219 | Status('FAIL', self._tag, |
| 1220 | 'Exception waiting for results').record_result(record) |
| 1221 | |
| 1222 | |
Allen Li | 26b340d | 2016-12-29 15:23:01 -0800 | [diff] [blame] | 1223 | def _record_result(self, result, record, results_generator, bug_reporter, |
| 1224 | bug_template): |
| 1225 | """ |
| 1226 | Record a single 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 results_generator: Results generator for sending job retries. |
| 1233 | @param bug_reporter: Reporter instance for reporting bugs. |
| 1234 | @param bug_template: A template dictionary specifying the default bug |
| 1235 | filing options for failures in this suite. |
| 1236 | """ |
Allen Li | 26b340d | 2016-12-29 15:23:01 -0800 | [diff] [blame] | 1237 | result.record_all(record) |
Allen Li | 4df053e | 2016-12-29 16:05:41 -0800 | [diff] [blame] | 1238 | self._remember_job_keyval(result) |
Allen Li | 26b340d | 2016-12-29 15:23:01 -0800 | [diff] [blame] | 1239 | |
Allen Li | cc75229 | 2017-01-03 12:44:39 -0800 | [diff] [blame] | 1240 | if self._has_retry(result): |
Allen Li | 26b340d | 2016-12-29 15:23:01 -0800 | [diff] [blame] | 1241 | new_job = self._schedule_test( |
| 1242 | record=record, test=self._jobs_to_tests[result.id], |
| 1243 | retry_for=result.id, ignore_errors=True) |
| 1244 | if new_job: |
| 1245 | results_generator.send([new_job]) |
| 1246 | |
| 1247 | # TODO (fdeng): If the suite times out before a retry could |
| 1248 | # finish, we would lose the chance to file a bug for the |
| 1249 | # original job. |
| 1250 | if self._should_report(result): |
Allen Li | 1130898 | 2016-12-29 16:19:55 -0800 | [diff] [blame] | 1251 | if self._should_file_bugs: |
Allen Li | 47c9fca | 2016-12-29 16:22:53 -0800 | [diff] [blame] | 1252 | self._file_bug(result, bug_reporter, bug_template) |
Allen Li | 26b340d | 2016-12-29 15:23:01 -0800 | [diff] [blame] | 1253 | else: |
Allen Li | d5df44b | 2016-12-29 15:59:06 -0800 | [diff] [blame] | 1254 | # reporting modules have dependency on external |
| 1255 | # packages, e.g., httplib2 Such dependency can cause |
| 1256 | # issue to any module tries to import suite.py without |
| 1257 | # building site-packages first. Since the reporting |
| 1258 | # modules are only used in this function, move the |
| 1259 | # imports here avoid the requirement of building site |
| 1260 | # packages to use other functions in this module. |
| 1261 | from autotest_lib.server.cros.dynamic_suite import reporting |
| 1262 | |
Allen Li | 7b97311 | 2016-12-29 16:17:41 -0800 | [diff] [blame] | 1263 | reporting.send_email( |
| 1264 | self._get_test_bug(result), |
| 1265 | self._get_bug_template(result, bug_template)) |
Allen Li | 26b340d | 2016-12-29 15:23:01 -0800 | [diff] [blame] | 1266 | |
| 1267 | |
Allen Li | d5df44b | 2016-12-29 15:59:06 -0800 | [diff] [blame] | 1268 | def _get_bug_template(self, result, bug_template): |
| 1269 | """Get BugTemplate for test job. |
| 1270 | |
| 1271 | @param result: Status instance for job. |
| 1272 | @param bug_template: A template dictionary specifying the default bug |
| 1273 | filing options for failures in this suite. |
| 1274 | @returns: BugTemplate instance |
| 1275 | """ |
| 1276 | # reporting modules have dependency on external packages, e.g., httplib2 |
| 1277 | # Such dependency can cause issue to any module tries to import suite.py |
| 1278 | # without building site-packages first. Since the reporting modules are |
| 1279 | # only used in this function, move the imports here avoid the |
| 1280 | # requirement of building site packages to use other functions in this |
| 1281 | # module. |
| 1282 | from autotest_lib.server.cros.dynamic_suite import reporting_utils |
| 1283 | |
| 1284 | # Try to merge with bug template in test control file. |
| 1285 | template = reporting_utils.BugTemplate(bug_template) |
| 1286 | try: |
| 1287 | test_data = self._jobs_to_tests[result.id] |
| 1288 | return template.finalize_bug_template( |
| 1289 | test_data.bug_template) |
| 1290 | except AttributeError: |
| 1291 | # Test control file does not have bug template defined. |
| 1292 | return template.bug_template |
| 1293 | except reporting_utils.InvalidBugTemplateException as e: |
| 1294 | logging.error('Merging bug templates failed with ' |
| 1295 | 'error: %s An empty bug template will ' |
| 1296 | 'be used.', e) |
| 1297 | return {} |
| 1298 | |
| 1299 | |
Allen Li | 003913e | 2016-12-29 15:53:34 -0800 | [diff] [blame] | 1300 | def _get_test_bug(self, result): |
| 1301 | """Get TestBug for the given result. |
| 1302 | |
| 1303 | @param result: Status instance for a test job. |
| 1304 | @returns: TestBug instance. |
| 1305 | """ |
| 1306 | # reporting modules have dependency on external packages, e.g., httplib2 |
| 1307 | # Such dependency can cause issue to any module tries to import suite.py |
| 1308 | # without building site-packages first. Since the reporting modules are |
| 1309 | # only used in this function, move the imports here avoid the |
| 1310 | # requirement of building site packages to use other functions in this |
| 1311 | # module. |
| 1312 | from autotest_lib.server.cros.dynamic_suite import reporting |
| 1313 | |
| 1314 | job_views = self._tko.run('get_detailed_test_views', |
| 1315 | afe_job_id=result.id) |
| 1316 | return reporting.TestBug(self._cros_build, |
| 1317 | site_utils.get_chrome_version(job_views), |
| 1318 | self._tag, |
| 1319 | result) |
| 1320 | |
| 1321 | |
Allen Li | 1130898 | 2016-12-29 16:19:55 -0800 | [diff] [blame] | 1322 | @property |
| 1323 | def _should_file_bugs(self): |
| 1324 | """Return whether bugs should be filed. |
| 1325 | |
| 1326 | @returns: bool |
| 1327 | """ |
| 1328 | # File bug when failure is one of the _FILE_BUG_SUITES, |
| 1329 | # otherwise send an email to the owner anc cc. |
| 1330 | return self._tag in _FILE_BUG_SUITES |
| 1331 | |
| 1332 | |
Allen Li | 47c9fca | 2016-12-29 16:22:53 -0800 | [diff] [blame] | 1333 | def _file_bug(self, result, bug_reporter, bug_template): |
| 1334 | """File a bug for a test job result. |
| 1335 | |
| 1336 | @param result: Status instance for job. |
| 1337 | @param bug_reporter: Reporter instance for reporting bugs. |
| 1338 | @param bug_template: A template dictionary specifying the default bug |
| 1339 | filing options for failures in this suite. |
| 1340 | """ |
| 1341 | bug_id, bug_count = bug_reporter.report( |
| 1342 | self._get_test_bug(result), |
| 1343 | self._get_bug_template(result, bug_template)) |
| 1344 | |
| 1345 | # We use keyvals to communicate bugs filed with run_suite. |
| 1346 | if bug_id is not None: |
| 1347 | bug_keyvals = tools.create_bug_keyvals( |
| 1348 | result.id, result.test_name, |
| 1349 | (bug_id, bug_count)) |
| 1350 | try: |
| 1351 | utils.write_keyval(self._results_dir, |
| 1352 | bug_keyvals) |
| 1353 | except ValueError: |
| 1354 | logging.error('Unable to log bug keyval for:%s', |
| 1355 | result.test_name) |
| 1356 | |
| 1357 | |
Alex Miller | 3a69adc | 2012-12-19 13:38:31 -0800 | [diff] [blame] | 1358 | def abort(self): |
| 1359 | """ |
| 1360 | Abort all scheduled test jobs. |
| 1361 | """ |
| 1362 | if self._jobs: |
| 1363 | job_ids = [job.id for job in self._jobs] |
| 1364 | self._afe.run('abort_host_queue_entries', job__id__in=job_ids) |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 1365 | |
| 1366 | |
Allen Li | 4df053e | 2016-12-29 16:05:41 -0800 | [diff] [blame] | 1367 | def _remember_job_keyval(self, job): |
Chris Masone | d9f13c5 | 2012-08-29 10:37:08 -0700 | [diff] [blame] | 1368 | """ |
| 1369 | Record provided job as a suite job keyval, for later referencing. |
| 1370 | |
Allen Li | 4df053e | 2016-12-29 16:05:41 -0800 | [diff] [blame] | 1371 | @param job: some representation of a job that has the attributes: |
| 1372 | id, test_name, and owner |
Chris Masone | d9f13c5 | 2012-08-29 10:37:08 -0700 | [diff] [blame] | 1373 | """ |
Allen Li | 3cc73cd | 2016-12-12 16:02:21 -0800 | [diff] [blame] | 1374 | if self._results_dir and job.id and job.owner and job.test_name: |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 1375 | job_id_owner = '%s-%s' % (job.id, job.owner) |
Chris Masone | d9f13c5 | 2012-08-29 10:37:08 -0700 | [diff] [blame] | 1376 | logging.debug('Adding job keyval for %s=%s', |
Chris Sosa | accb5ce | 2012-08-30 17:29:15 -0700 | [diff] [blame] | 1377 | job.test_name, job_id_owner) |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 1378 | utils.write_keyval( |
| 1379 | self._results_dir, |
| 1380 | {hashlib.md5(job.test_name).hexdigest(): job_id_owner}) |
| 1381 | |
Dan Shi | d152180 | 2013-05-24 13:08:37 -0700 | [diff] [blame] | 1382 | |
Allen Li | e61acfe | 2016-12-29 16:27:21 -0800 | [diff] [blame] | 1383 | @classmethod |
| 1384 | def find_and_parse_tests(cls, cf_getter, predicate, suite_name='', |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 1385 | add_experimental=False, forgiving_parser=True, |
Shuqian Zhao | ed0da86 | 2017-03-06 14:47:13 -0800 | [diff] [blame] | 1386 | run_prod_code=False, test_args=None): |
Dan Shi | 5783f8a | 2014-12-22 14:34:45 -0800 | [diff] [blame] | 1387 | """ |
| 1388 | Function to scan through all tests and find eligible tests. |
| 1389 | |
| 1390 | Search through all tests based on given cf_getter, suite_name, |
| 1391 | add_experimental and forgiving_parser, return the tests that match |
| 1392 | given predicate. |
| 1393 | |
| 1394 | @param cf_getter: a control_file_getter.ControlFileGetter used to list |
| 1395 | and fetch the content of control files |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 1396 | @param predicate: a function that should return True when run over a |
| 1397 | ControlData representation of a control file that should be in |
| 1398 | this Suite. |
beeps | c594c1c | 2013-07-09 22:33:18 -0700 | [diff] [blame] | 1399 | @param suite_name: If specified, this method will attempt to restrain |
| 1400 | the search space to just this suite's control files. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 1401 | @param add_experimental: add tests with experimental attribute set. |
beeps | 89f1e06 | 2013-09-18 12:00:17 -0700 | [diff] [blame] | 1402 | @param forgiving_parser: If False, will raise ControlVariableExceptions |
| 1403 | if any are encountered when parsing control |
| 1404 | files. Note that this can raise an exception |
| 1405 | for syntax errors in unrelated files, because |
| 1406 | we parse them before applying the predicate. |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 1407 | @param run_prod_code: If true, the suite will run the test code that |
| 1408 | lives in prod aka the test code currently on the |
| 1409 | lab servers by disabling SSP for the discovered |
| 1410 | tests. |
Shuqian Zhao | ed0da86 | 2017-03-06 14:47:13 -0800 | [diff] [blame] | 1411 | @param test_args: A dict of args to be seeded in test control file. |
beeps | 89f1e06 | 2013-09-18 12:00:17 -0700 | [diff] [blame] | 1412 | |
| 1413 | @raises ControlVariableException: If forgiving_parser is False and there |
| 1414 | is a syntax error in a control file. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 1415 | |
| 1416 | @return list of ControlData objects that should be run, with control |
Dan Shi | ef5b53f | 2013-01-22 10:22:01 -0800 | [diff] [blame] | 1417 | file text added in |text| attribute. Results are sorted based |
| 1418 | on the TIME setting in control file, slowest test comes first. |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 1419 | """ |
Allen Li | 9ea208e | 2017-02-28 13:43:11 -0800 | [diff] [blame] | 1420 | tests = _find_test_control_data_for_suite( |
Allen Li | 2015903 | 2017-02-28 13:22:51 -0800 | [diff] [blame] | 1421 | cf_getter, suite_name, add_experimental, forgiving_parser, |
| 1422 | run_prod_code=run_prod_code, |
| 1423 | test_args=test_args) |
Dan Shi | 5783f8a | 2014-12-22 14:34:45 -0800 | [diff] [blame] | 1424 | logging.debug('Parsed %s control files.', len(tests)) |
Dan Shi | ef5b53f | 2013-01-22 10:22:01 -0800 | [diff] [blame] | 1425 | tests = [test for test in tests.itervalues() if predicate(test)] |
| 1426 | tests.sort(key=lambda t: |
| 1427 | control_data.ControlData.get_test_time_index(t.time), |
| 1428 | reverse=True) |
| 1429 | return tests |
Dan Shi | 5783f8a | 2014-12-22 14:34:45 -0800 | [diff] [blame] | 1430 | |
| 1431 | |
Allen Li | e61acfe | 2016-12-29 16:27:21 -0800 | [diff] [blame] | 1432 | @classmethod |
| 1433 | def find_possible_tests(cls, cf_getter, predicate, suite_name='', count=10): |
Dan Shi | 5783f8a | 2014-12-22 14:34:45 -0800 | [diff] [blame] | 1434 | """ |
| 1435 | Function to scan through all tests and find possible tests. |
| 1436 | |
| 1437 | Search through all tests based on given cf_getter, suite_name, |
| 1438 | add_experimental and forgiving_parser. Use the given predicate to |
| 1439 | calculate the similarity and return the top 10 matches. |
| 1440 | |
| 1441 | @param cf_getter: a control_file_getter.ControlFileGetter used to list |
| 1442 | and fetch the content of control files |
| 1443 | @param predicate: a function that should return a tuple of (name, ratio) |
| 1444 | when run over a ControlData representation of a control file that |
| 1445 | should be in this Suite. `name` is the key to be compared, e.g., |
| 1446 | a suite name or test name. `ratio` is a value between [0,1] |
| 1447 | indicating the similarity of `name` and the value to be compared. |
| 1448 | @param suite_name: If specified, this method will attempt to restrain |
| 1449 | the search space to just this suite's control files. |
| 1450 | @param count: Number of suggestions to return, default to 10. |
| 1451 | |
| 1452 | @return list of top names that similar to the given test, sorted by |
| 1453 | match ratio. |
| 1454 | """ |
Allen Li | 9ea208e | 2017-02-28 13:43:11 -0800 | [diff] [blame] | 1455 | tests = _find_test_control_data_for_suite( |
Allen Li | 066f587 | 2017-02-28 13:30:44 -0800 | [diff] [blame] | 1456 | cf_getter, suite_name, |
| 1457 | add_experimental=True, forgiving_parser=True) |
Dan Shi | 5783f8a | 2014-12-22 14:34:45 -0800 | [diff] [blame] | 1458 | logging.debug('Parsed %s control files.', len(tests)) |
| 1459 | similarities = {} |
| 1460 | for test in tests.itervalues(): |
| 1461 | ratios = predicate(test) |
| 1462 | # Some predicates may return a list of tuples, e.g., |
| 1463 | # name_in_tag_similarity_predicate. Convert all returns to a list. |
| 1464 | if not isinstance(ratios, list): |
| 1465 | ratios = [ratios] |
| 1466 | for name, ratio in ratios: |
| 1467 | similarities[name] = ratio |
| 1468 | return [s[0] for s in |
| 1469 | sorted(similarities.items(), key=operator.itemgetter(1), |
| 1470 | reverse=True)][:count] |
Allen Li | 9fcd4b4 | 2016-12-12 16:15:14 -0800 | [diff] [blame] | 1471 | |
| 1472 | |
| 1473 | def _is_nonexistent_board_error(e): |
| 1474 | """Return True if error is caused by nonexistent board label. |
| 1475 | |
| 1476 | As of this writing, the particular case we want looks like this: |
| 1477 | |
| 1478 | 1) e.problem_keys is a dictionary |
| 1479 | 2) e.problem_keys['meta_hosts'] exists as the only key |
| 1480 | in the dictionary. |
| 1481 | 3) e.problem_keys['meta_hosts'] matches this pattern: |
| 1482 | "Label "board:.*" not found" |
| 1483 | |
| 1484 | We check for conditions 1) and 2) on the |
| 1485 | theory that they're relatively immutable. |
| 1486 | We don't check condition 3) because it seems |
| 1487 | likely to be a maintenance burden, and for the |
| 1488 | times when we're wrong, being right shouldn't |
| 1489 | matter enough (we _hope_). |
| 1490 | |
| 1491 | @param e: proxy.ValidationError instance |
| 1492 | @returns: boolean |
| 1493 | """ |
| 1494 | return (isinstance(e.problem_keys, dict) |
| 1495 | and len(e.problem_keys) == 1 |
| 1496 | and 'meta_hosts' in e.problem_keys) |