potiuk commented on a change in pull request #19737: URL: https://github.com/apache/airflow/pull/19737#discussion_r754957813
########## File path: scripts/ci/images/ci_run_docker_tests.py ########## @@ -0,0 +1,98 @@ +#!/usr/bin/env python +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import argparse +import shlex +import subprocess +import sys +from pathlib import Path +from typing import List + +AIRFLOW_SOURCE = Path(__file__).resolve().parents[3] +BUILD_CACHE_DIR = AIRFLOW_SOURCE / ".build" + + +def get_parser(): + parser = argparse.ArgumentParser( + prog="ci_run_docker_tests", + description="Running Docker tests using pytest", + epilog="Unknown arguments are passed unchanged to Pytest.", + ) + parser.add_argument( + "--interactive", + "-i", + action='store_true', + help="Activates virtual environment ready to run tests and drops you in", + ) + parser.add_argument("--initialize", action="store_true", help="Initialize virtual environment and exit") + parser.add_argument("pytestopts", nargs=argparse.REMAINDER, help="Tests to run") + return parser + + +def run_verbose(cmd: List[str], **kwargs): + print(f"$ {' '.join(shlex.quote(c) for c in cmd)}") + subprocess.run(cmd, **kwargs) + + +def create_virtualenv(): + virtualenv_path = ( + BUILD_CACHE_DIR / ".docker_venv" / f"host_python_{sys.version_info[0]}.{sys.version_info[1]}" + ) + virtualenv_path.parent.mkdir(parents=True, exist_ok=True) + if not virtualenv_path.exists(): + print("Creating virtualenv environment") + run_verbose([sys.executable, "-m", "venv", str(virtualenv_path)]) + + python_bin = virtualenv_path / "bin" / "python" + run_verbose([str(python_bin), "-m", "pip", "install", "pytest", "pytest-xdist"]) + return python_bin + + +def main(): + parser = get_parser() + args = parser.parse_args() + + python_bin = create_virtualenv() + + if args.initialize: + return + if args.interactive: + activate_bin = python_bin.parent / "activate" + bash_trampoline = f"source {shlex.quote(str(activate_bin))}" + print("To enter virtual environment, run:") + print(f" {bash_trampoline}") + return + + extra_pytest_args = ( + args.pytestopts[1:] if args.pytestopts and args.pytestopts[0] == "--" else args.pytestopts + ) + if not extra_pytest_args: + raise SystemExit("You must select the tests to run.") + + pytest_args = ( + "--pythonwarnings=ignore::DeprecationWarning", + "--pythonwarnings=ignore::PendingDeprecationWarning", + "-n", + "auto", + ) + + run_verbose([str(python_bin), "-m", "pytest", *pytest_args, *extra_pytest_args]) Review comment: Context why running command is better: One thing that is actually useful here is the abiliity for the user to be able to repeat what happens manually - not necessarily via the python script (and we are going to use similar approach in the future `breeze` python version for other stuff. The script is there to: a) run on CI (replacing bash script) b) give an easy way to the `casual` users or users who will be using codespaces in the future to create the venv and run tests seamlessly For most of the "power" users, they will have their own venv with `pytest` and `pytes-xdist` installed and they will run the tests via IDE/commandline whatever. So the `run verbose` command is good idea in this context because it will shows the exact command that is being run. For example when tnis script fails in the CI, anyone will be able to see the exact `pytest` command that was run in the CI that caused the problem. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
