From 79c6f842568cc49e500cdf85b28a5c6ba0e8e280 Mon Sep 17 00:00:00 2001 From: Kumar Mallikarjuna Date: Tue, 3 Dec 2024 22:53:06 +0530 Subject: [PATCH] refactor: move program-loading to Step out of LoadPrograms() Signed-off-by: Kumar Mallikarjuna --- pkg/expressions/cel.go | 24 ++++++++++++------------ pkg/test/step.go | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pkg/expressions/cel.go b/pkg/expressions/cel.go index 65558281..e25a331c 100644 --- a/pkg/expressions/cel.go +++ b/pkg/expressions/cel.go @@ -7,7 +7,6 @@ import ( "github.com/google/cel-go/cel" harness "github.com/kudobuilder/kuttl/pkg/apis/testharness/v1beta1" - "github.com/kudobuilder/kuttl/pkg/test" ) func buildProgram(expr string, env *cel.Env) (cel.Program, error) { @@ -94,42 +93,43 @@ func RunAssertExpressions( return errs } -func LoadPrograms(s *test.Step) error { +func LoadPrograms(testAssert *harness.TestAssert) (map[string]cel.Program, error) { var errs []error - for _, resourceRef := range s.Assert.ResourceRefs { + for _, resourceRef := range testAssert.ResourceRefs { if err := resourceRef.Validate(); err != nil { errs = append(errs, fmt.Errorf("validation failed for reference '%v': %w", resourceRef.String(), err)) } } if len(errs) > 0 { - return fmt.Errorf("failed to load resource reference(s): %w", errors.Join(errs...)) + return nil, fmt.Errorf("failed to load resource reference(s): %w", errors.Join(errs...)) } var assertions []*harness.Assertion - assertions = append(assertions, s.Assert.AssertAny...) - assertions = append(assertions, s.Assert.AssertAll...) + assertions = append(assertions, testAssert.AssertAny...) + assertions = append(assertions, testAssert.AssertAll...) - env, err := buildEnv(s.Assert.ResourceRefs) + env, err := buildEnv(testAssert.ResourceRefs) if err != nil { - return fmt.Errorf("failed to build environment: %w", err) + return nil, fmt.Errorf("failed to build environment: %w", err) } + var programs map[string]cel.Program if len(assertions) > 0 { - s.Programs = make(map[string]cel.Program) + programs = make(map[string]cel.Program) } for _, assertion := range assertions { if prg, err := buildProgram(assertion.CELExpression, env); err != nil { errs = append(errs, err) } else { - s.Programs[assertion.CELExpression] = prg + programs[assertion.CELExpression] = prg } } if len(errs) > 0 { - return fmt.Errorf("failed to build program(s): %w", errors.Join(errs...)) + return nil, fmt.Errorf("failed to build program(s): %w", errors.Join(errs...)) } - return nil + return programs, nil } diff --git a/pkg/test/step.go b/pkg/test/step.go index 3797bca6..08db8231 100644 --- a/pkg/test/step.go +++ b/pkg/test/step.go @@ -567,7 +567,7 @@ func (s *Step) LoadYAML(file string) error { return fmt.Errorf("failed to load TestAssert object from %s: it contains an object of type %T", file, obj) } - err := expressions.LoadPrograms(s) + s.Programs, err = expressions.LoadPrograms(s.Assert) if err != nil { return fmt.Errorf("failed to load programs: %w", err) }