diff --git a/pkg/test/case.go b/pkg/test/case.go index 97814b0d..d0b572bc 100644 --- a/pkg/test/case.go +++ b/pkg/test/case.go @@ -386,6 +386,10 @@ func (t *Case) determineNamespace() *namespace { ns.Name = fmt.Sprintf("kuttl-test-%s", petname.Generate(2, "-")) ns.AutoCreated = true } + + if _, ok := os.LookupEnv("NAMESPACE"); !ok { // user can supply this environment variable + os.Setenv("NAMESPACE", t.PreferredNamespace) // set this environment variable so it can be templated by other 'steps' + } // if we have a preferred namespace, we do NOT auto-create return ns } diff --git a/pkg/test/utils/kubernetes.go b/pkg/test/utils/kubernetes.go index 38603ba1..668f5eae 100644 --- a/pkg/test/utils/kubernetes.go +++ b/pkg/test/utils/kubernetes.go @@ -508,6 +508,9 @@ func LoadYAML(path string, r io.Reader) ([]client.Object, error) { return nil, fmt.Errorf("error reading yaml %s: %w", path, err) } + // replace all variables from the environment + data = []byte(os.ExpandEnv(string(data))) + unstructuredObj := &unstructured.Unstructured{} decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(data), len(data)) diff --git a/pkg/test/utils/kubernetes_test.go b/pkg/test/utils/kubernetes_test.go index 2cb1bdb1..6b04aa1a 100644 --- a/pkg/test/utils/kubernetes_test.go +++ b/pkg/test/utils/kubernetes_test.go @@ -5,6 +5,7 @@ import ( "context" "errors" "os" + "strconv" "testing" "time" @@ -168,7 +169,8 @@ func TestRetryWithTimeout(t *testing.T) { } func TestLoadYAML(t *testing.T) { - tmpfile, err := os.CreateTemp("", "test.yaml") + tmpDir := t.TempDir() + tmpfile, err := os.CreateTemp(tmpDir, "test.yaml") assert.Nil(t, err) defer tmpfile.Close() @@ -182,6 +184,11 @@ spec: containers: - name: nginx image: nginx:1.7.9 + - name: app + image: app:$APP_VERSION + resources: + requests: + cpu: ${CPU_COUNT} --- apiVersion: v1 kind: Pod @@ -193,11 +200,36 @@ spec: containers: - name: nginx image: nginx:1.7.9 +--- +apiVersion: $GROUP_VERSION +kind: $KIND +metadata: + name: hello +spec: + config: + $KEY_VAR: bar `), 0600) if err != nil { t.Fatal(err) } + appVersion := "0.0.1-alpha" + t.Setenv("APP_VERSION", appVersion) + // test replacing a different type (int64 in this case) + // and nested values + cpuCount := int64(2) + t.Setenv("CPU_COUNT", strconv.Itoa(int(cpuCount))) + + // test GVK + groupVersion := "example.com/v1" + t.Setenv("GROUP_VERSION", groupVersion) + kind := "custom" + t.Setenv("KIND", kind) + + // key update + keyVar := "baz" + t.Setenv("KEY_VAR", keyVar) + objs, err := LoadYAMLFromFile(tmpfile.Name()) assert.Nil(t, err) @@ -216,6 +248,15 @@ spec: "image": "nginx:1.7.9", "name": "nginx", }, + map[string]interface{}{ + "name": "app", + "image": "app:" + appVersion, + "resources": map[string]interface{}{ + "requests": map[string]interface{}{ + "cpu": cpuCount, + }, + }, + }, }, }, }, @@ -241,10 +282,26 @@ spec: }, }, }, objs[1]) + + assert.Equal(t, &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": groupVersion, + "kind": kind, + "metadata": map[string]interface{}{ + "name": "hello", + }, + "spec": map[string]interface{}{ + "config": map[string]interface{}{ + keyVar: "bar", + }, + }, + }, + }, objs[2]) } func TestMatchesKind(t *testing.T) { - tmpfile, err := os.CreateTemp("", "test.yaml") + tmpDir := t.TempDir() + tmpfile, err := os.CreateTemp(tmpDir, "test.yaml") assert.Nil(t, err) defer tmpfile.Close() diff --git a/tools.go b/tools.go index 655b4e6b..51f74411 100644 --- a/tools.go +++ b/tools.go @@ -1,3 +1,4 @@ +//go:build tools // +build tools // Package tools is used to import go modules that we use for tooling as dependencies.