Kuang-che Wu | 875c89a | 2020-01-08 14:30:55 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 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 | """Switcher for ChromeOS autotest prebuilt |
| 7 | |
Kuang-che Wu | 7f82c6f | 2019-08-12 14:29:28 +0800 | [diff] [blame] | 8 | It unpacks autotest prebuilt (both client and server packages) into |
| 9 | $CHROMEOS_ROOT/tmp/autotest-prebuilt (that is, autotest_dir). |
Kuang-che Wu | 99e808f | 2019-06-26 12:17:32 +0800 | [diff] [blame] | 10 | Later, you can run tests using "eval_cros_autotest.py --prebuilt" or |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 11 | "test_that --autotest_dir". |
| 12 | """ |
| 13 | |
| 14 | from __future__ import print_function |
| 15 | import argparse |
| 16 | import logging |
| 17 | import os |
| 18 | import shutil |
Kuang-che Wu | 057fa77 | 2019-08-21 11:35:42 +0800 | [diff] [blame] | 19 | import sys |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 20 | import tempfile |
| 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 | AUTOTEST_CLIENT_TARBALL = 'autotest_packages.tar' |
Kuang-che Wu | 7f82c6f | 2019-08-12 14:29:28 +0800 | [diff] [blame] | 31 | AUTOTEST_SERVER_TARBALL = 'autotest_server_package.tar.bz2' |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 32 | GS_BUILD_PATH = 'gs://chromeos-image-archive/{board}-release/{full_version}' |
| 33 | GS_AUTOTEST_CLIENT_PATH = GS_BUILD_PATH + '/' + AUTOTEST_CLIENT_TARBALL |
Kuang-che Wu | 7f82c6f | 2019-08-12 14:29:28 +0800 | [diff] [blame] | 34 | GS_AUTOTEST_SERVER_PATH = GS_BUILD_PATH + '/' + AUTOTEST_SERVER_TARBALL |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 35 | |
Zheng-Jie Chang | 181be6f | 2020-03-17 16:16:08 +0800 | [diff] [blame] | 36 | GS_BUILDBUCKET_BUILD_PATH = 'gs://chromeos-image-archive/{path}' |
| 37 | GS_BUILDBUCKET_BUILD_AUTOTEST_CLIENT_PATH = ( |
| 38 | GS_BUILDBUCKET_BUILD_PATH + '/' + AUTOTEST_CLIENT_TARBALL) |
| 39 | GS_BUILDBUCKET_BUILD_AUTOTEST_SERVER_PATH = ( |
| 40 | GS_BUILDBUCKET_BUILD_PATH + '/' + AUTOTEST_SERVER_TARBALL) |
| 41 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 42 | |
| 43 | def create_argument_parser(): |
Kuang-che Wu | d2d6e41 | 2021-01-28 16:26:41 +0800 | [diff] [blame^] | 44 | parents = [common.common_argument_parser, common.session_optional_parser] |
| 45 | parser = argparse.ArgumentParser(description=__doc__, parents=parents) |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 46 | cli.patching_argparser_exit(parser) |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 47 | parser.add_argument( |
| 48 | '--chromeos_root', |
| 49 | type=cli.argtype_dir_path, |
| 50 | default=configure.get('CHROMEOS_ROOT', ''), |
| 51 | help='ChromeOS tree root') |
| 52 | parser.add_argument( |
| 53 | '--test_name', |
| 54 | help='Client test name, like "video_VideoDecodeAccelerator.h264"') |
| 55 | parser.add_argument( |
| 56 | '--board', |
| 57 | metavar='BOARD', |
| 58 | default=configure.get('BOARD', ''), |
| 59 | help='ChromeOS board name') |
| 60 | parser.add_argument( |
Zheng-Jie Chang | 181be6f | 2020-03-17 16:16:08 +0800 | [diff] [blame] | 61 | '--dut', |
| 62 | type=cli.argtype_notempty, |
| 63 | metavar='DUT', |
| 64 | default=configure.get('DUT'), |
| 65 | help='Address of DUT') |
| 66 | parser.add_argument( |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 67 | 'version', |
| 68 | nargs='?', |
Zheng-Jie Chang | 181be6f | 2020-03-17 16:16:08 +0800 | [diff] [blame] | 69 | type=cli.argtype_notempty, |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 70 | metavar='CROS_VERSION', |
| 71 | default=configure.get('CROS_VERSION', ''), |
Zheng-Jie Chang | 181be6f | 2020-03-17 16:16:08 +0800 | [diff] [blame] | 72 | help='ChromeOS local build version string, in format short version, ' |
| 73 | 'full version, or "full,full+N"') |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 74 | |
| 75 | return parser |
| 76 | |
| 77 | |
Zheng-Jie Chang | 181be6f | 2020-03-17 16:16:08 +0800 | [diff] [blame] | 78 | def switch(autotest_dir, board, version, test_name, dut): |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 79 | logger.info('Unpack autotest packages for %s %s', board, version) |
Zheng-Jie Chang | 5402083 | 2020-04-21 15:52:48 +0800 | [diff] [blame] | 80 | if cros_util.is_cros_short_version(version) or cros_util.is_cros_full_version( |
| 81 | version): |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 82 | full_version = cros_util.version_to_full(board, version) |
| 83 | autotest_client_path = GS_AUTOTEST_CLIENT_PATH.format( |
| 84 | board=board, full_version=full_version) |
| 85 | autotest_server_path = GS_AUTOTEST_SERVER_PATH.format( |
| 86 | board=board, full_version=full_version) |
Zheng-Jie Chang | 181be6f | 2020-03-17 16:16:08 +0800 | [diff] [blame] | 87 | else: |
| 88 | gs_path = cros_util.query_dut_lsb_release(dut).get( |
| 89 | 'CHROMEOS_RELEASE_BUILDER_PATH') |
| 90 | autotest_client_path = GS_BUILDBUCKET_BUILD_AUTOTEST_CLIENT_PATH.format( |
| 91 | path=gs_path) |
| 92 | autotest_server_path = GS_BUILDBUCKET_BUILD_AUTOTEST_SERVER_PATH.format( |
| 93 | path=gs_path) |
| 94 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 95 | logger.info('autotest_client_path %s', autotest_client_path) |
| 96 | logger.info('autotest_server_path %s', autotest_server_path) |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 97 | |
| 98 | # TODO(kcwu): cache downloaded tarballs |
| 99 | tmp_dir = tempfile.mkdtemp() |
Kuang-che Wu | 7f82c6f | 2019-08-12 14:29:28 +0800 | [diff] [blame] | 100 | if os.path.exists(autotest_dir): |
| 101 | shutil.rmtree(autotest_dir) |
| 102 | os.makedirs(autotest_dir) |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 103 | |
| 104 | cros_util.gsutil('cp', autotest_client_path, tmp_dir) |
| 105 | tarball = os.path.join(tmp_dir, AUTOTEST_CLIENT_TARBALL) |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 106 | util.check_call( |
Kuang-che Wu | c6cafd3 | 2020-10-21 17:57:53 +0800 | [diff] [blame] | 107 | 'tar', |
| 108 | 'xvf', |
| 109 | tarball, |
| 110 | '--strip-components=1', # strip 'autotest/' |
| 111 | 'autotest', # only extract autotest/ |
| 112 | cwd=autotest_dir) |
Kuang-che Wu | 7f82c6f | 2019-08-12 14:29:28 +0800 | [diff] [blame] | 113 | cros_util.gsutil('cp', autotest_server_path, tmp_dir) |
| 114 | tarball = os.path.join(tmp_dir, AUTOTEST_SERVER_TARBALL) |
| 115 | util.check_call( |
Kuang-che Wu | c6cafd3 | 2020-10-21 17:57:53 +0800 | [diff] [blame] | 116 | 'tar', |
| 117 | 'xf', |
| 118 | tarball, |
| 119 | '--strip-components=1', # strip 'autotest/' |
| 120 | 'autotest', # only extract autotest/ |
| 121 | cwd=autotest_dir) |
Kuang-che Wu | 7f82c6f | 2019-08-12 14:29:28 +0800 | [diff] [blame] | 122 | |
| 123 | # Need to extract the control file if the target is a client site test. |
| 124 | if test_name: |
| 125 | test_name = test_name.split('.')[0] |
| 126 | client_tarball = os.path.abspath( |
| 127 | os.path.join(autotest_dir, 'packages', 'test-%s.tar.bz2' % test_name)) |
| 128 | if os.path.exists(client_tarball): |
| 129 | client_test_dir = os.path.join(autotest_dir, 'client', 'site_tests', |
| 130 | test_name) |
| 131 | if not os.path.exists(client_test_dir): |
| 132 | os.makedirs(client_test_dir) |
| 133 | util.check_call('tar', 'xvf', client_tarball, cwd=client_test_dir) |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 134 | |
| 135 | shutil.rmtree(tmp_dir) |
| 136 | |
| 137 | |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 138 | @cli.fatal_error_handler |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 139 | def main(args=None): |
| 140 | common.init() |
| 141 | parser = create_argument_parser() |
| 142 | opts = parser.parse_args(args) |
| 143 | common.config_logging(opts) |
| 144 | |
Kuang-che Wu | 7f82c6f | 2019-08-12 14:29:28 +0800 | [diff] [blame] | 145 | autotest_dir = os.path.join(opts.chromeos_root, |
| 146 | cros_util.prebuilt_autotest_dir) |
Zheng-Jie Chang | 181be6f | 2020-03-17 16:16:08 +0800 | [diff] [blame] | 147 | switch(autotest_dir, opts.board, opts.version, opts.test_name, opts.dut) |
Kuang-che Wu | 7f82c6f | 2019-08-12 14:29:28 +0800 | [diff] [blame] | 148 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 149 | # Verify test control file exists. |
| 150 | if opts.test_name: |
| 151 | found = cros_util.get_autotest_test_info(autotest_dir, opts.test_name) |
Kuang-che Wu | 99e808f | 2019-06-26 12:17:32 +0800 | [diff] [blame] | 152 | if not found: |
| 153 | names = [] |
| 154 | for control_file in cros_util.enumerate_autotest_control_files( |
| 155 | autotest_dir): |
Kuang-che Wu | 13a4241 | 2020-04-29 02:39:58 +0800 | [diff] [blame] | 156 | try: |
| 157 | info = cros_util.parse_autotest_control_file(control_file) |
| 158 | except SyntaxError: |
| 159 | logger.warning('%s is not parsable, ignore', control_file) |
| 160 | continue |
Kuang-che Wu | 99e808f | 2019-06-26 12:17:32 +0800 | [diff] [blame] | 161 | names.append(info.name) |
| 162 | util.show_similar_candidates('test_name', opts.test_name, names) |
Kuang-che Wu | 057fa77 | 2019-08-21 11:35:42 +0800 | [diff] [blame] | 163 | sys.exit(cli.EXIT_CODE_FATAL) |
Kuang-che Wu | 99e808f | 2019-06-26 12:17:32 +0800 | [diff] [blame] | 164 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 165 | |
| 166 | if __name__ == '__main__': |
| 167 | main() |