| # Copyright 2026 Google LLC |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| """Compositional macro for dimension-based bazel integration testing.""" |
| |
| load("//tools/bazel/sh_wrapper:sh_wrapper.bzl", "sh_wrapper", "sh_wrapper_test") |
| load( |
| ":integrations.bzl", |
| "INTEGRATIONS", |
| "get_integration_env", |
| "get_integration_sidecars", |
| "get_integration_verifiers", |
| ) |
| |
| def bazel_integration_test( |
| name, |
| workload, |
| outcome, |
| integration = "PLAIN", |
| tags = [], |
| visibility = None, |
| timeout = "moderate", |
| **kwargs |
| ): |
| """Composes a bazel integration test from a workload target and an outcome profile. |
| |
| This macro assembles a test by chaining multiple sh_wrapper targets. |
| The order of the chain is (from outermost to innermost): |
| 1. Exit Code Expectation (e.g., expect_success) |
| 2. Result Verifiers (ResultStore, Buildminder) |
| 3. Middleware/Sidecars (rsproxy, buildminder sidecar) |
| 4. Scenario Driver (e.g., send_sigint) |
| 5. Bazel Invocation (The Workload) |
| |
| Args: |
| name: Name of the test target. |
| workload: Label of a bazel_invocation target (Workload Dimension). |
| outcome: A struct from outcomes.bzl (Outcome Dimension). |
| integration: The integration configuration to test (Integration Dimension). |
| One of PLAIN, BUILDMINDER, RESULTSTORE, BUILDMINDER_RESULTSTORE. |
| tags: Standard bazel tags. |
| visibility: Target visibility. |
| timeout: Default to 'moderate' (60s) because bazel startup is slow. |
| **kwargs: Passed to the underlying sh_wrapper_test. |
| """ |
| if integration not in INTEGRATIONS: |
| fail("Invalid integration '{}'. Allowed values are: {}".format(integration, ", ".join(INTEGRATIONS))) |
| |
| chain = [] |
| env = {} |
| |
| # 0. Integration Specific Setup |
| env.update(get_integration_env(integration)) |
| |
| # 1. Exit Code Expectation (Outermost) |
| if outcome.exit_expectation: |
| chain.append(outcome.exit_expectation) |
| |
| # 2. Result Verification (Outcome Dimension) |
| chain.extend(get_integration_verifiers(integration, outcome)) |
| |
| # 3. Middleware / Sidecars (Integration Dimension) |
| chain.extend(get_integration_sidecars(integration)) |
| |
| # 4. Behavioral Driver (Outcome Dimension) |
| if outcome.driver: |
| chain.append(outcome.driver) |
| |
| # 5. The Build Plan (Workload Dimension - Innermost) |
| chain.append(workload) |
| |
| sh_wrapper_test( |
| name = name, |
| testonly = True, |
| chain = chain, |
| env = env, |
| tags = tags, |
| visibility = visibility, |
| timeout = timeout, |
| **kwargs |
| ) |
| |
| def _verification_wrapper_base(name, script, orchestrator = None): |
| """Internal base for declaring verification wrappers. |
| |
| Args: |
| name: Name of the sh_wrapper target. |
| script: Path to the shell script containing the verification logic. |
| orchestrator: Optional label of a tool that executes the script. |
| """ |
| sh_name = name + "_sh" |
| native.sh_binary( |
| name = sh_name, |
| testonly = True, |
| srcs = [script], |
| ) |
| |
| data = ["//prebuilt:jq", script] |
| args = ["--jq", "$(location //prebuilt:jq)"] |
| |
| if orchestrator: |
| executable = orchestrator |
| args = ["--check_dump_script", "$(location %s)" % script] + args |
| else: |
| executable = ":" + sh_name |
| |
| args.append("--") |
| |
| sh_wrapper( |
| name = name, |
| testonly = True, |
| args = args, |
| data = data, |
| executable = executable, |
| position = "nonterminal", |
| ) |
| |
| def bazel_resultstore_verification_wrapper(name, script): |
| """Declares a ResultStore verification wrapper using check_dump_wrapper_script. |
| |
| Args: |
| name: Name of the sh_wrapper target. |
| script: Path to the shell script that verifies the ResultStore JSON dump. |
| """ |
| _verification_wrapper_base( |
| name = name, |
| script = script, |
| orchestrator = "//cmd/fake_resultstore:check_dump_wrapper_script", |
| ) |
| |
| def bazel_buildminder_verification_wrapper(name, script): |
| """Declares a Buildminder verification wrapper. |
| |
| Args: |
| name: Name of the sh_wrapper target. |
| script: Path to the shell script that verifies the Buildminder log events. |
| """ |
| _verification_wrapper_base( |
| name = name, |
| script = script, |
| orchestrator = None, |
| ) |