Skip to content

Conversation

@hwang-cadent
Copy link
Contributor

Description

This PR fixes the broken test test_async_write_logs_should_execute_successfully() in the KubernetesPodOperator test suite. The test was incorrectly implemented and was not properly testing the async log writing functionality.

Changes

  • Added @patch(KUB_OP_PATH.format("client")) decorator to properly mock self.client.read_namespaced_pod_log() which is the actual method used by _write_logs()
  • Fixed mock setup to use mocked_client.read_namespaced_pod_log instead of incorrectly mocking mock_manager.read_pod_logs (which is never called)
  • Fixed mock return value to return an iterable of bytes: [test_logs.encode("utf-8")] as expected by the implementation
  • Added mock_manager.await_pod_completion.return_value for proper test flow
  • Fixed assertions to verify actual log output using caplog instead of a meaningless string assertion
  • Fixed get_logs=False assertion to check mocked_client.read_namespaced_pod_log.assert_not_called() instead of the incorrect pod_manager.read_pod_logs check

Root Cause

The test had three main issues:

  1. Wrong mock target: It mocked pod_manager.read_pod_logs but _write_logs() actually calls self.client.read_namespaced_pod_log() directly
  2. Incorrect assertion: The assertion assert f"Container logs: {test_logs}" always passes (it's just checking if a string exists, not if it was logged)
  3. Wrong verification for get_logs=False: It checked pod_manager.read_pod_logs which is never called by _write_logs()

Testing

  • Both parameterized test cases now pass:
    • test_async_write_logs_should_execute_successfully[True]
    • test_async_write_logs_should_execute_successfully[False]
  • All existing tests continue to pass

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Fixes #57515

- 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 apache#57515
@boring-cyborg boring-cyborg bot added area:providers provider:cncf-kubernetes Kubernetes (k8s) provider related issues labels Nov 13, 2025
hwang-cadent and others added 2 commits November 13, 2025 15:45
- 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.
@jscheffl
Copy link
Contributor

FInally now some static checks fail - can you run our pre-commit checks via prek (or ruff in this case) to fix code and push fixes?

hwang-cadent and others added 2 commits November 13, 2025 18:38
- Remove unused imports: BytesIO and HTTPResponse
- Format function parameters on multiple lines for better readability
@hwang-cadent
Copy link
Contributor Author

FInally now some static checks fail - can you run our pre-commit checks via prek (or ruff in this case) to fix code and push fixes?

Applied ruff formatting fixes: removed unused imports and formatted function parameters. All prek checks pass. Thanks!

Copy link
Contributor

@jscheffl jscheffl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! LGTM when CI turns green!

@jscheffl jscheffl merged commit fb029cf into apache:main Nov 15, 2025
94 checks passed
aaron-wolmutt pushed a commit to aaron-wolmutt/airflow that referenced this pull request Nov 20, 2025
…fully test (apache#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 apache#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
Copilot AI pushed a commit to jason810496/airflow that referenced this pull request Dec 5, 2025
…fully test (apache#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 apache#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
itayweb pushed a commit to itayweb/airflow that referenced this pull request Dec 6, 2025
…fully test (apache#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 apache#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:cncf-kubernetes Kubernetes (k8s) provider related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KubernetesPodOperator Test test_async_write_logs_should_execute_successfully is not working

2 participants