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: |
| 28 | if not cros_build_lib.IsInsideChroot(): |
| 29 | logging.warn('Running tests from inside the chroot will start up faster.') |
| 30 | |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 31 | ensure_chroot_exists() |
| 32 | re_execute_inside_chroot(argv) |
| 33 | |
| 34 | # This is a cheesy hack to make sure gsutil is populated in the cache before |
| 35 | # we run tests. This is a partial workaround for crbug.com/468838. |
| 36 | gs.GSContext.GetDefaultGSUtilBin() |
| 37 | |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 38 | if opts.quick: |
| 39 | logging.info('Skipping test namespacing due to --quickstart.') |
| 40 | # Default to running in a single process under --quickstart. User args can |
| 41 | # still override this. |
| 42 | pytest_args = ['-n', '0'] + pytest_args |
| 43 | else: |
| 44 | # Namespacing is enabled by default because tests may break each other or |
| 45 | # interfere with parts of the running system if not isolated in a namespace. |
| 46 | # Disabling namespaces is not recommended for general use. |
| 47 | re_execute_with_namespace([sys.argv[0]] + argv) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 48 | |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 49 | sys.exit(pytest.main(pytest_args)) |
Chris McDonald | 3c55739 | 2020-03-31 13:41:46 -0600 | [diff] [blame] | 50 | |
| 51 | |
| 52 | def re_execute_with_namespace(argv, network=False): |
| 53 | """Re-execute as root so we can unshare resources.""" |
| 54 | if os.geteuid() != 0: |
| 55 | cmd = [ |
| 56 | 'sudo', |
| 57 | 'HOME=%s' % os.environ['HOME'], |
| 58 | 'PATH=%s' % os.environ['PATH'], |
| 59 | '--', |
| 60 | ] + argv |
| 61 | os.execvp(cmd[0], cmd) |
| 62 | else: |
| 63 | cgroups.Cgroup.InitSystem() |
| 64 | namespaces.SimpleUnshare(net=not network, pid=True) |
| 65 | # We got our namespaces, so switch back to the user to run the tests. |
| 66 | gid = int(os.environ.pop('SUDO_GID')) |
| 67 | uid = int(os.environ.pop('SUDO_UID')) |
| 68 | user = os.environ.pop('SUDO_USER') |
| 69 | os.initgroups(user, gid) |
| 70 | os.setresgid(gid, gid, gid) |
| 71 | os.setresuid(uid, uid, uid) |
| 72 | os.environ['USER'] = user |
| 73 | |
| 74 | |
| 75 | def re_execute_inside_chroot(argv): |
| 76 | """Re-execute the test wrapper inside the chroot.""" |
| 77 | cmd = [ |
| 78 | 'cros_sdk', |
| 79 | '--', |
| 80 | os.path.join('..', '..', 'chromite', 'run_pytest'), |
| 81 | ] |
| 82 | if not cros_build_lib.IsInsideChroot(): |
| 83 | os.execvp(cmd[0], cmd + argv) |
| 84 | else: |
| 85 | os.chdir(constants.CHROMITE_DIR) |
| 86 | |
| 87 | |
| 88 | def ensure_chroot_exists(): |
| 89 | """Ensure that a chroot exists for us to run tests in.""" |
| 90 | chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR) |
| 91 | if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot(): |
| 92 | cros_build_lib.run(['cros_sdk', '--create']) |
Chris McDonald | 5d1af6a | 2020-04-21 08:00:15 -0600 | [diff] [blame] | 93 | |
| 94 | |
| 95 | def get_parser(): |
| 96 | """Build the parser for command line arguments.""" |
| 97 | parser = argparse.ArgumentParser( |
| 98 | description=__doc__, |
| 99 | epilog='To see the help output for pytest, run `pytest --help` inside ' |
| 100 | 'the chroot.', |
| 101 | ) |
| 102 | parser.add_argument( |
| 103 | '--quickstart', |
| 104 | dest='quick', |
| 105 | action='store_true', |
| 106 | help='Skip normal test sandboxing and namespacing for faster start up ' |
| 107 | 'time.', |
| 108 | ) |
| 109 | return parser |