You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2021/11/23 09:29:09 UTC

[GitHub] [airflow] uranusjr commented on a change in pull request #19737: Tests for Docker images in Python

uranusjr commented on a change in pull request #19737:
URL: https://github.com/apache/airflow/pull/19737#discussion_r754932760



##########
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:
       Since this script ends immediately after invoking pytest, maybe it can instead run pytest in-process?
   
   https://docs.pytest.org/en/latest/how-to/usage.html#calling-pytest-from-python-code

##########
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]

Review comment:
       This `3` is (too) magical. What does it do?

##########
File path: docker_tests/docker_tests_utils.py
##########
@@ -28,7 +29,7 @@
 
 
 def run_command(cmd: List[str], print_output_on_error: bool = True, **kwargs):
-    print(f"$ {' '.join(c for c in cmd)}")
+    print(f"$ {' '.join(shlex.quote(c) for c in cmd)}")

Review comment:
       There’s `shlex.join()` on 3.8+; do we need to run this on older versions?

##########
File path: scripts/ci/libraries/_verify_image.sh
##########
@@ -19,11 +19,11 @@
 function verify_image::verify_prod_image {
     DOCKER_IMAGE="${1}"
     export DOCKER_IMAGE
-    "${SCRIPTS_CI_DIR}/images/ci_run_docker_tests.sh" "${AIRFLOW_SOURCES}/docker_tests/prod_image.py"
+    "${SCRIPTS_CI_DIR}/images/ci_run_docker_tests.py" "${AIRFLOW_SOURCES}/docker_tests/prod_image.py"

Review comment:
       I feel it’s better to call `python` explicitly here instead of relying on the shebang.




-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org