Chris Masone | 8ac6671 | 2012-02-15 14:21:02 -0800 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import common |
| 6 | import compiler, logging, os, random, re, time |
Chris Masone | ab3e733 | 2012-02-29 18:54:58 -0800 | [diff] [blame] | 7 | from autotest_lib.client.common_lib import base_job, control_data, global_config |
| 8 | from autotest_lib.client.common_lib import error, utils |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 9 | from autotest_lib.client.common_lib.cros import dev_server |
Chris Masone | 8ac6671 | 2012-02-15 14:21:02 -0800 | [diff] [blame] | 10 | from autotest_lib.server.cros import control_file_getter, frontend_wrappers |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 11 | from autotest_lib.server import frontend |
| 12 | |
| 13 | |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 14 | VERSION_PREFIX = 'cros-version:' |
Chris Masone | 2ef1d4e | 2011-12-20 11:06:53 -0800 | [diff] [blame] | 15 | CONFIG = global_config.global_config |
| 16 | |
| 17 | |
Chris Sosa | 6b288c8 | 2012-03-29 15:31:06 -0700 | [diff] [blame] | 18 | class AsynchronousBuildFailure(Exception): |
| 19 | """Raised when the dev server throws 500 while finishing staging of a build. |
| 20 | """ |
| 21 | pass |
| 22 | |
| 23 | |
Chris Masone | ab3e733 | 2012-02-29 18:54:58 -0800 | [diff] [blame] | 24 | class SuiteArgumentException(Exception): |
| 25 | """Raised when improper arguments are used to run a suite.""" |
| 26 | pass |
| 27 | |
| 28 | |
Chris Masone | 5374c67 | 2012-03-05 15:11:39 -0800 | [diff] [blame] | 29 | class InadequateHostsException(Exception): |
| 30 | """Raised when there are too few hosts to run a suite.""" |
| 31 | pass |
| 32 | |
| 33 | |
Chris Masone | 502b71e | 2012-04-10 10:41:35 -0700 | [diff] [blame^] | 34 | class NoHostsException(Exception): |
| 35 | """Raised when there are no healthy hosts to run a suite.""" |
| 36 | pass |
| 37 | |
| 38 | |
Chris Masone | ab3e733 | 2012-02-29 18:54:58 -0800 | [diff] [blame] | 39 | def reimage_and_run(**dargs): |
| 40 | """ |
| 41 | Backward-compatible API for dynamic_suite. |
| 42 | |
| 43 | Will re-image a number of devices (of the specified board) with the |
| 44 | provided build, and then run the indicated test suite on them. |
| 45 | Guaranteed to be compatible with any build from stable to dev. |
| 46 | |
| 47 | Currently required args: |
| 48 | @param build: the build to install e.g. |
| 49 | x86-alex-release/R18-1655.0.0-a1-b1584. |
| 50 | @param board: which kind of devices to reimage. |
| 51 | @param name: a value of the SUITE control file variable to search for. |
| 52 | @param job: an instance of client.common_lib.base_job representing the |
| 53 | currently running suite job. |
| 54 | |
| 55 | Currently supported optional args: |
| 56 | @param pool: specify the pool of machines to use for scheduling purposes. |
| 57 | Default: None |
| 58 | @param num: how many devices to reimage. |
| 59 | Default in global_config |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 60 | @param check_hosts: require appropriate hosts to be available now. |
Chris Masone | ab3e733 | 2012-02-29 18:54:58 -0800 | [diff] [blame] | 61 | @param skip_reimage: skip reimaging, used for testing purposes. |
| 62 | Default: False |
| 63 | @param add_experimental: schedule experimental tests as well, or not. |
| 64 | Default: True |
Chris Sosa | 6b288c8 | 2012-03-29 15:31:06 -0700 | [diff] [blame] | 65 | @raises AsynchronousBuildFailure: if there was an issue finishing staging |
| 66 | from the devserver. |
Chris Masone | ab3e733 | 2012-02-29 18:54:58 -0800 | [diff] [blame] | 67 | """ |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 68 | (build, board, name, job, pool, num, check_hosts, skip_reimage, |
| 69 | add_experimental) = _vet_reimage_and_run_args(**dargs) |
Chris Masone | 5374c67 | 2012-03-05 15:11:39 -0800 | [diff] [blame] | 70 | board = 'board:%s' % board |
| 71 | if pool: |
| 72 | pool = 'pool:%s' % pool |
Chris Masone | 9f13ff2 | 2012-03-05 13:45:25 -0800 | [diff] [blame] | 73 | reimager = Reimager(job.autodir, pool=pool, results_dir=job.resultdir) |
Chris Masone | d368cc4 | 2012-03-07 15:16:59 -0800 | [diff] [blame] | 74 | |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 75 | if skip_reimage or reimager.attempt(build, board, job.record, check_hosts, |
| 76 | num=num): |
Chris Sosa | 6b288c8 | 2012-03-29 15:31:06 -0700 | [diff] [blame] | 77 | |
| 78 | # Ensure that the image's artifacts have completed downloading. |
| 79 | ds = dev_server.DevServer.create() |
| 80 | if not ds.finish_download(build): |
| 81 | raise AsynchronousBuildFailure( |
| 82 | "Server error completing staging for " + build) |
| 83 | |
Chris Masone | ab3e733 | 2012-02-29 18:54:58 -0800 | [diff] [blame] | 84 | suite = Suite.create_from_name(name, build, pool=pool, |
| 85 | results_dir=job.resultdir) |
| 86 | suite.run_and_wait(job.record, add_experimental=add_experimental) |
| 87 | |
Chris Masone | d368cc4 | 2012-03-07 15:16:59 -0800 | [diff] [blame] | 88 | reimager.clear_reimaged_host_state(build) |
| 89 | |
Chris Masone | ab3e733 | 2012-02-29 18:54:58 -0800 | [diff] [blame] | 90 | |
| 91 | def _vet_reimage_and_run_args(build=None, board=None, name=None, job=None, |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 92 | pool=None, num=None, check_hosts=True, |
| 93 | skip_reimage=False, add_experimental=True, |
| 94 | **dargs): |
Chris Masone | ab3e733 | 2012-02-29 18:54:58 -0800 | [diff] [blame] | 95 | """ |
| 96 | Vets arguments for reimage_and_run(). |
| 97 | |
| 98 | Currently required args: |
| 99 | @param build: the build to install e.g. |
| 100 | x86-alex-release/R18-1655.0.0-a1-b1584. |
| 101 | @param board: which kind of devices to reimage. |
| 102 | @param name: a value of the SUITE control file variable to search for. |
| 103 | @param job: an instance of client.common_lib.base_job representing the |
| 104 | currently running suite job. |
| 105 | |
| 106 | Currently supported optional args: |
| 107 | @param pool: specify the pool of machines to use for scheduling purposes. |
| 108 | Default: None |
| 109 | @param num: how many devices to reimage. |
| 110 | Default in global_config |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 111 | @param check_hosts: require appropriate hosts to be available now. |
Chris Masone | ab3e733 | 2012-02-29 18:54:58 -0800 | [diff] [blame] | 112 | @param skip_reimage: skip reimaging, used for testing purposes. |
| 113 | Default: False |
| 114 | @param add_experimental: schedule experimental tests as well, or not. |
| 115 | Default: True |
| 116 | @return a tuple of args set to provided (or default) values. |
| 117 | """ |
| 118 | required_keywords = {'build': str, |
| 119 | 'board': str, |
| 120 | 'name': str, |
| 121 | 'job': base_job.base_job} |
| 122 | for key, expected in required_keywords.iteritems(): |
| 123 | value = locals().get(key) |
| 124 | if not value or not isinstance(value, expected): |
| 125 | raise SuiteArgumentException("reimage_and_run() needs %s=<%r>" % ( |
| 126 | key, expected)) |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 127 | return (build, board, name, job, pool, num, check_hosts, skip_reimage, |
| 128 | add_experimental) |
Chris Masone | ab3e733 | 2012-02-29 18:54:58 -0800 | [diff] [blame] | 129 | |
| 130 | |
Chris Masone | 8b76425 | 2012-01-17 11:12:51 -0800 | [diff] [blame] | 131 | def inject_vars(vars, control_file_in): |
| 132 | """ |
Chris Masone | ab3e733 | 2012-02-29 18:54:58 -0800 | [diff] [blame] | 133 | Inject the contents of |vars| into |control_file_in|. |
Chris Masone | 8b76425 | 2012-01-17 11:12:51 -0800 | [diff] [blame] | 134 | |
| 135 | @param vars: a dict to shoehorn into the provided control file string. |
| 136 | @param control_file_in: the contents of a control file to munge. |
| 137 | @return the modified control file string. |
| 138 | """ |
| 139 | control_file = '' |
| 140 | for key, value in vars.iteritems(): |
Chris Masone | 6cb0d0d | 2012-03-05 15:37:49 -0800 | [diff] [blame] | 141 | # None gets injected as 'None' without this check; same for digits. |
| 142 | if isinstance(value, str): |
| 143 | control_file += "%s='%s'\n" % (key, value) |
| 144 | else: |
| 145 | control_file += "%s=%r\n" % (key, value) |
Chris Masone | 8b76425 | 2012-01-17 11:12:51 -0800 | [diff] [blame] | 146 | return control_file + control_file_in |
| 147 | |
| 148 | |
Chris Masone | 2ef1d4e | 2011-12-20 11:06:53 -0800 | [diff] [blame] | 149 | def _image_url_pattern(): |
| 150 | return CONFIG.get_config_value('CROS', 'image_url_pattern', type=str) |
| 151 | |
| 152 | |
| 153 | def _package_url_pattern(): |
| 154 | return CONFIG.get_config_value('CROS', 'package_url_pattern', type=str) |
| 155 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 156 | |
Chris Masone | ab3e733 | 2012-02-29 18:54:58 -0800 | [diff] [blame] | 157 | def skip_reimage(g): |
| 158 | return g.get('SKIP_IMAGE') |
| 159 | |
| 160 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 161 | class Reimager(object): |
| 162 | """ |
| 163 | A class that can run jobs to reimage devices. |
| 164 | |
| 165 | @var _afe: a frontend.AFE instance used to talk to autotest. |
| 166 | @var _tko: a frontend.TKO instance used to query the autotest results db. |
| 167 | @var _cf_getter: a ControlFileGetter used to get the AU control file. |
| 168 | """ |
| 169 | |
| 170 | |
Chris Masone | 9f13ff2 | 2012-03-05 13:45:25 -0800 | [diff] [blame] | 171 | def __init__(self, autotest_dir, afe=None, tko=None, pool=None, |
| 172 | results_dir=None): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 173 | """ |
| 174 | Constructor |
| 175 | |
| 176 | @param autotest_dir: the place to find autotests. |
| 177 | @param afe: an instance of AFE as defined in server/frontend.py. |
| 178 | @param tko: an instance of TKO as defined in server/frontend.py. |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 179 | @param pool: Specify the pool of machines to use for scheduling |
| 180 | purposes. |
Chris Masone | 9f13ff2 | 2012-03-05 13:45:25 -0800 | [diff] [blame] | 181 | @param results_dir: The directory where the job can write results to. |
| 182 | This must be set if you want job_id of sub-jobs |
| 183 | list in the job keyvals. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 184 | """ |
Chris Masone | 8ac6671 | 2012-02-15 14:21:02 -0800 | [diff] [blame] | 185 | self._afe = afe or frontend_wrappers.RetryingAFE(timeout_min=30, |
| 186 | delay_sec=10, |
| 187 | debug=False) |
| 188 | self._tko = tko or frontend_wrappers.RetryingTKO(timeout_min=30, |
| 189 | delay_sec=10, |
| 190 | debug=False) |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 191 | self._pool = pool |
Chris Masone | 9f13ff2 | 2012-03-05 13:45:25 -0800 | [diff] [blame] | 192 | self._results_dir = results_dir |
Chris Masone | d368cc4 | 2012-03-07 15:16:59 -0800 | [diff] [blame] | 193 | self._reimaged_hosts = {} |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 194 | self._cf_getter = control_file_getter.FileSystemGetter( |
| 195 | [os.path.join(autotest_dir, 'server/site_tests')]) |
| 196 | |
| 197 | |
Chris Masone | 2ef1d4e | 2011-12-20 11:06:53 -0800 | [diff] [blame] | 198 | def skip(self, g): |
Chris Masone | ab3e733 | 2012-02-29 18:54:58 -0800 | [diff] [blame] | 199 | """Deprecated in favor of dynamic_suite.skip_reimage().""" |
Chris Masone | 2ef1d4e | 2011-12-20 11:06:53 -0800 | [diff] [blame] | 200 | return 'SKIP_IMAGE' in g and g['SKIP_IMAGE'] |
| 201 | |
| 202 | |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 203 | def attempt(self, build, board, record, check_hosts, num=None): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 204 | """ |
| 205 | Synchronously attempt to reimage some machines. |
| 206 | |
| 207 | Fire off attempts to reimage |num| machines of type |board|, using an |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 208 | image at |url| called |build|. Wait for completion, polling every |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 209 | 10s, and log results with |record| upon completion. |
| 210 | |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 211 | @param build: the build to install e.g. |
| 212 | x86-alex-release/R18-1655.0.0-a1-b1584. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 213 | @param board: which kind of devices to reimage. |
| 214 | @param record: callable that records job status. |
Chris Masone | 796fcf1 | 2012-02-22 16:53:31 -0800 | [diff] [blame] | 215 | prototype: |
| 216 | record(status, subdir, name, reason) |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 217 | @param check_hosts: require appropriate hosts to be available now. |
Chris Masone | 5552dd7 | 2012-02-15 15:01:04 -0800 | [diff] [blame] | 218 | @param num: how many devices to reimage. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 219 | @return True if all reimaging jobs succeed, false otherwise. |
| 220 | """ |
Chris Masone | 5552dd7 | 2012-02-15 15:01:04 -0800 | [diff] [blame] | 221 | if not num: |
| 222 | num = CONFIG.get_config_value('CROS', 'sharding_factor', type=int) |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 223 | logging.debug("scheduling reimaging across %d machines", num) |
Chris Masone | 9f13ff2 | 2012-03-05 13:45:25 -0800 | [diff] [blame] | 224 | wrapper_job_name = 'try_new_image' |
Chris Masone | 73f6502 | 2012-01-31 14:00:43 -0800 | [diff] [blame] | 225 | record('START', None, wrapper_job_name) |
Chris Masone | 796fcf1 | 2012-02-22 16:53:31 -0800 | [diff] [blame] | 226 | try: |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 227 | self._ensure_version_label(VERSION_PREFIX + build) |
| 228 | |
| 229 | if check_hosts: |
| 230 | self._ensure_enough_hosts(board, self._pool, num) |
Chris Masone | 5374c67 | 2012-03-05 15:11:39 -0800 | [diff] [blame] | 231 | |
Chris Masone | d368cc4 | 2012-03-07 15:16:59 -0800 | [diff] [blame] | 232 | # Schedule job and record job metadata. |
Chris Masone | d368cc4 | 2012-03-07 15:16:59 -0800 | [diff] [blame] | 233 | canary_job = self._schedule_reimage_job(build, num, board) |
| 234 | self._record_job_if_possible(wrapper_job_name, canary_job) |
| 235 | logging.debug('Created re-imaging job: %d', canary_job.id) |
| 236 | |
| 237 | # Poll until reimaging is complete. |
| 238 | self._wait_for_job_to_start(canary_job.id) |
| 239 | self._wait_for_job_to_finish(canary_job.id) |
| 240 | |
| 241 | # Gather job results. |
| 242 | canary_job.result = self._afe.poll_job_results(self._tko, |
| 243 | canary_job, |
| 244 | 0) |
Chris Masone | 5374c67 | 2012-03-05 15:11:39 -0800 | [diff] [blame] | 245 | except InadequateHostsException as e: |
| 246 | logging.warning(e) |
| 247 | record('END WARN', None, wrapper_job_name, str(e)) |
| 248 | return False |
Chris Masone | 796fcf1 | 2012-02-22 16:53:31 -0800 | [diff] [blame] | 249 | except Exception as e: |
| 250 | # catch Exception so we record the job as terminated no matter what. |
| 251 | logging.error(e) |
| 252 | record('END ERROR', None, wrapper_job_name, str(e)) |
| 253 | return False |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 254 | |
Chris Masone | d368cc4 | 2012-03-07 15:16:59 -0800 | [diff] [blame] | 255 | self._remember_reimaged_hosts(build, canary_job) |
| 256 | |
| 257 | if canary_job.result is True: |
| 258 | self._report_results(canary_job, record) |
Chris Masone | 73f6502 | 2012-01-31 14:00:43 -0800 | [diff] [blame] | 259 | record('END GOOD', None, wrapper_job_name) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 260 | return True |
| 261 | |
Chris Masone | d368cc4 | 2012-03-07 15:16:59 -0800 | [diff] [blame] | 262 | if canary_job.result is None: |
| 263 | record('FAIL', None, canary_job.name, 'reimaging tasks did not run') |
| 264 | else: # canary_job.result is False |
| 265 | self._report_results(canary_job, record) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 266 | |
Chris Masone | 73f6502 | 2012-01-31 14:00:43 -0800 | [diff] [blame] | 267 | record('END FAIL', None, wrapper_job_name) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 268 | return False |
| 269 | |
| 270 | |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 271 | def _ensure_enough_hosts(self, board, pool, num): |
| 272 | """ |
| 273 | Determine if there are enough working hosts to run on. |
| 274 | |
| 275 | Raises exception if there are not enough hosts. |
| 276 | |
| 277 | @param board: which kind of devices to reimage. |
| 278 | @param pool: the pool of machines to use for scheduling purposes. |
| 279 | @param num: how many devices to reimage. |
| 280 | @raises InadequateHostsException: if too few working hosts. |
| 281 | """ |
| 282 | labels = [l for l in [board, pool] if l is not None] |
Chris Masone | 502b71e | 2012-04-10 10:41:35 -0700 | [diff] [blame^] | 283 | available = self._count_usable_hosts(labels) |
| 284 | if available == 0: |
| 285 | raise NoHostsException('All hosts with %r are dead!' % labels) |
| 286 | elif num > available: |
Chris Masone | 6257912 | 2012-03-08 15:18:43 -0800 | [diff] [blame] | 287 | raise InadequateHostsException('Too few hosts with %r' % labels) |
| 288 | |
| 289 | |
Chris Masone | d368cc4 | 2012-03-07 15:16:59 -0800 | [diff] [blame] | 290 | def _wait_for_job_to_start(self, job_id): |
| 291 | """ |
| 292 | Wait for the job specified by |job_id| to start. |
| 293 | |
| 294 | @param job_id: the job ID to poll on. |
| 295 | """ |
| 296 | while len(self._afe.get_jobs(id=job_id, not_yet_run=True)) > 0: |
| 297 | time.sleep(10) |
| 298 | logging.debug('Re-imaging job running.') |
| 299 | |
| 300 | |
| 301 | def _wait_for_job_to_finish(self, job_id): |
| 302 | """ |
| 303 | Wait for the job specified by |job_id| to finish. |
| 304 | |
| 305 | @param job_id: the job ID to poll on. |
| 306 | """ |
| 307 | while len(self._afe.get_jobs(id=job_id, finished=True)) == 0: |
| 308 | time.sleep(10) |
| 309 | logging.debug('Re-imaging job finished.') |
| 310 | |
| 311 | |
| 312 | def _remember_reimaged_hosts(self, build, canary_job): |
| 313 | """ |
| 314 | Remember hosts that were reimaged with |build| as a part |canary_job|. |
| 315 | |
| 316 | @param build: the build that was installed e.g. |
| 317 | x86-alex-release/R18-1655.0.0-a1-b1584. |
| 318 | @param canary_job: a completed frontend.Job object, possibly populated |
| 319 | by frontend.AFE.poll_job_results. |
| 320 | """ |
| 321 | if not hasattr(canary_job, 'results_platform_map'): |
| 322 | return |
| 323 | if not self._reimaged_hosts.get('build'): |
| 324 | self._reimaged_hosts[build] = [] |
| 325 | for platform in canary_job.results_platform_map: |
| 326 | for host in canary_job.results_platform_map[platform]['Total']: |
| 327 | self._reimaged_hosts[build].append(host) |
| 328 | |
| 329 | |
| 330 | def clear_reimaged_host_state(self, build): |
| 331 | """ |
| 332 | Clear per-host state created in the autotest DB for this job. |
| 333 | |
| 334 | After reimaging a host, we label it and set some host attributes on it |
| 335 | that are then used by the suite scheduling code. This call cleans |
| 336 | that up. |
| 337 | |
| 338 | @param build: the build whose hosts we want to clean up e.g. |
| 339 | x86-alex-release/R18-1655.0.0-a1-b1584. |
| 340 | """ |
| 341 | labels = self._afe.get_labels(name__startswith=VERSION_PREFIX + build) |
| 342 | for label in labels: self._afe.run('delete_label', id=label.id) |
| 343 | for host in self._reimaged_hosts.get('build', []): |
| 344 | self._clear_build_state(host) |
| 345 | |
| 346 | |
| 347 | def _clear_build_state(self, machine): |
| 348 | """ |
| 349 | Clear all build-specific labels, attributes from the target. |
| 350 | |
| 351 | @param machine: the host to clear labels, attributes from. |
| 352 | """ |
| 353 | self._afe.set_host_attribute('job_repo_url', None, hostname=machine) |
| 354 | |
| 355 | |
Chris Masone | 9f13ff2 | 2012-03-05 13:45:25 -0800 | [diff] [blame] | 356 | def _record_job_if_possible(self, test_name, job): |
| 357 | """ |
| 358 | Record job id as keyval, if possible, so it can be referenced later. |
| 359 | |
| 360 | If |self._results_dir| is None, then this is a NOOP. |
Chris Masone | 5374c67 | 2012-03-05 15:11:39 -0800 | [diff] [blame] | 361 | |
| 362 | @param test_name: the test to record id/owner for. |
| 363 | @param job: the job object to pull info from. |
Chris Masone | 9f13ff2 | 2012-03-05 13:45:25 -0800 | [diff] [blame] | 364 | """ |
| 365 | if self._results_dir: |
| 366 | job_id_owner = '%s-%s' % (job.id, job.owner) |
| 367 | utils.write_keyval(self._results_dir, {test_name: job_id_owner}) |
| 368 | |
| 369 | |
Chris Masone | 5374c67 | 2012-03-05 15:11:39 -0800 | [diff] [blame] | 370 | def _count_usable_hosts(self, host_spec): |
| 371 | """ |
| 372 | Given a set of host labels, count the live hosts that have them all. |
| 373 | |
| 374 | @param host_spec: list of labels specifying a set of hosts. |
| 375 | @return the number of live hosts that satisfy |host_spec|. |
| 376 | """ |
| 377 | count = 0 |
| 378 | for h in self._afe.get_hosts(multiple_labels=host_spec): |
| 379 | if h.status not in ['Repair Failed', 'Repairing']: |
| 380 | count += 1 |
| 381 | return count |
| 382 | |
| 383 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 384 | def _ensure_version_label(self, name): |
| 385 | """ |
| 386 | Ensure that a label called |name| exists in the autotest DB. |
| 387 | |
| 388 | @param name: the label to check for/create. |
| 389 | """ |
| 390 | labels = self._afe.get_labels(name=name) |
| 391 | if len(labels) == 0: |
| 392 | self._afe.create_label(name=name) |
| 393 | |
| 394 | |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 395 | def _schedule_reimage_job(self, build, num_machines, board): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 396 | """ |
| 397 | Schedules the reimaging of |num_machines| |board| devices with |image|. |
| 398 | |
| 399 | Sends an RPC to the autotest frontend to enqueue reimaging jobs on |
| 400 | |num_machines| devices of type |board| |
| 401 | |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 402 | @param build: the build to install (must be unique). |
Chris Masone | 2ef1d4e | 2011-12-20 11:06:53 -0800 | [diff] [blame] | 403 | @param num_machines: how many devices to reimage. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 404 | @param board: which kind of devices to reimage. |
| 405 | @return a frontend.Job object for the reimaging job we scheduled. |
| 406 | """ |
Chris Masone | 8b76425 | 2012-01-17 11:12:51 -0800 | [diff] [blame] | 407 | control_file = inject_vars( |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 408 | {'image_url': _image_url_pattern() % build, 'image_name': build}, |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 409 | self._cf_getter.get_control_file_contents_by_name('autoupdate')) |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 410 | job_deps = [] |
| 411 | if self._pool: |
Chris Masone | 5374c67 | 2012-03-05 15:11:39 -0800 | [diff] [blame] | 412 | meta_host = self._pool |
| 413 | board_label = board |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 414 | job_deps.append(board_label) |
| 415 | else: |
| 416 | # No pool specified use board. |
Chris Masone | 5374c67 | 2012-03-05 15:11:39 -0800 | [diff] [blame] | 417 | meta_host = board |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 418 | |
Chris Masone | 2ef1d4e | 2011-12-20 11:06:53 -0800 | [diff] [blame] | 419 | return self._afe.create_job(control_file=control_file, |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 420 | name=build + '-try', |
Chris Masone | 2ef1d4e | 2011-12-20 11:06:53 -0800 | [diff] [blame] | 421 | control_type='Server', |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 422 | meta_hosts=[meta_host] * num_machines, |
| 423 | dependencies=job_deps) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 424 | |
| 425 | |
| 426 | def _report_results(self, job, record): |
| 427 | """ |
| 428 | Record results from a completed frontend.Job object. |
| 429 | |
| 430 | @param job: a completed frontend.Job object populated by |
| 431 | frontend.AFE.poll_job_results. |
| 432 | @param record: callable that records job status. |
| 433 | prototype: |
| 434 | record(status, subdir, name, reason) |
| 435 | """ |
| 436 | if job.result == True: |
| 437 | record('GOOD', None, job.name) |
| 438 | return |
| 439 | |
| 440 | for platform in job.results_platform_map: |
| 441 | for status in job.results_platform_map[platform]: |
| 442 | if status == 'Total': |
| 443 | continue |
| 444 | for host in job.results_platform_map[platform][status]: |
| 445 | if host not in job.test_status: |
| 446 | record('ERROR', None, host, 'Job failed to run.') |
| 447 | elif status == 'Failed': |
| 448 | for test_status in job.test_status[host].fail: |
| 449 | record('FAIL', None, host, test_status.reason) |
| 450 | elif status == 'Aborted': |
| 451 | for test_status in job.test_status[host].fail: |
| 452 | record('ABORT', None, host, test_status.reason) |
| 453 | elif status == 'Completed': |
| 454 | record('GOOD', None, host) |
| 455 | |
| 456 | |
| 457 | class Suite(object): |
| 458 | """ |
| 459 | A suite of tests, defined by some predicate over control file variables. |
| 460 | |
| 461 | Given a place to search for control files a predicate to match the desired |
| 462 | tests, can gather tests and fire off jobs to run them, and then wait for |
| 463 | results. |
| 464 | |
| 465 | @var _predicate: a function that should return True when run over a |
| 466 | ControlData representation of a control file that should be in |
| 467 | this Suite. |
| 468 | @var _tag: a string with which to tag jobs run in this suite. |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 469 | @var _build: the build on which we're running this suite. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 470 | @var _afe: an instance of AFE as defined in server/frontend.py. |
| 471 | @var _tko: an instance of TKO as defined in server/frontend.py. |
| 472 | @var _jobs: currently scheduled jobs, if any. |
| 473 | @var _cf_getter: a control_file_getter.ControlFileGetter |
| 474 | """ |
| 475 | |
| 476 | |
Chris Masone | fef2138 | 2012-01-17 11:16:32 -0800 | [diff] [blame] | 477 | @staticmethod |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame] | 478 | def create_ds_getter(build): |
Chris Masone | fef2138 | 2012-01-17 11:16:32 -0800 | [diff] [blame] | 479 | """ |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 480 | @param build: the build on which we're running this suite. |
Chris Masone | fef2138 | 2012-01-17 11:16:32 -0800 | [diff] [blame] | 481 | @return a FileSystemGetter instance that looks under |autotest_dir|. |
| 482 | """ |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 483 | return control_file_getter.DevServerGetter( |
| 484 | build, dev_server.DevServer.create()) |
Chris Masone | fef2138 | 2012-01-17 11:16:32 -0800 | [diff] [blame] | 485 | |
| 486 | |
| 487 | @staticmethod |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame] | 488 | def create_fs_getter(autotest_dir): |
| 489 | """ |
| 490 | @param autotest_dir: the place to find autotests. |
| 491 | @return a FileSystemGetter instance that looks under |autotest_dir|. |
| 492 | """ |
| 493 | # currently hard-coded places to look for tests. |
| 494 | subpaths = ['server/site_tests', 'client/site_tests', |
| 495 | 'server/tests', 'client/tests'] |
| 496 | directories = [os.path.join(autotest_dir, p) for p in subpaths] |
| 497 | return control_file_getter.FileSystemGetter(directories) |
| 498 | |
| 499 | |
| 500 | @staticmethod |
Zdenek Behan | 849db05 | 2012-02-29 19:16:28 +0100 | [diff] [blame] | 501 | def parse_tag(tag): |
| 502 | """Splits a string on ',' optionally surrounded by whitespace.""" |
| 503 | return map(lambda x: x.strip(), tag.split(',')) |
| 504 | |
| 505 | |
| 506 | @staticmethod |
Chris Masone | 8456479 | 2012-02-23 10:52:42 -0800 | [diff] [blame] | 507 | def name_in_tag_predicate(name): |
| 508 | """Returns predicate that takes a control file and looks for |name|. |
| 509 | |
| 510 | Builds a predicate that takes in a parsed control file (a ControlData) |
| 511 | and returns True if the SUITE tag is present and contains |name|. |
| 512 | |
| 513 | @param name: the suite name to base the predicate on. |
| 514 | @return a callable that takes a ControlData and looks for |name| in that |
| 515 | ControlData object's suite member. |
| 516 | """ |
Zdenek Behan | 849db05 | 2012-02-29 19:16:28 +0100 | [diff] [blame] | 517 | return lambda t: hasattr(t, 'suite') and \ |
| 518 | name in Suite.parse_tag(t.suite) |
Chris Masone | 8456479 | 2012-02-23 10:52:42 -0800 | [diff] [blame] | 519 | |
Zdenek Behan | 849db05 | 2012-02-29 19:16:28 +0100 | [diff] [blame] | 520 | |
| 521 | @staticmethod |
| 522 | def list_all_suites(build, cf_getter=None): |
| 523 | """ |
| 524 | Parses all ControlData objects with a SUITE tag and extracts all |
| 525 | defined suite names. |
| 526 | |
| 527 | @param cf_getter: control_file_getter.ControlFileGetter. Defaults to |
| 528 | using DevServerGetter. |
| 529 | |
| 530 | @return list of suites |
| 531 | """ |
| 532 | if cf_getter is None: |
| 533 | cf_getter = Suite.create_ds_getter(build) |
| 534 | |
| 535 | suites = set() |
| 536 | predicate = lambda t: hasattr(t, 'suite') |
| 537 | for test in Suite.find_and_parse_tests(cf_getter, predicate): |
| 538 | suites.update(Suite.parse_tag(test.suite)) |
| 539 | return list(suites) |
Chris Masone | 8456479 | 2012-02-23 10:52:42 -0800 | [diff] [blame] | 540 | |
| 541 | |
| 542 | @staticmethod |
Scott Zawalski | 9ece653 | 2012-02-28 14:10:47 -0500 | [diff] [blame] | 543 | def create_from_name(name, build, cf_getter=None, afe=None, tko=None, |
| 544 | pool=None, results_dir=None): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 545 | """ |
| 546 | Create a Suite using a predicate based on the SUITE control file var. |
| 547 | |
| 548 | Makes a predicate based on |name| and uses it to instantiate a Suite |
| 549 | that looks for tests in |autotest_dir| and will schedule them using |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame] | 550 | |afe|. Pulls control files from the default dev server. |
| 551 | Results will be pulled from |tko| upon completion. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 552 | |
| 553 | @param name: a value of the SUITE control file variable to search for. |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 554 | @param build: the build on which we're running this suite. |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame] | 555 | @param cf_getter: a control_file_getter.ControlFileGetter. |
| 556 | If None, default to using a DevServerGetter. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 557 | @param afe: an instance of AFE as defined in server/frontend.py. |
| 558 | @param tko: an instance of TKO as defined in server/frontend.py. |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 559 | @param pool: Specify the pool of machines to use for scheduling |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame] | 560 | purposes. |
Scott Zawalski | 9ece653 | 2012-02-28 14:10:47 -0500 | [diff] [blame] | 561 | @param results_dir: The directory where the job can write results to. |
| 562 | This must be set if you want job_id of sub-jobs |
| 563 | list in the job keyvals. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 564 | @return a Suite instance. |
| 565 | """ |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame] | 566 | if cf_getter is None: |
| 567 | cf_getter = Suite.create_ds_getter(build) |
Chris Masone | 8456479 | 2012-02-23 10:52:42 -0800 | [diff] [blame] | 568 | return Suite(Suite.name_in_tag_predicate(name), |
Scott Zawalski | 9ece653 | 2012-02-28 14:10:47 -0500 | [diff] [blame] | 569 | name, build, cf_getter, afe, tko, pool, results_dir) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 570 | |
| 571 | |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame] | 572 | def __init__(self, predicate, tag, build, cf_getter, afe=None, tko=None, |
Scott Zawalski | 9ece653 | 2012-02-28 14:10:47 -0500 | [diff] [blame] | 573 | pool=None, results_dir=None): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 574 | """ |
| 575 | Constructor |
| 576 | |
| 577 | @param predicate: a function that should return True when run over a |
| 578 | ControlData representation of a control file that should be in |
| 579 | this Suite. |
| 580 | @param tag: a string with which to tag jobs run in this suite. |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 581 | @param build: the build on which we're running this suite. |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame] | 582 | @param cf_getter: a control_file_getter.ControlFileGetter |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 583 | @param afe: an instance of AFE as defined in server/frontend.py. |
| 584 | @param tko: an instance of TKO as defined in server/frontend.py. |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 585 | @param pool: Specify the pool of machines to use for scheduling |
| 586 | purposes. |
Scott Zawalski | 9ece653 | 2012-02-28 14:10:47 -0500 | [diff] [blame] | 587 | @param results_dir: The directory where the job can write results to. |
| 588 | This must be set if you want job_id of sub-jobs |
| 589 | list in the job keyvals. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 590 | """ |
| 591 | self._predicate = predicate |
| 592 | self._tag = tag |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 593 | self._build = build |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame] | 594 | self._cf_getter = cf_getter |
Scott Zawalski | 9ece653 | 2012-02-28 14:10:47 -0500 | [diff] [blame] | 595 | self._results_dir = results_dir |
Chris Masone | 8ac6671 | 2012-02-15 14:21:02 -0800 | [diff] [blame] | 596 | self._afe = afe or frontend_wrappers.RetryingAFE(timeout_min=30, |
| 597 | delay_sec=10, |
| 598 | debug=False) |
| 599 | self._tko = tko or frontend_wrappers.RetryingTKO(timeout_min=30, |
| 600 | delay_sec=10, |
| 601 | debug=False) |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 602 | self._pool = pool |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 603 | self._jobs = [] |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 604 | self._tests = Suite.find_and_parse_tests(self._cf_getter, |
| 605 | self._predicate, |
| 606 | add_experimental=True) |
| 607 | |
| 608 | |
| 609 | @property |
| 610 | def tests(self): |
| 611 | """ |
| 612 | A list of ControlData objects in the suite, with added |text| attr. |
| 613 | """ |
| 614 | return self._tests |
| 615 | |
| 616 | |
| 617 | def stable_tests(self): |
| 618 | """ |
| 619 | |self.tests|, filtered for non-experimental tests. |
| 620 | """ |
| 621 | return filter(lambda t: not t.experimental, self.tests) |
| 622 | |
| 623 | |
| 624 | def unstable_tests(self): |
| 625 | """ |
| 626 | |self.tests|, filtered for experimental tests. |
| 627 | """ |
| 628 | return filter(lambda t: t.experimental, self.tests) |
| 629 | |
| 630 | |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 631 | def _create_job(self, test): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 632 | """ |
| 633 | Thin wrapper around frontend.AFE.create_job(). |
| 634 | |
| 635 | @param test: ControlData object for a test to run. |
Scott Zawalski | e5bb1c5 | 2012-02-29 13:15:50 -0500 | [diff] [blame] | 636 | @return a frontend.Job object with an added test_name member. |
| 637 | test_name is used to preserve the higher level TEST_NAME |
| 638 | name of the job. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 639 | """ |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 640 | job_deps = [] |
| 641 | if self._pool: |
Chris Masone | 5374c67 | 2012-03-05 15:11:39 -0800 | [diff] [blame] | 642 | meta_hosts = self._pool |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 643 | cros_label = VERSION_PREFIX + self._build |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 644 | job_deps.append(cros_label) |
| 645 | else: |
| 646 | # No pool specified use any machines with the following label. |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 647 | meta_hosts = VERSION_PREFIX + self._build |
Scott Zawalski | e5bb1c5 | 2012-02-29 13:15:50 -0500 | [diff] [blame] | 648 | test_obj = self._afe.create_job( |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 649 | control_file=test.text, |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 650 | name='/'.join([self._build, self._tag, test.name]), |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 651 | control_type=test.test_type.capitalize(), |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 652 | meta_hosts=[meta_hosts], |
| 653 | dependencies=job_deps) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 654 | |
Scott Zawalski | e5bb1c5 | 2012-02-29 13:15:50 -0500 | [diff] [blame] | 655 | setattr(test_obj, 'test_name', test.name) |
| 656 | |
| 657 | return test_obj |
| 658 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 659 | |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 660 | def run_and_wait(self, record, add_experimental=True): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 661 | """ |
| 662 | Synchronously run tests in |self.tests|. |
| 663 | |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 664 | Schedules tests against a device running image |self._build|, and |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 665 | then polls for status, using |record| to print status when each |
| 666 | completes. |
| 667 | |
| 668 | Tests returned by self.stable_tests() will always be run, while tests |
| 669 | in self.unstable_tests() will only be run if |add_experimental| is true. |
| 670 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 671 | @param record: callable that records job status. |
| 672 | prototype: |
| 673 | record(status, subdir, name, reason) |
| 674 | @param add_experimental: schedule experimental tests as well, or not. |
| 675 | """ |
| 676 | try: |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 677 | record('INFO', None, 'Start %s' % self._tag) |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 678 | self.schedule(add_experimental) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 679 | try: |
| 680 | for result in self.wait_for_results(): |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 681 | # |result| will be a tuple of a maximum of 4 entries and a |
| 682 | # minimum of 3. We use the first 3 for START and END |
| 683 | # entries so we separate those variables out for legible |
| 684 | # variable names, nothing more. |
| 685 | status = result[0] |
| 686 | test_name = result[2] |
| 687 | record('START', None, test_name) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 688 | record(*result) |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 689 | record('END %s' % status, None, test_name) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 690 | except Exception as e: |
| 691 | logging.error(e) |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 692 | record('FAIL', None, self._tag, |
| 693 | 'Exception waiting for results') |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 694 | except Exception as e: |
| 695 | logging.error(e) |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 696 | record('FAIL', None, self._tag, |
| 697 | 'Exception while scheduling suite') |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 698 | |
| 699 | |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 700 | def schedule(self, add_experimental=True): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 701 | """ |
| 702 | Schedule jobs using |self._afe|. |
| 703 | |
| 704 | frontend.Job objects representing each scheduled job will be put in |
| 705 | |self._jobs|. |
| 706 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 707 | @param add_experimental: schedule experimental tests as well, or not. |
| 708 | """ |
| 709 | for test in self.stable_tests(): |
| 710 | logging.debug('Scheduling %s', test.name) |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 711 | self._jobs.append(self._create_job(test)) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 712 | |
| 713 | if add_experimental: |
| 714 | # TODO(cmasone): ensure I can log results from these differently. |
| 715 | for test in self.unstable_tests(): |
| 716 | logging.debug('Scheduling %s', test.name) |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 717 | self._jobs.append(self._create_job(test)) |
Scott Zawalski | 9ece653 | 2012-02-28 14:10:47 -0500 | [diff] [blame] | 718 | if self._results_dir: |
| 719 | self._record_scheduled_jobs() |
| 720 | |
| 721 | |
| 722 | def _record_scheduled_jobs(self): |
| 723 | """ |
| 724 | Record scheduled job ids as keyvals, so they can be referenced later. |
Scott Zawalski | 9ece653 | 2012-02-28 14:10:47 -0500 | [diff] [blame] | 725 | """ |
| 726 | for job in self._jobs: |
| 727 | job_id_owner = '%s-%s' % (job.id, job.owner) |
Scott Zawalski | e5bb1c5 | 2012-02-29 13:15:50 -0500 | [diff] [blame] | 728 | utils.write_keyval(self._results_dir, {job.test_name: job_id_owner}) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 729 | |
| 730 | |
| 731 | def _status_is_relevant(self, status): |
| 732 | """ |
| 733 | Indicates whether the status of a given test is meaningful or not. |
| 734 | |
| 735 | @param status: frontend.TestStatus object to look at. |
| 736 | @return True if this is a test result worth looking at further. |
| 737 | """ |
| 738 | return not (status.test_name.startswith('SERVER_JOB') or |
| 739 | status.test_name.startswith('CLIENT_JOB')) |
| 740 | |
| 741 | |
| 742 | def _collate_aborted(self, current_value, entry): |
| 743 | """ |
| 744 | reduce() over a list of HostQueueEntries for a job; True if any aborted. |
| 745 | |
| 746 | Functor that can be reduced()ed over a list of |
| 747 | HostQueueEntries for a job. If any were aborted |
| 748 | (|entry.aborted| exists and is True), then the reduce() will |
| 749 | return True. |
| 750 | |
| 751 | Ex: |
| 752 | entries = self._afe.run('get_host_queue_entries', job=job.id) |
| 753 | reduce(self._collate_aborted, entries, False) |
| 754 | |
| 755 | @param current_value: the current accumulator (a boolean). |
| 756 | @param entry: the current entry under consideration. |
| 757 | @return the value of |entry.aborted| if it exists, False if not. |
| 758 | """ |
| 759 | return current_value or ('aborted' in entry and entry['aborted']) |
| 760 | |
| 761 | |
| 762 | def wait_for_results(self): |
| 763 | """ |
| 764 | Wait for results of all tests in all jobs in |self._jobs|. |
| 765 | |
| 766 | Currently polls for results every 5s. When all results are available, |
| 767 | @return a list of tuples, one per test: (status, subdir, name, reason) |
| 768 | """ |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 769 | while self._jobs: |
| 770 | for job in list(self._jobs): |
| 771 | if not self._afe.get_jobs(id=job.id, finished=True): |
| 772 | continue |
| 773 | |
| 774 | self._jobs.remove(job) |
| 775 | |
| 776 | entries = self._afe.run('get_host_queue_entries', job=job.id) |
| 777 | if reduce(self._collate_aborted, entries, False): |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 778 | yield('ABORT', None, job.name) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 779 | else: |
| 780 | statuses = self._tko.get_status_counts(job=job.id) |
| 781 | for s in filter(self._status_is_relevant, statuses): |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 782 | yield(s.status, None, s.test_name, s.reason) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 783 | time.sleep(5) |
| 784 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 785 | |
Chris Masone | fef2138 | 2012-01-17 11:16:32 -0800 | [diff] [blame] | 786 | @staticmethod |
| 787 | def find_and_parse_tests(cf_getter, predicate, add_experimental=False): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 788 | """ |
| 789 | Function to scan through all tests and find eligible tests. |
| 790 | |
| 791 | Looks at control files returned by _cf_getter.get_control_file_list() |
| 792 | for tests that pass self._predicate(). |
| 793 | |
| 794 | @param cf_getter: a control_file_getter.ControlFileGetter used to list |
| 795 | and fetch the content of control files |
| 796 | @param predicate: a function that should return True when run over a |
| 797 | ControlData representation of a control file that should be in |
| 798 | this Suite. |
| 799 | @param add_experimental: add tests with experimental attribute set. |
| 800 | |
| 801 | @return list of ControlData objects that should be run, with control |
| 802 | file text added in |text| attribute. |
| 803 | """ |
| 804 | tests = {} |
| 805 | files = cf_getter.get_control_file_list() |
| 806 | for file in files: |
| 807 | text = cf_getter.get_control_file_contents(file) |
| 808 | try: |
| 809 | found_test = control_data.parse_control_string(text, |
| 810 | raise_warnings=True) |
| 811 | if not add_experimental and found_test.experimental: |
| 812 | continue |
| 813 | |
| 814 | found_test.text = text |
Chris Masone | e8a4eff | 2012-02-28 16:33:43 -0800 | [diff] [blame] | 815 | found_test.path = file |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 816 | tests[file] = found_test |
| 817 | except control_data.ControlVariableException, e: |
| 818 | logging.warn("Skipping %s\n%s", file, e) |
| 819 | except Exception, e: |
| 820 | logging.error("Bad %s\n%s", file, e) |
| 821 | |
| 822 | return [test for test in tests.itervalues() if predicate(test)] |