Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2020 The ChromiumOS Authors |
Chris McDonald | 17d86b3 | 2020-03-18 17:28:43 -0600 | [diff] [blame] | 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 | |
Brian Norris | d7d9150 | 2023-08-18 16:39:38 -0700 | [diff] [blame] | 10 | https://docs.pytest.org/en/latest/how-to/usage.html#specifying-which-tests-to-run |
Mike Frysinger | 2aa424f | 2021-07-19 21:37:29 -0400 | [diff] [blame] | 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 | fbb1c14 | 2023-03-10 00:47:21 -0500 | [diff] [blame] | 30 | from chromite.format import formatters |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 31 | from chromite.lib import commandline |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 32 | from chromite.lib import constants |
| 33 | from chromite.lib import cros_build_lib |
| 34 | from chromite.lib import gs |
| 35 | from chromite.lib import namespaces |
Mike Frysinger | 3763bf2 | 2023-03-30 12:38:28 -0400 | [diff] [blame] | 36 | from chromite.lint import linters |
Mike Frysinger | fbb1c14 | 2023-03-10 00:47:21 -0500 | [diff] [blame] | 37 | from chromite.scripts import clang_format |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 38 | |
Chris McDonald | 17d86b3 | 2020-03-18 17:28:43 -0600 | [diff] [blame] | 39 | |
| 40 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 41 | parser = get_parser() |
| 42 | opts = parser.parse_args() |
| 43 | opts.Freeze() |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | pytest_args = opts.pytest_args |
Mike Frysinger | d7af846 | 2020-11-11 03:44:54 -0500 | [diff] [blame] | 46 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | if opts.chroot: |
| 48 | ensure_chroot_exists() |
| 49 | re_execute_inside_chroot(argv) |
| 50 | else: |
| 51 | pytest_args += ["--no-chroot"] |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 52 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 53 | if opts.network: |
| 54 | pytest_args += ["-m", "not network_test or network_test"] |
Mike Frysinger | c73c6b8 | 2021-04-22 00:07:54 -0400 | [diff] [blame] | 55 | |
Mike Frysinger | 08b1012 | 2023-03-29 21:46:50 -0400 | [diff] [blame] | 56 | if opts.precache: |
| 57 | precache() |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 58 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | if opts.quick: |
| 60 | logging.info("Skipping test namespacing due to --quickstart.") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 61 | else: |
Trent Apted | 4a0812b | 2023-05-15 15:33:55 +1000 | [diff] [blame] | 62 | # Namespacing is enabled by default because tests may break each other |
| 63 | # or interfere with parts of the running system if not isolated in a |
| 64 | # namespace. Disabling namespaces is not recommended for general use. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 65 | namespaces.ReExecuteWithNamespace( |
Mike Frysinger | 08b1012 | 2023-03-29 21:46:50 -0400 | [diff] [blame] | 66 | [sys.argv[0], "--no-precache"] + argv, network=opts.network |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | ) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 68 | |
Mike Frysinger | 55aa309 | 2023-02-15 09:43:22 -0500 | [diff] [blame] | 69 | jobs = opts.jobs |
| 70 | if jobs is None: |
Trent Apted | 4a0812b | 2023-05-15 15:33:55 +1000 | [diff] [blame] | 71 | # Default to running in a single process under --quickstart. User args |
Alex Klein | de65935 | 2023-10-03 12:14:21 -0600 | [diff] [blame^] | 72 | # can still override this. Cap it at 64 by default to prevent the |
| 73 | # overhead from spawning too many nodes. |
| 74 | jobs = 0 if opts.quick else min(os.cpu_count(), 64) |
Mike Frysinger | 55aa309 | 2023-02-15 09:43:22 -0500 | [diff] [blame] | 75 | pytest_args = ["-n", str(jobs)] + pytest_args |
| 76 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | # Check the environment. https://crbug.com/1015450 |
| 78 | st = os.stat("/") |
| 79 | if st.st_mode & 0o007 != 0o005: |
| 80 | cros_build_lib.Die( |
| 81 | f"The root directory has broken permissions: {st.st_mode:o}\n" |
| 82 | "Fix with: sudo chmod o+rx-w /" |
| 83 | ) |
| 84 | if st.st_uid or st.st_gid: |
| 85 | cros_build_lib.Die( |
| 86 | f"The root directory has broken ownership: {st.st_uid}:{st.st_gid}" |
| 87 | " (should be 0:0)\nFix with: sudo chown 0:0 /" |
| 88 | ) |
Mike Frysinger | 55919ef | 2021-04-01 00:55:39 -0400 | [diff] [blame] | 89 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | logging.debug("Running: pytest %s", cros_build_lib.CmdToStr(pytest_args)) |
| 91 | sys.exit(pytest.main(pytest_args)) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 92 | |
| 93 | |
Alex Klein | 9ea00b5 | 2021-02-24 14:47:22 -0700 | [diff] [blame] | 94 | def precache(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 95 | """Do some network-dependent stuff before we disallow network access.""" |
Mike Frysinger | fbb1c14 | 2023-03-10 00:47:21 -0500 | [diff] [blame] | 96 | # pylint: disable=protected-access |
Mike Frysinger | 0aa0d58 | 2023-03-24 12:55:01 -0400 | [diff] [blame] | 97 | logging.notice("Caching tools from network (cipd/vpython/etc...)") |
Mike Frysinger | fbb1c14 | 2023-03-10 00:47:21 -0500 | [diff] [blame] | 98 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 99 | # This is a cheesy hack to make sure gsutil is populated in the cache before |
| 100 | # we run tests. This is a partial workaround for crbug.com/468838. |
| 101 | gs.GSContext.InitializeCache() |
| 102 | # Ensure protoc is installed for api/compile_build_api_proto_unittest. |
| 103 | compile_build_api_proto.InstallProtoc( |
| 104 | compile_build_api_proto.ProtocVersion.CHROMITE |
| 105 | ) |
Mike Frysinger | fbb1c14 | 2023-03-10 00:47:21 -0500 | [diff] [blame] | 106 | # Ensure various tools are available. |
Mike Frysinger | 29e1da9 | 2023-03-21 10:52:43 -0400 | [diff] [blame] | 107 | cros_build_lib.dbg_run( |
Mike Frysinger | a69df98 | 2023-03-21 16:52:27 -0400 | [diff] [blame] | 108 | [constants.CHROMITE_DIR / "scripts" / "black", "--version"], |
Mike Frysinger | 29e1da9 | 2023-03-21 10:52:43 -0400 | [diff] [blame] | 109 | capture_output=True, |
| 110 | ) |
| 111 | cros_build_lib.dbg_run( |
Mike Frysinger | a69df98 | 2023-03-21 16:52:27 -0400 | [diff] [blame] | 112 | [constants.CHROMITE_DIR / "scripts" / "isort", "--version"], |
Mike Frysinger | 29e1da9 | 2023-03-21 10:52:43 -0400 | [diff] [blame] | 113 | capture_output=True, |
| 114 | ) |
Mike Frysinger | 0aa0d58 | 2023-03-24 12:55:01 -0400 | [diff] [blame] | 115 | formatters.gn._find_gn() |
Mike Frysinger | fbb1c14 | 2023-03-10 00:47:21 -0500 | [diff] [blame] | 116 | formatters.star._find_buildifier() |
| 117 | formatters.textproto._find_txtpbfmt() |
Mike Frysinger | 3763bf2 | 2023-03-30 12:38:28 -0400 | [diff] [blame] | 118 | linters.shell._find_shellcheck() |
Mike Frysinger | fbb1c14 | 2023-03-10 00:47:21 -0500 | [diff] [blame] | 119 | with clang_format.ClangFormat(): |
| 120 | pass |
Alex Klein | 9ea00b5 | 2021-02-24 14:47:22 -0700 | [diff] [blame] | 121 | |
| 122 | |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 123 | def re_execute_inside_chroot(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 124 | """Re-execute the test wrapper inside the chroot.""" |
| 125 | if cros_build_lib.IsInsideChroot(): |
| 126 | return |
Mike Frysinger | 6df594e | 2020-11-11 15:02:59 -0500 | [diff] [blame] | 127 | |
Mike Frysinger | a69df98 | 2023-03-21 16:52:27 -0400 | [diff] [blame] | 128 | target = constants.CHROMITE_DIR / "scripts" / "run_tests" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 129 | relpath = os.path.relpath(target, ".") |
| 130 | # If we're in the scripts dir, make sure we always have a relative path, |
| 131 | # otherwise cros_sdk will search $PATH and fail. |
| 132 | if os.path.sep not in relpath: |
| 133 | relpath = os.path.join(".", relpath) |
| 134 | cmd = [ |
| 135 | "cros_sdk", |
| 136 | "--working-dir", |
| 137 | ".", |
| 138 | "--", |
| 139 | relpath, |
| 140 | ] |
| 141 | os.execvp(cmd[0], cmd + argv) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 142 | |
| 143 | |
| 144 | def ensure_chroot_exists(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | """Ensure that a chroot exists for us to run tests in.""" |
| 146 | chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR) |
| 147 | if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot(): |
| 148 | cros_build_lib.run(["cros_sdk", "--create"]) |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 149 | |
| 150 | |
| 151 | def get_parser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 152 | """Build the parser for command line arguments.""" |
| 153 | parser = commandline.ArgumentParser( |
| 154 | description=__doc__, |
| 155 | epilog="To see the help output for pytest:\n$ %(prog)s -- --help", |
Mike Frysinger | 9abf4b5 | 2023-09-15 11:22:43 -0400 | [diff] [blame] | 156 | default_log_level="notice", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 157 | ) |
| 158 | parser.add_argument( |
Mike Frysinger | 55aa309 | 2023-02-15 09:43:22 -0500 | [diff] [blame] | 159 | "-j", |
| 160 | "--jobs", |
| 161 | type=int, |
| 162 | default=None, |
| 163 | help="Number of tests to run in parallel.", |
| 164 | ) |
| 165 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 166 | "--quickstart", |
| 167 | dest="quick", |
| 168 | action="store_true", |
Trent Apted | c20bb6d | 2023-05-10 15:00:03 +1000 | [diff] [blame] | 169 | help=( |
| 170 | "Skip normal test sandboxing and namespacing for faster start up " |
| 171 | "time." |
| 172 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 173 | ) |
| 174 | parser.add_argument( |
| 175 | "--network", |
| 176 | action="store_true", |
| 177 | help="Include network tests.", |
| 178 | ) |
| 179 | parser.add_argument( |
Mike Frysinger | 08b1012 | 2023-03-29 21:46:50 -0400 | [diff] [blame] | 180 | "--no-precache", |
| 181 | dest="precache", |
| 182 | action="store_false", |
| 183 | help="Skip precaching packages from the network.", |
| 184 | ) |
| 185 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 186 | "--no-chroot", |
| 187 | dest="chroot", |
| 188 | action="store_false", |
Trent Apted | c20bb6d | 2023-05-10 15:00:03 +1000 | [diff] [blame] | 189 | help=( |
| 190 | "Don't initialize or enter a chroot for the test invocation. May " |
| 191 | "cause tests to unexpectedly fail!" |
| 192 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 193 | ) |
| 194 | parser.add_argument( |
| 195 | "pytest_args", |
| 196 | metavar="pytest arguments", |
| 197 | nargs="*", |
| 198 | help="Arguments to pass down to pytest (use -- to help separate)", |
| 199 | ) |
| 200 | return parser |