blob: 0420b1a4a5752c2b7d5ea3665c4e47628d6aaef8 [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 McDonald3c557392020-03-31 13:41:46 -060010import os
Chris McDonald17d86b32020-03-18 17:28:43 -060011import sys
12
13import pytest # pylint: disable=import-error
14
Mike Frysingerd7af8462020-11-11 03:44:54 -050015from chromite.lib import commandline
Chris McDonald3c557392020-03-31 13:41:46 -060016from chromite.lib import constants
17from chromite.lib import cros_build_lib
18from chromite.lib import gs
Chris McDonald5d1af6a2020-04-21 08:00:15 -060019from chromite.lib import cros_logging as logging
Chris McDonald3c557392020-03-31 13:41:46 -060020from chromite.lib import namespaces
21
Chris McDonald17d86b32020-03-18 17:28:43 -060022
23def main(argv):
Chris McDonald5d1af6a2020-04-21 08:00:15 -060024 parser = get_parser()
Mike Frysingerd7af8462020-11-11 03:44:54 -050025 opts = parser.parse_args()
Mike Frysingera819cb32021-04-01 00:55:10 -040026 opts.Freeze()
Mike Frysingerd7af8462020-11-11 03:44:54 -050027
28 pytest_args = opts.pytest_args
29
Chris McDonald5d1af6a2020-04-21 08:00:15 -060030 if opts.quick:
Chris McDonald653510e2020-05-01 17:15:16 -060031 if not cros_build_lib.IsInsideChroot() and opts.chroot:
Mike Frysinger968c1142020-05-09 00:37:56 -040032 logging.warning('Tests start up faster when run from inside the chroot.')
Chris McDonald5d1af6a2020-04-21 08:00:15 -060033
Chris McDonald653510e2020-05-01 17:15:16 -060034 if opts.chroot:
35 ensure_chroot_exists()
36 re_execute_inside_chroot(argv)
37 else:
Chris McDonaldcdfd1132020-05-12 07:09:51 -060038 pytest_args += ['--no-chroot']
Chris McDonald3c557392020-03-31 13:41:46 -060039
40 # This is a cheesy hack to make sure gsutil is populated in the cache before
41 # we run tests. This is a partial workaround for crbug.com/468838.
42 gs.GSContext.GetDefaultGSUtilBin()
43
Chris McDonald5d1af6a2020-04-21 08:00:15 -060044 if opts.quick:
45 logging.info('Skipping test namespacing due to --quickstart.')
46 # Default to running in a single process under --quickstart. User args can
47 # still override this.
48 pytest_args = ['-n', '0'] + pytest_args
49 else:
50 # Namespacing is enabled by default because tests may break each other or
51 # interfere with parts of the running system if not isolated in a namespace.
52 # Disabling namespaces is not recommended for general use.
53 re_execute_with_namespace([sys.argv[0]] + argv)
Chris McDonald3c557392020-03-31 13:41:46 -060054
Chris McDonald5d1af6a2020-04-21 08:00:15 -060055 sys.exit(pytest.main(pytest_args))
Chris McDonald3c557392020-03-31 13:41:46 -060056
57
58def re_execute_with_namespace(argv, network=False):
59 """Re-execute as root so we can unshare resources."""
60 if os.geteuid() != 0:
61 cmd = [
62 'sudo',
63 'HOME=%s' % os.environ['HOME'],
64 'PATH=%s' % os.environ['PATH'],
65 '--',
66 ] + argv
67 os.execvp(cmd[0], cmd)
68 else:
Chris McDonald3c557392020-03-31 13:41:46 -060069 namespaces.SimpleUnshare(net=not network, pid=True)
70 # We got our namespaces, so switch back to the user to run the tests.
71 gid = int(os.environ.pop('SUDO_GID'))
72 uid = int(os.environ.pop('SUDO_UID'))
73 user = os.environ.pop('SUDO_USER')
74 os.initgroups(user, gid)
75 os.setresgid(gid, gid, gid)
76 os.setresuid(uid, uid, uid)
77 os.environ['USER'] = user
78
79
80def re_execute_inside_chroot(argv):
81 """Re-execute the test wrapper inside the chroot."""
Mike Frysinger6df594e2020-11-11 15:02:59 -050082 if cros_build_lib.IsInsideChroot():
83 return
84
85 target = os.path.join(constants.CHROMITE_DIR, 'scripts', 'run_pytest')
86 relpath = os.path.relpath(target, '.')
87 # If we're in the scripts dir, make sure we always have a relative path,
88 # otherwise cros_sdk will search $PATH and fail.
89 if os.path.sep not in relpath:
90 relpath = os.path.join('.', relpath)
Chris McDonald3c557392020-03-31 13:41:46 -060091 cmd = [
92 'cros_sdk',
Mike Frysinger6df594e2020-11-11 15:02:59 -050093 '--working-dir', '.',
Chris McDonald3c557392020-03-31 13:41:46 -060094 '--',
Mike Frysinger6df594e2020-11-11 15:02:59 -050095 relpath,
Chris McDonald3c557392020-03-31 13:41:46 -060096 ]
Mike Frysinger6df594e2020-11-11 15:02:59 -050097 os.execvp(cmd[0], cmd + argv)
Chris McDonald3c557392020-03-31 13:41:46 -060098
99
100def ensure_chroot_exists():
101 """Ensure that a chroot exists for us to run tests in."""
102 chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR)
103 if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot():
104 cros_build_lib.run(['cros_sdk', '--create'])
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600105
106
107def get_parser():
108 """Build the parser for command line arguments."""
Mike Frysingerd7af8462020-11-11 03:44:54 -0500109 parser = commandline.ArgumentParser(
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600110 description=__doc__,
Mike Frysingerd7af8462020-11-11 03:44:54 -0500111 epilog='To see the help output for pytest:\n$ %(prog)s -- --help',
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600112 )
113 parser.add_argument(
114 '--quickstart',
115 dest='quick',
116 action='store_true',
117 help='Skip normal test sandboxing and namespacing for faster start up '
118 'time.',
119 )
Chris McDonald653510e2020-05-01 17:15:16 -0600120 parser.add_argument(
121 '--no-chroot',
122 dest='chroot',
123 action='store_false',
124 help="Don't initialize or enter a chroot for the test invocation. May "
125 'cause tests to unexpectedly fail!',
126 )
Mike Frysingerd7af8462020-11-11 03:44:54 -0500127 parser.add_argument(
128 'pytest_args',
129 metavar='pytest arguments',
130 nargs='*',
131 help='Arguments to pass down to pytest (use -- to help separate)',
132 )
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600133 return parser