Chris McDonald | 17d86b3 | 2020-03-18 17:28:43 -0600 | [diff] [blame] | 1 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Mike Frysinger | 4975e5a | 2021-04-13 17:06:09 -0400 | [diff] [blame] | 5 | """Chromite main test runner. |
| 6 | |
| 7 | Run the specified tests. If none are specified, we'll scan the |
| 8 | tree looking for tests to run and then only run the semi-fast ones. |
Mike Frysinger | 2aa424f | 2021-07-19 21:37:29 -0400 | [diff] [blame] | 9 | |
| 10 | https://docs.pytest.org/en/latest/how-to/usage.html#specifying-tests-selecting-tests |
| 11 | |
| 12 | Examples: |
| 13 | # Run all tests in a module. |
| 14 | $ ./run_tests lib/osutils_unittest.py |
| 15 | # Run a class of tests in a module. |
| 16 | $ ./run_tests lib/osutils_unittest.py::TestOsutils |
| 17 | # Run a single test. |
| 18 | $ ./run_tests lib/osutils_unittest.py::TestOsutils::testIsSubPath |
| 19 | # List all tests that'd be run. |
| 20 | $ ./run_tests -- --collect-only |
Mike Frysinger | 4975e5a | 2021-04-13 17:06:09 -0400 | [diff] [blame] | 21 | """ |
Chris McDonald | 17d86b3 | 2020-03-18 17:28:43 -0600 | [diff] [blame] | 22 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 23 | import logging |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 24 | import os |
Chris McDonald | 17d86b3 | 2020-03-18 17:28:43 -0600 | [diff] [blame] | 25 | import sys |
| 26 | |
| 27 | import pytest # pylint: disable=import-error |
| 28 | |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 29 | from chromite.lib import commandline |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 30 | from chromite.lib import constants |
| 31 | from chromite.lib import cros_build_lib |
| 32 | from chromite.lib import gs |
| 33 | from chromite.lib import namespaces |
| 34 | |
Chris McDonald | 17d86b3 | 2020-03-18 17:28:43 -0600 | [diff] [blame] | 35 | |
| 36 | def main(argv): |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 37 | parser = get_parser() |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 38 | opts = parser.parse_args() |
Mike Frysinger | a819cb3 | 2021-04-01 00:55:10 -0400 | [diff] [blame] | 39 | opts.Freeze() |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 40 | |
| 41 | pytest_args = opts.pytest_args |
| 42 | |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 43 | if opts.quick: |
Chris McDonald | 653510e | 2020-05-01 17:15:16 -0600 | [diff] [blame] | 44 | if not cros_build_lib.IsInsideChroot() and opts.chroot: |
Mike Frysinger | 968c114 | 2020-05-09 00:37:56 -0400 | [diff] [blame] | 45 | logging.warning('Tests start up faster when run from inside the chroot.') |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 46 | |
Chris McDonald | 653510e | 2020-05-01 17:15:16 -0600 | [diff] [blame] | 47 | if opts.chroot: |
| 48 | ensure_chroot_exists() |
| 49 | re_execute_inside_chroot(argv) |
| 50 | else: |
Chris McDonald | cdfd113 | 2020-05-12 07:09:51 -0600 | [diff] [blame] | 51 | pytest_args += ['--no-chroot'] |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 52 | |
Mike Frysinger | c73c6b8 | 2021-04-22 00:07:54 -0400 | [diff] [blame] | 53 | if opts.network: |
| 54 | pytest_args += ['-m', 'not network_test or network_test'] |
| 55 | |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 56 | # This is a cheesy hack to make sure gsutil is populated in the cache before |
| 57 | # we run tests. This is a partial workaround for crbug.com/468838. |
Mike Frysinger | 682b333 | 2021-06-22 11:07:17 -0400 | [diff] [blame] | 58 | gs.GSContext.InitializeCache() |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 59 | |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 60 | if opts.quick: |
| 61 | logging.info('Skipping test namespacing due to --quickstart.') |
| 62 | # Default to running in a single process under --quickstart. User args can |
| 63 | # still override this. |
| 64 | pytest_args = ['-n', '0'] + pytest_args |
| 65 | else: |
| 66 | # Namespacing is enabled by default because tests may break each other or |
| 67 | # interfere with parts of the running system if not isolated in a namespace. |
| 68 | # Disabling namespaces is not recommended for general use. |
Mike Frysinger | c73c6b8 | 2021-04-22 00:07:54 -0400 | [diff] [blame] | 69 | re_execute_with_namespace([sys.argv[0]] + argv, network=opts.network) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 70 | |
Mike Frysinger | 55919ef | 2021-04-01 00:55:39 -0400 | [diff] [blame] | 71 | # Check the environment. https://crbug.com/1015450 |
| 72 | st = os.stat('/') |
| 73 | if st.st_mode & 0o7777 != 0o755: |
| 74 | cros_build_lib.Die( |
| 75 | f'The root directory has broken permissions: {st.st_mode:o}\n' |
| 76 | 'Fix with: sudo chmod 755 /') |
| 77 | if st.st_uid or st.st_gid: |
| 78 | cros_build_lib.Die( |
| 79 | f'The root directory has broken ownership: {st.st_uid}:{st.st_gid}' |
| 80 | ' (should be 0:0)\nFix with: sudo chown 0:0 /') |
| 81 | |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 82 | sys.exit(pytest.main(pytest_args)) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def re_execute_with_namespace(argv, network=False): |
| 86 | """Re-execute as root so we can unshare resources.""" |
| 87 | if os.geteuid() != 0: |
| 88 | cmd = [ |
| 89 | 'sudo', |
| 90 | 'HOME=%s' % os.environ['HOME'], |
| 91 | 'PATH=%s' % os.environ['PATH'], |
| 92 | '--', |
| 93 | ] + argv |
| 94 | os.execvp(cmd[0], cmd) |
| 95 | else: |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 96 | namespaces.SimpleUnshare(net=not network, pid=True) |
| 97 | # We got our namespaces, so switch back to the user to run the tests. |
| 98 | gid = int(os.environ.pop('SUDO_GID')) |
| 99 | uid = int(os.environ.pop('SUDO_UID')) |
| 100 | user = os.environ.pop('SUDO_USER') |
| 101 | os.initgroups(user, gid) |
| 102 | os.setresgid(gid, gid, gid) |
| 103 | os.setresuid(uid, uid, uid) |
| 104 | os.environ['USER'] = user |
| 105 | |
| 106 | |
| 107 | def re_execute_inside_chroot(argv): |
| 108 | """Re-execute the test wrapper inside the chroot.""" |
Mike Frysinger | 6df594e | 2020-11-11 15:02:59 -0500 | [diff] [blame] | 109 | if cros_build_lib.IsInsideChroot(): |
| 110 | return |
| 111 | |
Mike Frysinger | 4975e5a | 2021-04-13 17:06:09 -0400 | [diff] [blame] | 112 | target = os.path.join(constants.CHROMITE_DIR, 'scripts', 'run_tests') |
Mike Frysinger | 6df594e | 2020-11-11 15:02:59 -0500 | [diff] [blame] | 113 | relpath = os.path.relpath(target, '.') |
| 114 | # If we're in the scripts dir, make sure we always have a relative path, |
| 115 | # otherwise cros_sdk will search $PATH and fail. |
| 116 | if os.path.sep not in relpath: |
| 117 | relpath = os.path.join('.', relpath) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 118 | cmd = [ |
| 119 | 'cros_sdk', |
Mike Frysinger | 6df594e | 2020-11-11 15:02:59 -0500 | [diff] [blame] | 120 | '--working-dir', '.', |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 121 | '--', |
Mike Frysinger | 6df594e | 2020-11-11 15:02:59 -0500 | [diff] [blame] | 122 | relpath, |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 123 | ] |
Mike Frysinger | 6df594e | 2020-11-11 15:02:59 -0500 | [diff] [blame] | 124 | os.execvp(cmd[0], cmd + argv) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 125 | |
| 126 | |
| 127 | def ensure_chroot_exists(): |
| 128 | """Ensure that a chroot exists for us to run tests in.""" |
| 129 | chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR) |
| 130 | if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot(): |
| 131 | cros_build_lib.run(['cros_sdk', '--create']) |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 132 | |
| 133 | |
| 134 | def get_parser(): |
| 135 | """Build the parser for command line arguments.""" |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 136 | parser = commandline.ArgumentParser( |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 137 | description=__doc__, |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 138 | epilog='To see the help output for pytest:\n$ %(prog)s -- --help', |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 139 | ) |
| 140 | parser.add_argument( |
| 141 | '--quickstart', |
| 142 | dest='quick', |
| 143 | action='store_true', |
| 144 | help='Skip normal test sandboxing and namespacing for faster start up ' |
| 145 | 'time.', |
| 146 | ) |
Chris McDonald | 653510e | 2020-05-01 17:15:16 -0600 | [diff] [blame] | 147 | parser.add_argument( |
Mike Frysinger | c73c6b8 | 2021-04-22 00:07:54 -0400 | [diff] [blame] | 148 | '--network', |
| 149 | action='store_true', |
| 150 | help='Include network tests.', |
| 151 | ) |
| 152 | parser.add_argument( |
Chris McDonald | 653510e | 2020-05-01 17:15:16 -0600 | [diff] [blame] | 153 | '--no-chroot', |
| 154 | dest='chroot', |
| 155 | action='store_false', |
| 156 | help="Don't initialize or enter a chroot for the test invocation. May " |
| 157 | 'cause tests to unexpectedly fail!', |
| 158 | ) |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 159 | parser.add_argument( |
| 160 | 'pytest_args', |
| 161 | metavar='pytest arguments', |
| 162 | nargs='*', |
| 163 | help='Arguments to pass down to pytest (use -- to help separate)', |
| 164 | ) |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 165 | return parser |