diff --git a/cmd/flux/check_test.go b/cmd/flux/check_test.go new file mode 100644 index 00000000..913bab0a --- /dev/null +++ b/cmd/flux/check_test.go @@ -0,0 +1,40 @@ +// +build e2e + +package main + +import ( + "context" + "encoding/json" + "strings" + "testing" + + "github.com/fluxcd/flux2/internal/utils" + "k8s.io/apimachinery/pkg/version" +) + +func TestCheckPre(t *testing.T) { + jsonOutput, err := utils.ExecKubectlCommand(context.TODO(), utils.ModeCapture, rootArgs.kubeconfig, rootArgs.kubecontext, "version", "--output", "json") + if err != nil { + t.Fatalf("Error running utils.ExecKubectlCommand: %v", err.Error()) + } + + var versions map[string]version.Info + if err := json.Unmarshal([]byte(jsonOutput), &versions); err != nil { + t.Fatalf("Error unmarshalling: %v", err.Error()) + } + + clientVersion := strings.TrimPrefix(versions["clientVersion"].GitVersion, "v") + serverVersion := strings.TrimPrefix(versions["serverVersion"].GitVersion, "v") + + cmd := cmdTestCase{ + args: "check --pre", + wantError: false, + testClusterMode: ExistingClusterMode, + templateValues: map[string]string{ + "clientVersion": clientVersion, + "serverVersion": serverVersion, + }, + goldenFile: "testdata/check/check_pre.golden", + } + cmd.runTestCmd(t) +} diff --git a/cmd/flux/main_test.go b/cmd/flux/main_test.go index c7cd3627..0f4df3b7 100644 --- a/cmd/flux/main_test.go +++ b/cmd/flux/main_test.go @@ -10,6 +10,7 @@ import ( "os" "path/filepath" "testing" + "text/template" "time" "github.com/fluxcd/flux2/internal/utils" @@ -169,6 +170,8 @@ type cmdTestCase struct { objectFile string // TestClusterMode to bootstrap and testing, default to Fake testClusterMode TestClusterMode + // TemplateValues enable template preprocessing for the golden file, or golden value + templateValues map[string]string } func (cmd *cmdTestCase) runTestCmd(t *testing.T) { @@ -203,14 +206,28 @@ func (cmd *cmdTestCase) runTestCmd(t *testing.T) { var expected string if cmd.goldenValue != "" { - expected = cmd.goldenValue + if cmd.templateValues != nil { + expected, err = executeGoldenTemplate(cmd.goldenValue, cmd.templateValues) + if err != nil { + t.Fatalf("Error executing golden template: %s", err) + } + } else { + expected = cmd.goldenValue + } } if cmd.goldenFile != "" { - expectedOutput, err := os.ReadFile(cmd.goldenFile) + goldenFileContent, err := os.ReadFile(cmd.goldenFile) if err != nil { t.Fatalf("Error reading golden file: '%s'", err) } - expected = string(expectedOutput) + if cmd.templateValues != nil { + expected, err = executeGoldenTemplate(string(goldenFileContent), cmd.templateValues) + if err != nil { + t.Fatalf("Error executing golden template file '%s': %s", cmd.goldenFile, err) + } + } else { + expected = string(goldenFileContent) + } } diff := cmp.Diff(expected, actual) @@ -219,6 +236,15 @@ func (cmd *cmdTestCase) runTestCmd(t *testing.T) { } } +func executeGoldenTemplate(goldenValue string, templateValues map[string]string) (string, error) { + tmpl := template.Must(template.New("golden").Parse(goldenValue)) + var out bytes.Buffer + if err := tmpl.Execute(&out, templateValues); err != nil { + return "", err + } + return out.String(), nil +} + // Run the command and return the captured output. func executeCommand(cmd string) (string, error) { args, err := shellwords.Parse(cmd) diff --git a/cmd/flux/testdata/check/check_pre.golden b/cmd/flux/testdata/check/check_pre.golden new file mode 100644 index 00000000..42b7acad --- /dev/null +++ b/cmd/flux/testdata/check/check_pre.golden @@ -0,0 +1,4 @@ +► checking prerequisites +✔ kubectl {{ .clientVersion }} >=1.18.0-0 +✔ Kubernetes {{ .serverVersion }} >=1.16.0-0 +✔ prerequisites checks passed