| 1 |
# Copyright 2025 Google LLC |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
""" |
| 16 |
Starlark rule for triggering unit tests on CEL policies and expressions for go |
| 17 |
runtime target. |
| 18 |
""" |
| 19 |
|
| 20 |
load("@bazel_skylib//lib:paths.bzl", "paths") |
| 21 |
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") |
| 22 |
|
| 23 |
def cel_go_test( |
| 24 |
name, |
| 25 |
test_suite = "", |
| 26 |
config = "", |
| 27 |
test_src = "", |
| 28 |
cel_expr = "", |
| 29 |
is_raw_expr = False, |
| 30 |
base_config = "", |
| 31 |
test_data_path = "", |
| 32 |
file_descriptor_set = "", |
| 33 |
filegroup = "", |
| 34 |
enable_coverage = False, |
| 35 |
deps = [], |
| 36 |
data = [], |
| 37 |
**kwargs): |
| 38 |
"""trigger tests for a CEL checked expression |
| 39 |
|
| 40 |
This macro wraps an invocation of the go_test rule and is used to trigger tests for a CEL |
| 41 |
checked expression. |
| 42 |
|
| 43 |
Args: |
| 44 |
name: str name for the generated artifact |
| 45 |
test_suite: str label of a file containing a cel.expr.conformance.test.TestSuite message in |
| 46 |
Textproto format. Alternatively, the file can also contain a test.Suite object in YAML format. |
| 47 |
config: str label of a file containing a google.api.expr.conformance.Environment message in |
| 48 |
Textproto format. Alternatively, the file can also contain the environment configuration represented |
| 49 |
as an env.Config object in YAML format. |
| 50 |
base_config: str label of a file containing a google.api.expr.conformance.Environment message |
| 51 |
in Textproto format. Alternatively, the file can also contain the environment configuration represented |
| 52 |
as an env.Config object in YAML format. |
| 53 |
test_src: source file containing the invocaton of the test runner. |
| 54 |
cel_expr: str label of a file containing either a CEL policy or a CEL expression or a checked |
| 55 |
expression or a raw CEL expression string. |
| 56 |
is_raw_expr: boolean indicating if the cel_expr is a raw CEL expression string. |
| 57 |
test_data_path: path of the directory containing the test files. This is needed only |
| 58 |
if the test files are not located in the same directory as the BUILD file. |
| 59 |
file_descriptor_set: str label or filename pointing to a file_descriptor_set message. Note: |
| 60 |
this must be in binary format with either a .binarypb or .pb or.fds extension. If you need |
| 61 |
to support a textformat file_descriptor_set, embed it in the environment file. (default None) |
| 62 |
filegroup: str label of a filegroup containing the test suite, config, and cel_expr. |
| 63 |
enable_coverage: boolean indicating if coverage should be enabled for the test. |
| 64 |
deps: list of dependencies for the go_test rule |
| 65 |
data: list of data dependencies for the go_test rule |
| 66 |
**kwargs: additional arguments to pass to the go_test rule |
| 67 |
""" |
| 68 |
|
| 69 |
_, cel_expr_format = paths.split_extension(cel_expr) |
| 70 |
if filegroup != "": |
| 71 |
data = data + [filegroup] |
| 72 |
elif test_data_path != "" and test_data_path != native.package_name(): |
| 73 |
if config != "": |
| 74 |
data = data + [test_data_path + ":" + config] |
| 75 |
if base_config != "": |
| 76 |
data = data + [test_data_path + ":" + base_config] |
| 77 |
if test_suite != "": |
| 78 |
data = data + [test_data_path + ":" + test_suite] |
| 79 |
if cel_expr_format == ".cel" or cel_expr_format == ".celpolicy" or cel_expr_format == ".yaml": |
| 80 |
data = data + [test_data_path + ":" + cel_expr] |
| 81 |
else: |
| 82 |
test_data_path = native.package_name() |
| 83 |
if config != "": |
| 84 |
data = data + [config] |
| 85 |
if base_config != "": |
| 86 |
data = data + [base_config] |
| 87 |
if test_suite != "": |
| 88 |
data = data + [test_suite] |
| 89 |
if cel_expr_format == ".cel" or cel_expr_format == ".celpolicy" or cel_expr_format == ".yaml": |
| 90 |
data = data + [cel_expr] |
| 91 |
|
| 92 |
test_data_path = test_data_path.lstrip("/") |
| 93 |
|
| 94 |
if test_suite != "": |
| 95 |
test_suite = test_data_path + "/" + test_suite |
| 96 |
|
| 97 |
if config != "": |
| 98 |
config = test_data_path + "/" + config |
| 99 |
|
| 100 |
if base_config != "": |
| 101 |
base_config = test_data_path + "/" + base_config |
| 102 |
|
| 103 |
srcs = [test_src] |
| 104 |
|
| 105 |
args = [ |
| 106 |
"--test_suite_path=%s" % test_suite, |
| 107 |
"--config_path=%s" % config, |
| 108 |
"--base_config_path=%s" % base_config, |
| 109 |
"--enable_coverage=%s" % enable_coverage, |
| 110 |
] |
| 111 |
|
| 112 |
if cel_expr_format == ".cel" or cel_expr_format == ".celpolicy" or cel_expr_format == ".yaml": |
| 113 |
args.append("--cel_expr=%s" % test_data_path + "/" + cel_expr) |
| 114 |
elif is_raw_expr: |
| 115 |
data = data + [cel_expr] |
| 116 |
args.append("--cel_expr=%s" % cel_expr) |
| 117 |
else: |
| 118 |
args.append("--cel_expr=$(location {})".format(cel_expr)) |
| 119 |
|
| 120 |
if file_descriptor_set != "": |
| 121 |
data = data + [file_descriptor_set] |
| 122 |
args.append("--file_descriptor_set=$(location {})".format(file_descriptor_set)) |
| 123 |
|
| 124 |
go_test( |
| 125 |
name = name, |
| 126 |
srcs = srcs, |
| 127 |
args = args, |
| 128 |
data = data, |
| 129 |
deps = deps + [ |
| 130 |
"//cel:go_default_library", |
| 131 |
"//common/types:go_default_library", |
| 132 |
"//common/types/ref:go_default_library", |
| 133 |
"//interpreter:go_default_library", |
| 134 |
"//test:go_default_library", |
| 135 |
"//tools/celtest:go_default_library", |
| 136 |
"//tools/compiler:go_default_library", |
| 137 |
"@com_github_google_go_cmp//cmp:go_default_library", |
| 138 |
"@dev_cel_expr//:expr", |
| 139 |
"@dev_cel_expr//conformance/test:go_default_library", |
| 140 |
"@in_yaml_go_yaml_v3//:go_default_library", |
| 141 |
"@io_bazel_rules_go//go/runfiles", |
| 142 |
"@org_golang_google_genproto_googleapis_api//expr/v1alpha1:go_default_library", |
| 143 |
"@org_golang_google_protobuf//encoding/prototext:go_default_library", |
| 144 |
"@org_golang_google_protobuf//proto:go_default_library", |
| 145 |
"@org_golang_google_protobuf//reflect/protodesc:go_default_library", |
| 146 |
"@org_golang_google_protobuf//reflect/protoreflect:go_default_library", |
| 147 |
"@org_golang_google_protobuf//reflect/protoregistry:go_default_library", |
| 148 |
"@org_golang_google_protobuf//testing/protocmp:go_default_library", |
| 149 |
"@org_golang_google_protobuf//types/descriptorpb:go_default_library", |
| 150 |
"@org_golang_google_protobuf//types/dynamicpb:go_default_library", |
| 151 |
], |
| 152 |
**kwargs |
| 153 |
) |