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