Skip to content

Commit 2aa9572

Browse files
authored
Add test runner library (#1149)
Test runner library for compiling and executing CEL unit tests with Bazel.
1 parent 13e5296 commit 2aa9572

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1369
-257
lines changed

WORKSPACE

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ go_repository(
101101
go_repository(
102102
name = "dev_cel_expr",
103103
importpath = "cel.dev/expr",
104-
sum = "h1:xoFEsNh972Yzey8N9TCPx2nDvMN7TMhQEzxLuj/iRrI=",
105-
version = "v0.22.1",
104+
sum = "h1:K4KOtPCJQjVggkARsjG9RWXP6O4R73aHeJMa/dmCQQg=",
105+
version = "v0.23.1",
106106
)
107107

108108
# local_repository(
@@ -153,7 +153,7 @@ go_repository(
153153
# of the above repositories but at different versions, so ours must come first.
154154
go_rules_dependencies()
155155

156-
go_register_toolchains(version = "1.21.1")
156+
go_register_toolchains(version = "1.22.0")
157157

158158
gazelle_dependencies()
159159

conformance/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/google/cel-go/conformance
33
go 1.22.0
44

55
require (
6-
cel.dev/expr v0.22.1
6+
cel.dev/expr v0.23.1
77
github.com/bazelbuild/rules_go v0.49.0
88
github.com/google/cel-go v0.21.0
99
github.com/google/go-cmp v0.6.0

conformance/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
cel.dev/expr v0.22.1 h1:xoFEsNh972Yzey8N9TCPx2nDvMN7TMhQEzxLuj/iRrI=
2-
cel.dev/expr v0.22.1/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
1+
cel.dev/expr v0.23.1 h1:K4KOtPCJQjVggkARsjG9RWXP6O4R73aHeJMa/dmCQQg=
2+
cel.dev/expr v0.23.1/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
33
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
44
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
55
github.com/bazelbuild/rules_go v0.49.0 h1:5vCbuvy8Q11g41lseGJDc5vxhDjJtfxr6nM/IC4VmqM=

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22.0
55
toolchain go1.23.0
66

77
require (
8-
cel.dev/expr v0.22.1
8+
cel.dev/expr v0.23.1
99
github.com/antlr4-go/antlr/v4 v4.13.0
1010
github.com/stoewer/go-strcase v1.2.0
1111
google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
cel.dev/expr v0.22.1 h1:xoFEsNh972Yzey8N9TCPx2nDvMN7TMhQEzxLuj/iRrI=
2-
cel.dev/expr v0.22.1/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
1+
cel.dev/expr v0.23.1 h1:K4KOtPCJQjVggkARsjG9RWXP6O4R73aHeJMa/dmCQQg=
2+
cel.dev/expr v0.23.1/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
33
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
44
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
55
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=

policy/BUILD.bazel

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ go_test(
6363
embed = [":go_default_library"],
6464
deps = [
6565
"//cel:go_default_library",
66+
"//test:go_default_library",
6667
"//common/types:go_default_library",
6768
"//interpreter:go_default_library",
6869
"//common/types/ref:go_default_library",
@@ -72,6 +73,6 @@ go_test(
7273
)
7374

7475
filegroup(
75-
name = "k8s_policy_testdata",
76-
srcs = glob(["testdata/k8s/*"]),
77-
)
76+
name = "testdata",
77+
srcs = glob(["testdata/**"]),
78+
)

policy/compiler_test.go

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -243,24 +243,25 @@ func (r *runner) run(t *testing.T) {
243243
input := map[string]any{}
244244
var err error
245245
var activation interpreter.Activation
246-
for k, v := range tc.Input {
247-
if v.Expr != "" {
248-
input[k] = r.eval(t, v.Expr)
249-
continue
246+
if tc.InputContext != nil && tc.InputContext.ContextExpr != "" {
247+
ctxExpr := tc.InputContext.ContextExpr
248+
ctx, err := r.eval(t, ctxExpr).ConvertToNative(
249+
reflect.TypeOf(((*proto.Message)(nil))).Elem())
250+
if err != nil {
251+
t.Fatalf("context variable is not a valid proto: %v", err)
250252
}
251-
if v.ContextExpr != "" {
252-
ctx, err := r.eval(t, v.ContextExpr).ConvertToNative(
253-
reflect.TypeOf(((*proto.Message)(nil))).Elem())
254-
if err != nil {
255-
t.Fatalf("context variable is not a valid proto: %v", err)
256-
}
257-
activation, err = cel.ContextProtoVars(ctx.(proto.Message))
258-
if err != nil {
259-
t.Fatalf("cel.ContextProtoVars() failed: %v", err)
253+
activation, err = cel.ContextProtoVars(ctx.(proto.Message))
254+
if err != nil {
255+
t.Fatalf("cel.ContextProtoVars() failed: %v", err)
256+
}
257+
} else if len(tc.Input) != 0 {
258+
for k, v := range tc.Input {
259+
if v.Expr != "" {
260+
input[k] = r.eval(t, v.Expr)
261+
continue
260262
}
261-
break
263+
input[k] = v.Value
262264
}
263-
input[k] = v.Value
264265
}
265266
if activation == nil {
266267
activation, err = interpreter.NewActivation(input)
@@ -272,7 +273,12 @@ func (r *runner) run(t *testing.T) {
272273
if err != nil {
273274
t.Fatalf("prg.Eval(input) failed: %v", err)
274275
}
275-
testOut := r.eval(t, tc.Output)
276+
var testOut ref.Val
277+
if tc.Output.Expr != "" {
278+
testOut = r.eval(t, tc.Output.Expr)
279+
} else if tc.Output.Value != nil {
280+
testOut = r.env.CELTypeAdapter().NativeToValue(tc.Output.Value)
281+
}
276282
if optOut, ok := out.(*types.Optional); ok {
277283
if optOut.Equal(types.OptionalNone) == types.True {
278284
if testOut.Equal(types.OptionalNone) != types.True {
@@ -299,24 +305,25 @@ func (r *runner) bench(b *testing.B) {
299305
input := map[string]any{}
300306
var err error
301307
var activation interpreter.Activation
302-
for k, v := range tc.Input {
303-
if v.Expr != "" {
304-
input[k] = r.eval(b, v.Expr)
305-
continue
308+
if tc.InputContext != nil && tc.InputContext.ContextExpr != "" {
309+
ctxExpr := tc.InputContext.ContextExpr
310+
ctx, err := r.eval(b, ctxExpr).ConvertToNative(
311+
reflect.TypeOf(((*proto.Message)(nil))).Elem())
312+
if err != nil {
313+
b.Fatalf("context variable is not a valid proto: %v", err)
306314
}
307-
if v.ContextExpr != "" {
308-
ctx, err := r.eval(b, v.ContextExpr).ConvertToNative(
309-
reflect.TypeOf(((*proto.Message)(nil))).Elem())
310-
if err != nil {
311-
b.Fatalf("context variable is not a valid proto: %v", err)
312-
}
313-
activation, err = cel.ContextProtoVars(ctx.(proto.Message))
314-
if err != nil {
315-
b.Fatalf("cel.ContextProtoVars() failed: %v", err)
315+
activation, err = cel.ContextProtoVars(ctx.(proto.Message))
316+
if err != nil {
317+
b.Fatalf("cel.ContextProtoVars() failed: %v", err)
318+
}
319+
} else if tc.Input != nil {
320+
for k, v := range tc.Input {
321+
if v.Expr != "" {
322+
input[k] = r.eval(b, v.Expr)
323+
continue
316324
}
317-
break
325+
input[k] = v.Value
318326
}
319-
input[k] = v.Value
320327
}
321328
if activation == nil {
322329
activation, err = interpreter.NewActivation(input)

policy/conformance.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package policy
1616

1717
// TestSuite describes a set of tests divided by section.
18+
//
19+
// Deprecated: Use google3/third_party/cel/go/test/suite.go instead.
1820
type TestSuite struct {
1921
Description string `yaml:"description"`
2022
Sections []*TestSection `yaml:"section"`

policy/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
)
1010

1111
require (
12-
cel.dev/expr v0.22.1 // indirect
12+
cel.dev/expr v0.23.1 // indirect
1313
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
1414
github.com/stoewer/go-strcase v1.3.0 // indirect
1515
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect

policy/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
cel.dev/expr v0.22.1 h1:xoFEsNh972Yzey8N9TCPx2nDvMN7TMhQEzxLuj/iRrI=
2-
cel.dev/expr v0.22.1/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
1+
cel.dev/expr v0.23.1 h1:K4KOtPCJQjVggkARsjG9RWXP6O4R73aHeJMa/dmCQQg=
2+
cel.dev/expr v0.23.1/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
33
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
44
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
55
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

0 commit comments

Comments
 (0)