Skip to content

Add test runner library #1149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 30 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a30d67d
Add k8s custom policy tag handler for test
aakash070 Feb 4, 2025
77aab71
added copyright and removed redundant attribute from go_library target
aakash070 Feb 5, 2025
9e155af
refactor code to extract common methods
aakash070 Mar 10, 2025
73bba9e
reverted redundant code
aakash070 Mar 11, 2025
7afe78c
added compiler tool
aakash070 Mar 12, 2025
d261979
replaced textproto file with an in memory pb object
aakash070 Mar 21, 2025
6cb138d
reverted update to root go.mod
aakash070 Mar 25, 2025
5cab162
added cel.dev/expr/conformance to vendor
aakash070 Mar 25, 2025
907b6c3
add vendor to tools
aakash070 Mar 25, 2025
09f6866
deleted tools/vendor
aakash070 Mar 25, 2025
bf7624c
Add test runner library
aakash070 Apr 2, 2025
afdf755
created test runner option for test suite parser
aakash070 Apr 2, 2025
e6028d1
update test suite structure
aakash070 Apr 4, 2025
98be9bd
fix test suite path used to set up test suite parser
aakash070 Apr 4, 2025
dcfc1a0
updated cel.dev/expr version in WORKSPACE
aakash070 Apr 7, 2025
a8a77f5
updated go registered toolchain version in WORKSPACE
aakash070 Apr 7, 2025
3c58df5
updated checksum for cel.dev/expr in WORKSPACE
aakash070 Apr 7, 2025
b867b8a
update cel.dev/expr release version to resolve failing tests
aakash070 Apr 7, 2025
9b8248c
fix vendor verification error
aakash070 Apr 7, 2025
bf96c47
resolved pr comments on YAML test suite and test runner
aakash070 Apr 8, 2025
449ba4f
update test runner to accept any type options instead of TestRunnerOp…
aakash070 Apr 8, 2025
b8c7496
replaced functional test suite parsers with an interface
aakash070 Apr 9, 2025
f942827
update test suite schema in policy testdata files
aakash070 Apr 10, 2025
61399be
added sync once as a compiler object level variable
aakash070 Apr 10, 2025
2dad2f3
fix test failure
aakash070 Apr 10, 2025
c101087
added test for test runner for standard policy
aakash070 Apr 10, 2025
156addf
added tests for custom policy parser via flags
aakash070 Apr 11, 2025
f2022c3
moved test for custom policy from compiler test to test runner test
aakash070 Apr 14, 2025
59cf7ee
changed argument type for Test Suite Parser methods to string
aakash070 Apr 14, 2025
70d7f5f
added tests for raw CEL expression and CEL expression file
aakash070 Apr 14, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ go_repository(
go_repository(
name = "dev_cel_expr",
importpath = "cel.dev/expr",
sum = "h1:xoFEsNh972Yzey8N9TCPx2nDvMN7TMhQEzxLuj/iRrI=",
version = "v0.22.1",
sum = "h1:K4KOtPCJQjVggkARsjG9RWXP6O4R73aHeJMa/dmCQQg=",
version = "v0.23.1",
)

# local_repository(
Expand Down Expand Up @@ -153,7 +153,7 @@ go_repository(
# of the above repositories but at different versions, so ours must come first.
go_rules_dependencies()

go_register_toolchains(version = "1.21.1")
go_register_toolchains(version = "1.22.0")

gazelle_dependencies()

Expand Down
2 changes: 1 addition & 1 deletion conformance/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/google/cel-go/conformance
go 1.22.0

require (
cel.dev/expr v0.22.1
cel.dev/expr v0.23.1
github.com/bazelbuild/rules_go v0.49.0
github.com/google/cel-go v0.21.0
github.com/google/go-cmp v0.6.0
Expand Down
4 changes: 2 additions & 2 deletions conformance/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cel.dev/expr v0.22.1 h1:xoFEsNh972Yzey8N9TCPx2nDvMN7TMhQEzxLuj/iRrI=
cel.dev/expr v0.22.1/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
cel.dev/expr v0.23.1 h1:K4KOtPCJQjVggkARsjG9RWXP6O4R73aHeJMa/dmCQQg=
cel.dev/expr v0.23.1/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/bazelbuild/rules_go v0.49.0 h1:5vCbuvy8Q11g41lseGJDc5vxhDjJtfxr6nM/IC4VmqM=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.0
toolchain go1.23.0

require (
cel.dev/expr v0.22.1
cel.dev/expr v0.23.1
github.com/antlr4-go/antlr/v4 v4.13.0
github.com/stoewer/go-strcase v1.2.0
google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cel.dev/expr v0.22.1 h1:xoFEsNh972Yzey8N9TCPx2nDvMN7TMhQEzxLuj/iRrI=
cel.dev/expr v0.22.1/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
cel.dev/expr v0.23.1 h1:K4KOtPCJQjVggkARsjG9RWXP6O4R73aHeJMa/dmCQQg=
cel.dev/expr v0.23.1/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand Down
7 changes: 4 additions & 3 deletions policy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ go_test(
embed = [":go_default_library"],
deps = [
"//cel:go_default_library",
"//test:go_default_library",
"//common/types:go_default_library",
"//interpreter:go_default_library",
"//common/types/ref:go_default_library",
Expand All @@ -72,6 +73,6 @@ go_test(
)

filegroup(
name = "k8s_policy_testdata",
srcs = glob(["testdata/k8s/*"]),
)
name = "testdata",
srcs = glob(["testdata/**"]),
)
69 changes: 38 additions & 31 deletions policy/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,24 +243,25 @@ func (r *runner) run(t *testing.T) {
input := map[string]any{}
var err error
var activation interpreter.Activation
for k, v := range tc.Input {
if v.Expr != "" {
input[k] = r.eval(t, v.Expr)
continue
if tc.InputContext != nil && tc.InputContext.ContextExpr != "" {
ctxExpr := tc.InputContext.ContextExpr
ctx, err := r.eval(t, ctxExpr).ConvertToNative(
reflect.TypeOf(((*proto.Message)(nil))).Elem())
if err != nil {
t.Fatalf("context variable is not a valid proto: %v", err)
}
if v.ContextExpr != "" {
ctx, err := r.eval(t, v.ContextExpr).ConvertToNative(
reflect.TypeOf(((*proto.Message)(nil))).Elem())
if err != nil {
t.Fatalf("context variable is not a valid proto: %v", err)
}
activation, err = cel.ContextProtoVars(ctx.(proto.Message))
if err != nil {
t.Fatalf("cel.ContextProtoVars() failed: %v", err)
activation, err = cel.ContextProtoVars(ctx.(proto.Message))
if err != nil {
t.Fatalf("cel.ContextProtoVars() failed: %v", err)
}
} else if len(tc.Input) != 0 {
for k, v := range tc.Input {
if v.Expr != "" {
input[k] = r.eval(t, v.Expr)
continue
}
break
input[k] = v.Value
}
input[k] = v.Value
}
if activation == nil {
activation, err = interpreter.NewActivation(input)
Expand All @@ -272,7 +273,12 @@ func (r *runner) run(t *testing.T) {
if err != nil {
t.Fatalf("prg.Eval(input) failed: %v", err)
}
testOut := r.eval(t, tc.Output)
var testOut ref.Val
if tc.Output.Expr != "" {
testOut = r.eval(t, tc.Output.Expr)
} else if tc.Output.Value != nil {
testOut = r.env.CELTypeAdapter().NativeToValue(tc.Output.Value)
}
if optOut, ok := out.(*types.Optional); ok {
if optOut.Equal(types.OptionalNone) == types.True {
if testOut.Equal(types.OptionalNone) != types.True {
Expand All @@ -299,24 +305,25 @@ func (r *runner) bench(b *testing.B) {
input := map[string]any{}
var err error
var activation interpreter.Activation
for k, v := range tc.Input {
if v.Expr != "" {
input[k] = r.eval(b, v.Expr)
continue
if tc.InputContext != nil && tc.InputContext.ContextExpr != "" {
ctxExpr := tc.InputContext.ContextExpr
ctx, err := r.eval(b, ctxExpr).ConvertToNative(
reflect.TypeOf(((*proto.Message)(nil))).Elem())
if err != nil {
b.Fatalf("context variable is not a valid proto: %v", err)
}
if v.ContextExpr != "" {
ctx, err := r.eval(b, v.ContextExpr).ConvertToNative(
reflect.TypeOf(((*proto.Message)(nil))).Elem())
if err != nil {
b.Fatalf("context variable is not a valid proto: %v", err)
}
activation, err = cel.ContextProtoVars(ctx.(proto.Message))
if err != nil {
b.Fatalf("cel.ContextProtoVars() failed: %v", err)
activation, err = cel.ContextProtoVars(ctx.(proto.Message))
if err != nil {
b.Fatalf("cel.ContextProtoVars() failed: %v", err)
}
} else if tc.Input != nil {
for k, v := range tc.Input {
if v.Expr != "" {
input[k] = r.eval(b, v.Expr)
continue
}
break
input[k] = v.Value
}
input[k] = v.Value
}
if activation == nil {
activation, err = interpreter.NewActivation(input)
Expand Down
2 changes: 2 additions & 0 deletions policy/conformance.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package policy

// TestSuite describes a set of tests divided by section.
//
// Deprecated: Use google3/third_party/cel/go/test/suite.go instead.
type TestSuite struct {
Description string `yaml:"description"`
Sections []*TestSection `yaml:"section"`
Expand Down
2 changes: 1 addition & 1 deletion policy/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
)

require (
cel.dev/expr v0.22.1 // indirect
cel.dev/expr v0.23.1 // indirect
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect
Expand Down
4 changes: 2 additions & 2 deletions policy/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cel.dev/expr v0.22.1 h1:xoFEsNh972Yzey8N9TCPx2nDvMN7TMhQEzxLuj/iRrI=
cel.dev/expr v0.22.1/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
cel.dev/expr v0.23.1 h1:K4KOtPCJQjVggkARsjG9RWXP6O4R73aHeJMa/dmCQQg=
cel.dev/expr v0.23.1/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
5 changes: 3 additions & 2 deletions policy/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/google/cel-go/common/env"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/test"

"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -447,13 +448,13 @@ func readPolicyConfig(t testing.TB, fileName string) *env.Config {
return config
}

func readTestSuite(t testing.TB, fileName string) *TestSuite {
func readTestSuite(t testing.TB, fileName string) *test.Suite {
t.Helper()
testCaseBytes, err := os.ReadFile(fileName)
if err != nil {
t.Fatalf("os.ReadFile(%s) failed: %v", fileName, err)
}
suite := &TestSuite{}
suite := &test.Suite{}
err = yaml.Unmarshal(testCaseBytes, suite)
if err != nil {
log.Fatalf("yaml.Unmarshal(%s) error: %v", fileName, err)
Expand Down
2 changes: 0 additions & 2 deletions policy/testdata/context_pb/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
name: "context_pb"
container: "google.expr.proto3"
extensions:
- name: "optional"
version: "latest"
- name: "strings"
version: 2
context_variable:
Expand Down
21 changes: 8 additions & 13 deletions policy/testdata/context_pb/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ description: "Protobuf input tests"
section:
- name: "valid"
tests:
- name: "good spec"
input:
spec:
context_expr: >
test.TestAllTypes{single_int32: 10}
output: "optional.none()"
- name: "good spec"
context_expr: "test.TestAllTypes{single_int32: 10}"
output:
expr: "optional.none()"
- name: "invalid"
tests:
- name: "bad spec"
input:
spec:
context_expr: >
test.TestAllTypes{single_int32: 11}
output: >
"invalid spec, got single_int32=11, wanted <= 10"
- name: "bad spec"
context_expr: "test.TestAllTypes{single_int32: 11}"
output:
value: "invalid spec, got single_int32=11, wanted <= 10"
4 changes: 0 additions & 4 deletions policy/testdata/k8s/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@

name: k8s
extensions:
- name: "optional"
version: "latest"
- name: "bindings"
version: "latest"
- name: "strings"
version: 2
variables:
Expand Down
3 changes: 2 additions & 1 deletion policy/testdata/k8s/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ section:
- staging.dev.cel.container1
- staging.dev.cel.container2
- preprod.dev.cel.container3
output: "'only staging containers are allowed in namespace dev.cel'"
output:
value: "only staging containers are allowed in namespace dev.cel"
12 changes: 8 additions & 4 deletions policy/testdata/limits/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ section:
input:
now:
expr: "timestamp('2024-07-30T00:30:00Z')"
output: "'hello, me'"
output:
value: "hello, me"
- name: "8pm"
input:
now:
expr: "timestamp('2024-07-30T20:30:00Z')"
output: "'goodbye, me!'"
output:
value: "goodbye, me!"
- name: "9pm"
input:
now:
expr: "timestamp('2024-07-30T21:30:00Z')"
output: "'goodbye, me!!'"
output:
value: "goodbye, me!!"
- name: "11pm"
input:
now:
expr: "timestamp('2024-07-30T23:30:00Z')"
output: "'goodbye, me!!!'"
output:
value: "goodbye, me!!!"
9 changes: 6 additions & 3 deletions policy/testdata/nested_rule/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ section:
resource:
value:
origin: "ir"
output: "{'banned': true}"
output:
expr: "{'banned': true}"
- name: "by_default"
input:
resource:
value:
origin: "de"
output: "{'banned': true}"
output:
expr: "{'banned': true}"
- name: "permitted"
tests:
- name: "valid_origin"
input:
resource:
value:
origin: "uk"
output: "{'banned': false}"
output:
expr: "{'banned': false}"
12 changes: 8 additions & 4 deletions policy/testdata/nested_rule2/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@ section:
value:
user: "bad-user"
origin: "ir"
output: "{'banned': 'restricted_region'}"
output:
expr: "{'banned': 'restricted_region'}"
- name: "by_default"
input:
resource:
value:
user: "bad-user"
origin: "de"
output: "{'banned': 'bad_actor'}"
output:
expr: "{'banned': 'bad_actor'}"
- name: "unconfigured_region"
input:
resource:
value:
user: "good-user"
origin: "de"
output: "{'banned': 'unconfigured_region'}"
output:
expr: "{'banned': 'unconfigured_region'}"
- name: "permitted"
tests:
- name: "valid_origin"
Expand All @@ -45,4 +48,5 @@ section:
value:
user: "good-user"
origin: "uk"
output: "{}"
output:
expr: "{}"
12 changes: 8 additions & 4 deletions policy/testdata/nested_rule3/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@ section:
value:
user: "bad-user"
origin: "ir"
output: "{'banned': 'restricted_region'}"
output:
expr: "{'banned': 'restricted_region'}"
- name: "by_default"
input:
resource:
value:
user: "bad-user"
origin: "de"
output: "{'banned': 'bad_actor'}"
output:
expr: "{'banned': 'bad_actor'}"
- name: "unconfigured_region"
input:
resource:
value:
user: "good-user"
origin: "de"
output: "{'banned': 'unconfigured_region'}"
output:
expr: "{'banned': 'unconfigured_region'}"
- name: "permitted"
tests:
- name: "valid_origin"
Expand All @@ -45,4 +48,5 @@ section:
value:
user: "good-user"
origin: "uk"
output: "optional.none()"
output:
expr: "optional.none()"
Loading