Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow DockerOperator.skip_on_exit_code to be zero #36360

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion airflow/providers/docker/operators/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def __init__(
skip_on_exit_code
if isinstance(skip_on_exit_code, Container)
else [skip_on_exit_code]
if skip_on_exit_code
if skip_on_exit_code is not None
else []
)
self.port_bindings = port_bindings or {}
Expand Down
23 changes: 18 additions & 5 deletions tests/providers/docker/decorators/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,29 @@ def do_run():
assert dag.task_ids[-1] == "do_run__20"

@pytest.mark.parametrize(
"extra_kwargs, actual_exit_code, expected_state",
"kwargs, actual_exit_code, expected_state",
[
(None, 99, TaskInstanceState.FAILED),
({}, 0, TaskInstanceState.SUCCESS),
({}, 100, TaskInstanceState.FAILED),
({}, 101, TaskInstanceState.FAILED),
({"skip_on_exit_code": None}, 0, TaskInstanceState.SUCCESS),
({"skip_on_exit_code": None}, 100, TaskInstanceState.FAILED),
({"skip_on_exit_code": None}, 101, TaskInstanceState.FAILED),
({"skip_on_exit_code": 100}, 0, TaskInstanceState.SUCCESS),
({"skip_on_exit_code": 100}, 100, TaskInstanceState.SKIPPED),
({"skip_on_exit_code": 100}, 101, TaskInstanceState.FAILED),
({"skip_on_exit_code": None}, 0, TaskInstanceState.SUCCESS),
({"skip_on_exit_code": 0}, 0, TaskInstanceState.SKIPPED),
({"skip_on_exit_code": [100]}, 0, TaskInstanceState.SUCCESS),
({"skip_on_exit_code": [100]}, 100, TaskInstanceState.SKIPPED),
({"skip_on_exit_code": [100]}, 101, TaskInstanceState.FAILED),
({"skip_on_exit_code": [100, 102]}, 101, TaskInstanceState.FAILED),
({"skip_on_exit_code": (100,)}, 0, TaskInstanceState.SUCCESS),
({"skip_on_exit_code": (100,)}, 100, TaskInstanceState.SKIPPED),
({"skip_on_exit_code": (100,)}, 101, TaskInstanceState.FAILED),
],
)
def test_skip_docker_operator(self, extra_kwargs, actual_exit_code, expected_state, dag_maker):
@task.docker(image="python:3.9-slim", auto_remove="force", **(extra_kwargs or {}))
def test_skip_docker_operator(self, kwargs, actual_exit_code, expected_state, dag_maker):
@task.docker(image="python:3.9-slim", auto_remove="force", **kwargs)
def f(exit_code):
raise SystemExit(exit_code)

Expand Down
27 changes: 16 additions & 11 deletions tests/providers/docker/operators/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,27 +527,32 @@ def test_execute_unicode_logs(self):
print_exception_mock.assert_not_called()

@pytest.mark.parametrize(
"extra_kwargs, actual_exit_code, expected_exc",
"kwargs, actual_exit_code, expected_exc",
[
(None, 99, AirflowException),
({}, 0, None),
({}, 100, AirflowException),
({}, 101, AirflowException),
({"skip_on_exit_code": None}, 0, None),
({"skip_on_exit_code": None}, 100, AirflowException),
({"skip_on_exit_code": None}, 101, AirflowException),
({"skip_on_exit_code": 100}, 0, None),
({"skip_on_exit_code": 100}, 100, AirflowSkipException),
({"skip_on_exit_code": 100}, 101, AirflowException),
({"skip_on_exit_code": None}, 100, AirflowException),
({"skip_on_exit_code": 0}, 0, AirflowSkipException),
({"skip_on_exit_code": [100]}, 0, None),
({"skip_on_exit_code": [100]}, 100, AirflowSkipException),
({"skip_on_exit_code": (100, 101)}, 100, AirflowSkipException),
({"skip_on_exit_code": 100}, 101, AirflowException),
({"skip_on_exit_code": [100]}, 101, AirflowException),
({"skip_on_exit_code": [100, 102]}, 101, AirflowException),
({"skip_on_exit_code": None}, 0, None),
({"skip_on_exit_code": (100,)}, 0, None),
({"skip_on_exit_code": (100,)}, 100, AirflowSkipException),
({"skip_on_exit_code": (100,)}, 101, AirflowException),
],
)
def test_skip(self, extra_kwargs, actual_exit_code, expected_exc):
def test_skip(self, kwargs, actual_exit_code, expected_exc):
msg = {"StatusCode": actual_exit_code}
self.client_mock.wait.return_value = msg

kwargs = dict(image="ubuntu", owner="unittest", task_id="unittest")
if extra_kwargs:
kwargs.update(**extra_kwargs)
operator = DockerOperator(**kwargs)
operator = DockerOperator(image="ubuntu", owner="unittest", task_id="unittest", **kwargs)

if expected_exc is None:
operator.execute({})
Expand Down