Chris Masone | 24b80f1 | 2012-02-14 14:18:01 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 7 | |
Chris Masone | 24b80f1 | 2012-02-14 14:18:01 -0800 | [diff] [blame] | 8 | """Tool for running suites of tests and waiting for completion. |
| 9 | |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 10 | The desired test suite will be scheduled with autotest. By default, |
Chris Masone | 24b80f1 | 2012-02-14 14:18:01 -0800 | [diff] [blame] | 11 | this tool will block until the job is complete, printing a summary |
| 12 | at the end. Error conditions result in exceptions. |
| 13 | |
| 14 | This is intended for use only with Chrome OS test suits that leverage the |
| 15 | dynamic suite infrastructure in server/cros/dynamic_suite.py. |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 16 | |
| 17 | This script exits with one of the following codes: |
| 18 | 0 - OK: Suite finished successfully |
| 19 | 1 - ERROR: Test(s) failed, or hits its own timeout |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 20 | 2 - WARNING: Test(s) raised a warning or passed on retry, none failed/timed out. |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 21 | 3 - INFRA_FAILURE: Infrastructure related issues, e.g. |
| 22 | * Lab is down |
| 23 | * Too many duts (defined as a constant) in repair failed status |
| 24 | * Suite job issues, like bug in dynamic suite, |
| 25 | user aborted the suite, lose a drone/all devservers/rpc server, |
| 26 | 0 tests ran, etc. |
Fang Deng | 95af42f | 2014-09-12 14:16:11 -0700 | [diff] [blame] | 27 | * provision failed |
| 28 | TODO(fdeng): crbug.com/413918, reexamine treating all provision |
| 29 | failures as INFRA failures. |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 30 | 4 - SUITE_TIMEOUT: Suite timed out, some tests ran, |
| 31 | none failed by the time the suite job was aborted. This will cover, |
| 32 | but not limited to, the following cases: |
| 33 | * A devserver failure that manifests as a timeout |
| 34 | * No DUTs available midway through a suite |
| 35 | * Provision/Reset/Cleanup took longer time than expected for new image |
| 36 | * A regression in scheduler tick time. |
Fang Deng | 6197da3 | 2014-09-25 10:18:48 -0700 | [diff] [blame] | 37 | 5- BOARD_NOT_AVAILABLE: If there is no host for the requested board/pool. |
| 38 | 6- INVALID_OPTIONS: If options are not valid. |
Chris Masone | 24b80f1 | 2012-02-14 14:18:01 -0800 | [diff] [blame] | 39 | """ |
| 40 | |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 41 | import argparse |
| 42 | import ast |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 43 | import collections |
Chris Masone | cfa7efc | 2012-09-06 16:00:07 -0700 | [diff] [blame] | 44 | from datetime import datetime |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 45 | from datetime import timedelta |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 46 | import functools |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 47 | import getpass |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 48 | import logging |
| 49 | import os |
| 50 | import re |
| 51 | import sys |
| 52 | import time |
Allen Li | 04afc8f | 2017-11-27 15:36:34 -0800 | [diff] [blame] | 53 | import warnings |
Chris Masone | cfa7efc | 2012-09-06 16:00:07 -0700 | [diff] [blame] | 54 | |
Chris Masone | 24b80f1 | 2012-02-14 14:18:01 -0800 | [diff] [blame] | 55 | import common |
Allen Li | e082ced | 2016-09-14 15:19:20 -0700 | [diff] [blame] | 56 | from chromite.lib import buildbot_annotations as annotations |
| 57 | |
Aviv Keshet | 9eb5c89 | 2018-07-16 13:44:42 -0700 | [diff] [blame] | 58 | from django.core import exceptions as django_exceptions |
| 59 | |
Shuqian Zhao | 2fecacd | 2015-08-05 22:56:30 -0700 | [diff] [blame] | 60 | from autotest_lib.client.common_lib import control_data |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 61 | from autotest_lib.client.common_lib import error |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 62 | from autotest_lib.client.common_lib import global_config |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 63 | from autotest_lib.client.common_lib import priorities |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 64 | from autotest_lib.client.common_lib import time_utils |
Prashanth B | 6285f6a | 2014-05-08 18:01:27 -0700 | [diff] [blame] | 65 | from autotest_lib.client.common_lib.cros import retry |
Prathmesh Prabhu | cd246f5 | 2018-01-03 13:45:48 -0800 | [diff] [blame] | 66 | from autotest_lib.frontend.afe import rpc_client_lib |
Prashanth B | 923ca26 | 2014-03-14 12:36:29 -0700 | [diff] [blame] | 67 | from autotest_lib.frontend.afe.json_rpc import proxy |
xixuan | ae791b1 | 2017-06-29 15:40:19 -0700 | [diff] [blame] | 68 | from autotest_lib.server import site_utils |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 69 | from autotest_lib.server import utils |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 70 | from autotest_lib.server.cros.dynamic_suite import constants |
Chris Masone | b493555 | 2012-08-14 12:05:54 -0700 | [diff] [blame] | 71 | from autotest_lib.server.cros.dynamic_suite import frontend_wrappers |
Prashanth B | 923ca26 | 2014-03-14 12:36:29 -0700 | [diff] [blame] | 72 | from autotest_lib.server.cros.dynamic_suite import reporting_utils |
Xixuan Wu | 7cc10e5 | 2018-04-25 17:04:51 -0700 | [diff] [blame] | 73 | from autotest_lib.server.cros.dynamic_suite import suite_common |
J. Richard Barnette | e7b98bb | 2013-08-21 16:34:16 -0700 | [diff] [blame] | 74 | from autotest_lib.server.cros.dynamic_suite import tools |
Aviv Keshet | 9eb5c89 | 2018-07-16 13:44:42 -0700 | [diff] [blame] | 75 | try: |
| 76 | from autotest_lib.site_utils import diagnosis_utils |
| 77 | except django_exceptions.ImproperlyConfigured as e: |
| 78 | if 'Error loading MySQLdb module: libmariadbclient' in str(e): |
| 79 | logging.error('Unable to import a necessary MySQLdb module. This is ' |
| 80 | 'commonly caused by running a command inside[outside] ' |
| 81 | 'of the chroot but having autotest utility packages ' |
| 82 | 'that were build outside[inside] the chroot. ' |
| 83 | 'Please re-run utils/build_externals.py inside[outside] ' |
| 84 | 'of the chroot accordingly.') |
| 85 | raise |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 86 | from autotest_lib.site_utils import run_suite_common |
MK Ryu | 977a975 | 2014-10-21 11:58:09 -0700 | [diff] [blame] | 87 | |
Chris Masone | 1120cdf | 2012-02-27 17:35:07 -0800 | [diff] [blame] | 88 | CONFIG = global_config.global_config |
| 89 | |
Allen Li | dc2c69a | 2016-09-14 19:05:47 -0700 | [diff] [blame] | 90 | _DEFAULT_AUTOTEST_INSTANCE = CONFIG.get_config_value( |
| 91 | 'SERVER', 'hostname', type=str) |
| 92 | _URL_PATTERN = CONFIG.get_config_value('CROS', 'log_url_pattern', type=str) |
Simran Basi | 7203d4e | 2015-02-03 15:50:18 -0800 | [diff] [blame] | 93 | |
xixuan | ae791b1 | 2017-06-29 15:40:19 -0700 | [diff] [blame] | 94 | # Minimum RPC timeout setting for calls expected to take long time, e.g., |
| 95 | # create_suite_job. If default socket time (socket.getdefaulttimeout()) is |
| 96 | # None or greater than this value, the default will be used. |
| 97 | # The value here is set to be the same as the timeout for the RetryingAFE object |
| 98 | # so long running RPCs can wait long enough before being aborted. |
| 99 | _MIN_RPC_TIMEOUT = 600 |
| 100 | |
| 101 | # Number of days back to search for existing job. |
| 102 | _SEARCH_JOB_MAX_DAYS = 14 |
| 103 | |
Allen Li | 977760b | 2017-11-06 18:11:37 -0800 | [diff] [blame] | 104 | _PROVISION_SUITE = 'provision' |
| 105 | |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 106 | |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 107 | @functools.total_ordering |
| 108 | class _ReturnResult(object): |
| 109 | """Represents overall result of run_suite operation. |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 110 | |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 111 | _ReturnResult instances sort based on priority (the order in |
| 112 | _RETURN_RESULTS). |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 113 | |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 114 | Furthermore, _ReturnResult instances can be combined by bitwise or |
| 115 | ("union"), which returns the instance with the higher priority |
| 116 | between the two (the instance with higher priority is a "superset" |
| 117 | of the other). |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 118 | |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 119 | Do not create new instances of this; use _RETURN_RESULTS instead. |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 120 | """ |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 121 | |
| 122 | def __init__(self, return_code, message): |
| 123 | self.return_code = return_code |
| 124 | self.message = message |
| 125 | |
| 126 | def __repr__(self): |
| 127 | return '<{cls} {key}, {this.return_code}, {this.message}>'.format( |
| 128 | cls=type(self).__name__, |
| 129 | key=self._getkey(), |
| 130 | this=self) |
| 131 | |
| 132 | def __gt__(self, other): |
| 133 | if isinstance(other, type(self)): |
| 134 | return self._getkey() > other._getkey() |
| 135 | else: |
| 136 | return NotImplemented |
| 137 | |
| 138 | def __eq__(self, other): |
| 139 | if isinstance(other, type(self)): |
| 140 | return (self.return_code == other.return_code |
| 141 | and self.message == other.message) |
| 142 | else: |
| 143 | return NotImplemented |
| 144 | |
| 145 | def __hash__(self): |
| 146 | return hash(self.return_code) ^ hash(self.message) |
| 147 | |
| 148 | def __or__(self, other): |
| 149 | if isinstance(other, type(self)): |
| 150 | if self > other: |
| 151 | return self |
| 152 | else: |
| 153 | return other |
| 154 | else: |
| 155 | return NotImplemented |
| 156 | |
| 157 | def _getkey(self): |
| 158 | """Return sort key.""" |
| 159 | return _RETURN_RESULTS_LIST.index(self) |
| 160 | |
| 161 | def suite_result(self, output_dict=None): |
| 162 | """Make a SuiteResult using this _ReturnResult. |
| 163 | |
| 164 | @param output_dict: output_dict to merge into SuiteResult. |
| 165 | """ |
| 166 | if output_dict is None: |
| 167 | output_dict = dict() |
| 168 | else: |
| 169 | output_dict = output_dict.copy() |
| 170 | if self.message: |
| 171 | output_dict['return_message'] = self.message |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 172 | return run_suite_common.SuiteResult(self.return_code, output_dict) |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 173 | |
| 174 | |
| 175 | _RETURN_RESULTS = collections.OrderedDict([ |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 176 | ('ok', _ReturnResult(run_suite_common.RETURN_CODES.OK, '')), |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 177 | |
| 178 | ('test_warning', _ReturnResult( |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 179 | run_suite_common.RETURN_CODES.WARNING, 'Test job raised warning.')), |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 180 | ('suite_warning', _ReturnResult( |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 181 | run_suite_common.RETURN_CODES.WARNING, 'Suite job raised warning.')), |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 182 | ('test_retry', _ReturnResult( |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 183 | run_suite_common.RETURN_CODES.WARNING, 'Tests were retried.')), |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 184 | |
Prathmesh Prabhu | 316180c | 2017-12-19 16:06:44 -0800 | [diff] [blame] | 185 | ('test_aborted_prestart', _ReturnResult( |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 186 | run_suite_common.RETURN_CODES.SUITE_TIMEOUT, |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 187 | 'Tests were aborted before running; suite must have timed out.')), |
Prathmesh Prabhu | 316180c | 2017-12-19 16:06:44 -0800 | [diff] [blame] | 188 | # This really indicates a user action or an infra failure. But, suite |
| 189 | # timeouts cause similar fauilres in the individual tests, so we must |
| 190 | # classify these lower than suite_timeout. In case of a suite_timeout, the |
| 191 | # result from the suite job will promote the result to suite_timeout. |
| 192 | ('test_aborted_mystery', |
| 193 | _ReturnResult( |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 194 | run_suite_common.RETURN_CODES.SUITE_TIMEOUT, |
Prathmesh Prabhu | 316180c | 2017-12-19 16:06:44 -0800 | [diff] [blame] | 195 | 'Tests were aborted after running, but before timeout; ' |
| 196 | 'Test was manually aborted or parsing results failed: ' |
| 197 | 'crbug.com/796348.')), |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 198 | ('suite_timeout', _ReturnResult( |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 199 | run_suite_common.RETURN_CODES.SUITE_TIMEOUT, 'Suite job timed out.')), |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 200 | |
| 201 | ('test_views_missing', _ReturnResult( |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 202 | run_suite_common.RETURN_CODES.INFRA_FAILURE, 'No test views found.')), |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 203 | ('suite_failed', _ReturnResult( |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 204 | run_suite_common.RETURN_CODES.INFRA_FAILURE, 'Suite job failed.')), |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 205 | ('provision_failed', _ReturnResult( |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 206 | run_suite_common.RETURN_CODES.INFRA_FAILURE, 'Provisioning failed.')), |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 207 | |
| 208 | ('test_failure', _ReturnResult( |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 209 | run_suite_common.RETURN_CODES.ERROR, 'Tests failed.')), |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 210 | ]) |
| 211 | _RETURN_RESULTS_LIST = list(_RETURN_RESULTS.values()) |
Simran Basi | 22aa9fe | 2012-12-07 16:37:09 -0800 | [diff] [blame] | 212 | |
Chris Masone | dfa0beba | 2012-03-19 11:41:47 -0700 | [diff] [blame] | 213 | |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 214 | def bool_str(x): |
| 215 | """Boolean string type for option arguments. |
| 216 | |
| 217 | @param x: string representation of boolean value. |
| 218 | |
| 219 | """ |
| 220 | if x == 'True': |
| 221 | return True |
| 222 | elif x == 'False': |
| 223 | return False |
| 224 | else: |
| 225 | raise argparse.ArgumentTypeError( |
| 226 | '%s is not one of True or False' % (x,)) |
| 227 | |
| 228 | |
Allen Li | 603728a | 2016-12-08 13:58:11 -0800 | [diff] [blame] | 229 | def _get_priority_value(x): |
| 230 | """Convert a priority representation to its int value. |
| 231 | |
| 232 | Priorities can be described either by an int value (possibly as a string) |
| 233 | or a name string. This function coerces both forms to an int value. |
| 234 | |
| 235 | This function is intended for casting command line arguments during |
| 236 | parsing. |
| 237 | |
| 238 | @param x: priority value as an int, int string, or name string |
| 239 | |
| 240 | @returns: int value of priority |
| 241 | """ |
| 242 | try: |
| 243 | return int(x) |
| 244 | except ValueError: |
| 245 | try: |
| 246 | return priorities.Priority.get_value(x) |
| 247 | except AttributeError: |
| 248 | raise argparse.ArgumentTypeError( |
| 249 | 'Unknown priority level %s. Try one of %s.' |
| 250 | % (x, ', '.join(priorities.Priority.names))) |
| 251 | |
| 252 | |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 253 | def make_parser(): |
| 254 | """Make ArgumentParser instance for run_suite.py.""" |
| 255 | parser = argparse.ArgumentParser( |
| 256 | usage="%(prog)s [options]") |
| 257 | parser.add_argument("-b", "--board", dest="board") |
Prathmesh Prabhu | 9b8e7ad | 2017-10-30 14:26:13 -0700 | [diff] [blame] | 258 | parser.add_argument( |
| 259 | "--model", |
| 260 | help="The device model to run tests against. For non-unified " |
| 261 | "builds, model and board are synonymous, but board is more " |
| 262 | "accurate in some cases. Only pass this option if your build " |
| 263 | "is a unified build.", |
| 264 | ) |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 265 | parser.add_argument("-i", "--build", dest="build") |
| 266 | parser.add_argument( |
| 267 | "-w", "--web", dest="web", default=None, |
| 268 | help="Address of a webserver to receive suite requests.") |
| 269 | parser.add_argument( |
Rohit Makasana | df0a3a3 | 2017-06-30 13:55:18 -0700 | [diff] [blame] | 270 | '--cheets_build', dest='cheets_build', default=None, |
| 271 | help='ChromeOS Android build to be installed on dut.') |
| 272 | parser.add_argument( |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 273 | '--firmware_rw_build', dest='firmware_rw_build', default=None, |
| 274 | help='Firmware build to be installed in dut RW firmware.') |
| 275 | parser.add_argument( |
| 276 | '--firmware_ro_build', dest='firmware_ro_build', default=None, |
| 277 | help='Firmware build to be installed in dut RO firmware.') |
| 278 | parser.add_argument( |
| 279 | '--test_source_build', dest='test_source_build', default=None, |
| 280 | help=('Build that contains the test code, ' |
| 281 | 'e.g., it can be the value of `--build`, ' |
| 282 | '`--firmware_rw_build` or `--firmware_ro_build` ' |
| 283 | 'arguments. Default is None, that is, use the test ' |
| 284 | 'code from `--build` (CrOS image)')) |
Chris Masone | 359c0fd | 2012-03-13 15:18:59 -0700 | [diff] [blame] | 285 | # This should just be a boolean flag, but the autotest "proxy" code |
| 286 | # can't handle flags that don't take arguments. |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 287 | parser.add_argument( |
| 288 | "-n", "--no_wait", dest="no_wait", default=False, type=bool_str, |
| 289 | help='Must pass "True" or "False" if used.') |
Alex Miller | 0032e93 | 2013-10-23 12:52:58 -0700 | [diff] [blame] | 290 | # If you really want no pool, --pool="" will do it. USE WITH CARE. |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 291 | parser.add_argument("-p", "--pool", dest="pool", default="suites") |
| 292 | parser.add_argument("-s", "--suite_name", dest="name") |
| 293 | parser.add_argument("-a", "--afe_timeout_mins", type=int, |
| 294 | dest="afe_timeout_mins", default=30) |
| 295 | parser.add_argument("-t", "--timeout_mins", type=int, |
| 296 | dest="timeout_mins", default=1440) |
| 297 | parser.add_argument("-x", "--max_runtime_mins", type=int, |
| 298 | dest="max_runtime_mins", default=1440) |
| 299 | parser.add_argument("-d", "--delay_sec", type=int, |
| 300 | dest="delay_sec", default=10) |
| 301 | parser.add_argument("-m", "--mock_job_id", dest="mock_job_id", |
| 302 | help="Attach to existing job id for already running " |
| 303 | "suite, and creates report.") |
Aviv Keshet | db321de | 2015-04-10 19:09:58 -0700 | [diff] [blame] | 304 | # NOTE(akeshet): This looks similar to --no_wait, but behaves differently. |
| 305 | # --no_wait is passed in to the suite rpc itself and affects the suite, |
| 306 | # while this does not. |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 307 | parser.add_argument("-c", "--create_and_return", dest="create_and_return", |
| 308 | action="store_true", |
| 309 | help="Create the suite and print the job id, then " |
| 310 | "finish immediately.") |
| 311 | parser.add_argument("-u", "--num", dest="num", type=int, default=None, |
Allen Li | 04afc8f | 2017-11-27 15:36:34 -0800 | [diff] [blame] | 312 | help="Deprecated, does nothing.") |
Alex Miller | f43d0eb | 2012-10-01 13:43:13 -0700 | [diff] [blame] | 313 | # Same boolean flag issue applies here. |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 314 | parser.add_argument( |
| 315 | "-f", "--file_bugs", dest="file_bugs", default=False, type=bool_str, |
| 316 | help=('File bugs on test failures. Must pass "True" or ' |
| 317 | '"False" if used.')) |
| 318 | parser.add_argument("-l", "--bypass_labstatus", dest="bypass_labstatus", |
| 319 | action="store_true", help='Bypass lab status check.') |
Alex Miller | 88762a8 | 2013-09-04 15:41:28 -0700 | [diff] [blame] | 320 | # We allow either a number or a string for the priority. This way, if you |
| 321 | # know what you're doing, one can specify a custom priority level between |
| 322 | # other levels. |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 323 | parser.add_argument("-r", "--priority", dest="priority", |
Allen Li | 603728a | 2016-12-08 13:58:11 -0800 | [diff] [blame] | 324 | type=_get_priority_value, |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 325 | default=priorities.Priority.DEFAULT, |
| 326 | action="store", |
| 327 | help="Priority of suite. Either numerical value, or " |
| 328 | "one of (" + ", ".join(priorities.Priority.names) |
| 329 | + ").") |
| 330 | parser.add_argument( |
| 331 | '--retry', dest='retry', default=False, type=bool_str, action='store', |
| 332 | help='Enable test retry. Must pass "True" or "False" if used.') |
| 333 | parser.add_argument('--max_retries', dest='max_retries', default=None, |
| 334 | type=int, action='store', help='Maximum retries' |
| 335 | 'allowed at suite level. No limit if not specified.') |
| 336 | parser.add_argument('--minimum_duts', dest='minimum_duts', type=int, |
| 337 | default=0, action='store', |
| 338 | help='Check that the pool has at least such many ' |
| 339 | 'healthy machines, otherwise suite will not run. ' |
| 340 | 'Default to 0.') |
| 341 | parser.add_argument('--suite_min_duts', dest='suite_min_duts', type=int, |
| 342 | default=0, action='store', |
| 343 | help='Preferred minimum number of machines. Scheduler ' |
| 344 | 'will prioritize on getting such many machines for ' |
| 345 | 'the suite when it is competing with another suite ' |
| 346 | 'that has a higher priority but already got minimum ' |
| 347 | 'machines it needs. Default to 0.') |
| 348 | parser.add_argument("--suite_args", dest="suite_args", |
Allen Li | ecdba6c | 2017-07-11 12:10:26 -0700 | [diff] [blame] | 349 | type=ast.literal_eval, |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 350 | default=None, action="store", |
Allen Li | ecdba6c | 2017-07-11 12:10:26 -0700 | [diff] [blame] | 351 | help="A dict of args passed to the suite control file.") |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 352 | parser.add_argument('--offload_failures_only', |
Allen Li | 40599a3 | 2016-12-08 13:23:35 -0800 | [diff] [blame] | 353 | dest='offload_failures_only', type=bool_str, |
| 354 | action='store', default=False, |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 355 | help='Only enable gs_offloading for failed tests. ' |
| 356 | 'Successful tests will be deleted. Must pass "True"' |
| 357 | ' or "False" if used.') |
| 358 | parser.add_argument('--use_suite_attr', dest='use_suite_attr', |
| 359 | action='store_true', default=False, |
| 360 | help='Advanced. Run the suite based on ATTRIBUTES of ' |
| 361 | 'control files, rather than SUITE.') |
| 362 | parser.add_argument('--json_dump', dest='json_dump', action='store_true', |
| 363 | default=False, |
| 364 | help='Dump the output of run_suite to stdout.') |
| 365 | parser.add_argument( |
| 366 | '--run_prod_code', dest='run_prod_code', |
| 367 | action='store_true', default=False, |
| 368 | help='Run the test code that lives in prod aka the test ' |
| 369 | 'code currently on the lab servers.') |
| 370 | parser.add_argument( |
| 371 | '--delay_minutes', type=int, default=0, |
| 372 | help=('Delay the creation of test jobs for a given ' |
| 373 | 'number of minutes. This argument can be used to ' |
| 374 | 'force provision jobs being delayed, which helps ' |
| 375 | 'to distribute loads across devservers.')) |
| 376 | parser.add_argument( |
| 377 | '--skip_duts_check', dest='skip_duts_check', action='store_true', |
| 378 | default=False, help='If True, skip minimum available DUTs check') |
Shuqian Zhao | 843ae5c7 | 2017-02-22 11:25:01 -0800 | [diff] [blame] | 379 | parser.add_argument( |
Shuqian Zhao | 637d22c | 2017-03-06 15:52:32 -0800 | [diff] [blame] | 380 | '--job_keyvals', dest='job_keyvals', type=ast.literal_eval, |
Shuqian Zhao | 843ae5c7 | 2017-02-22 11:25:01 -0800 | [diff] [blame] | 381 | action='store', default=None, |
| 382 | help='A dict of job keyvals to be inject to suite control file') |
Shuqian Zhao | ed0da86 | 2017-03-06 14:47:13 -0800 | [diff] [blame] | 383 | parser.add_argument( |
| 384 | '--test_args', dest='test_args', type=ast.literal_eval, |
| 385 | action='store', default=None, |
| 386 | help=('A dict of args passed all the way to each individual test that ' |
| 387 | 'will be actually ran.')) |
xixuan | d3cb33d | 2017-07-07 14:47:53 -0700 | [diff] [blame] | 388 | parser.add_argument( |
xixuan | 99eba0b | 2017-07-12 15:10:01 -0700 | [diff] [blame] | 389 | '--require_logfile', action='store_true', |
xixuan | d3cb33d | 2017-07-07 14:47:53 -0700 | [diff] [blame] | 390 | help=('Stream logs of run_suite.py to a local file named ' |
| 391 | 'run_suite-<build name>.log.')) |
Aviv Keshet | 97bebd4 | 2017-05-24 21:02:32 -0700 | [diff] [blame] | 392 | |
| 393 | # Used for monitoring purposes, to measure no-op swarming proxy latency. |
| 394 | parser.add_argument('--do_nothing', action='store_true', |
| 395 | help=argparse.SUPPRESS) |
| 396 | |
xixuan | ae791b1 | 2017-06-29 15:40:19 -0700 | [diff] [blame] | 397 | # Used when lab/job status checking is needed. Currently its only user is |
| 398 | # suite scheduler v2. |
| 399 | parser.add_argument( |
| 400 | '--pre_check', action='store_true', |
| 401 | help=('Check lab and job status before kicking off a suite. Used by ' |
| 402 | 'suite scheduler v2.')) |
| 403 | |
Allen Li | 02b46c5 | 2017-09-11 11:48:12 -0700 | [diff] [blame] | 404 | # TODO(crbug.com/763207): This is to support calling old moblab RPC |
| 405 | # with ToT code. This does not need to be supported after M62. |
| 406 | parser.add_argument('--oldrpc', action='store_true', |
| 407 | help='Use old AFE RPC.') |
| 408 | |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 409 | return parser |
Chris Masone | 24b80f1 | 2012-02-14 14:18:01 -0800 | [diff] [blame] | 410 | |
| 411 | |
Allen Li | 85ae5df | 2017-07-10 14:58:16 -0700 | [diff] [blame] | 412 | def verify_and_clean_options(options): |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 413 | """Verify the validity of options. |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 414 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 415 | @param options: The parsed options to verify. |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 416 | |
| 417 | @returns: True if verification passes, False otherwise. |
| 418 | |
| 419 | """ |
Fang Deng | 6865aab | 2015-02-20 14:49:47 -0800 | [diff] [blame] | 420 | if options.mock_job_id and ( |
| 421 | not options.build or not options.name or not options.board): |
| 422 | print ('When using -m, need to specify build, board and suite ' |
| 423 | 'name which you have used for creating the original job') |
| 424 | return False |
| 425 | else: |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 426 | if not options.build: |
| 427 | print 'Need to specify which build to use' |
| 428 | return False |
| 429 | if not options.board: |
| 430 | print 'Need to specify board' |
| 431 | return False |
| 432 | if not options.name: |
| 433 | print 'Need to specify suite name' |
| 434 | return False |
Allen Li | 04afc8f | 2017-11-27 15:36:34 -0800 | [diff] [blame] | 435 | if options.num is not None: |
| 436 | warnings.warn('-u/--num option is deprecated; it does nothing.') |
| 437 | del options.num |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 438 | if not options.retry and options.max_retries is not None: |
Fang Deng | 443f195 | 2015-01-02 14:51:49 -0800 | [diff] [blame] | 439 | print 'max_retries can only be used with --retry=True' |
| 440 | return False |
Shuqian Zhao | ab1bedc | 2015-06-02 11:12:28 -0700 | [diff] [blame] | 441 | if options.use_suite_attr and options.suite_args is not None: |
| 442 | print ('The new suite control file cannot parse the suite_args: %s.' |
| 443 | 'Please not specify any suite_args here.' % options.suite_args) |
| 444 | return False |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 445 | if options.no_wait and options.retry: |
Fang Deng | 058860c | 2014-05-15 15:41:50 -0700 | [diff] [blame] | 446 | print 'Test retry is not available when using --no_wait=True' |
Dan Shi | 36cfd83 | 2014-10-10 13:38:51 -0700 | [diff] [blame] | 447 | # Default to use the test code in CrOS build. |
| 448 | if not options.test_source_build and options.build: |
| 449 | options.test_source_build = options.build |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 450 | return True |
| 451 | |
| 452 | |
Shuqian Zhao | ab1bedc | 2015-06-02 11:12:28 -0700 | [diff] [blame] | 453 | def change_options_for_suite_attr(options): |
| 454 | """Change options to be prepared to run the suite_attr_wrapper. |
| 455 | |
| 456 | If specify 'use_suite_attr' from the cmd line, it indicates to run the |
| 457 | new style suite control file, suite_attr_wrapper. Then, change the |
Allen Li | 6a61239 | 2016-08-18 12:09:32 -0700 | [diff] [blame] | 458 | options.name to 'suite_attr_wrapper', change the options.suite_args to |
Shuqian Zhao | ab1bedc | 2015-06-02 11:12:28 -0700 | [diff] [blame] | 459 | include the arguments needed by suite_attr_wrapper. |
| 460 | |
| 461 | @param options: The verified options. |
| 462 | |
| 463 | @returns: The changed options. |
| 464 | |
| 465 | """ |
| 466 | # Convert the suite_name to attribute boolean expression. |
| 467 | if type(options.name) is str: |
| 468 | attr_filter_val = 'suite:%s' % options.name |
| 469 | else: |
| 470 | attr_filter_val = ' or '.join(['suite:%s' % x for x in options.name]) |
| 471 | |
| 472 | # change the suite_args to be a dict of arguments for suite_attr_wrapper |
| 473 | # if suite_args is not None, store the values in 'other_args' of the dict |
| 474 | args_dict = {} |
| 475 | args_dict['attr_filter'] = attr_filter_val |
Allen Li | ecdba6c | 2017-07-11 12:10:26 -0700 | [diff] [blame] | 476 | options.suite_args = args_dict |
Shuqian Zhao | ab1bedc | 2015-06-02 11:12:28 -0700 | [diff] [blame] | 477 | options.name = 'suite_attr_wrapper' |
| 478 | |
| 479 | return options |
| 480 | |
| 481 | |
Allen Li | 3461324 | 2016-09-02 11:52:34 -0700 | [diff] [blame] | 482 | class TestResult(object): |
Aviv Keshet | 1480c4a | 2013-03-21 16:38:31 -0700 | [diff] [blame] | 483 | |
Allen Li | 3461324 | 2016-09-02 11:52:34 -0700 | [diff] [blame] | 484 | """Represents the result of a TestView.""" |
Aviv Keshet | 1480c4a | 2013-03-21 16:38:31 -0700 | [diff] [blame] | 485 | |
Allen Li | 3461324 | 2016-09-02 11:52:34 -0700 | [diff] [blame] | 486 | def __init__(self, test_view, retry_count=0): |
| 487 | """Initialize instance. |
| 488 | |
| 489 | @param test_view: TestView instance. |
| 490 | @param retry_count: Retry count for test. Optional. |
| 491 | """ |
| 492 | self.name = test_view.get_testname() |
| 493 | self.status = test_view['status'] |
| 494 | self.reason = test_view['reason'] |
| 495 | self.retry_count = retry_count |
| 496 | |
| 497 | _PRETTY_STATUS_MAP = { |
| 498 | 'GOOD': '[ PASSED ]', |
| 499 | 'TEST_NA': '[ INFO ]', |
| 500 | } |
| 501 | |
| 502 | @property |
| 503 | def _pretty_status(self): |
| 504 | """Pretty status string.""" |
| 505 | return self._PRETTY_STATUS_MAP.get(self.status, '[ FAILED ]') |
| 506 | |
| 507 | def log_using(self, log_function, name_column_width): |
| 508 | """Log the test result using the given log function. |
| 509 | |
| 510 | @param log_function: Log function to use. Example: logging.info |
| 511 | @param name_column_width: Width of name column for formatting. |
| 512 | """ |
| 513 | padded_name = self.name.ljust(name_column_width) |
| 514 | log_function('%s%s', padded_name, self._pretty_status) |
| 515 | if self.status != 'GOOD': |
| 516 | log_function('%s %s: %s', padded_name, self.status, self.reason) |
| 517 | if self.retry_count > 0: |
| 518 | log_function('%s retry_count: %s', padded_name, self.retry_count) |
Chris Masone | 24b80f1 | 2012-02-14 14:18:01 -0800 | [diff] [blame] | 519 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 520 | |
Shuqian Zhao | f39bf2a | 2015-09-29 14:19:28 -0700 | [diff] [blame] | 521 | def get_original_suite_name(suite_name, suite_args): |
| 522 | """Get the original suite name when running suite_attr_wrapper. |
| 523 | |
| 524 | @param suite_name: the name of the suite launched in afe. When it is |
| 525 | suite_attr_wrapper, the suite that actually running is |
| 526 | specified in the suite_args. |
Allen Li | ecdba6c | 2017-07-11 12:10:26 -0700 | [diff] [blame] | 527 | @param suite_args: dict of suite args from argument parsing. |
Shuqian Zhao | f39bf2a | 2015-09-29 14:19:28 -0700 | [diff] [blame] | 528 | |
| 529 | @returns: the original suite name. |
| 530 | |
| 531 | """ |
| 532 | if suite_name == 'suite_attr_wrapper': |
Allen Li | ecdba6c | 2017-07-11 12:10:26 -0700 | [diff] [blame] | 533 | attrs = suite_args.get('attr_filter', '') |
Shuqian Zhao | f39bf2a | 2015-09-29 14:19:28 -0700 | [diff] [blame] | 534 | suite_list = ([x[6:] for x in re.split('[() ]', attrs) |
| 535 | if x and x.startswith('suite:')]) |
| 536 | return suite_list[0] if suite_list else suite_name |
| 537 | return suite_name |
| 538 | |
| 539 | |
Craig Harrison | 25eb0f3 | 2012-08-23 16:48:49 -0700 | [diff] [blame] | 540 | class LogLink(object): |
J. Richard Barnette | b9c911d | 2013-08-23 11:24:21 -0700 | [diff] [blame] | 541 | """Information needed to record a link in the logs. |
Craig Harrison | 25eb0f3 | 2012-08-23 16:48:49 -0700 | [diff] [blame] | 542 | |
J. Richard Barnette | b9c911d | 2013-08-23 11:24:21 -0700 | [diff] [blame] | 543 | Depending on context and the information provided at |
| 544 | construction time, the link may point to either to log files for |
| 545 | a job, or to a bug filed for a failure in the job. |
Craig Harrison | 25eb0f3 | 2012-08-23 16:48:49 -0700 | [diff] [blame] | 546 | |
J. Richard Barnette | b9c911d | 2013-08-23 11:24:21 -0700 | [diff] [blame] | 547 | @var anchor The link text. |
| 548 | @var url The link url. |
| 549 | @var bug_id Id of a bug to link to, or None. |
| 550 | """ |
| 551 | |
Kevin Cheng | 2bdd372 | 2016-03-24 21:30:52 -0700 | [diff] [blame] | 552 | # A list of tests that don't get retried so skip the dashboard. |
| 553 | _SKIP_RETRY_DASHBOARD = ['provision'] |
| 554 | |
Ningning Xia | bd911bd | 2016-04-19 14:06:03 -0700 | [diff] [blame] | 555 | _BUG_LINK_PREFIX = 'Auto-Bug' |
| 556 | _LOG_LINK_PREFIX = 'Test-Logs' |
| 557 | |
J. Richard Barnette | b9c911d | 2013-08-23 11:24:21 -0700 | [diff] [blame] | 558 | |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 559 | def __init__(self, anchor, server, job_string, bug_info=None, reason=None, |
Dan Shi | 9b620c2 | 2017-10-10 10:58:37 -0700 | [diff] [blame] | 560 | retry_count=0, testname=None, sponge_url=None): |
J. Richard Barnette | b9c911d | 2013-08-23 11:24:21 -0700 | [diff] [blame] | 561 | """Initialize the LogLink by generating the log URL. |
| 562 | |
| 563 | @param anchor The link text. |
Alex Miller | c7a5952 | 2013-10-30 15:18:57 -0700 | [diff] [blame] | 564 | @param server The hostname of the server this suite ran on. |
J. Richard Barnette | b9c911d | 2013-08-23 11:24:21 -0700 | [diff] [blame] | 565 | @param job_string The job whose logs we'd like to link to. |
| 566 | @param bug_info Info about the bug, if one was filed. |
Fang Deng | 53c6ff5 | 2014-02-24 17:51:24 -0800 | [diff] [blame] | 567 | @param reason A string representing the reason of failure if any. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 568 | @param retry_count How many times the test has been retried. |
Simran Basi | 7203d4e | 2015-02-03 15:50:18 -0800 | [diff] [blame] | 569 | @param testname Optional Arg that supplies the testname. |
Dan Shi | 9b620c2 | 2017-10-10 10:58:37 -0700 | [diff] [blame] | 570 | @param sponge_url url to Sponge result. |
Craig Harrison | 25eb0f3 | 2012-08-23 16:48:49 -0700 | [diff] [blame] | 571 | """ |
| 572 | self.anchor = anchor |
Prathmesh Prabhu | cd246f5 | 2018-01-03 13:45:48 -0800 | [diff] [blame] | 573 | self.url = _URL_PATTERN % (rpc_client_lib.add_protocol(server), |
| 574 | job_string) |
Fang Deng | 53c6ff5 | 2014-02-24 17:51:24 -0800 | [diff] [blame] | 575 | self.reason = reason |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 576 | self.retry_count = retry_count |
Simran Basi | 7203d4e | 2015-02-03 15:50:18 -0800 | [diff] [blame] | 577 | self.testname = testname |
Dan Shi | 9b620c2 | 2017-10-10 10:58:37 -0700 | [diff] [blame] | 578 | self.sponge_url = sponge_url |
J. Richard Barnette | b9c911d | 2013-08-23 11:24:21 -0700 | [diff] [blame] | 579 | if bug_info: |
| 580 | self.bug_id, self.bug_count = bug_info |
| 581 | else: |
| 582 | self.bug_id = None |
| 583 | self.bug_count = None |
Craig Harrison | 25eb0f3 | 2012-08-23 16:48:49 -0700 | [diff] [blame] | 584 | |
| 585 | |
Allen Li | e082ced | 2016-09-14 15:19:20 -0700 | [diff] [blame] | 586 | @property |
| 587 | def bug_url(self): |
| 588 | """URL of associated bug.""" |
| 589 | if self.bug_id: |
| 590 | return reporting_utils.link_crbug(self.bug_id) |
| 591 | else: |
| 592 | return None |
| 593 | |
| 594 | |
| 595 | @property |
| 596 | def _bug_count_text(self): |
| 597 | """Return bug count as human friendly text.""" |
| 598 | if self.bug_count is None: |
| 599 | bug_info = 'unknown number of reports' |
| 600 | elif self.bug_count == 1: |
| 601 | bug_info = 'new report' |
| 602 | else: |
| 603 | bug_info = '%s reports' % self.bug_count |
| 604 | return bug_info |
| 605 | |
| 606 | |
Ningning Xia | bd911bd | 2016-04-19 14:06:03 -0700 | [diff] [blame] | 607 | def GenerateBuildbotLinks(self): |
J. Richard Barnette | b9c911d | 2013-08-23 11:24:21 -0700 | [diff] [blame] | 608 | """Generate a link formatted to meet buildbot expectations. |
| 609 | |
Ningning Xia | bd911bd | 2016-04-19 14:06:03 -0700 | [diff] [blame] | 610 | If there is a bug associated with this link, report a link to the bug |
Allen Li | 4e7365e | 2017-07-10 15:40:24 -0700 | [diff] [blame] | 611 | and a link to the job logs; otherwise report a link to the job logs. |
Craig Harrison | 25eb0f3 | 2012-08-23 16:48:49 -0700 | [diff] [blame] | 612 | |
Allen Li | 4e7365e | 2017-07-10 15:40:24 -0700 | [diff] [blame] | 613 | @return A generator of links formatted for the buildbot log annotator. |
Craig Harrison | 25eb0f3 | 2012-08-23 16:48:49 -0700 | [diff] [blame] | 614 | """ |
Allen Li | 4e7365e | 2017-07-10 15:40:24 -0700 | [diff] [blame] | 615 | if self.bug_url: |
| 616 | yield self._get_link_to_bug() |
| 617 | yield self._get_link_to_job_logs() |
Ningning Xia | bd911bd | 2016-04-19 14:06:03 -0700 | [diff] [blame] | 618 | |
Allen Li | 4e7365e | 2017-07-10 15:40:24 -0700 | [diff] [blame] | 619 | |
| 620 | def _get_link_to_bug(self): |
| 621 | """Return buildbot link to bug. |
| 622 | |
| 623 | @return A link formatted for the buildbot log annotator. |
| 624 | """ |
| 625 | info_strings = self._get_info_strings() |
| 626 | info_strings.append(self._bug_count_text) |
| 627 | anchor_text = self._format_anchor_text(self._BUG_LINK_PREFIX, |
| 628 | info_strings) |
| 629 | return annotations.StepLink(anchor_text, self.bug_url) |
| 630 | |
| 631 | |
| 632 | def _get_link_to_job_logs(self): |
| 633 | """Return buildbot link to job logs. |
| 634 | |
| 635 | @return A link formatted for the buildbot log annotator. |
| 636 | """ |
| 637 | anchor_text = self._format_anchor_text(self._LOG_LINK_PREFIX, |
| 638 | self._get_info_strings()) |
| 639 | return annotations.StepLink(anchor_text, self.url) |
| 640 | |
| 641 | |
| 642 | def _get_info_strings(self): |
| 643 | """Return a list of info strings for _format_anchor_text().""" |
| 644 | info_strings = [] |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 645 | if self.retry_count > 0: |
| 646 | info_strings.append('retry_count: %d' % self.retry_count) |
Fang Deng | 53c6ff5 | 2014-02-24 17:51:24 -0800 | [diff] [blame] | 647 | if self.reason: |
Allen Li | e082ced | 2016-09-14 15:19:20 -0700 | [diff] [blame] | 648 | info_strings.append(self.reason) |
Allen Li | 4e7365e | 2017-07-10 15:40:24 -0700 | [diff] [blame] | 649 | return info_strings |
Ningning Xia | bd911bd | 2016-04-19 14:06:03 -0700 | [diff] [blame] | 650 | |
| 651 | |
Allen Li | e082ced | 2016-09-14 15:19:20 -0700 | [diff] [blame] | 652 | def _format_anchor_text(self, prefix, info_strings): |
| 653 | """Format anchor text given a prefix and info strings. |
Ningning Xia | bd911bd | 2016-04-19 14:06:03 -0700 | [diff] [blame] | 654 | |
| 655 | @param prefix The prefix of the anchor text. |
Allen Li | b1cb384 | 2017-07-10 15:34:29 -0700 | [diff] [blame] | 656 | @param info_strings Iterable of strings. |
Ningning Xia | bd911bd | 2016-04-19 14:06:03 -0700 | [diff] [blame] | 657 | @return A anchor_text with the right prefix and info strings. |
| 658 | """ |
Allen Li | b1cb384 | 2017-07-10 15:34:29 -0700 | [diff] [blame] | 659 | return '[{prefix}]: {anchor}: {info}'.format( |
Allen Li | e082ced | 2016-09-14 15:19:20 -0700 | [diff] [blame] | 660 | prefix=prefix, |
Allen Li | b1cb384 | 2017-07-10 15:34:29 -0700 | [diff] [blame] | 661 | anchor=self.anchor.strip(), |
| 662 | info=', '.join(info_strings)) |
Craig Harrison | 25eb0f3 | 2012-08-23 16:48:49 -0700 | [diff] [blame] | 663 | |
Allen Li | e082ced | 2016-09-14 15:19:20 -0700 | [diff] [blame] | 664 | @property |
| 665 | def text_link(self): |
| 666 | """Link to the job's logs, for consumption by a human. |
Craig Harrison | 25eb0f3 | 2012-08-23 16:48:49 -0700 | [diff] [blame] | 667 | |
Craig Harrison | d845157 | 2012-08-31 10:29:33 -0700 | [diff] [blame] | 668 | @return A link formatted for human readability. |
Craig Harrison | 25eb0f3 | 2012-08-23 16:48:49 -0700 | [diff] [blame] | 669 | """ |
Aviv Keshet | 269848b | 2016-10-03 00:13:19 -0700 | [diff] [blame] | 670 | return '%s %s' % (self.anchor, self.url) |
Craig Harrison | 25eb0f3 | 2012-08-23 16:48:49 -0700 | [diff] [blame] | 671 | |
Shuhei Takahashi | 18f5649 | 2017-11-14 16:23:46 +0900 | [diff] [blame] | 672 | def GenerateRetryLink(self): |
| 673 | """Generate a link to the retry dashboard. |
Simran Basi | 7203d4e | 2015-02-03 15:50:18 -0800 | [diff] [blame] | 674 | |
| 675 | @return A link formatted for the buildbot log annotator. |
| 676 | """ |
Allen Li | e082ced | 2016-09-14 15:19:20 -0700 | [diff] [blame] | 677 | if not self.testname or self.testname in self._SKIP_RETRY_DASHBOARD: |
Simran Basi | 7203d4e | 2015-02-03 15:50:18 -0800 | [diff] [blame] | 678 | return None |
Xixuan Wu | 1729fca | 2018-03-21 16:32:58 -0700 | [diff] [blame] | 679 | |
| 680 | # TODO(xixuan): Return the right flake dashboard later. |
| 681 | return None |
Simran Basi | 7203d4e | 2015-02-03 15:50:18 -0800 | [diff] [blame] | 682 | |
Shuhei Takahashi | 18f5649 | 2017-11-14 16:23:46 +0900 | [diff] [blame] | 683 | def GenerateHistoryLink(self): |
| 684 | """Generate a link to the test history dashboard. |
David Riley | a0cd1c2 | 2017-07-10 11:15:57 -0700 | [diff] [blame] | 685 | |
| 686 | @return A link formatted for the buildbot log annotator. |
| 687 | """ |
| 688 | if not self.testname or self.testname in self._SKIP_RETRY_DASHBOARD: |
| 689 | return None |
| 690 | return annotations.StepLink( |
| 691 | text='[Test-History]: %s' % self.testname, |
| 692 | url=reporting_utils.link_test_history(self.testname)) |
| 693 | |
Simran Basi | 7203d4e | 2015-02-03 15:50:18 -0800 | [diff] [blame] | 694 | |
Chris Masone | b61b405 | 2012-04-30 14:35:28 -0700 | [diff] [blame] | 695 | class Timings(object): |
| 696 | """Timings for important events during a suite. |
| 697 | |
| 698 | All timestamps are datetime.datetime objects. |
| 699 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 700 | @var suite_job_id: the afe job id of the suite job for which |
| 701 | we are recording the timing for. |
| 702 | @var download_start_time: the time the devserver starts staging |
| 703 | the build artifacts. Recorded in create_suite_job. |
| 704 | @var payload_end_time: the time when the artifacts only necessary to start |
| 705 | installsing images onto DUT's are staged. |
| 706 | Recorded in create_suite_job. |
| 707 | @var artifact_end_time: the remaining artifacts are downloaded after we kick |
| 708 | off the reimaging job, at which point we record |
| 709 | artifact_end_time. Recorded in dynamic_suite.py. |
Chris Masone | b61b405 | 2012-04-30 14:35:28 -0700 | [diff] [blame] | 710 | @var suite_start_time: the time the suite started. |
Chris Masone | b61b405 | 2012-04-30 14:35:28 -0700 | [diff] [blame] | 711 | @var tests_start_time: the time the first test started running. |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 712 | @var tests_end_time: the time the last test finished running. |
Chris Masone | b61b405 | 2012-04-30 14:35:28 -0700 | [diff] [blame] | 713 | """ |
beeps | 6f02d19 | 2013-03-22 13:15:49 -0700 | [diff] [blame] | 714 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 715 | def __init__(self, suite_job_id): |
| 716 | self.suite_job_id = suite_job_id |
| 717 | # Timings related to staging artifacts on devserver. |
| 718 | self.download_start_time = None |
| 719 | self.payload_end_time = None |
| 720 | self.artifact_end_time = None |
beeps | 6f02d19 | 2013-03-22 13:15:49 -0700 | [diff] [blame] | 721 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 722 | # The test_start_time, but taken off the view that corresponds to the |
| 723 | # suite instead of an individual test. |
| 724 | self.suite_start_time = None |
beeps | 6f02d19 | 2013-03-22 13:15:49 -0700 | [diff] [blame] | 725 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 726 | # Earliest and Latest tests in the set of TestViews passed to us. |
| 727 | self.tests_start_time = None |
| 728 | self.tests_end_time = None |
| 729 | |
Chris Masone | b61b405 | 2012-04-30 14:35:28 -0700 | [diff] [blame] | 730 | |
Chris Masone | d9f13c5 | 2012-08-29 10:37:08 -0700 | [diff] [blame] | 731 | def RecordTiming(self, view): |
| 732 | """Given a test report view, extract and record pertinent time info. |
Chris Masone | b61b405 | 2012-04-30 14:35:28 -0700 | [diff] [blame] | 733 | |
| 734 | get_detailed_test_views() returns a list of entries that provide |
| 735 | info about the various parts of a suite run. This method can take |
| 736 | any one of these entries and look up timestamp info we might want |
| 737 | and record it. |
| 738 | |
Chris Masone | cfa7efc | 2012-09-06 16:00:07 -0700 | [diff] [blame] | 739 | If timestamps are unavailable, datetime.datetime.min/max will be used. |
| 740 | |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 741 | @param view: A TestView object. |
Chris Masone | b61b405 | 2012-04-30 14:35:28 -0700 | [diff] [blame] | 742 | """ |
Chris Masone | cfa7efc | 2012-09-06 16:00:07 -0700 | [diff] [blame] | 743 | start_candidate = datetime.min |
| 744 | end_candidate = datetime.max |
| 745 | if view['test_started_time']: |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 746 | start_candidate = time_utils.time_string_to_datetime( |
| 747 | view['test_started_time']) |
Chris Masone | cfa7efc | 2012-09-06 16:00:07 -0700 | [diff] [blame] | 748 | if view['test_finished_time']: |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 749 | end_candidate = time_utils.time_string_to_datetime( |
| 750 | view['test_finished_time']) |
Chris Masone | cfa7efc | 2012-09-06 16:00:07 -0700 | [diff] [blame] | 751 | |
Shuqian Zhao | c085abb | 2016-02-24 11:27:26 -0800 | [diff] [blame] | 752 | if view.get_testname() == TestView.SUITE_JOB: |
Chris Masone | b61b405 | 2012-04-30 14:35:28 -0700 | [diff] [blame] | 753 | self.suite_start_time = start_candidate |
Chris Masone | b61b405 | 2012-04-30 14:35:28 -0700 | [diff] [blame] | 754 | else: |
| 755 | self._UpdateFirstTestStartTime(start_candidate) |
| 756 | self._UpdateLastTestEndTime(end_candidate) |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 757 | if view['afe_job_id'] == self.suite_job_id and 'job_keyvals' in view: |
Chris Masone | d9f13c5 | 2012-08-29 10:37:08 -0700 | [diff] [blame] | 758 | keyvals = view['job_keyvals'] |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 759 | self.download_start_time = time_utils.time_string_to_datetime( |
| 760 | keyvals.get(constants.DOWNLOAD_STARTED_TIME), |
| 761 | handle_type_error=True) |
beeps | 6f02d19 | 2013-03-22 13:15:49 -0700 | [diff] [blame] | 762 | |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 763 | self.payload_end_time = time_utils.time_string_to_datetime( |
| 764 | keyvals.get(constants.PAYLOAD_FINISHED_TIME), |
| 765 | handle_type_error=True) |
beeps | 6f02d19 | 2013-03-22 13:15:49 -0700 | [diff] [blame] | 766 | |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 767 | self.artifact_end_time = time_utils.time_string_to_datetime( |
| 768 | keyvals.get(constants.ARTIFACT_FINISHED_TIME), |
| 769 | handle_type_error=True) |
Chris Masone | 44e4d6c | 2012-08-15 14:25:53 -0700 | [diff] [blame] | 770 | |
Chris Masone | b61b405 | 2012-04-30 14:35:28 -0700 | [diff] [blame] | 771 | |
| 772 | def _UpdateFirstTestStartTime(self, candidate): |
| 773 | """Update self.tests_start_time, iff candidate is an earlier time. |
| 774 | |
| 775 | @param candidate: a datetime.datetime object. |
| 776 | """ |
| 777 | if not self.tests_start_time or candidate < self.tests_start_time: |
| 778 | self.tests_start_time = candidate |
| 779 | |
| 780 | |
| 781 | def _UpdateLastTestEndTime(self, candidate): |
| 782 | """Update self.tests_end_time, iff candidate is a later time. |
| 783 | |
| 784 | @param candidate: a datetime.datetime object. |
| 785 | """ |
| 786 | if not self.tests_end_time or candidate > self.tests_end_time: |
| 787 | self.tests_end_time = candidate |
| 788 | |
| 789 | |
| 790 | def __str__(self): |
| 791 | return ('\n' |
| 792 | 'Suite timings:\n' |
Chris Masone | a8066a9 | 2012-05-01 16:52:31 -0700 | [diff] [blame] | 793 | 'Downloads started at %s\n' |
| 794 | 'Payload downloads ended at %s\n' |
Chris Masone | b61b405 | 2012-04-30 14:35:28 -0700 | [diff] [blame] | 795 | 'Suite started at %s\n' |
Chris Masone | a8066a9 | 2012-05-01 16:52:31 -0700 | [diff] [blame] | 796 | 'Artifact downloads ended (at latest) at %s\n' |
Chris Masone | b61b405 | 2012-04-30 14:35:28 -0700 | [diff] [blame] | 797 | 'Testing started at %s\n' |
Chris Masone | a8066a9 | 2012-05-01 16:52:31 -0700 | [diff] [blame] | 798 | 'Testing ended at %s\n' % (self.download_start_time, |
| 799 | self.payload_end_time, |
| 800 | self.suite_start_time, |
Chris Masone | a8066a9 | 2012-05-01 16:52:31 -0700 | [diff] [blame] | 801 | self.artifact_end_time, |
Chris Masone | b61b405 | 2012-04-30 14:35:28 -0700 | [diff] [blame] | 802 | self.tests_start_time, |
| 803 | self.tests_end_time)) |
| 804 | |
| 805 | |
Alex Miller | c7a5952 | 2013-10-30 15:18:57 -0700 | [diff] [blame] | 806 | def instance_for_pool(pool_name): |
| 807 | """ |
| 808 | Return the hostname of the server that should be used to service a suite |
| 809 | for the specified pool. |
| 810 | |
| 811 | @param pool_name: The pool (without 'pool:' to schedule the suite against. |
| 812 | @return: The correct host that should be used to service this suite run. |
| 813 | """ |
| 814 | return CONFIG.get_config_value( |
| 815 | 'POOL_INSTANCE_SHARDING', pool_name, |
| 816 | default=_DEFAULT_AUTOTEST_INSTANCE) |
| 817 | |
| 818 | |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 819 | class TestView(object): |
| 820 | """Represents a test view and provides a set of helper functions.""" |
| 821 | |
| 822 | |
Shuqian Zhao | c085abb | 2016-02-24 11:27:26 -0800 | [diff] [blame] | 823 | SUITE_JOB = 'Suite job' |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 824 | |
| 825 | |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 826 | def __init__(self, view, afe_job, suite_name, build, user, |
| 827 | solo_test_run=False): |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 828 | """Init a TestView object representing a tko test view. |
| 829 | |
| 830 | @param view: A dictionary representing a tko test view. |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 831 | @param afe_job: An instance of frontend.afe.models.Job |
| 832 | representing the job that kicked off the test. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 833 | @param suite_name: The name of the suite |
| 834 | that the test belongs to. |
| 835 | @param build: The build for which the test is run. |
Simran Basi | 01984f5 | 2015-10-12 15:36:45 -0700 | [diff] [blame] | 836 | @param user: The user for which the test is run. |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 837 | @param solo_test_run: This is a solo test run not part of a suite. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 838 | """ |
| 839 | self.view = view |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 840 | self.afe_job = afe_job |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 841 | self.suite_name = suite_name |
| 842 | self.build = build |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 843 | self.is_suite_view = afe_job.parent_job is None and not solo_test_run |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 844 | # This is the test name that will be shown in the output. |
| 845 | self.testname = None |
Simran Basi | 01984f5 | 2015-10-12 15:36:45 -0700 | [diff] [blame] | 846 | self.user = user |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 847 | |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 848 | # The case that a job was aborted before it got a chance to run |
| 849 | # usually indicates suite has timed out (unless aborted by user). |
| 850 | # In this case, the abort reason will be None. |
| 851 | # Update the reason with proper information. |
| 852 | if (self.is_relevant_suite_view() and |
Shuqian Zhao | c085abb | 2016-02-24 11:27:26 -0800 | [diff] [blame] | 853 | not self.get_testname() == self.SUITE_JOB and |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 854 | self.view['status'] == 'ABORT' and |
| 855 | not self.view['reason']): |
| 856 | self.view['reason'] = 'Timed out, did not run.' |
| 857 | |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 858 | |
| 859 | def __getitem__(self, key): |
| 860 | """Overload __getitem__ so that we can still use [] |
| 861 | |
| 862 | @param key: A key of the tko test view. |
| 863 | |
| 864 | @returns: The value of an attribute in the view. |
| 865 | |
| 866 | """ |
| 867 | return self.view[key] |
| 868 | |
| 869 | |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 870 | def __iter__(self): |
| 871 | """Overload __iter__ so that it supports 'in' operator.""" |
| 872 | return iter(self.view) |
| 873 | |
| 874 | |
| 875 | def get_testname(self): |
| 876 | """Get test name that should be shown in the output. |
| 877 | |
| 878 | Formalize the test_name we got from the test view. |
| 879 | |
Allen Li | e6236ec | 2017-07-05 12:52:36 -0700 | [diff] [blame] | 880 | Remove 'build/suite' prefix if any. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 881 | |
| 882 | If one runs a test in control file via the following code, |
| 883 | job.runtest('my_Test', tag='tag') |
| 884 | for most of the cases, view['test_name'] would look like 'my_Test.tag'. |
| 885 | If this is the case, this method will just return the original |
| 886 | test name, i.e. 'my_Test.tag'. |
| 887 | |
| 888 | There are four special cases. |
| 889 | 1) A test view is for the suite job's SERVER_JOB. |
Shuqian Zhao | c085abb | 2016-02-24 11:27:26 -0800 | [diff] [blame] | 890 | In this case, this method will return 'Suite job'. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 891 | |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 892 | 2) A test view is of a child job or a solo test run not part of a |
| 893 | suite, and for a SERVER_JOB or CLIENT_JOB. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 894 | In this case, we will take the job name, remove the build/suite |
| 895 | prefix from the job name, and append the rest to 'SERVER_JOB' |
| 896 | or 'CLIENT_JOB' as a prefix. So the names returned by this |
| 897 | method will look like: |
Allen Li | e6236ec | 2017-07-05 12:52:36 -0700 | [diff] [blame] | 898 | 'dummy_Pass_SERVER_JOB' |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 899 | 'dummy_Fail_SERVER_JOB' |
| 900 | |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 901 | 3) A test view is of a suite job and its status is ABORT. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 902 | In this case, the view['test_name'] is the child job's name. |
Allen Li | e6236ec | 2017-07-05 12:52:36 -0700 | [diff] [blame] | 903 | For instance, |
Allen Li | e6236ec | 2017-07-05 12:52:36 -0700 | [diff] [blame] | 904 | 'lumpy-release/R35-5712.0.0/dummy/dummy_Pass' |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 905 | 'lumpy-release/R35-5712.0.0/dummy/dummy_Fail' |
| 906 | The above names will be converted to the following: |
Allen Li | e6236ec | 2017-07-05 12:52:36 -0700 | [diff] [blame] | 907 | 'dummy_Pass' |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 908 | 'dummy_Fail' |
| 909 | |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 910 | 4) A test view's status is of a suite job and its status is TEST_NA. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 911 | In this case, the view['test_name'] is the NAME field of the control |
Allen Li | e6236ec | 2017-07-05 12:52:36 -0700 | [diff] [blame] | 912 | file. For instance, |
Allen Li | e6236ec | 2017-07-05 12:52:36 -0700 | [diff] [blame] | 913 | 'dummy_Pass' |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 914 | 'dummy_Fail' |
| 915 | This method will not modify these names. |
| 916 | |
| 917 | @returns: Test name after normalization. |
| 918 | |
| 919 | """ |
| 920 | if self.testname is not None: |
| 921 | return self.testname |
| 922 | |
| 923 | if (self.is_suite_view and |
| 924 | self.view['test_name'].startswith('SERVER_JOB')): |
Shuqian Zhao | c085abb | 2016-02-24 11:27:26 -0800 | [diff] [blame] | 925 | # Rename suite job's SERVER_JOB to 'Suite job'. |
| 926 | self.testname = self.SUITE_JOB |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 927 | return self.testname |
| 928 | |
| 929 | if (self.view['test_name'].startswith('SERVER_JOB') or |
| 930 | self.view['test_name'].startswith('CLIENT_JOB')): |
| 931 | # Append job name as a prefix for SERVER_JOB and CLIENT_JOB |
| 932 | testname= '%s_%s' % (self.view['job_name'], self.view['test_name']) |
| 933 | else: |
| 934 | testname = self.view['test_name'] |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 935 | # Remove the build and suite name from testname if any. |
Allen Li | e6236ec | 2017-07-05 12:52:36 -0700 | [diff] [blame] | 936 | self.testname = tools.get_test_name( |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 937 | self.build, self.suite_name, testname) |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 938 | return self.testname |
| 939 | |
| 940 | |
| 941 | def is_relevant_suite_view(self): |
| 942 | """Checks whether this is a suite view we should care about. |
| 943 | |
| 944 | @returns: True if it is relevant. False otherwise. |
| 945 | """ |
Shuqian Zhao | c085abb | 2016-02-24 11:27:26 -0800 | [diff] [blame] | 946 | return (self.get_testname() == self.SUITE_JOB or |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 947 | (self.is_suite_view and |
| 948 | not self.view['test_name'].startswith('CLIENT_JOB') and |
| 949 | not self.view['subdir'])) |
| 950 | |
| 951 | |
| 952 | def is_test(self): |
| 953 | """Return whether the view is for an actual test. |
| 954 | |
| 955 | @returns True if the view is for an actual test. |
| 956 | False if the view is for SERVER_JOB or CLIENT_JOB. |
| 957 | |
| 958 | """ |
| 959 | return not (self.view['test_name'].startswith('SERVER_JOB') or |
| 960 | self.view['test_name'].startswith('CLIENT_JOB')) |
| 961 | |
| 962 | |
| 963 | def is_retry(self): |
| 964 | """Check whether the view is for a retry. |
| 965 | |
| 966 | @returns: True, if the view is for a retry; False otherwise. |
| 967 | |
| 968 | """ |
| 969 | return self.view['job_keyvals'].get('retry_original_job_id') is not None |
| 970 | |
| 971 | |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 972 | def hit_timeout(self): |
| 973 | """Check whether the corresponding job has hit its own timeout. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 974 | |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 975 | Note this method should not be called for those test views |
| 976 | that belongs to a suite job and are determined as irrelevant |
| 977 | by is_relevant_suite_view. This is because they are associated |
| 978 | to the suite job, whose job start/finished time make no sense |
| 979 | to an irrelevant test view. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 980 | |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 981 | @returns: True if the corresponding afe job has hit timeout. |
| 982 | False otherwise. |
| 983 | """ |
| 984 | if (self.is_relevant_suite_view() and |
Shuqian Zhao | c085abb | 2016-02-24 11:27:26 -0800 | [diff] [blame] | 985 | self.get_testname() != self.SUITE_JOB): |
| 986 | # Any relevant suite test view except SUITE_JOB |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 987 | # did not hit its own timeout because it was not ever run. |
| 988 | return False |
| 989 | start = (datetime.strptime( |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 990 | self.view['job_started_time'], time_utils.TIME_FMT) |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 991 | if self.view['job_started_time'] else None) |
| 992 | end = (datetime.strptime( |
Dan Shi | dfea368 | 2014-08-10 23:38:40 -0700 | [diff] [blame] | 993 | self.view['job_finished_time'], time_utils.TIME_FMT) |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 994 | if self.view['job_finished_time'] else None) |
| 995 | if not start or not end: |
| 996 | return False |
| 997 | else: |
| 998 | return ((end - start).total_seconds()/60.0 |
| 999 | > self.afe_job.max_runtime_mins) |
| 1000 | |
| 1001 | |
| 1002 | def is_aborted(self): |
| 1003 | """Check if the view was aborted. |
| 1004 | |
Shuqian Zhao | c085abb | 2016-02-24 11:27:26 -0800 | [diff] [blame] | 1005 | For suite job and child job test views, we check job keyval |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 1006 | 'aborted_by' and test status. |
| 1007 | |
| 1008 | For relevant suite job test views, we only check test status |
| 1009 | because the suite job keyval won't make sense to individual |
| 1010 | test views. |
| 1011 | |
| 1012 | @returns: True if the test was as aborted, False otherwise. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1013 | |
| 1014 | """ |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 1015 | |
| 1016 | if (self.is_relevant_suite_view() and |
Shuqian Zhao | c085abb | 2016-02-24 11:27:26 -0800 | [diff] [blame] | 1017 | self.get_testname() != self.SUITE_JOB): |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 1018 | return self.view['status'] == 'ABORT' |
| 1019 | else: |
| 1020 | return (bool(self.view['job_keyvals'].get('aborted_by')) and |
| 1021 | self.view['status'] in ['ABORT', 'RUNNING']) |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1022 | |
| 1023 | |
| 1024 | def is_in_fail_status(self): |
Fang Deng | 95af42f | 2014-09-12 14:16:11 -0700 | [diff] [blame] | 1025 | """Check if the given test's status corresponds to a failure. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1026 | |
| 1027 | @returns: True if the test's status is FAIL or ERROR. False otherwise. |
| 1028 | |
| 1029 | """ |
| 1030 | # All the statuses tests can have when they fail. |
| 1031 | return self.view['status'] in ['FAIL', 'ERROR', 'ABORT'] |
| 1032 | |
| 1033 | |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 1034 | def is_provision(self): |
| 1035 | """Check whether this is a provision test.""" |
| 1036 | return self.get_testname() == 'provision' |
Fang Deng | 95af42f | 2014-09-12 14:16:11 -0700 | [diff] [blame] | 1037 | |
| 1038 | |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1039 | def get_buildbot_link_reason(self): |
| 1040 | """Generate the buildbot link reason for the test. |
| 1041 | |
| 1042 | @returns: A string representing the reason. |
| 1043 | |
| 1044 | """ |
| 1045 | return ('%s: %s' % (self.view['status'], self.view['reason']) |
| 1046 | if self.view['reason'] else self.view['status']) |
| 1047 | |
| 1048 | |
| 1049 | def get_job_id_owner_str(self): |
| 1050 | """Generate the job_id_owner string for a test. |
| 1051 | |
| 1052 | @returns: A string which looks like 135036-username |
| 1053 | |
| 1054 | """ |
Simran Basi | 01984f5 | 2015-10-12 15:36:45 -0700 | [diff] [blame] | 1055 | return '%s-%s' % (self.view['afe_job_id'], self.user) |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1056 | |
| 1057 | |
| 1058 | def get_bug_info(self, suite_job_keyvals): |
| 1059 | """Get the bug info from suite_job_keyvals. |
| 1060 | |
| 1061 | If a bug has been filed for the test, its bug info (bug id and counts) |
| 1062 | will be stored in the suite job's keyvals. This method attempts to |
| 1063 | retrieve bug info of the test from |suite_job_keyvals|. It will return |
| 1064 | None if no bug info is found. No need to check bug info if the view is |
Shuqian Zhao | c085abb | 2016-02-24 11:27:26 -0800 | [diff] [blame] | 1065 | SUITE_JOB. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1066 | |
| 1067 | @param suite_job_keyvals: The job keyval dictionary of the suite job. |
| 1068 | All the bug info about child jobs are stored in |
| 1069 | suite job's keyvals. |
| 1070 | |
| 1071 | @returns: None if there is no bug info, or a pair with the |
| 1072 | id of the bug, and the count of the number of |
| 1073 | times the bug has been seen. |
| 1074 | |
| 1075 | """ |
Shuqian Zhao | c085abb | 2016-02-24 11:27:26 -0800 | [diff] [blame] | 1076 | if self.get_testname() == self.SUITE_JOB: |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1077 | return None |
| 1078 | if (self.view['test_name'].startswith('SERVER_JOB') or |
| 1079 | self.view['test_name'].startswith('CLIENT_JOB')): |
| 1080 | # Append job name as a prefix for SERVER_JOB and CLIENT_JOB |
| 1081 | testname= '%s_%s' % (self.view['job_name'], self.view['test_name']) |
| 1082 | else: |
| 1083 | testname = self.view['test_name'] |
| 1084 | |
| 1085 | return tools.get_test_failure_bug_info( |
| 1086 | suite_job_keyvals, self.view['afe_job_id'], |
| 1087 | testname) |
| 1088 | |
| 1089 | |
| 1090 | def should_display_buildbot_link(self): |
| 1091 | """Check whether a buildbot link should show for this view. |
| 1092 | |
Shuqian Zhao | c085abb | 2016-02-24 11:27:26 -0800 | [diff] [blame] | 1093 | For suite job view, show buildbot link if it fails. |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 1094 | For normal test view, |
| 1095 | show buildbot link if it is a retry |
| 1096 | show buildbot link if it hits its own timeout. |
| 1097 | show buildbot link if it fails. This doesn't |
| 1098 | include the case where it was aborted but has |
| 1099 | not hit its own timeout (most likely it was aborted because |
| 1100 | suite has timed out). |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1101 | |
| 1102 | @returns: True if we should show the buildbot link. |
| 1103 | False otherwise. |
| 1104 | """ |
| 1105 | is_bad_status = (self.view['status'] != 'GOOD' and |
| 1106 | self.view['status'] != 'TEST_NA') |
Shuqian Zhao | c085abb | 2016-02-24 11:27:26 -0800 | [diff] [blame] | 1107 | if self.get_testname() == self.SUITE_JOB: |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 1108 | return is_bad_status |
| 1109 | else: |
| 1110 | if self.is_retry(): |
| 1111 | return True |
| 1112 | if is_bad_status: |
| 1113 | return not self.is_aborted() or self.hit_timeout() |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1114 | |
| 1115 | |
Shuqian Zhao | 2fecacd | 2015-08-05 22:56:30 -0700 | [diff] [blame] | 1116 | def get_control_file_attributes(self): |
| 1117 | """Get the attributes from the control file of the test. |
| 1118 | |
| 1119 | @returns: A list of test attribute or None. |
| 1120 | """ |
| 1121 | control_file = self.afe_job.control_file |
| 1122 | attributes = None |
| 1123 | if control_file: |
| 1124 | cd = control_data.parse_control_string(control_file) |
| 1125 | attributes = list(cd.attributes) |
| 1126 | return attributes |
| 1127 | |
| 1128 | |
David Riley | dcd1a64 | 2017-03-01 23:15:08 -0800 | [diff] [blame] | 1129 | def override_afe_job_id(self, afe_job_id): |
| 1130 | """Overrides the AFE job id for the test. |
| 1131 | |
| 1132 | @param afe_job_id: The new AFE job id to use. |
| 1133 | """ |
| 1134 | self.view['afe_job_id'] = afe_job_id |
| 1135 | |
| 1136 | |
Allen Li | dc2c69a | 2016-09-14 19:05:47 -0700 | [diff] [blame] | 1137 | def log_buildbot_links(log_func, links): |
| 1138 | """Output buildbot links to log. |
| 1139 | |
| 1140 | @param log_func: Logging function to use. |
| 1141 | @param links: Iterable of LogLink instances. |
| 1142 | """ |
| 1143 | for link in links: |
| 1144 | for generated_link in link.GenerateBuildbotLinks(): |
| 1145 | log_func(generated_link) |
Shuhei Takahashi | 18f5649 | 2017-11-14 16:23:46 +0900 | [diff] [blame] | 1146 | retry_link = link.GenerateRetryLink() |
| 1147 | if retry_link: |
| 1148 | log_func(retry_link) |
| 1149 | history_link = link.GenerateHistoryLink() |
| 1150 | if history_link: |
| 1151 | log_func(history_link) |
Allen Li | dc2c69a | 2016-09-14 19:05:47 -0700 | [diff] [blame] | 1152 | |
| 1153 | |
Allen Li | 0b675b6 | 2017-07-05 13:38:04 -0700 | [diff] [blame] | 1154 | class _ReturnCodeComputer(object): |
Allen Li | 422f24b | 2017-07-12 15:15:43 -0700 | [diff] [blame] | 1155 | """This is responsible for returning the _ReturnResult for a suite.""" |
Allen Li | 0b675b6 | 2017-07-05 13:38:04 -0700 | [diff] [blame] | 1156 | |
| 1157 | def __call__(self, test_views): |
| 1158 | """Compute the exit code based on test results.""" |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 1159 | result = _RETURN_RESULTS['ok'] |
Allen Li | 0b675b6 | 2017-07-05 13:38:04 -0700 | [diff] [blame] | 1160 | |
| 1161 | for v in test_views: |
Allen Li | 0b675b6 | 2017-07-05 13:38:04 -0700 | [diff] [blame] | 1162 | if v.get_testname() == TestView.SUITE_JOB: |
Allen Li | 422f24b | 2017-07-12 15:15:43 -0700 | [diff] [blame] | 1163 | result |= self._get_suite_result(v) |
Allen Li | 0b675b6 | 2017-07-05 13:38:04 -0700 | [diff] [blame] | 1164 | else: |
Allen Li | 422f24b | 2017-07-12 15:15:43 -0700 | [diff] [blame] | 1165 | result |= self._get_test_result(v) |
| 1166 | return result |
| 1167 | |
| 1168 | def _get_suite_result(self, test_view): |
| 1169 | """Return the _ReturnResult for the given suite job.""" |
| 1170 | # The order of checking each case is important. |
| 1171 | if test_view.is_aborted() and test_view.hit_timeout(): |
| 1172 | return _RETURN_RESULTS['suite_timeout'] |
| 1173 | elif test_view.is_in_fail_status(): |
| 1174 | return _RETURN_RESULTS['suite_failed'] |
| 1175 | elif test_view['status'] == 'WARN': |
| 1176 | return _RETURN_RESULTS['suite_warning'] |
| 1177 | else: |
| 1178 | return _RETURN_RESULTS['ok'] |
| 1179 | |
| 1180 | def _get_test_result(self, test_view): |
| 1181 | """Return the _ReturnResult for the given test job.""" |
| 1182 | # The order of checking each case is important. |
| 1183 | if test_view.is_aborted() and test_view.is_relevant_suite_view(): |
| 1184 | # The test was aborted before started |
| 1185 | # This gurantees that the suite has timed out. |
Prathmesh Prabhu | 316180c | 2017-12-19 16:06:44 -0800 | [diff] [blame] | 1186 | return _RETURN_RESULTS['test_aborted_prestart'] |
Allen Li | 422f24b | 2017-07-12 15:15:43 -0700 | [diff] [blame] | 1187 | elif test_view.is_aborted() and not test_view.hit_timeout(): |
| 1188 | # The test was aborted, but |
| 1189 | # not due to a timeout. This is most likely |
| 1190 | # because the suite has timed out, but may |
| 1191 | # also because it was aborted by the user. |
| 1192 | # Since suite timing out is determined by checking |
| 1193 | # the suite job view, we simply ignore this view here. |
Prathmesh Prabhu | 316180c | 2017-12-19 16:06:44 -0800 | [diff] [blame] | 1194 | return _RETURN_RESULTS['test_aborted_mystery'] |
Allen Li | 422f24b | 2017-07-12 15:15:43 -0700 | [diff] [blame] | 1195 | elif test_view.is_in_fail_status(): # The test job failed |
| 1196 | if test_view.is_provision(): |
| 1197 | return _RETURN_RESULTS['provision_failed'] |
| 1198 | else: |
| 1199 | return _RETURN_RESULTS['test_failure'] |
| 1200 | elif test_view['status'] == 'WARN': |
| 1201 | return _RETURN_RESULTS['test_warning'] |
| 1202 | elif test_view.is_retry(): |
| 1203 | # The test is a passing retry. |
| 1204 | return _RETURN_RESULTS['test_retry'] |
| 1205 | else: |
| 1206 | return _RETURN_RESULTS['ok'] |
| 1207 | |
| 1208 | |
| 1209 | class _ProvisionReturnCodeComputer(_ReturnCodeComputer): |
| 1210 | """This is used for returning the _ReturnResult for provision suites.""" |
| 1211 | |
| 1212 | def __init__(self, num_required): |
| 1213 | """Initialize instance. |
| 1214 | |
| 1215 | num_required is the number of passing provision jobs needed. |
| 1216 | """ |
| 1217 | super(_ProvisionReturnCodeComputer, self).__init__() |
| 1218 | self._num_required = num_required |
| 1219 | self._num_successful = 0 |
| 1220 | |
| 1221 | def __call__(self, test_views): |
| 1222 | result = super(_ProvisionReturnCodeComputer, self).__call__(test_views) |
| 1223 | if self._num_successful >= self._num_required: |
| 1224 | logging.info('Return result upgraded from %r' |
| 1225 | ' due to enough ok provisions', |
| 1226 | result) |
| 1227 | return _RETURN_RESULTS['ok'] |
| 1228 | else: |
| 1229 | return result |
| 1230 | |
| 1231 | def _get_test_result(self, test_view): |
| 1232 | result = (super(_ProvisionReturnCodeComputer, self) |
| 1233 | ._get_test_result(test_view)) |
| 1234 | if result in {_RETURN_RESULTS[s] for s in ('ok', 'test_retry')}: |
| 1235 | self._num_successful += 1 |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 1236 | return result |
Allen Li | 0b675b6 | 2017-07-05 13:38:04 -0700 | [diff] [blame] | 1237 | |
| 1238 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1239 | class ResultCollector(object): |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 1240 | """Collect test results of a suite or a single test run. |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1241 | |
| 1242 | Once a suite job has finished, use this class to collect test results. |
| 1243 | `run` is the core method that is to be called first. Then the caller |
| 1244 | could retrieve information like return code, return message, is_aborted, |
| 1245 | and timings by accessing the collector's public attributes. And output |
| 1246 | the test results and links by calling the 'output_*' methods. |
| 1247 | |
| 1248 | Here is a overview of what `run` method does. |
| 1249 | |
| 1250 | 1) Collect the suite job's results from tko_test_view_2. |
| 1251 | For the suite job, we only pull test views without a 'subdir'. |
| 1252 | A NULL subdir indicates that the test was _not_ executed. This could be |
| 1253 | that no child job was scheduled for this test or the child job got |
| 1254 | aborted before starts running. |
| 1255 | (Note 'SERVER_JOB'/'CLIENT_JOB' are handled specially) |
| 1256 | |
| 1257 | 2) Collect the child jobs' results from tko_test_view_2. |
| 1258 | For child jobs, we pull all the test views associated with them. |
Allen Li | dc2c69a | 2016-09-14 19:05:47 -0700 | [diff] [blame] | 1259 | (Note 'SERVER_JOB'/'CLIENT_JOB' are handled specially) |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1260 | |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1261 | 3) Generate web and buildbot links. |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1262 | 4) Compute timings of the suite run. |
| 1263 | 5) Compute the return code based on test results. |
| 1264 | |
| 1265 | @var _instance_server: The hostname of the server that is used |
| 1266 | to service the suite. |
| 1267 | @var _afe: The afe rpc client. |
| 1268 | @var _tko: The tko rpc client. |
| 1269 | @var _build: The build for which the suite is run, |
| 1270 | e.g. 'lumpy-release/R35-5712.0.0' |
MK Ryu | 977a975 | 2014-10-21 11:58:09 -0700 | [diff] [blame] | 1271 | @var _board: The target board for which the suite is run, |
| 1272 | e.g., 'lumpy', 'link'. |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1273 | @var _suite_name: The suite name, e.g. 'bvt', 'dummy'. |
| 1274 | @var _suite_job_id: The job id of the suite for which we are going to |
| 1275 | collect results. |
Shuqian Zhao | f39bf2a | 2015-09-29 14:19:28 -0700 | [diff] [blame] | 1276 | @var _original_suite_name: The suite name we record timing would be |
| 1277 | different from _suite_name when running |
| 1278 | suite_attr_wrapper. |
Allen Li | 0b675b6 | 2017-07-05 13:38:04 -0700 | [diff] [blame] | 1279 | @var _return_code_function: Called to return what the overall result of |
| 1280 | the suite is. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1281 | @var _suite_views: A list of TestView objects, representing relevant |
| 1282 | test views of the suite job. |
| 1283 | @var _child_views: A list of TestView objects, representing test views |
| 1284 | of the child jobs. |
| 1285 | @var _test_views: A list of TestView objects, representing all test views |
| 1286 | from _suite_views and _child_views. |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1287 | @var _web_links: A list of web links pointing to the results of jobs. |
Allen Li | 28be064 | 2017-07-10 15:16:26 -0700 | [diff] [blame] | 1288 | @var buildbot_links: A list of buildbot links for non-passing tests. |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 1289 | @var _solo_test_run: True if this is a single test run. |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 1290 | @var return_result: The _ReturnResult of the suite run. |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1291 | @var is_aborted: Whether the suite was aborted or not. |
| 1292 | True, False or None (aborting status is unknown yet) |
| 1293 | @var timings: A Timing object that records the suite's timings. |
| 1294 | |
| 1295 | """ |
| 1296 | |
| 1297 | |
MK Ryu | 977a975 | 2014-10-21 11:58:09 -0700 | [diff] [blame] | 1298 | def __init__(self, instance_server, afe, tko, build, board, |
Allen Li | 0b675b6 | 2017-07-05 13:38:04 -0700 | [diff] [blame] | 1299 | suite_name, suite_job_id, |
| 1300 | return_code_function, |
| 1301 | original_suite_name=None, |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 1302 | user=None, solo_test_run=False): |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1303 | self._instance_server = instance_server |
| 1304 | self._afe = afe |
| 1305 | self._tko = tko |
| 1306 | self._build = build |
MK Ryu | 977a975 | 2014-10-21 11:58:09 -0700 | [diff] [blame] | 1307 | self._board = board |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1308 | self._suite_name = suite_name |
| 1309 | self._suite_job_id = suite_job_id |
Shuqian Zhao | f39bf2a | 2015-09-29 14:19:28 -0700 | [diff] [blame] | 1310 | self._original_suite_name = original_suite_name or suite_name |
Allen Li | 0b675b6 | 2017-07-05 13:38:04 -0700 | [diff] [blame] | 1311 | self._return_code_function = return_code_function |
Fang Deng | 0454e63 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1312 | self._suite_views = [] |
| 1313 | self._child_views = [] |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1314 | self._test_views = [] |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1315 | self._retry_counts = {} |
David Riley | dcd1a64 | 2017-03-01 23:15:08 -0800 | [diff] [blame] | 1316 | self._missing_results = {} |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1317 | self._web_links = [] |
Allen Li | 28be064 | 2017-07-10 15:16:26 -0700 | [diff] [blame] | 1318 | self.buildbot_links = [] |
MK Ryu | 977a975 | 2014-10-21 11:58:09 -0700 | [diff] [blame] | 1319 | self._num_child_jobs = 0 |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 1320 | self.return_result = None |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1321 | self.is_aborted = None |
| 1322 | self.timings = None |
Simran Basi | 01984f5 | 2015-10-12 15:36:45 -0700 | [diff] [blame] | 1323 | self._user = user or getpass.getuser() |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 1324 | self._solo_test_run = solo_test_run |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1325 | |
| 1326 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1327 | def _fetch_relevant_test_views_of_suite(self): |
| 1328 | """Fetch relevant test views of the suite job. |
| 1329 | |
| 1330 | For the suite job, there will be a test view for SERVER_JOB, and views |
Allen Li | dc2c69a | 2016-09-14 19:05:47 -0700 | [diff] [blame] | 1331 | for results of its child jobs. For example, assume we've created |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1332 | a suite job (afe_job_id: 40) that runs dummy_Pass, dummy_Fail, |
| 1333 | dummy_Pass.bluetooth. Assume dummy_Pass was aborted before running while |
| 1334 | dummy_Path.bluetooth got TEST_NA as no duts have bluetooth. |
| 1335 | So the suite job's test views would look like |
| 1336 | _____________________________________________________________________ |
| 1337 | test_idx| job_idx|test_name |subdir |afe_job_id|status |
| 1338 | 10 | 1000 |SERVER_JOB |---- |40 |GOOD |
| 1339 | 11 | 1000 |dummy_Pass |NULL |40 |ABORT |
| 1340 | 12 | 1000 |dummy_Fail.Fail |41-onwer/...|40 |FAIL |
| 1341 | 13 | 1000 |dummy_Fail.Error |42-owner/...|40 |ERROR |
| 1342 | 14 | 1000 |dummy_Pass.bluetooth|NULL |40 |TEST_NA |
| 1343 | |
| 1344 | For a suite job, we only care about |
| 1345 | a) The test view for the suite job's SERVER_JOB |
| 1346 | b) The test views for real tests without a subdir. A NULL subdir |
| 1347 | indicates that a test didn't get executed. |
| 1348 | So, for the above example, we only keep test views whose test_idxs |
| 1349 | are 10, 11, 14. |
| 1350 | |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1351 | @returns: A list of TestView objects, representing relevant |
| 1352 | test views of the suite job. |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1353 | |
| 1354 | """ |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 1355 | suite_job = self._afe.get_jobs(id=self._suite_job_id)[0] |
Fang Deng | 0454e63 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1356 | views = self._tko.run(call='get_detailed_test_views', |
| 1357 | afe_job_id=self._suite_job_id) |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1358 | relevant_views = [] |
| 1359 | for v in views: |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 1360 | v = TestView(v, suite_job, self._suite_name, self._build, self._user, |
| 1361 | solo_test_run=self._solo_test_run) |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1362 | if v.is_relevant_suite_view(): |
David Riley | dcd1a64 | 2017-03-01 23:15:08 -0800 | [diff] [blame] | 1363 | # If the test doesn't have results in TKO and is being |
| 1364 | # displayed in the suite view instead of the child view, |
| 1365 | # then afe_job_id is incorrect and from the suite. |
| 1366 | # Override it based on the AFE job id which was missing |
| 1367 | # results. |
| 1368 | # TODO: This is likely inaccurate if a test has multiple |
| 1369 | # tries which all fail TKO parse stage. |
| 1370 | if v['test_name'] in self._missing_results: |
| 1371 | v.override_afe_job_id( |
| 1372 | self._missing_results[v['test_name']][0]) |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1373 | relevant_views.append(v) |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1374 | return relevant_views |
| 1375 | |
| 1376 | |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1377 | def _compute_retry_count(self, view): |
| 1378 | """Return how many times the test has been retried. |
| 1379 | |
| 1380 | @param view: A TestView instance. |
| 1381 | @returns: An int value indicating the retry count. |
| 1382 | |
| 1383 | """ |
| 1384 | old_job = view['job_keyvals'].get('retry_original_job_id') |
| 1385 | count = 0 |
| 1386 | while old_job: |
| 1387 | count += 1 |
| 1388 | views = self._tko.run( |
| 1389 | call='get_detailed_test_views', afe_job_id=old_job) |
| 1390 | old_job = (views[0]['job_keyvals'].get('retry_original_job_id') |
| 1391 | if views else None) |
| 1392 | return count |
| 1393 | |
| 1394 | |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 1395 | def _fetch_test_views_of_child_jobs(self, jobs=None): |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1396 | """Fetch test views of child jobs. |
| 1397 | |
David Riley | dcd1a64 | 2017-03-01 23:15:08 -0800 | [diff] [blame] | 1398 | @returns: A tuple (child_views, retry_counts, missing_results) |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1399 | child_views is list of TestView objects, representing |
David Riley | dcd1a64 | 2017-03-01 23:15:08 -0800 | [diff] [blame] | 1400 | all valid views. |
| 1401 | retry_counts is a dictionary that maps test_idx to retry |
| 1402 | counts. It only stores retry counts that are greater than 0. |
| 1403 | missing_results is a dictionary that maps test names to |
| 1404 | lists of job ids. |
Fang Deng | 0454e63 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1405 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1406 | """ |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1407 | child_views = [] |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1408 | retry_counts = {} |
David Riley | dcd1a64 | 2017-03-01 23:15:08 -0800 | [diff] [blame] | 1409 | missing_results = {} |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 1410 | child_jobs = jobs or self._afe.get_jobs(parent_job_id=self._suite_job_id) |
MK Ryu | 977a975 | 2014-10-21 11:58:09 -0700 | [diff] [blame] | 1411 | if child_jobs: |
| 1412 | self._num_child_jobs = len(child_jobs) |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 1413 | for job in child_jobs: |
Simran Basi | 01984f5 | 2015-10-12 15:36:45 -0700 | [diff] [blame] | 1414 | views = [TestView(v, job, self._suite_name, self._build, self._user) |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1415 | for v in self._tko.run( |
Fang Deng | f850353 | 2014-06-12 18:21:55 -0700 | [diff] [blame] | 1416 | call='get_detailed_test_views', afe_job_id=job.id, |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1417 | invalid=0)] |
David Riley | dcd1a64 | 2017-03-01 23:15:08 -0800 | [diff] [blame] | 1418 | if len(views) == 0: |
| 1419 | missing_results.setdefault(job.name, []).append(job.id) |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1420 | contains_test_failure = any( |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1421 | v.is_test() and v['status'] != 'GOOD' for v in views) |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1422 | for v in views: |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1423 | if (v.is_test() or |
| 1424 | v['status'] != 'GOOD' and not contains_test_failure): |
| 1425 | # For normal test view, just keep it. |
| 1426 | # For SERVER_JOB or CLIENT_JOB, only keep it |
| 1427 | # if it fails and no other test failure. |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1428 | child_views.append(v) |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1429 | retry_count = self._compute_retry_count(v) |
| 1430 | if retry_count > 0: |
| 1431 | retry_counts[v['test_idx']] = retry_count |
David Riley | dcd1a64 | 2017-03-01 23:15:08 -0800 | [diff] [blame] | 1432 | return child_views, retry_counts, missing_results |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1433 | |
| 1434 | |
| 1435 | def _generate_web_and_buildbot_links(self): |
| 1436 | """Generate web links and buildbot links.""" |
| 1437 | # TODO(fdeng): If a job was aborted before it reaches Running |
| 1438 | # state, we read the test view from the suite job |
| 1439 | # and thus this method generates a link pointing to the |
| 1440 | # suite job's page for the aborted job. Need a fix. |
| 1441 | self._web_links = [] |
Allen Li | 28be064 | 2017-07-10 15:16:26 -0700 | [diff] [blame] | 1442 | self.buildbot_links = [] |
Aviv Keshet | a336b9f | 2018-01-26 11:23:11 -0800 | [diff] [blame] | 1443 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1444 | # Bug info are stored in the suite job's keyvals. |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 1445 | if self._solo_test_run: |
| 1446 | suite_job_keyvals = {} |
Aviv Keshet | a336b9f | 2018-01-26 11:23:11 -0800 | [diff] [blame] | 1447 | elif not self._suite_views: |
| 1448 | suite_job_keyvals = {} |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 1449 | else: |
| 1450 | suite_job_keyvals = self._suite_views[0]['job_keyvals'] |
Aviv Keshet | a336b9f | 2018-01-26 11:23:11 -0800 | [diff] [blame] | 1451 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1452 | for v in self._test_views: |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1453 | retry_count = self._retry_counts.get(v['test_idx'], 0) |
| 1454 | bug_info = v.get_bug_info(suite_job_keyvals) |
| 1455 | job_id_owner = v.get_job_id_owner_str() |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1456 | link = LogLink( |
Allen Li | 3461324 | 2016-09-02 11:52:34 -0700 | [diff] [blame] | 1457 | anchor=v.get_testname(), |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1458 | server=self._instance_server, |
| 1459 | job_string=job_id_owner, |
Simran Basi | 7203d4e | 2015-02-03 15:50:18 -0800 | [diff] [blame] | 1460 | bug_info=bug_info, retry_count=retry_count, |
Dan Shi | 9b620c2 | 2017-10-10 10:58:37 -0700 | [diff] [blame] | 1461 | testname=v.get_testname(), |
| 1462 | sponge_url=suite_job_keyvals.get('sponge_url')) |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1463 | self._web_links.append(link) |
| 1464 | |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1465 | if v.should_display_buildbot_link(): |
| 1466 | link.reason = v.get_buildbot_link_reason() |
Allen Li | 28be064 | 2017-07-10 15:16:26 -0700 | [diff] [blame] | 1467 | self.buildbot_links.append(link) |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1468 | |
| 1469 | |
| 1470 | def _record_timings(self): |
| 1471 | """Record suite timings.""" |
| 1472 | self.timings = Timings(self._suite_job_id) |
| 1473 | for v in self._test_views: |
| 1474 | self.timings.RecordTiming(v) |
| 1475 | |
| 1476 | |
| 1477 | def _compute_return_code(self): |
| 1478 | """Compute the exit code based on test results.""" |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 1479 | self.return_result = self._return_code_function(self._test_views) |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1480 | |
| 1481 | |
Allen Li | 3461324 | 2016-09-02 11:52:34 -0700 | [diff] [blame] | 1482 | def _make_test_results(self): |
| 1483 | """Make TestResults for collected tests. |
| 1484 | |
| 1485 | @returns: List of TestResult instances. |
| 1486 | """ |
| 1487 | test_results = [] |
| 1488 | for test_view in self._test_views: |
| 1489 | test_result = TestResult( |
| 1490 | test_view=test_view, |
| 1491 | retry_count=self._retry_counts.get(test_view['test_idx'], 0)) |
| 1492 | test_results.append(test_result) |
| 1493 | return test_results |
| 1494 | |
| 1495 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1496 | def output_results(self): |
| 1497 | """Output test results, timings and web links.""" |
| 1498 | # Output test results |
Allen Li | 3461324 | 2016-09-02 11:52:34 -0700 | [diff] [blame] | 1499 | test_results = self._make_test_results() |
Brian Norris | c7575d3 | 2017-09-25 17:08:19 -0700 | [diff] [blame] | 1500 | if len(test_results) == 0: |
| 1501 | max_name_length = 0 |
| 1502 | else: |
| 1503 | max_name_length = max(len(t.name) for t in test_results) |
Allen Li | 3461324 | 2016-09-02 11:52:34 -0700 | [diff] [blame] | 1504 | for test_result in test_results: |
| 1505 | test_result.log_using(logging.info, max_name_length + 3) |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1506 | # Output suite timings |
| 1507 | logging.info(self.timings) |
| 1508 | # Output links to test logs |
| 1509 | logging.info('\nLinks to test logs:') |
| 1510 | for link in self._web_links: |
Allen Li | e082ced | 2016-09-14 15:19:20 -0700 | [diff] [blame] | 1511 | logging.info(link.text_link) |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1512 | logging.info('\n') |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1513 | |
| 1514 | |
Shuqian Zhao | 2fecacd | 2015-08-05 22:56:30 -0700 | [diff] [blame] | 1515 | def get_results_dict(self): |
| 1516 | """Write test results, timings and web links into a dict. |
| 1517 | |
| 1518 | @returns: A dict of results in the format like: |
| 1519 | { |
| 1520 | 'tests': { |
| 1521 | 'test_1': {'status': 'PASSED', 'attributes': [1,2], ...} |
| 1522 | 'test_2': {'status': 'FAILED', 'attributes': [1],...} |
| 1523 | } |
| 1524 | 'suite_timings': { |
| 1525 | 'download_start': '1998-07-17 00:00:00', |
| 1526 | 'payload_download_end': '1998-07-17 00:00:05', |
| 1527 | ... |
| 1528 | } |
| 1529 | } |
| 1530 | """ |
| 1531 | output_dict = {} |
| 1532 | tests_dict = output_dict.setdefault('tests', {}) |
| 1533 | for v in self._test_views: |
Shuqian Zhao | fae149c | 2017-01-30 16:46:53 -0800 | [diff] [blame] | 1534 | test_name = v.get_testname() |
| 1535 | test_info = tests_dict.setdefault(test_name, {}) |
| 1536 | test_info.update({ |
| 1537 | 'status': v['status'], |
| 1538 | 'attributes': v.get_control_file_attributes() or list(), |
| 1539 | 'reason': v['reason'], |
| 1540 | 'retry_count': self._retry_counts.get(v['test_idx'], 0), |
| 1541 | }) |
| 1542 | # For aborted test, the control file will not be parsed and thus |
| 1543 | # fail to get the attributes info. Therefore, the subsystems the |
| 1544 | # abort test testing will be missing. For this case, we will assume |
| 1545 | # the aborted test will test all subsystems, set subsystem:default. |
| 1546 | if (test_info['status'] == 'ABORT' and |
| 1547 | not any('subsystem:' in a for a in test_info['attributes'])): |
| 1548 | test_info['attributes'].append('subsystem:default') |
Shuqian Zhao | 2fecacd | 2015-08-05 22:56:30 -0700 | [diff] [blame] | 1549 | |
| 1550 | # Write the links to test logs into the |tests_dict| of |output_dict|. |
| 1551 | # For test whose status is not 'GOOD', the link is also buildbot_link. |
| 1552 | for link in self._web_links: |
Shuqian Zhao | fae149c | 2017-01-30 16:46:53 -0800 | [diff] [blame] | 1553 | test_name = link.anchor.strip() |
| 1554 | test_info = tests_dict.get(test_name) |
| 1555 | if test_info: |
| 1556 | test_info['link_to_logs'] = link.url |
Dan Shi | 9b620c2 | 2017-10-10 10:58:37 -0700 | [diff] [blame] | 1557 | test_info['sponge_url'] = link.sponge_url |
Shuhei Takahashi | 18f5649 | 2017-11-14 16:23:46 +0900 | [diff] [blame] | 1558 | # Write the retry dashboard link into the dict. |
Allen Li | 28be064 | 2017-07-10 15:16:26 -0700 | [diff] [blame] | 1559 | if link in self.buildbot_links and link.testname: |
Shuhei Takahashi | 18f5649 | 2017-11-14 16:23:46 +0900 | [diff] [blame] | 1560 | test_info['retry_dashboard_link'] \ |
Shuqian Zhao | fae149c | 2017-01-30 16:46:53 -0800 | [diff] [blame] | 1561 | = reporting_utils.link_retry_url(link.testname) |
Shuhei Takahashi | 18f5649 | 2017-11-14 16:23:46 +0900 | [diff] [blame] | 1562 | # Always write the wmatrix link for compatibility. |
| 1563 | test_info['wmatrix_link'] \ |
| 1564 | = reporting_utils.link_wmatrix_retry_url(link.testname) |
Shuqian Zhao | fae149c | 2017-01-30 16:46:53 -0800 | [diff] [blame] | 1565 | # Write the bug url into the dict. |
| 1566 | if link.bug_id: |
| 1567 | test_info['bug_url'] = link.bug_url |
Shuqian Zhao | 2fecacd | 2015-08-05 22:56:30 -0700 | [diff] [blame] | 1568 | |
| 1569 | # Write the suite timings into |output_dict| |
Allen Li | 2c5d44b | 2016-08-15 17:58:58 -0700 | [diff] [blame] | 1570 | timings = self.timings |
| 1571 | if timings is not None: |
| 1572 | time_dict = output_dict.setdefault('suite_timings', {}) |
| 1573 | time_dict.update({ |
| 1574 | 'download_start' : str(timings.download_start_time), |
| 1575 | 'payload_download_end' : str(timings.payload_end_time), |
| 1576 | 'suite_start' : str(timings.suite_start_time), |
| 1577 | 'artifact_download_end' : str(timings.artifact_end_time), |
| 1578 | 'tests_start' : str(timings.tests_start_time), |
| 1579 | 'tests_end' : str(timings.tests_end_time), |
| 1580 | }) |
Shuqian Zhao | 2fecacd | 2015-08-05 22:56:30 -0700 | [diff] [blame] | 1581 | |
| 1582 | output_dict['suite_job_id'] = self._suite_job_id |
| 1583 | |
| 1584 | return output_dict |
| 1585 | |
| 1586 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1587 | def run(self): |
| 1588 | """Collect test results. |
| 1589 | |
| 1590 | This method goes through the following steps: |
| 1591 | Fetch relevent test views of the suite job. |
| 1592 | Fetch test views of child jobs |
| 1593 | Check whether the suite was aborted. |
Fang Deng | aeab617 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1594 | Generate links. |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1595 | Calculate suite timings. |
| 1596 | Compute return code based on the test result. |
| 1597 | |
| 1598 | """ |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 1599 | if self._solo_test_run: |
Allen Li | 29f5e24 | 2017-07-10 15:00:57 -0700 | [diff] [blame] | 1600 | self._test_views, self._retry_counts, self._missing_results = ( |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 1601 | self._fetch_test_views_of_child_jobs( |
| 1602 | jobs=self._afe.get_jobs(id=self._suite_job_id))) |
| 1603 | else: |
David Riley | dcd1a64 | 2017-03-01 23:15:08 -0800 | [diff] [blame] | 1604 | self._child_views, self._retry_counts, self._missing_results = ( |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 1605 | self._fetch_test_views_of_child_jobs()) |
David Riley | dcd1a64 | 2017-03-01 23:15:08 -0800 | [diff] [blame] | 1606 | self._suite_views = self._fetch_relevant_test_views_of_suite() |
Simran Basi | 17ca77c | 2015-10-14 19:05:00 -0700 | [diff] [blame] | 1607 | self._test_views = self._suite_views + self._child_views |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1608 | # For hostless job in Starting status, there is no test view associated. |
| 1609 | # This can happen when a suite job in Starting status is aborted. When |
| 1610 | # the scheduler hits some limit, e.g., max_hostless_jobs_per_drone, |
| 1611 | # max_jobs_started_per_cycle, a suite job can stays in Starting status. |
| 1612 | if not self._test_views: |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 1613 | self.return_result = _RETURN_RESULTS['test_views_missing'] |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1614 | return |
| 1615 | self.is_aborted = any([view['job_keyvals'].get('aborted_by') |
| 1616 | for view in self._suite_views]) |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1617 | self._generate_web_and_buildbot_links() |
| 1618 | self._record_timings() |
| 1619 | self._compute_return_code() |
| 1620 | |
| 1621 | |
MK Ryu | 977a975 | 2014-10-21 11:58:09 -0700 | [diff] [blame] | 1622 | def gather_timing_stats(self): |
| 1623 | """Collect timing related statistics.""" |
MK Ryu | 977a975 | 2014-10-21 11:58:09 -0700 | [diff] [blame] | 1624 | # Record suite runtime in metadata db. |
Prathmesh Prabhu | a3713a0 | 2015-03-11 13:50:55 -0700 | [diff] [blame] | 1625 | # Some failure modes can leave times unassigned, report sentinel value |
| 1626 | # in that case. |
| 1627 | runtime_in_secs = -1 |
| 1628 | if (self.timings.tests_end_time is not None and |
| 1629 | self.timings.suite_start_time is not None): |
Dan Shi | 0723bf5 | 2015-06-24 10:52:38 -0700 | [diff] [blame] | 1630 | runtime_in_secs = (self.timings.tests_end_time - |
| 1631 | self.timings.suite_start_time).total_seconds() |
Prathmesh Prabhu | a3713a0 | 2015-03-11 13:50:55 -0700 | [diff] [blame] | 1632 | |
MK Ryu | 977a975 | 2014-10-21 11:58:09 -0700 | [diff] [blame] | 1633 | |
Prathmesh Prabhu | 9b8e7ad | 2017-10-30 14:26:13 -0700 | [diff] [blame] | 1634 | def _make_child_deps_from_options(options): |
| 1635 | """Creates a list of extra dependencies for child jobs. |
| 1636 | |
| 1637 | @param options: Parsed arguments to run_suite. |
| 1638 | |
| 1639 | @returns: A list of label strings if any dependencies should be added. None |
| 1640 | otherwise. |
| 1641 | """ |
| 1642 | if not options.model: |
| 1643 | return () |
| 1644 | return ['model:%s' % options.model] |
| 1645 | |
| 1646 | |
Allen Li | 0716efa | 2016-12-08 13:51:31 -0800 | [diff] [blame] | 1647 | @retry.retry(error.StageControlFileFailure, timeout_min=10) |
| 1648 | def create_suite(afe, options): |
| 1649 | """Create a suite with retries. |
| 1650 | |
| 1651 | @param afe: The afe object to insert the new suite job into. |
| 1652 | @param options: The options to use in creating the suite. |
| 1653 | |
| 1654 | @return: The afe_job_id of the new suite job. |
| 1655 | """ |
Prashanth B | 6285f6a | 2014-05-08 18:01:27 -0700 | [diff] [blame] | 1656 | logging.info('%s Submitted create_suite_job rpc', |
| 1657 | diagnosis_utils.JobTimer.format_time(datetime.now())) |
Allen Li | 02b46c5 | 2017-09-11 11:48:12 -0700 | [diff] [blame] | 1658 | |
| 1659 | # TODO(crbug.com/763207): This is to support calling old moblab RPC |
| 1660 | # with ToT code. This does not need to be supported after M62. |
| 1661 | if options.oldrpc: |
| 1662 | suite_args = options.suite_args |
| 1663 | if 'tests' in suite_args: |
| 1664 | # This is for test_that_wrapper |
| 1665 | suite_args = ' '.join([':lab:'] + suite_args['tests']) |
| 1666 | else: |
| 1667 | # This is for suite_attr_wrapper |
| 1668 | suite_args = repr(suite_args) |
| 1669 | options.suite_args = suite_args |
| 1670 | |
Allen Li | 5312170 | 2016-12-08 12:50:22 -0800 | [diff] [blame] | 1671 | return afe.run( |
| 1672 | 'create_suite_job', |
| 1673 | name=options.name, |
| 1674 | board=options.board, |
Xixuan Wu | 7cc10e5 | 2018-04-25 17:04:51 -0700 | [diff] [blame] | 1675 | builds=suite_common.make_builds_from_options(options), |
Allen Li | 5312170 | 2016-12-08 12:50:22 -0800 | [diff] [blame] | 1676 | test_source_build=options.test_source_build, |
Allen Li | 0fd0889 | 2016-12-08 13:47:38 -0800 | [diff] [blame] | 1677 | check_hosts=not options.no_wait, |
Allen Li | 5312170 | 2016-12-08 12:50:22 -0800 | [diff] [blame] | 1678 | pool=options.pool, |
Allen Li | d3758d4 | 2016-12-08 13:46:17 -0800 | [diff] [blame] | 1679 | file_bugs=options.file_bugs, |
Allen Li | 603728a | 2016-12-08 13:58:11 -0800 | [diff] [blame] | 1680 | priority=options.priority, |
Allen Li | 5312170 | 2016-12-08 12:50:22 -0800 | [diff] [blame] | 1681 | suite_args=options.suite_args, |
Allen Li | 0fd0889 | 2016-12-08 13:47:38 -0800 | [diff] [blame] | 1682 | wait_for_results=not options.no_wait, |
Allen Li | 5312170 | 2016-12-08 12:50:22 -0800 | [diff] [blame] | 1683 | timeout_mins=options.timeout_mins + options.delay_minutes, |
| 1684 | max_runtime_mins=options.max_runtime_mins + options.delay_minutes, |
| 1685 | job_retry=options.retry, |
| 1686 | max_retries=options.max_retries, |
| 1687 | suite_min_duts=options.suite_min_duts, |
Allen Li | 40599a3 | 2016-12-08 13:23:35 -0800 | [diff] [blame] | 1688 | offload_failures_only=options.offload_failures_only, |
Allen Li | 5312170 | 2016-12-08 12:50:22 -0800 | [diff] [blame] | 1689 | run_prod_code=options.run_prod_code, |
| 1690 | delay_minutes=options.delay_minutes, |
Shuqian Zhao | 843ae5c7 | 2017-02-22 11:25:01 -0800 | [diff] [blame] | 1691 | job_keyvals=options.job_keyvals, |
Shuqian Zhao | ed0da86 | 2017-03-06 14:47:13 -0800 | [diff] [blame] | 1692 | test_args=options.test_args, |
Prathmesh Prabhu | 9b8e7ad | 2017-10-30 14:26:13 -0700 | [diff] [blame] | 1693 | child_dependencies=_make_child_deps_from_options(options), |
Allen Li | 5312170 | 2016-12-08 12:50:22 -0800 | [diff] [blame] | 1694 | ) |
Prashanth B | 6285f6a | 2014-05-08 18:01:27 -0700 | [diff] [blame] | 1695 | |
| 1696 | |
Allen Li | 5e9c35f | 2017-07-05 14:24:18 -0700 | [diff] [blame] | 1697 | def _run_suite(options): |
Aviv Keshet | 1480c4a | 2013-03-21 16:38:31 -0700 | [diff] [blame] | 1698 | """ |
Shuqian Zhao | 2fecacd | 2015-08-05 22:56:30 -0700 | [diff] [blame] | 1699 | run_suite script without exception handling. |
Shuqian Zhao | d235107 | 2015-08-06 01:48:23 +0000 | [diff] [blame] | 1700 | |
Shuqian Zhao | 2fecacd | 2015-08-05 22:56:30 -0700 | [diff] [blame] | 1701 | @param options: The parsed options. |
| 1702 | |
| 1703 | @returns: A tuple contains the return_code of run_suite and the dictionary |
| 1704 | of the output. |
| 1705 | |
| 1706 | """ |
Shuqian Zhao | ab1bedc | 2015-06-02 11:12:28 -0700 | [diff] [blame] | 1707 | # If indicate to use the new style suite control file, convert the args |
| 1708 | if options.use_suite_attr: |
| 1709 | options = change_options_for_suite_attr(options) |
| 1710 | |
xixuan | 99eba0b | 2017-07-12 15:10:01 -0700 | [diff] [blame] | 1711 | log_name = _get_log_name(options) |
| 1712 | utils.setup_logging(logfile=log_name) |
Alex Miller | 88762a8 | 2013-09-04 15:41:28 -0700 | [diff] [blame] | 1713 | |
John Carey | 1425d29 | 2016-09-30 15:25:09 -0700 | [diff] [blame] | 1714 | if not options.bypass_labstatus and not options.web: |
Fang Deng | 6197da3 | 2014-09-25 10:18:48 -0700 | [diff] [blame] | 1715 | utils.check_lab_status(options.build) |
xixuan | ae791b1 | 2017-06-29 15:40:19 -0700 | [diff] [blame] | 1716 | |
| 1717 | afe = _create_afe(options) |
| 1718 | instance_server = afe.server |
Chris Masone | 359c0fd | 2012-03-13 15:18:59 -0700 | [diff] [blame] | 1719 | |
Dan Shi | 20952c1 | 2014-05-14 17:07:38 -0700 | [diff] [blame] | 1720 | rpc_helper = diagnosis_utils.RPCHelper(afe) |
Fang Deng | 6865aab | 2015-02-20 14:49:47 -0800 | [diff] [blame] | 1721 | is_real_time = True |
Chris Masone | 986459e | 2012-04-11 11:36:48 -0700 | [diff] [blame] | 1722 | if options.mock_job_id: |
| 1723 | job_id = int(options.mock_job_id) |
Fang Deng | 6865aab | 2015-02-20 14:49:47 -0800 | [diff] [blame] | 1724 | existing_job = afe.get_jobs(id=job_id, finished=True) |
| 1725 | if existing_job: |
| 1726 | is_real_time = False |
| 1727 | else: |
| 1728 | existing_job = afe.get_jobs(id=job_id) |
| 1729 | if existing_job: |
| 1730 | job_created_on = time_utils.date_string_to_epoch_time( |
| 1731 | existing_job[0].created_on) |
| 1732 | else: |
| 1733 | raise utils.TestLabException('Failed to retrieve job: %d' % job_id) |
Chris Masone | 986459e | 2012-04-11 11:36:48 -0700 | [diff] [blame] | 1734 | else: |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1735 | try: |
Fang Deng | 6865aab | 2015-02-20 14:49:47 -0800 | [diff] [blame] | 1736 | rpc_helper.check_dut_availability(options.board, options.pool, |
Ningning Xia | f2c206c | 2016-04-13 14:15:51 -0700 | [diff] [blame] | 1737 | options.minimum_duts, |
| 1738 | options.skip_duts_check) |
Prashanth B | 6285f6a | 2014-05-08 18:01:27 -0700 | [diff] [blame] | 1739 | job_id = create_suite(afe, options) |
Fang Deng | 6865aab | 2015-02-20 14:49:47 -0800 | [diff] [blame] | 1740 | job_created_on = time.time() |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1741 | except (error.CrosDynamicSuiteException, |
| 1742 | error.RPCException, proxy.JSONRPCException) as e: |
Allen Li | c3aa769 | 2016-08-08 11:45:00 -0700 | [diff] [blame] | 1743 | logging.exception('Error Message: %s', e) |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 1744 | return run_suite_common.SuiteResult( |
| 1745 | run_suite_common.RETURN_CODES.INFRA_FAILURE, |
| 1746 | {'return_message': str(e)}) |
Xixuan Wu | 610606b | 2017-11-13 14:03:33 -0800 | [diff] [blame] | 1747 | except AttributeError as e: |
| 1748 | logging.exception('Error Message: %s', e) |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 1749 | return run_suite_common.SuiteResult( |
| 1750 | run_suite_common.RETURN_CODES.INVALID_OPTIONS) |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1751 | |
Prashanth B | 923ca26 | 2014-03-14 12:36:29 -0700 | [diff] [blame] | 1752 | job_timer = diagnosis_utils.JobTimer( |
Fang Deng | 6865aab | 2015-02-20 14:49:47 -0800 | [diff] [blame] | 1753 | job_created_on, float(options.timeout_mins)) |
Aviv Keshet | 9afee5e | 2014-10-09 16:33:09 -0700 | [diff] [blame] | 1754 | job_url = reporting_utils.link_job(job_id, |
| 1755 | instance_server=instance_server) |
Prashanth B | 923ca26 | 2014-03-14 12:36:29 -0700 | [diff] [blame] | 1756 | logging.info('%s Created suite job: %s', |
| 1757 | job_timer.format_time(job_timer.job_created_time), |
Aviv Keshet | 9afee5e | 2014-10-09 16:33:09 -0700 | [diff] [blame] | 1758 | job_url) |
Allen Li | e082ced | 2016-09-14 15:19:20 -0700 | [diff] [blame] | 1759 | logging.info(annotations.StepLink( |
| 1760 | text='Link to suite', |
| 1761 | url=job_url)) |
Aviv Keshet | db321de | 2015-04-10 19:09:58 -0700 | [diff] [blame] | 1762 | |
| 1763 | if options.create_and_return: |
Shuqian Zhao | 2fecacd | 2015-08-05 22:56:30 -0700 | [diff] [blame] | 1764 | msg = '--create_and_return was specified, terminating now.' |
| 1765 | logging.info(msg) |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 1766 | return run_suite_common.SuiteResult( |
| 1767 | run_suite_common.RETURN_CODES.OK, |
| 1768 | {'return_message': msg}) |
Aviv Keshet | db321de | 2015-04-10 19:09:58 -0700 | [diff] [blame] | 1769 | |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 1770 | if options.no_wait: |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1771 | return _handle_job_nowait(job_id, options, instance_server) |
Allen Li | 93f4db5 | 2016-09-14 14:44:59 -0700 | [diff] [blame] | 1772 | else: |
| 1773 | return _handle_job_wait(afe, job_id, options, job_timer, is_real_time) |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1774 | |
| 1775 | |
xixuan | 99eba0b | 2017-07-12 15:10:01 -0700 | [diff] [blame] | 1776 | def _get_log_name(options): |
| 1777 | """Return local log file's name. |
| 1778 | |
| 1779 | @param options: Parsed options. |
| 1780 | |
| 1781 | @return log_name, a string file name. |
| 1782 | """ |
| 1783 | if options.require_logfile: |
| 1784 | # options.build is verified to exist in verify_options. |
| 1785 | # convert build name from containing / to containing only _. |
| 1786 | log_name = 'run_suite-%s.log' % options.build.replace('/', '_') |
| 1787 | log_dir = os.path.join(common.autotest_dir, 'logs') |
| 1788 | if os.path.exists(log_dir): |
| 1789 | log_name = os.path.join(log_dir, log_name) |
| 1790 | |
| 1791 | return log_name |
| 1792 | else: |
| 1793 | return None |
| 1794 | |
| 1795 | |
xixuan | ae791b1 | 2017-06-29 15:40:19 -0700 | [diff] [blame] | 1796 | def _create_afe(options): |
| 1797 | """Return an afe instance based on options. |
| 1798 | |
| 1799 | @param options Parsed options. |
| 1800 | |
| 1801 | @return afe, an AFE instance. |
| 1802 | """ |
| 1803 | instance_server = (options.web if options.web else |
| 1804 | instance_for_pool(options.pool)) |
| 1805 | afe = frontend_wrappers.RetryingAFE(server=instance_server, |
| 1806 | timeout_min=options.afe_timeout_mins, |
| 1807 | delay_sec=options.delay_sec) |
| 1808 | logging.info('Autotest instance created: %s', instance_server) |
| 1809 | return afe |
| 1810 | |
| 1811 | |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1812 | def _handle_job_wait(afe, job_id, options, job_timer, is_real_time): |
| 1813 | """Handle suite job synchronously. |
| 1814 | |
| 1815 | @param afe AFE instance. |
| 1816 | @param job_id Suite job id. |
| 1817 | @param options Parsed options. |
| 1818 | @param job_timer JobTimer for suite job. |
| 1819 | @param is_real_time Whether or not to handle job timeout. |
| 1820 | |
| 1821 | @return SuiteResult of suite job. |
| 1822 | """ |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1823 | rpc_helper = diagnosis_utils.RPCHelper(afe) |
| 1824 | instance_server = afe.server |
| 1825 | while not afe.get_jobs(id=job_id, finished=True): |
Allen Li | 425d91f | 2017-07-10 15:14:20 -0700 | [diff] [blame] | 1826 | _poke_buildbot_with_output(afe, job_id, job_timer) |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1827 | if job_timer.debug_output_timer.poll(): |
| 1828 | logging.info('The suite job has another %s till timeout.', |
Allen Li | 425d91f | 2017-07-10 15:14:20 -0700 | [diff] [blame] | 1829 | job_timer.timeout_hours - job_timer.elapsed_time()) |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1830 | time.sleep(10) |
xixuan | a96bd21 | 2017-01-13 12:51:22 +0800 | [diff] [blame] | 1831 | logging.info('%s Suite job is finished.', |
| 1832 | diagnosis_utils.JobTimer.format_time(datetime.now())) |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1833 | # For most cases, ResultCollector should be able to determine whether |
| 1834 | # a suite has timed out by checking information in the test view. |
| 1835 | # However, occationally tko parser may fail on parsing the |
| 1836 | # job_finished time from the job's keyval file. So we add another |
| 1837 | # layer of timeout check in run_suite. We do the check right after |
| 1838 | # the suite finishes to make it as accurate as possible. |
| 1839 | # There is a minor race condition here where we might have aborted |
| 1840 | # for some reason other than a timeout, and the job_timer thinks |
| 1841 | # it's a timeout because of the jitter in waiting for results. |
| 1842 | # The consequence would be that run_suite exits with code |
| 1843 | # SUITE_TIMEOUT while it should have returned INFRA_FAILURE |
| 1844 | # instead, which should happen very rarely. |
| 1845 | # Note the timeout will have no sense when using -m option. |
| 1846 | is_suite_timeout = job_timer.is_suite_timeout() |
| 1847 | |
| 1848 | # Extract the original suite name to record timing. |
| 1849 | original_suite_name = get_original_suite_name(options.name, |
Allen Li | 425d91f | 2017-07-10 15:14:20 -0700 | [diff] [blame] | 1850 | options.suite_args) |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1851 | # Start collecting test results. |
Aseda Aboagye | d72df75 | 2017-05-22 14:30:11 -0700 | [diff] [blame] | 1852 | logging.info('%s Start collecting test results and dump them to json.', |
xixuan | a96bd21 | 2017-01-13 12:51:22 +0800 | [diff] [blame] | 1853 | diagnosis_utils.JobTimer.format_time(datetime.now())) |
Alex Miller | c7a5952 | 2013-10-30 15:18:57 -0700 | [diff] [blame] | 1854 | TKO = frontend_wrappers.RetryingTKO(server=instance_server, |
Simran Basi | 25effe3 | 2013-11-26 13:02:11 -0800 | [diff] [blame] | 1855 | timeout_min=options.afe_timeout_mins, |
Chris Masone | 8ac6671 | 2012-02-15 14:21:02 -0800 | [diff] [blame] | 1856 | delay_sec=options.delay_sec) |
Allen Li | 637683b | 2017-11-06 17:36:27 -0800 | [diff] [blame] | 1857 | # TODO(crbug.com/672348): It needs to be possible for provision |
| 1858 | # suite to pass if only a few tests fail. Otherwise, a single |
| 1859 | # failing test will be reported as failure even if the suite reports |
| 1860 | # success. |
Allen Li | 977760b | 2017-11-06 18:11:37 -0800 | [diff] [blame] | 1861 | if options.name == _PROVISION_SUITE: |
Allen Li | 637683b | 2017-11-06 17:36:27 -0800 | [diff] [blame] | 1862 | # TODO(crbug.com/672348): Creating the suite job requires that |
| 1863 | # suite_args contains num_required. |
Allen Li | 422f24b | 2017-07-12 15:15:43 -0700 | [diff] [blame] | 1864 | return_code_function = _ProvisionReturnCodeComputer( |
| 1865 | num_required=options.suite_args['num_required']) |
| 1866 | else: |
| 1867 | return_code_function = _ReturnCodeComputer() |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1868 | collector = ResultCollector(instance_server=instance_server, |
| 1869 | afe=afe, tko=TKO, build=options.build, |
| 1870 | board=options.board, |
| 1871 | suite_name=options.name, |
| 1872 | suite_job_id=job_id, |
Allen Li | 422f24b | 2017-07-12 15:15:43 -0700 | [diff] [blame] | 1873 | return_code_function=return_code_function, |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1874 | original_suite_name=original_suite_name) |
| 1875 | collector.run() |
| 1876 | # Dump test outputs into json. |
| 1877 | output_dict = collector.get_results_dict() |
| 1878 | output_dict['autotest_instance'] = instance_server |
| 1879 | if not options.json_dump: |
| 1880 | collector.output_results() |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 1881 | result = collector.return_result |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1882 | if is_real_time: |
| 1883 | # Do not record stats if the suite was aborted (either by a user |
| 1884 | # or through the golo rpc). |
| 1885 | # Also do not record stats if is_aborted is None, indicating |
| 1886 | # aborting status is unknown yet. |
| 1887 | if collector.is_aborted == False: |
xixuan | a96bd21 | 2017-01-13 12:51:22 +0800 | [diff] [blame] | 1888 | logging.info('%s Gathering timing stats for the suite job.', |
| 1889 | diagnosis_utils.JobTimer.format_time(datetime.now())) |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1890 | collector.gather_timing_stats() |
J. Richard Barnette | 712eb40 | 2013-08-13 18:03:00 -0700 | [diff] [blame] | 1891 | |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1892 | if collector.is_aborted == True and is_suite_timeout: |
| 1893 | # There are two possible cases when a suite times out. |
| 1894 | # 1. the suite job was aborted due to timing out |
| 1895 | # 2. the suite job succeeded, but some child jobs |
| 1896 | # were already aborted before the suite job exited. |
| 1897 | # The case 2 was handled by ResultCollector, |
| 1898 | # here we handle case 1. |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 1899 | result |= _RETURN_RESULTS['suite_timeout'] |
xixuan | a96bd21 | 2017-01-13 12:51:22 +0800 | [diff] [blame] | 1900 | logging.info('\n %s Attempting to display pool info: %s', |
| 1901 | diagnosis_utils.JobTimer.format_time(datetime.now()), |
| 1902 | options.pool) |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1903 | try: |
| 1904 | # Add some jitter to make up for any latency in |
| 1905 | # aborting the suite or checking for results. |
Allen Li | 0b675b6 | 2017-07-05 13:38:04 -0700 | [diff] [blame] | 1906 | cutoff = job_timer.timeout_hours + timedelta(hours=0.3) |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1907 | rpc_helper.diagnose_pool( |
| 1908 | options.board, options.pool, cutoff) |
Allen Li | d4aa2fb | 2016-12-08 14:03:54 -0800 | [diff] [blame] | 1909 | except proxy.JSONRPCException: |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1910 | logging.warning('Unable to display pool info.') |
Aviv Keshet | 6b1122d | 2016-06-20 13:29:52 -0700 | [diff] [blame] | 1911 | |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1912 | # And output return message. |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 1913 | if result.message: |
| 1914 | logging.info('Reason: %s', result.message) |
Fang Deng | 5a43be6 | 2014-05-07 17:17:04 -0700 | [diff] [blame] | 1915 | |
xixuan | a96bd21 | 2017-01-13 12:51:22 +0800 | [diff] [blame] | 1916 | logging.info('\n %s Output below this line is for buildbot consumption:', |
| 1917 | diagnosis_utils.JobTimer.format_time(datetime.now())) |
Allen Li | 28be064 | 2017-07-10 15:16:26 -0700 | [diff] [blame] | 1918 | log_buildbot_links(logging.info, collector.buildbot_links) |
Allen Li | cc20549 | 2017-07-10 17:26:04 -0700 | [diff] [blame] | 1919 | return result.suite_result(output_dict) |
Prashanth B | 923ca26 | 2014-03-14 12:36:29 -0700 | [diff] [blame] | 1920 | |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1921 | |
| 1922 | def _handle_job_nowait(job_id, options, instance_server): |
| 1923 | """Handle suite job asynchronously. |
| 1924 | |
| 1925 | @param job_id Suite job id. |
| 1926 | @param options Parsed options. |
| 1927 | @param instance_server Autotest instance hostname. |
| 1928 | |
| 1929 | @return SuiteResult of suite job. |
| 1930 | """ |
| 1931 | logging.info('Created suite job: %r', job_id) |
| 1932 | link = LogLink(options.name, instance_server, |
Allen Li | 0b675b6 | 2017-07-05 13:38:04 -0700 | [diff] [blame] | 1933 | '%s-%s' % (job_id, getpass.getuser())) |
Allen Li | 340414e | 2016-08-16 14:19:08 -0700 | [diff] [blame] | 1934 | for generate_link in link.GenerateBuildbotLinks(): |
| 1935 | logging.info(generate_link) |
| 1936 | logging.info('--no_wait specified; Exiting.') |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 1937 | return run_suite_common.SuiteResult( |
| 1938 | run_suite_common.RETURN_CODES.OK, |
| 1939 | {'return_message': '--no_wait specified; Exiting.'}) |
Chris Masone | 24b80f1 | 2012-02-14 14:18:01 -0800 | [diff] [blame] | 1940 | |
Fang Deng | dd20e45 | 2014-04-07 15:39:47 -0700 | [diff] [blame] | 1941 | |
xixuan | ae791b1 | 2017-06-29 15:40:19 -0700 | [diff] [blame] | 1942 | def _should_run(options): |
| 1943 | """Check whether the suite should be run based on lab/job status checking. |
| 1944 | |
| 1945 | @param options Parsed options. |
| 1946 | """ |
| 1947 | try: |
| 1948 | site_utils.check_lab_status(options.test_source_build) |
| 1949 | except site_utils.TestLabException as ex: |
| 1950 | logging.exception('Lab is closed or build is blocked. Skipping ' |
| 1951 | 'suite %s, board %s, build %s: %s', |
| 1952 | options.name, options.board, |
| 1953 | options.test_source_build, str(ex)) |
| 1954 | return False |
| 1955 | |
| 1956 | start_time = str(datetime.now() - |
| 1957 | timedelta(days=_SEARCH_JOB_MAX_DAYS)) |
| 1958 | afe = _create_afe(options) |
Xixuan Wu | f8ca782 | 2017-09-07 17:28:09 -0700 | [diff] [blame] | 1959 | afe_job_id = afe.get_jobs( |
xixuan | ae791b1 | 2017-06-29 15:40:19 -0700 | [diff] [blame] | 1960 | name__istartswith=options.test_source_build, |
| 1961 | name__iendswith='control.'+options.name, |
| 1962 | created_on__gte=start_time, |
| 1963 | min_rpc_timeout=_MIN_RPC_TIMEOUT) |
Xixuan Wu | f8ca782 | 2017-09-07 17:28:09 -0700 | [diff] [blame] | 1964 | if afe_job_id: |
| 1965 | logging.info('Found duplicate suite %s scheduled in past.', |
| 1966 | afe_job_id) |
| 1967 | return False |
xixuan | ae791b1 | 2017-06-29 15:40:19 -0700 | [diff] [blame] | 1968 | |
Xixuan Wu | 3ae6e8b | 2017-10-12 09:57:07 -0700 | [diff] [blame] | 1969 | return True |
| 1970 | |
Shuqian Zhao | 2fecacd | 2015-08-05 22:56:30 -0700 | [diff] [blame] | 1971 | |
Allen Li | 425d91f | 2017-07-10 15:14:20 -0700 | [diff] [blame] | 1972 | def _poke_buildbot_with_output(afe, job_id, job_timer): |
| 1973 | """Poke buildbot so it doesn't timeout from silence. |
| 1974 | |
| 1975 | @param afe AFE instance. |
| 1976 | @param job_id Suite job id. |
| 1977 | @param job_timer JobTimer for suite job. |
| 1978 | """ |
| 1979 | rpc_helper = diagnosis_utils.RPCHelper(afe) |
| 1980 | # Note that this call logs output, preventing buildbot's |
| 1981 | # 9000 second silent timeout from kicking in. Let there be no |
| 1982 | # doubt, this is a hack. The timeout is from upstream buildbot and |
| 1983 | # this is the easiest work around. |
| 1984 | if job_timer.first_past_halftime(): |
| 1985 | rpc_helper.diagnose_job(job_id, afe.server) |
| 1986 | |
| 1987 | |
| 1988 | |
Allen Li | 85ae5df | 2017-07-10 14:58:16 -0700 | [diff] [blame] | 1989 | def _run_task(options): |
Allen Li | 5e9c35f | 2017-07-05 14:24:18 -0700 | [diff] [blame] | 1990 | """Perform this script's function minus setup. |
Aviv Keshet | 97bebd4 | 2017-05-24 21:02:32 -0700 | [diff] [blame] | 1991 | |
Allen Li | 5e9c35f | 2017-07-05 14:24:18 -0700 | [diff] [blame] | 1992 | Boilerplate like argument parsing, logging, output formatting happen |
| 1993 | elsewhere. |
Allen Li | 85ae5df | 2017-07-10 14:58:16 -0700 | [diff] [blame] | 1994 | |
| 1995 | Returns a SuiteResult instance. |
| 1996 | |
| 1997 | TODO(ayatane): The try/except should be moved into _run_suite(). |
| 1998 | Good luck trying to figure out which function calls are supposed to |
| 1999 | raise which of the exceptions. |
Allen Li | 5e9c35f | 2017-07-05 14:24:18 -0700 | [diff] [blame] | 2000 | """ |
Fang Deng | fb4a949 | 2014-09-18 17:52:06 -0700 | [diff] [blame] | 2001 | try: |
Allen Li | 85ae5df | 2017-07-10 14:58:16 -0700 | [diff] [blame] | 2002 | return _run_suite(options) |
Shuqian Zhao | ade6e7d | 2015-12-07 18:01:11 -0800 | [diff] [blame] | 2003 | except diagnosis_utils.BoardNotAvailableError as e: |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 2004 | result = run_suite_common.SuiteResult( |
| 2005 | run_suite_common.RETURN_CODES.BOARD_NOT_AVAILABLE, |
Allen Li | 85ae5df | 2017-07-10 14:58:16 -0700 | [diff] [blame] | 2006 | {'return_message': 'Skipping testing: %s' % e.message}) |
| 2007 | logging.info(result.output_dict['return_message']) |
| 2008 | return result |
Shuqian Zhao | ade6e7d | 2015-12-07 18:01:11 -0800 | [diff] [blame] | 2009 | except utils.TestLabException as e: |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 2010 | result = run_suite_common.SuiteResult( |
| 2011 | run_suite_common.RETURN_CODES.INFRA_FAILURE, |
Allen Li | 85ae5df | 2017-07-10 14:58:16 -0700 | [diff] [blame] | 2012 | {'return_message': 'TestLabException: %s' % e}) |
| 2013 | logging.exception(result.output_dict['return_message']) |
| 2014 | return result |
| 2015 | |
| 2016 | |
| 2017 | class _ExceptionHandler(object): |
| 2018 | """Global exception handler replacement.""" |
| 2019 | |
| 2020 | def __init__(self, dump_json): |
| 2021 | """Initialize instance. |
| 2022 | |
| 2023 | @param dump_json: Whether to print a JSON dump of the result dict to |
| 2024 | stdout. |
| 2025 | """ |
| 2026 | self._should_dump_json = dump_json |
| 2027 | |
| 2028 | def __call__(self, exc_type, value, traceback): |
| 2029 | if self._should_dump_json: |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 2030 | run_suite_common.dump_json( |
| 2031 | {'return_message': ('Unhandled run_suite exception: %s' |
| 2032 | % value)}) |
| 2033 | sys.exit(run_suite_common.RETURN_CODES.INFRA_FAILURE) |
Allen Li | 5e9c35f | 2017-07-05 14:24:18 -0700 | [diff] [blame] | 2034 | |
| 2035 | |
| 2036 | def main(): |
| 2037 | """Entry point.""" |
| 2038 | utils.verify_not_root_user() |
| 2039 | |
| 2040 | parser = make_parser() |
| 2041 | options = parser.parse_args() |
| 2042 | if options.do_nothing: |
Allen Li | 85ae5df | 2017-07-10 14:58:16 -0700 | [diff] [blame] | 2043 | return 0 |
| 2044 | |
| 2045 | sys.exceptionhandler = _ExceptionHandler(dump_json=options.json_dump) |
Allen Li | 5e9c35f | 2017-07-05 14:24:18 -0700 | [diff] [blame] | 2046 | if options.json_dump: |
| 2047 | logging.disable(logging.CRITICAL) |
| 2048 | |
Allen Li | 85ae5df | 2017-07-10 14:58:16 -0700 | [diff] [blame] | 2049 | options_okay = verify_and_clean_options(options) |
Xixuan Wu | f8ca782 | 2017-09-07 17:28:09 -0700 | [diff] [blame] | 2050 | # Set StreamHandler first to capture error messages if suite is not run. |
| 2051 | utils.setup_logging() |
Allen Li | 85ae5df | 2017-07-10 14:58:16 -0700 | [diff] [blame] | 2052 | if not options_okay: |
| 2053 | parser.print_help() |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 2054 | result = run_suite_common.SuiteResult( |
| 2055 | run_suite_common.RETURN_CODES.INVALID_OPTIONS) |
Allen Li | 85ae5df | 2017-07-10 14:58:16 -0700 | [diff] [blame] | 2056 | elif options.pre_check and not _should_run(options): |
Xixuan Wu | f8ca782 | 2017-09-07 17:28:09 -0700 | [diff] [blame] | 2057 | logging.info('Suite %s-%s is terminated: Lab is closed, OR build is ' |
| 2058 | 'blocked, OR this suite has already been kicked off ' |
Allen Li | 85ae5df | 2017-07-10 14:58:16 -0700 | [diff] [blame] | 2059 | 'once in past %d days.', |
| 2060 | options.test_source_build, options.name, |
| 2061 | _SEARCH_JOB_MAX_DAYS) |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 2062 | result = run_suite_common.SuiteResult( |
| 2063 | run_suite_common.RETURN_CODES.ERROR, |
Allen Li | 85ae5df | 2017-07-10 14:58:16 -0700 | [diff] [blame] | 2064 | {'return_message': ("Lab is closed OR other reason" |
| 2065 | " (see code, it's complicated)")}) |
| 2066 | else: |
| 2067 | result = _run_task(options) |
Shuqian Zhao | 2fecacd | 2015-08-05 22:56:30 -0700 | [diff] [blame] | 2068 | |
Shuqian Zhao | 2fecacd | 2015-08-05 22:56:30 -0700 | [diff] [blame] | 2069 | if options.json_dump: |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 2070 | run_suite_common.dump_json(result.output_dict) |
Fang Deng | 6197da3 | 2014-09-25 10:18:48 -0700 | [diff] [blame] | 2071 | |
| 2072 | logging.info('Will return from run_suite with status: %s', |
Xixuan Wu | 888ee7a | 2018-04-24 10:27:27 -0700 | [diff] [blame] | 2073 | run_suite_common.RETURN_CODES.get_string(result.return_code)) |
Allen Li | 85ae5df | 2017-07-10 14:58:16 -0700 | [diff] [blame] | 2074 | return result.return_code |
| 2075 | |
| 2076 | |
Chris Masone | 24b80f1 | 2012-02-14 14:18:01 -0800 | [diff] [blame] | 2077 | if __name__ == "__main__": |
| 2078 | sys.exit(main()) |