Skip to content

Commit

Permalink
chore: incorporate review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Kumar Mallikarjuna <[email protected]>
  • Loading branch information
kumar-mallikarjuna committed Dec 19, 2024
1 parent a7122a6 commit 1752619
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
25 changes: 12 additions & 13 deletions pkg/expressions/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,25 @@ func RunAssertExpressions(
return errs
}

var anyExpressionsEvaluation, allExpressionsEvaluation []error
var anyExprErrors, allExprErrors []error
for _, expr := range assertAny {
if err := evaluateExpression(expr.CELExpression, programs, variables); err != nil {
anyExpressionsEvaluation = append(anyExpressionsEvaluation, err)
anyExprErrors = append(anyExprErrors, err)
}
}

for _, expr := range assertAll {
if err := evaluateExpression(expr.CELExpression, programs, variables); err != nil {
allExpressionsEvaluation = append(allExpressionsEvaluation, err)
allExprErrors = append(allExprErrors, err)
}
}

if len(assertAny) != 0 && len(anyExpressionsEvaluation) == len(assertAny) {
errs = append(errs, fmt.Errorf("no expression evaluated to true: %w", errors.Join(anyExpressionsEvaluation...)))
if len(assertAny) != 0 && len(anyExprErrors) == len(assertAny) {
errs = append(errs, fmt.Errorf("no expression evaluated to true: %w", errors.Join(anyExprErrors...)))
}

if len(allExpressionsEvaluation) > 0 {
errs = append(errs, fmt.Errorf("not all expressions evaluated to true: %w", errors.Join(allExpressionsEvaluation...)))
if len(allExprErrors) > 0 {
errs = append(errs, fmt.Errorf("not all assertAll evaluated to true: %w", errors.Join(allExprErrors...)))
}

return errs
Expand All @@ -94,28 +94,27 @@ func LoadPrograms(testAssert *harness.TestAssert) (map[string]cel.Program, error

env, err := buildEnv(testAssert.ResourceRefs)
if err != nil {
return nil, fmt.Errorf("failed to build environment: %w", err)
return nil, fmt.Errorf("failed to build CEL environment: %w", err)
}

var programs map[string]cel.Program
if len(assertions) == 0 {
return programs, nil
return nil, nil
}
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,
fmt.Errorf("failed to parse CEL expression '%v': %w", assertion.CELExpression, err),
fmt.Errorf("failed to build CEL program from expression %q: %w", assertion.CELExpression, err),
)
} else {
programs[assertion.CELExpression] = prg
}
}

if len(errs) > 0 {
return nil, fmt.Errorf("failed to parse expression(s): %w", errors.Join(errs...))
return nil, fmt.Errorf("failed to load expression(s): %w", errors.Join(errs...))
}

return programs, nil
Expand Down
9 changes: 4 additions & 5 deletions pkg/test/expression_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ func TestAssertExpressions(t *testing.T) {
assert.NoError(t, testenv.Client.Create(context.TODO(), metricServerPod))

testCases := []struct {
name string
loadingFailed bool
runFailed bool
errorMessage string
name string
expectLoadFailure bool
expectRunFailure bool
expectedErrorMessage string
}{
{
name: "invalid expression",
Expand Down Expand Up @@ -152,7 +152,6 @@ func TestAssertExpressions(t *testing.T) {

fName := fmt.Sprintf("%s/%s", dirName, files[len(files)-1].Name())

// Load test that has an invalid expression
err = step.LoadYAML(fName)
if !tc.loadingFailed {
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func (s *Step) LoadYAML(file string) error {

s.Programs, err = expressions.LoadPrograms(s.Assert)
if err != nil {
return fmt.Errorf("failed to load programs: %w", err)
return fmt.Errorf("failed to prepare expression evaluation: %w", err)
}
} else {
asserts = append(asserts, obj)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
apiVersion: v1
kind: Pod
metadata:
Expand Down

0 comments on commit 1752619

Please sign in to comment.