Merge pull request #1719 from chanwit/e2e_check_pre

Add e2e test case fo check --pre
pull/1727/head
Stefan Prodan 3 years ago committed by GitHub
commit 103ed2be65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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)
}

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

@ -0,0 +1,4 @@
► checking prerequisites
✔ kubectl {{ .clientVersion }} >=1.18.0-0
✔ Kubernetes {{ .serverVersion }} >=1.16.0-0
✔ prerequisites checks passed
Loading…
Cancel
Save