Skip to content

Commit

Permalink
Replace caplog with patching log property in k8s tests (apache#46273)
Browse files Browse the repository at this point in the history
Since caplog is very vulnerable to side effects we remove it from
k8s tests and replace it with patching the log property of the
K8SHook. This should not be vulnerable to side-effects.
  • Loading branch information
potiuk authored Jan 30, 2025
1 parent 8e39da3 commit 3187141
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions providers/tests/cncf/kubernetes/hooks/test_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,28 +976,33 @@ async def test_delete_pod(self, lib_method, kube_config_loader):

@pytest.mark.asyncio
@mock.patch(KUBE_API.format("read_namespaced_pod_log"))
async def test_read_logs(self, lib_method, kube_config_loader, caplog):
async def test_read_logs(self, lib_method, kube_config_loader):
lib_method.return_value = self.mock_await_result("2023-01-11 Some string logs...")

hook = AsyncKubernetesHook(
conn_id=None,
in_cluster=False,
config_file=None,
cluster_context=None,
)
await hook.read_logs(
name=POD_NAME,
namespace=NAMESPACE,
)
with mock.patch(
"airflow.providers.cncf.kubernetes.hooks.kubernetes.AsyncKubernetesHook.log",
new_callable=PropertyMock,
) as log:
await hook.read_logs(
name=POD_NAME,
namespace=NAMESPACE,
)

lib_method.assert_called_once()
lib_method.assert_called_with(
name=POD_NAME,
namespace=NAMESPACE,
follow=False,
timestamps=True,
)
assert "Container logs from 2023-01-11 Some string logs..." in caplog.text
lib_method.assert_called_once()
lib_method.assert_called_with(
name=POD_NAME,
namespace=NAMESPACE,
follow=False,
timestamps=True,
)
log.return_value.info.assert_called_with(
"Container logs from %s", "2023-01-11 Some string logs..."
)

@pytest.mark.asyncio
@mock.patch(KUBE_BATCH_API.format("read_namespaced_job_status"))
Expand Down

0 comments on commit 3187141

Please sign in to comment.