Kuang-che Wu | 875c89a | 2020-01-08 14:30:55 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
| 3 | # Copyright 2019 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 tast tests.""" |
| 7 | from __future__ import print_function |
| 8 | import argparse |
| 9 | import json |
| 10 | import logging |
| 11 | import os |
| 12 | import re |
Kuang-che Wu | 6c51d30 | 2019-06-19 09:07:20 +0800 | [diff] [blame] | 13 | import shutil |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 14 | import subprocess |
| 15 | import sys |
| 16 | |
| 17 | from bisect_kit import catapult_util |
| 18 | from bisect_kit import cli |
| 19 | from bisect_kit import common |
| 20 | from bisect_kit import configure |
| 21 | from bisect_kit import cros_util |
Kuang-che Wu | 1171305 | 2019-05-30 16:21:54 +0800 | [diff] [blame] | 22 | from bisect_kit import util |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 23 | |
| 24 | logger = logging.getLogger(__name__) |
| 25 | |
| 26 | OLD = 'old' |
| 27 | NEW = 'new' |
| 28 | SKIP = 'skip' |
| 29 | FATAL = 'fatal' |
| 30 | |
| 31 | EXIT_CODE_MAP = { |
| 32 | OLD: cli.EXIT_CODE_OLD, |
| 33 | NEW: cli.EXIT_CODE_NEW, |
| 34 | SKIP: cli.EXIT_CODE_SKIP, |
| 35 | FATAL: cli.EXIT_CODE_FATAL, |
| 36 | } |
| 37 | |
| 38 | |
| 39 | def create_argument_parser(): |
| 40 | parser = argparse.ArgumentParser(description=__doc__) |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 41 | cli.patching_argparser_exit(parser) |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 42 | common.add_common_arguments(parser) |
| 43 | parser.add_argument( |
| 44 | 'dut', |
| 45 | nargs='?', |
| 46 | type=cli.argtype_notempty, |
| 47 | metavar='DUT', |
| 48 | default=configure.get('DUT', '')) |
| 49 | parser.add_argument( |
| 50 | '--chromeos_root', |
| 51 | type=cli.argtype_dir_path, |
| 52 | metavar='CHROMEOS_ROOT', |
| 53 | default=configure.get('CHROMEOS_ROOT', ''), |
| 54 | help='ChromeOS tree root') |
| 55 | parser.add_argument( |
| 56 | '--tast_build', |
| 57 | action='store_true', |
| 58 | help='Build tast test bundle (-build=true) if specified; ' |
| 59 | 'default is using prebuilt bundle on the DUT') |
| 60 | parser.add_argument( |
Kuang-che Wu | 4fc0f1c | 2019-08-02 21:56:49 +0800 | [diff] [blame] | 61 | '--with_private_bundles', |
| 62 | action='store_true', |
| 63 | help='Whether search tests in private bundles or not') |
| 64 | parser.add_argument( |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 65 | '--reboot_before_test', |
| 66 | action='store_true', |
| 67 | help='Reboot before test run') |
| 68 | |
| 69 | group = parser.add_argument_group(title='Options for normal autotest tests') |
| 70 | group.add_argument( |
| 71 | '--test_name', |
| 72 | required=True, |
| 73 | help='Test name, like "video_VideoDecodeAccelerator.h264"') |
| 74 | group.add_argument( |
| 75 | '--fail_to_pass', |
| 76 | action='store_true', |
| 77 | help='For functional tests: old behavior is FAIL and new behavior is ' |
| 78 | 'PASS; If not specified, default = old behavior is PASS and new ' |
| 79 | 'behavior is FAIL') |
| 80 | group.add_argument( |
| 81 | '--metric', |
| 82 | help= |
| 83 | 'Metric name of performance test; example: "cheets_SystemRawImageSize"') |
| 84 | |
| 85 | return parser |
| 86 | |
| 87 | |
| 88 | def prepare_to_run_test(opts): |
| 89 | if opts.reboot_before_test: |
| 90 | cros_util.reboot(opts.dut) |
| 91 | |
| 92 | |
Kuang-che Wu | 4fc0f1c | 2019-08-02 21:56:49 +0800 | [diff] [blame] | 93 | def get_tast_bundle_info(opts, pattern=None): |
| 94 | bundles = ['cros'] |
| 95 | flags = ['-json'] |
Kuang-che Wu | 1171305 | 2019-05-30 16:21:54 +0800 | [diff] [blame] | 96 | if not opts.tast_build: |
Kuang-che Wu | 4fc0f1c | 2019-08-02 21:56:49 +0800 | [diff] [blame] | 97 | flags.append('-build=false') |
| 98 | if opts.with_private_bundles: |
| 99 | flags.append('-downloadprivatebundles=true') |
| 100 | else: |
| 101 | if opts.with_private_bundles: |
| 102 | bundles.append('crosint') |
| 103 | |
| 104 | args = [opts.dut] |
Kuang-che Wu | 1171305 | 2019-05-30 16:21:54 +0800 | [diff] [blame] | 105 | if pattern: |
Kuang-che Wu | 4fc0f1c | 2019-08-02 21:56:49 +0800 | [diff] [blame] | 106 | args.append(pattern) |
| 107 | |
| 108 | result = {} |
| 109 | for bundle in bundles: |
| 110 | cmd = ['tast', 'list'] + flags + ['-buildbundle=' + bundle] + args |
Kuang-che Wu | bcafc55 | 2019-08-15 15:27:02 +0800 | [diff] [blame] | 111 | json_text = cros_util.cros_sdk(opts.chromeos_root, *cmd, log_stdout=False) |
Kuang-che Wu | 4fc0f1c | 2019-08-02 21:56:49 +0800 | [diff] [blame] | 112 | for entry in json.loads(json_text): |
| 113 | result[entry['name']] = bundle |
| 114 | |
| 115 | return result |
Kuang-che Wu | 1171305 | 2019-05-30 16:21:54 +0800 | [diff] [blame] | 116 | |
| 117 | |
Kuang-che Wu | 4fc0f1c | 2019-08-02 21:56:49 +0800 | [diff] [blame] | 118 | def run_test(opts, bundle): |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 119 | """Runs an autotest test. |
| 120 | |
| 121 | Args: |
| 122 | opts: An argparse.Namespace to hold command line arguments. |
Kuang-che Wu | 4fc0f1c | 2019-08-02 21:56:49 +0800 | [diff] [blame] | 123 | bundle: tast's test bundle |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 124 | |
| 125 | Returns: |
| 126 | path of test result (outside chroot) |
| 127 | """ |
| 128 | # Set results dir inside source tree, so it's easier to access them outside |
| 129 | # chroot. |
| 130 | results_dir = os.path.join(cros_util.chromeos_root_inside_chroot, |
| 131 | 'tmp/tast_results_tmp') |
Kuang-che Wu | 6c51d30 | 2019-06-19 09:07:20 +0800 | [diff] [blame] | 132 | results_dir_output_chroot = results_dir.replace( |
| 133 | cros_util.chromeos_root_inside_chroot, opts.chromeos_root) |
| 134 | # Don't reuse existing results dir, otherwise tast may rename output files. |
| 135 | if os.path.exists(results_dir_output_chroot): |
| 136 | shutil.rmtree(results_dir_output_chroot) |
| 137 | |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 138 | # TODO(kcwu): add -timeout |
| 139 | cmd = ['tast', '-verbose', 'run'] |
| 140 | if not opts.tast_build: |
| 141 | cmd.append('-build=false') |
Kuang-che Wu | 4fc0f1c | 2019-08-02 21:56:49 +0800 | [diff] [blame] | 142 | if opts.with_private_bundles: |
| 143 | cmd.append('-downloadprivatebundles=true') |
| 144 | else: |
| 145 | cmd.append('-buildbundle=' + bundle) |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 146 | cmd += ['-resultsdir', results_dir, opts.dut, opts.test_name] |
| 147 | |
Kuang-che Wu | 34a6754 | 2019-09-09 18:01:05 +0800 | [diff] [blame] | 148 | cros_util.cros_sdk(opts.chromeos_root, *cmd) |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 149 | |
Kuang-che Wu | 6c51d30 | 2019-06-19 09:07:20 +0800 | [diff] [blame] | 150 | return results_dir_output_chroot |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 151 | |
| 152 | |
| 153 | def gather_test_result(opts, result_dir): |
| 154 | error_path = os.path.join(result_dir, 'run_error.txt') |
| 155 | if os.path.exists(error_path): |
Kuang-che Wu | a572349 | 2019-11-25 20:59:34 +0800 | [diff] [blame] | 156 | with open(error_path) as f: |
| 157 | message = f.read() |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 158 | raise Exception('tast global error: %s' % message) |
| 159 | |
| 160 | results_path = os.path.join(result_dir, 'results.json') |
| 161 | passed = None |
Kuang-che Wu | 74bcb64 | 2020-02-20 18:45:53 +0800 | [diff] [blame^] | 162 | with open(results_path) as f: |
| 163 | for result in json.load(f): |
| 164 | if result['name'] != opts.test_name: |
| 165 | logger.warning('unexpected test ran: %s', result['name']) |
| 166 | continue |
| 167 | passed = result['errors'] is None |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 168 | if passed is None: |
| 169 | raise Exception('no test result for "%s"?' % opts.test_name) |
| 170 | |
| 171 | values = [] |
| 172 | if opts.metric: |
| 173 | chart_path = os.path.join(result_dir, 'tests', opts.test_name, |
| 174 | 'results-chart.json') |
| 175 | values = catapult_util.get_benchmark_values(chart_path, opts.metric) |
| 176 | |
| 177 | return passed, values |
| 178 | |
| 179 | |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 180 | @cli.fatal_error_handler |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 181 | def main(args=None): |
| 182 | common.init() |
| 183 | parser = create_argument_parser() |
| 184 | opts = parser.parse_args(args) |
| 185 | common.config_logging(opts) |
| 186 | |
| 187 | if not cros_util.is_dut(opts.dut): |
| 188 | logger.error('%r is not a valid DUT address', opts.dut) |
| 189 | return FATAL |
| 190 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 191 | is_snapshot = cros_util.query_dut_is_snapshot(opts.dut) |
| 192 | if opts.with_private_bundles and not opts.tast_build and not is_snapshot: |
Kuang-che Wu | 4fc0f1c | 2019-08-02 21:56:49 +0800 | [diff] [blame] | 193 | version = cros_util.query_dut_short_version(opts.dut) |
| 194 | if cros_util.is_cros_localbuild_version(version): |
| 195 | logger.error( |
| 196 | 'for non-official chromeos image, --tast_build must be specified') |
| 197 | return FATAL |
| 198 | |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 199 | # Verify command line options. |
| 200 | if opts.metric: |
| 201 | if opts.fail_to_pass: |
| 202 | logger.error('--fail_to_pass is not for benchmark test (--metric)') |
| 203 | return FATAL |
| 204 | # Remove the "tast." prefix prepended by autotest. |
| 205 | opts.test_name = re.sub(r'^tast\.', '', opts.test_name) |
| 206 | |
Zheng-Jie Chang | 6dc5fe0 | 2019-11-19 15:58:27 +0800 | [diff] [blame] | 207 | try: |
| 208 | tast_bundle_info = get_tast_bundle_info(opts, opts.test_name) |
| 209 | if not tast_bundle_info: |
| 210 | tast_bundle_info = get_tast_bundle_info(opts) |
| 211 | util.show_similar_candidates('test name', opts.test_name, |
| 212 | list(tast_bundle_info)) |
| 213 | return FATAL |
| 214 | except subprocess.CalledProcessError: |
| 215 | logger.exception( |
| 216 | 'failed to get tast bundle info, assume it is temporary; SKIP') |
| 217 | return SKIP |
Kuang-che Wu | 4fc0f1c | 2019-08-02 21:56:49 +0800 | [diff] [blame] | 218 | |
| 219 | if len(tast_bundle_info) != 1 or opts.test_name not in tast_bundle_info: |
| 220 | # For example, tast in chroot after 12205.0.0 is IPC incompatible with |
| 221 | # tast on DUT earlier than 12028.0.0 (crbug/932307) |
| 222 | logger.fatal('"tast list" returns unexpected tests; ' |
| 223 | 'incompatible tast on DUT and in chroot?') |
Kuang-che Wu | 1171305 | 2019-05-30 16:21:54 +0800 | [diff] [blame] | 224 | return FATAL |
| 225 | |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 226 | try: |
| 227 | prepare_to_run_test(opts) |
| 228 | except Exception: |
| 229 | logger.exception('failed when prepare, assume it is temporary; SKIP') |
| 230 | return SKIP |
| 231 | |
Kuang-che Wu | 4fc0f1c | 2019-08-02 21:56:49 +0800 | [diff] [blame] | 232 | bundle = tast_bundle_info[opts.test_name] |
Kuang-che Wu | 34a6754 | 2019-09-09 18:01:05 +0800 | [diff] [blame] | 233 | try: |
| 234 | result_dir = run_test(opts, bundle) |
| 235 | except subprocess.CalledProcessError: |
| 236 | logger.error('failed to run tast; maybe build, ssh, or setup failures') |
| 237 | return SKIP |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 238 | |
| 239 | try: |
| 240 | passed, values = gather_test_result(opts, result_dir) |
| 241 | except Exception: |
| 242 | logger.exception('failed to parse test result') |
| 243 | return FATAL |
| 244 | |
| 245 | if opts.metric: |
| 246 | if not values: |
| 247 | logger.warning('no values found; SKIP') |
| 248 | return SKIP |
| 249 | |
Kuang-che Wu | c89f2a2 | 2019-11-26 15:30:50 +0800 | [diff] [blame] | 250 | print('BISECT_RESULT_VALUES=', ' '.join(str(v) for v in values)) |
Kuang-che Wu | 32f2724 | 2019-05-16 17:34:50 +0800 | [diff] [blame] | 251 | logger.info('values=%s', values) |
| 252 | # The exit code doesn't matter. |
| 253 | return OLD |
| 254 | else: |
| 255 | if opts.fail_to_pass: |
| 256 | if passed: |
| 257 | logger.info('passed') |
| 258 | return NEW |
| 259 | logger.info('failed') |
| 260 | return OLD |
| 261 | else: |
| 262 | if passed: |
| 263 | logger.info('passed') |
| 264 | return OLD |
| 265 | logger.info('failed') |
| 266 | return NEW |
| 267 | |
| 268 | |
| 269 | if __name__ == '__main__': |
| 270 | sys.exit(EXIT_CODE_MAP[main()]) |