This is an automated email from the ASF dual-hosted git repository.

jscheffl pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new fb029cf738a Fixes #57515 - Fix 
test_async_write_logs_should_execute_successfully test (#58276)
fb029cf738a is described below

commit fb029cf738ab0e483139f479693175fb1e96d975
Author: hwang-cadent <[email protected]>
AuthorDate: Sat Nov 15 11:22:32 2025 -0600

    Fixes #57515 - Fix test_async_write_logs_should_execute_successfully test 
(#58276)
    
    * Fix test_async_write_logs_should_execute_successfully test
    
    - Add client mock patch to properly mock self.client.read_namespaced_pod_log
    - Fix assertions to verify actual log output using caplog
    - Fix get_logs=False assertion to check client.read_namespaced_pod_log 
instead of pod_manager.read_pod_logs
    - Add await_pod_completion mock return value
    
    Fixes #57515
    
    * Remove @pytest.mark.xfail and fix incorrect assertion
    
    - Remove @pytest.mark.xfail decorator as the test is now fixed
    - Remove incorrect assertion checking mock_manager.read_pod_logs which
      is never called by _write_logs() (it calls client.read_namespaced_pod_log
      directly instead)
    
    Both parameterized test cases now pass:
    - test_async_write_logs_should_execute_successfully[True] ✅
    - test_async_write_logs_should_execute_successfully[False] ✅
    
    Addresses reviewer feedback to remove xfail flag as proof the test works.
    
    * Apply ruff formatting fixes
    
    - Remove unused imports: BytesIO and HTTPResponse
    - Format function parameters on multiple lines for better readability
---
 .../unit/cncf/kubernetes/operators/test_pod.py     | 32 +++++++++++++---------
 1 file changed, 19 insertions(+), 13 deletions(-)

diff --git 
a/providers/cncf/kubernetes/tests/unit/cncf/kubernetes/operators/test_pod.py 
b/providers/cncf/kubernetes/tests/unit/cncf/kubernetes/operators/test_pod.py
index b201a4ef4b9..a061eead8fb 100644
--- a/providers/cncf/kubernetes/tests/unit/cncf/kubernetes/operators/test_pod.py
+++ b/providers/cncf/kubernetes/tests/unit/cncf/kubernetes/operators/test_pod.py
@@ -19,7 +19,6 @@ from __future__ import annotations
 import datetime
 import re
 from contextlib import contextmanager, nullcontext
-from io import BytesIO
 from typing import TYPE_CHECKING
 from unittest import mock
 from unittest.mock import MagicMock, mock_open, patch
@@ -28,7 +27,6 @@ import pendulum
 import pytest
 from kubernetes.client import ApiClient, V1Pod, V1PodSecurityContext, 
V1PodStatus, models as k8s
 from kubernetes.client.exceptions import ApiException
-from urllib3 import HTTPResponse
 
 from airflow.exceptions import (
     AirflowException,
@@ -2595,17 +2593,25 @@ class TestKubernetesPodOperatorAsync:
 
     @pytest.mark.parametrize("get_logs", [True, False])
     @patch(KUB_OP_PATH.format("post_complete_action"))
+    @patch(KUB_OP_PATH.format("client"))
     @patch(KUB_OP_PATH.format("extract_xcom"))
     @patch(HOOK_CLASS)
     @patch(KUB_OP_PATH.format("pod_manager"))
-    @pytest.mark.xfail
     def test_async_write_logs_should_execute_successfully(
-        self, mock_manager, mocked_hook, mock_extract_xcom, 
post_complete_action, get_logs
+        self,
+        mock_manager,
+        mocked_hook,
+        mock_extract_xcom,
+        mocked_client,
+        post_complete_action,
+        get_logs,
+        caplog,
     ):
         test_logs = "ok"
-        mock_manager.read_pod_logs.return_value = HTTPResponse(
-            body=BytesIO(test_logs.encode("utf-8")),
-            preload_content=False,
+        # Mock client.read_namespaced_pod_log to return an iterable of bytes
+        mocked_client.read_namespaced_pod_log.return_value = 
[test_logs.encode("utf-8")]
+        mock_manager.await_pod_completion.return_value = k8s.V1Pod(
+            metadata=k8s.V1ObjectMeta(name=TEST_NAME, namespace=TEST_NAMESPACE)
         )
         mocked_hook.return_value.get_pod.return_value = k8s.V1Pod(
             metadata=k8s.V1ObjectMeta(name=TEST_NAME, namespace=TEST_NAMESPACE)
@@ -2619,14 +2625,14 @@ class TestKubernetesPodOperatorAsync:
         self.run_pod_async(k)
 
         if get_logs:
-            # Note: the test below is broken and failing. Either the mock is 
wrong
-            # or the mocked container is not in a state that logging methods 
are called at-all.
-            # See https://github.com/apache/airflow/issues/57515
-            assert f"Container logs: {test_logs}"  # noqa: PLW0129
+            # Verify that client.read_namespaced_pod_log was called
+            mocked_client.read_namespaced_pod_log.assert_called_once()
+            # Verify the log output using caplog
+            assert f"[base] logs: {test_logs}" in caplog.text
             post_complete_action.assert_called_once()
-            mock_manager.return_value.read_pod_logs.assert_called()
         else:
-            mock_manager.return_value.read_pod_logs.assert_not_called()
+            # When get_logs=False, _write_logs should not be called, so 
client.read_namespaced_pod_log should not be called
+            mocked_client.read_namespaced_pod_log.assert_not_called()
 
     @patch(KUB_OP_PATH.format("post_complete_action"))
     @patch(KUB_OP_PATH.format("client"))

Reply via email to