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

tests: fix missing mock in attach unit test #2785

Merged
merged 2 commits into from
Nov 29, 2023
Merged

tests: fix missing mock in attach unit test #2785

merged 2 commits into from
Nov 29, 2023

Conversation

renanrodrigo
Copy link
Member

Why is this needed?

This PR solves all of our problems because it fixes a unit test leak in _post_cli_attach.

Test Steps

Unit tests runs

Checklist

  • I have updated or added any unit tests accordingly
  • I have updated or added any integration tests accordingly
  • Changes here need to be documented, and this was done in:

Does this PR require extra reviews?

  • Yes
  • No

@github-actions
Copy link

Jira: This PR is not related to a Jira item. (The PR title does not include a SC-#### reference)

GitHub Issues: No GitHub issues are fixed by this PR. (No commits have Fixes: #### references)

Launchpad Bugs: No Launchpad bugs are fixed by this PR. (No commits have LP: #### references)

Documentation: The changes in this PR do not require documentation changes.

👍 this comment to confirm that this is correct.

@@ -241,8 +241,10 @@ def test_lock_file_exists(
@mock.patch(M_PATH + "contract.UAContractClient.add_contract_machine")
@mock.patch("uaclient.actions.status", return_value=("", 0))
@mock.patch("uaclient.status.format_tabular")
@mock.patch(M_PATH + "daemon")
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be better to mock _post_cli_attach like we do on the other tests ? The concern is that for some reason we add new functionality to that method, we might forget to mock it again here

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, but in this case another test needs to be added to cover _post_cli_attach, as it is being tested as part of this test.
I added this as a quick path, but I can add the extra test if that suits better

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that would be better tbh, since we it will be more explicit that we have a dedicated test just for that function

_post_cli_attach must be mocked to avoid leaks. The JSON part of the
test removed in this commit is covered in test_event_logger.

Signed-off-by: Renan Rodrigo <[email protected]>
testing only the relevant output

Signed-off-by: Renan Rodrigo <[email protected]>
@renanrodrigo
Copy link
Member Author

@lucasmoura done there

with contextlib.redirect_stdout(fake_stdout):
main_error_handler(action_attach)(args, cfg)

expected = {
Copy link
Contributor

Choose a reason for hiding this comment

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

With this new setup we are no longer testing the event_logger part of the command. Should we add an explicit test on for this as well ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure - there are tests in tests/event_logger.py to cover that, and the text output covers the infos.
I would even go further and mock the event logger to be honest - the unit test should not be responsible for that after all.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We have integration tests that cover json output for attach, so I think this is okay to remove.

@lucasmoura
Copy link
Contributor

CI errors are unrelated to this change

@lucasmoura lucasmoura merged commit 8dcb50b into main Nov 29, 2023
22 of 25 checks passed
@lucasmoura lucasmoura deleted the leak-file-rm branch November 29, 2023 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants