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

feat: Use caplog to test log messages #4315

Merged
merged 2 commits into from
Dec 20, 2024
Merged

Conversation

Glutexo
Copy link
Collaborator

@Glutexo Glutexo commented Dec 18, 2024

All Pull Requests:

Check all that apply:

  • Have you followed the guidelines in our Contributing document, including the instructions about commit messages?
  • No Sensitive Data in this change?
  • Is this PR to correct an issue?
  • Is this PR an enhancement?

Logger asserts are error-prone, because they assert calls, not actual messages. The caplog fixture is Pytest-native way to test logs. This makes integration-level unit tests easier to add.

For the caplog fixture to work on Python 2.6 (RHEL 6), pytest_catchlog needs to be installed. On more recent version of Python, the fixture is included in Pytest.

pytest_catchlog caused PID file write tests to fail. The fixture itself calls os.getpid, interfering with the patch. Every log message makes a getpid call that is recorded in the patch. assert_not_called patches then stopped working.

Fixed by removing the os.getpid patch. The original function is safe to use. test_write_pidfile_not_called verifies several calls among which write_to_disk is the important one, not getpid.

Card IDs:

  • CCT-774 closed at the time as WONTDO
  • CCT-963 blocked by this patch
  • CCT-1084 duplicate of the original card, now relevant

@Glutexo Glutexo force-pushed the caplog branch 2 times, most recently from f742a17 to 2df12ba Compare December 18, 2024 16:59
@Glutexo
Copy link
Collaborator Author

Glutexo commented Dec 18, 2024

May I ask for a review, @m-horky? For more information on why this is here, see my comment in CCT-1084.

Copy link
Contributor

@pkoprda pkoprda 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, I just left some nitpicks. Also please squash the commits and rebase

Copy link
Collaborator Author

@Glutexo Glutexo left a comment

Choose a reason for hiding this comment

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

Thank you, @pkoprda, for a precise review! ❤️ I fixed the issues you spotted and looked through the code for other instances. You spotted them all. Good job!

I was not sure about the squash: I kept the commits separate intentionally. But I have no problems doing so. So squashed and rebased.

Re-requesting a review.

@codecov-commenter
Copy link

codecov-commenter commented Dec 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.17%. Comparing base (91adb4e) to head (4dc8f78).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4315   +/-   ##
=======================================
  Coverage   77.17%   77.17%           
=======================================
  Files         765      765           
  Lines       41770    41770           
  Branches     8821     8821           
=======================================
  Hits        32238    32238           
  Misses       8464     8464           
  Partials     1068     1068           
Flag Coverage Δ
unittests 77.16% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Glutexo
Copy link
Collaborator Author

Glutexo commented Dec 19, 2024

Thanks, @pkoprda! One more round?

@pkoprda
Copy link
Contributor

pkoprda commented Dec 19, 2024

@Glutexo looks good, just squash the commits and then I will approve it

Logger asserts are error-prone, because they assert calls, not actual messages. The caplog fixture is Pytest-native way to test logs. This makes integration-level unit tests easier to add.

For the caplog fixture to work on Python 2.6 (RHEL 6), pytest_catchlog needs to be installed. On more recent version of python, the fixture is included in Pytest.

pytest_catchlog caused pidfile write tests to fail. The fixture itself calls os.getpid, interfering with the patch. Every log message makes a getpid call that is recorded in the patch. assert_not_called patches then stopped working.

Fixed by removing the os.getpid patch. The original function is safe to use. test_write_pidfile_not_called verifies several calls among which write_to_disk is the important one, not getpid.

Card IDs:

* CCT-774
* CCT-963
* CCT-1084

Signed-off-by: Štěpán Tomsa <[email protected]>
@Glutexo
Copy link
Collaborator Author

Glutexo commented Dec 19, 2024

Sure thing! Squashed. @pkoprda

Copy link
Contributor

@pkoprda pkoprda left a comment

Choose a reason for hiding this comment

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

Thank you @Glutexo, LGTM! The failing test is not related to this change.

@xiangce can we merge this?

EDIT: I opened PR insights-core#4316 to fix that failing test.

@xiangce xiangce added the RHEL 6 Only when this label is specified, it can be applided to "rhel_6" branch. label Dec 20, 2024
@xiangce xiangce merged commit 644d314 into RedHatInsights:master Dec 20, 2024
13 checks passed
@Glutexo
Copy link
Collaborator Author

Glutexo commented Dec 20, 2024

@pkoprda You were faster. 😃 I wanted to do that too.

@Glutexo Glutexo deleted the caplog branch December 20, 2024 09:49
xiangce pushed a commit that referenced this pull request Jan 2, 2025
Logger asserts are error-prone, because they assert calls, not actual messages. The caplog fixture is Pytest-native way to test logs. This makes integration-level unit tests easier to add.

For the caplog fixture to work on Python 2.6 (RHEL 6), pytest_catchlog needs to be installed. On more recent version of python, the fixture is included in Pytest.

pytest_catchlog caused pidfile write tests to fail. The fixture itself calls os.getpid, interfering with the patch. Every log message makes a getpid call that is recorded in the patch. assert_not_called patches then stopped working.

Fixed by removing the os.getpid patch. The original function is safe to use. test_write_pidfile_not_called verifies several calls among which write_to_disk is the important one, not getpid.

Card IDs:

* CCT-774
* CCT-963
* CCT-1084

Signed-off-by: Štěpán Tomsa <[email protected]>
(cherry picked from commit 644d314)
xiangce pushed a commit that referenced this pull request Jan 2, 2025
Logger asserts are error-prone, because they assert calls, not actual messages. The caplog fixture is Pytest-native way to test logs. This makes integration-level unit tests easier to add.

For the caplog fixture to work on Python 2.6 (RHEL 6), pytest_catchlog needs to be installed. On more recent version of python, the fixture is included in Pytest.

pytest_catchlog caused pidfile write tests to fail. The fixture itself calls os.getpid, interfering with the patch. Every log message makes a getpid call that is recorded in the patch. assert_not_called patches then stopped working.

Fixed by removing the os.getpid patch. The original function is safe to use. test_write_pidfile_not_called verifies several calls among which write_to_disk is the important one, not getpid.

Card IDs:

* CCT-774
* CCT-963
* CCT-1084

Signed-off-by: Štěpán Tomsa <[email protected]>
(cherry picked from commit 644d314)
(cherry picked from commit b22745e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RHEL 6 Only when this label is specified, it can be applided to "rhel_6" branch.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants