Skip to content

Commit bb8bcc4

Browse files
l46kokcopybara-github
authored andcommitted
Remove duplicate test policies and use the ones from cel-policy as SoT
PiperOrigin-RevId: 918667081
1 parent 919ae0b commit bb8bcc4

68 files changed

Lines changed: 244 additions & 2036 deletions

File tree

Some content is hidden

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ java_library(
77
name = "tests",
88
testonly = True,
99
srcs = glob(["*.java"]),
10+
data = [
11+
"@cel_policy//conformance:testdata",
12+
],
1013
resources = [
1114
"//testing:policy_test_resources",
1215
],
@@ -39,6 +42,7 @@ java_library(
3942
"//runtime:function_binding",
4043
"//testing:cel_runtime_flavor",
4144
"//testing/protos:single_file_java_proto",
45+
"@bazel_tools//tools/java/runfiles",
4246
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
4347
"@maven//:com_google_guava_guava",
4448
"@maven//:com_google_testparameterinjector_test_parameter_injector",

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
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;
20+
import static java.nio.charset.StandardCharsets.UTF_8;
2021
import static org.junit.Assert.assertThrows;
2122

2223
import com.google.common.collect.ImmutableList;
2324
import com.google.common.collect.ImmutableMap;
25+
import com.google.common.io.Resources;
2426
import com.google.testing.junit.testparameterinjector.TestParameter;
2527
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2628
import com.google.testing.junit.testparameterinjector.TestParameterValue;
@@ -50,6 +52,7 @@
5052
import dev.cel.testing.testdata.SingleFile;
5153
import dev.cel.testing.testdata.proto3.StandaloneGlobalEnum;
5254
import java.io.IOException;
55+
import java.net.URL;
5356
import java.util.Map;
5457
import java.util.Optional;
5558
import org.junit.Test;
@@ -112,9 +115,12 @@ public void compileYamlPolicy_withImportsOnNestedRules() throws Exception {
112115
public void compileYamlPolicy_containsCompilationError_throws(
113116
@TestParameter TestErrorYamlPolicy testCase) throws Exception {
114117
// Read config and produce an environment to compile policies
115-
String configSource = testCase.readConfigYamlContent();
116-
CelEnvironment celEnvironment = ENVIRONMENT_PARSER.parse(configSource);
117-
Cel cel = celEnvironment.extend(newCel(), CEL_OPTIONS);
118+
Optional<String> configSource = testCase.readConfigYamlContent();
119+
Cel baseCel = newCel();
120+
Cel cel =
121+
configSource.isPresent()
122+
? ENVIRONMENT_PARSER.parse(configSource.get()).extend(baseCel, CEL_OPTIONS)
123+
: baseCel;
118124
// Read the policy source
119125
String policySource = testCase.readPolicyYamlContent();
120126
CelPolicy policy = POLICY_PARSER.parse(policySource, testCase.getPolicyFilePath());
@@ -509,10 +515,14 @@ private enum MultilineErrorTest {
509515
}
510516

511517
private enum TestErrorYamlPolicy {
512-
COMPILE_ERRORS("compile_errors"),
513-
COMPOSE_ERRORS_CONFLICTING_OUTPUT("compose_errors_conflicting_output"),
514-
COMPOSE_ERRORS_CONFLICTING_SUBRULE("compose_errors_conflicting_subrule"),
515-
ERRORS_UNREACHABLE("errors_unreachable");
518+
COMPOSE_ERRORS_CONFLICTING_OUTPUT("compose_conflicting_output"),
519+
COMPOSE_ERRORS_CONFLICTING_SUBRULE("compose_conflicting_subrule"),
520+
ERRORS_UNREACHABLE("unreachable"),
521+
DUPLICATE_VARIABLE("duplicate_variable"),
522+
IMPORT("import"),
523+
INCOMPATIBLE_OUTPUTS("incompatible_outputs"),
524+
SYNTAX("syntax"),
525+
UNDECLARED_REFERENCE("undeclared_reference");
516526

517527
private final String name;
518528
private final String policyFilePath;
@@ -522,15 +532,26 @@ private String getPolicyFilePath() {
522532
}
523533

524534
private String readPolicyYamlContent() throws IOException {
525-
return readFromYaml(String.format("policy/%s/policy.yaml", name));
535+
return readFromYaml(
536+
String.format(
537+
"cel_policy/conformance/testdata/compile_errors/%s/policy.yaml",
538+
name));
526539
}
527540

528-
private String readConfigYamlContent() throws IOException {
529-
return readFromYaml(String.format("policy/%s/config.yaml", name));
541+
private Optional<String> readConfigYamlContent() throws IOException {
542+
String rlocationPath =
543+
String.format(
544+
"cel_policy/conformance/testdata/compile_errors/%s/config.yaml",
545+
name);
546+
if (PolicyTestHelper.hasRunfile(rlocationPath)) {
547+
return Optional.of(readFromYaml(rlocationPath));
548+
}
549+
return Optional.empty();
530550
}
531551

532552
private String readExpectedErrorsBaseline() throws IOException {
533-
return readFromYaml(String.format("policy/%s/expected_errors.baseline", name));
553+
URL url = Resources.getResource(String.format("policy/%s/expected_errors.baseline", name));
554+
return Resources.toString(url, UTF_8).trim();
534555
}
535556

536557
TestErrorYamlPolicy(String name) {

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

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@
1818

1919
import com.google.common.annotations.VisibleForTesting;
2020
import com.google.common.base.Ascii;
21-
import com.google.common.io.Resources;
21+
import com.google.common.io.Files;
22+
import com.google.devtools.build.runfiles.AutoBazelRepository;
23+
import com.google.devtools.build.runfiles.Runfiles;
24+
import java.io.File;
2225
import java.io.IOException;
23-
import java.net.URL;
2426
import java.util.List;
2527
import java.util.Map;
2628
import org.yaml.snakeyaml.LoaderOptions;
2729
import org.yaml.snakeyaml.Yaml;
2830
import org.yaml.snakeyaml.constructor.Constructor;
2931

3032
/** Package-private class to assist with policy testing. */
33+
@AutoBazelRepository
3134
final class PolicyTestHelper {
3235

36+
private static final Runfiles runfiles = createRunfiles();
37+
3338
enum TestYamlPolicy {
3439
NESTED_RULE(
3540
"nested_rule",
@@ -74,11 +79,11 @@ enum TestYamlPolicy {
7479
"required_labels",
7580
true,
7681
"cel.@block([spec.labels.filter(@it:0:0, !(@it:0:0 in resource.labels)), spec.labels,"
77-
+ " resource.labels, @index2.filter(@it:0:0, @it:0:0 in @index1 && @index1[@it:0:0] !="
78-
+ " @index2[@it:0:0])], (@index0.size() > 0) ? optional.of(\"missing one or more"
79-
+ " required labels: [\"\" + @index0.join(\",\") + \"\"]\") : ((@index3.size() > 0) ?"
80-
+ " optional.of(\"invalid values provided on one or more labels: [\"\" +"
81-
+ " @index3.join(\",\") + \"\"]\") : optional.none()))"),
82+
+ " resource.labels.transformList(@it:0:1, @it2:0:1, @it:0:1 in @index1 && @it2:0:1 !="
83+
+ " @index1[@it:0:1], @it:0:1)], (@index0.size() > 0) ? optional.of(\"missing one or"
84+
+ " more required labels: [\"\" + @index0.join(\"\", \"\") + \"\"]\") :"
85+
+ " ((@index2.size() > 0) ? optional.of(\"invalid values provided on one or more"
86+
+ " labels: [\"\" + @index2.join(\"\", \"\") + \"\"]\") : optional.none()))"),
8287
RESTRICTED_DESTINATIONS(
8388
"restricted_destinations",
8489
false,
@@ -102,9 +107,10 @@ enum TestYamlPolicy {
102107
"cel.@block([spec.single_int32], (@index0 > 10) ? optional.of(\"invalid spec, got"
103108
+ " single_int32=\" + string(@index0) + \", wanted <= 10\") : ((spec.standalone_enum =="
104109
+ " cel.expr.conformance.proto3.TestAllTypes.NestedEnum.BAR ||"
105-
+ " dev.cel.testing.testdata.proto3.StandaloneGlobalEnum.SGAR =="
106-
+ " dev.cel.testing.testdata.proto3.StandaloneGlobalEnum.SGOO) ? optional.of(\"invalid"
107-
+ " spec, neither nested nor imported enums may refer to BAR\") : optional.none()))"),
110+
+ " cel.expr.conformance.proto3.TestAllTypes.NestedEnum.BAZ in"
111+
+ " spec.repeated_nested_enum || cel.expr.conformance.proto3.GlobalEnum.GAR =="
112+
+ " cel.expr.conformance.proto3.GlobalEnum.GOO) ? optional.of(\"invalid spec, neither"
113+
+ " nested nor repeated enums may refer to BAR or BAZ\") : optional.none()))"),
108114
LIMITS(
109115
"limits",
110116
true,
@@ -136,16 +142,23 @@ String getUnparsed() {
136142
}
137143

138144
String readPolicyYamlContent() throws IOException {
139-
return readFromYaml(String.format("policy/%s/policy.yaml", name));
145+
return readFromYaml(
146+
String.format(
147+
"cel_policy/conformance/testdata/%s/policy.yaml", name));
140148
}
141149

142150
String readConfigYamlContent() throws IOException {
143-
return readFromYaml(String.format("policy/%s/config.yaml", name));
151+
return readFromYaml(
152+
String.format(
153+
"cel_policy/conformance/testdata/%s/config.yaml", name));
144154
}
145155

146156
PolicyTestSuite readTestYamlContent() throws IOException {
147157
Yaml yaml = new Yaml(new Constructor(PolicyTestSuite.class, new LoaderOptions()));
148-
String testContent = readFile(String.format("policy/%s/tests.yaml", name));
158+
String testContent =
159+
readFile(
160+
String.format(
161+
"cel_policy/conformance/testdata/%s/tests.yaml", name));
149162

150163
return yaml.load(testContent);
151164
}
@@ -163,9 +176,18 @@ static String readFromYaml(String yamlPath) throws IOException {
163176
*/
164177
@VisibleForTesting
165178
public static final class PolicyTestSuite {
179+
private String name;
166180
private String description;
167181
private List<PolicyTestSection> section;
168182

183+
public void setName(String name) {
184+
this.name = name;
185+
}
186+
187+
public String getName() {
188+
return name;
189+
}
190+
169191
public void setDescription(String description) {
170192
this.description = description;
171193
}
@@ -258,12 +280,32 @@ public void setExpr(String expr) {
258280
}
259281
}
260282

261-
private static URL getResource(String path) {
262-
return Resources.getResource(Ascii.toLowerCase(path));
283+
private static String readFile(String rlocationPath) throws IOException {
284+
String resolvedPath = runfiles.rlocation(Ascii.toLowerCase(rlocationPath));
285+
if (resolvedPath == null) {
286+
throw new IOException("Unmapped runfile path: " + rlocationPath);
287+
}
288+
File file = new File(resolvedPath);
289+
if (!file.exists()) {
290+
throw new IOException(
291+
String.format(
292+
"Runfile not found on disk at '%s' (unresolved path: '%s')",
293+
resolvedPath, rlocationPath));
294+
}
295+
return Files.asCharSource(file, UTF_8).read();
296+
}
297+
298+
static boolean hasRunfile(String rlocationPath) {
299+
String resolvedPath = runfiles.rlocation(Ascii.toLowerCase(rlocationPath));
300+
return resolvedPath != null && new File(resolvedPath).exists();
263301
}
264302

265-
private static String readFile(String path) throws IOException {
266-
return Resources.toString(getResource(path), UTF_8);
303+
private static Runfiles createRunfiles() {
304+
try {
305+
return Runfiles.preload().withSourceRepository(AutoBazelRepository_PolicyTestHelper.NAME);
306+
} catch (IOException e) {
307+
throw new RuntimeException("Failed to initialize Runfiles", e);
308+
}
267309
}
268310

269311
private PolicyTestHelper() {}

repositories.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def bazel_common_dependency():
3434
)
3535

3636
def cel_policy_dependency():
37-
cel_policy_tag = "569292f1c4eaa41894c1e37ee94eb146e284bcfa"
38-
cel_policy_sha = "5a68318d906f6ce18492ad6f82b5f8bb083fd9d694cf567d399216c11da03157"
37+
cel_policy_tag = "e4c38defbbf34dfff2dc448dc58e93a9733ae8b1"
38+
cel_policy_sha = "46378e0d17a16465899f9fefc94c3d44e1f40aedd8a31c9c0b2b6198048eabd6"
3939
http_archive(
4040
name = "cel_policy",
4141
sha256 = cel_policy_sha,

testing/src/test/java/dev/cel/testing/testrunner/BUILD.bazel

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,13 @@ java_test(
157157

158158
cel_java_test(
159159
name = "test_runner_sample_yaml",
160-
cel_expr = "nested_rule/policy.yaml",
160+
cel_expr = "@cel_policy//conformance:testdata/nested_rule/policy.yaml",
161161
proto_deps = [
162162
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
163163
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
164164
],
165-
test_data_path = "//testing/src/test/resources/policy",
166165
test_src = ":user_test",
167-
test_suite = "nested_rule/testrunner_tests.yaml",
166+
test_suite = "@cel_policy//conformance:testdata/nested_rule/tests.yaml",
168167
deps = [
169168
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
170169
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
@@ -173,7 +172,7 @@ cel_java_test(
173172

174173
cel_java_test(
175174
name = "unknown_set_yaml",
176-
cel_expr = "nested_rule/policy.yaml",
175+
cel_expr = "@cel_policy//conformance:testdata/nested_rule/policy.yaml",
177176
proto_deps = [
178177
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
179178
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
@@ -211,15 +210,14 @@ cel_java_test(
211210

212211
cel_java_test(
213212
name = "context_pb_user_test_runner_sample",
214-
cel_expr = "context_pb/policy.yaml",
215-
config = "context_pb/config.yaml",
213+
cel_expr = "@cel_policy//conformance:testdata/context_pb/policy.yaml",
214+
config = "@cel_policy//conformance:testdata/context_pb/config.yaml",
216215
proto_deps = [
217216
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
218217
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
219218
],
220-
test_data_path = "//testing/src/test/resources/policy",
221219
test_src = ":context_pb_user_test",
222-
test_suite = "context_pb/tests.yaml",
220+
test_suite = "@cel_policy//conformance:testdata/context_pb/tests.yaml",
223221
deps = [
224222
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
225223
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
@@ -228,15 +226,14 @@ cel_java_test(
228226

229227
cel_java_test(
230228
name = "additional_config_test_runner_sample",
231-
cel_expr = "nested_rule/policy.yaml",
232-
config = "nested_rule/config.yaml",
229+
cel_expr = "@cel_policy//conformance:testdata/nested_rule/policy.yaml",
230+
config = "@cel_policy//conformance:testdata/nested_rule/config.yaml",
233231
proto_deps = [
234232
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
235233
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
236234
],
237-
test_data_path = "//testing/src/test/resources/policy",
238235
test_src = ":env_config_user_test",
239-
test_suite = "nested_rule/testrunner_tests.textproto",
236+
test_suite = "@cel_policy//conformance:testdata/nested_rule/tests.textproto",
240237
deps = [
241238
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
242239
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
@@ -245,14 +242,13 @@ cel_java_test(
245242

246243
cel_java_test(
247244
name = "test_runner_sample",
248-
cel_expr = "nested_rule/policy.yaml",
245+
cel_expr = "@cel_policy//conformance:testdata/nested_rule/policy.yaml",
249246
proto_deps = [
250247
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
251248
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
252249
],
253-
test_data_path = "//testing/src/test/resources/policy",
254250
test_src = ":user_test",
255-
test_suite = "nested_rule/testrunner_tests.textproto",
251+
test_suite = "@cel_policy//conformance:testdata/nested_rule/tests.textproto",
256252
deps = [
257253
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
258254
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
@@ -286,8 +282,8 @@ cel_java_test(
286282

287283
cel_java_test(
288284
name = "context_message_user_test_runner_textproto_sample",
289-
cel_expr = "context_pb/policy.yaml",
290-
config = "context_pb/config.yaml",
285+
cel_expr = "@cel_policy//conformance:testdata/context_pb/policy.yaml",
286+
config = "@cel_policy//conformance:testdata/context_pb/config.yaml",
291287
proto_deps = [
292288
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
293289
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
@@ -303,15 +299,14 @@ cel_java_test(
303299

304300
cel_java_test(
305301
name = "context_pb_user_test_runner_textproto_sample",
306-
cel_expr = "context_pb/policy.yaml",
307-
config = "context_pb/config.yaml",
302+
cel_expr = "@cel_policy//conformance:testdata/context_pb/policy.yaml",
303+
config = "@cel_policy//conformance:testdata/context_pb/config.yaml",
308304
proto_deps = [
309305
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
310306
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
311307
],
312-
test_data_path = "//testing/src/test/resources/policy",
313308
test_src = ":context_pb_user_test",
314-
test_suite = "context_pb/tests.textproto",
309+
test_suite = "@cel_policy//conformance:testdata/context_pb/tests.textproto",
315310
deps = [
316311
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
317312
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",

0 commit comments

Comments
 (0)