Chris McDonald | 17d86b3 | 2020-03-18 17:28:43 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Wrapper to execute pytest inside the chromite virtualenv.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 10 | import argparse |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 11 | import os |
Chris McDonald | 17d86b3 | 2020-03-18 17:28:43 -0600 | [diff] [blame] | 12 | import sys |
| 13 | |
| 14 | import pytest # pylint: disable=import-error |
| 15 | |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 16 | from chromite.lib import cgroups |
| 17 | from chromite.lib import constants |
| 18 | from chromite.lib import cros_build_lib |
| 19 | from chromite.lib import gs |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 20 | from chromite.lib import cros_logging as logging |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 21 | from chromite.lib import namespaces |
| 22 | |
Chris McDonald | 17d86b3 | 2020-03-18 17:28:43 -0600 | [diff] [blame] | 23 | |
| 24 | def main(argv): |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 25 | parser = get_parser() |
| 26 | opts, pytest_args = parser.parse_known_args() |
| 27 | if opts.quick: |
Chris McDonald | 653510e | 2020-05-01 17:15:16 -0600 | [diff] [blame] | 28 | if not cros_build_lib.IsInsideChroot() and opts.chroot: |
Mike Frysinger | 968c114 | 2020-05-09 00:37:56 -0400 | [diff] [blame] | 29 | logging.warning('Tests start up faster when run from inside the chroot.') |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 30 | |
Chris McDonald | 653510e | 2020-05-01 17:15:16 -0600 | [diff] [blame] | 31 | if opts.chroot: |
| 32 | ensure_chroot_exists() |
| 33 | re_execute_inside_chroot(argv) |
| 34 | else: |
| 35 | os.chdir(constants.CHROMITE_DIR) |
Chris McDonald | cdfd113 | 2020-05-12 07:09:51 -0600 | [diff] [blame^] | 36 | pytest_args += ['--no-chroot'] |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 37 | |
| 38 | # This is a cheesy hack to make sure gsutil is populated in the cache before |
| 39 | # we run tests. This is a partial workaround for crbug.com/468838. |
| 40 | gs.GSContext.GetDefaultGSUtilBin() |
| 41 | |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 42 | if opts.quick: |
| 43 | logging.info('Skipping test namespacing due to --quickstart.') |
| 44 | # Default to running in a single process under --quickstart. User args can |
| 45 | # still override this. |
| 46 | pytest_args = ['-n', '0'] + pytest_args |
| 47 | else: |
| 48 | # Namespacing is enabled by default because tests may break each other or |
| 49 | # interfere with parts of the running system if not isolated in a namespace. |
| 50 | # Disabling namespaces is not recommended for general use. |
| 51 | re_execute_with_namespace([sys.argv[0]] + argv) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 52 | |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 53 | sys.exit(pytest.main(pytest_args)) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def re_execute_with_namespace(argv, network=False): |
| 57 | """Re-execute as root so we can unshare resources.""" |
| 58 | if os.geteuid() != 0: |
| 59 | cmd = [ |
| 60 | 'sudo', |
| 61 | 'HOME=%s' % os.environ['HOME'], |
| 62 | 'PATH=%s' % os.environ['PATH'], |
| 63 | '--', |
| 64 | ] + argv |
| 65 | os.execvp(cmd[0], cmd) |
| 66 | else: |
| 67 | cgroups.Cgroup.InitSystem() |
| 68 | namespaces.SimpleUnshare(net=not network, pid=True) |
| 69 | # We got our namespaces, so switch back to the user to run the tests. |
| 70 | gid = int(os.environ.pop('SUDO_GID')) |
| 71 | uid = int(os.environ.pop('SUDO_UID')) |
| 72 | user = os.environ.pop('SUDO_USER') |
| 73 | os.initgroups(user, gid) |
| 74 | os.setresgid(gid, gid, gid) |
| 75 | os.setresuid(uid, uid, uid) |
| 76 | os.environ['USER'] = user |
| 77 | |
| 78 | |
| 79 | def re_execute_inside_chroot(argv): |
| 80 | """Re-execute the test wrapper inside the chroot.""" |
| 81 | cmd = [ |
| 82 | 'cros_sdk', |
| 83 | '--', |
| 84 | os.path.join('..', '..', 'chromite', 'run_pytest'), |
| 85 | ] |
| 86 | if not cros_build_lib.IsInsideChroot(): |
| 87 | os.execvp(cmd[0], cmd + argv) |
| 88 | else: |
| 89 | os.chdir(constants.CHROMITE_DIR) |
| 90 | |
| 91 | |
| 92 | def ensure_chroot_exists(): |
| 93 | """Ensure that a chroot exists for us to run tests in.""" |
| 94 | chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR) |
| 95 | if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot(): |
| 96 | cros_build_lib.run(['cros_sdk', '--create']) |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 97 | |
| 98 | |
| 99 | def get_parser(): |
| 100 | """Build the parser for command line arguments.""" |
| 101 | parser = argparse.ArgumentParser( |
| 102 | description=__doc__, |
| 103 | epilog='To see the help output for pytest, run `pytest --help` inside ' |
| 104 | 'the chroot.', |
| 105 | ) |
| 106 | parser.add_argument( |
| 107 | '--quickstart', |
| 108 | dest='quick', |
| 109 | action='store_true', |
| 110 | help='Skip normal test sandboxing and namespacing for faster start up ' |
| 111 | 'time.', |
| 112 | ) |
Chris McDonald | 653510e | 2020-05-01 17:15:16 -0600 | [diff] [blame] | 113 | parser.add_argument( |
| 114 | '--no-chroot', |
| 115 | dest='chroot', |
| 116 | action='store_false', |
| 117 | help="Don't initialize or enter a chroot for the test invocation. May " |
| 118 | 'cause tests to unexpectedly fail!', |
| 119 | ) |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 120 | return parser |