Xixuan Wu | c7bf77c | 2018-04-24 12:05:40 -0700 | [diff] [blame] | 1 | # Copyright 2018 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 | |
| 5 | """Module for CrOS dynamic test suite generation and execution.""" |
| 6 | |
| 7 | from __future__ import absolute_import |
| 8 | from __future__ import division |
| 9 | from __future__ import print_function |
| 10 | |
Xixuan Wu | 80795c8 | 2018-06-12 11:56:17 -0700 | [diff] [blame] | 11 | import contextlib |
| 12 | import itertools |
Xixuan Wu | 0bea952 | 2018-05-08 17:49:19 -0700 | [diff] [blame] | 13 | import json |
Xixuan Wu | 6c04133 | 2018-05-07 16:04:36 -0700 | [diff] [blame] | 14 | import logging |
Xixuan Wu | e71c893 | 2018-05-07 17:18:34 -0700 | [diff] [blame] | 15 | import os |
Aviv Keshet | f095121 | 2019-03-18 14:54:32 -0700 | [diff] [blame] | 16 | import re |
Xixuan Wu | 0bea952 | 2018-05-08 17:49:19 -0700 | [diff] [blame] | 17 | import time |
Xixuan Wu | 6c04133 | 2018-05-07 16:04:36 -0700 | [diff] [blame] | 18 | |
Xixuan Wu | e71c893 | 2018-05-07 17:18:34 -0700 | [diff] [blame] | 19 | from lucifer import autotest |
Xixuan Wu | 9af95a2 | 2018-05-18 10:46:42 -0700 | [diff] [blame] | 20 | from skylab_suite import cros_suite |
Xixuan Wu | 0bea952 | 2018-05-08 17:49:19 -0700 | [diff] [blame] | 21 | from skylab_suite import swarming_lib |
Xixuan Wu | c7bf77c | 2018-04-24 12:05:40 -0700 | [diff] [blame] | 22 | |
Xixuan Wu | 0bea952 | 2018-05-08 17:49:19 -0700 | [diff] [blame] | 23 | |
Allen Li | 1ccca8f | 2018-08-29 12:11:06 -0700 | [diff] [blame] | 24 | SKYLAB_DRONE_SWARMING_WORKER = '/opt/infra-tools/skylab_swarming_worker' |
Aviv Keshet | f095121 | 2019-03-18 14:54:32 -0700 | [diff] [blame] | 25 | SKYLAB_SUITE_USER = 'skylab_suite_runner' |
| 26 | SKYLAB_TOOL = '/opt/infra-tools/skylab' |
Xixuan Wu | c7bf77c | 2018-04-24 12:05:40 -0700 | [diff] [blame] | 27 | |
Xixuan Wu | 80795c8 | 2018-06-12 11:56:17 -0700 | [diff] [blame] | 28 | SUITE_WAIT_SLEEP_INTERVAL_SECONDS = 30 |
| 29 | |
Xixuan Wu | 79d1466 | 2018-08-20 11:15:41 -0700 | [diff] [blame] | 30 | # See #5 in crbug.com/873886 for more details. |
| 31 | _NOT_SUPPORTED_DEPENDENCIES = ['skip_provision', 'cleanup-reboot', 'rpm', |
| 32 | 'modem_repair'] |
| 33 | |
Xixuan Wu | e71c893 | 2018-05-07 17:18:34 -0700 | [diff] [blame] | 34 | |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 35 | def run(client, test_specs, suite_handler, dry_run=False): |
Xixuan Wu | 56424bc | 2018-05-15 11:03:27 -0700 | [diff] [blame] | 36 | """Run a CrOS dynamic test suite. |
| 37 | |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 38 | @param client: A swarming_lib.Client instance. |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 39 | @param test_specs: A list of cros_suite.TestSpec objects. |
Xixuan Wu | 9af95a2 | 2018-05-18 10:46:42 -0700 | [diff] [blame] | 40 | @param suite_handler: A cros_suite.SuiteHandler object. |
Xixuan Wu | 56424bc | 2018-05-15 11:03:27 -0700 | [diff] [blame] | 41 | @param dry_run: Whether to kick off dry runs of the tests. |
| 42 | """ |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 43 | assert isinstance(client, swarming_lib.Client) |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 44 | if suite_handler.suite_id: |
| 45 | # Resume an existing suite. |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 46 | _resume_suite(client, test_specs, suite_handler, dry_run) |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 47 | else: |
| 48 | # Make a new suite. |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 49 | _run_suite(test_specs, suite_handler, dry_run) |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 50 | |
| 51 | |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 52 | def _resume_suite(client, test_specs, suite_handler, dry_run=False): |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 53 | """Resume a suite and its child tasks by given suite id.""" |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 54 | assert isinstance(client, swarming_lib.Client) |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 55 | suite_id = suite_handler.suite_id |
Aviv Keshet | d3adbfa | 2019-03-19 11:43:24 -0700 | [diff] [blame] | 56 | all_tasks = client.get_child_tasks(suite_id) |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 57 | not_yet_scheduled = _get_unscheduled_test_specs( |
Xixuan Wu | 6c1866b | 2018-07-12 17:04:39 -0700 | [diff] [blame] | 58 | test_specs, suite_handler, all_tasks) |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 59 | |
| 60 | logging.info('Not yet scheduled test_specs: %r', not_yet_scheduled) |
Aviv Keshet | d993510 | 2019-03-18 14:44:28 -0700 | [diff] [blame] | 61 | _create_test_tasks(not_yet_scheduled, suite_handler, suite_id, dry_run) |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 62 | |
| 63 | if suite_id is not None and suite_handler.should_wait(): |
| 64 | _wait_for_results(suite_handler, dry_run=dry_run) |
| 65 | |
| 66 | |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 67 | def _get_unscheduled_test_specs(test_specs, suite_handler, all_tasks): |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 68 | not_yet_scheduled = [] |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 69 | for test_spec in test_specs: |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 70 | if suite_handler.is_provision(): |
Xixuan Wu | 58bbb64 | 2018-07-12 14:12:14 -0700 | [diff] [blame] | 71 | # We cannot check bot_id because pending tasks do not have it yet. |
| 72 | bot_id_tag = 'id:%s' % test_spec.bot_id |
| 73 | tasks = [t for t in all_tasks if bot_id_tag in t['tags']] |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 74 | else: |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 75 | tasks = [t for t in all_tasks if t['name']==test_spec.test.name] |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 76 | |
| 77 | if not tasks: |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 78 | not_yet_scheduled.append(test_spec) |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 79 | continue |
| 80 | |
| 81 | current_task = _get_current_task(tasks) |
| 82 | test_task_id = (current_task['task_id'] if current_task |
| 83 | else tasks[0]['task_id']) |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 84 | remaining_retries = test_spec.test.job_retries - len(tasks) |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 85 | previous_retried_ids = [t['task_id'] for t in tasks |
| 86 | if t['task_id'] != test_task_id] |
| 87 | suite_handler.add_test_by_task_id( |
| 88 | test_task_id, |
Xixuan Wu | 9d5d703 | 2018-07-12 16:44:02 -0700 | [diff] [blame] | 89 | cros_suite.TestHandlerSpec( |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 90 | test_spec=test_spec, |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 91 | remaining_retries=remaining_retries, |
| 92 | previous_retried_ids=previous_retried_ids)) |
| 93 | |
| 94 | return not_yet_scheduled |
| 95 | |
| 96 | |
| 97 | def _get_current_task(tasks): |
| 98 | """Get current running task. |
| 99 | |
| 100 | @param tasks: A list of task dicts including task_id, state, etc. |
| 101 | |
| 102 | @return a dict representing the current running task. |
| 103 | """ |
| 104 | current_task = None |
| 105 | for t in tasks: |
| 106 | if t['state'] not in swarming_lib.TASK_FINISHED_STATUS: |
| 107 | if current_task: |
| 108 | raise ValueError( |
| 109 | 'Parent task has 2 same running child tasks: %s, %s' |
| 110 | % (current_task['task_id'], t['task_id'])) |
| 111 | |
| 112 | current_task = t |
| 113 | |
| 114 | return current_task |
| 115 | |
| 116 | |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 117 | def _run_suite(test_specs, suite_handler, dry_run=False): |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 118 | """Make a new suite.""" |
Xixuan Wu | 56424bc | 2018-05-15 11:03:27 -0700 | [diff] [blame] | 119 | suite_id = os.environ.get('SWARMING_TASK_ID') |
Aviv Keshet | d993510 | 2019-03-18 14:44:28 -0700 | [diff] [blame] | 120 | _create_test_tasks(test_specs, suite_handler, suite_id, dry_run) |
Xixuan Wu | a79b5f7 | 2018-12-26 12:29:39 -0800 | [diff] [blame] | 121 | suite_handler.set_suite_id(suite_id) |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 122 | |
| 123 | if suite_id is not None and suite_handler.should_wait(): |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 124 | _wait_for_results(suite_handler, dry_run=dry_run) |
| 125 | |
| 126 | |
Aviv Keshet | d993510 | 2019-03-18 14:44:28 -0700 | [diff] [blame] | 127 | def _create_test_tasks(test_specs, suite_handler, suite_id, dry_run=False): |
| 128 | """Create test tasks for a list of tests (TestSpecs). |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 129 | |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 130 | Given a list of TestSpec object, this function will schedule them on |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 131 | swarming one by one, and add them to the swarming_task_id-to-test map |
| 132 | of suite_handler to keep monitoring them. |
| 133 | |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 134 | @param test_specs: A list of cros_suite.TestSpec objects to schedule. |
Xixuan Wu | c743071 | 2018-07-10 12:04:34 -0700 | [diff] [blame] | 135 | @param suite_handler: A cros_suite.SuiteHandler object to monitor the |
| 136 | test_specs' progress. |
| 137 | @param suite_id: A string ID for a suite task, it's the parent task id for |
| 138 | these to-be-scheduled test_specs. |
| 139 | @param dry_run: Whether to kick off dry runs of the tests. |
| 140 | """ |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 141 | for test_spec in test_specs: |
Aviv Keshet | d993510 | 2019-03-18 14:44:28 -0700 | [diff] [blame] | 142 | test_task_id = _create_test_task( |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 143 | test_spec, |
Xixuan Wu | 5cb5a40 | 2018-06-04 16:37:23 -0700 | [diff] [blame] | 144 | suite_id=suite_id, |
Xixuan Wu | 814ceb6 | 2018-08-27 15:47:34 -0700 | [diff] [blame] | 145 | is_provision=suite_handler.is_provision(), |
Xixuan Wu | 5cb5a40 | 2018-06-04 16:37:23 -0700 | [diff] [blame] | 146 | dry_run=dry_run) |
Xixuan Wu | 9af95a2 | 2018-05-18 10:46:42 -0700 | [diff] [blame] | 147 | suite_handler.add_test_by_task_id( |
| 148 | test_task_id, |
Xixuan Wu | 9d5d703 | 2018-07-12 16:44:02 -0700 | [diff] [blame] | 149 | cros_suite.TestHandlerSpec( |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 150 | test_spec=test_spec, |
| 151 | remaining_retries=test_spec.test.job_retries - 1, |
Xixuan Wu | 9af95a2 | 2018-05-18 10:46:42 -0700 | [diff] [blame] | 152 | previous_retried_ids=[])) |
Xixuan Wu | 56424bc | 2018-05-15 11:03:27 -0700 | [diff] [blame] | 153 | |
Xixuan Wu | 56424bc | 2018-05-15 11:03:27 -0700 | [diff] [blame] | 154 | |
Aviv Keshet | d993510 | 2019-03-18 14:44:28 -0700 | [diff] [blame] | 155 | def _create_test_task(test_spec, suite_id=None, |
| 156 | is_provision=False, dry_run=False): |
| 157 | """Create a test task for a given test spec. |
Xixuan Wu | c7bf77c | 2018-04-24 12:05:40 -0700 | [diff] [blame] | 158 | |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 159 | @param test_spec: A cros_suite.TestSpec object. |
Xixuan Wu | 56424bc | 2018-05-15 11:03:27 -0700 | [diff] [blame] | 160 | @param suite_id: the suite task id of the test. |
Aviv Keshet | f095121 | 2019-03-18 14:54:32 -0700 | [diff] [blame] | 161 | @param dry_run: If true, don't actually create task. |
Xixuan Wu | 56424bc | 2018-05-15 11:03:27 -0700 | [diff] [blame] | 162 | |
| 163 | @return the swarming task id of this task. |
Xixuan Wu | c7bf77c | 2018-04-24 12:05:40 -0700 | [diff] [blame] | 164 | """ |
Aviv Keshet | d993510 | 2019-03-18 14:44:28 -0700 | [diff] [blame] | 165 | logging.info('Creating task for test %s', test_spec.test.name) |
Aviv Keshet | 2c25d06 | 2019-03-19 13:18:23 -0700 | [diff] [blame^] | 166 | skylab_tool_path = os.environ.get('SKYLAB_TOOL', SKYLAB_TOOL) |
Aviv Keshet | f095121 | 2019-03-18 14:54:32 -0700 | [diff] [blame] | 167 | |
| 168 | cmd = [ |
Aviv Keshet | 2c25d06 | 2019-03-19 13:18:23 -0700 | [diff] [blame^] | 169 | skylab_tool_path, 'create-test', |
Aviv Keshet | f095121 | 2019-03-18 14:54:32 -0700 | [diff] [blame] | 170 | '-board', test_spec.board, |
| 171 | '-image', test_spec.build, |
| 172 | ] |
| 173 | if test_spec.pool: |
| 174 | # TODO(akeshet): Clean up this hack around pool name translation. |
| 175 | autotest_pool_label = 'pool:%s' % test_spec.pool |
| 176 | pool_dependency_value = swarming_lib.task_dependencies_from_labels( |
| 177 | [autotest_pool_label])['label-pool'] |
| 178 | cmd += ['-pool', pool_dependency_value] |
| 179 | |
| 180 | if test_spec.model: |
| 181 | cmd += ['-model', test_spec.model] |
| 182 | if test_spec.quota_account: |
| 183 | cmd += ['-qs-account', test_spec.quota_account] |
| 184 | if test_spec.test.test_type.lower() == 'client': |
| 185 | cmd += ['-client-test'] |
| 186 | |
| 187 | tags = _compute_tags(test_spec.build, suite_id) |
| 188 | dimensions = _compute_dimensions( |
| 189 | test_spec.bot_id, test_spec.test.dependencies) |
| 190 | keyvals_flat = _compute_job_keyvals_flat(test_spec.keyvals, suite_id) |
| 191 | |
| 192 | for tag in tags: |
| 193 | cmd += ['-tag', tag] |
| 194 | for keyval in keyvals_flat: |
| 195 | cmd += ['-keyval', keyval] |
| 196 | cmd += [test_spec.test.name] |
| 197 | cmd += dimensions |
| 198 | |
Xixuan Wu | e71c893 | 2018-05-07 17:18:34 -0700 | [diff] [blame] | 199 | if dry_run: |
Aviv Keshet | f095121 | 2019-03-18 14:54:32 -0700 | [diff] [blame] | 200 | logging.info('Would have created task with command %s', cmd) |
| 201 | return |
Xixuan Wu | e71c893 | 2018-05-07 17:18:34 -0700 | [diff] [blame] | 202 | |
Aviv Keshet | f095121 | 2019-03-18 14:54:32 -0700 | [diff] [blame] | 203 | # TODO(akeshet): Avoid this late chromite import. |
| 204 | cros_build_lib = autotest.chromite_load('cros_build_lib') |
| 205 | result = cros_build_lib.RunCommand(cmd, capture_output=True) |
| 206 | # TODO(akeshet): Use -json flag and json-parse output of the command instead |
| 207 | # of regex matching to determine task_id. |
| 208 | m = re.match('.*id=(.*)$', result.output) |
| 209 | task_id = m.group(1) |
| 210 | logging.info('Created task with id %s', task_id) |
| 211 | return task_id |
Xixuan Wu | 3dea7cf | 2018-12-10 17:50:45 -0800 | [diff] [blame] | 212 | |
Aviv Keshet | f095121 | 2019-03-18 14:54:32 -0700 | [diff] [blame] | 213 | |
| 214 | def _compute_tags(build, suite_id): |
| 215 | tags = [ |
| 216 | 'build:%s' % build, |
| 217 | ] |
| 218 | if suite_id is not None: |
| 219 | tags += ['parent_task_id:%s' % suite_id] |
| 220 | return tags |
| 221 | |
| 222 | |
| 223 | def _compute_dimensions(bot_id, dependencies): |
| 224 | dimensions = [] |
| 225 | if bot_id: |
| 226 | dimensions += ['id:%s' % bot_id] |
| 227 | deps = _filter_unsupported_dependencies(dependencies) |
| 228 | flattened_swarming_deps = sorted([ |
| 229 | '%s:%s' % (k, v) for |
| 230 | k, v in swarming_lib.task_dependencies_from_labels(deps).items() |
| 231 | ]) |
| 232 | dimensions += flattened_swarming_deps |
| 233 | return dimensions |
| 234 | |
| 235 | |
| 236 | def _compute_job_keyvals_flat(keyvals, suite_id): |
| 237 | # Job keyvals calculation. |
| 238 | job_keyvals = keyvals.copy() |
| 239 | if suite_id is not None: |
| 240 | # TODO(akeshet): Avoid this late autotest constants import. |
| 241 | constants = autotest.load('server.cros.dynamic_suite.constants') |
| 242 | job_keyvals[constants.PARENT_JOB_ID] = suite_id |
| 243 | keyvals_flat = sorted( |
| 244 | ['%s:%s' % (k, v) for k, v in job_keyvals.items()]) |
| 245 | return keyvals_flat |
| 246 | |
| 247 | |
| 248 | def _filter_unsupported_dependencies(dependencies): |
| 249 | """Filter out Skylab-unsupported test dependencies, with a warning.""" |
Xixuan Wu | db053c8 | 2019-01-31 20:07:06 -0800 | [diff] [blame] | 250 | deps = [] |
Aviv Keshet | f095121 | 2019-03-18 14:54:32 -0700 | [diff] [blame] | 251 | for dep in dependencies: |
Xixuan Wu | 79d1466 | 2018-08-20 11:15:41 -0700 | [diff] [blame] | 252 | if dep in _NOT_SUPPORTED_DEPENDENCIES: |
| 253 | logging.warning('Dependency %s is not supported in skylab', dep) |
Aviv Keshet | f095121 | 2019-03-18 14:54:32 -0700 | [diff] [blame] | 254 | else: |
| 255 | deps.append(dep) |
| 256 | return deps |
Xixuan Wu | 0bea952 | 2018-05-08 17:49:19 -0700 | [diff] [blame] | 257 | |
| 258 | |
Xixuan Wu | 80795c8 | 2018-06-12 11:56:17 -0700 | [diff] [blame] | 259 | @contextlib.contextmanager |
Xixuan Wu | f52e40d | 2018-06-14 12:10:44 -0700 | [diff] [blame] | 260 | def disable_logging(logging_level): |
| 261 | """Context manager for disabling logging of a given logging level.""" |
Xixuan Wu | 80795c8 | 2018-06-12 11:56:17 -0700 | [diff] [blame] | 262 | try: |
Xixuan Wu | f52e40d | 2018-06-14 12:10:44 -0700 | [diff] [blame] | 263 | logging.disable(logging_level) |
Xixuan Wu | 80795c8 | 2018-06-12 11:56:17 -0700 | [diff] [blame] | 264 | yield |
| 265 | finally: |
| 266 | logging.disable(logging.NOTSET) |
| 267 | |
| 268 | |
Xixuan Wu | f52e40d | 2018-06-14 12:10:44 -0700 | [diff] [blame] | 269 | def _loop_and_wait_forever(suite_handler, dry_run): |
| 270 | """Wait for child tasks to finish or break.""" |
| 271 | for iterations in itertools.count(0): |
| 272 | # Log progress every 300 seconds. |
| 273 | no_logging = bool(iterations * SUITE_WAIT_SLEEP_INTERVAL_SECONDS % 300) |
| 274 | with disable_logging(logging.INFO if no_logging else logging.NOTSET): |
Xixuan Wu | c6e28d3 | 2018-08-27 14:48:14 -0700 | [diff] [blame] | 275 | suite_handler.handle_results(suite_handler.suite_id) |
Xixuan Wu | f52e40d | 2018-06-14 12:10:44 -0700 | [diff] [blame] | 276 | if suite_handler.is_finished_waiting(): |
| 277 | break |
Xixuan Wu | 80795c8 | 2018-06-12 11:56:17 -0700 | [diff] [blame] | 278 | |
Xixuan Wu | 4d5d014 | 2018-08-27 15:26:58 -0700 | [diff] [blame] | 279 | for t in suite_handler.retried_tasks: |
| 280 | _retry_test(suite_handler, t['task_id'], dry_run=dry_run) |
| 281 | |
Xixuan Wu | f52e40d | 2018-06-14 12:10:44 -0700 | [diff] [blame] | 282 | time.sleep(SUITE_WAIT_SLEEP_INTERVAL_SECONDS) |
Xixuan Wu | 80795c8 | 2018-06-12 11:56:17 -0700 | [diff] [blame] | 283 | |
| 284 | |
Xixuan Wu | 9af95a2 | 2018-05-18 10:46:42 -0700 | [diff] [blame] | 285 | def _wait_for_results(suite_handler, dry_run=False): |
Xixuan Wu | 2406be3 | 2018-05-14 13:51:30 -0700 | [diff] [blame] | 286 | """Wait for child tasks to finish and return their results. |
Xixuan Wu | 0bea952 | 2018-05-08 17:49:19 -0700 | [diff] [blame] | 287 | |
Xixuan Wu | 9af95a2 | 2018-05-18 10:46:42 -0700 | [diff] [blame] | 288 | @param suite_handler: a cros_suite.SuiteHandler object. |
Xixuan Wu | 2406be3 | 2018-05-14 13:51:30 -0700 | [diff] [blame] | 289 | """ |
| 290 | timeout_util = autotest.chromite_load('timeout_util') |
Xixuan Wu | f52e40d | 2018-06-14 12:10:44 -0700 | [diff] [blame] | 291 | try: |
Xixuan Wu | b098363 | 2018-08-17 17:54:42 -0700 | [diff] [blame] | 292 | with timeout_util.Timeout(suite_handler.timeout_mins * 60 - |
| 293 | suite_handler.passed_mins * 60): |
Xixuan Wu | f52e40d | 2018-06-14 12:10:44 -0700 | [diff] [blame] | 294 | _loop_and_wait_forever(suite_handler, dry_run) |
| 295 | except timeout_util.TimeoutError: |
| 296 | logging.error('Timeout in waiting for child tasks.') |
| 297 | return |
Xixuan Wu | 0bea952 | 2018-05-08 17:49:19 -0700 | [diff] [blame] | 298 | |
| 299 | logging.info('Finished to wait for child tasks.') |
Xixuan Wu | 56424bc | 2018-05-15 11:03:27 -0700 | [diff] [blame] | 300 | |
| 301 | |
Xixuan Wu | 9af95a2 | 2018-05-18 10:46:42 -0700 | [diff] [blame] | 302 | def _retry_test(suite_handler, task_id, dry_run=False): |
Xixuan Wu | 56424bc | 2018-05-15 11:03:27 -0700 | [diff] [blame] | 303 | """Retry test for a suite. |
| 304 | |
| 305 | We will execute the following actions for retrying a test: |
| 306 | 1. Schedule the test. |
| 307 | 2. Add the test with the new swarming task id to the suite's |
| 308 | retry handler, but reduce its remaining retries by 1. |
| 309 | 3. Reduce the suite-level max retries by 1. |
| 310 | 4. Remove prevous failed test from retry handler since it's not |
| 311 | actively monitored by the suite. |
| 312 | |
Xixuan Wu | 9af95a2 | 2018-05-18 10:46:42 -0700 | [diff] [blame] | 313 | @param suite_handler: a cros_suite.SuiteHandler object. |
Xixuan Wu | 56424bc | 2018-05-15 11:03:27 -0700 | [diff] [blame] | 314 | @param task_id: The swarming task id for the retried test. |
| 315 | @param dry_run: Whether to retry a dry run of the test. |
| 316 | """ |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 317 | last_retry_spec = suite_handler.get_test_by_task_id(task_id) |
Xixuan Wu | 56424bc | 2018-05-15 11:03:27 -0700 | [diff] [blame] | 318 | logging.info('Retrying test %s, remaining %d retries.', |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 319 | last_retry_spec.test_spec.test.name, |
| 320 | last_retry_spec.remaining_retries - 1) |
Aviv Keshet | d993510 | 2019-03-18 14:44:28 -0700 | [diff] [blame] | 321 | retried_task_id = _create_test_task( |
Xixuan Wu | b60c143 | 2019-03-07 17:15:39 +0000 | [diff] [blame] | 322 | last_retry_spec.test_spec, |
| 323 | suite_id=suite_handler.suite_id, |
| 324 | is_provision=suite_handler.is_provision(), |
| 325 | dry_run=dry_run) |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 326 | previous_retried_ids = last_retry_spec.previous_retried_ids + [task_id] |
Xixuan Wu | 9af95a2 | 2018-05-18 10:46:42 -0700 | [diff] [blame] | 327 | suite_handler.add_test_by_task_id( |
| 328 | retried_task_id, |
Xixuan Wu | 9d5d703 | 2018-07-12 16:44:02 -0700 | [diff] [blame] | 329 | cros_suite.TestHandlerSpec( |
Xixuan Wu | 5811e83 | 2018-07-12 11:56:24 -0700 | [diff] [blame] | 330 | test_spec=last_retry_spec.test_spec, |
| 331 | remaining_retries=last_retry_spec.remaining_retries - 1, |
Xixuan Wu | 9af95a2 | 2018-05-18 10:46:42 -0700 | [diff] [blame] | 332 | previous_retried_ids=previous_retried_ids)) |
| 333 | suite_handler.set_max_retries(suite_handler.max_retries - 1) |
| 334 | suite_handler.remove_test_by_task_id(task_id) |
Xixuan Wu | 7a450c5 | 2018-07-20 15:07:51 -0700 | [diff] [blame] | 335 | |
| 336 | |
| 337 | def _convert_dict_to_string(input_dict): |
| 338 | """Convert dictionary to a string. |
| 339 | |
| 340 | @param input_dict: A dictionary. |
| 341 | """ |
| 342 | for k, v in input_dict.iteritems(): |
| 343 | if isinstance(v, dict): |
| 344 | input_dict[k] = _convert_dict_to_string(v) |
| 345 | else: |
| 346 | input_dict[k] = str(v) |
| 347 | |
| 348 | return json.dumps(input_dict) |