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 2022/03/18 10:59:37 UTC

[GitHub] [airflow] kosteev commented on a change in pull request #22311: New design of system tests

kosteev commented on a change in pull request #22311:
URL: https://github.com/apache/airflow/pull/22311#discussion_r829863869



##########
File path: docs/apache-airflow-providers-google/operators/cloud/bigquery.rst
##########
@@ -29,7 +29,7 @@ data.
 Prerequisite Tasks
 ^^^^^^^^^^^^^^^^^^
 
-.. include::/operators/_partials/prerequisite_tasks.rst
+.. include:: ../_partials/prerequisite_tasks.rst

Review comment:
       Is space needed before ".."?

##########
File path: tests/always/test_example_dags.py
##########
@@ -29,33 +30,38 @@
 NO_DB_QUERY_EXCEPTION = ["/airflow/example_dags/example_subdag_operator.py"]
 
 
-class TestExampleDags(unittest.TestCase):
-    def test_should_be_importable(self):
-        example_dags = list(glob(f"{ROOT_FOLDER}/airflow/**/example_dags/example_*.py", recursive=True))
-        assert 0 != len(example_dags)
-        for filepath in example_dags:
-            relative_filepath = os.path.relpath(filepath, ROOT_FOLDER)
-            with self.subTest(f"File {relative_filepath} should contain dags"):
-                dagbag = DagBag(
-                    dag_folder=filepath,
-                    include_examples=False,
-                )
-                assert 0 == len(dagbag.import_errors), f"import_errors={str(dagbag.import_errors)}"
-                assert len(dagbag.dag_ids) >= 1
-
-    def test_should_not_do_database_queries(self):
-        example_dags = glob(f"{ROOT_FOLDER}/airflow/**/example_dags/example_*.py", recursive=True)
-        example_dags = [
-            dag_file
-            for dag_file in example_dags
-            if any(not dag_file.endswith(e) for e in NO_DB_QUERY_EXCEPTION)
-        ]
-        assert 0 != len(example_dags)
-        for filepath in example_dags:
-            relative_filepath = os.path.relpath(filepath, ROOT_FOLDER)
-            with self.subTest(f"File {relative_filepath} shouldn't do database queries"):
-                with assert_queries_count(0):
-                    DagBag(
-                        dag_folder=filepath,
-                        include_examples=False,
-                    )
+def example_dags():
+    example_dirs = ["airflow/**/example_dags/example_*.py", "tests/system/providers/**/example_*.py"]
+    for example_dir in example_dirs:
+        yield from glob(f"{ROOT_FOLDER}/{example_dir}", recursive=True)
+
+
+def example_dags_except_db_exception():
+    return [
+        dag_file
+        for dag_file in example_dags()
+        if any(not dag_file.endswith(e) for e in NO_DB_QUERY_EXCEPTION)
+    ]
+
+
+def relative_path(path):
+    return os.path.relpath(path, ROOT_FOLDER)
+
+
+@pytest.mark.parametrize("example", list(example_dags()), ids=relative_path)
+def test_should_be_importable(example):
+    dagbag = DagBag(
+        dag_folder=example,
+        include_examples=False,
+    )
+    assert 0 == len(dagbag.import_errors), f"import_errors={str(dagbag.import_errors)}"

Review comment:
       Nit: I am not sure about this, but I believe the general guide is to use expected value on the right side of assert statement.
   It is not different at all from the test status perspective, but may be more clear in the error message in case assert fails that `len(dagbag.import_errors)` actually has to be qual to zero.

