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

rdctl: linux/darwin: use process group on factory reset #7908

Merged

Conversation

mook-as
Copy link
Contributor

@mook-as mook-as commented Dec 9, 2024

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.

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>
@mook-as mook-as force-pushed the rdx/process-groups-factory-reset branch from 095c575 to c0251b2 Compare December 11, 2024 23:15
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>
@mook-as mook-as force-pushed the rdx/process-groups-factory-reset branch from c0251b2 to 4be4ed4 Compare December 12, 2024 00:27
@mook-as mook-as marked this pull request as ready for review December 12, 2024 00:28
@gunamata gunamata requested a review from Nino-K December 12, 2024 17:53
Copy link
Member

@Nino-K Nino-K left a 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 {
Copy link
Member

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.

Copy link
Contributor Author

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{}
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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)
})
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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?

Copy link
Contributor Author

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.)

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.
Copy link
Member

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?

Copy link
Contributor Author

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")
Copy link
Member

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)?

Copy link
Contributor Author

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")
Copy link
Member

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"))
Copy link
Member

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,....

Copy link
Contributor Author

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).

@mook-as mook-as requested a review from Nino-K December 12, 2024 21:46
@mook-as mook-as force-pushed the rdx/process-groups-factory-reset branch from 4bc4229 to 516abf1 Compare December 12, 2024 21:47
- 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>
@mook-as mook-as force-pushed the rdx/process-groups-factory-reset branch from 516abf1 to 88e930e Compare December 12, 2024 21:50
@mook-as mook-as merged commit bbd7c63 into rancher-sandbox:main Dec 12, 2024
21 checks passed
@mook-as mook-as deleted the rdx/process-groups-factory-reset branch December 12, 2024 23:14
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.

2 participants