blob: 1d48652dc3e0df7e00c35c3452cfbd714610f923 [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):
Chris McDonald5d1af6a2020-04-21 08:00:15 -060038 parser = get_parser()
Mike Frysingerd7af8462020-11-11 03:44:54 -050039 opts = parser.parse_args()
Mike Frysingera819cb32021-04-01 00:55:10 -040040 opts.Freeze()
Mike Frysingerd7af8462020-11-11 03:44:54 -050041
42 pytest_args = opts.pytest_args
43
Chris McDonald5d1af6a2020-04-21 08:00:15 -060044 if opts.quick:
Chris McDonald653510e2020-05-01 17:15:16 -060045 if not cros_build_lib.IsInsideChroot() and opts.chroot:
Mike Frysinger968c1142020-05-09 00:37:56 -040046 logging.warning('Tests start up faster when run from inside the chroot.')
Chris McDonald5d1af6a2020-04-21 08:00:15 -060047
Chris McDonald653510e2020-05-01 17:15:16 -060048 if opts.chroot:
49 ensure_chroot_exists()
50 re_execute_inside_chroot(argv)
51 else:
Chris McDonaldcdfd1132020-05-12 07:09:51 -060052 pytest_args += ['--no-chroot']
Chris McDonald3c557392020-03-31 13:41:46 -060053
Mike Frysingerc73c6b82021-04-22 00:07:54 -040054 if opts.network:
55 pytest_args += ['-m', 'not network_test or network_test']
56
Alex Klein9ea00b52021-02-24 14:47:22 -070057 precache()
Chris McDonald3c557392020-03-31 13:41:46 -060058
Chris McDonald5d1af6a2020-04-21 08:00:15 -060059 if opts.quick:
60 logging.info('Skipping test namespacing due to --quickstart.')
61 # Default to running in a single process under --quickstart. User args can
62 # still override this.
63 pytest_args = ['-n', '0'] + pytest_args
64 else:
65 # Namespacing is enabled by default because tests may break each other or
66 # interfere with parts of the running system if not isolated in a namespace.
67 # Disabling namespaces is not recommended for general use.
Cindy Lind9765782022-08-10 02:57:51 +000068 namespaces.ReExecuteWithNamespace(
69 [sys.argv[0]] + argv, network=opts.network)
Chris McDonald3c557392020-03-31 13:41:46 -060070
Mike Frysinger55919ef2021-04-01 00:55:39 -040071 # Check the environment. https://crbug.com/1015450
72 st = os.stat('/')
Henrique Ferreiro41ba2992022-04-28 14:52:06 +020073 if st.st_mode & 0o007 != 0o005:
Mike Frysinger55919ef2021-04-01 00:55:39 -040074 cros_build_lib.Die(
75 f'The root directory has broken permissions: {st.st_mode:o}\n'
Henrique Ferreiro41ba2992022-04-28 14:52:06 +020076 'Fix with: sudo chmod o+rx-w /')
Mike Frysinger55919ef2021-04-01 00:55:39 -040077 if st.st_uid or st.st_gid:
78 cros_build_lib.Die(
79 f'The root directory has broken ownership: {st.st_uid}:{st.st_gid}'
80 ' (should be 0:0)\nFix with: sudo chown 0:0 /')
81
Mike Frysingerafcba312022-08-02 06:12:22 -040082 logging.debug('Running: pytest %s', cros_build_lib.CmdToStr(pytest_args))
Chris McDonald5d1af6a2020-04-21 08:00:15 -060083 sys.exit(pytest.main(pytest_args))
Chris McDonald3c557392020-03-31 13:41:46 -060084
85
Alex Klein9ea00b52021-02-24 14:47:22 -070086def precache():
87 """Do some network-dependent stuff before we disallow network access."""
88 # This is a cheesy hack to make sure gsutil is populated in the cache before
89 # we run tests. This is a partial workaround for crbug.com/468838.
90 gs.GSContext.InitializeCache()
91 # Ensure protoc is installed for api/compile_build_api_proto_unittest.
92 compile_build_api_proto.InstallProtoc(
93 compile_build_api_proto.ProtocVersion.CHROMITE)
94
95
Chris McDonald3c557392020-03-31 13:41:46 -060096def re_execute_inside_chroot(argv):
97 """Re-execute the test wrapper inside the chroot."""
Mike Frysinger6df594e2020-11-11 15:02:59 -050098 if cros_build_lib.IsInsideChroot():
99 return
100
Mike Frysinger4975e5a2021-04-13 17:06:09 -0400101 target = os.path.join(constants.CHROMITE_DIR, 'scripts', 'run_tests')
Mike Frysinger6df594e2020-11-11 15:02:59 -0500102 relpath = os.path.relpath(target, '.')
103 # If we're in the scripts dir, make sure we always have a relative path,
104 # otherwise cros_sdk will search $PATH and fail.
105 if os.path.sep not in relpath:
106 relpath = os.path.join('.', relpath)
Chris McDonald3c557392020-03-31 13:41:46 -0600107 cmd = [
108 'cros_sdk',
Mike Frysinger6df594e2020-11-11 15:02:59 -0500109 '--working-dir', '.',
Chris McDonald3c557392020-03-31 13:41:46 -0600110 '--',
Mike Frysinger6df594e2020-11-11 15:02:59 -0500111 relpath,
Chris McDonald3c557392020-03-31 13:41:46 -0600112 ]
Mike Frysinger6df594e2020-11-11 15:02:59 -0500113 os.execvp(cmd[0], cmd + argv)
Chris McDonald3c557392020-03-31 13:41:46 -0600114
115
116def ensure_chroot_exists():
117 """Ensure that a chroot exists for us to run tests in."""
118 chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR)
119 if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot():
120 cros_build_lib.run(['cros_sdk', '--create'])
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600121
122
123def get_parser():
124 """Build the parser for command line arguments."""
Mike Frysingerd7af8462020-11-11 03:44:54 -0500125 parser = commandline.ArgumentParser(
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600126 description=__doc__,
Mike Frysingerd7af8462020-11-11 03:44:54 -0500127 epilog='To see the help output for pytest:\n$ %(prog)s -- --help',
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600128 )
129 parser.add_argument(
130 '--quickstart',
131 dest='quick',
132 action='store_true',
133 help='Skip normal test sandboxing and namespacing for faster start up '
134 'time.',
135 )
Chris McDonald653510e2020-05-01 17:15:16 -0600136 parser.add_argument(
Mike Frysingerc73c6b82021-04-22 00:07:54 -0400137 '--network',
138 action='store_true',
139 help='Include network tests.',
140 )
141 parser.add_argument(
Chris McDonald653510e2020-05-01 17:15:16 -0600142 '--no-chroot',
143 dest='chroot',
144 action='store_false',
145 help="Don't initialize or enter a chroot for the test invocation. May "
146 'cause tests to unexpectedly fail!',
147 )
Mike Frysingerd7af8462020-11-11 03:44:54 -0500148 parser.add_argument(
149 'pytest_args',
150 metavar='pytest arguments',
151 nargs='*',
152 help='Arguments to pass down to pytest (use -- to help separate)',
153 )
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600154 return parser