blob: 7ab49b39b030c803096d62ddeac9c3f4b264dc91 [file] [log] [blame]
Chris McDonald17d86b32020-03-18 17:28:43 -06001# -*- coding: utf-8 -*-
2# Copyright 2020 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Wrapper to execute pytest inside the chromite virtualenv."""
7
8from __future__ import print_function
9
Chris McDonald5d1af6a2020-04-21 08:00:15 -060010import argparse
Chris McDonald3c557392020-03-31 13:41:46 -060011import os
Chris McDonald17d86b32020-03-18 17:28:43 -060012import sys
13
14import pytest # pylint: disable=import-error
15
Chris McDonald3c557392020-03-31 13:41:46 -060016from chromite.lib import cgroups
17from chromite.lib import constants
18from chromite.lib import cros_build_lib
19from chromite.lib import gs
Chris McDonald5d1af6a2020-04-21 08:00:15 -060020from chromite.lib import cros_logging as logging
Chris McDonald3c557392020-03-31 13:41:46 -060021from chromite.lib import namespaces
22
Chris McDonald17d86b32020-03-18 17:28:43 -060023
24def main(argv):
Chris McDonald5d1af6a2020-04-21 08:00:15 -060025 parser = get_parser()
26 opts, pytest_args = parser.parse_known_args()
27 if opts.quick:
28 if not cros_build_lib.IsInsideChroot():
29 logging.warn('Running tests from inside the chroot will start up faster.')
30
Chris McDonald3c557392020-03-31 13:41:46 -060031 ensure_chroot_exists()
32 re_execute_inside_chroot(argv)
33
34 # This is a cheesy hack to make sure gsutil is populated in the cache before
35 # we run tests. This is a partial workaround for crbug.com/468838.
36 gs.GSContext.GetDefaultGSUtilBin()
37
Chris McDonald5d1af6a2020-04-21 08:00:15 -060038 if opts.quick:
39 logging.info('Skipping test namespacing due to --quickstart.')
40 # Default to running in a single process under --quickstart. User args can
41 # still override this.
42 pytest_args = ['-n', '0'] + pytest_args
43 else:
44 # Namespacing is enabled by default because tests may break each other or
45 # interfere with parts of the running system if not isolated in a namespace.
46 # Disabling namespaces is not recommended for general use.
47 re_execute_with_namespace([sys.argv[0]] + argv)
Chris McDonald3c557392020-03-31 13:41:46 -060048
Chris McDonald5d1af6a2020-04-21 08:00:15 -060049 sys.exit(pytest.main(pytest_args))
Chris McDonald3c557392020-03-31 13:41:46 -060050
51
52def re_execute_with_namespace(argv, network=False):
53 """Re-execute as root so we can unshare resources."""
54 if os.geteuid() != 0:
55 cmd = [
56 'sudo',
57 'HOME=%s' % os.environ['HOME'],
58 'PATH=%s' % os.environ['PATH'],
59 '--',
60 ] + argv
61 os.execvp(cmd[0], cmd)
62 else:
63 cgroups.Cgroup.InitSystem()
64 namespaces.SimpleUnshare(net=not network, pid=True)
65 # We got our namespaces, so switch back to the user to run the tests.
66 gid = int(os.environ.pop('SUDO_GID'))
67 uid = int(os.environ.pop('SUDO_UID'))
68 user = os.environ.pop('SUDO_USER')
69 os.initgroups(user, gid)
70 os.setresgid(gid, gid, gid)
71 os.setresuid(uid, uid, uid)
72 os.environ['USER'] = user
73
74
75def re_execute_inside_chroot(argv):
76 """Re-execute the test wrapper inside the chroot."""
77 cmd = [
78 'cros_sdk',
79 '--',
80 os.path.join('..', '..', 'chromite', 'run_pytest'),
81 ]
82 if not cros_build_lib.IsInsideChroot():
83 os.execvp(cmd[0], cmd + argv)
84 else:
85 os.chdir(constants.CHROMITE_DIR)
86
87
88def ensure_chroot_exists():
89 """Ensure that a chroot exists for us to run tests in."""
90 chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR)
91 if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot():
92 cros_build_lib.run(['cros_sdk', '--create'])
Chris McDonald5d1af6a2020-04-21 08:00:15 -060093
94
95def get_parser():
96 """Build the parser for command line arguments."""
97 parser = argparse.ArgumentParser(
98 description=__doc__,
99 epilog='To see the help output for pytest, run `pytest --help` inside '
100 'the chroot.',
101 )
102 parser.add_argument(
103 '--quickstart',
104 dest='quick',
105 action='store_true',
106 help='Skip normal test sandboxing and namespacing for faster start up '
107 'time.',
108 )
109 return parser