blob: a9f28aa0ccaab3f15d32949a229d6e9e4c20ee2c [file] [log] [blame]
Kuang-che Wu875c89a2020-01-08 14:30:55 +08001#!/usr/bin/env python3
Kuang-che Wub9705bd2018-06-28 17:59:18 +08002# -*- 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 Wu7f82c6f2019-08-12 14:29:28 +08008It unpacks autotest prebuilt (both client and server packages) into
9$CHROMEOS_ROOT/tmp/autotest-prebuilt (that is, autotest_dir).
Kuang-che Wu99e808f2019-06-26 12:17:32 +080010Later, you can run tests using "eval_cros_autotest.py --prebuilt" or
Kuang-che Wub9705bd2018-06-28 17:59:18 +080011"test_that --autotest_dir".
12"""
13
14from __future__ import print_function
15import argparse
16import logging
17import os
18import shutil
Kuang-che Wu057fa772019-08-21 11:35:42 +080019import sys
Kuang-che Wub9705bd2018-06-28 17:59:18 +080020import tempfile
21
22from bisect_kit import cli
23from bisect_kit import common
24from bisect_kit import configure
25from bisect_kit import cros_util
26from bisect_kit import util
27
28logger = logging.getLogger(__name__)
29
30AUTOTEST_CLIENT_TARBALL = 'autotest_packages.tar'
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +080031AUTOTEST_SERVER_TARBALL = 'autotest_server_package.tar.bz2'
Kuang-che Wub9705bd2018-06-28 17:59:18 +080032GS_BUILD_PATH = 'gs://chromeos-image-archive/{board}-release/{full_version}'
33GS_AUTOTEST_CLIENT_PATH = GS_BUILD_PATH + '/' + AUTOTEST_CLIENT_TARBALL
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +080034GS_AUTOTEST_SERVER_PATH = GS_BUILD_PATH + '/' + AUTOTEST_SERVER_TARBALL
Kuang-che Wub9705bd2018-06-28 17:59:18 +080035
Zheng-Jie Chang181be6f2020-03-17 16:16:08 +080036GS_BUILDBUCKET_BUILD_PATH = 'gs://chromeos-image-archive/{path}'
37GS_BUILDBUCKET_BUILD_AUTOTEST_CLIENT_PATH = (
38 GS_BUILDBUCKET_BUILD_PATH + '/' + AUTOTEST_CLIENT_TARBALL)
39GS_BUILDBUCKET_BUILD_AUTOTEST_SERVER_PATH = (
40 GS_BUILDBUCKET_BUILD_PATH + '/' + AUTOTEST_SERVER_TARBALL)
41
Kuang-che Wub9705bd2018-06-28 17:59:18 +080042
43def create_argument_parser():
Kuang-che Wud2d6e412021-01-28 16:26:41 +080044 parents = [common.common_argument_parser, common.session_optional_parser]
45 parser = argparse.ArgumentParser(description=__doc__, parents=parents)
Kuang-che Wufe1e88a2019-09-10 21:52:25 +080046 cli.patching_argparser_exit(parser)
Kuang-che Wub9705bd2018-06-28 17:59:18 +080047 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 Chang181be6f2020-03-17 16:16:08 +080061 '--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 Wub9705bd2018-06-28 17:59:18 +080067 'version',
68 nargs='?',
Zheng-Jie Chang181be6f2020-03-17 16:16:08 +080069 type=cli.argtype_notempty,
Kuang-che Wub9705bd2018-06-28 17:59:18 +080070 metavar='CROS_VERSION',
71 default=configure.get('CROS_VERSION', ''),
Zheng-Jie Chang181be6f2020-03-17 16:16:08 +080072 help='ChromeOS local build version string, in format short version, '
73 'full version, or "full,full+N"')
Kuang-che Wub9705bd2018-06-28 17:59:18 +080074
75 return parser
76
77
Zheng-Jie Chang181be6f2020-03-17 16:16:08 +080078def switch(autotest_dir, board, version, test_name, dut):
Zheng-Jie Chang127c3302019-09-10 17:17:04 +080079 logger.info('Unpack autotest packages for %s %s', board, version)
Zheng-Jie Chang54020832020-04-21 15:52:48 +080080 if cros_util.is_cros_short_version(version) or cros_util.is_cros_full_version(
81 version):
Zheng-Jie Chang127c3302019-09-10 17:17:04 +080082 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 Chang181be6f2020-03-17 16:16:08 +080087 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 Chang127c3302019-09-10 17:17:04 +080095 logger.info('autotest_client_path %s', autotest_client_path)
96 logger.info('autotest_server_path %s', autotest_server_path)
Kuang-che Wub9705bd2018-06-28 17:59:18 +080097
98 # TODO(kcwu): cache downloaded tarballs
99 tmp_dir = tempfile.mkdtemp()
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +0800100 if os.path.exists(autotest_dir):
101 shutil.rmtree(autotest_dir)
102 os.makedirs(autotest_dir)
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800103
104 cros_util.gsutil('cp', autotest_client_path, tmp_dir)
105 tarball = os.path.join(tmp_dir, AUTOTEST_CLIENT_TARBALL)
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800106 util.check_call(
Kuang-che Wuc6cafd32020-10-21 17:57:53 +0800107 'tar',
108 'xvf',
109 tarball,
110 '--strip-components=1', # strip 'autotest/'
111 'autotest', # only extract autotest/
112 cwd=autotest_dir)
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +0800113 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 Wuc6cafd32020-10-21 17:57:53 +0800116 'tar',
117 'xf',
118 tarball,
119 '--strip-components=1', # strip 'autotest/'
120 'autotest', # only extract autotest/
121 cwd=autotest_dir)
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +0800122
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 Wub9705bd2018-06-28 17:59:18 +0800134
135 shutil.rmtree(tmp_dir)
136
137
Kuang-che Wufe1e88a2019-09-10 21:52:25 +0800138@cli.fatal_error_handler
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800139def 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 Wu7f82c6f2019-08-12 14:29:28 +0800145 autotest_dir = os.path.join(opts.chromeos_root,
146 cros_util.prebuilt_autotest_dir)
Zheng-Jie Chang181be6f2020-03-17 16:16:08 +0800147 switch(autotest_dir, opts.board, opts.version, opts.test_name, opts.dut)
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +0800148
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800149 # 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 Wu99e808f2019-06-26 12:17:32 +0800152 if not found:
153 names = []
154 for control_file in cros_util.enumerate_autotest_control_files(
155 autotest_dir):
Kuang-che Wu13a42412020-04-29 02:39:58 +0800156 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 Wu99e808f2019-06-26 12:17:32 +0800161 names.append(info.name)
162 util.show_similar_candidates('test_name', opts.test_name, names)
Kuang-che Wu057fa772019-08-21 11:35:42 +0800163 sys.exit(cli.EXIT_CODE_FATAL)
Kuang-che Wu99e808f2019-06-26 12:17:32 +0800164
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800165
166if __name__ == '__main__':
167 main()