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
45 changes: 36 additions & 9 deletions testing/src/test/java/dev/cel/testing/testrunner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ java_test(
cel_java_test(
name = "test_runner_sample_yaml",
cel_expr = "nested_rule/policy.yaml",
file_descriptor_set = "//testing/src/test/java/dev/cel/testing/testrunner:test_all_types_fds",
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",
Expand All @@ -156,7 +159,10 @@ cel_java_test(
name = "test_runner_yaml_sample_with_eval_error",
cel_expr = "nested_rule/eval_error_policy.yaml",
config = "nested_rule/eval_error_config.yaml",
file_descriptor_set = ":test_all_types_fds",
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/eval_error_tests.yaml",
Expand All @@ -166,7 +172,10 @@ cel_java_test(
name = "context_pb_user_test_runner_sample",
cel_expr = "context_pb/policy.yaml",
config = "context_pb/config.yaml",
file_descriptor_set = ":test_all_types_fds",
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",
Expand All @@ -176,7 +185,10 @@ cel_java_test(
name = "additional_config_test_runner_sample",
cel_expr = "nested_rule/policy.yaml",
config = "nested_rule/config.yaml",
file_descriptor_set = ":test_all_types_fds",
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",
Expand All @@ -185,7 +197,10 @@ cel_java_test(
cel_java_test(
name = "test_runner_sample",
cel_expr = "nested_rule/policy.yaml",
file_descriptor_set = ":test_all_types_fds",
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",
Expand Down Expand Up @@ -213,7 +228,10 @@ cel_java_test(
name = "test_runner_sample_with_eval_error",
cel_expr = "nested_rule/eval_error_policy.yaml",
config = "nested_rule/eval_error_config.yaml",
file_descriptor_set = ":test_all_types_fds",
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/eval_error_tests.textproto",
Expand All @@ -223,7 +241,10 @@ cel_java_test(
name = "context_message_user_test_runner_textproto_sample",
cel_expr = "context_pb/policy.yaml",
config = "context_pb/config.yaml",
file_descriptor_set = ":test_all_types_fds",
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/context_msg_tests.textproto",
Expand All @@ -233,7 +254,10 @@ cel_java_test(
name = "context_pb_user_test_runner_textproto_sample",
cel_expr = "context_pb/policy.yaml",
config = "context_pb/config.yaml",
file_descriptor_set = ":test_all_types_fds",
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",
Expand All @@ -251,8 +275,11 @@ cel_java_test(
cel_java_test(
name = "extension_as_input_test",
cel_expr = "2 + 2 == 4",
file_descriptor_set = ":test_all_types_fds",
is_raw_expr = True,
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 = "protoextension_value_as_input/tests.textproto",
Expand Down
1 change: 1 addition & 0 deletions testing/testrunner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@ bzl_library(
deps = [
"@bazel_skylib//lib:paths",
"@rules_java//java:core_rules",
"@rules_proto//proto:defs",
],
)
78 changes: 46 additions & 32 deletions testing/testrunner/cel_java_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Rules for triggering the java impl of the CEL test runner."""

load("@rules_java//java:java_binary.bzl", "java_binary")
load("@rules_proto//proto:defs.bzl", "proto_descriptor_set")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
load("@bazel_skylib//lib:paths.bzl", "paths")

Expand All @@ -27,17 +28,19 @@ def cel_java_test(
filegroup = "",
config = "",
deps = [],
proto_deps = [],
test_data_path = "",
data = [],
file_descriptor_set = None):
data = []):
"""trigger the java impl of the CEL test runner.

This rule will generate a java_binary and a run_test rule. This rule will be used to trigger
the java impl of the cel_test rule.

Note: This rule is to be used only for OSS until cel/expr folder is made available in OSS. Internally,
the cel_test rule is supposed to be used.

Args:
name: str name for the generated artifact
test_class: str fully qualified user's test class name.
test_suite: str label of a file containing a test suite. The file should have a .yaml or a
.textproto extension.
cel_expr: cel expression to be evaluated. This could be a raw expression or a compiled
Expand All @@ -51,39 +54,18 @@ def cel_java_test(
test_src: user's test class build target.
deps: list of dependencies for the java_binary rule.
data: list of data dependencies for the java_binary rule.
file_descriptor_set: str label or filename pointing to a file_descriptor_set message. Note:
this must be in binary format. If you need to support a textformat file_descriptor_set,
embed it in the environment file. (default None)
proto_deps: str label of the proto dependencies for the test. Note: This only supports proto_library rules.
test_data_path: absolute path of the directory containing the test files. This is needed only
if the test files are not located in the same directory as the BUILD file. This
would be of the form "//foo/bar".
"""
jvm_flags = []

# TODO: File path computation will be removed once the cel_java_test
# rule is updated to be integrated with the cel_test rule in order to avoid repetition.
_, cel_expr_format = paths.split_extension(cel_expr)
if filegroup != "":
data = data + [filegroup]
elif test_data_path != "" and test_data_path != native.package_name():
if test_suite != "":
data = data + [test_data_path + ":" + test_suite]
if config != "":
data = data + [test_data_path + ":" + config]
if is_valid_cel_file_format(file_extension = cel_expr_format):
data = data + [test_data_path + ":" + cel_expr]
else:
test_data_path = native.package_name()
if test_suite != "":
data = data + [test_suite]
if config != "":
data = data + [config]
if is_valid_cel_file_format(file_extension = cel_expr_format):
data = data + [cel_expr]
data, test_data_path = _update_data_with_test_files(data, filegroup, test_data_path, config, test_suite, cel_expr, is_raw_expr)

# Since the test_data_path is of the form "//foo/bar", we need to strip the leading "/" to get
# the absolute path.
test_data_path = test_data_path.lstrip("/")
jvm_flags = []

if test_suite != "":
test_suite = test_data_path + "/" + test_suite
Expand All @@ -93,17 +75,23 @@ def cel_java_test(
config = test_data_path + "/" + config
jvm_flags.append("-Dconfig_path=%s" % config)

if file_descriptor_set != None:
data.append(file_descriptor_set)
jvm_flags.append("-Dfile_descriptor_set_path=$(location {})".format(file_descriptor_set))
_, cel_expr_format = paths.split_extension(cel_expr)

if is_valid_cel_file_format(file_extension = cel_expr_format) == True:
jvm_flags.append("-Dcel_expr=%s" % test_data_path + "/" + cel_expr)
elif is_raw_expr == True:
jvm_flags.append("-Dcel_expr='%s'" % cel_expr)
else:
elif not is_valid_cel_file_format(file_extension = cel_expr_format) and not is_raw_expr:
jvm_flags.append("-Dcel_expr=$(location {})".format(cel_expr))
data = data + [cel_expr]

if proto_deps:
proto_descriptor_set(
name = name + "_proto_descriptor_set",
deps = proto_deps,
)
descriptor_set_path = ":" + name + "_proto_descriptor_set"
data.append(descriptor_set_path)
jvm_flags.append("-Dfile_descriptor_set_path=$(location {})".format(descriptor_set_path))

jvm_flags.append("-Dis_raw_expr=%s" % is_raw_expr)

Expand Down Expand Up @@ -136,6 +124,32 @@ def cel_java_test(
],
)

def _update_data_with_test_files(data, filegroup, test_data_path, config, test_suite, cel_expr, is_raw_expr):
"""Updates the data with the test files."""

_, cel_expr_format = paths.split_extension(cel_expr)
if filegroup != "":
data = data + [filegroup]
elif test_data_path != "" and test_data_path != native.package_name():
if config != "":
data = data + [test_data_path + ":" + config]
if test_suite != "":
data = data + [test_data_path + ":" + test_suite]
if is_valid_cel_file_format(file_extension = cel_expr_format):
data = data + [test_data_path + ":" + cel_expr]
else:
test_data_path = native.package_name()
if config != "":
data = data + [config]
if test_suite != "":
data = data + [test_suite]
if is_valid_cel_file_format(file_extension = cel_expr_format):
data = data + [cel_expr]

if not is_valid_cel_file_format(file_extension = cel_expr_format) and not is_raw_expr:
data = data + [cel_expr]
return data, test_data_path

def is_valid_cel_file_format(file_extension):
"""Checks if the file extension is a valid CEL file format.

Expand Down
Loading