Formatting: Format all python code with black.
This CL is probably not what you're looking for, it's only
automated formatting. Ignore it with
`git blame --ignore-rev <revision>` for this commit.
BUG=b:233893248
TEST=CQ
Change-Id: I66591d7a738d241aed3290138c0f68065ab10a6d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/3879174
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Tested-by: Alex Klein <saklein@chromium.org>
diff --git a/scripts/run_tests.py b/scripts/run_tests.py
index 1d48652..0c415a3 100644
--- a/scripts/run_tests.py
+++ b/scripts/run_tests.py
@@ -35,120 +35,127 @@
def main(argv):
- parser = get_parser()
- opts = parser.parse_args()
- opts.Freeze()
+ parser = get_parser()
+ opts = parser.parse_args()
+ opts.Freeze()
- pytest_args = opts.pytest_args
+ pytest_args = opts.pytest_args
- if opts.quick:
- if not cros_build_lib.IsInsideChroot() and opts.chroot:
- logging.warning('Tests start up faster when run from inside the chroot.')
+ if opts.quick:
+ if not cros_build_lib.IsInsideChroot() and opts.chroot:
+ logging.warning(
+ "Tests start up faster when run from inside the chroot."
+ )
- if opts.chroot:
- ensure_chroot_exists()
- re_execute_inside_chroot(argv)
- else:
- pytest_args += ['--no-chroot']
+ if opts.chroot:
+ ensure_chroot_exists()
+ re_execute_inside_chroot(argv)
+ else:
+ pytest_args += ["--no-chroot"]
- if opts.network:
- pytest_args += ['-m', 'not network_test or network_test']
+ if opts.network:
+ pytest_args += ["-m", "not network_test or network_test"]
- precache()
+ precache()
- if opts.quick:
- logging.info('Skipping test namespacing due to --quickstart.')
- # Default to running in a single process under --quickstart. User args can
- # still override this.
- pytest_args = ['-n', '0'] + pytest_args
- else:
- # Namespacing is enabled by default because tests may break each other or
- # interfere with parts of the running system if not isolated in a namespace.
- # Disabling namespaces is not recommended for general use.
- namespaces.ReExecuteWithNamespace(
- [sys.argv[0]] + argv, network=opts.network)
+ if opts.quick:
+ logging.info("Skipping test namespacing due to --quickstart.")
+ # Default to running in a single process under --quickstart. User args can
+ # still override this.
+ pytest_args = ["-n", "0"] + pytest_args
+ else:
+ # Namespacing is enabled by default because tests may break each other or
+ # interfere with parts of the running system if not isolated in a namespace.
+ # Disabling namespaces is not recommended for general use.
+ namespaces.ReExecuteWithNamespace(
+ [sys.argv[0]] + argv, network=opts.network
+ )
- # Check the environment. https://crbug.com/1015450
- st = os.stat('/')
- if st.st_mode & 0o007 != 0o005:
- cros_build_lib.Die(
- f'The root directory has broken permissions: {st.st_mode:o}\n'
- 'Fix with: sudo chmod o+rx-w /')
- if st.st_uid or st.st_gid:
- cros_build_lib.Die(
- f'The root directory has broken ownership: {st.st_uid}:{st.st_gid}'
- ' (should be 0:0)\nFix with: sudo chown 0:0 /')
+ # Check the environment. https://crbug.com/1015450
+ st = os.stat("/")
+ if st.st_mode & 0o007 != 0o005:
+ cros_build_lib.Die(
+ f"The root directory has broken permissions: {st.st_mode:o}\n"
+ "Fix with: sudo chmod o+rx-w /"
+ )
+ if st.st_uid or st.st_gid:
+ cros_build_lib.Die(
+ f"The root directory has broken ownership: {st.st_uid}:{st.st_gid}"
+ " (should be 0:0)\nFix with: sudo chown 0:0 /"
+ )
- logging.debug('Running: pytest %s', cros_build_lib.CmdToStr(pytest_args))
- sys.exit(pytest.main(pytest_args))
+ logging.debug("Running: pytest %s", cros_build_lib.CmdToStr(pytest_args))
+ sys.exit(pytest.main(pytest_args))
def precache():
- """Do some network-dependent stuff before we disallow network access."""
- # This is a cheesy hack to make sure gsutil is populated in the cache before
- # we run tests. This is a partial workaround for crbug.com/468838.
- gs.GSContext.InitializeCache()
- # Ensure protoc is installed for api/compile_build_api_proto_unittest.
- compile_build_api_proto.InstallProtoc(
- compile_build_api_proto.ProtocVersion.CHROMITE)
+ """Do some network-dependent stuff before we disallow network access."""
+ # This is a cheesy hack to make sure gsutil is populated in the cache before
+ # we run tests. This is a partial workaround for crbug.com/468838.
+ gs.GSContext.InitializeCache()
+ # Ensure protoc is installed for api/compile_build_api_proto_unittest.
+ compile_build_api_proto.InstallProtoc(
+ compile_build_api_proto.ProtocVersion.CHROMITE
+ )
def re_execute_inside_chroot(argv):
- """Re-execute the test wrapper inside the chroot."""
- if cros_build_lib.IsInsideChroot():
- return
+ """Re-execute the test wrapper inside the chroot."""
+ if cros_build_lib.IsInsideChroot():
+ return
- target = os.path.join(constants.CHROMITE_DIR, 'scripts', 'run_tests')
- relpath = os.path.relpath(target, '.')
- # If we're in the scripts dir, make sure we always have a relative path,
- # otherwise cros_sdk will search $PATH and fail.
- if os.path.sep not in relpath:
- relpath = os.path.join('.', relpath)
- cmd = [
- 'cros_sdk',
- '--working-dir', '.',
- '--',
- relpath,
- ]
- os.execvp(cmd[0], cmd + argv)
+ target = os.path.join(constants.CHROMITE_DIR, "scripts", "run_tests")
+ relpath = os.path.relpath(target, ".")
+ # If we're in the scripts dir, make sure we always have a relative path,
+ # otherwise cros_sdk will search $PATH and fail.
+ if os.path.sep not in relpath:
+ relpath = os.path.join(".", relpath)
+ cmd = [
+ "cros_sdk",
+ "--working-dir",
+ ".",
+ "--",
+ relpath,
+ ]
+ os.execvp(cmd[0], cmd + argv)
def ensure_chroot_exists():
- """Ensure that a chroot exists for us to run tests in."""
- chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR)
- if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot():
- cros_build_lib.run(['cros_sdk', '--create'])
+ """Ensure that a chroot exists for us to run tests in."""
+ chroot = os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR)
+ if not os.path.exists(chroot) and not cros_build_lib.IsInsideChroot():
+ cros_build_lib.run(["cros_sdk", "--create"])
def get_parser():
- """Build the parser for command line arguments."""
- parser = commandline.ArgumentParser(
- description=__doc__,
- epilog='To see the help output for pytest:\n$ %(prog)s -- --help',
- )
- parser.add_argument(
- '--quickstart',
- dest='quick',
- action='store_true',
- help='Skip normal test sandboxing and namespacing for faster start up '
- 'time.',
- )
- parser.add_argument(
- '--network',
- action='store_true',
- help='Include network tests.',
- )
- parser.add_argument(
- '--no-chroot',
- dest='chroot',
- action='store_false',
- help="Don't initialize or enter a chroot for the test invocation. May "
- 'cause tests to unexpectedly fail!',
- )
- parser.add_argument(
- 'pytest_args',
- metavar='pytest arguments',
- nargs='*',
- help='Arguments to pass down to pytest (use -- to help separate)',
- )
- return parser
+ """Build the parser for command line arguments."""
+ parser = commandline.ArgumentParser(
+ description=__doc__,
+ epilog="To see the help output for pytest:\n$ %(prog)s -- --help",
+ )
+ parser.add_argument(
+ "--quickstart",
+ dest="quick",
+ action="store_true",
+ help="Skip normal test sandboxing and namespacing for faster start up "
+ "time.",
+ )
+ parser.add_argument(
+ "--network",
+ action="store_true",
+ help="Include network tests.",
+ )
+ parser.add_argument(
+ "--no-chroot",
+ dest="chroot",
+ action="store_false",
+ help="Don't initialize or enter a chroot for the test invocation. May "
+ "cause tests to unexpectedly fail!",
+ )
+ parser.add_argument(
+ "pytest_args",
+ metavar="pytest arguments",
+ nargs="*",
+ help="Arguments to pass down to pytest (use -- to help separate)",
+ )
+ return parser