-
Notifications
You must be signed in to change notification settings - Fork 298
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
rdctl: linux/darwin: use process group on factory reset #7908
rdctl: linux/darwin: use process group on factory reset #7908
Conversation
This ensures we terminate the whole process group when we do a factory reset. This also changes some of the other process killing to not rely on spawning /usr/bin/pkill or pgrep. Signed-off-by: Mark Yen <mark.yen@suse.com>
095c575
to
c0251b2
Compare
This adds tests for (mainly) GetRDLaunchPath and GetMainExecutable; this has meant threading a context through the code so we can override when we get the rdctl path. Signed-off-by: Mark Yen <mark.yen@suse.com>
c0251b2
to
4be4ed4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just need clarification on including some testing parameters in the actual code to make it testable which is not ideal. I provided a few suggestions about that in some areas, let me know if that makes sense.
|
||
// OverrideRdctlPath produces a context that will override the path of the rdctl | ||
// executable. This should only be used in tests. | ||
func OverrideRdctlPath(ctx context.Context, rdctlPath string) context.Context { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this here? It should be part of the test, that way, you can make sure nobody can call it mistakenly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the relevant tests are cross-package. (Using internal
wouldn't work, because we're not using this as a library, so we're just guarding from ourselves.)
) | ||
|
||
type rdctlOverrideKeyType struct{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to change the type to String constant and export it? The context is limited enough here to avoid context key collision(guarantee uniqueness) by using string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per the docs, we should define our own context type, and specifically should not be of a built-in type.
var err error | ||
override, ok := ctx.Value(rdctlOverrideKey).(string) | ||
if ok { | ||
exePathWithSymlinks = override |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was gonna suggest to checking for empty paths but filepath.EvalSymlinks(exePathWithSymlinks)
should take care of that.
actual, err := directories.GetApplicationDirectory(ctx) | ||
require.NoError(t, err) | ||
assert.Equal(t, dir, actual) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to add some additional test cases for the following scenarios:
- Invalid or empty executable path.
- Non-existent executable file or Empty directory.
- Symlink loop or recursive symlinks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test cases added. (Obviously the symlink test is skipped on Windows.)
@@ -83,12 +83,11 @@ func getKnownFolder(folder *windows.KNOWNFOLDERID) (string, error) { | |||
) | |||
// SHGetKnownFolderPath documentation says we _must_ free the result with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a link?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a doc comment, so the link wouldn't work. I can add the URL as plain text though. (The https://git.io/JMpgD
in the function doc comment actually links to the documentation.)
src/go/rdctl/pkg/paths/paths.go
Outdated
return "", fmt.Errorf("failed to resolve %q: %w", rdctlSymlinkPath, err) | ||
// Get the path to the resources directory (the parent directory of the | ||
// platform-specific directory); this is used to fill in [Paths.Resources]. | ||
// The argument is used for testing; do not provide it in non-test code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea here works but seems like a hack to bend the rules for testing. Should this be a struct member function, with a separate SetPath...
function that tests can exclusively call?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, since the test in the same package, I don't even need that. But you're right, changing the function signature was silly.
return dir, nil | ||
} | ||
} | ||
return "", errors.New("search locations exhausted") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the returned error be more descriptive to include the locations that were searched (we will thank ourselves during debugging :b)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense; will do that here, and for the other platforms, plus for GetMainExecutable
.
return candidatePath, nil | ||
} | ||
} | ||
return "", errors.New("search locations exhausted") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also include the search locations?
@@ -201,20 +221,36 @@ func runCommandIgnoreOutput(cmd *exec.Cmd) error { | |||
return cmd.Run() | |||
} | |||
|
|||
func stopLima() error { | |||
func stopLima(ctx context.Context) error { | |||
return runCommandIgnoreOutput(exec.Command(limaCtlPath, "stop", "0")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are injecting ctx
we don't even use it, why can't we pass it to exec.CommandContext(ctx,....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, done (and the next two).
4bc4229
to
516abf1
Compare
- directories: Add more tests, reject rdctl path being a directory. - paths: Drop test-only argument for GetResourcesPath - paths: Add searched paths to GetRDLaunchPath / GetMainExecutable error - shutdown: Use context when launching limactl Signed-off-by: Mark Yen <mark.yen@suse.com>
516abf1
to
88e930e
Compare
This adjust rdctl to also use process groups on factory reset, as in #7846.
This will need to land after #7846 and #7838 due to conflicts.