Skip to content

Commit 138b11d

Browse files
l46kokcopybara-github
authored andcommitted
Introduce protobuf message testing to policies
PiperOrigin-RevId: 649212262
1 parent 893dfea commit 138b11d

6 files changed

Lines changed: 99 additions & 7 deletions

File tree

policy/src/test/java/dev/cel/policy/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ java_library(
1515
"//common",
1616
"//common:options",
1717
"//common/internal",
18+
"//common/resources/testdata/proto3:test_all_types_java_proto",
1819
"//compiler",
1920
"//extensions:optional_library",
2021
"//parser",

policy/src/test/java/dev/cel/policy/CelPolicyCompilerImplTest.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package dev.cel.policy;
1616

17-
import static com.google.common.collect.ImmutableMap.toImmutableMap;
17+
import static com.google.common.base.Strings.isNullOrEmpty;
1818
import static com.google.common.truth.Truth.assertThat;
1919
import static dev.cel.policy.PolicyTestHelper.readFromYaml;
2020
import static org.junit.Assert.assertThrows;
@@ -36,9 +36,11 @@
3636
import dev.cel.policy.PolicyTestHelper.PolicyTestSuite;
3737
import dev.cel.policy.PolicyTestHelper.PolicyTestSuite.PolicyTestSection;
3838
import dev.cel.policy.PolicyTestHelper.PolicyTestSuite.PolicyTestSection.PolicyTestCase;
39+
import dev.cel.policy.PolicyTestHelper.PolicyTestSuite.PolicyTestSection.PolicyTestCase.PolicyTestInput;
3940
import dev.cel.policy.PolicyTestHelper.TestYamlPolicy;
4041
import dev.cel.runtime.CelRuntime.CelFunctionBinding;
41-
import java.util.Map.Entry;
42+
import dev.cel.testing.testdata.proto3.TestAllTypesProto.TestAllTypes;
43+
import java.util.Map;
4244
import java.util.Optional;
4345
import org.junit.Test;
4446
import org.junit.runner.RunWith;
@@ -138,10 +140,17 @@ public void evaluateYamlPolicy_withCanonicalTestData(
138140
// Compile then evaluate the policy
139141
CelAbstractSyntaxTree compiledPolicyAst =
140142
CelPolicyCompilerFactory.newPolicyCompiler(cel).build().compile(policy);
141-
ImmutableMap<String, Object> input =
142-
testData.testCase.getInput().entrySet().stream()
143-
.collect(toImmutableMap(Entry::getKey, e -> e.getValue().getValue()));
144-
Object evalResult = cel.createProgram(compiledPolicyAst).eval(input);
143+
ImmutableMap.Builder<String, Object> inputBuilder = ImmutableMap.builder();
144+
for (Map.Entry<String, PolicyTestInput> entry : testData.testCase.getInput().entrySet()) {
145+
String exprInput = entry.getValue().getExpr();
146+
if (isNullOrEmpty(exprInput)) {
147+
inputBuilder.put(entry.getKey(), entry.getValue().getValue());
148+
} else {
149+
CelAbstractSyntaxTree exprInputAst = cel.compile(exprInput).getAst();
150+
inputBuilder.put(entry.getKey(), cel.createProgram(exprInputAst).eval());
151+
}
152+
}
153+
Object evalResult = cel.createProgram(compiledPolicyAst).eval(inputBuilder.buildOrThrow());
145154

146155
// Assert
147156
// Note that policies may either produce an optional or a non-optional result,
@@ -219,6 +228,7 @@ private static Cel newCel() {
219228
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
220229
.addCompilerLibraries(CelOptionalLibrary.INSTANCE)
221230
.addRuntimeLibraries(CelOptionalLibrary.INSTANCE)
231+
.addMessageTypes(TestAllTypes.getDescriptor())
222232
.setOptions(CEL_OPTIONS)
223233
.addFunctionBindings(
224234
CelFunctionBinding.from(

policy/src/test/java/dev/cel/policy/PolicyTestHelper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ enum TestYamlPolicy {
8282
+ " \"true\", !(variables.break_glass || resource.containers.all(c,"
8383
+ " c.startsWith(variables.env + \".\"))) ? optional.of(\"only \" + variables.env + \""
8484
+ " containers are allowed in namespace \" + resource.namespace) :"
85-
+ " optional.none()))");
85+
+ " optional.none()))"),
86+
PB(
87+
"pb",
88+
true,
89+
"(spec.single_int32 > 10) ? optional.of(\"invalid spec, got single_int32=\" +"
90+
+ " string(spec.single_int32) + \", wanted <= 10\") : optional.none()");
8691
private final String name;
8792
private final boolean producesOptionalResult;
8893
private final String unparsed;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: "pb"
16+
container: "dev.cel.testing.testdata.proto3"
17+
extensions:
18+
- name: "strings"
19+
version: 2
20+
variables:
21+
- name: "spec"
22+
type:
23+
type_name: "dev.cel.testing.testdata.proto3.TestAllTypes"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: "pb"
16+
rule:
17+
match:
18+
- condition: spec.single_int32 > 10
19+
output: |
20+
"invalid spec, got single_int32=" + string(spec.single_int32) + ", wanted <= 10"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
description: "Protobuf input tests"
16+
section:
17+
- name: "valid"
18+
tests:
19+
- name: "good spec"
20+
input:
21+
spec:
22+
expr: >
23+
TestAllTypes{single_int32: 10}
24+
output: "optional.none()"
25+
- name: "invalid"
26+
tests:
27+
- name: "bad spec"
28+
input:
29+
spec:
30+
expr: >
31+
TestAllTypes{single_int32: 11}
32+
output: >
33+
"invalid spec, got single_int32=11, wanted <= 10"

0 commit comments

Comments
 (0)