Skip to content

Commit b659f60

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 b659f60

48 files changed

Lines changed: 159 additions & 1687 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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ java_library(
77
name = "tests",
88
testonly = True,
99
srcs = glob(["*.java"]),
10-
resources = [
10+
data = [
1111
"//testing:policy_test_resources",
12+
"@cel_policy//conformance:testdata",
1213
],
1314
deps = [
1415
"//:java_truth",
@@ -39,6 +40,7 @@ java_library(
3940
"//runtime:function_binding",
4041
"//testing:cel_runtime_flavor",
4142
"//testing/protos:single_file_java_proto",
43+
"@bazel_tools//tools/java/runfiles",
4244
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
4345
"@maven//:com_google_guava_guava",
4446
"@maven//:com_google_testparameterinjector_test_parameter_injector",

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,15 +522,24 @@ private String getPolicyFilePath() {
522522
}
523523

524524
private String readPolicyYamlContent() throws IOException {
525-
return readFromYaml(String.format("policy/%s/policy.yaml", name));
525+
return readFromYaml(
526+
String.format(
527+
"google3/third_party/java/cel/testing/src/test/resources/policy/%s/policy.yaml",
528+
name));
526529
}
527530

528531
private String readConfigYamlContent() throws IOException {
529-
return readFromYaml(String.format("policy/%s/config.yaml", name));
532+
return readFromYaml(
533+
String.format(
534+
"google3/third_party/java/cel/testing/src/test/resources/policy/%s/config.yaml",
535+
name));
530536
}
531537

532538
private String readExpectedErrorsBaseline() throws IOException {
533-
return readFromYaml(String.format("policy/%s/expected_errors.baseline", name));
539+
return readFromYaml(
540+
String.format(
541+
"google3/third_party/java/cel/testing/src/test/resources/policy/%s/expected_errors.baseline",
542+
name));
534543
}
535544

536545
TestErrorYamlPolicy(String name) {

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

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
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.Runfiles;
23+
import java.io.File;
2224
import java.io.IOException;
23-
import java.net.URL;
2425
import java.util.List;
2526
import java.util.Map;
2627
import org.yaml.snakeyaml.LoaderOptions;
@@ -30,6 +31,8 @@
3031
/** Package-private class to assist with policy testing. */
3132
final class PolicyTestHelper {
3233

34+
private static final Runfiles runfiles = createRunfiles();
35+
3336
enum TestYamlPolicy {
3437
NESTED_RULE(
3538
"nested_rule",
@@ -74,11 +77,11 @@ enum TestYamlPolicy {
7477
"required_labels",
7578
true,
7679
"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()))"),
80+
+ " resource.labels.transformList(@it:0:1, @it2:0:1, @it:0:1 in @index1 && @it2:0:1 !="
81+
+ " @index1[@it:0:1], @it:0:1)], (@index0.size() > 0) ? optional.of(\"missing one or"
82+
+ " more required labels: [\"\" + @index0.join(\"\", \"\") + \"\"]\") :"
83+
+ " ((@index2.size() > 0) ? optional.of(\"invalid values provided on one or more"
84+
+ " labels: [\"\" + @index2.join(\"\", \"\") + \"\"]\") : optional.none()))"),
8285
RESTRICTED_DESTINATIONS(
8386
"restricted_destinations",
8487
false,
@@ -102,9 +105,10 @@ enum TestYamlPolicy {
102105
"cel.@block([spec.single_int32], (@index0 > 10) ? optional.of(\"invalid spec, got"
103106
+ " single_int32=\" + string(@index0) + \", wanted <= 10\") : ((spec.standalone_enum =="
104107
+ " 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()))"),
108+
+ " cel.expr.conformance.proto3.TestAllTypes.NestedEnum.BAZ in"
109+
+ " spec.repeated_nested_enum || cel.expr.conformance.proto3.GlobalEnum.GAR =="
110+
+ " cel.expr.conformance.proto3.GlobalEnum.GOO) ? optional.of(\"invalid spec, neither"
111+
+ " nested nor repeated enums may refer to BAR or BAZ\") : optional.none()))"),
108112
LIMITS(
109113
"limits",
110114
true,
@@ -136,16 +140,23 @@ String getUnparsed() {
136140
}
137141

138142
String readPolicyYamlContent() throws IOException {
139-
return readFromYaml(String.format("policy/%s/policy.yaml", name));
143+
return readFromYaml(
144+
String.format(
145+
"cel_policy/conformance/testdata/%s/policy.yaml", name));
140146
}
141147

142148
String readConfigYamlContent() throws IOException {
143-
return readFromYaml(String.format("policy/%s/config.yaml", name));
149+
return readFromYaml(
150+
String.format(
151+
"cel_policy/conformance/testdata/%s/config.yaml", name));
144152
}
145153

146154
PolicyTestSuite readTestYamlContent() throws IOException {
147155
Yaml yaml = new Yaml(new Constructor(PolicyTestSuite.class, new LoaderOptions()));
148-
String testContent = readFile(String.format("policy/%s/tests.yaml", name));
156+
String testContent =
157+
readFile(
158+
String.format(
159+
"cel_policy/conformance/testdata/%s/tests.yaml", name));
149160

150161
return yaml.load(testContent);
151162
}
@@ -163,9 +174,18 @@ static String readFromYaml(String yamlPath) throws IOException {
163174
*/
164175
@VisibleForTesting
165176
public static final class PolicyTestSuite {
177+
private String name;
166178
private String description;
167179
private List<PolicyTestSection> section;
168180

181+
public void setName(String name) {
182+
this.name = name;
183+
}
184+
185+
public String getName() {
186+
return name;
187+
}
188+
169189
public void setDescription(String description) {
170190
this.description = description;
171191
}
@@ -258,12 +278,21 @@ public void setExpr(String expr) {
258278
}
259279
}
260280

261-
private static URL getResource(String path) {
262-
return Resources.getResource(Ascii.toLowerCase(path));
281+
private static String readFile(String rlocationPath) throws IOException {
282+
String resolvedPath = runfiles.rlocation(Ascii.toLowerCase(rlocationPath));
283+
if (resolvedPath == null || !new File(resolvedPath).exists()) {
284+
throw new IOException("Runfile not found: " + rlocationPath);
285+
}
286+
File file = new File(resolvedPath);
287+
return Files.asCharSource(file, UTF_8).read();
263288
}
264289

265-
private static String readFile(String path) throws IOException {
266-
return Resources.toString(getResource(path), UTF_8);
290+
private static Runfiles createRunfiles() {
291+
try {
292+
return Runfiles.create();
293+
} catch (IOException e) {
294+
throw new RuntimeException("Failed to initialize Runfiles", e);
295+
}
267296
}
268297

269298
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/context_pb/config.yaml

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

testing/src/test/resources/policy/context_pb/policy.yaml

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

testing/src/test/resources/policy/context_pb/tests.textproto

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

testing/src/test/resources/policy/context_pb/tests.yaml

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

0 commit comments

Comments
 (0)