Skip to content

Commit

Permalink
chore: move RunAssertExpressions() to pkg/expressions
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 212b993 commit 32ec715
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 56 deletions.
55 changes: 55 additions & 0 deletions pkg/expressions/cel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package expressions

import (
"errors"
"fmt"

"github.com/google/cel-go/cel"
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
55 changes: 0 additions & 55 deletions pkg/test/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 32ec715

Please sign in to comment.