Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

[Enhancement] YAML validation of a TestStep file #27

Merged
merged 16 commits into from
Sep 14, 2023
31 changes: 31 additions & 0 deletions pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -561,6 +562,10 @@ func (s *Step) LoadYAML(file string) error {
for _, apply := range s.Apply {
if apply.object.GetObjectKind().GroupVersionKind().Kind == "TestStep" {
if testStep, ok := apply.object.(*harness.TestStep); ok {
// Validate TestStep
if err := validateTestStep(testStep, s.Dir); err != nil {
return fmt.Errorf("failed to validate TestStep object from %s: %v", file, err)
}
if s.Step != nil {
return fmt.Errorf("more than 1 TestStep not allowed in step %q", s.Name)
}
Expand Down Expand Up @@ -688,3 +693,29 @@ func hasTimeoutErr(err []error) bool {
}
return false
}

func validateTestStep(ts *harness.TestStep, baseDir string) error {
// Check if referenced files in Apply exist
for _, apply := range ts.Apply {
path := filepath.Join(baseDir, apply.File)
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("referenced file in Apply does not exist: %s", path)
}
}
// Check if referenced files in Assert exist
for _, assert := range ts.Assert {
path := filepath.Join(baseDir, assert)
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("referenced file in Assert does not exist: %s", path)
}
}
// Check if referenced files in Error exist
for _, errorPath := range ts.Error {
path := filepath.Join(baseDir, errorPath)
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("referenced file in Error does not exist: %s", path)
}
}

return nil
}
24 changes: 21 additions & 3 deletions pkg/test/step_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -337,10 +339,26 @@ func TestApplyExpansion(t *testing.T) {
t.Cleanup(func() {
os.Unsetenv("TEST_FOO")
})
dirPath := filepath.Join(t.TempDir(), "step_integration_test_data/assert_expand")
err := os.MkdirAll(dirPath, 0755)
assert.NoError(t, err)
yamlFilePath := filepath.Join(dirPath, "00-step1.yaml")

data := `
apiVersion: v1
kind: Pod
metadata:
name: hello
spec:
containers:
- image: alpine
name: test
`
err = ioutil.WriteFile(yamlFilePath, []byte(data), 0644)
assert.NoError(t, err)

step := Step{Dir: "step_integration_test_data/assert_expand/"}
path := "step_integration_test_data/assert_expand/00-step1.yaml"
err := step.LoadYAML(path)
step := Step{Dir: dirPath}
err = step.LoadYAML(yamlFilePath)
assert.NoError(t, err)
assert.Equal(t, 1, len(step.Apply))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
kubeconfig: ./kubeconfig-${SUBPATH}.yaml
timeout: 30
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
kubeconfig: /absolute/kubeconfig-${SUBPATH}.yaml
timeout: 30
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
timeout: 30
kind: TestStep
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
timeout: 30
kind: TestStep
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
timeout: 30

----
apiVersion: kuttl.dev/v1beta1
kind: TestStep
timeout: 30
kind: TestStep
21 changes: 3 additions & 18 deletions pkg/test/test_data/teststep-assert/00-create.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
apiVersion: v1
kind: Pod
metadata:
name: hello2
spec:
containers:
- image: alpine
name: test
---
apiVersion: v1
kind: Pod
metadata:
name: hello
spec:
containers:
- image: alpine
name: test
---
# using assert from TestStep from multiple folder locations
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- hello1-pod.yaml
- hello2/hello2-pod.yaml
assert:
- hello1-assert.yaml
- hello2/hello2-assert.yaml
8 changes: 8 additions & 0 deletions pkg/test/test_data/teststep-assert/hello1-pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Pod
metadata:
name: hello
spec:
containers:
- image: alpine
name: test
8 changes: 8 additions & 0 deletions pkg/test/test_data/teststep-assert/hello2/hello2-pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Pod
metadata:
name: hello2
spec:
containers:
- image: alpine
name: test
2 changes: 1 addition & 1 deletion pkg/test/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func ConvertUnstructured(in client.Object) (client.Object, error) {
return in, nil
}

err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstruct, converted)
err = runtime.DefaultUnstructuredConverter.FromUnstructuredWithValidation(unstruct, converted, true)
if err != nil {
return nil, fmt.Errorf("error converting %s from unstructured error: %w", ResourceID(in), err)
}
Expand Down