diff --git a/pkg/test/step.go b/pkg/test/step.go index 2c0b605d..0d5f4f1f 100644 --- a/pkg/test/step.go +++ b/pkg/test/step.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "os" "path/filepath" "regexp" "strings" @@ -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) } @@ -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 +} diff --git a/pkg/test/step_integration_test.go b/pkg/test/step_integration_test.go index 837377d2..46d5901a 100644 --- a/pkg/test/step_integration_test.go +++ b/pkg/test/step_integration_test.go @@ -4,7 +4,9 @@ package test import ( "fmt" + "io/ioutil" "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -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)) } diff --git a/pkg/test/step_integration_test_data/kubeconfig_path_resolution/00-step1.yaml b/pkg/test/step_integration_test_data/kubeconfig_path_resolution/00-step1.yaml index 5664a541..13d9bf92 100644 --- a/pkg/test/step_integration_test_data/kubeconfig_path_resolution/00-step1.yaml +++ b/pkg/test/step_integration_test_data/kubeconfig_path_resolution/00-step1.yaml @@ -1,4 +1,3 @@ apiVersion: kuttl.dev/v1beta1 kind: TestStep kubeconfig: ./kubeconfig-${SUBPATH}.yaml -timeout: 30 diff --git a/pkg/test/step_integration_test_data/kubeconfig_path_resolution/00-step2.yaml b/pkg/test/step_integration_test_data/kubeconfig_path_resolution/00-step2.yaml index 3371ac65..64f405b7 100644 --- a/pkg/test/step_integration_test_data/kubeconfig_path_resolution/00-step2.yaml +++ b/pkg/test/step_integration_test_data/kubeconfig_path_resolution/00-step2.yaml @@ -1,4 +1,3 @@ apiVersion: kuttl.dev/v1beta1 kind: TestStep kubeconfig: /absolute/kubeconfig-${SUBPATH}.yaml -timeout: 30 diff --git a/pkg/test/step_integration_test_data/two_step/00-step1.yaml b/pkg/test/step_integration_test_data/two_step/00-step1.yaml index ca22f3d2..c0a66de9 100644 --- a/pkg/test/step_integration_test_data/two_step/00-step1.yaml +++ b/pkg/test/step_integration_test_data/two_step/00-step1.yaml @@ -1,3 +1,2 @@ apiVersion: kuttl.dev/v1beta1 -kind: TestStep -timeout: 30 \ No newline at end of file +kind: TestStep \ No newline at end of file diff --git a/pkg/test/step_integration_test_data/two_step/00-step2.yaml b/pkg/test/step_integration_test_data/two_step/00-step2.yaml index ca22f3d2..c0a66de9 100644 --- a/pkg/test/step_integration_test_data/two_step/00-step2.yaml +++ b/pkg/test/step_integration_test_data/two_step/00-step2.yaml @@ -1,3 +1,2 @@ apiVersion: kuttl.dev/v1beta1 -kind: TestStep -timeout: 30 \ No newline at end of file +kind: TestStep \ No newline at end of file diff --git a/pkg/test/step_integration_test_data/two_step/01-step1.yaml b/pkg/test/step_integration_test_data/two_step/01-step1.yaml index e9116f05..72cec77e 100644 --- a/pkg/test/step_integration_test_data/two_step/01-step1.yaml +++ b/pkg/test/step_integration_test_data/two_step/01-step1.yaml @@ -1,8 +1,6 @@ apiVersion: kuttl.dev/v1beta1 kind: TestStep -timeout: 30 ---- apiVersion: kuttl.dev/v1beta1 -kind: TestStep -timeout: 30 \ No newline at end of file +kind: TestStep \ No newline at end of file diff --git a/pkg/test/test_data/teststep-assert/00-create.yaml b/pkg/test/test_data/teststep-assert/00-create.yaml index 3137032c..0eb60fe0 100644 --- a/pkg/test/test_data/teststep-assert/00-create.yaml +++ b/pkg/test/test_data/teststep-assert/00-create.yaml @@ -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 \ No newline at end of file diff --git a/pkg/test/test_data/teststep-assert/hello1-pod.yaml b/pkg/test/test_data/teststep-assert/hello1-pod.yaml new file mode 100644 index 00000000..df6b4ec4 --- /dev/null +++ b/pkg/test/test_data/teststep-assert/hello1-pod.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: Pod +metadata: + name: hello +spec: + containers: + - image: alpine + name: test \ No newline at end of file diff --git a/pkg/test/test_data/teststep-assert/hello2/hello2-pod.yaml b/pkg/test/test_data/teststep-assert/hello2/hello2-pod.yaml new file mode 100644 index 00000000..2394699d --- /dev/null +++ b/pkg/test/test_data/teststep-assert/hello2/hello2-pod.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: Pod +metadata: + name: hello2 +spec: + containers: + - image: alpine + name: test \ No newline at end of file diff --git a/pkg/test/utils/kubernetes.go b/pkg/test/utils/kubernetes.go index 26762f74..9a944c61 100644 --- a/pkg/test/utils/kubernetes.go +++ b/pkg/test/utils/kubernetes.go @@ -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) }