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

Raise error when DagRun fails while running dag test #36517

Merged
merged 2 commits into from
Jan 2, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion airflow/cli/commands/dag_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ def dag_test(args, dag: DAG | None = None, session: Session = NEW_SESSION) -> No
raise SystemExit(f"Configuration {args.conf!r} is not valid JSON. Error: {e}")
execution_date = args.execution_date or timezone.utcnow()
dag = dag or get_dag(subdir=args.subdir, dag_id=args.dag_id)
dag.test(execution_date=execution_date, run_conf=run_conf, session=session)
dr: DagRun = dag.test(execution_date=execution_date, run_conf=run_conf, session=session)
show_dagrun = args.show_dagrun
imgcat = args.imgcat_dagrun
filename = args.save_dagrun
Expand All @@ -536,6 +536,9 @@ def dag_test(args, dag: DAG | None = None, session: Session = NEW_SESSION) -> No
if show_dagrun:
print(dot_graph.source)

if dr and dr.state == DagRunState.FAILED:
raise SystemExit("DagRun failed")


@cli_utils.action_cli
@providers_configuration_loaded
Expand Down
11 changes: 11 additions & 0 deletions tests/cli/commands/test_dag_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from airflow.triggers.temporal import DateTimeTrigger, TimeDeltaTrigger
from airflow.utils import timezone
from airflow.utils.session import create_session
from airflow.utils.state import DagRunState
from airflow.utils.types import DagRunType
from tests.models import TEST_DAGS_FOLDER
from tests.test_utils.config import conf_vars
Expand Down Expand Up @@ -747,6 +748,16 @@ def test_dag_test(self, mock_get_dag):
]
)

@mock.patch("airflow.cli.commands.dag_command.get_dag")
def test_dag_test_fail_raise_error(self, mock_get_dag):
execution_date_str = DEFAULT_DATE.isoformat()
mock_get_dag.return_value.test.return_value = DagRun(
dag_id="example_bash_operator", execution_date=DEFAULT_DATE, state=DagRunState.FAILED
)
cli_args = self.parser.parse_args(["dags", "test", "example_bash_operator", execution_date_str])
with pytest.raises(SystemExit, match=r"DagRun failed"):
dag_command.dag_test(cli_args)

@mock.patch("airflow.cli.commands.dag_command.get_dag")
@mock.patch("airflow.utils.timezone.utcnow")
def test_dag_test_no_execution_date(self, mock_utcnow, mock_get_dag):
Expand Down