Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # Copyright 2018 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 | """Evaluate ChromeOS autotest. |
| 7 | |
| 8 | Note that by default 'test_that' will install dependency packages of autotest |
| 9 | only once. For example, if you overwrote chrome's unittest binary, your new |
Kuang-che Wu | 927231f | 2018-07-24 14:21:56 +0800 | [diff] [blame] | 10 | binary will be persistent across autotest runs. Add --reinstall if you want |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 11 | clean autotest install. |
| 12 | """ |
| 13 | from __future__ import print_function |
| 14 | import argparse |
| 15 | import json |
| 16 | import logging |
| 17 | import os |
| 18 | import re |
| 19 | import subprocess |
| 20 | import sys |
| 21 | |
| 22 | from bisect_kit import cli |
| 23 | from bisect_kit import common |
| 24 | from bisect_kit import configure |
| 25 | from bisect_kit import cros_util |
| 26 | from bisect_kit import util |
| 27 | |
| 28 | logger = logging.getLogger(__name__) |
| 29 | |
| 30 | OLD = 'old' |
| 31 | NEW = 'new' |
| 32 | SKIP = 'skip' |
| 33 | FATAL = 'fatal' |
| 34 | |
| 35 | EXIT_CODE_MAP = { |
Kuang-che Wu | 0476d1f | 2019-03-04 19:27:01 +0800 | [diff] [blame] | 36 | OLD: cli.EXIT_CODE_OLD, |
| 37 | NEW: cli.EXIT_CODE_NEW, |
| 38 | SKIP: cli.EXIT_CODE_SKIP, |
| 39 | FATAL: cli.EXIT_CODE_FATAL, |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | |
| 43 | def create_argument_parser(): |
| 44 | parser = argparse.ArgumentParser(description=__doc__) |
| 45 | common.add_common_arguments(parser) |
| 46 | parser.add_argument( |
| 47 | 'dut', |
| 48 | nargs='?', |
| 49 | type=cli.argtype_notempty, |
| 50 | metavar='DUT', |
| 51 | default=configure.get('DUT', '')) |
| 52 | parser.add_argument( |
| 53 | '--chromeos_root', |
| 54 | type=cli.argtype_dir_path, |
| 55 | metavar='CHROMEOS_ROOT', |
| 56 | default=configure.get('CHROMEOS_ROOT', ''), |
| 57 | help='ChromeOS tree root') |
| 58 | parser.add_argument( |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 59 | '--chrome_root', |
| 60 | metavar='CHROME_ROOT', |
| 61 | type=cli.argtype_dir_path, |
| 62 | default=configure.get('CHROME_ROOT'), |
| 63 | help='Chrome tree root; necessary for telemetry tests') |
| 64 | parser.add_argument( |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 65 | '--prebuilt', |
| 66 | action='store_true', |
| 67 | help='Run autotest using existing prebuilt package if specified; ' |
| 68 | 'otherwise use the default one') |
| 69 | parser.add_argument( |
| 70 | '--reinstall', |
| 71 | action='store_true', |
| 72 | help='Remove existing autotest folder on the DUT first') |
Kuang-che Wu | da3abfe | 2019-03-21 14:48:12 +0800 | [diff] [blame] | 73 | parser.add_argument( |
| 74 | '--reboot_before_test', |
| 75 | action='store_true', |
| 76 | help='Reboot before test run') |
Kuang-che Wu | 85c613c | 2019-01-09 15:46:11 +0800 | [diff] [blame] | 77 | |
| 78 | group = parser.add_argument_group(title='Options for normal autotest tests') |
| 79 | group.add_argument( |
| 80 | '--test_name', help='Test name, like "video_VideoDecodeAccelerator.h264"') |
| 81 | group.add_argument( |
Kuang-che Wu | 0a4304a | 2019-01-19 01:32:11 +0800 | [diff] [blame] | 82 | '--fail_to_pass', |
| 83 | action='store_true', |
| 84 | help='For functional tests: old behavior is FAIL and new behavior is ' |
| 85 | 'PASS; If not specified, default = old behavior is PASS and new ' |
| 86 | 'behavior is FAIL') |
| 87 | group.add_argument( |
Kuang-che Wu | 85c613c | 2019-01-09 15:46:11 +0800 | [diff] [blame] | 88 | '--metric', |
| 89 | help= |
| 90 | 'Metric name of performance test; example: "cheets_SystemRawImageSize"') |
| 91 | group.add_argument( |
| 92 | '--old_value', |
| 93 | type=float, |
| 94 | help='For performance test, old value of given metric') |
| 95 | group.add_argument( |
| 96 | '--new_value', |
| 97 | type=float, |
| 98 | help='For performance test, new value of given metric') |
| 99 | group.add_argument( |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 100 | '--args', |
| 101 | help='Extra args passed to "test_that --args"; Overrides the default') |
| 102 | |
Kuang-che Wu | 85c613c | 2019-01-09 15:46:11 +0800 | [diff] [blame] | 103 | group = parser.add_argument_group(title='Options for CTS/GTS tests') |
| 104 | group.add_argument('--cts_revision', help='CTS revision, like "9.0_r3"') |
Kuang-che Wu | 63f836a | 2019-02-21 16:33:32 +0000 | [diff] [blame] | 105 | group.add_argument('--cts_abi', choices=['arm', 'x86']) |
Kuang-che Wu | 85c613c | 2019-01-09 15:46:11 +0800 | [diff] [blame] | 106 | group.add_argument( |
| 107 | '--cts_prefix', |
| 108 | help='Prefix of autotest test name, ' |
| 109 | 'like cheets_CTS_N, cheets_CTS_P, cheets_GTS') |
| 110 | group.add_argument( |
| 111 | '--cts_module', help='CTS/GTS module name, like "CtsCameraTestCases"') |
| 112 | group.add_argument( |
| 113 | '--cts_test', |
| 114 | help='CTS/GTS test name, like ' |
| 115 | '"android.hardware.cts.CameraTest#testDisplayOrientation"') |
| 116 | group.add_argument('--cts_timeout', type=float, help='timeout, in seconds') |
| 117 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 118 | return parser |
| 119 | |
| 120 | |
| 121 | def parse_test_report_log(result_log, metric): |
| 122 | """Parses autotest result log. |
| 123 | |
| 124 | Args: |
| 125 | result_log: content of test_report.log |
| 126 | metric: what metric to capture if not None |
| 127 | |
| 128 | Returns: |
| 129 | passed, values: |
| 130 | passed: True if test run successfully |
| 131 | values: captured metric values; None if test failed or metric is None |
| 132 | """ |
| 133 | m = re.search(r'Total PASS: (\d+)/(\d+)', result_log) |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 134 | passed = (m and m.group(1) == m.group(2)) |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 135 | |
| 136 | if not metric: |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 137 | return passed, None |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 138 | |
| 139 | values = [] |
| 140 | for line in result_log.splitlines(): |
| 141 | m = re.match(r'^(\S+)\s+(\w+)(?:\{\d+\})?\s+(\d+\.\d+)$', line) |
| 142 | if not m: |
| 143 | continue |
| 144 | if m.group(2) == metric: |
| 145 | values.append(float(m.group(3))) |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 146 | return passed, values |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 147 | |
| 148 | |
| 149 | def parse_test_result_chart(json_path, metric): |
| 150 | data = json.load(open(json_path)) |
Kuang-che Wu | 3331caf | 2018-09-06 19:47:02 +0800 | [diff] [blame] | 151 | |
| 152 | # format 1, telemetry |
| 153 | if 'charts' in data: |
| 154 | summary = data['charts'][metric]['summary'] |
| 155 | |
| 156 | # format 2, autotest without graph |
| 157 | elif metric in data: |
| 158 | summary = data[metric]['summary'] |
| 159 | |
| 160 | # format 3, autotest with graph |
| 161 | elif metric.count('.') == 1: |
| 162 | name, subname = metric.split('.') |
| 163 | summary = data[name][subname] |
| 164 | |
| 165 | else: |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 166 | logger.error('metric "%s" not in %s', metric, json_path) |
Kuang-che Wu | dd80267 | 2018-08-10 19:40:14 +0800 | [diff] [blame] | 167 | return [] |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 168 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 169 | if 'values' in summary: |
| 170 | return summary['values'] |
| 171 | return [summary['value']] |
| 172 | |
| 173 | |
| 174 | def get_additional_test_args(test_name): |
| 175 | """Gets extra arguments to specific test. |
| 176 | |
| 177 | Some tests may require special arguments to run. |
| 178 | |
| 179 | Args: |
| 180 | test_name: test name |
| 181 | |
| 182 | Returns: |
| 183 | arguments (str) |
| 184 | """ |
| 185 | if test_name.startswith('telemetry_'): |
| 186 | return 'local=True' |
| 187 | return '' |
| 188 | |
| 189 | |
Kuang-che Wu | 6c5a5b2 | 2019-01-17 18:09:50 +0800 | [diff] [blame] | 190 | def prepare_to_run_test(opts): |
| 191 | # Some versions of ChromeOS SDK is broken and ship bad 'ssh' executable. This |
Kuang-che Wu | 9fcf108 | 2019-03-04 11:24:04 +0800 | [diff] [blame] | 192 | # works around the issue. See crbug/906289 for detail. |
| 193 | # TODO(kcwu): remove this workaround once we no longer support bisecting |
| 194 | # versions earlier than R73-11445.0.0. |
| 195 | ssh_path = os.path.join(opts.chromeos_root, 'chroot/usr/bin/ssh') |
Kuang-che Wu | 4fbd2d3 | 2019-03-07 01:07:57 +0800 | [diff] [blame] | 196 | if os.path.exists(ssh_path): |
| 197 | if 'file descriptor passing not supported' in open(ssh_path).read(): |
| 198 | cros_util.cros_sdk(opts.chromeos_root, 'sudo', 'emerge', |
| 199 | 'net-misc/openssh') |
Kuang-che Wu | 6c5a5b2 | 2019-01-17 18:09:50 +0800 | [diff] [blame] | 200 | |
| 201 | if opts.reinstall: |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 202 | util.ssh_cmd(opts.dut, 'rm', '-rf', '/usr/local/autotest') |
Kuang-che Wu | 6c5a5b2 | 2019-01-17 18:09:50 +0800 | [diff] [blame] | 203 | |
Kuang-che Wu | da3abfe | 2019-03-21 14:48:12 +0800 | [diff] [blame] | 204 | if opts.reboot_before_test: |
| 205 | cros_util.reboot(opts.dut) |
| 206 | |
Kuang-che Wu | 6c5a5b2 | 2019-01-17 18:09:50 +0800 | [diff] [blame] | 207 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 208 | def run_test(opts): |
| 209 | """Runs an autotest test. |
| 210 | |
| 211 | Args: |
| 212 | opts: An argparse.Namespace to hold command line arguments. |
| 213 | |
| 214 | Returns: |
| 215 | path of test result (outside chroot) |
| 216 | """ |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 217 | prebuilt_autotest_dir = os.path.join(cros_util.chromeos_root_inside_chroot, |
| 218 | cros_util.prebuilt_autotest_dir) |
| 219 | # Set results dir inside source tree, so it's easier to access them outside |
| 220 | # chroot. |
| 221 | results_dir = os.path.join(cros_util.chromeos_root_inside_chroot, |
| 222 | 'tmp/autotest_results_tmp') |
| 223 | if opts.prebuilt: |
| 224 | test_that_bin = os.path.join(prebuilt_autotest_dir, |
| 225 | 'site_utils/test_that.py') |
| 226 | else: |
| 227 | test_that_bin = '/usr/bin/test_that' |
Kuang-che Wu | f3d03ca | 2019-03-11 17:31:40 +0800 | [diff] [blame] | 228 | cmd = [ |
| 229 | test_that_bin, opts.dut, opts.test_name, '--debug', '--results_dir', |
| 230 | results_dir |
| 231 | ] |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 232 | if opts.prebuilt: |
| 233 | cmd += ['--autotest_dir', prebuilt_autotest_dir] |
| 234 | |
| 235 | args = get_additional_test_args(opts.test_name) |
| 236 | if opts.args: |
| 237 | if args: |
Kuang-che Wu | 74768d3 | 2018-09-07 12:03:24 +0800 | [diff] [blame] | 238 | logger.info( |
| 239 | 'default test_that args `%s` is overridden by ' |
| 240 | 'command line option `%s`', args, opts.args) |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 241 | cmd += ['--args', opts.args] |
| 242 | elif args: |
| 243 | cmd += ['--args', args] |
| 244 | |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 245 | try: |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 246 | output = cros_util.cros_sdk( |
| 247 | opts.chromeos_root, *cmd, chrome_root=opts.chrome_root) |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 248 | except subprocess.CalledProcessError as e: |
| 249 | output = e.output |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 250 | |
| 251 | m = re.search(r'Finished running tests. Results can be found in (\S+)', |
| 252 | output) |
| 253 | if not m: |
| 254 | logger.error('result dir is unknown') |
| 255 | return None |
| 256 | assert m.group(1) == results_dir |
| 257 | return results_dir.replace(cros_util.chromeos_root_inside_chroot, |
| 258 | opts.chromeos_root) |
| 259 | |
| 260 | |
| 261 | def gather_test_result(opts, result_dir): |
| 262 | result_log_path = os.path.join(result_dir, 'test_report.log') |
| 263 | result_log = open(result_log_path).read() |
| 264 | |
| 265 | passed, values = parse_test_report_log(result_log, opts.metric) |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 266 | if opts.metric and not values: |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 267 | values = [] |
| 268 | for root, _, files in os.walk(result_dir): |
| 269 | for filename in files: |
| 270 | if filename != 'results-chart.json': |
| 271 | continue |
| 272 | full_path = os.path.join(root, filename) |
| 273 | values += parse_test_result_chart(full_path, opts.metric) |
| 274 | |
| 275 | return passed, values |
| 276 | |
| 277 | |
| 278 | def main(args=None): |
| 279 | common.init() |
| 280 | parser = create_argument_parser() |
| 281 | opts = parser.parse_args(args) |
| 282 | common.config_logging(opts) |
| 283 | |
| 284 | if not cros_util.is_dut(opts.dut): |
Kuang-che Wu | 4a75f95 | 2019-03-26 17:22:42 +0800 | [diff] [blame] | 285 | logger.error('%r is not a valid DUT address', opts.dut) |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 286 | return FATAL |
| 287 | |
Kuang-che Wu | 85c613c | 2019-01-09 15:46:11 +0800 | [diff] [blame] | 288 | is_cts = ( |
| 289 | opts.cts_revision or opts.cts_abi or opts.cts_prefix or opts.cts_module or |
| 290 | opts.cts_test or opts.cts_timeout) |
| 291 | if is_cts: |
| 292 | if opts.test_name or opts.metric or opts.args: |
| 293 | parser.error( |
| 294 | 'do not specify --test_name, --metric, --args for CTS/GTS tests') |
| 295 | opts.test_name = '%s.tradefed-run-test' % opts.cts_prefix |
| 296 | opts.args = 'module=%s test=%s' % (opts.cts_module, opts.cts_test) |
| 297 | if opts.cts_revision: |
| 298 | opts.args += ' revision=%s' % opts.cts_revision |
| 299 | if opts.cts_abi: |
| 300 | opts.args += ' abi=%s' % opts.cts_abi |
| 301 | if opts.cts_timeout: |
| 302 | opts.args += ' timeout=%s' % opts.cts_timeout |
| 303 | else: |
| 304 | if not opts.test_name: |
| 305 | parser.error('argument --test_name is required') |
| 306 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 307 | # Verify command line options. |
| 308 | if opts.metric: |
| 309 | if opts.old_value is None: |
| 310 | logger.error('--old_value is not provided') |
| 311 | return FATAL |
| 312 | if opts.new_value is None: |
| 313 | logger.error('--new_value is not provided') |
| 314 | return FATAL |
Kuang-che Wu | 0a4304a | 2019-01-19 01:32:11 +0800 | [diff] [blame] | 315 | if opts.fail_to_pass: |
| 316 | logger.error('--fail_to_pass is not for benchmark test (--metric)') |
| 317 | return FATAL |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 318 | else: |
| 319 | if opts.old_value is not None: |
| 320 | logger.error('--old_value is provided but --metric is not') |
| 321 | return FATAL |
| 322 | if opts.new_value is not None: |
| 323 | logger.error('--new_value is provided but --metric is not') |
| 324 | return FATAL |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 325 | if opts.test_name.startswith('telemetry_'): |
| 326 | if not opts.chrome_root: |
| 327 | logger.error('--chrome_root is mandatory for telemetry tests') |
| 328 | return FATAL |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 329 | |
Kuang-che Wu | 6c5a5b2 | 2019-01-17 18:09:50 +0800 | [diff] [blame] | 330 | try: |
| 331 | prepare_to_run_test(opts) |
| 332 | except Exception: |
| 333 | logger.exception('failed when prepare, assume it is temporary; SKIP') |
| 334 | return SKIP |
Kuang-che Wu | e47162d | 2018-10-29 17:24:04 +0800 | [diff] [blame] | 335 | |
Kuang-che Wu | 171dcb6 | 2018-10-25 12:37:05 +0800 | [diff] [blame] | 336 | result_dir = run_test(opts) |
| 337 | if not result_dir: |
Kuang-che Wu | dd80267 | 2018-08-10 19:40:14 +0800 | [diff] [blame] | 338 | return FATAL |
| 339 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 340 | passed, values = gather_test_result(opts, result_dir) |
| 341 | |
| 342 | if opts.metric: |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 343 | if not values: |
| 344 | logger.warning('no values found; SKIP') |
| 345 | return SKIP |
| 346 | |
| 347 | print('BISECT_RESULT_VALUES=', ' '.join(map(str, values))) |
| 348 | average = float(sum(values)) / len(values) |
Kuang-che Wu | 689f154 | 2018-08-20 17:45:58 +0800 | [diff] [blame] | 349 | if abs(average - opts.old_value) < abs(average - opts.new_value): |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 350 | logger.info('values=%s, average=%s; OLD', values, average) |
| 351 | return OLD |
| 352 | logger.info('values=%s, average=%s; NEW', values, average) |
| 353 | return NEW |
| 354 | else: |
Kuang-che Wu | 0a4304a | 2019-01-19 01:32:11 +0800 | [diff] [blame] | 355 | if opts.fail_to_pass: |
| 356 | if passed: |
| 357 | logger.info('passed') |
| 358 | return NEW |
| 359 | logger.info('failed') |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 360 | return OLD |
Kuang-che Wu | 0a4304a | 2019-01-19 01:32:11 +0800 | [diff] [blame] | 361 | else: |
| 362 | if passed: |
| 363 | logger.info('passed') |
| 364 | return OLD |
| 365 | logger.info('failed') |
| 366 | return NEW |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 367 | |
| 368 | |
| 369 | if __name__ == '__main__': |
| 370 | sys.exit(EXIT_CODE_MAP[main()]) |