Chris McDonald | 17d86b3 | 2020-03-18 17:28:43 -0600 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 10 | import os |
Chris McDonald | 17d86b3 | 2020-03-18 17:28:43 -0600 | [diff] [blame] | 11 | import sys |
| 12 | |
| 13 | import pytest # pylint: disable=import-error |
| 14 | |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 15 | from chromite.lib import commandline |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 16 | from chromite.lib import constants |
| 17 | from chromite.lib import cros_build_lib |
| 18 | from chromite.lib import gs |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 19 | from chromite.lib import cros_logging as logging |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 20 | from chromite.lib import namespaces |
| 21 | |
Chris McDonald | 17d86b3 | 2020-03-18 17:28:43 -0600 | [diff] [blame] | 22 | |
| 23 | def main(argv): |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 24 | parser = get_parser() |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 25 | opts = parser.parse_args() |
Mike Frysinger | a819cb3 | 2021-04-01 00:55:10 -0400 | [diff] [blame] | 26 | opts.Freeze() |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 27 | |
| 28 | pytest_args = opts.pytest_args |
| 29 | |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 30 | if opts.quick: |
Chris McDonald | 653510e | 2020-05-01 17:15:16 -0600 | [diff] [blame] | 31 | if not cros_build_lib.IsInsideChroot() and opts.chroot: |
Mike Frysinger | 968c114 | 2020-05-09 00:37:56 -0400 | [diff] [blame] | 32 | logging.warning('Tests start up faster when run from inside the chroot.') |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 33 | |
Chris McDonald | 653510e | 2020-05-01 17:15:16 -0600 | [diff] [blame] | 34 | if opts.chroot: |
| 35 | ensure_chroot_exists() |
| 36 | re_execute_inside_chroot(argv) |
| 37 | else: |
Chris McDonald | cdfd113 | 2020-05-12 07:09:51 -0600 | [diff] [blame] | 38 | pytest_args += ['--no-chroot'] |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 39 | |
| 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 McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 44 | 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 McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 54 | |
Mike Frysinger | 55919ef | 2021-04-01 00:55:39 -0400 | [diff] [blame^] | 55 | # Check the environment. https://crbug.com/1015450 |
| 56 | st = os.stat('/') |
| 57 | if st.st_mode & 0o7777 != 0o755: |
| 58 | cros_build_lib.Die( |
| 59 | f'The root directory has broken permissions: {st.st_mode:o}\n' |
| 60 | 'Fix with: sudo chmod 755 /') |
| 61 | if st.st_uid or st.st_gid: |
| 62 | cros_build_lib.Die( |
| 63 | f'The root directory has broken ownership: {st.st_uid}:{st.st_gid}' |
| 64 | ' (should be 0:0)\nFix with: sudo chown 0:0 /') |
| 65 | |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 66 | sys.exit(pytest.main(pytest_args)) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 67 | |
| 68 | |
| 69 | def re_execute_with_namespace(argv, network=False): |
| 70 | """Re-execute as root so we can unshare resources.""" |
| 71 | if os.geteuid() != 0: |
| 72 | cmd = [ |
| 73 | 'sudo', |
| 74 | 'HOME=%s' % os.environ['HOME'], |
| 75 | 'PATH=%s' % os.environ['PATH'], |
| 76 | '--', |
| 77 | ] + argv |
| 78 | os.execvp(cmd[0], cmd) |
| 79 | else: |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 80 | namespaces.SimpleUnshare(net=not network, pid=True) |
| 81 | # We got our namespaces, so switch back to the user to run the tests. |
| 82 | gid = int(os.environ.pop('SUDO_GID')) |
| 83 | uid = int(os.environ.pop('SUDO_UID')) |
| 84 | user = os.environ.pop('SUDO_USER') |
| 85 | os.initgroups(user, gid) |
| 86 | os.setresgid(gid, gid, gid) |
| 87 | os.setresuid(uid, uid, uid) |
| 88 | os.environ['USER'] = user |
| 89 | |
| 90 | |
| 91 | def re_execute_inside_chroot(argv): |
| 92 | """Re-execute the test wrapper inside the chroot.""" |
Mike Frysinger | 6df594e | 2020-11-11 15:02:59 -0500 | [diff] [blame] | 93 | if cros_build_lib.IsInsideChroot(): |
| 94 | return |
| 95 | |
| 96 | target = os.path.join(constants.CHROMITE_DIR, 'scripts', 'run_pytest') |
| 97 | relpath = os.path.relpath(target, '.') |
| 98 | # If we're in the scripts dir, make sure we always have a relative path, |
| 99 | # otherwise cros_sdk will search $PATH and fail. |
| 100 | if os.path.sep not in relpath: |
| 101 | relpath = os.path.join('.', relpath) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 102 | cmd = [ |
| 103 | 'cros_sdk', |
Mike Frysinger | 6df594e | 2020-11-11 15:02:59 -0500 | [diff] [blame] | 104 | '--working-dir', '.', |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 105 | '--', |
Mike Frysinger | 6df594e | 2020-11-11 15:02:59 -0500 | [diff] [blame] | 106 | relpath, |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 107 | ] |
Mike Frysinger | 6df594e | 2020-11-11 15:02:59 -0500 | [diff] [blame] | 108 | os.execvp(cmd[0], cmd + argv) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 109 | |
| 110 | |
| 111 | def ensure_chroot_exists(): |
| 112 | """Ensure that a chroot exists for us to run tests in.""" |
| 113 | chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR) |
| 114 | if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot(): |
| 115 | cros_build_lib.run(['cros_sdk', '--create']) |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 116 | |
| 117 | |
| 118 | def get_parser(): |
| 119 | """Build the parser for command line arguments.""" |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 120 | parser = commandline.ArgumentParser( |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 121 | description=__doc__, |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 122 | epilog='To see the help output for pytest:\n$ %(prog)s -- --help', |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 123 | ) |
| 124 | parser.add_argument( |
| 125 | '--quickstart', |
| 126 | dest='quick', |
| 127 | action='store_true', |
| 128 | help='Skip normal test sandboxing and namespacing for faster start up ' |
| 129 | 'time.', |
| 130 | ) |
Chris McDonald | 653510e | 2020-05-01 17:15:16 -0600 | [diff] [blame] | 131 | parser.add_argument( |
| 132 | '--no-chroot', |
| 133 | dest='chroot', |
| 134 | action='store_false', |
| 135 | help="Don't initialize or enter a chroot for the test invocation. May " |
| 136 | 'cause tests to unexpectedly fail!', |
| 137 | ) |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 138 | parser.add_argument( |
| 139 | 'pytest_args', |
| 140 | metavar='pytest arguments', |
| 141 | nargs='*', |
| 142 | help='Arguments to pass down to pytest (use -- to help separate)', |
| 143 | ) |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 144 | return parser |