blob: f011d66834c6cde6bfb522936d1af0b651acd2c0 [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.
9"""
Chris McDonald17d86b32020-03-18 17:28:43 -060010
Chris McDonald3c557392020-03-31 13:41:46 -060011import os
Chris McDonald17d86b32020-03-18 17:28:43 -060012import sys
13
14import pytest # pylint: disable=import-error
15
Mike Frysingerd7af8462020-11-11 03:44:54 -050016from chromite.lib import commandline
Chris McDonald3c557392020-03-31 13:41:46 -060017from chromite.lib import constants
18from chromite.lib import cros_build_lib
19from chromite.lib import gs
Chris McDonald5d1af6a2020-04-21 08:00:15 -060020from chromite.lib import cros_logging as logging
Chris McDonald3c557392020-03-31 13:41:46 -060021from chromite.lib import namespaces
22
Chris McDonald17d86b32020-03-18 17:28:43 -060023
24def main(argv):
Chris McDonald5d1af6a2020-04-21 08:00:15 -060025 parser = get_parser()
Mike Frysingerd7af8462020-11-11 03:44:54 -050026 opts = parser.parse_args()
Mike Frysingera819cb32021-04-01 00:55:10 -040027 opts.Freeze()
Mike Frysingerd7af8462020-11-11 03:44:54 -050028
29 pytest_args = opts.pytest_args
30
Chris McDonald5d1af6a2020-04-21 08:00:15 -060031 if opts.quick:
Chris McDonald653510e2020-05-01 17:15:16 -060032 if not cros_build_lib.IsInsideChroot() and opts.chroot:
Mike Frysinger968c1142020-05-09 00:37:56 -040033 logging.warning('Tests start up faster when run from inside the chroot.')
Chris McDonald5d1af6a2020-04-21 08:00:15 -060034
Chris McDonald653510e2020-05-01 17:15:16 -060035 if opts.chroot:
36 ensure_chroot_exists()
37 re_execute_inside_chroot(argv)
38 else:
Chris McDonaldcdfd1132020-05-12 07:09:51 -060039 pytest_args += ['--no-chroot']
Chris McDonald3c557392020-03-31 13:41:46 -060040
Mike Frysingerc73c6b82021-04-22 00:07:54 -040041 if opts.network:
42 pytest_args += ['-m', 'not network_test or network_test']
43
Chris McDonald3c557392020-03-31 13:41:46 -060044 # This is a cheesy hack to make sure gsutil is populated in the cache before
45 # we run tests. This is a partial workaround for crbug.com/468838.
Mike Frysinger682b3332021-06-22 11:07:17 -040046 gs.GSContext.InitializeCache()
Chris McDonald3c557392020-03-31 13:41:46 -060047
Chris McDonald5d1af6a2020-04-21 08:00:15 -060048 if opts.quick:
49 logging.info('Skipping test namespacing due to --quickstart.')
50 # Default to running in a single process under --quickstart. User args can
51 # still override this.
52 pytest_args = ['-n', '0'] + pytest_args
53 else:
54 # Namespacing is enabled by default because tests may break each other or
55 # interfere with parts of the running system if not isolated in a namespace.
56 # Disabling namespaces is not recommended for general use.
Mike Frysingerc73c6b82021-04-22 00:07:54 -040057 re_execute_with_namespace([sys.argv[0]] + argv, network=opts.network)
Chris McDonald3c557392020-03-31 13:41:46 -060058
Mike Frysinger55919ef2021-04-01 00:55:39 -040059 # Check the environment. https://crbug.com/1015450
60 st = os.stat('/')
61 if st.st_mode & 0o7777 != 0o755:
62 cros_build_lib.Die(
63 f'The root directory has broken permissions: {st.st_mode:o}\n'
64 'Fix with: sudo chmod 755 /')
65 if st.st_uid or st.st_gid:
66 cros_build_lib.Die(
67 f'The root directory has broken ownership: {st.st_uid}:{st.st_gid}'
68 ' (should be 0:0)\nFix with: sudo chown 0:0 /')
69
Chris McDonald5d1af6a2020-04-21 08:00:15 -060070 sys.exit(pytest.main(pytest_args))
Chris McDonald3c557392020-03-31 13:41:46 -060071
72
73def re_execute_with_namespace(argv, network=False):
74 """Re-execute as root so we can unshare resources."""
75 if os.geteuid() != 0:
76 cmd = [
77 'sudo',
78 'HOME=%s' % os.environ['HOME'],
79 'PATH=%s' % os.environ['PATH'],
80 '--',
81 ] + argv
82 os.execvp(cmd[0], cmd)
83 else:
Chris McDonald3c557392020-03-31 13:41:46 -060084 namespaces.SimpleUnshare(net=not network, pid=True)
85 # We got our namespaces, so switch back to the user to run the tests.
86 gid = int(os.environ.pop('SUDO_GID'))
87 uid = int(os.environ.pop('SUDO_UID'))
88 user = os.environ.pop('SUDO_USER')
89 os.initgroups(user, gid)
90 os.setresgid(gid, gid, gid)
91 os.setresuid(uid, uid, uid)
92 os.environ['USER'] = user
93
94
95def re_execute_inside_chroot(argv):
96 """Re-execute the test wrapper inside the chroot."""
Mike Frysinger6df594e2020-11-11 15:02:59 -050097 if cros_build_lib.IsInsideChroot():
98 return
99
Mike Frysinger4975e5a2021-04-13 17:06:09 -0400100 target = os.path.join(constants.CHROMITE_DIR, 'scripts', 'run_tests')
Mike Frysinger6df594e2020-11-11 15:02:59 -0500101 relpath = os.path.relpath(target, '.')
102 # If we're in the scripts dir, make sure we always have a relative path,
103 # otherwise cros_sdk will search $PATH and fail.
104 if os.path.sep not in relpath:
105 relpath = os.path.join('.', relpath)
Chris McDonald3c557392020-03-31 13:41:46 -0600106 cmd = [
107 'cros_sdk',
Mike Frysinger6df594e2020-11-11 15:02:59 -0500108 '--working-dir', '.',
Chris McDonald3c557392020-03-31 13:41:46 -0600109 '--',
Mike Frysinger6df594e2020-11-11 15:02:59 -0500110 relpath,
Chris McDonald3c557392020-03-31 13:41:46 -0600111 ]
Mike Frysinger6df594e2020-11-11 15:02:59 -0500112 os.execvp(cmd[0], cmd + argv)
Chris McDonald3c557392020-03-31 13:41:46 -0600113
114
115def ensure_chroot_exists():
116 """Ensure that a chroot exists for us to run tests in."""
117 chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR)
118 if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot():
119 cros_build_lib.run(['cros_sdk', '--create'])
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600120
121
122def get_parser():
123 """Build the parser for command line arguments."""
Mike Frysingerd7af8462020-11-11 03:44:54 -0500124 parser = commandline.ArgumentParser(
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600125 description=__doc__,
Mike Frysingerd7af8462020-11-11 03:44:54 -0500126 epilog='To see the help output for pytest:\n$ %(prog)s -- --help',
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600127 )
128 parser.add_argument(
129 '--quickstart',
130 dest='quick',
131 action='store_true',
132 help='Skip normal test sandboxing and namespacing for faster start up '
133 'time.',
134 )
Chris McDonald653510e2020-05-01 17:15:16 -0600135 parser.add_argument(
Mike Frysingerc73c6b82021-04-22 00:07:54 -0400136 '--network',
137 action='store_true',
138 help='Include network tests.',
139 )
140 parser.add_argument(
Chris McDonald653510e2020-05-01 17:15:16 -0600141 '--no-chroot',
142 dest='chroot',
143 action='store_false',
144 help="Don't initialize or enter a chroot for the test invocation. May "
145 'cause tests to unexpectedly fail!',
146 )
Mike Frysingerd7af8462020-11-11 03:44:54 -0500147 parser.add_argument(
148 'pytest_args',
149 metavar='pytest arguments',
150 nargs='*',
151 help='Arguments to pass down to pytest (use -- to help separate)',
152 )
Chris McDonald5d1af6a2020-04-21 08:00:15 -0600153 return parser