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 a smoke test for clusterctl plugins #11950

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions cmd/clusterctl/cmd/plugin_test.go
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) {
Copy link
Member

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.

Copy link
Member Author

@dmvolod dmvolod Mar 11, 2025

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.

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

Choose a reason for hiding this comment

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

Suggested change
tmpDir := os.TempDir()
tmpDir := t.TempDir()

Copy link
Member Author

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Suggested change
defer os.Remove(tmpDir)

Copy link
Member Author

Choose a reason for hiding this comment

The 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
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -54,10 +54,11 @@ require (
sigs.k8s.io/yaml v1.4.0
)

require github.com/Masterminds/goutils v1.1.1

require (
cel.dev/expr v0.18.0 // indirect
dario.cat/mergo v1.0.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.3.0 // indirect
github.com/NYTimes/gziphandler v1.1.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect