blob: a045b641c2683a0873f9d92a82cff30ecf734528 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2020 The ChromiumOS Authors
Chris McDonald17d86b32020-03-18 17:28:43 -06002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Mike Frysinger4975e5a2021-04-13 17:06:09 -04005"""Chromite main test runner.
6
7Run the specified tests. If none are specified, we'll scan the
8tree looking for tests to run and then only run the semi-fast ones.
Mike Frysinger2aa424f2021-07-19 21:37:29 -04009
10https://docs.pytest.org/en/latest/how-to/usage.html#specifying-tests-selecting-tests
11
12Examples:
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 Frysinger4975e5a2021-04-13 17:06:09 -040021"""
Chris McDonald17d86b32020-03-18 17:28:43 -060022
Chris McDonald59650c32021-07-20 15:29:28 -060023import logging
Chris McDonald3c557392020-03-31 13:41:46 -060024import os
Chris McDonald17d86b32020-03-18 17:28:43 -060025import sys
26
27import pytest # pylint: disable=import-error
28
Alex Klein9ea00b52021-02-24 14:47:22 -070029from chromite.api import compile_build_api_proto
Mike Frysingerfbb1c142023-03-10 00:47:21 -050030from chromite.format import formatters
Mike Frysingerd7af8462020-11-11 03:44:54 -050031from chromite.lib import commandline
Chris McDonald3c557392020-03-31 13:41:46 -060032from chromite.lib import constants
33from chromite.lib import cros_build_lib
34from chromite.lib import gs
35from chromite.lib import namespaces
Mike Frysinger3763bf22023-03-30 12:38:28 -040036from chromite.lint import linters
Mike Frysingerfbb1c142023-03-10 00:47:21 -050037from chromite.scripts import clang_format
Chris McDonald3c557392020-03-31 13:41:46 -060038
Chris McDonald17d86b32020-03-18 17:28:43 -060039
40def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060041 parser = get_parser()
42 opts = parser.parse_args()
43 opts.Freeze()
Mike Frysingerd7af8462020-11-11 03:44:54 -050044
Alex Klein1699fab2022-09-08 08:46:06 -060045 pytest_args = opts.pytest_args
Mike Frysingerd7af8462020-11-11 03:44:54 -050046
Alex Klein1699fab2022-09-08 08:46:06 -060047 if opts.quick:
48 if not cros_build_lib.IsInsideChroot() and opts.chroot:
49 logging.warning(
50 "Tests start up faster when run from inside the chroot."
51 )
Chris McDonald5d1af6a2020-04-21 08:00:15 -060052
Alex Klein1699fab2022-09-08 08:46:06 -060053 if opts.chroot:
54 ensure_chroot_exists()
55 re_execute_inside_chroot(argv)
56 else:
57 pytest_args += ["--no-chroot"]
Chris McDonald3c557392020-03-31 13:41:46 -060058
Alex Klein1699fab2022-09-08 08:46:06 -060059 if opts.network:
60 pytest_args += ["-m", "not network_test or network_test"]
Mike Frysingerc73c6b82021-04-22 00:07:54 -040061
Mike Frysinger08b10122023-03-29 21:46:50 -040062 if opts.precache:
63 precache()
Chris McDonald3c557392020-03-31 13:41:46 -060064
Alex Klein1699fab2022-09-08 08:46:06 -060065 if opts.quick:
66 logging.info("Skipping test namespacing due to --quickstart.")
Alex Klein1699fab2022-09-08 08:46:06 -060067 else:
68 # Namespacing is enabled by default because tests may break each other or
69 # interfere with parts of the running system if not isolated in a namespace.
70 # Disabling namespaces is not recommended for general use.
71 namespaces.ReExecuteWithNamespace(
Mike Frysinger08b10122023-03-29 21:46:50 -040072 [sys.argv[0], "--no-precache"] + argv, network=opts.network
Alex Klein1699fab2022-09-08 08:46:06 -060073 )
Chris McDonald3c557392020-03-31 13:41:46 -060074
Mike Frysinger55aa3092023-02-15 09:43:22 -050075 jobs = opts.jobs
76 if jobs is None:
77 # Default to running in a single process under --quickstart. User args can
78 # still override this.
79 jobs = 0 if opts.quick else os.cpu_count()
80 pytest_args = ["-n", str(jobs)] + pytest_args
81
Alex Klein1699fab2022-09-08 08:46:06 -060082 # Check the environment. https://crbug.com/1015450
83 st = os.stat("/")
84 if st.st_mode & 0o007 != 0o005:
85 cros_build_lib.Die(
86 f"The root directory has broken permissions: {st.st_mode:o}\n"
87 "Fix with: sudo chmod o+rx-w /"
88 )
89 if st.st_uid or st.st_gid:
90 cros_build_lib.Die(
91 f"The root directory has broken ownership: {st.st_uid}:{st.st_gid}"
92 " (should be 0:0)\nFix with: sudo chown 0:0 /"
93 )
Mike Frysinger55919ef2021-04-01 00:55:39 -040094
Alex Klein1699fab2022-09-08 08:46:06 -060095 logging.debug("Running: pytest %s", cros_build_lib.CmdToStr(pytest_args))
96 sys.exit(pytest.main(pytest_args))
Chris McDonald3c557392020-03-31 13:41:46 -060097
98
Alex Klein9ea00b52021-02-24 14:47:22 -070099def precache():
Alex Klein1699fab2022-09-08 08:46:06 -0600100 """Do some network-dependent stuff before we disallow network access."""
Mike Frysingerfbb1c142023-03-10 00:47:21 -0500101 # pylint: disable=protected-access
Mike Frysinger0aa0d582023-03-24 12:55:01 -0400102 logging.notice("Caching tools from network (cipd/vpython/etc...)")
Mike Frysingerfbb1c142023-03-10 00:47:21 -0500103
Alex Klein1699fab2022-09-08 08:46:06 -0600104 # This is a cheesy hack to make sure gsutil is populated in the cache before
105 # we run tests. This is a partial workaround for crbug.com/468838.
106 gs.GSContext.InitializeCache()
107 # Ensure protoc is installed for api/compile_build_api_proto_unittest.
108 compile_build_api_proto.InstallProtoc(
109 compile_build_api_proto.ProtocVersion.CHROMITE
110 )
Mike Frysingerfbb1c142023-03-10 00:47:21 -0500111 # Ensure various tools are available.
Mike Frysinger29e1da92023-03-21 10:52:43 -0400112 cros_build_lib.dbg_run(
Mike Frysingera69df982023-03-21 16:52:27 -0400113 [constants.CHROMITE_DIR / "scripts" / "black", "--version"],
Mike Frysinger29e1da92023-03-21 10:52:43 -0400114 capture_output=True,
115 )
116 cros_build_lib.dbg_run(
Mike Frysingera69df982023-03-21 16:52:27 -0400117 [constants.CHROMITE_DIR / "scripts" / "isort", "--version"],
Mike Frysinger29e1da92023-03-21 10:52:43 -0400118 capture_output=True,
119 )
Mike Frysinger0aa0d582023-03-24 12:55:01 -0400120 formatters.gn._find_gn()
Mike Frysingerfbb1c142023-03-10 00:47:21 -0500121 formatters.star._find_buildifier()
122 formatters.textproto._find_txtpbfmt()
Mike Frysinger3763bf22023-03-30 12:38:28 -0400123 linters.shell._find_shellcheck()
Mike Frysingerfbb1c142023-03-10 00:47:21 -0500124 with clang_format.ClangFormat():
125 pass
Alex Klein9ea00b52021-02-24 14:47:22 -0700126
127
Chris McDonald3c557392020-03-31 13:41:46 -0600128def re_execute_inside_chroot(argv):
Alex Klein1699fab2022-09-08 08:46:06 -0600129 """Re-execute the test wrapper inside the chroot."""
130 if cros_build_lib.IsInsideChroot():
131 return
Mike Frysinger6df594e2020-11-11 15:02:59 -0500132
Mike Frysingera69df982023-03-21 16:52:27 -0400133 target = constants.CHROMITE_DIR / "scripts" / "run_tests"
Alex Klein1699fab2022-09-08 08:46:06 -0600134 relpath = os.path.relpath(target, ".")
135 # If we're in the scripts dir, make sure we always have a relative path,
136 # otherwise cros_sdk will search $PATH and fail.
137 if os.path.sep not in relpath:
138 relpath = os.path.join(".", relpath)
139 cmd = [
140 "cros_sdk",
141 "--working-dir",
142 ".",
143 "--",
144 relpath,
145 ]
146 os.execvp(cmd[0], cmd + argv)
Chris McDonald3c557392020-03-31 13:41:46 -0600147
148
149def ensure_chroot_exists():
Alex Klein1699fab2022-09-08 08:46:06 -0600150 """Ensure that a chroot exists for us to run tests in."""
151 chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR)
152 if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot():
153 cros_build_lib.run(["cros_sdk", "--create"])
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600154
155
156def get_parser():
Alex Klein1699fab2022-09-08 08:46:06 -0600157 """Build the parser for command line arguments."""
158 parser = commandline.ArgumentParser(
159 description=__doc__,
160 epilog="To see the help output for pytest:\n$ %(prog)s -- --help",
161 )
162 parser.add_argument(
Mike Frysinger55aa3092023-02-15 09:43:22 -0500163 "-j",
164 "--jobs",
165 type=int,
166 default=None,
167 help="Number of tests to run in parallel.",
168 )
169 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600170 "--quickstart",
171 dest="quick",
172 action="store_true",
Trent Aptedc20bb6d2023-05-10 15:00:03 +1000173 help=(
174 "Skip normal test sandboxing and namespacing for faster start up "
175 "time."
176 ),
Alex Klein1699fab2022-09-08 08:46:06 -0600177 )
178 parser.add_argument(
179 "--network",
180 action="store_true",
181 help="Include network tests.",
182 )
183 parser.add_argument(
Mike Frysinger08b10122023-03-29 21:46:50 -0400184 "--no-precache",
185 dest="precache",
186 action="store_false",
187 help="Skip precaching packages from the network.",
188 )
189 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600190 "--no-chroot",
191 dest="chroot",
192 action="store_false",
Trent Aptedc20bb6d2023-05-10 15:00:03 +1000193 help=(
194 "Don't initialize or enter a chroot for the test invocation. May "
195 "cause tests to unexpectedly fail!"
196 ),
Alex Klein1699fab2022-09-08 08:46:06 -0600197 )
198 parser.add_argument(
199 "pytest_args",
200 metavar="pytest arguments",
201 nargs="*",
202 help="Arguments to pass down to pytest (use -- to help separate)",
203 )
204 return parser