Skip to content

Commit

Permalink
fix: run target expressions test within a Kuttl owned ephemeral names…
Browse files Browse the repository at this point in the history
…pace

Signed-off-by: Kumar Mallikarjuna <[email protected]>
  • Loading branch information
kumar-mallikarjuna committed Dec 19, 2024
1 parent ffc298b commit 7044f54
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
38 changes: 32 additions & 6 deletions pkg/test/expression_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"errors"
"fmt"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -115,27 +116,52 @@ 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 {
assert.ErrorContains(t, err, tc.errorMessage)
return
}

err = errors.Join(step.Run(t, "")...)
err = errors.Join(step.Run(t, testNamespace)...)
if !tc.runFailed {
assert.NoError(t, err)
} else {
Expand Down
7 changes: 5 additions & 2 deletions pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,17 @@ 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}
}

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)}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7044f54

Please sign in to comment.