Skip to content

Commit

Permalink
refactor: move program-loading to Step out of LoadPrograms()
Browse files Browse the repository at this point in the history
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
  • Loading branch information
kumar-mallikarjuna committed Dec 3, 2024
1 parent 65dd813 commit 79c6f84
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions pkg/expressions/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 79c6f84

Please sign in to comment.