Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
moved test for custom policy from compiler test to test runner test
  • Loading branch information
aakash070 committed Apr 15, 2025
commit f2022c379393d16ef6f8a9e21d605d86394f1bd2
9 changes: 2 additions & 7 deletions policy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ go_test(
)

filegroup(
name = "k8s_policy_testdata",
srcs = glob(["testdata/k8s/*"]),
)

filegroup(
name = "restricted_destinations_policy_test_data",
srcs = glob(["testdata/restricted_destinations/*"]),
name = "testdata",
srcs = glob(["testdata/**"]),
)
11 changes: 9 additions & 2 deletions tools/celtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,22 @@ go_test(
"test_runner_test.go",
],
data = [
"//policy:k8s_policy_testdata",
"//policy:restricted_destinations_policy_test_data",
":testdata",
"//policy:testdata",
],
embed = [":go_default_library"],
deps = [
"//cel:go_default_library",
"//common/decls:go_default_library",
"//common/types:go_default_library",
"//common/types/ref:go_default_library",
"//policy:go_default_library",
"//tools/compiler:go_default_library",
"@in_gopkg_yaml_v3//:go_default_library",
]
)

filegroup(
name = "testdata",
srcs = glob(["testdata/**"]),
)
4 changes: 2 additions & 2 deletions tools/celtest/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (p *tsParser) ParseTextproto(path any) (*conformancepb.TestSuite, error) {
testSuite := &conformancepb.TestSuite{}
data, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("runfiles.ReadFile(%q) failed: %v", path, err)
return nil, fmt.Errorf("os.ReadFile(%q) failed: %v", path, err)
}
err = prototext.Unmarshal(data, testSuite)
return testSuite, err
Expand All @@ -181,7 +181,7 @@ func (p *tsParser) ParseYAML(path any) (*test.Suite, error) {
}
testSuiteBytes, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("runfiles.ReadFile(%q) failed: %v", filePath, err)
return nil, fmt.Errorf("os.ReadFile(%q) failed: %v", filePath, err)
}
testSuite := &test.Suite{}
err = yaml.Unmarshal(testSuiteBytes, testSuite)
Expand Down
58 changes: 58 additions & 0 deletions tools/celtest/test_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import (
"testing"

"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/decls"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/policy"
"github.com/google/cel-go/tools/compiler"
"gopkg.in/yaml.v3"
)

type testCase struct {
Expand Down Expand Up @@ -50,6 +52,12 @@ func setupTests() []*testCase {
configPath: "../../policy/testdata/restricted_destinations/config.yaml",
opts: []any{locationCodeEnvOption()},
},
{
name: "policy test with custom policy metadata",
celExpression: "testdata/custom_policy.celpolicy",
testSuitePath: "testdata/custom_policy_tests.yaml",
opts: []any{customPolicyParserOption(), compiler.PolicyMetadataEnvOption(ParsePolicyVariables)},
},
}
return testCases
}
Expand Down Expand Up @@ -103,6 +111,56 @@ func TestTriggerTestsWithRunnerOptions(t *testing.T) {
})
}

func customPolicyParserOption() policy.ParserOption {
return func(p *policy.Parser) (*policy.Parser, error) {
p.TagVisitor = customTagHandler{TagVisitor: policy.DefaultTagVisitor()}
return p, nil
}
}
func ParsePolicyVariables(metadata map[string]any) cel.EnvOption {
var variables []*decls.VariableDecl
for n, t := range metadata {
variables = append(variables, decls.NewVariable(n, parseCustomPolicyVariableType(t.(string))))
}
return cel.VariableDecls(variables...)
}

func parseCustomPolicyVariableType(t string) *types.Type {
switch t {
case "int":
return types.IntType
case "string":
return types.StringType
default:
return types.UnknownType
}
}

type variableType struct {
VariableName string `yaml:"variable_name"`
VariableType string `yaml:"variable_type"`
}

type customTagHandler struct {
policy.TagVisitor
}

func (customTagHandler) PolicyTag(ctx policy.ParserContext, id int64, tagName string, node *yaml.Node, p *policy.Policy) {
switch tagName {
case "variable_types":
var varList []*variableType
if err := node.Decode(&varList); err != nil {
ctx.ReportErrorAtID(id, "invalid yaml variable_types node: %v, error: %w", node, err)
return
}
for _, v := range varList {
p.SetMetadata(v.VariableName, v.VariableType)
}
default:
ctx.ReportErrorAtID(id, "unsupported policy tag: %s", tagName)
}
}

