-
Notifications
You must be signed in to change notification settings - Fork 13.7k
/
test_cli_parser.py
532 lines (475 loc) · 22.2 KB
/
test_cli_parser.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#!/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.
from __future__ import annotations
import argparse
import contextlib
import os
import re
import subprocess
import sys
import timeit
from collections import Counter
from importlib import reload
from io import StringIO
from pathlib import Path
from unittest import mock
from unittest.mock import MagicMock, patch
import pytest
from airflow.cli import cli_config, cli_parser
from airflow.cli.cli_config import ActionCommand, core_commands, lazy_load_command
from airflow.cli.utils import CliConflictError
from airflow.configuration import AIRFLOW_HOME
from airflow.executors import executor_loader
from airflow.executors.executor_utils import ExecutorName
from airflow.executors.local_executor import LocalExecutor
from airflow.providers.amazon.aws.executors.ecs.ecs_executor import AwsEcsExecutor
from airflow.providers.celery.executors.celery_executor import CeleryExecutor
from tests.test_utils.config import conf_vars
# Can not be `--snake_case` or contain uppercase letter
ILLEGAL_LONG_OPTION_PATTERN = re.compile("^--[a-z]+_[a-z]+|^--.*[A-Z].*")
# Only can be `-[a-z]` or `-[A-Z]`
LEGAL_SHORT_OPTION_PATTERN = re.compile("^-[a-zA-z]$")
cli_args = {k: v for k, v in cli_parser.__dict__.items() if k.startswith("ARG_")}
class TestCli:
def test_arg_option_long_only(self):
"""
Test if the name of cli.args long option valid
"""
optional_long = [
arg for arg in cli_args.values() if len(arg.flags) == 1 and arg.flags[0].startswith("-")
]
for arg in optional_long:
assert ILLEGAL_LONG_OPTION_PATTERN.match(arg.flags[0]) is None, f"{arg.flags[0]} is not match"
def test_arg_option_mix_short_long(self):
"""
Test if the name of cli.args mix option (-s, --long) valid
"""
optional_mix = [
arg for arg in cli_args.values() if len(arg.flags) == 2 and arg.flags[0].startswith("-")
]
for arg in optional_mix:
assert LEGAL_SHORT_OPTION_PATTERN.match(arg.flags[0]) is not None, f"{arg.flags[0]} is not match"
assert ILLEGAL_LONG_OPTION_PATTERN.match(arg.flags[1]) is None, f"{arg.flags[1]} is not match"
def test_subcommand_conflict(self):
"""
Test if each of cli.*_COMMANDS without conflict subcommand
"""
subcommand = {
var: cli_parser.__dict__.get(var)
for var in cli_parser.__dict__
if var.isupper() and var.startswith("COMMANDS")
}
for group_name, sub in subcommand.items():
name = [command.name.lower() for command in sub]
assert len(name) == len(set(name)), f"Command group {group_name} have conflict subcommand"
def test_subcommand_arg_name_conflict(self):
"""
Test if each of cli.*_COMMANDS.arg name without conflict
"""
subcommand = {
var: cli_parser.__dict__.get(var)
for var in cli_parser.__dict__
if var.isupper() and var.startswith("COMMANDS")
}
for group, command in subcommand.items():
for com in command:
conflict_arg = [arg for arg, count in Counter(com.args).items() if count > 1]
assert (
[] == conflict_arg
), f"Command group {group} function {com.name} have conflict args name {conflict_arg}"
def test_subcommand_arg_flag_conflict(self):
"""
Test if each of cli.*_COMMANDS.arg flags without conflict
"""
subcommand = {
key: val
for key, val in cli_parser.__dict__.items()
if key.isupper() and key.startswith("COMMANDS")
}
for group, command in subcommand.items():
for com in command:
position = [
a.flags[0] for a in com.args if (len(a.flags) == 1 and not a.flags[0].startswith("-"))
]
conflict_position = [arg for arg, count in Counter(position).items() if count > 1]
assert [] == conflict_position, (
f"Command group {group} function {com.name} have conflict "
f"position flags {conflict_position}"
)
long_option = [
a.flags[0] for a in com.args if (len(a.flags) == 1 and a.flags[0].startswith("-"))
] + [a.flags[1] for a in com.args if len(a.flags) == 2]
conflict_long_option = [arg for arg, count in Counter(long_option).items() if count > 1]
assert [] == conflict_long_option, (
f"Command group {group} function {com.name} have conflict "
f"long option flags {conflict_long_option}"
)
short_option = [a.flags[0] for a in com.args if len(a.flags) == 2]
conflict_short_option = [arg for arg, count in Counter(short_option).items() if count > 1]
assert [] == conflict_short_option, (
f"Command group {group} function {com.name} have conflict "
f"short option flags {conflict_short_option}"
)
@pytest.mark.db_test
@patch.object(LocalExecutor, "get_cli_commands")
def test_dynamic_conflict_detection(self, cli_commands_mock: MagicMock):
core_commands.append(
ActionCommand(
name="test_command",
help="does nothing",
func=lambda: None,
args=[],
)
)
cli_commands_mock.return_value = [
ActionCommand(
name="test_command",
help="just a command that'll conflict with one defined in core",
func=lambda: None,
args=[],
)
]
reload(executor_loader)
with pytest.raises(CliConflictError, match="test_command"):
# force re-evaluation of cli commands (done in top level code)
reload(cli_parser)
@patch.object(CeleryExecutor, "get_cli_commands")
@patch.object(AwsEcsExecutor, "get_cli_commands")
def test_hybrid_executor_get_cli_commands(
self, ecs_executor_cli_commands_mock, celery_executor_cli_commands_mock
):
"""Test that if multiple executors are configured, then every executor loads its commands."""
ecs_executor_command = ActionCommand(
name="ecs_command",
help="test command for ecs executor",
func=lambda: None,
args=[],
)
ecs_executor_cli_commands_mock.return_value = [ecs_executor_command]
celery_executor_command = ActionCommand(
name="celery_command",
help="test command for celery executor",
func=lambda: None,
args=[],
)
celery_executor_cli_commands_mock.return_value = [celery_executor_command]
reload(executor_loader)
executor_loader.ExecutorLoader.get_executor_names = mock.Mock(
return_value=[
ExecutorName("airflow.providers.celery.executors.celery_executor.CeleryExecutor"),
ExecutorName("airflow.providers.amazon.aws.executors.ecs.ecs_executor.AwsEcsExecutor"),
]
)
reload(cli_parser)
commands = [command.name for command in cli_parser.airflow_commands]
assert celery_executor_command.name in commands
assert ecs_executor_command.name in commands
@patch.object(CeleryExecutor, "get_cli_commands")
@patch.object(AwsEcsExecutor, "get_cli_commands")
def test_hybrid_executor_get_cli_commands_with_error(
self, ecs_executor_cli_commands_mock, celery_executor_cli_commands_mock, caplog
):
"""Test that if multiple executors are configured, then every executor loads its commands.
If the executor fails to load its commands, the CLI should log the error, and continue loading"""
caplog.set_level("ERROR")
ecs_executor_command = ActionCommand(
name="ecs_command",
help="test command for ecs executor",
func=lambda: None,
args=[],
)
ecs_executor_cli_commands_mock.side_effect = Exception()
celery_executor_command = ActionCommand(
name="celery_command",
help="test command for celery executor",
func=lambda: None,
args=[],
)
celery_executor_cli_commands_mock.return_value = [celery_executor_command]
reload(executor_loader)
executor_loader.ExecutorLoader.get_executor_names = mock.Mock(
return_value=[
ExecutorName("airflow.providers.amazon.aws.executors.ecs.ecs_executor.AwsEcsExecutor"),
ExecutorName("airflow.providers.celery.executors.celery_executor.CeleryExecutor"),
]
)
reload(cli_parser)
commands = [command.name for command in cli_parser.airflow_commands]
assert celery_executor_command.name in commands
assert ecs_executor_command.name not in commands
assert (
"Failed to load CLI commands from executor: airflow.providers.amazon.aws.executors.ecs.ecs_executor.AwsEcsExecutor"
in caplog.messages[0]
)
@patch.object(AwsEcsExecutor, "get_cli_commands")
def test_cli_parser_fail_to_load_executor(self, ecs_executor_cli_commands_mock, caplog):
caplog.set_level("ERROR")
ecs_executor_command = ActionCommand(
name="ecs_command",
help="test command for ecs executor",
func=lambda: None,
args=[],
)
ecs_executor_cli_commands_mock.return_value = [ecs_executor_command]
reload(executor_loader)
executor_loader.ExecutorLoader.get_executor_names = mock.Mock(
return_value=[
ExecutorName("airflow.providers.incorrect.executor.Executor"),
ExecutorName("airflow.providers.amazon.aws.executors.ecs.ecs_executor.AwsEcsExecutor"),
]
)
reload(cli_parser)
commands = [command.name for command in cli_parser.airflow_commands]
assert ecs_executor_command.name in commands
assert (
"Failed to load CLI commands from executor: airflow.providers.incorrect.executor.Executor"
in caplog.messages[0]
)
def test_falsy_default_value(self):
arg = cli_config.Arg(("--test",), default=0, type=int)
parser = argparse.ArgumentParser()
arg.add_to_parser(parser)
args = parser.parse_args(["--test", "10"])
assert args.test == 10
args = parser.parse_args([])
assert args.test == 0
def test_commands_and_command_group_sections(self):
parser = cli_parser.get_parser()
with contextlib.redirect_stdout(StringIO()) as stdout:
with pytest.raises(SystemExit):
parser.parse_args(["--help"])
stdout = stdout.getvalue()
assert "Commands" in stdout
assert "Groups" in stdout
def test_dag_parser_commands_and_comamnd_group_sections(self):
parser = cli_parser.get_parser(dag_parser=True)
with contextlib.redirect_stdout(StringIO()) as stdout:
with pytest.raises(SystemExit):
parser.parse_args(["--help"])
stdout = stdout.getvalue()
assert "Commands" in stdout
assert "Groups" in stdout
def test_should_display_help(self):
parser = cli_parser.get_parser()
all_command_as_args = [
command_as_args
for top_command in cli_parser.airflow_commands
for command_as_args in (
[[top_command.name]]
if isinstance(top_command, cli_parser.ActionCommand)
else [[top_command.name, nested_command.name] for nested_command in top_command.subcommands]
)
]
for cmd_args in all_command_as_args:
with pytest.raises(SystemExit):
parser.parse_args([*cmd_args, "--help"])
def test_dag_cli_should_display_help(self):
parser = cli_parser.get_parser(dag_parser=True)
all_command_as_args = [
command_as_args
for top_command in cli_config.dag_cli_commands
for command_as_args in (
[[top_command.name]]
if isinstance(top_command, cli_parser.ActionCommand)
else [[top_command.name, nested_command.name] for nested_command in top_command.subcommands]
)
]
for cmd_args in all_command_as_args:
with pytest.raises(SystemExit):
parser.parse_args([*cmd_args, "--help"])
def test_positive_int(self):
assert 1 == cli_config.positive_int(allow_zero=True)("1")
assert 0 == cli_config.positive_int(allow_zero=True)("0")
with pytest.raises(argparse.ArgumentTypeError):
cli_config.positive_int(allow_zero=False)("0")
with pytest.raises(argparse.ArgumentTypeError):
cli_config.positive_int(allow_zero=True)("-1")
@pytest.mark.parametrize(
"command",
[
"celery",
"kubernetes",
],
)
def test_executor_specific_commands_not_accessible(self, command):
with conf_vars({("core", "executor"): "SequentialExecutor"}), contextlib.redirect_stderr(
StringIO()
) as stderr:
reload(executor_loader)
reload(cli_parser)
parser = cli_parser.get_parser()
with pytest.raises(SystemExit):
parser.parse_args([command])
stderr = stderr.getvalue()
assert (f"airflow command error: argument GROUP_OR_COMMAND: invalid choice: '{command}'") in stderr
@pytest.mark.parametrize(
"executor,expected_args",
[
("CeleryExecutor", ["celery"]),
("CeleryKubernetesExecutor", ["celery", "kubernetes"]),
("KubernetesExecutor", ["kubernetes"]),
("LocalExecutor", []),
("LocalKubernetesExecutor", ["kubernetes"]),
("SequentialExecutor", []),
# custom executors are mapped to the regular ones in `conftest.py`
("custom_executor.CustomLocalExecutor", []),
("custom_executor.CustomLocalKubernetesExecutor", ["kubernetes"]),
("custom_executor.CustomCeleryExecutor", ["celery"]),
("custom_executor.CustomCeleryKubernetesExecutor", ["celery", "kubernetes"]),
("custom_executor.CustomKubernetesExecutor", ["kubernetes"]),
],
)
def test_cli_parser_executors(self, executor, expected_args):
"""Test that CLI commands for the configured executor are present"""
for expected_arg in expected_args:
with conf_vars({("core", "executor"): executor}), contextlib.redirect_stderr(
StringIO()
) as stderr:
reload(executor_loader)
reload(cli_parser)
parser = cli_parser.get_parser()
with pytest.raises(SystemExit) as e: # running the help command exits, so we prevent that
parser.parse_args([expected_arg, "--help"])
assert e.value.code == 0, stderr.getvalue() # return code 0 == no problem
stderr = stderr.getvalue()
assert "airflow command error" not in stderr
def test_non_existing_directory_raises_when_metavar_is_dir_for_db_export_cleaned(self):
"""Test that the error message is correct when the directory does not exist."""
with contextlib.redirect_stderr(StringIO()) as stderr:
parser = cli_parser.get_parser()
with pytest.raises(SystemExit):
parser.parse_args(["db", "export-archived", "--output-path", "/non/existing/directory"])
error_msg = stderr.getvalue()
assert error_msg == (
"\nairflow db export-archived command error: The directory "
"'/non/existing/directory' does not exist!, see help above.\n"
)
@pytest.mark.parametrize("export_format", ["json", "yaml", "unknown"])
@patch("airflow.cli.cli_config.os.path.isdir", return_value=True)
def test_invalid_choice_raises_for_export_format_in_db_export_archived_command(
self, mock_isdir, export_format
):
"""Test that invalid choice raises for export-format in db export-cleaned command."""
with contextlib.redirect_stderr(StringIO()) as stderr:
parser = cli_parser.get_parser()
with pytest.raises(SystemExit):
parser.parse_args(
["db", "export-archived", "--export-format", export_format, "--output-path", "mydir"]
)
error_msg = stderr.getvalue()
assert error_msg == (
"\nairflow db export-archived command error: argument "
f"--export-format: invalid choice: '{export_format}' "
"(choose from 'csv'), see help above.\n"
)
@pytest.mark.parametrize(
"action_cmd",
[
ActionCommand(name="name", help="help", func=lazy_load_command(""), args=(), hide=True),
ActionCommand(name="name", help="help", func=lazy_load_command(""), args=(), hide=False),
],
)
@patch("argparse._SubParsersAction")
def test_add_command_with_hide(self, mock_subparser_actions, action_cmd):
cli_parser._add_command(mock_subparser_actions, action_cmd)
if action_cmd.hide:
mock_subparser_actions.add_parser.assert_called_once_with(
action_cmd.name, epilog=action_cmd.epilog
)
else:
mock_subparser_actions.add_parser.assert_called_once_with(
action_cmd.name, help=action_cmd.help, description=action_cmd.help, epilog=action_cmd.epilog
)
# We need to run it from sources with PYTHONPATH, not command line tool,
# because we need to make sure that we have providers configured from source provider.yaml files
CONFIG_FILE = Path(AIRFLOW_HOME) / "airflow.cfg"
class TestCliSubprocess:
"""
We need to run it from sources using "__main__" and setting the PYTHONPATH, not command line tool,
because we need to make sure that we have providers loaded from source provider.yaml files rather
than from provider packages which might not be installed in the test environment.
"""
@pytest.mark.quarantined
def test_cli_run_time(self):
setup_code = "import subprocess"
command = [sys.executable, "-m", "airflow", "--help"]
env = {"PYTHONPATH": os.pathsep.join(sys.path)}
timing_code = f"subprocess.run({command},env={env})"
# Limit the number of samples otherwise the test will take a very long time
num_samples = 3
threshold = 3.5
timing_result = timeit.timeit(stmt=timing_code, number=num_samples, setup=setup_code) / num_samples
# Average run time of Airflow CLI should at least be within 3.5s
assert timing_result < threshold
def test_cli_parsing_does_not_initialize_providers_manager(self):
"""Test that CLI parsing does not initialize providers manager.
This test is here to make sure that we do not initialize providers manager - it is run as a
separate subprocess, to make sure we do not have providers manager initialized in the main
process from other tests.
"""
CONFIG_FILE.parent.mkdir(parents=True, exist_ok=True)
CONFIG_FILE.touch(exist_ok=True)
result = subprocess.run(
[sys.executable, "-m", "airflow", "providers", "lazy-loaded"],
env={"PYTHONPATH": os.pathsep.join(sys.path)},
check=False,
text=True,
)
assert result.returncode == 0
def test_airflow_config_contains_providers(self):
"""Test that airflow config has providers included by default.
This test is run as a separate subprocess, to make sure we do not have providers manager
initialized in the main process from other tests.
"""
CONFIG_FILE.unlink(missing_ok=True)
result = subprocess.run(
[sys.executable, "-m", "airflow", "config", "list"],
env={"PYTHONPATH": os.pathsep.join(sys.path)},
check=False,
text=True,
)
assert result.returncode == 0
assert CONFIG_FILE.exists()
assert "celery_config_options" in CONFIG_FILE.read_text()
def test_airflow_config_output_contains_providers_by_default(self):
"""Test that airflow config has providers excluded in config list when asked for it."""
CONFIG_FILE.parent.mkdir(parents=True, exist_ok=True)
CONFIG_FILE.touch(exist_ok=True)
result = subprocess.run(
[sys.executable, "-m", "airflow", "config", "list"],
env={"PYTHONPATH": os.pathsep.join(sys.path)},
check=False,
text=True,
capture_output=True,
)
assert result.returncode == 0
assert "celery_config_options" in result.stdout
def test_airflow_config_output_does_not_contain_providers_when_excluded(self):
"""Test that airflow config has providers excluded in config list when asked for it."""
CONFIG_FILE.parent.mkdir(parents=True, exist_ok=True)
CONFIG_FILE.unlink(missing_ok=True)
CONFIG_FILE.touch(exist_ok=True)
result = subprocess.run(
[sys.executable, "-m", "airflow", "config", "list", "--exclude-providers"],
env={"PYTHONPATH": os.pathsep.join(sys.path)},
check=False,
text=True,
capture_output=True,
)
assert result.returncode == 0
assert "celery_config_options" not in result.stdout