From 7044f54fa38699affcf6d0867de507e4a99acae8 Mon Sep 17 00:00:00 2001 From: Kumar Mallikarjuna Date: Thu, 19 Dec 2024 12:30:24 +0530 Subject: [PATCH] fix: run target expressions test within a Kuttl owned ephemeral namespace Signed-off-by: Kumar Mallikarjuna --- pkg/test/expression_integration_test.go | 38 ++++++++++++++++--- pkg/test/step.go | 7 +++- .../00-create.yaml | 13 +++++++ .../01-assert.yaml | 10 +++++ 4 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 pkg/test/step_integration_test_data/assert_expressions/check_expression_for_ephemeral_namespace/00-create.yaml create mode 100644 pkg/test/step_integration_test_data/assert_expressions/check_expression_for_ephemeral_namespace/01-assert.yaml diff --git a/pkg/test/expression_integration_test.go b/pkg/test/expression_integration_test.go index 25c5098e..0857a199 100644 --- a/pkg/test/expression_integration_test.go +++ b/pkg/test/expression_integration_test.go @@ -6,6 +6,7 @@ import ( "context" "errors" "fmt" + "os" "strings" "testing" @@ -115,19 +116,44 @@ func TestAssertExpressions(t *testing.T) { runFailed: true, errorMessage: "no expression evaluated to true", }, + { + name: "check expression for ephemeral namespace", + }, } + const testNamespace = "kuttl-ephemeral-xyz" + assert.NoError(t, testenv.Client.Create(context.TODO(), &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: testNamespace, + }, + TypeMeta: metav1.TypeMeta{ + Kind: "Namespace", + }, + })) + for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - step := buildTestStep(t) - - fName := fmt.Sprintf( - "step_integration_test_data/assert_expressions/%s/00-assert.yaml", + dirName := fmt.Sprintf( + "step_integration_test_data/assert_expressions/%s", strings.ReplaceAll(tc.name, " ", "_"), ) + files, err := os.ReadDir(dirName) + assert.NoError(t, err) + + for i := 0; i < len(files)-1; i++ { + fName := fmt.Sprintf("%s/%s", dirName, files[i].Name()) + step := buildTestStep(t) + assert.NoError(t, step.LoadYAML(fName)) + assert.NoError(t, errors.Join(errors.Join(step.Run(t, testNamespace)...))) + } + + step := buildTestStep(t) + + fName := fmt.Sprintf("%s/%s", dirName, files[len(files)-1].Name()) + // Load test that has an invalid expression - err := step.LoadYAML(fName) + err = step.LoadYAML(fName) if !tc.loadingFailed { assert.NoError(t, err) } else { @@ -135,7 +161,7 @@ func TestAssertExpressions(t *testing.T) { return } - err = errors.Join(step.Run(t, "")...) + err = errors.Join(step.Run(t, testNamespace)...) if !tc.runFailed { assert.NoError(t, err) } else { diff --git a/pkg/test/step.go b/pkg/test/step.go index e56320e6..6ba9c0d5 100644 --- a/pkg/test/step.go +++ b/pkg/test/step.go @@ -416,7 +416,7 @@ func (s *Step) CheckAssertCommands(ctx context.Context, namespace string, comman return testErrors } -func (s *Step) CheckAssertExpressions() []error { +func (s *Step) CheckAssertExpressions(namespace string) []error { client, err := s.Client(false) if err != nil { return []error{err} @@ -424,6 +424,9 @@ func (s *Step) CheckAssertExpressions() []error { variables := make(map[string]interface{}) for _, resourceRef := range s.Assert.ResourceRefs { + if resourceRef.Namespace == "" { + resourceRef.Namespace = namespace + } namespacedName, referencedResource := resourceRef.BuildResourceReference() if err := client.Get(context.TODO(), namespacedName, referencedResource); err != nil { return []error{fmt.Errorf("failed to get referenced resource '%v': %w", namespacedName, err)} @@ -445,7 +448,7 @@ func (s *Step) Check(namespace string, timeout int) []error { if s.Assert != nil { testErrors = append(testErrors, s.CheckAssertCommands(context.TODO(), namespace, s.Assert.Commands, timeout)...) - testErrors = append(testErrors, s.CheckAssertExpressions()...) + testErrors = append(testErrors, s.CheckAssertExpressions(namespace)...) } for _, expected := range s.Errors { diff --git a/pkg/test/step_integration_test_data/assert_expressions/check_expression_for_ephemeral_namespace/00-create.yaml b/pkg/test/step_integration_test_data/assert_expressions/check_expression_for_ephemeral_namespace/00-create.yaml new file mode 100644 index 00000000..8f064b21 --- /dev/null +++ b/pkg/test/step_integration_test_data/assert_expressions/check_expression_for_ephemeral_namespace/00-create.yaml @@ -0,0 +1,13 @@ +--- +apiVersion: v1 +kind: Pod +metadata: + name: nginx-pod + labels: + app: nginx +spec: + containers: + - name: nginx-container + image: nginx:latest + ports: + - containerPort: 80 diff --git a/pkg/test/step_integration_test_data/assert_expressions/check_expression_for_ephemeral_namespace/01-assert.yaml b/pkg/test/step_integration_test_data/assert_expressions/check_expression_for_ephemeral_namespace/01-assert.yaml new file mode 100644 index 00000000..fd6fb16a --- /dev/null +++ b/pkg/test/step_integration_test_data/assert_expressions/check_expression_for_ephemeral_namespace/01-assert.yaml @@ -0,0 +1,10 @@ +apiVersion: kuttl.dev/v1beta1 +kind: TestAssert +resourceRefs: + - apiVersion: v1 + kind: Pod + name: nginx-pod + ref: nginxPod +assertAll: + - celExpr: "nginxPod.metadata.name == 'nginx-pod'" +timeout: 1