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 | |
Alex Klein | 9ea00b5 | 2021-02-24 14:47:22 -0700 | [diff] [blame] | 29 | from chromite.api import compile_build_api_proto |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 30 | from chromite.lib import commandline |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 31 | from chromite.lib import constants |
| 32 | from chromite.lib import cros_build_lib |
| 33 | from chromite.lib import gs |
| 34 | from chromite.lib import namespaces |
| 35 | |
Chris McDonald | 17d86b3 | 2020-03-18 17:28:43 -0600 | [diff] [blame] | 36 | |
| 37 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 38 | parser = get_parser() |
| 39 | opts = parser.parse_args() |
| 40 | opts.Freeze() |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 41 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 42 | pytest_args = opts.pytest_args |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 43 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 44 | if opts.quick: |
| 45 | if not cros_build_lib.IsInsideChroot() and opts.chroot: |
| 46 | logging.warning( |
| 47 | "Tests start up faster when run from inside the chroot." |
| 48 | ) |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 49 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 50 | if opts.chroot: |
| 51 | ensure_chroot_exists() |
| 52 | re_execute_inside_chroot(argv) |
| 53 | else: |
| 54 | pytest_args += ["--no-chroot"] |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 55 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 56 | if opts.network: |
| 57 | pytest_args += ["-m", "not network_test or network_test"] |
Mike Frysinger | c73c6b8 | 2021-04-22 00:07:54 -0400 | [diff] [blame] | 58 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 59 | precache() |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 60 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 61 | if opts.quick: |
| 62 | logging.info("Skipping test namespacing due to --quickstart.") |
| 63 | # Default to running in a single process under --quickstart. User args can |
| 64 | # still override this. |
| 65 | pytest_args = ["-n", "0"] + pytest_args |
| 66 | else: |
| 67 | # Namespacing is enabled by default because tests may break each other or |
| 68 | # interfere with parts of the running system if not isolated in a namespace. |
| 69 | # Disabling namespaces is not recommended for general use. |
| 70 | namespaces.ReExecuteWithNamespace( |
| 71 | [sys.argv[0]] + argv, network=opts.network |
| 72 | ) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 74 | # Check the environment. https://crbug.com/1015450 |
| 75 | st = os.stat("/") |
| 76 | if st.st_mode & 0o007 != 0o005: |
| 77 | cros_build_lib.Die( |
| 78 | f"The root directory has broken permissions: {st.st_mode:o}\n" |
| 79 | "Fix with: sudo chmod o+rx-w /" |
| 80 | ) |
| 81 | if st.st_uid or st.st_gid: |
| 82 | cros_build_lib.Die( |
| 83 | f"The root directory has broken ownership: {st.st_uid}:{st.st_gid}" |
| 84 | " (should be 0:0)\nFix with: sudo chown 0:0 /" |
| 85 | ) |
Mike Frysinger | 55919ef | 2021-04-01 00:55:39 -0400 | [diff] [blame] | 86 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 87 | logging.debug("Running: pytest %s", cros_build_lib.CmdToStr(pytest_args)) |
| 88 | sys.exit(pytest.main(pytest_args)) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 89 | |
| 90 | |
Alex Klein | 9ea00b5 | 2021-02-24 14:47:22 -0700 | [diff] [blame] | 91 | def precache(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 92 | """Do some network-dependent stuff before we disallow network access.""" |
| 93 | # This is a cheesy hack to make sure gsutil is populated in the cache before |
| 94 | # we run tests. This is a partial workaround for crbug.com/468838. |
| 95 | gs.GSContext.InitializeCache() |
| 96 | # Ensure protoc is installed for api/compile_build_api_proto_unittest. |
| 97 | compile_build_api_proto.InstallProtoc( |
| 98 | compile_build_api_proto.ProtocVersion.CHROMITE |
| 99 | ) |
Alex Klein | 9ea00b5 | 2021-02-24 14:47:22 -0700 | [diff] [blame] | 100 | |
| 101 | |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 102 | def re_execute_inside_chroot(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 103 | """Re-execute the test wrapper inside the chroot.""" |
| 104 | if cros_build_lib.IsInsideChroot(): |
| 105 | return |
Mike Frysinger | 6df594e | 2020-11-11 15:02:59 -0500 | [diff] [blame] | 106 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 107 | target = os.path.join(constants.CHROMITE_DIR, "scripts", "run_tests") |
| 108 | relpath = os.path.relpath(target, ".") |
| 109 | # If we're in the scripts dir, make sure we always have a relative path, |
| 110 | # otherwise cros_sdk will search $PATH and fail. |
| 111 | if os.path.sep not in relpath: |
| 112 | relpath = os.path.join(".", relpath) |
| 113 | cmd = [ |
| 114 | "cros_sdk", |
| 115 | "--working-dir", |
| 116 | ".", |
| 117 | "--", |
| 118 | relpath, |
| 119 | ] |
| 120 | os.execvp(cmd[0], cmd + argv) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 121 | |
| 122 | |
| 123 | def ensure_chroot_exists(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 124 | """Ensure that a chroot exists for us to run tests in.""" |
| 125 | chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR) |
| 126 | if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot(): |
| 127 | cros_build_lib.run(["cros_sdk", "--create"]) |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 128 | |
| 129 | |
| 130 | def get_parser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 131 | """Build the parser for command line arguments.""" |
| 132 | parser = commandline.ArgumentParser( |
| 133 | description=__doc__, |
| 134 | epilog="To see the help output for pytest:\n$ %(prog)s -- --help", |
| 135 | ) |
| 136 | parser.add_argument( |
| 137 | "--quickstart", |
| 138 | dest="quick", |
| 139 | action="store_true", |
| 140 | help="Skip normal test sandboxing and namespacing for faster start up " |
| 141 | "time.", |
| 142 | ) |
| 143 | parser.add_argument( |
| 144 | "--network", |
| 145 | action="store_true", |
| 146 | help="Include network tests.", |
| 147 | ) |
| 148 | parser.add_argument( |
| 149 | "--no-chroot", |
| 150 | dest="chroot", |
| 151 | action="store_false", |
| 152 | help="Don't initialize or enter a chroot for the test invocation. May " |
| 153 | "cause tests to unexpectedly fail!", |
| 154 | ) |
| 155 | parser.add_argument( |
| 156 | "pytest_args", |
| 157 | metavar="pytest arguments", |
| 158 | nargs="*", |
| 159 | help="Arguments to pass down to pytest (use -- to help separate)", |
| 160 | ) |
| 161 | return parser |