Skip to content

Commit 4b6975f

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 4b6975f

57 files changed

Lines changed: 176 additions & 2017 deletions

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ java_library(
77
name = "tests",
88
testonly = True,
99
srcs = glob(["*.java"]),
10+
data = [
11+
"//testing:policy_test_resources",
12+
"@cel_policy//conformance:testdata",
13+
],
1014
resources = [
1115
"//testing:policy_test_resources",
1216
],
@@ -39,6 +43,7 @@ java_library(
3943
"//runtime:function_binding",
4044
"//testing:cel_runtime_flavor",
4145
"//testing/protos:single_file_java_proto",
46+
"@bazel_tools//tools/java/runfiles",
4247
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
4348
"@maven//:com_google_guava_guava",
4449
"@maven//:com_google_testparameterinjector_test_parameter_injector",

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -509,33 +509,43 @@ private enum MultilineErrorTest {
509509
}
510510

511511
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");
516-
517-
private final String name;
512+
COMPOSE_ERRORS_CONFLICTING_OUTPUT(
513+
"compose_errors_conflicting_output", "compose_conflicting_output"),
514+
COMPOSE_ERRORS_CONFLICTING_SUBRULE(
515+
"compose_errors_conflicting_subrule", "compose_conflicting_subrule"),
516+
ERRORS_UNREACHABLE("errors_unreachable", "unreachable");
517+
518+
private final String localBaselineDir;
519+
private final String conformanceDir;
518520
private final String policyFilePath;
519521

520522
private String getPolicyFilePath() {
521523
return policyFilePath;
522524
}
523525

524526
private String readPolicyYamlContent() throws IOException {
525-
return readFromYaml(String.format("policy/%s/policy.yaml", name));
527+
return readFromYaml(
528+
String.format(
529+
"cel_policy/conformance/testdata/compile_errors/%s/policy.yaml",
530+
conformanceDir));
526531
}
527532

528533
private String readConfigYamlContent() throws IOException {
529-
return readFromYaml(String.format("policy/%s/config.yaml", name));
534+
return readFromYaml(
535+
String.format(
536+
"cel_policy/conformance/testdata/compile_errors/%s/config.yaml",
537+
conformanceDir));
530538
}
531539

532540
private String readExpectedErrorsBaseline() throws IOException {
533-
return readFromYaml(String.format("policy/%s/expected_errors.baseline", name));
541+
return PolicyTestHelper.readBaseline(
542+
String.format("policy/%s/expected_errors.baseline", localBaselineDir));
534543
}
535544

536-
TestErrorYamlPolicy(String name) {
537-
this.name = name;
538-
this.policyFilePath = String.format("%s/policy.yaml", name);
545+
TestErrorYamlPolicy(String localBaselineDir, String conformanceDir) {
546+
this.localBaselineDir = localBaselineDir;
547+
this.conformanceDir = conformanceDir;
548+
this.policyFilePath = String.format("%s/policy.yaml", localBaselineDir);
539549
}
540550
}
541551
}

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

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
import com.google.common.annotations.VisibleForTesting;
2020
import com.google.common.base.Ascii;
21+
import com.google.common.io.Files;
2122
import com.google.common.io.Resources;
23+
import com.google.devtools.build.runfiles.Runfiles;
24+
import java.io.File;
2225
import java.io.IOException;
2326
import java.net.URL;
2427
import java.util.List;
@@ -30,6 +33,8 @@
3033
/** Package-private class to assist with policy testing. */
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,26 @@ 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 || !new File(resolvedPath).exists()) {
286+
throw new IOException("Runfile not found: " + rlocationPath);
287+
}
288+
File file = new File(resolvedPath);
289+
return Files.asCharSource(file, UTF_8).read();
263290
}
264291

265-
private static String readFile(String path) throws IOException {
266-
return Resources.toString(getResource(path), UTF_8);
292+
static String readBaseline(String path) throws IOException {
293+
URL url = Resources.getResource(path);
294+
return Resources.toString(url, UTF_8);
295+
}
296+
297+
private static Runfiles createRunfiles() {
298+
try {
299+
return Runfiles.preload().unmapped();
300+
} catch (IOException e) {
301+
throw new RuntimeException("Failed to initialize Runfiles", e);
302+
}
267303
}
268304

269305
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",

testing/src/test/resources/policy/compile_errors/config.yaml

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)