Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions policy/src/test/java/dev/cel/policy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ java_library(
name = "tests",
testonly = True,
srcs = glob(["*.java"]),
data = [
"@cel_policy//conformance:testdata",
],
resources = [
"//testing:policy_test_resources",
],
Expand Down Expand Up @@ -39,6 +42,7 @@ java_library(
"//runtime:function_binding",
"//testing:cel_runtime_flavor",
"//testing/protos:single_file_java_proto",
"@bazel_tools//tools/java/runfiles",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
"@maven//:com_google_guava_guava",
"@maven//:com_google_testparameterinjector_test_parameter_injector",
Expand Down
43 changes: 32 additions & 11 deletions policy/src/test/java/dev/cel/policy/CelPolicyCompilerImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.truth.Truth.assertThat;
import static dev.cel.policy.PolicyTestHelper.readFromYaml;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Resources;
import com.google.testing.junit.testparameterinjector.TestParameter;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import com.google.testing.junit.testparameterinjector.TestParameterValue;
Expand Down Expand Up @@ -50,6 +52,7 @@
import dev.cel.testing.testdata.SingleFile;
import dev.cel.testing.testdata.proto3.StandaloneGlobalEnum;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.Optional;
import org.junit.Test;
Expand Down Expand Up @@ -112,9 +115,12 @@ public void compileYamlPolicy_withImportsOnNestedRules() throws Exception {
public void compileYamlPolicy_containsCompilationError_throws(
@TestParameter TestErrorYamlPolicy testCase) throws Exception {
// Read config and produce an environment to compile policies
String configSource = testCase.readConfigYamlContent();
CelEnvironment celEnvironment = ENVIRONMENT_PARSER.parse(configSource);
Cel cel = celEnvironment.extend(newCel(), CEL_OPTIONS);
Optional<String> configSource = testCase.readConfigYamlContent();
Cel baseCel = newCel();
Cel cel =
configSource.isPresent()
? ENVIRONMENT_PARSER.parse(configSource.get()).extend(baseCel, CEL_OPTIONS)
: baseCel;
// Read the policy source
String policySource = testCase.readPolicyYamlContent();
CelPolicy policy = POLICY_PARSER.parse(policySource, testCase.getPolicyFilePath());
Expand Down Expand Up @@ -509,10 +515,14 @@ private enum MultilineErrorTest {
}

private enum TestErrorYamlPolicy {
COMPILE_ERRORS("compile_errors"),
COMPOSE_ERRORS_CONFLICTING_OUTPUT("compose_errors_conflicting_output"),
COMPOSE_ERRORS_CONFLICTING_SUBRULE("compose_errors_conflicting_subrule"),
ERRORS_UNREACHABLE("errors_unreachable");
COMPOSE_ERRORS_CONFLICTING_OUTPUT("compose_conflicting_output"),
COMPOSE_ERRORS_CONFLICTING_SUBRULE("compose_conflicting_subrule"),
ERRORS_UNREACHABLE("unreachable"),
DUPLICATE_VARIABLE("duplicate_variable"),
IMPORT("import"),
INCOMPATIBLE_OUTPUTS("incompatible_outputs"),
SYNTAX("syntax"),
UNDECLARED_REFERENCE("undeclared_reference");

private final String name;
private final String policyFilePath;
Expand All @@ -522,15 +532,26 @@ private String getPolicyFilePath() {
}

private String readPolicyYamlContent() throws IOException {
return readFromYaml(String.format("policy/%s/policy.yaml", name));
return readFromYaml(
String.format(
"cel_policy/conformance/testdata/compile_errors/%s/policy.yaml",
name));
}

private String readConfigYamlContent() throws IOException {
return readFromYaml(String.format("policy/%s/config.yaml", name));
private Optional<String> readConfigYamlContent() throws IOException {
String rlocationPath =
String.format(
"cel_policy/conformance/testdata/compile_errors/%s/config.yaml",
name);
if (PolicyTestHelper.hasRunfile(rlocationPath)) {
return Optional.of(readFromYaml(rlocationPath));
}
return Optional.empty();
}

private String readExpectedErrorsBaseline() throws IOException {
return readFromYaml(String.format("policy/%s/expected_errors.baseline", name));
URL url = Resources.getResource(String.format("policy/%s/expected_errors.baseline", name));
return Resources.toString(url, UTF_8).trim();
}

TestErrorYamlPolicy(String name) {
Expand Down
76 changes: 59 additions & 17 deletions policy/src/test/java/dev/cel/policy/PolicyTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Ascii;
import com.google.common.io.Resources;
import com.google.common.io.Files;
import com.google.devtools.build.runfiles.AutoBazelRepository;
import com.google.devtools.build.runfiles.Runfiles;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

/** Package-private class to assist with policy testing. */
@AutoBazelRepository
final class PolicyTestHelper {

private static final Runfiles runfiles = createRunfiles();

enum TestYamlPolicy {
NESTED_RULE(
"nested_rule",
Expand Down Expand Up @@ -74,11 +79,11 @@ enum TestYamlPolicy {
"required_labels",
true,
"cel.@block([spec.labels.filter(@it:0:0, !(@it:0:0 in resource.labels)), spec.labels,"
+ " resource.labels, @index2.filter(@it:0:0, @it:0:0 in @index1 && @index1[@it:0:0] !="
+ " @index2[@it:0:0])], (@index0.size() > 0) ? optional.of(\"missing one or more"
+ " required labels: [\"\" + @index0.join(\",\") + \"\"]\") : ((@index3.size() > 0) ?"
+ " optional.of(\"invalid values provided on one or more labels: [\"\" +"
+ " @index3.join(\",\") + \"\"]\") : optional.none()))"),
+ " resource.labels.transformList(@it:0:1, @it2:0:1, @it:0:1 in @index1 && @it2:0:1 !="
+ " @index1[@it:0:1], @it:0:1)], (@index0.size() > 0) ? optional.of(\"missing one or"
+ " more required labels: [\"\" + @index0.join(\"\", \"\") + \"\"]\") :"
+ " ((@index2.size() > 0) ? optional.of(\"invalid values provided on one or more"
+ " labels: [\"\" + @index2.join(\"\", \"\") + \"\"]\") : optional.none()))"),
RESTRICTED_DESTINATIONS(
"restricted_destinations",
false,
Expand All @@ -102,9 +107,10 @@ enum TestYamlPolicy {
"cel.@block([spec.single_int32], (@index0 > 10) ? optional.of(\"invalid spec, got"
+ " single_int32=\" + string(@index0) + \", wanted <= 10\") : ((spec.standalone_enum =="
+ " cel.expr.conformance.proto3.TestAllTypes.NestedEnum.BAR ||"
+ " dev.cel.testing.testdata.proto3.StandaloneGlobalEnum.SGAR =="
+ " dev.cel.testing.testdata.proto3.StandaloneGlobalEnum.SGOO) ? optional.of(\"invalid"
+ " spec, neither nested nor imported enums may refer to BAR\") : optional.none()))"),
+ " cel.expr.conformance.proto3.TestAllTypes.NestedEnum.BAZ in"
+ " spec.repeated_nested_enum || cel.expr.conformance.proto3.GlobalEnum.GAR =="
+ " cel.expr.conformance.proto3.GlobalEnum.GOO) ? optional.of(\"invalid spec, neither"
+ " nested nor repeated enums may refer to BAR or BAZ\") : optional.none()))"),
LIMITS(
"limits",
true,
Expand Down Expand Up @@ -136,16 +142,23 @@ String getUnparsed() {
}

String readPolicyYamlContent() throws IOException {
return readFromYaml(String.format("policy/%s/policy.yaml", name));
return readFromYaml(
String.format(
"cel_policy/conformance/testdata/%s/policy.yaml", name));
}

String readConfigYamlContent() throws IOException {
return readFromYaml(String.format("policy/%s/config.yaml", name));
return readFromYaml(
String.format(
"cel_policy/conformance/testdata/%s/config.yaml", name));
}

PolicyTestSuite readTestYamlContent() throws IOException {
Yaml yaml = new Yaml(new Constructor(PolicyTestSuite.class, new LoaderOptions()));
String testContent = readFile(String.format("policy/%s/tests.yaml", name));
String testContent =
readFile(
String.format(
"cel_policy/conformance/testdata/%s/tests.yaml", name));

return yaml.load(testContent);
}
Expand All @@ -163,9 +176,18 @@ static String readFromYaml(String yamlPath) throws IOException {
*/
@VisibleForTesting
public static final class PolicyTestSuite {
private String name;
private String description;
private List<PolicyTestSection> section;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setDescription(String description) {
this.description = description;
}
Expand Down Expand Up @@ -258,12 +280,32 @@ public void setExpr(String expr) {
}
}

private static URL getResource(String path) {
return Resources.getResource(Ascii.toLowerCase(path));
private static String readFile(String rlocationPath) throws IOException {
String resolvedPath = runfiles.rlocation(Ascii.toLowerCase(rlocationPath));
if (resolvedPath == null) {
throw new IOException("Unmapped runfile path: " + rlocationPath);
}
File file = new File(resolvedPath);
if (!file.exists()) {
throw new IOException(
String.format(
"Runfile not found on disk at '%s' (unresolved path: '%s')",
resolvedPath, rlocationPath));
}
return Files.asCharSource(file, UTF_8).read();
}

static boolean hasRunfile(String rlocationPath) {
String resolvedPath = runfiles.rlocation(Ascii.toLowerCase(rlocationPath));
return resolvedPath != null && new File(resolvedPath).exists();
}

private static String readFile(String path) throws IOException {
return Resources.toString(getResource(path), UTF_8);
private static Runfiles createRunfiles() {
try {
return Runfiles.preload().withSourceRepository(AutoBazelRepository_PolicyTestHelper.NAME);
} catch (IOException e) {
throw new RuntimeException("Failed to initialize Runfiles", e);
}
}

private PolicyTestHelper() {}
Expand Down
4 changes: 2 additions & 2 deletions repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def bazel_common_dependency():
)

def cel_policy_dependency():
cel_policy_tag = "569292f1c4eaa41894c1e37ee94eb146e284bcfa"
cel_policy_sha = "5a68318d906f6ce18492ad6f82b5f8bb083fd9d694cf567d399216c11da03157"
cel_policy_tag = "e4c38defbbf34dfff2dc448dc58e93a9733ae8b1"
cel_policy_sha = "46378e0d17a16465899f9fefc94c3d44e1f40aedd8a31c9c0b2b6198048eabd6"
http_archive(
name = "cel_policy",
sha256 = cel_policy_sha,
Expand Down
37 changes: 16 additions & 21 deletions testing/src/test/java/dev/cel/testing/testrunner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,13 @@ java_test(

cel_java_test(
name = "test_runner_sample_yaml",
cel_expr = "nested_rule/policy.yaml",
cel_expr = "@cel_policy//conformance:testdata/nested_rule/policy.yaml",
proto_deps = [
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
],
test_data_path = "//testing/src/test/resources/policy",
test_src = ":user_test",
test_suite = "nested_rule/testrunner_tests.yaml",
test_suite = "@cel_policy//conformance:testdata/nested_rule/tests.yaml",
deps = [
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
Expand All @@ -173,7 +172,7 @@ cel_java_test(

cel_java_test(
name = "unknown_set_yaml",
cel_expr = "nested_rule/policy.yaml",
cel_expr = "@cel_policy//conformance:testdata/nested_rule/policy.yaml",
proto_deps = [
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
Expand Down Expand Up @@ -211,15 +210,14 @@ cel_java_test(

cel_java_test(
name = "context_pb_user_test_runner_sample",
cel_expr = "context_pb/policy.yaml",
config = "context_pb/config.yaml",
cel_expr = "@cel_policy//conformance:testdata/context_pb/policy.yaml",
config = "@cel_policy//conformance:testdata/context_pb/config.yaml",
proto_deps = [
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
],
test_data_path = "//testing/src/test/resources/policy",
test_src = ":context_pb_user_test",
test_suite = "context_pb/tests.yaml",
test_suite = "@cel_policy//conformance:testdata/context_pb/tests.yaml",
deps = [
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
Expand All @@ -228,15 +226,14 @@ cel_java_test(

cel_java_test(
name = "additional_config_test_runner_sample",
cel_expr = "nested_rule/policy.yaml",
config = "nested_rule/config.yaml",
cel_expr = "@cel_policy//conformance:testdata/nested_rule/policy.yaml",
config = "@cel_policy//conformance:testdata/nested_rule/config.yaml",
proto_deps = [
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
],
test_data_path = "//testing/src/test/resources/policy",
test_src = ":env_config_user_test",
test_suite = "nested_rule/testrunner_tests.textproto",
test_suite = "@cel_policy//conformance:testdata/nested_rule/tests.textproto",
deps = [
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
Expand All @@ -245,14 +242,13 @@ cel_java_test(

cel_java_test(
name = "test_runner_sample",
cel_expr = "nested_rule/policy.yaml",
cel_expr = "@cel_policy//conformance:testdata/nested_rule/policy.yaml",
proto_deps = [
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
],
test_data_path = "//testing/src/test/resources/policy",
test_src = ":user_test",
test_suite = "nested_rule/testrunner_tests.textproto",
test_suite = "@cel_policy//conformance:testdata/nested_rule/tests.textproto",
deps = [
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
Expand Down Expand Up @@ -286,8 +282,8 @@ cel_java_test(

cel_java_test(
name = "context_message_user_test_runner_textproto_sample",
cel_expr = "context_pb/policy.yaml",
config = "context_pb/config.yaml",
cel_expr = "@cel_policy//conformance:testdata/context_pb/policy.yaml",
config = "@cel_policy//conformance:testdata/context_pb/config.yaml",
proto_deps = [
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
Expand All @@ -303,15 +299,14 @@ cel_java_test(

cel_java_test(
name = "context_pb_user_test_runner_textproto_sample",
cel_expr = "context_pb/policy.yaml",
config = "context_pb/config.yaml",
cel_expr = "@cel_policy//conformance:testdata/context_pb/policy.yaml",
config = "@cel_policy//conformance:testdata/context_pb/config.yaml",
proto_deps = [
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto",
],
test_data_path = "//testing/src/test/resources/policy",
test_src = ":context_pb_user_test",
test_suite = "context_pb/tests.textproto",
test_suite = "@cel_policy//conformance:testdata/context_pb/tests.textproto",
deps = [
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
Expand Down
Loading
Loading