mik-laj commented on a change in pull request #19737: URL: https://github.com/apache/airflow/pull/19737#discussion_r755032085
########## 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: The problem is also with the interpreter. This script is run using the system interpreter and `pytest` uses the virtual environment. This means a big difference in `sys.path`. You can probably change that, but that adds extra complexity to the code area where we don't have too many contributors to troubleshoot, so I prefer to take the safe path. -- 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]
