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
22 changes: 10 additions & 12 deletions bazel/bundle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
r"""Script to bundle CEL policy conformance tests.

Each CEL policy conformance test is a self-contained directory containing an
optional environment, a policy, and a test suite. This script
optional environment (config.yaml), a policy, and a test suite. This script
bundles them all together into a single multidocument YAML file to simplify file
loading.

Expand All @@ -22,7 +22,6 @@

Usage:
bazel run //third_party/cel/policy/bazel:bundle -- \
--environment=<environment file> \
--policy=<policy file> \
--test=<test file> \
--output=<path to output file>
Expand All @@ -35,16 +34,21 @@


def _read_and_format_section(path: str) -> bytes:
# We're assuming UTF-8 here. Unclear if all of the CEL policy parsers
# correctly handle non-UTF-8 since they directly read some parts of the source
# instead of relying on the yaml parse.
with open(path, "rb") as f:
content = f.read().rstrip()
comment = f"# {os.path.basename(path)}".encode("utf-8")
return comment + b"\n" + content


def bundle(environment: str | None, policy: str, test: str) -> bytes:
def bundle(policy: str, test: str) -> bytes:
sections = []
if environment:
sections.append(_read_and_format_section(environment))
policy_dir = os.path.dirname(policy)
config_path = os.path.join(policy_dir, "config.yaml")
if os.path.exists(config_path):
sections.append(_read_and_format_section(config_path))
sections.append(_read_and_format_section(policy))
sections.append(_read_and_format_section(test))
return b"\n---\n".join(sections) + b"\n"
Expand All @@ -54,12 +58,6 @@ def main(argv: Sequence[str]) -> None:
parser = argparse.ArgumentParser(
description="Bundle CEL policy conformance tests."
)
parser.add_argument(
"--environment",
help="Path to the environment file.",
required=False,
default=None,
)
parser.add_argument(
"--policy",
help="Path to the policy file.",
Expand All @@ -76,7 +74,7 @@ def main(argv: Sequence[str]) -> None:
required=True,
)
args = parser.parse_args(argv[1:])
bundled_content = bundle(args.environment, args.policy, args.test)
bundled_content = bundle(args.policy, args.test)
with open(args.output, "wb") as f:
f.write(bundled_content)

Expand Down
24 changes: 11 additions & 13 deletions bazel/test_bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,10 @@
"""Defines a bazel rule for bundling CEL policy conformance tests."""

def _cel_policy_test_bundle_impl(ctx):
inputs = []
inputs = [ctx.file.policy, ctx.file.test] + ctx.files._testdata
args = ctx.actions.args()
if ctx.file.environment:
inputs.append(ctx.file.environment)
args.add("--environment", ctx.file.environment.path)
if ctx.file.policy:
inputs.append(ctx.file.policy)
args.add("--policy", ctx.file.policy.path)
if ctx.file.test:
inputs.append(ctx.file.test)
args.add("--test", ctx.file.test.path)

args.add("--policy", ctx.file.policy.path)
args.add("--test", ctx.file.test.path)
args.add("--output", ctx.outputs.out.path)

ctx.actions.run(
Expand All @@ -45,14 +37,20 @@ cel_policy_test_bundle = rule(
Output file is a YAML file with three documents: environment, policy, and
test.

If there is no environment file, the environment document will be omitted.
If there is no config.yaml in the policy directory, the environment
document will be omitted.
""",
implementation = _cel_policy_test_bundle_impl,
outputs = {"out": "%{name}_bundle.yaml"},
attrs = {
"environment": attr.label(allow_single_file = [".yaml"]),
"policy": attr.label(allow_single_file = [".yaml"], mandatory = True),
"test": attr.label(allow_single_file = [".yaml"], mandatory = True),
# We include the entire testdata tree as an input so the bundler can lookup optional files
# like config.yaml. May revisit if the bundle format becomes the default instead of just for
# C++.
"_testdata": attr.label(
default = "//conformance:testdata",
),
"_bundle_tool": attr.label(
default = "//bazel:bundle",
executable = True,
Expand Down
29 changes: 15 additions & 14 deletions conformance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ exports_files(
)

_TEST_DIRS = [
"aggregate",
"aggregate_explicit_list_output",
"aggregate_explicit_optional_none",
"aggregate_nested_explicit_list_double_wrapping",
"aggregate_shadowed_variables",
"context_pb",
"first_match_nested_aggregate",
"k8s",
"limits",
"nested_rule",
Expand All @@ -40,34 +46,29 @@ _TEST_DIRS = [
"variable_type_propagation",
]

_TEST_ERROR_DIRS_WITH_ENV = [
_TEST_ERROR_DIRS = [
"aggregate_false_condition",
"aggregate_heterogeneous_outputs",
"aggregate_nested_mixed_semantics",
"aggregate_subrule_heterogeneous_outputs",
"aggregate_unreachable_in_nested_first_match",
"compose_conflicting_output",
"compose_conflicting_subrule",
"unreachable",
]

_TEST_ERROR_DIRS = [
"duplicate_variable",
"import",
"incompatible_outputs",
"syntax",
"undeclared_reference",
"unreachable",
"unreachable_under_unconditional_aggregate_subrule",
]

[cel_policy_test_bundle(
name = dir_name,
environment = "testdata/%s/config.yaml" % dir_name,
policy = "testdata/%s/policy.yaml" % dir_name,
test = "testdata/%s/tests.yaml" % dir_name,
) for dir_name in _TEST_DIRS]

[cel_policy_test_bundle(
name = "compile_error_" + dir_name,
environment = "testdata/compile_errors/%s/config.yaml" % dir_name,
policy = "testdata/compile_errors/%s/policy.yaml" % dir_name,
test = "testdata/compile_errors/%s/tests.yaml" % dir_name,
) for dir_name in _TEST_ERROR_DIRS_WITH_ENV]

[cel_policy_test_bundle(
name = "compile_error_" + dir_name,
policy = "testdata/compile_errors/%s/policy.yaml" % dir_name,
Expand All @@ -81,6 +82,6 @@ filegroup(
for dir_name in _TEST_DIRS
] + [
":compile_error_" + dir_name
for dir_name in _TEST_ERROR_DIRS_WITH_ENV + _TEST_ERROR_DIRS
for dir_name in _TEST_ERROR_DIRS
],
)
Loading