| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package celtest |
| 17 |
|
| 18 |
import ( |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/google/cel-go/cel" |
| 22 |
"github.com/google/cel-go/common/ast" |
| 23 |
"github.com/google/cel-go/tools/compiler" |
| 24 |
) |
| 25 |
|
| 26 |
func TestCoverageStats(t *testing.T) { |
| 27 |
testCases := []struct { |
| 28 |
name string |
| 29 |
configPath string |
| 30 |
testSuitePath string |
| 31 |
celExpr string |
| 32 |
compilerOpts []any |
| 33 |
totalASTNodes int64 |
| 34 |
coveredBranchNodesComplete []int64 |
| 35 |
coveredBranchNodesPartial []int64 |
| 36 |
uncoveredNodes []int64 |
| 37 |
}{ |
| 38 |
{ |
| 39 |
name: "restricted destinations policy coverage", |
| 40 |
configPath: "../../policy/testdata/restricted_destinations/config.yaml", |
| 41 |
testSuitePath: "../../policy/testdata/restricted_destinations/tests.yaml", |
| 42 |
celExpr: "../../policy/testdata/restricted_destinations/policy.yaml", |
| 43 |
compilerOpts: []any{locationCodeEnvOption()}, |
| 44 |
totalASTNodes: 44, |
| 45 |
coveredBranchNodesComplete: []int64{1, 7, 11, 12, 19, 23, 28, 29, 30, 31, 32, 33, 34, 36, 37, 38, 39, 40, 42}, |
| 46 |
coveredBranchNodesPartial: []int64{3, 13, 41}, |
| 47 |
uncoveredNodes: []int64{}, |
| 48 |
}, |
| 49 |
{ |
| 50 |
name: "k8s policy coverage", |
| 51 |
configPath: "../../policy/testdata/k8s/config.yaml", |
| 52 |
testSuitePath: "../../policy/testdata/k8s/tests.yaml", |
| 53 |
celExpr: "../../policy/testdata/k8s/policy.yaml", |
| 54 |
compilerOpts: []any{k8sParserOpts()}, |
| 55 |
totalASTNodes: 38, |
| 56 |
coveredBranchNodesComplete: []int64{}, |
| 57 |
coveredBranchNodesPartial: []int64{8, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 31}, |
| 58 |
uncoveredNodes: []int64{5, 6, 7, 11, 12, 38}, |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "policy with custom policy metadata coverage", |
| 62 |
testSuitePath: "../../tools/celtest/testdata/custom_policy_tests.yaml", |
| 63 |
celExpr: "../../tools/celtest/testdata/custom_policy.celpolicy", |
| 64 |
compilerOpts: []any{customPolicyParserOption(), compiler.PolicyMetadataEnvOption(ParsePolicyVariables)}, |
| 65 |
totalASTNodes: 10, |
| 66 |
coveredBranchNodesComplete: []int64{1, 2, 3, 6}, |
| 67 |
coveredBranchNodesPartial: []int64{}, |
| 68 |
uncoveredNodes: []int64{}, |
| 69 |
}, |
| 70 |
{ |
| 71 |
name: "raw expression file with unknowns test coverage", |
| 72 |
configPath: "../../tools/celtest/testdata/config.yaml", |
| 73 |
testSuitePath: "../../tools/celtest/testdata/raw_expr_tests.yaml", |
| 74 |
celExpr: "../../tools/celtest/testdata/raw_expr.cel", |
| 75 |
compilerOpts: []any{fnEnvOption()}, |
| 76 |
totalASTNodes: 8, |
| 77 |
coveredBranchNodesComplete: []int64{1, 6, 8}, |
| 78 |
coveredBranchNodesPartial: []int64{}, |
| 79 |
uncoveredNodes: []int64{}, |
| 80 |
}, |
| 81 |
} |
| 82 |
for _, tc := range testCases { |
| 83 |
t.Run(tc.name, func(t *testing.T) { |
| 84 |
tc.compilerOpts = append(tc.compilerOpts, cel.EnableMacroCallTracking()) |
| 85 |
if tc.configPath != "" { |
| 86 |
envOpt := compiler.EnvironmentFile(tc.configPath) |
| 87 |
tc.compilerOpts = append(tc.compilerOpts, envOpt) |
| 88 |
} |
| 89 |
compilerOpt := TestCompiler(tc.compilerOpts...) |
| 90 |
testSuiteParserOpt := DefaultTestSuiteParser(tc.testSuitePath) |
| 91 |
testExprOpt := TestExpression(tc.celExpr) |
| 92 |
tr, err := NewTestRunner(compilerOpt, testSuiteParserOpt, testExprOpt, EnableCoverage(), PartialEvalProgramOption()) |
| 93 |
if err != nil { |
| 94 |
t.Fatalf("compiler.NewCompiler() failed: %v", err) |
| 95 |
} |
| 96 |
programs, err := tr.Programs(t, tr.testProgramOptions...) |
| 97 |
if err != nil { |
| 98 |
t.Fatalf("error creating programs: %v", err) |
| 99 |
} |
| 100 |
tests, err := tr.Tests(t) |
| 101 |
if err != nil { |
| 102 |
t.Fatalf("error creating tests: %v", err) |
| 103 |
} |
| 104 |
for _, test := range tests { |
| 105 |
err := tr.ExecuteTest(t, programs, test) |
| 106 |
if err != nil { |
| 107 |
t.Fatalf("error executing test: %v", err) |
| 108 |
} |
| 109 |
} |
| 110 |
p := programs[0] |
| 111 |
rootNavigableExpr := ast.NavigateAST(p.Ast.NativeRep()) |
| 112 |
cr := &coverageReport{ |
| 113 |
nodes: 0, |
| 114 |
coveredNodes: 0, |
| 115 |
branches: 0, |
| 116 |
coveredBooleanOutcomes: 0, |
| 117 |
unencounteredNodes: []string{}, |
| 118 |
unencounteredBranches: []string{}, |
| 119 |
} |
| 120 |
traverseAndCalculateCoverage(t, rootNavigableExpr, p, true, "", cr) |
| 121 |
coverageStats := p.CoverageStats |
| 122 |
if cr.nodes != tc.totalASTNodes { |
| 123 |
t.Errorf("totalASTNodes = %d, want %d", cr.nodes, tc.totalASTNodes) |
| 124 |
} |
| 125 |
for _, id := range tc.coveredBranchNodesComplete { |
| 126 |
if val, ok := coverageStats[id]; !ok || len(val) < 2 { |
| 127 |
t.Errorf("id %d is not covered completely", id) |
| 128 |
} |
| 129 |
} |
| 130 |
for _, id := range tc.coveredBranchNodesPartial { |
| 131 |
if val, ok := coverageStats[id]; !ok || len(val) != 1 { |
| 132 |
t.Errorf("id %d is not covered partially", id) |
| 133 |
} |
| 134 |
} |
| 135 |
for _, id := range tc.uncoveredNodes { |
| 136 |
if _, ok := coverageStats[id]; ok { |
| 137 |
t.Errorf("id %d is covered, expected uncoveredNodes", id) |
| 138 |
} |
| 139 |
} |
| 140 |
}) |
| 141 |
} |
| 142 |
} |