// TestTriggerTests tests different scenarios of the TriggerTestsFromCompiler function.
func TestTriggerTests(t *testing.T) {
for _, tc := range setupTests() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ rule:
- condition: |
variable1 == 1 || variable2 == "known"
output: "true"
- output: "false"
42 changes: 42 additions & 0 deletions tools/celtest/testdata/custom_policy_tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

description: "Custom policy tests"
section:
- name: "output true"
tests:
- name: "variable 1 match"
input:
variable1:
value: 1
output:
value: true
- name: "variable 2 match"
input:
variable1:
value: 2
variable2:
value: "known"
output:
value: true
- name: "output false"
tests:
- name: "variable mismatch"
input:
variable1:
value: 2
variable2:
value: "unknown"
output:
value: false
5 changes: 1 addition & 4 deletions tools/compiler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,16 @@ go_test(
],
data = [
":compiler_testdata",
"//policy:k8s_policy_testdata",
"//policy:testdata",
],
embed = [":go_default_library"],
deps = [
"//cel:go_default_library",
"//common/decls:go_default_library",
"//common/env:go_default_library",
"//common/types:go_default_library",
"//ext:go_default_library",
"//policy:go_default_library",
"@dev_cel_expr//:expr",
"@dev_cel_expr//conformance:go_default_library",
"@in_gopkg_yaml_v3//:go_default_library",
"@org_golang_google_protobuf//types/known/structpb:go_default_library",
],
)
75 changes: 1 addition & 74 deletions tools/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ import (
"testing"

"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/decls"
"github.com/google/cel-go/common/env"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/ext"
"github.com/google/cel-go/policy"
"gopkg.in/yaml.v3"

celpb "cel.dev/expr"
configpb "cel.dev/expr/conformance"
Expand Down Expand Up @@ -75,7 +72,7 @@ func TestEnvironmentFileCompareTextprotoAndYAML(t *testing.T) {
for i, v := range protoConfig.Variables {
for j, p := range v.TypeDesc.Params {
if p.TypeName == "google.protobuf.Any" &&
config.Variables[i].TypeDesc.Params[j].TypeName == "dyn" {
config.Variables[i].TypeDesc.Params[j].TypeName == "dyn" {
p.TypeName = "dyn"
}
}
Expand Down Expand Up @@ -397,76 +394,6 @@ func TestFileExpressionCustomPolicyParser(t *testing.T) {
})
}

func TestFileExpressionPolicyMetadataOptions(t *testing.T) {
t.Run("test file expression policy metadata options", func(t *testing.T) {
envOpt := EnvironmentFile("testdata/custom_policy_config.yaml")
parserOpt := policy.ParserOption(func(p *policy.Parser) (*policy.Parser, error) {
p.TagVisitor = customTagHandler{TagVisitor: policy.DefaultTagVisitor()}
return p, nil
})
policyMetadataOpt := PolicyMetadataEnvOption(ParsePolicyVariables)
compilerOpts := []any{envOpt, parserOpt, policyMetadataOpt}
compiler, err := NewCompiler(compilerOpts...)
if err != nil {
t.Fatalf("NewCompiler() failed: %v", err)
}
policyFile := &FileExpression{
Path: "testdata/custom_policy.celpolicy",
}
ast, _, err := policyFile.CreateAST(compiler)
if err != nil {
t.Fatalf("CreateAST() failed: %v", err)
}
if ast == nil {
t.Fatalf("CreateAST() returned nil ast")
}
})
}

func ParsePolicyVariables(metadata map[string]any) cel.EnvOption {
variables := []*decls.VariableDecl{}
for n, t := range metadata {
variables = append(variables, decls.NewVariable(n, parseCustomPolicyVariableType(t.(string))))
}
return cel.VariableDecls(variables...)
}

func parseCustomPolicyVariableType(t string) *types.Type {
switch t {
case "int":
return types.IntType
case "string":
return types.StringType
default:
return types.UnknownType
}
}

type variableType struct {
VariableName string `yaml:"variable_name"`
VariableType string `yaml:"variable_type"`
}

type customTagHandler struct {
policy.TagVisitor
}

func (customTagHandler) PolicyTag(ctx policy.ParserContext, id int64, tagName string, node *yaml.Node, p *policy.Policy) {
switch tagName {
case "variable_types":
varList := []*variableType{}
if err := node.Decode(&varList); err != nil {
ctx.ReportErrorAtID(id, "invalid yaml variable_types node: %v, error: %w", node, err)
return
}
for _, v := range varList {
p.SetMetadata(v.VariableName, v.VariableType)
}
default:
ctx.ReportErrorAtID(id, "unsupported policy tag: %s", tagName)
}
}

func TestRawExpressionCreateAst(t *testing.T) {
t.Run("test raw expression create ast", func(t *testing.T) {
envOpt := EnvironmentFile("testdata/config.yaml")
Expand Down
18 changes: 0 additions & 18 deletions tools/compiler/testdata/custom_policy_config.yaml

This file was deleted.