Skip to content

Commit

Permalink
fix(providers/jenkins): respect soft_fail argument when exception is …
Browse files Browse the repository at this point in the history
…raised (#34475)
  • Loading branch information
Lee-W committed Sep 19, 2023
1 parent defc68b commit 43f5d13
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
8 changes: 6 additions & 2 deletions airflow/providers/jenkins/sensors/jenkins.py
Expand Up @@ -19,7 +19,7 @@

from typing import TYPE_CHECKING, Iterable

from airflow import AirflowException
from airflow.exceptions import AirflowException, AirflowSkipException
from airflow.providers.jenkins.hooks.jenkins import JenkinsHook
from airflow.sensors.base import BaseSensorOperator

Expand Down Expand Up @@ -67,7 +67,11 @@ def poke(self, context: Context) -> bool:
if build_result in self.target_states:
return True
else:
raise AirflowException(
# TODO: remove this if check when min_airflow_version is set to higher than 2.7.1
message = (
f"Build {build_number} finished with a result {build_result}, "
f"which does not meet the target state {self.target_states}."
)
if self.soft_fail:
raise AirflowSkipException(message)
raise AirflowException(message)
12 changes: 9 additions & 3 deletions tests/providers/jenkins/sensors/test_jenkins.py
Expand Up @@ -21,7 +21,7 @@

import pytest

from airflow import AirflowException
from airflow.exceptions import AirflowException, AirflowSkipException
from airflow.providers.jenkins.hooks.jenkins import JenkinsHook
from airflow.providers.jenkins.sensors.jenkins import JenkinsBuildSensor

Expand Down Expand Up @@ -69,6 +69,9 @@ def test_poke_buliding(self, mock_jenkins, build_number, build_state, result):
assert jenkins_mock.get_job_info.call_count == 0 if build_number else 1
jenkins_mock.get_build_info.assert_called_once_with("a_job_on_jenkins", target_build_number)

@pytest.mark.parametrize(
"soft_fail, expected_exception", ((False, AirflowException), (True, AirflowSkipException))
)
@pytest.mark.parametrize(
"build_number, build_state, result",
[
Expand All @@ -85,7 +88,9 @@ def test_poke_buliding(self, mock_jenkins, build_number, build_state, result):
],
)
@patch("jenkins.Jenkins")
def test_poke_finish_building(self, mock_jenkins, build_number, build_state, result):
def test_poke_finish_building(
self, mock_jenkins, build_number, build_state, result, soft_fail, expected_exception
):
target_build_number = build_number if build_number else 10

jenkins_mock = MagicMock()
Expand All @@ -103,9 +108,10 @@ def test_poke_finish_building(self, mock_jenkins, build_number, build_state, res
job_name="a_job_on_jenkins",
build_number=target_build_number,
target_states=["SUCCESS"],
soft_fail=soft_fail,
)
if result not in sensor.target_states:
with pytest.raises(AirflowException):
with pytest.raises(expected_exception):
sensor.poke(None)
assert jenkins_mock.get_build_info.call_count == 2
else:
Expand Down

0 comments on commit 43f5d13

Please sign in to comment.