diff --git a/pkg/expressions/cel.go b/pkg/expressions/cel.go index cd52fe7d..55fb23b7 100644 --- a/pkg/expressions/cel.go +++ b/pkg/expressions/cel.go @@ -1,6 +1,7 @@ package expressions import ( + "errors" "fmt" "github.com/google/cel-go/cel" @@ -37,3 +38,57 @@ func BuildEnv(resourceRefs []v1beta1.TestResourceRef) (*cel.Env, error) { return env, nil } + +// RunAssertExpressions evaluates a set of CEL expressions specified as AnyAllExpressions +func RunAssertExpressions( + programs map[string]cel.Program, + variables map[string]interface{}, + assertAny, + assertAll []*v1beta1.Assertion, +) []error { + var errs []error + if len(assertAny) == 0 && len(assertAll) == 0 { + return errs + } + + var anyExpressionsEvaluation, allExpressionsEvaluation []error + for _, expr := range assertAny { + prg, ok := programs[expr.CELExpression] + if !ok { + return []error{fmt.Errorf("couldn't find pre-built program for expression: %v", expr.CELExpression)} + } + out, _, err := prg.Eval(variables) + if err != nil { + return []error{fmt.Errorf("failed to evaluate program: %w", err)} + } + + if out.Value() != true { + anyExpressionsEvaluation = append(anyExpressionsEvaluation, fmt.Errorf("expression '%v' evaluated to '%v'", expr.CELExpression, out.Value())) + } + } + + for _, expr := range assertAll { + prg, ok := programs[expr.CELExpression] + if !ok { + return []error{fmt.Errorf("couldn't find pre-built program for expression: %v", expr.CELExpression)} + } + out, _, err := prg.Eval(variables) + if err != nil { + return []error{fmt.Errorf("failed to evaluate program: %w", err)} + } + + if out.Value() != true { + allExpressionsEvaluation = append(allExpressionsEvaluation, fmt.Errorf("expression '%v' evaluated to '%v'", expr.CELExpression, out.Value())) + } + } + + if len(assertAny) != 0 && len(anyExpressionsEvaluation) == len(assertAny) { + errs = append(errs, fmt.Errorf("no expression evaluated to true: %w", errors.Join(anyExpressionsEvaluation...))) + } + + if len(allExpressionsEvaluation) != len(assertAll) { + errs = append(errs, fmt.Errorf("not all expressions evaluated to true: %w", errors.Join(allExpressionsEvaluation...))) + } + + return errs +} diff --git a/pkg/test/step.go b/pkg/test/step.go index d8f2693a..1c44ffa4 100644 --- a/pkg/test/step.go +++ b/pkg/test/step.go @@ -441,7 +441,7 @@ func (s *Step) CheckAssertExpressions( variables[resourceRef.Ref] = referencedResource.Object } - return testutils.RunAssertExpressions(s.Programs, variables, assertAny, assertAll) + return expressions.RunAssertExpressions(s.Programs, variables, assertAny, assertAll) } // Check checks if the resources defined in Asserts and Errors are in the correct state. diff --git a/pkg/test/utils/kubernetes.go b/pkg/test/utils/kubernetes.go index 611f078c..4c703691 100644 --- a/pkg/test/utils/kubernetes.go +++ b/pkg/test/utils/kubernetes.go @@ -19,7 +19,6 @@ import ( "testing" "time" - "github.com/google/cel-go/cel" "github.com/google/shlex" "github.com/pmezard/go-difflib/difflib" "github.com/spf13/pflag" @@ -1218,60 +1217,6 @@ func RunAssertCommands(ctx context.Context, logger Logger, namespace string, com return RunCommands(ctx, logger, namespace, convertAssertCommand(commands, timeout), workdir, timeout, kubeconfigOverride) } -// RunAssertExpressions evaluates a set of CEL expressions specified as AnyAllExpressions -func RunAssertExpressions( - programs map[string]cel.Program, - variables map[string]interface{}, - assertAny, - assertAll []*harness.Assertion, -) []error { - var errs []error - if len(assertAny) == 0 && len(assertAll) == 0 { - return errs - } - - var anyExpressionsEvaluation, allExpressionsEvaluation []error - for _, expr := range assertAny { - prg, ok := programs[expr.CELExpression] - if !ok { - return []error{fmt.Errorf("couldn't find pre-built program for expression: %v", expr.CELExpression)} - } - out, _, err := prg.Eval(variables) - if err != nil { - return []error{fmt.Errorf("failed to evaluate program: %w", err)} - } - - if out.Value() != true { - anyExpressionsEvaluation = append(anyExpressionsEvaluation, fmt.Errorf("expression '%v' evaluated to '%v'", expr.CELExpression, out.Value())) - } - } - - for _, expr := range assertAll { - prg, ok := programs[expr.CELExpression] - if !ok { - return []error{fmt.Errorf("couldn't find pre-built program for expression: %v", expr.CELExpression)} - } - out, _, err := prg.Eval(variables) - if err != nil { - return []error{fmt.Errorf("failed to evaluate program: %w", err)} - } - - if out.Value() != true { - allExpressionsEvaluation = append(allExpressionsEvaluation, fmt.Errorf("expression '%v' evaluated to '%v'", expr.CELExpression, out.Value())) - } - } - - if len(assertAny) != 0 && len(anyExpressionsEvaluation) == len(assertAny) { - errs = append(errs, fmt.Errorf("no expression evaluated to true: %w", errors.Join(anyExpressionsEvaluation...))) - } - - if len(allExpressionsEvaluation) != len(assertAll) { - errs = append(errs, fmt.Errorf("not all expressions evaluated to true: %w", errors.Join(allExpressionsEvaluation...))) - } - - return errs -} - // RunCommands runs a set of commands, returning any errors. // If any (non-background) command fails, the following commands are skipped // commands running in the background are returned