blob: 3fd6b1ab9d838041defcf08fc7fc66da41334cf2 [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__)
39 common.add_common_arguments(parser)
40 parser.add_argument(
41 '--chromeos_root',
42 type=cli.argtype_dir_path,
43 default=configure.get('CHROMEOS_ROOT', ''),
44 help='ChromeOS tree root')
45 parser.add_argument(
46 '--test_name',
47 help='Client test name, like "video_VideoDecodeAccelerator.h264"')
48 parser.add_argument(
49 '--board',
50 metavar='BOARD',
51 default=configure.get('BOARD', ''),
52 help='ChromeOS board name')
53 parser.add_argument(
54 'version',
55 nargs='?',
56 type=cros_util.argtype_cros_version,
57 metavar='CROS_VERSION',
58 default=configure.get('CROS_VERSION', ''),
59 help='ChromeOS version number, short (10162.0.0) or full (R64-10162.0.0)')
60
61 return parser
62
63
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +080064def switch(autotest_dir, board, version, test_name):
Kuang-che Wub9705bd2018-06-28 17:59:18 +080065 full_version = cros_util.version_to_full(board, version)
66 logger.info('Unpack autotest packages for %s %s', board, full_version)
67
68 autotest_client_path = GS_AUTOTEST_CLIENT_PATH.format(
69 board=board, full_version=full_version)
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +080070 autotest_server_path = GS_AUTOTEST_SERVER_PATH.format(
71 board=board, full_version=full_version)
Kuang-che Wub9705bd2018-06-28 17:59:18 +080072
73 # TODO(kcwu): cache downloaded tarballs
74 tmp_dir = tempfile.mkdtemp()
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +080075 if os.path.exists(autotest_dir):
76 shutil.rmtree(autotest_dir)
77 os.makedirs(autotest_dir)
Kuang-che Wub9705bd2018-06-28 17:59:18 +080078
79 cros_util.gsutil('cp', autotest_client_path, tmp_dir)
80 tarball = os.path.join(tmp_dir, AUTOTEST_CLIENT_TARBALL)
81 # strip 'autotest/'
82 util.check_call(
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +080083 'tar', 'xvf', tarball, '--strip-components=1', cwd=autotest_dir)
84 cros_util.gsutil('cp', autotest_server_path, tmp_dir)
85 tarball = os.path.join(tmp_dir, AUTOTEST_SERVER_TARBALL)
86 util.check_call(
87 'tar', 'xf', tarball, '--strip-components=1', cwd=autotest_dir)
88
89 # Need to extract the control file if the target is a client site test.
90 if test_name:
91 test_name = test_name.split('.')[0]
92 client_tarball = os.path.abspath(
93 os.path.join(autotest_dir, 'packages', 'test-%s.tar.bz2' % test_name))
94 if os.path.exists(client_tarball):
95 client_test_dir = os.path.join(autotest_dir, 'client', 'site_tests',
96 test_name)
97 if not os.path.exists(client_test_dir):
98 os.makedirs(client_test_dir)
99 util.check_call('tar', 'xvf', client_tarball, cwd=client_test_dir)
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800100
101 shutil.rmtree(tmp_dir)
102
103
104def main(args=None):
105 common.init()
106 parser = create_argument_parser()
107 opts = parser.parse_args(args)
108 common.config_logging(opts)
109
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +0800110 autotest_dir = os.path.join(opts.chromeos_root,
111 cros_util.prebuilt_autotest_dir)
112 switch(autotest_dir, opts.board, opts.version, opts.test_name)
113
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800114 # Verify test control file exists.
115 if opts.test_name:
116 found = cros_util.get_autotest_test_info(autotest_dir, opts.test_name)
Kuang-che Wu99e808f2019-06-26 12:17:32 +0800117 if not found:
118 names = []
119 for control_file in cros_util.enumerate_autotest_control_files(
120 autotest_dir):
121 info = cros_util.parse_autotest_control_file(control_file)
122 names.append(info.name)
123 util.show_similar_candidates('test_name', opts.test_name, names)
Kuang-che Wu057fa772019-08-21 11:35:42 +0800124 sys.exit(cli.EXIT_CODE_FATAL)
Kuang-che Wu99e808f2019-06-26 12:17:32 +0800125
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800126
127if __name__ == '__main__':
128 main()