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 | 2ef1d4e | 2011-12-20 11:06:53 -0800 | [diff] [blame] | 7 | from autotest_lib.client.common_lib import control_data, global_config, error |
| 8 | from autotest_lib.client.common_lib import 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 Masone | 8b76425 | 2012-01-17 11:12:51 -0800 | [diff] [blame] | 18 | def inject_vars(vars, control_file_in): |
| 19 | """ |
| 20 | Inject the contents of |vars| into |control_file_in| |
| 21 | |
| 22 | @param vars: a dict to shoehorn into the provided control file string. |
| 23 | @param control_file_in: the contents of a control file to munge. |
| 24 | @return the modified control file string. |
| 25 | """ |
| 26 | control_file = '' |
| 27 | for key, value in vars.iteritems(): |
| 28 | control_file += "%s='%s'\n" % (key, value) |
| 29 | return control_file + control_file_in |
| 30 | |
| 31 | |
Chris Masone | 2ef1d4e | 2011-12-20 11:06:53 -0800 | [diff] [blame] | 32 | def _image_url_pattern(): |
| 33 | return CONFIG.get_config_value('CROS', 'image_url_pattern', type=str) |
| 34 | |
| 35 | |
| 36 | def _package_url_pattern(): |
| 37 | return CONFIG.get_config_value('CROS', 'package_url_pattern', type=str) |
| 38 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 39 | |
| 40 | class Reimager(object): |
| 41 | """ |
| 42 | A class that can run jobs to reimage devices. |
| 43 | |
| 44 | @var _afe: a frontend.AFE instance used to talk to autotest. |
| 45 | @var _tko: a frontend.TKO instance used to query the autotest results db. |
| 46 | @var _cf_getter: a ControlFileGetter used to get the AU control file. |
| 47 | """ |
| 48 | |
| 49 | |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 50 | def __init__(self, autotest_dir, afe=None, tko=None, pool=None): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 51 | """ |
| 52 | Constructor |
| 53 | |
| 54 | @param autotest_dir: the place to find autotests. |
| 55 | @param afe: an instance of AFE as defined in server/frontend.py. |
| 56 | @param tko: an instance of TKO as defined in server/frontend.py. |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 57 | @param pool: Specify the pool of machines to use for scheduling |
| 58 | purposes. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 59 | """ |
Chris Masone | 8ac6671 | 2012-02-15 14:21:02 -0800 | [diff] [blame] | 60 | self._afe = afe or frontend_wrappers.RetryingAFE(timeout_min=30, |
| 61 | delay_sec=10, |
| 62 | debug=False) |
| 63 | self._tko = tko or frontend_wrappers.RetryingTKO(timeout_min=30, |
| 64 | delay_sec=10, |
| 65 | debug=False) |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 66 | self._pool = pool |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 67 | self._cf_getter = control_file_getter.FileSystemGetter( |
| 68 | [os.path.join(autotest_dir, 'server/site_tests')]) |
| 69 | |
| 70 | |
Chris Masone | 2ef1d4e | 2011-12-20 11:06:53 -0800 | [diff] [blame] | 71 | def skip(self, g): |
| 72 | return 'SKIP_IMAGE' in g and g['SKIP_IMAGE'] |
| 73 | |
| 74 | |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 75 | def attempt(self, build, board, record, num=None, pool=None): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 76 | """ |
| 77 | Synchronously attempt to reimage some machines. |
| 78 | |
| 79 | Fire off attempts to reimage |num| machines of type |board|, using an |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 80 | image at |url| called |build|. Wait for completion, polling every |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 81 | 10s, and log results with |record| upon completion. |
| 82 | |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 83 | @param build: the build to install e.g. |
| 84 | x86-alex-release/R18-1655.0.0-a1-b1584. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 85 | @param board: which kind of devices to reimage. |
| 86 | @param record: callable that records job status. |
| 87 | prototype: |
| 88 | record(status, subdir, name, reason) |
Chris Masone | 5552dd7 | 2012-02-15 15:01:04 -0800 | [diff] [blame] | 89 | @param num: how many devices to reimage. |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 90 | @param pool: Specify the pool of machines to use for scheduling |
| 91 | purposes. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 92 | @return True if all reimaging jobs succeed, false otherwise. |
| 93 | """ |
Chris Masone | 5552dd7 | 2012-02-15 15:01:04 -0800 | [diff] [blame] | 94 | if not num: |
| 95 | num = CONFIG.get_config_value('CROS', 'sharding_factor', type=int) |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 96 | if pool: |
| 97 | self._pool = pool |
| 98 | logging.debug("scheduling reimaging across %d machines", num) |
Chris Masone | 73f6502 | 2012-01-31 14:00:43 -0800 | [diff] [blame] | 99 | wrapper_job_name = 'try new image' |
| 100 | record('START', None, wrapper_job_name) |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 101 | self._ensure_version_label(VERSION_PREFIX + build) |
| 102 | canary = self._schedule_reimage_job(build, num, board) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 103 | logging.debug('Created re-imaging job: %d', canary.id) |
| 104 | while len(self._afe.get_jobs(id=canary.id, not_yet_run=True)) > 0: |
| 105 | time.sleep(10) |
| 106 | logging.debug('Re-imaging job running.') |
| 107 | while len(self._afe.get_jobs(id=canary.id, finished=True)) == 0: |
| 108 | time.sleep(10) |
| 109 | logging.debug('Re-imaging job finished.') |
| 110 | canary.result = self._afe.poll_job_results(self._tko, canary, 0) |
| 111 | |
| 112 | if canary.result is True: |
| 113 | self._report_results(canary, record) |
Chris Masone | 73f6502 | 2012-01-31 14:00:43 -0800 | [diff] [blame] | 114 | record('END GOOD', None, wrapper_job_name) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 115 | return True |
| 116 | |
| 117 | if canary.result is None: |
| 118 | record('FAIL', None, canary.name, 're-imaging tasks did not run') |
| 119 | else: # canary.result is False |
| 120 | self._report_results(canary, record) |
| 121 | |
Chris Masone | 73f6502 | 2012-01-31 14:00:43 -0800 | [diff] [blame] | 122 | record('END FAIL', None, wrapper_job_name) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 123 | return False |
| 124 | |
| 125 | |
| 126 | def _ensure_version_label(self, name): |
| 127 | """ |
| 128 | Ensure that a label called |name| exists in the autotest DB. |
| 129 | |
| 130 | @param name: the label to check for/create. |
| 131 | """ |
| 132 | labels = self._afe.get_labels(name=name) |
| 133 | if len(labels) == 0: |
| 134 | self._afe.create_label(name=name) |
| 135 | |
| 136 | |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 137 | def _schedule_reimage_job(self, build, num_machines, board): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 138 | """ |
| 139 | Schedules the reimaging of |num_machines| |board| devices with |image|. |
| 140 | |
| 141 | Sends an RPC to the autotest frontend to enqueue reimaging jobs on |
| 142 | |num_machines| devices of type |board| |
| 143 | |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 144 | @param build: the build to install (must be unique). |
Chris Masone | 2ef1d4e | 2011-12-20 11:06:53 -0800 | [diff] [blame] | 145 | @param num_machines: how many devices to reimage. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 146 | @param board: which kind of devices to reimage. |
| 147 | @return a frontend.Job object for the reimaging job we scheduled. |
| 148 | """ |
Chris Masone | 8b76425 | 2012-01-17 11:12:51 -0800 | [diff] [blame] | 149 | control_file = inject_vars( |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 150 | {'image_url': _image_url_pattern() % build, 'image_name': build}, |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 151 | self._cf_getter.get_control_file_contents_by_name('autoupdate')) |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 152 | job_deps = [] |
| 153 | if self._pool: |
| 154 | meta_host = 'pool:%s' % self._pool |
| 155 | board_label = 'board:%s' % board |
| 156 | job_deps.append(board_label) |
| 157 | else: |
| 158 | # No pool specified use board. |
| 159 | meta_host = 'board:%s' % board |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 160 | |
Chris Masone | 2ef1d4e | 2011-12-20 11:06:53 -0800 | [diff] [blame] | 161 | return self._afe.create_job(control_file=control_file, |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 162 | name=build + '-try', |
Chris Masone | 2ef1d4e | 2011-12-20 11:06:53 -0800 | [diff] [blame] | 163 | control_type='Server', |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 164 | meta_hosts=[meta_host] * num_machines, |
| 165 | dependencies=job_deps) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 166 | |
| 167 | |
| 168 | def _report_results(self, job, record): |
| 169 | """ |
| 170 | Record results from a completed frontend.Job object. |
| 171 | |
| 172 | @param job: a completed frontend.Job object populated by |
| 173 | frontend.AFE.poll_job_results. |
| 174 | @param record: callable that records job status. |
| 175 | prototype: |
| 176 | record(status, subdir, name, reason) |
| 177 | """ |
| 178 | if job.result == True: |
| 179 | record('GOOD', None, job.name) |
| 180 | return |
| 181 | |
| 182 | for platform in job.results_platform_map: |
| 183 | for status in job.results_platform_map[platform]: |
| 184 | if status == 'Total': |
| 185 | continue |
| 186 | for host in job.results_platform_map[platform][status]: |
| 187 | if host not in job.test_status: |
| 188 | record('ERROR', None, host, 'Job failed to run.') |
| 189 | elif status == 'Failed': |
| 190 | for test_status in job.test_status[host].fail: |
| 191 | record('FAIL', None, host, test_status.reason) |
| 192 | elif status == 'Aborted': |
| 193 | for test_status in job.test_status[host].fail: |
| 194 | record('ABORT', None, host, test_status.reason) |
| 195 | elif status == 'Completed': |
| 196 | record('GOOD', None, host) |
| 197 | |
| 198 | |
| 199 | class Suite(object): |
| 200 | """ |
| 201 | A suite of tests, defined by some predicate over control file variables. |
| 202 | |
| 203 | Given a place to search for control files a predicate to match the desired |
| 204 | tests, can gather tests and fire off jobs to run them, and then wait for |
| 205 | results. |
| 206 | |
| 207 | @var _predicate: a function that should return True when run over a |
| 208 | ControlData representation of a control file that should be in |
| 209 | this Suite. |
| 210 | @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] | 211 | @var _build: the build on which we're running this suite. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 212 | @var _afe: an instance of AFE as defined in server/frontend.py. |
| 213 | @var _tko: an instance of TKO as defined in server/frontend.py. |
| 214 | @var _jobs: currently scheduled jobs, if any. |
| 215 | @var _cf_getter: a control_file_getter.ControlFileGetter |
| 216 | """ |
| 217 | |
| 218 | |
Chris Masone | fef2138 | 2012-01-17 11:16:32 -0800 | [diff] [blame] | 219 | @staticmethod |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame^] | 220 | def create_ds_getter(build): |
Chris Masone | fef2138 | 2012-01-17 11:16:32 -0800 | [diff] [blame] | 221 | """ |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 222 | @param build: the build on which we're running this suite. |
Chris Masone | fef2138 | 2012-01-17 11:16:32 -0800 | [diff] [blame] | 223 | @return a FileSystemGetter instance that looks under |autotest_dir|. |
| 224 | """ |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 225 | return control_file_getter.DevServerGetter( |
| 226 | build, dev_server.DevServer.create()) |
Chris Masone | fef2138 | 2012-01-17 11:16:32 -0800 | [diff] [blame] | 227 | |
| 228 | |
| 229 | @staticmethod |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame^] | 230 | def create_fs_getter(autotest_dir): |
| 231 | """ |
| 232 | @param autotest_dir: the place to find autotests. |
| 233 | @return a FileSystemGetter instance that looks under |autotest_dir|. |
| 234 | """ |
| 235 | # currently hard-coded places to look for tests. |
| 236 | subpaths = ['server/site_tests', 'client/site_tests', |
| 237 | 'server/tests', 'client/tests'] |
| 238 | directories = [os.path.join(autotest_dir, p) for p in subpaths] |
| 239 | return control_file_getter.FileSystemGetter(directories) |
| 240 | |
| 241 | |
| 242 | @staticmethod |
| 243 | def create_from_name(name, build, cf_getter=None, |
| 244 | afe=None, tko=None, pool=None): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 245 | """ |
| 246 | Create a Suite using a predicate based on the SUITE control file var. |
| 247 | |
| 248 | Makes a predicate based on |name| and uses it to instantiate a Suite |
| 249 | that looks for tests in |autotest_dir| and will schedule them using |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame^] | 250 | |afe|. Pulls control files from the default dev server. |
| 251 | Results will be pulled from |tko| upon completion. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 252 | |
| 253 | @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] | 254 | @param build: the build on which we're running this suite. |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame^] | 255 | @param cf_getter: a control_file_getter.ControlFileGetter. |
| 256 | If None, default to using a DevServerGetter. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 257 | @param afe: an instance of AFE as defined in server/frontend.py. |
| 258 | @param tko: an instance of TKO as defined in server/frontend.py. |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 259 | @param pool: Specify the pool of machines to use for scheduling |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame^] | 260 | purposes. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 261 | @return a Suite instance. |
| 262 | """ |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame^] | 263 | if cf_getter is None: |
| 264 | cf_getter = Suite.create_ds_getter(build) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 265 | return Suite(lambda t: hasattr(t, 'suite') and t.suite == name, |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame^] | 266 | name, build, cf_getter, afe, tko, pool) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 267 | |
| 268 | |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame^] | 269 | |
| 270 | def __init__(self, predicate, tag, build, cf_getter, afe=None, tko=None, |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 271 | pool=None): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 272 | """ |
| 273 | Constructor |
| 274 | |
| 275 | @param predicate: a function that should return True when run over a |
| 276 | ControlData representation of a control file that should be in |
| 277 | this Suite. |
| 278 | @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] | 279 | @param build: the build on which we're running this suite. |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame^] | 280 | @param cf_getter: a control_file_getter.ControlFileGetter |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 281 | @param afe: an instance of AFE as defined in server/frontend.py. |
| 282 | @param tko: an instance of TKO as defined in server/frontend.py. |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 283 | @param pool: Specify the pool of machines to use for scheduling |
| 284 | purposes. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 285 | """ |
| 286 | self._predicate = predicate |
| 287 | self._tag = tag |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 288 | self._build = build |
Chris Masone | d6f38c8 | 2012-02-22 14:53:42 -0800 | [diff] [blame^] | 289 | self._cf_getter = cf_getter |
Chris Masone | 8ac6671 | 2012-02-15 14:21:02 -0800 | [diff] [blame] | 290 | self._afe = afe or frontend_wrappers.RetryingAFE(timeout_min=30, |
| 291 | delay_sec=10, |
| 292 | debug=False) |
| 293 | self._tko = tko or frontend_wrappers.RetryingTKO(timeout_min=30, |
| 294 | delay_sec=10, |
| 295 | debug=False) |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 296 | self._pool = pool |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 297 | self._jobs = [] |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 298 | self._tests = Suite.find_and_parse_tests(self._cf_getter, |
| 299 | self._predicate, |
| 300 | add_experimental=True) |
| 301 | |
| 302 | |
| 303 | @property |
| 304 | def tests(self): |
| 305 | """ |
| 306 | A list of ControlData objects in the suite, with added |text| attr. |
| 307 | """ |
| 308 | return self._tests |
| 309 | |
| 310 | |
| 311 | def stable_tests(self): |
| 312 | """ |
| 313 | |self.tests|, filtered for non-experimental tests. |
| 314 | """ |
| 315 | return filter(lambda t: not t.experimental, self.tests) |
| 316 | |
| 317 | |
| 318 | def unstable_tests(self): |
| 319 | """ |
| 320 | |self.tests|, filtered for experimental tests. |
| 321 | """ |
| 322 | return filter(lambda t: t.experimental, self.tests) |
| 323 | |
| 324 | |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 325 | def _create_job(self, test): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 326 | """ |
| 327 | Thin wrapper around frontend.AFE.create_job(). |
| 328 | |
| 329 | @param test: ControlData object for a test to run. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 330 | @return frontend.Job object for the job just scheduled. |
| 331 | """ |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 332 | job_deps = [] |
| 333 | if self._pool: |
| 334 | meta_hosts = 'pool:%s' % self._pool |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 335 | cros_label = VERSION_PREFIX + self._build |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 336 | job_deps.append(cros_label) |
| 337 | else: |
| 338 | # No pool specified use any machines with the following label. |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 339 | meta_hosts = VERSION_PREFIX + self._build |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 340 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 341 | return self._afe.create_job( |
| 342 | control_file=test.text, |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 343 | name='/'.join([self._build, self._tag, test.name]), |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 344 | control_type=test.test_type.capitalize(), |
Scott Zawalski | 6565017 | 2012-02-16 11:48:26 -0500 | [diff] [blame] | 345 | meta_hosts=[meta_hosts], |
| 346 | dependencies=job_deps) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 347 | |
| 348 | |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 349 | def run_and_wait(self, record, add_experimental=True): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 350 | """ |
| 351 | Synchronously run tests in |self.tests|. |
| 352 | |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 353 | Schedules tests against a device running image |self._build|, and |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 354 | then polls for status, using |record| to print status when each |
| 355 | completes. |
| 356 | |
| 357 | Tests returned by self.stable_tests() will always be run, while tests |
| 358 | in self.unstable_tests() will only be run if |add_experimental| is true. |
| 359 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 360 | @param record: callable that records job status. |
| 361 | prototype: |
| 362 | record(status, subdir, name, reason) |
| 363 | @param add_experimental: schedule experimental tests as well, or not. |
| 364 | """ |
| 365 | try: |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 366 | record('INFO', None, 'Start %s' % self._tag) |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 367 | self.schedule(add_experimental) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 368 | try: |
| 369 | for result in self.wait_for_results(): |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 370 | # |result| will be a tuple of a maximum of 4 entries and a |
| 371 | # minimum of 3. We use the first 3 for START and END |
| 372 | # entries so we separate those variables out for legible |
| 373 | # variable names, nothing more. |
| 374 | status = result[0] |
| 375 | test_name = result[2] |
| 376 | record('START', None, test_name) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 377 | record(*result) |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 378 | record('END %s' % status, None, test_name) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 379 | except Exception as e: |
| 380 | logging.error(e) |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 381 | record('FAIL', None, self._tag, |
| 382 | 'Exception waiting for results') |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 383 | except Exception as e: |
| 384 | logging.error(e) |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 385 | record('FAIL', None, self._tag, |
| 386 | 'Exception while scheduling suite') |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 387 | |
| 388 | |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 389 | def schedule(self, add_experimental=True): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 390 | """ |
| 391 | Schedule jobs using |self._afe|. |
| 392 | |
| 393 | frontend.Job objects representing each scheduled job will be put in |
| 394 | |self._jobs|. |
| 395 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 396 | @param add_experimental: schedule experimental tests as well, or not. |
| 397 | """ |
| 398 | for test in self.stable_tests(): |
| 399 | logging.debug('Scheduling %s', test.name) |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 400 | self._jobs.append(self._create_job(test)) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 401 | |
| 402 | if add_experimental: |
| 403 | # TODO(cmasone): ensure I can log results from these differently. |
| 404 | for test in self.unstable_tests(): |
| 405 | logging.debug('Scheduling %s', test.name) |
Chris Masone | 8b7cd42 | 2012-02-22 13:16:11 -0800 | [diff] [blame] | 406 | self._jobs.append(self._create_job(test)) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 407 | |
| 408 | |
| 409 | def _status_is_relevant(self, status): |
| 410 | """ |
| 411 | Indicates whether the status of a given test is meaningful or not. |
| 412 | |
| 413 | @param status: frontend.TestStatus object to look at. |
| 414 | @return True if this is a test result worth looking at further. |
| 415 | """ |
| 416 | return not (status.test_name.startswith('SERVER_JOB') or |
| 417 | status.test_name.startswith('CLIENT_JOB')) |
| 418 | |
| 419 | |
| 420 | def _collate_aborted(self, current_value, entry): |
| 421 | """ |
| 422 | reduce() over a list of HostQueueEntries for a job; True if any aborted. |
| 423 | |
| 424 | Functor that can be reduced()ed over a list of |
| 425 | HostQueueEntries for a job. If any were aborted |
| 426 | (|entry.aborted| exists and is True), then the reduce() will |
| 427 | return True. |
| 428 | |
| 429 | Ex: |
| 430 | entries = self._afe.run('get_host_queue_entries', job=job.id) |
| 431 | reduce(self._collate_aborted, entries, False) |
| 432 | |
| 433 | @param current_value: the current accumulator (a boolean). |
| 434 | @param entry: the current entry under consideration. |
| 435 | @return the value of |entry.aborted| if it exists, False if not. |
| 436 | """ |
| 437 | return current_value or ('aborted' in entry and entry['aborted']) |
| 438 | |
| 439 | |
| 440 | def wait_for_results(self): |
| 441 | """ |
| 442 | Wait for results of all tests in all jobs in |self._jobs|. |
| 443 | |
| 444 | Currently polls for results every 5s. When all results are available, |
| 445 | @return a list of tuples, one per test: (status, subdir, name, reason) |
| 446 | """ |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 447 | while self._jobs: |
| 448 | for job in list(self._jobs): |
| 449 | if not self._afe.get_jobs(id=job.id, finished=True): |
| 450 | continue |
| 451 | |
| 452 | self._jobs.remove(job) |
| 453 | |
| 454 | entries = self._afe.run('get_host_queue_entries', job=job.id) |
| 455 | if reduce(self._collate_aborted, entries, False): |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 456 | yield('ABORT', None, job.name) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 457 | else: |
| 458 | statuses = self._tko.get_status_counts(job=job.id) |
| 459 | for s in filter(self._status_is_relevant, statuses): |
Scott Zawalski | ab25bd6 | 2012-02-10 18:29:12 -0500 | [diff] [blame] | 460 | yield(s.status, None, s.test_name, s.reason) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 461 | time.sleep(5) |
| 462 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 463 | |
Chris Masone | fef2138 | 2012-01-17 11:16:32 -0800 | [diff] [blame] | 464 | @staticmethod |
| 465 | def find_and_parse_tests(cf_getter, predicate, add_experimental=False): |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 466 | """ |
| 467 | Function to scan through all tests and find eligible tests. |
| 468 | |
| 469 | Looks at control files returned by _cf_getter.get_control_file_list() |
| 470 | for tests that pass self._predicate(). |
| 471 | |
| 472 | @param cf_getter: a control_file_getter.ControlFileGetter used to list |
| 473 | and fetch the content of control files |
| 474 | @param predicate: a function that should return True when run over a |
| 475 | ControlData representation of a control file that should be in |
| 476 | this Suite. |
| 477 | @param add_experimental: add tests with experimental attribute set. |
| 478 | |
| 479 | @return list of ControlData objects that should be run, with control |
| 480 | file text added in |text| attribute. |
| 481 | """ |
| 482 | tests = {} |
| 483 | files = cf_getter.get_control_file_list() |
| 484 | for file in files: |
| 485 | text = cf_getter.get_control_file_contents(file) |
| 486 | try: |
| 487 | found_test = control_data.parse_control_string(text, |
| 488 | raise_warnings=True) |
| 489 | if not add_experimental and found_test.experimental: |
| 490 | continue |
| 491 | |
| 492 | found_test.text = text |
| 493 | tests[file] = found_test |
| 494 | except control_data.ControlVariableException, e: |
| 495 | logging.warn("Skipping %s\n%s", file, e) |
| 496 | except Exception, e: |
| 497 | logging.error("Bad %s\n%s", file, e) |
| 498 | |
| 499 | return [test for test in tests.itervalues() if predicate(test)] |