blob: c46427dc3eaf8b1b7cf0065632adb67bc490d2f1 [file] [log] [blame]
Kuang-che Wu875c89a2020-01-08 14:30:55 +08001#!/usr/bin/env python3
Kuang-che Wu32f27242019-05-16 17:34:50 +08002# -*- 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."""
7from __future__ import print_function
8import argparse
9import json
10import logging
11import os
12import re
Kuang-che Wu6c51d302019-06-19 09:07:20 +080013import shutil
Kuang-che Wu32f27242019-05-16 17:34:50 +080014import subprocess
15import sys
16
17from bisect_kit import catapult_util
18from bisect_kit import cli
19from bisect_kit import common
20from bisect_kit import configure
21from bisect_kit import cros_util
Kuang-che Wu11713052019-05-30 16:21:54 +080022from bisect_kit import util
Kuang-che Wu32f27242019-05-16 17:34:50 +080023
24logger = logging.getLogger(__name__)
25
26OLD = 'old'
27NEW = 'new'
28SKIP = 'skip'
29FATAL = 'fatal'
30
31EXIT_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
39def create_argument_parser():
40 parser = argparse.ArgumentParser(description=__doc__)
Kuang-che Wufe1e88a2019-09-10 21:52:25 +080041 cli.patching_argparser_exit(parser)
Kuang-che Wu32f27242019-05-16 17:34:50 +080042 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 Wu4fc0f1c2019-08-02 21:56:49 +080061 '--with_private_bundles',
62 action='store_true',
63 help='Whether search tests in private bundles or not')
64 parser.add_argument(
Kuang-che Wu32f27242019-05-16 17:34:50 +080065 '--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',
Kuang-che Wud1b74152020-05-20 08:46:46 +080082 help='Metric name of performance test; example: '
83 '"cheets_SystemRawImageSize"')
Kuang-che Wu32f27242019-05-16 17:34:50 +080084
85 return parser
86
87
88def prepare_to_run_test(opts):
89 if opts.reboot_before_test:
90 cros_util.reboot(opts.dut)
91
92
Kuang-che Wu4fc0f1c2019-08-02 21:56:49 +080093def get_tast_bundle_info(opts, pattern=None):
94 bundles = ['cros']
95 flags = ['-json']
Zheng-Jie Chang181be6f2020-03-17 16:16:08 +080096 # TODO(zjchang): ensure the tast version for buildbucket builds is correct
Kuang-che Wu11713052019-05-30 16:21:54 +080097 if not opts.tast_build:
Kuang-che Wu4fc0f1c2019-08-02 21:56:49 +080098 flags.append('-build=false')
99 if opts.with_private_bundles:
100 flags.append('-downloadprivatebundles=true')
101 else:
102 if opts.with_private_bundles:
103 bundles.append('crosint')
104
105 args = [opts.dut]
Kuang-che Wu11713052019-05-30 16:21:54 +0800106 if pattern:
Kuang-che Wu4fc0f1c2019-08-02 21:56:49 +0800107 args.append(pattern)
108
109 result = {}
110 for bundle in bundles:
111 cmd = ['tast', 'list'] + flags + ['-buildbundle=' + bundle] + args
Kuang-che Wubcafc552019-08-15 15:27:02 +0800112 json_text = cros_util.cros_sdk(opts.chromeos_root, *cmd, log_stdout=False)
Kuang-che Wu4fc0f1c2019-08-02 21:56:49 +0800113 for entry in json.loads(json_text):
114 result[entry['name']] = bundle
115
116 return result
Kuang-che Wu11713052019-05-30 16:21:54 +0800117
118
Kuang-che Wu4fc0f1c2019-08-02 21:56:49 +0800119def run_test(opts, bundle):
Kuang-che Wu32f27242019-05-16 17:34:50 +0800120 """Runs an autotest test.
121
122 Args:
123 opts: An argparse.Namespace to hold command line arguments.
Kuang-che Wu4fc0f1c2019-08-02 21:56:49 +0800124 bundle: tast's test bundle
Kuang-che Wu32f27242019-05-16 17:34:50 +0800125
126 Returns:
127 path of test result (outside chroot)
128 """
129 # Set results dir inside source tree, so it's easier to access them outside
130 # chroot.
131 results_dir = os.path.join(cros_util.chromeos_root_inside_chroot,
132 'tmp/tast_results_tmp')
Kuang-che Wu6c51d302019-06-19 09:07:20 +0800133 results_dir_output_chroot = results_dir.replace(
134 cros_util.chromeos_root_inside_chroot, opts.chromeos_root)
135 # Don't reuse existing results dir, otherwise tast may rename output files.
136 if os.path.exists(results_dir_output_chroot):
137 shutil.rmtree(results_dir_output_chroot)
138
Kuang-che Wu32f27242019-05-16 17:34:50 +0800139 # TODO(kcwu): add -timeout
140 cmd = ['tast', '-verbose', 'run']
141 if not opts.tast_build:
142 cmd.append('-build=false')
Kuang-che Wu4fc0f1c2019-08-02 21:56:49 +0800143 if opts.with_private_bundles:
144 cmd.append('-downloadprivatebundles=true')
145 else:
146 cmd.append('-buildbundle=' + bundle)
Kuang-che Wu32f27242019-05-16 17:34:50 +0800147 cmd += ['-resultsdir', results_dir, opts.dut, opts.test_name]
148
Kuang-che Wu34a67542019-09-09 18:01:05 +0800149 cros_util.cros_sdk(opts.chromeos_root, *cmd)
Kuang-che Wu32f27242019-05-16 17:34:50 +0800150
Kuang-che Wu6c51d302019-06-19 09:07:20 +0800151 return results_dir_output_chroot
Kuang-che Wu32f27242019-05-16 17:34:50 +0800152
153
154def gather_test_result(opts, result_dir):
155 error_path = os.path.join(result_dir, 'run_error.txt')
156 if os.path.exists(error_path):
Kuang-che Wua5723492019-11-25 20:59:34 +0800157 with open(error_path) as f:
158 message = f.read()
Kuang-che Wu32f27242019-05-16 17:34:50 +0800159 raise Exception('tast global error: %s' % message)
160
161 results_path = os.path.join(result_dir, 'results.json')
162 passed = None
Kuang-che Wu74bcb642020-02-20 18:45:53 +0800163 with open(results_path) as f:
164 for result in json.load(f):
165 if result['name'] != opts.test_name:
166 logger.warning('unexpected test ran: %s', result['name'])
167 continue
168 passed = result['errors'] is None
Kuang-che Wu32f27242019-05-16 17:34:50 +0800169 if passed is None:
170 raise Exception('no test result for "%s"?' % opts.test_name)
171
172 values = []
173 if opts.metric:
174 chart_path = os.path.join(result_dir, 'tests', opts.test_name,
175 'results-chart.json')
176 values = catapult_util.get_benchmark_values(chart_path, opts.metric)
177
178 return passed, values
179
180
Kuang-che Wufe1e88a2019-09-10 21:52:25 +0800181@cli.fatal_error_handler
Kuang-che Wu32f27242019-05-16 17:34:50 +0800182def main(args=None):
183 common.init()
184 parser = create_argument_parser()
185 opts = parser.parse_args(args)
186 common.config_logging(opts)
187
188 if not cros_util.is_dut(opts.dut):
189 logger.error('%r is not a valid DUT address', opts.dut)
190 return FATAL
191
Zheng-Jie Chang181be6f2020-03-17 16:16:08 +0800192 by_official_builder = cros_util.query_dut_is_by_official_builder(opts.dut)
193 if (opts.with_private_bundles and not opts.tast_build and
194 not by_official_builder):
Kuang-che Wu4fc0f1c2019-08-02 21:56:49 +0800195 version = cros_util.query_dut_short_version(opts.dut)
196 if cros_util.is_cros_localbuild_version(version):
197 logger.error(
198 'for non-official chromeos image, --tast_build must be specified')
199 return FATAL
200
Kuang-che Wu32f27242019-05-16 17:34:50 +0800201 # Verify command line options.
202 if opts.metric:
203 if opts.fail_to_pass:
204 logger.error('--fail_to_pass is not for benchmark test (--metric)')
205 return FATAL
206 # Remove the "tast." prefix prepended by autotest.
207 opts.test_name = re.sub(r'^tast\.', '', opts.test_name)
208
Zheng-Jie Chang6dc5fe02019-11-19 15:58:27 +0800209 try:
210 tast_bundle_info = get_tast_bundle_info(opts, opts.test_name)
211 if not tast_bundle_info:
212 tast_bundle_info = get_tast_bundle_info(opts)
213 util.show_similar_candidates('test name', opts.test_name,
214 list(tast_bundle_info))
215 return FATAL
216 except subprocess.CalledProcessError:
217 logger.exception(
218 'failed to get tast bundle info, assume it is temporary; SKIP')
219 return SKIP
Kuang-che Wu4fc0f1c2019-08-02 21:56:49 +0800220
221 if len(tast_bundle_info) != 1 or opts.test_name not in tast_bundle_info:
222 # For example, tast in chroot after 12205.0.0 is IPC incompatible with
223 # tast on DUT earlier than 12028.0.0 (crbug/932307)
224 logger.fatal('"tast list" returns unexpected tests; '
225 'incompatible tast on DUT and in chroot?')
Kuang-che Wu11713052019-05-30 16:21:54 +0800226 return FATAL
227
Kuang-che Wu32f27242019-05-16 17:34:50 +0800228 try:
229 prepare_to_run_test(opts)
230 except Exception:
231 logger.exception('failed when prepare, assume it is temporary; SKIP')
232 return SKIP
233
Kuang-che Wu4fc0f1c2019-08-02 21:56:49 +0800234 bundle = tast_bundle_info[opts.test_name]
Kuang-che Wu34a67542019-09-09 18:01:05 +0800235 try:
236 result_dir = run_test(opts, bundle)
237 except subprocess.CalledProcessError:
238 logger.error('failed to run tast; maybe build, ssh, or setup failures')
239 return SKIP
Kuang-che Wu32f27242019-05-16 17:34:50 +0800240
241 try:
242 passed, values = gather_test_result(opts, result_dir)
243 except Exception:
244 logger.exception('failed to parse test result')
245 return FATAL
246
247 if opts.metric:
248 if not values:
249 logger.warning('no values found; SKIP')
250 return SKIP
251
Kuang-che Wuc89f2a22019-11-26 15:30:50 +0800252 print('BISECT_RESULT_VALUES=', ' '.join(str(v) for v in values))
Kuang-che Wu32f27242019-05-16 17:34:50 +0800253 logger.info('values=%s', values)
254 # The exit code doesn't matter.
255 return OLD
Kuang-che Wu084eef22020-03-11 18:29:48 +0800256
257 if opts.fail_to_pass:
258 if passed:
259 logger.info('passed')
Kuang-che Wu32f27242019-05-16 17:34:50 +0800260 return NEW
Kuang-che Wu084eef22020-03-11 18:29:48 +0800261 logger.info('failed')
262 return OLD
263 if passed:
264 logger.info('passed')
265 return OLD
266 logger.info('failed')
267 return NEW
Kuang-che Wu32f27242019-05-16 17:34:50 +0800268
269
270if __name__ == '__main__':
271 sys.exit(EXIT_CODE_MAP[main()])