-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 a smoke test for clusterctl plugins #11950
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,105 @@ | ||||||
/* | ||||||
Copyright 2025 The Kubernetes Authors. | ||||||
|
||||||
Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
you may not use this file except in compliance with the License. | ||||||
You may obtain a copy of the License at | ||||||
|
||||||
http://www.apache.org/licenses/LICENSE-2.0 | ||||||
|
||||||
Unless required by applicable law or agreed to in writing, software | ||||||
distributed under the License is distributed on an "AS IS" BASIS, | ||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
See the License for the specific language governing permissions and | ||||||
limitations under the License. | ||||||
*/ | ||||||
|
||||||
package cmd | ||||||
|
||||||
import ( | ||||||
"bytes" | ||||||
"fmt" | ||||||
"os" | ||||||
"os/exec" | ||||||
"path/filepath" | ||||||
"strconv" | ||||||
"testing" | ||||||
|
||||||
"github.com/Masterminds/goutils" | ||||||
. "github.com/onsi/gomega" | ||||||
) | ||||||
|
||||||
var baseArgs = []string{"clusterctl", "foo"} | ||||||
|
||||||
const ( | ||||||
forkEnvVar = "FORK" | ||||||
pluginCommandEnvVar = "PLUGIN_COMMAND" | ||||||
) | ||||||
|
||||||
func Test_plugin(t *testing.T) { | ||||||
if goutils.DefaultString(os.Getenv(forkEnvVar), "false") == strconv.FormatBool(false) { | ||||||
g := NewWithT(t) | ||||||
tt := []struct { | ||||||
name string | ||||||
command string | ||||||
expected string | ||||||
}{ | ||||||
{ | ||||||
name: "base plugin command test", | ||||||
expected: "I am a plugin named clusterctl-foo", | ||||||
}, | ||||||
{ | ||||||
name: "plugin version command test", | ||||||
command: "version", | ||||||
expected: "1.0.0", | ||||||
}, | ||||||
} | ||||||
|
||||||
tmpDir := os.TempDir() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not working in container environment due to limitations. It was my original approach. |
||||||
defer os.Remove(tmpDir) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same reason as above |
||||||
|
||||||
pluginPath := filepath.Join(tmpDir, "clusterctl-foo") | ||||||
path := os.Getenv("PATH") | ||||||
g.Expect(os.WriteFile(pluginPath, []byte(pluginCode), 0755)).To(Succeed()) //nolint:gosec | ||||||
|
||||||
for _, tc := range tt { | ||||||
t.Run(tc.name, func(t *testing.T) { | ||||||
g := NewWithT(t) | ||||||
stdout, _, err := runForkTest(t.Name(), fmt.Sprintf("%s=%s", pluginCommandEnvVar, tc.command), fmt.Sprintf("PATH=%s:%s", tmpDir, path)) | ||||||
g.Expect(err).NotTo(HaveOccurred()) | ||||||
g.Expect(stdout).To(ContainSubstring(tc.expected)) | ||||||
}) | ||||||
} | ||||||
|
||||||
return | ||||||
} | ||||||
|
||||||
os.Args = append(baseArgs, os.Getenv(pluginCommandEnvVar)) | ||||||
Execute() | ||||||
} | ||||||
|
||||||
var pluginCode = `#!/bin/bash | ||||||
|
||||||
# optional argument handling | ||||||
if [[ "$1" == "version" ]] | ||||||
then | ||||||
echo "1.0.0" | ||||||
exit 0 | ||||||
fi | ||||||
|
||||||
echo "I am a plugin named clusterctl-foo" | ||||||
` | ||||||
|
||||||
func runForkTest(testName string, options ...string) (string, string, error) { | ||||||
cmd := exec.Command(os.Args[0], "-test.run", testName) //nolint:gosec | ||||||
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%v", forkEnvVar, true)) | ||||||
cmd.Env = append(cmd.Env, options...) | ||||||
|
||||||
var stdoutB, stderrB bytes.Buffer | ||||||
cmd.Stdout = &stdoutB | ||||||
cmd.Stderr = &stderrB | ||||||
|
||||||
err := cmd.Run() | ||||||
|
||||||
return stdoutB.String(), stderrB.String(), err | ||||||
} |
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 do we need this check? I can't understand this situation.
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.
Please note that this code forks the test executable and runs it inside the test suite. The reason it uses this sub-hack is that the
syscall.Exec
does not return control back to the code after successful execution.