##########
File path: scripts/ci/pre_commit/pre_commit_check_watcher_in_examples.py
##########
@@ -0,0 +1,86 @@
+#!/usr/bin/env python3
+# 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 sys
+from pathlib import Path
+from typing import List
+
+from rich.console import Console
+
+if __name__ not in ("__main__", "__mp_main__"):
+    raise SystemExit(
+        "This file is intended to be executed as an executable program. You cannot use it as a module."
+        f"To run this script, run the ./{__file__} command [FILE] ..."
+    )
+
+
+console = Console(color_system="standard", width=200)
+
+errors: List[str] = []
+
+WATCHER_APPEND_INSTRUCTION = "list(dag.tasks) >> watcher()"
+
+PYTEST_FUNCTION = """
+def test_run():
+    from airflow.utils.state import State
+
+    dag.clear(dag_run_state=State.NONE)
+    dag.run()
+"""
+
+
+def _check_file(file: Path):
+    content = file.read_text()
+    if "from tests.system.utils.watcher import watcher" in content:
+        index = content.find(WATCHER_APPEND_INSTRUCTION)
+        if index == -1:
+            errors.append(
+                f"[red]The example {file} imports tests.system.utils.watcher "
+                f"but does not use it properly![/]\n\n"
+                "[yellow]Make sure you have:[/]\n\n"
+                f"        {WATCHER_APPEND_INSTRUCTION}\n\n"
+                "[yellow]as last instruction in your example DAG.[/]\n"
+            )
+        else:
+            operator_leftshift_index = content.find("<<", index + len(WATCHER_APPEND_INSTRUCTION))
+            operator_rightshift_index = content.find(">>", index + len(WATCHER_APPEND_INSTRUCTION))
+            if operator_leftshift_index != -1 or operator_rightshift_index != -1:
+                errors.append(
+                    f"[red]In the  example {file} "
+                    f"watcher is not last instruction in your DAG (there are << "
+                    f"or >> operators after it)![/]\n\n"
+                    "[yellow]Make sure you have:[/]\n"
+                    f"        {WATCHER_APPEND_INSTRUCTION}\n\n"
+                    "[yellow]as last instruction in your example DAG.[/]\n"
+                )
+        if PYTEST_FUNCTION not in content:
+            errors.append(
+                f"[yellow]The  example {file} missed the pytest function at the end.[/]\n\n"
+                "All example tests should have this function added:\n\n" + PYTEST_FUNCTION + "\n\n"
+                "[yellow]Automatically adding it now!\n"
+            )
+            file.write_text(content + "\n" + PYTEST_FUNCTION)

Review comment:
       The name of the script does not completely correspond to what it does.
   Maybe this is not a big deal. Just pointing this out as it may confuse developer.

##########
File path: tests/always/test_example_dags.py
##########
@@ -29,33 +30,38 @@
 NO_DB_QUERY_EXCEPTION = ["/airflow/example_dags/example_subdag_operator.py"]
 
 
-class TestExampleDags(unittest.TestCase):
-    def test_should_be_importable(self):
-        example_dags = list(glob(f"{ROOT_FOLDER}/airflow/**/example_dags/example_*.py", recursive=True))
-        assert 0 != len(example_dags)
-        for filepath in example_dags:
-            relative_filepath = os.path.relpath(filepath, ROOT_FOLDER)
-            with self.subTest(f"File {relative_filepath} should contain dags"):
-                dagbag = DagBag(
-                    dag_folder=filepath,
-                    include_examples=False,
-                )
-                assert 0 == len(dagbag.import_errors), f"import_errors={str(dagbag.import_errors)}"
-                assert len(dagbag.dag_ids) >= 1
-
-    def test_should_not_do_database_queries(self):
-        example_dags = glob(f"{ROOT_FOLDER}/airflow/**/example_dags/example_*.py", recursive=True)
-        example_dags = [
-            dag_file
-            for dag_file in example_dags
-            if any(not dag_file.endswith(e) for e in NO_DB_QUERY_EXCEPTION)
-        ]
-        assert 0 != len(example_dags)
-        for filepath in example_dags:
-            relative_filepath = os.path.relpath(filepath, ROOT_FOLDER)
-            with self.subTest(f"File {relative_filepath} shouldn't do database queries"):
-                with assert_queries_count(0):
-                    DagBag(
-                        dag_folder=filepath,
-                        include_examples=False,
-                    )
+def example_dags():
+    example_dirs = ["airflow/**/example_dags/example_*.py", "tests/system/providers/**/example_*.py"]
+    for example_dir in example_dirs:
+        yield from glob(f"{ROOT_FOLDER}/{example_dir}", recursive=True)
+
+
+def example_dags_except_db_exception():
+    return [
+        dag_file
+        for dag_file in example_dags()
+        if any(not dag_file.endswith(e) for e in NO_DB_QUERY_EXCEPTION)
+    ]
+
+
+def relative_path(path):
+    return os.path.relpath(path, ROOT_FOLDER)
+
+
+@pytest.mark.parametrize("example", list(example_dags()), ids=relative_path)

Review comment:
       Nit: `list(example_dags())` converting to list is not necessary here as `parametrize` accepts iterable, right?

##########
File path: tests/system/conftest.py
##########
@@ -0,0 +1,61 @@
+# 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 os
+import re
+from itertools import chain
+from pathlib import Path
+
+import pytest
+
+OLD_EXECUTOR = os.environ.get("AIRFLOW__CORE__EXECUTOR", default=None)
+REQUIRED_ENV_VARS = ("SYSTEM_TESTS_ENV_ID",)
+
+
+def pytest_configure(config):
+    os.environ["AIRFLOW__CORE__EXECUTOR"] = "DebugExecutor"
+
+
+def pytest_unconfigure(config):
+    if OLD_EXECUTOR is not None:
+        os.environ["AIRFLOW__CORE__EXECUTOR"] = OLD_EXECUTOR

Review comment:
       Should unset "DebugExecutor" here also if the OLD_EXECUTOR is None?




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