-
Notifications
You must be signed in to change notification settings - Fork 14.5k
/
test_example_dags.py
200 lines (171 loc) · 9.31 KB
/
test_example_dags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# 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.
from __future__ import annotations
import os
import sys
from glob import glob
from importlib import metadata as importlib_metadata
from pathlib import Path
import pytest
from packaging.specifiers import SpecifierSet
from packaging.version import Version
from airflow.models import DagBag
from airflow.utils import yaml
from tests.test_utils.asserts import assert_queries_count
AIRFLOW_SOURCES_ROOT = Path(__file__).resolve().parents[2]
AIRFLOW_PROVIDERS_ROOT = AIRFLOW_SOURCES_ROOT / "airflow" / "providers"
CURRENT_PYTHON_VERSION = f"{sys.version_info.major}.{sys.version_info.minor}"
NO_DB_QUERY_EXCEPTION = ("/airflow/example_dags/example_subdag_operator.py",)
PROVIDERS_PREFIXES = ("airflow/providers/", "tests/system/providers/")
OPTIONAL_PROVIDERS_DEPENDENCIES: dict[str, dict[str, str | None]] = {
# Some examples or system tests may depend on additional packages
# that are not included in certain CI checks.
# The format of the dictionary is as follows:
# key: the prefix of the file to be excluded,
# value: a dictionary containing package distributions with an optional version specifier, e.g., >=2.3.4
}
IGNORE_AIRFLOW_PROVIDER_DEPRECATION_WARNING: tuple[str, ...] = (
# Certain examples or system tests may trigger AirflowProviderDeprecationWarnings.
# Generally, these should be resolved as soon as a parameter or operator is deprecated.
# If the deprecation is postponed, the item should be added to this tuple,
# and a corresponding Issue should be created on GitHub.
"tests/system/providers/google/cloud/bigquery/example_bigquery_operations.py",
"tests/system/providers/google/cloud/dataproc/example_dataproc_gke.py",
"tests/system/providers/google/cloud/datapipelines/example_datapipeline.py",
"tests/system/providers/google/cloud/gcs/example_gcs_sensor.py",
"tests/system/providers/google/cloud/kubernetes_engine/example_kubernetes_engine.py",
"tests/system/providers/google/cloud/kubernetes_engine/example_kubernetes_engine_async.py",
"tests/system/providers/google/cloud/kubernetes_engine/example_kubernetes_engine_job.py",
"tests/system/providers/google/cloud/kubernetes_engine/example_kubernetes_engine_kueue.py",
"tests/system/providers/google/cloud/kubernetes_engine/example_kubernetes_engine_resource.py",
"tests/system/providers/google/cloud/life_sciences/example_life_sciences.py",
"tests/system/providers/google/marketing_platform/example_analytics.py",
# Deprecated Operators/Hooks, which replaced by common.sql Operators/Hooks
)
if os.environ.get("PYDANTIC", "v2") != "v2":
pytest.skip(
"The test is skipped because we are running in limited Pydantic environment", allow_module_level=True
)
def match_optional_dependencies(distribution_name: str, specifier: str | None) -> tuple[bool, str]:
try:
package_version = Version(importlib_metadata.version(distribution_name))
except ImportError:
return False, f"{distribution_name!r} not installed."
if specifier and package_version not in SpecifierSet(specifier):
return False, f"{distribution_name!r} required {specifier}, but installed {package_version}."
return True, ""
def get_suspended_providers_folders() -> list[str]:
"""
Returns a list of suspended providers folders that should be
skipped when running tests (without any prefix - for example apache/beam, yandex, google etc.).
"""
suspended_providers = []
for provider_path in AIRFLOW_PROVIDERS_ROOT.rglob("provider.yaml"):
provider_yaml = yaml.safe_load(provider_path.read_text())
if provider_yaml["state"] == "suspended":
suspended_providers.append(
provider_path.parent.relative_to(AIRFLOW_SOURCES_ROOT)
.as_posix()
.replace("airflow/providers/", "")
)
return suspended_providers
def get_python_excluded_providers_folders() -> list[str]:
"""
Returns a list of providers folders that should be excluded for current Python version and
skipped when running tests (without any prefix - for example apache/beam, yandex, google etc.).
"""
excluded_providers = []
for provider_path in AIRFLOW_PROVIDERS_ROOT.rglob("provider.yaml"):
provider_yaml = yaml.safe_load(provider_path.read_text())
excluded_python_versions = provider_yaml.get("excluded-python-versions", [])
if CURRENT_PYTHON_VERSION in excluded_python_versions:
excluded_providers.append(
provider_path.parent.relative_to(AIRFLOW_SOURCES_ROOT)
.as_posix()
.replace("airflow/providers/", "")
)
return excluded_providers
def example_not_excluded_dags(xfail_db_exception: bool = False):
example_dirs = ["airflow/**/example_dags/example_*.py", "tests/system/**/example_*.py"]
suspended_providers_folders = get_suspended_providers_folders()
current_python_excluded_providers_folders = get_python_excluded_providers_folders()
suspended_providers_folders = [
AIRFLOW_SOURCES_ROOT.joinpath(prefix, provider).as_posix()
for prefix in PROVIDERS_PREFIXES
for provider in suspended_providers_folders
]
current_python_excluded_providers_folders = [
AIRFLOW_SOURCES_ROOT.joinpath(prefix, provider).as_posix()
for prefix in PROVIDERS_PREFIXES
for provider in current_python_excluded_providers_folders
]
providers_folders = tuple([AIRFLOW_SOURCES_ROOT.joinpath(pp).as_posix() for pp in PROVIDERS_PREFIXES])
for example_dir in example_dirs:
candidates = glob(f"{AIRFLOW_SOURCES_ROOT.as_posix()}/{example_dir}", recursive=True)
for candidate in sorted(candidates):
param_marks = []
if candidate.startswith(tuple(suspended_providers_folders)):
param_marks.append(pytest.mark.skip(reason="Suspended provider"))
if candidate.startswith(tuple(current_python_excluded_providers_folders)):
param_marks.append(
pytest.mark.skip(reason=f"Not supported for Python {CURRENT_PYTHON_VERSION}")
)
for optional, dependencies in OPTIONAL_PROVIDERS_DEPENDENCIES.items():
if candidate.endswith(optional):
for distribution_name, specifier in dependencies.items():
result, reason = match_optional_dependencies(distribution_name, specifier)
if not result:
param_marks.append(pytest.mark.skip(reason=reason))
if xfail_db_exception and candidate.endswith(NO_DB_QUERY_EXCEPTION):
# Use strict XFAIL for excluded tests. So if it is not failed, we should remove from the list.
param_marks.append(pytest.mark.xfail(reason="Expected DB call", strict=True))
if candidate.startswith(providers_folders):
# Do not raise an error for airflow.exceptions.RemovedInAirflow3Warning.
# We should not rush to enforce new syntax updates in providers
# because a version of Airflow that deprecates certain features may not yet be released.
# Instead, it is advisable to periodically review the warning reports and implement manual
# updates as needed.
param_marks.append(
pytest.mark.filterwarnings("default::airflow.exceptions.RemovedInAirflow3Warning")
)
if candidate.endswith(IGNORE_AIRFLOW_PROVIDER_DEPRECATION_WARNING):
param_marks.append(
pytest.mark.filterwarnings(
"default::airflow.exceptions.AirflowProviderDeprecationWarning"
)
)
yield pytest.param(candidate, marks=tuple(param_marks), id=relative_path(candidate))
def relative_path(path):
return os.path.relpath(path, AIRFLOW_SOURCES_ROOT.as_posix())
@pytest.mark.db_test
@pytest.mark.parametrize("example", example_not_excluded_dags())
def test_should_be_importable(example: str):
dagbag = DagBag(
dag_folder=example,
include_examples=False,
)
assert len(dagbag.import_errors) == 0, f"import_errors={str(dagbag.import_errors)}"
assert len(dagbag.dag_ids) >= 1
@pytest.mark.skip_if_database_isolation_mode
@pytest.mark.db_test
@pytest.mark.parametrize("example", example_not_excluded_dags(xfail_db_exception=True))
def test_should_not_do_database_queries(example: str):
with assert_queries_count(1, stacklevel_from_module=example.rsplit(os.sep, 1)[-1]):
DagBag(
dag_folder=example,
include_examples=False,
)