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