blob: 707f0695789f9914f4f6189ab0b9407900e20188 [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:
Chris McDonald653510e2020-05-01 17:15:16 -060028 if not cros_build_lib.IsInsideChroot() and opts.chroot:
Mike Frysinger968c1142020-05-09 00:37:56 -040029 logging.warning('Tests start up faster when run from inside the chroot.')
Chris McDonald5d1af6a2020-04-21 08:00:15 -060030
Chris McDonald653510e2020-05-01 17:15:16 -060031 if opts.chroot:
32 ensure_chroot_exists()
33 re_execute_inside_chroot(argv)
34 else:
35 os.chdir(constants.CHROMITE_DIR)
Chris McDonaldcdfd1132020-05-12 07:09:51 -060036 pytest_args += ['--no-chroot']
Chris McDonald3c557392020-03-31 13:41:46 -060037
38 # This is a cheesy hack to make sure gsutil is populated in the cache before
39 # we run tests. This is a partial workaround for crbug.com/468838.
40 gs.GSContext.GetDefaultGSUtilBin()
41
Chris McDonald5d1af6a2020-04-21 08:00:15 -060042 if opts.quick:
43 logging.info('Skipping test namespacing due to --quickstart.')
44 # Default to running in a single process under --quickstart. User args can
45 # still override this.
46 pytest_args = ['-n', '0'] + pytest_args
47 else:
48 # Namespacing is enabled by default because tests may break each other or
49 # interfere with parts of the running system if not isolated in a namespace.
50 # Disabling namespaces is not recommended for general use.
51 re_execute_with_namespace([sys.argv[0]] + argv)
Chris McDonald3c557392020-03-31 13:41:46 -060052
Chris McDonald5d1af6a2020-04-21 08:00:15 -060053 sys.exit(pytest.main(pytest_args))
Chris McDonald3c557392020-03-31 13:41:46 -060054
55
56def re_execute_with_namespace(argv, network=False):
57 """Re-execute as root so we can unshare resources."""
58 if os.geteuid() != 0:
59 cmd = [
60 'sudo',
61 'HOME=%s' % os.environ['HOME'],
62 'PATH=%s' % os.environ['PATH'],
63 '--',
64 ] + argv
65 os.execvp(cmd[0], cmd)
66 else:
67 cgroups.Cgroup.InitSystem()
68 namespaces.SimpleUnshare(net=not network, pid=True)
69 # We got our namespaces, so switch back to the user to run the tests.
70 gid = int(os.environ.pop('SUDO_GID'))
71 uid = int(os.environ.pop('SUDO_UID'))
72 user = os.environ.pop('SUDO_USER')
73 os.initgroups(user, gid)
74 os.setresgid(gid, gid, gid)
75 os.setresuid(uid, uid, uid)
76 os.environ['USER'] = user
77
78
79def re_execute_inside_chroot(argv):
80 """Re-execute the test wrapper inside the chroot."""
81 cmd = [
82 'cros_sdk',
83 '--',
84 os.path.join('..', '..', 'chromite', 'run_pytest'),
85 ]
86 if not cros_build_lib.IsInsideChroot():
87 os.execvp(cmd[0], cmd + argv)
88 else:
89 os.chdir(constants.CHROMITE_DIR)
90
91
92def ensure_chroot_exists():
93 """Ensure that a chroot exists for us to run tests in."""
94 chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR)
95 if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot():
96 cros_build_lib.run(['cros_sdk', '--create'])
Chris McDonald5d1af6a2020-04-21 08:00:15 -060097
98
99def get_parser():
100 """Build the parser for command line arguments."""
101 parser = argparse.ArgumentParser(
102 description=__doc__,
103 epilog='To see the help output for pytest, run `pytest --help` inside '
104 'the chroot.',
105 )
106 parser.add_argument(
107 '--quickstart',
108 dest='quick',
109 action='store_true',
110 help='Skip normal test sandboxing and namespacing for faster start up '
111 'time.',
112 )
Chris McDonald653510e2020-05-01 17:15:16 -0600113 parser.add_argument(
114 '--no-chroot',
115 dest='chroot',
116 action='store_false',
117 help="Don't initialize or enter a chroot for the test invocation. May "
118 'cause tests to unexpectedly fail!',
119 )
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600120 return parser