blob: 18fe2da64e9d918876e2c538c035fc54f7daba12 [file] [log] [blame]
Kuang-che Wub9705bd2018-06-28 17:59:18 +08001#!/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"""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
36
37def create_argument_parser():
38 parser = argparse.ArgumentParser(description=__doc__)
Kuang-che Wufe1e88a2019-09-10 21:52:25 +080039 cli.patching_argparser_exit(parser)
Kuang-che Wub9705bd2018-06-28 17:59:18 +080040 common.add_common_arguments(parser)
41 parser.add_argument(
42 '--chromeos_root',
43 type=cli.argtype_dir_path,
44 default=configure.get('CHROMEOS_ROOT', ''),
45 help='ChromeOS tree root')
46 parser.add_argument(
47 '--test_name',
48 help='Client test name, like "video_VideoDecodeAccelerator.h264"')
49 parser.add_argument(
50 '--board',
51 metavar='BOARD',
52 default=configure.get('BOARD', ''),
53 help='ChromeOS board name')
54 parser.add_argument(
55 'version',
56 nargs='?',
57 type=cros_util.argtype_cros_version,
58 metavar='CROS_VERSION',
59 default=configure.get('CROS_VERSION', ''),
60 help='ChromeOS version number, short (10162.0.0) or full (R64-10162.0.0)')
61
62 return parser
63
64
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +080065def switch(autotest_dir, board, version, test_name):
Kuang-che Wub9705bd2018-06-28 17:59:18 +080066 full_version = cros_util.version_to_full(board, version)
67 logger.info('Unpack autotest packages for %s %s', board, full_version)
68
69 autotest_client_path = GS_AUTOTEST_CLIENT_PATH.format(
70 board=board, full_version=full_version)
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +080071 autotest_server_path = GS_AUTOTEST_SERVER_PATH.format(
72 board=board, full_version=full_version)
Kuang-che Wub9705bd2018-06-28 17:59:18 +080073
74 # TODO(kcwu): cache downloaded tarballs
75 tmp_dir = tempfile.mkdtemp()
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +080076 if os.path.exists(autotest_dir):
77 shutil.rmtree(autotest_dir)
78 os.makedirs(autotest_dir)
Kuang-che Wub9705bd2018-06-28 17:59:18 +080079
80 cros_util.gsutil('cp', autotest_client_path, tmp_dir)
81 tarball = os.path.join(tmp_dir, AUTOTEST_CLIENT_TARBALL)
82 # strip 'autotest/'
83 util.check_call(
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +080084 'tar', 'xvf', tarball, '--strip-components=1', cwd=autotest_dir)
85 cros_util.gsutil('cp', autotest_server_path, tmp_dir)
86 tarball = os.path.join(tmp_dir, AUTOTEST_SERVER_TARBALL)
87 util.check_call(
88 'tar', 'xf', tarball, '--strip-components=1', cwd=autotest_dir)
89
90 # Need to extract the control file if the target is a client site test.
91 if test_name:
92 test_name = test_name.split('.')[0]
93 client_tarball = os.path.abspath(
94 os.path.join(autotest_dir, 'packages', 'test-%s.tar.bz2' % test_name))
95 if os.path.exists(client_tarball):
96 client_test_dir = os.path.join(autotest_dir, 'client', 'site_tests',
97 test_name)
98 if not os.path.exists(client_test_dir):
99 os.makedirs(client_test_dir)
100 util.check_call('tar', 'xvf', client_tarball, cwd=client_test_dir)
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800101
102 shutil.rmtree(tmp_dir)
103
104
Kuang-che Wufe1e88a2019-09-10 21:52:25 +0800105@cli.fatal_error_handler
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800106def main(args=None):
107 common.init()
108 parser = create_argument_parser()
109 opts = parser.parse_args(args)
110 common.config_logging(opts)
111
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +0800112 autotest_dir = os.path.join(opts.chromeos_root,
113 cros_util.prebuilt_autotest_dir)
114 switch(autotest_dir, opts.board, opts.version, opts.test_name)
115
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800116 # Verify test control file exists.
117 if opts.test_name:
118 found = cros_util.get_autotest_test_info(autotest_dir, opts.test_name)
Kuang-che Wu99e808f2019-06-26 12:17:32 +0800119 if not found:
120 names = []
121 for control_file in cros_util.enumerate_autotest_control_files(
122 autotest_dir):
123 info = cros_util.parse_autotest_control_file(control_file)
124 names.append(info.name)
125 util.show_similar_candidates('test_name', opts.test_name, names)
Kuang-che Wu057fa772019-08-21 11:35:42 +0800126 sys.exit(cli.EXIT_CODE_FATAL)
Kuang-che Wu99e808f2019-06-26 12:17:32 +0800127
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800128
129if __name__ == '__main__':
130 main()