-
Notifications
You must be signed in to change notification settings - Fork 14.4k
/
run_tests.py
412 lines (378 loc) · 16 KB
/
run_tests.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# 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 re
import sys
from itertools import chain
from subprocess import DEVNULL
from airflow_breeze.global_constants import (
ALL_TEST_SUITES,
ALL_TEST_TYPE,
NONE_TEST_TYPE,
PIP_VERSION,
UV_VERSION,
GroupOfTests,
SelectiveCoreTestType,
all_helm_test_packages,
)
from airflow_breeze.utils.console import Output, get_console
from airflow_breeze.utils.packages import get_excluded_provider_folders, get_suspended_provider_folders
from airflow_breeze.utils.path_utils import AIRFLOW_SOURCES_ROOT, TESTS_PROVIDERS_ROOT
from airflow_breeze.utils.run_utils import run_command
from airflow_breeze.utils.virtualenv_utils import create_temp_venv
DOCKER_TESTS_ROOT = AIRFLOW_SOURCES_ROOT / "docker_tests"
DOCKER_TESTS_REQUIREMENTS = DOCKER_TESTS_ROOT / "requirements.txt"
def verify_an_image(
image_name: str,
image_type: str,
output: Output | None,
slim_image: bool,
extra_pytest_args: tuple[str, ...],
) -> tuple[int, str]:
command_result = run_command(
["docker", "inspect", image_name],
check=False,
output=output,
)
if command_result.returncode != 0:
get_console(output=output).print(
f"[error]Error when inspecting {image_type} image: {command_result.returncode}[/]"
)
return command_result.returncode, f"Testing {image_type} python {image_name}"
pytest_args = ("-n", str(os.cpu_count()), "--color=yes")
if image_type == "PROD":
test_path = DOCKER_TESTS_ROOT / "test_prod_image.py"
else:
test_path = DOCKER_TESTS_ROOT / "test_ci_image.py"
env = os.environ.copy()
env["DOCKER_IMAGE"] = image_name
if slim_image:
env["TEST_SLIM_IMAGE"] = "true"
with create_temp_venv(
pip_version=PIP_VERSION, uv_version=UV_VERSION, requirements_file=DOCKER_TESTS_REQUIREMENTS
) as py_exe:
command_result = run_command(
[py_exe, "-m", "pytest", str(test_path), *pytest_args, *extra_pytest_args],
env=env,
output=output,
check=False,
)
return command_result.returncode, f"Testing {image_type} python {image_name}"
def run_docker_compose_tests(
image_name: str,
extra_pytest_args: tuple,
skip_docker_compose_deletion: bool,
) -> tuple[int, str]:
command_result = run_command(["docker", "inspect", image_name], check=False, stdout=DEVNULL)
if command_result.returncode != 0:
get_console().print(f"[error]Error when inspecting PROD image: {command_result.returncode}[/]")
return command_result.returncode, f"Testing docker-compose python with {image_name}"
pytest_args = ("--color=yes",)
test_path = DOCKER_TESTS_ROOT / "test_docker_compose_quick_start.py"
env = os.environ.copy()
env["DOCKER_IMAGE"] = image_name
if skip_docker_compose_deletion:
env["SKIP_DOCKER_COMPOSE_DELETION"] = "true"
with create_temp_venv(pip_version=PIP_VERSION, requirements_file=DOCKER_TESTS_REQUIREMENTS) as py_exe:
command_result = run_command(
[py_exe, "-m", "pytest", str(test_path), *pytest_args, *extra_pytest_args],
env=env,
check=False,
)
return command_result.returncode, f"Testing docker-compose python with {image_name}"
def file_name_from_test_type(test_type: str):
test_type_no_brackets = test_type.lower().replace("[", "_").replace("]", "")
return re.sub("[,.]", "_", test_type_no_brackets)[:30]
def test_paths(test_type: str, backend: str) -> tuple[str, str, str]:
file_friendly_test_type = file_name_from_test_type(test_type)
random_suffix = os.urandom(4).hex()
result_log_file = f"/files/test_result-{file_friendly_test_type}-{backend}.xml"
warnings_file = f"/files/warnings-{file_friendly_test_type}-{backend}.txt"
coverage_file = f"/files/coverage-{file_friendly_test_type}-{backend}-{random_suffix}.xml"
return result_log_file, warnings_file, coverage_file
def get_ignore_switches_for_provider(provider_folders: list[str]) -> list[str]:
args = []
for providers in provider_folders:
args.extend(
[
f"--ignore=providers/tests/{providers}",
f"--ignore=providers/tests/system/{providers}",
f"--ignore=providers/tests/integration/{providers}",
]
)
return args
def get_suspended_provider_args() -> list[str]:
suspended_folders = get_suspended_provider_folders()
return get_ignore_switches_for_provider(suspended_folders)
def get_excluded_provider_args(python_version: str) -> list[str]:
excluded_folders = get_excluded_provider_folders(python_version)
return get_ignore_switches_for_provider(excluded_folders)
TEST_TYPE_CORE_MAP_TO_PYTEST_ARGS: dict[str, list[str]] = {
"Always": ["tests/always"],
"API": ["tests/api", "tests/api_connexion", "tests/api_internal", "tests/api_fastapi"],
"CLI": ["tests/cli"],
"Core": [
"tests/core",
"tests/executors",
"tests/jobs",
"tests/models",
"tests/ti_deps",
"tests/utils",
],
"Integration": ["tests/integration"],
"Operators": ["tests/operators"],
"Serialization": [
"tests/serialization",
],
"TaskSDK": ["task_sdk/tests"],
"WWW": [
"tests/www",
],
}
TEST_GROUP_TO_TEST_FOLDER: dict[GroupOfTests, str] = {
GroupOfTests.CORE: "tests",
GroupOfTests.PROVIDERS: "providers/tests",
GroupOfTests.TASK_SDK: "task_sdk/tests",
GroupOfTests.HELM: "helm_tests",
GroupOfTests.INTEGRATION_CORE: "tests/integration",
GroupOfTests.INTEGRATION_PROVIDERS: "providers/tests/integration",
}
# Those directories are already ignored vu pyproject.toml. We want to exclude them here as well.
NO_RECURSE_DIRS = [
"tests/_internals",
"tests/dags_with_system_exit",
"tests/dags_corrupted",
"tests/dags",
"providers/tests/system/google/cloud/dataproc/resources",
"providers/tests/system/google/cloud/gcs/resources",
]
def find_all_other_tests() -> list[str]:
all_named_test_folders = list(chain.from_iterable(TEST_TYPE_CORE_MAP_TO_PYTEST_ARGS.values()))
all_named_test_folders.append(TEST_GROUP_TO_TEST_FOLDER[GroupOfTests.PROVIDERS])
all_named_test_folders.append(TEST_GROUP_TO_TEST_FOLDER[GroupOfTests.TASK_SDK])
all_named_test_folders.append(TEST_GROUP_TO_TEST_FOLDER[GroupOfTests.HELM])
all_named_test_folders.append(TEST_GROUP_TO_TEST_FOLDER[GroupOfTests.INTEGRATION_CORE])
all_named_test_folders.append(TEST_GROUP_TO_TEST_FOLDER[GroupOfTests.INTEGRATION_PROVIDERS])
all_named_test_folders.append("tests/system")
all_named_test_folders.append("providers/tests/system")
all_named_test_folders.extend(NO_RECURSE_DIRS)
all_current_test_folders = [
str(path.relative_to(AIRFLOW_SOURCES_ROOT))
for path in AIRFLOW_SOURCES_ROOT.glob("tests/*")
if path.is_dir() and path.name != "__pycache__"
]
for named_test_folder in all_named_test_folders:
if named_test_folder in all_current_test_folders:
all_current_test_folders.remove(named_test_folder)
return sorted(all_current_test_folders)
PROVIDERS_PREFIX = "Providers"
PROVIDERS_LIST_PREFIX = "Providers["
PROVIDERS_LIST_EXCLUDE_PREFIX = "Providers[-"
def convert_test_type_to_pytest_args(
*,
test_group: GroupOfTests,
test_type: str,
) -> list[str]:
if test_type == "None":
return []
if test_type in ALL_TEST_SUITES:
return [
TEST_GROUP_TO_TEST_FOLDER[test_group],
*ALL_TEST_SUITES[test_type],
]
if test_group == GroupOfTests.SYSTEM and test_type != NONE_TEST_TYPE:
get_console().print(f"[error]Only {NONE_TEST_TYPE} should be allowed as test type[/]")
sys.exit(1)
if test_group == GroupOfTests.HELM:
if test_type not in all_helm_test_packages():
get_console().print(f"[error]Unknown helm test type: {test_type}[/]")
sys.exit(1)
helm_folder = TEST_GROUP_TO_TEST_FOLDER[test_group]
if test_type and test_type != ALL_TEST_TYPE:
return [f"{helm_folder}/{test_type}"]
else:
return [helm_folder]
if test_type == SelectiveCoreTestType.OTHER.value and test_group == GroupOfTests.CORE:
return find_all_other_tests()
if test_group in [
GroupOfTests.INTEGRATION_CORE,
GroupOfTests.INTEGRATION_PROVIDERS,
]:
if test_type != ALL_TEST_TYPE:
get_console().print(f"[error]Unknown test type for {test_group}: {test_type}[/]")
sys.exit(1)
if test_group == GroupOfTests.PROVIDERS:
if test_type.startswith(PROVIDERS_LIST_EXCLUDE_PREFIX):
excluded_provider_list = test_type[len(PROVIDERS_LIST_EXCLUDE_PREFIX) : -1].split(",")
providers_folder = TEST_GROUP_TO_TEST_FOLDER[GroupOfTests.PROVIDERS]
providers_with_exclusions: list = [providers_folder]
for excluded_provider in excluded_provider_list:
providers_with_exclusions.append(
f"--ignore={providers_folder}/" + excluded_provider.replace(".", "/")
)
return providers_with_exclusions
if test_type.startswith(PROVIDERS_LIST_PREFIX):
provider_list = test_type[len(PROVIDERS_LIST_PREFIX) : -1].split(",")
providers_to_test = []
for provider in provider_list:
provider_path = TESTS_PROVIDERS_ROOT.joinpath(provider.replace(".", "/"))
if provider_path.is_dir():
providers_to_test.append(provider_path.relative_to(AIRFLOW_SOURCES_ROOT).as_posix())
else:
get_console().print(
f"[error]Provider directory {provider_path} does not exist for {provider}. "
f"This is bad. Please add it (all providers should have a package in tests)"
)
sys.exit(1)
return providers_to_test
if not test_type.startswith(PROVIDERS_PREFIX):
get_console().print(f"[error]Unknown test type for {GroupOfTests.PROVIDERS}: {test_type}[/]")
sys.exit(1)
return [TEST_GROUP_TO_TEST_FOLDER[test_group]]
if test_group != GroupOfTests.CORE:
get_console().print(f"[error]Only {GroupOfTests.CORE} should be allowed here[/]")
test_dirs = TEST_TYPE_CORE_MAP_TO_PYTEST_ARGS.get(test_type)
if test_dirs:
return test_dirs.copy()
get_console().print(f"[error]Unknown test type: {test_type}[/]")
sys.exit(1)
def generate_args_for_pytest(
*,
test_group: GroupOfTests,
test_type: str,
test_timeout: int,
skip_db_tests: bool,
run_db_tests_only: bool,
backend: str,
use_xdist: bool,
enable_coverage: bool,
collect_only: bool,
parallelism: int,
parallel_test_types_list: list[str],
python_version: str,
keep_env_variables: bool,
no_db_cleanup: bool,
):
result_log_file, warnings_file, coverage_file = test_paths(test_type, backend)
if skip_db_tests and parallel_test_types_list:
args = convert_parallel_types_to_folders(
test_group=test_group,
parallel_test_types_list=parallel_test_types_list,
)
else:
args = convert_test_type_to_pytest_args(
test_group=test_group,
test_type=test_type,
)
args.extend(
[
"--verbosity=0",
"--strict-markers",
"--durations=100",
"--maxfail=50",
"--color=yes",
f"--junitxml={result_log_file}",
# timeouts in seconds for individual tests
"--timeouts-order=moi",
f"--setup-timeout={test_timeout}",
f"--execution-timeout={test_timeout}",
f"--teardown-timeout={test_timeout}",
"--disable-warnings",
# Only display summary for non-expected cases
#
# f - failed
# E - error
# X - xpassed (passed even if expected to fail)
#
# The following cases are not displayed:
# x - xfailed (expected to fail and failed)
# p - passed
# P - passed with output
#
"-rfEX",
]
)
if skip_db_tests:
args.append("--skip-db-tests")
if run_db_tests_only:
args.append("--run-db-tests-only")
if test_group not in [GroupOfTests.SYSTEM]:
args.append("--ignore-glob=*/tests/system/*")
if test_group != GroupOfTests.INTEGRATION_CORE:
args.append(f"--ignore-glob={TEST_GROUP_TO_TEST_FOLDER[GroupOfTests.INTEGRATION_CORE]}/*")
if test_group != GroupOfTests.INTEGRATION_PROVIDERS:
args.append(f"--ignore-glob={TEST_GROUP_TO_TEST_FOLDER[GroupOfTests.INTEGRATION_PROVIDERS]}/*")
if test_group != GroupOfTests.HELM:
# do not produce warnings output for helm tests
args.append(f"--warning-output-path={warnings_file}")
args.append(f"--ignore={TEST_GROUP_TO_TEST_FOLDER[GroupOfTests.HELM]}")
if test_group not in [GroupOfTests.HELM, GroupOfTests.SYSTEM]:
args.append("--with-db-init")
args.extend(get_suspended_provider_args())
args.extend(get_excluded_provider_args(python_version))
if use_xdist:
args.extend(["-n", str(parallelism) if parallelism else "auto"])
# We have to disable coverage for Python 3.12 because of the issue with coverage that takes too long, despite
# Using experimental support for Python 3.12 PEP 669. The coverage.py is not yet fully compatible with the
# full scope of PEP-669. That will be fully done when https://github.com/nedbat/coveragepy/issues/1746 is
# resolve for now we are disabling coverage for Python 3.12, and it causes slower execution and occasional
# timeouts
if enable_coverage and python_version != "3.12":
args.extend(
[
"--cov=airflow",
"--cov-config=pyproject.toml",
f"--cov-report=xml:{coverage_file}",
]
)
else:
args.append("--no-cov")
if collect_only:
args.extend(
[
"--collect-only",
"-qqqq",
"--disable-warnings",
]
)
if keep_env_variables:
args.append("--keep-env-variables")
if no_db_cleanup:
args.append("--no-db-cleanup")
return args
def convert_parallel_types_to_folders(test_group: GroupOfTests, parallel_test_types_list: list[str]):
args = []
for _test_type in parallel_test_types_list:
args.extend(
convert_test_type_to_pytest_args(
test_group=test_group,
test_type=_test_type,
)
)
# leave only folders, strip --pytest-args that exclude some folders with `-' prefix
folders = [
arg for arg in args if any(arg.startswith(prefix) for prefix in TEST_GROUP_TO_TEST_FOLDER.values())
]
# remove specific provider sub-folders if "providers/tests" is already in the list
# This workarounds pytest issues where it will only run tests from specific subfolders
# if both parent and child folders are in the list
# The issue in Pytest (changed behaviour in Pytest 8.2 is tracked here
# https://github.com/pytest-dev/pytest/issues/12605
if "providers/tests" in folders:
folders = [folder for folder in folders if not folder.startswith("providers/tests/")]
return folders