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

Implement context cancellation for command based gatherers #379

Merged
merged 13 commits into from
Jan 23, 2025

Conversation

balanza
Copy link
Member

@balanza balanza commented Jan 22, 2025

Description

Implement context cancellation for all gatherers that execute commands on the host machine:

  • ascsers
  • cibadmin
  • corosync
  • dispwork
  • mountinfo
  • packageversion
  • saphostctrl
  • sbddump
  • sysctl
  • verifypassword

@balanza balanza requested review from arbulu89 and CDimonaco January 22, 2025 20:21
Copy link
Contributor

@arbulu89 arbulu89 left a comment

Choose a reason for hiding this comment

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

Hey @balanza
Good job. Just some minor changes to do.

Besides that:
A question about the new test cases.
Why do you mock the execution here? you could test a real cancellation as well, not using a mock. At the end, it is cancelled and it should return the same error.
(for example, this has hidden the fact that you didn't use the ctx coming from Gather in the ascsers_cluster.go

If you think that is already well tested and you prefer to mock is OK.

nitpick:I think we could improve a bit them changing suite.Error(err) to see that this actually has a cancellation error rather than any possible error.

internal/factsengine/gatherers/ascsers_cluster_test.go Outdated Show resolved Hide resolved
@@ -7,6 +7,7 @@ import (
"os/exec"
Copy link
Contributor

Choose a reason for hiding this comment

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

Strange that we still need to import os/exec here

cancel()

suite.mockExecutor.
On("ExecContext", mock.Anything, "cibadmin", "--query", "--local").
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you try putting ctx instead of mock.Anything?
Maybe it validates that the function is called with the expected argument.
I don't know if it works though.

Copy link
Member Author

Choose a reason for hiding this comment

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

It works and it's actually brilliant! I refactored all tests like that in 5961524

It would have spotted the bug in #379 (comment)
You made my day

arbulu89
arbulu89 previously approved these changes Jan 23, 2025
Copy link
Contributor

@arbulu89 arbulu89 left a comment

Choose a reason for hiding this comment

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

Great! 💚
You can merge once the Ersk typo is fixed!

@balanza balanza force-pushed the context-command-gatherers branch from 7cf523b to b064d32 Compare January 23, 2025 09:33
@balanza
Copy link
Member Author

balanza commented Jan 23, 2025

A question about the new test cases.
Why do you mock the execution here? you could test a real cancellation as well, not using a mock. At the end, it is cancelled and it should return the same error.

You're right. In the end, command cancellation (and cleanup) is covered by tests on the command executor itself.

I refactor all tests in b064d32 and they work smoothly using the real executor. The less we mock the better, here it comes with a big BUT:

tests will succeed even if ExecCommand isn't hit. For example, imagine the case in which the Gather implementation has an early return on ctx.Err().
By doing this, we are theoretically missing the scenario in which the command has been started and then cancelled.

Anyway, it's nitpick and it's not our case so far. I like what we have now, I just wanted to highlight a possible drawback.

If you are fine with that, we can merge.

@balanza balanza requested a review from arbulu89 January 23, 2025 09:49
@balanza balanza dismissed arbulu89’s stale review January 23, 2025 09:50

looking for further review

Copy link
Contributor

@arbulu89 arbulu89 left a comment

Choose a reason for hiding this comment

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

@balanza That's ok.
I don't see why we should be checking ctx.Err before doing anything. It looks like something we shouldn't do.
At the end, the context we pass is to be used by system calls (commands, api calls, etc), so we can cancel them (even what we return is not that critical).

I think we cover all of our real cases

PD: if we have some early ctx.Err usage in the code, we should aim to a different test format as well

@balanza balanza merged commit 9ba65de into main Jan 23, 2025
11 checks passed
@balanza balanza deleted the context-command-gatherers branch January 23, 2025 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants