blob: 0c415a3078c40bc466e47887a79b33094d13e0c4 [file] [log] [blame]
Chris McDonald17d86b32020-03-18 17:28:43 -06001# 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 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 Frysingerd7af8462020-11-11 03:44:54 -050030from chromite.lib import commandline
Chris McDonald3c557392020-03-31 13:41:46 -060031from chromite.lib import constants
32from chromite.lib import cros_build_lib
33from chromite.lib import gs
34from chromite.lib import namespaces
35
Chris McDonald17d86b32020-03-18 17:28:43 -060036
37def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060038 parser = get_parser()
39 opts = parser.parse_args()
40 opts.Freeze()
Mike Frysingerd7af8462020-11-11 03:44:54 -050041
Alex Klein1699fab2022-09-08 08:46:06 -060042 pytest_args = opts.pytest_args
Mike Frysingerd7af8462020-11-11 03:44:54 -050043
Alex Klein1699fab2022-09-08 08:46:06 -060044 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 McDonald5d1af6a2020-04-21 08:00:15 -060049
Alex Klein1699fab2022-09-08 08:46:06 -060050 if opts.chroot:
51 ensure_chroot_exists()
52 re_execute_inside_chroot(argv)
53 else:
54 pytest_args += ["--no-chroot"]
Chris McDonald3c557392020-03-31 13:41:46 -060055
Alex Klein1699fab2022-09-08 08:46:06 -060056 if opts.network:
57 pytest_args += ["-m", "not network_test or network_test"]
Mike Frysingerc73c6b82021-04-22 00:07:54 -040058
Alex Klein1699fab2022-09-08 08:46:06 -060059 precache()
Chris McDonald3c557392020-03-31 13:41:46 -060060
Alex Klein1699fab2022-09-08 08:46:06 -060061 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 McDonald3c557392020-03-31 13:41:46 -060073
Alex Klein1699fab2022-09-08 08:46:06 -060074 # 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 Frysinger55919ef2021-04-01 00:55:39 -040086
Alex Klein1699fab2022-09-08 08:46:06 -060087 logging.debug("Running: pytest %s", cros_build_lib.CmdToStr(pytest_args))
88 sys.exit(pytest.main(pytest_args))
Chris McDonald3c557392020-03-31 13:41:46 -060089
90
Alex Klein9ea00b52021-02-24 14:47:22 -070091def precache():
Alex Klein1699fab2022-09-08 08:46:06 -060092 """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 Klein9ea00b52021-02-24 14:47:22 -0700100
101
Chris McDonald3c557392020-03-31 13:41:46 -0600102def re_execute_inside_chroot(argv):
Alex Klein1699fab2022-09-08 08:46:06 -0600103 """Re-execute the test wrapper inside the chroot."""
104 if cros_build_lib.IsInsideChroot():
105 return
Mike Frysinger6df594e2020-11-11 15:02:59 -0500106
Alex Klein1699fab2022-09-08 08:46:06 -0600107 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 McDonald3c557392020-03-31 13:41:46 -0600121
122
123def ensure_chroot_exists():
Alex Klein1699fab2022-09-08 08:46:06 -0600124 """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 McDonald5d1af6a2020-04-21 08:00:15 -0600128
129
130def get_parser():
Alex Klein1699fab2022-09-08 08:46:06 -0600131 """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