| #!/bin/bash |
| # Copyright 2025 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| set -euo pipefail |
| |
| usage() { |
| cat <<EOF |
| Usage: $0 [test_options...] -- [wrapped_command...] |
| |
| This script is a test driver for rsproxy-wrap.sh. It starts a fake ResultStore |
| server, runs the wrapper script with a given command, and verifies that the |
| command exits with the expected status code. |
| |
| This script is intended to be run as a Bazel sh_test. |
| |
| Test Options: |
| -h, --help Show this help message. |
| --fake_server_bin <path> (Required) Path to the fake_resultstore binary. |
| --rsproxy_wrap_sh <path> (Required) Path to the rsproxy-wrap.sh script. |
| --expected_exit_code <code> (Required) The expected exit code of the wrapped command. |
| --wrapper_options [opts...] A list of options to pass to rsproxy-wrap.sh. |
| This must be terminated by '--'. |
| EOF |
| } |
| |
| # This test script is intended to be run via `bazel test`. |
| # It parses its own arguments and then passes them to the wrapper script. |
| |
| FAKE_SERVER_BIN="" |
| RSPROXY_WRAP_SH="" |
| EXPECTED_EXIT_CODE="" |
| WRAPPER_OPTIONS=() |
| |
| # Parse arguments for this test script. |
| while [[ $# -gt 0 ]]; do |
| case "$1" in |
| -h | --help) |
| usage |
| exit 0 |
| ;; |
| --fake_server_bin) FAKE_SERVER_BIN="$2"; shift 2 ;; |
| --rsproxy_wrap_sh) RSPROXY_WRAP_SH="$2"; shift 2 ;; |
| --expected_exit_code) EXPECTED_EXIT_CODE="$2"; shift 2 ;; |
| --wrapper_options) |
| shift |
| while [[ $# -gt 0 && "$1" != "--" ]]; do |
| WRAPPER_OPTIONS+=("$1") |
| shift |
| done |
| ;; |
| --) shift; break ;; |
| *) echo "Unknown option: $1" >&2; exit 1 ;; |
| esac |
| done |
| |
| if [[ -z "${FAKE_SERVER_BIN}" || -z "${RSPROXY_WRAP_SH}" || -z "${EXPECTED_EXIT_CODE}" ]]; then |
| echo "Error: Missing one or more required tool flags." >&2 |
| exit 1 |
| fi |
| |
| |
| if [[ $# -eq 0 ]]; then |
| echo "Error: No command provided for the wrapper to run." >&2 |
| exit 1 |
| fi |
| |
| # Use the Bazel-provided temporary directory for all test artifacts. |
| readonly RS_PORT_FILE="${TEST_TMPDIR}/rs.port" |
| readonly CAS_PORT_FILE="${TEST_TMPDIR}/cas.port" |
| readonly RS_CFG_FILE="${TEST_TMPDIR}/rs.cfg" |
| readonly LOG_DIR="${TEST_TMPDIR}/logs" |
| readonly FAKE_SERVER_LOG="${TEST_TMPDIR}/fake_server.log" |
| |
| # Start the fake server in the background. |
| # Use --logtostderr to prevent glog from creating log files on disk, which |
| # can cause "file exists" errors in parallel test environments. |
| "${FAKE_SERVER_BIN}" --logtostderr --rs_port_file="${RS_PORT_FILE}" --cas_port_file="${CAS_PORT_FILE}" &> "${FAKE_SERVER_LOG}" & |
| FAKE_SERVER_PID=$! |
| trap "kill ${FAKE_SERVER_PID} 2>/dev/null || true" EXIT |
| |
| # Wait for the server to create its port files. |
| for _ in $(seq 1 50); do |
| if [[ -f "${RS_PORT_FILE}" && -f "${CAS_PORT_FILE}" ]]; then |
| break |
| fi |
| sleep 0.1 |
| done |
| if [[ ! -f "${RS_PORT_FILE}" || ! -f "${CAS_PORT_FILE}" ]]; then |
| echo "Error: Fake server did not start within 5 seconds." >&2 |
| echo "--- FAKE SERVER LOG ---" >&2 |
| cat "${FAKE_SERVER_LOG}" >&2 |
| echo "--- END FAKE SERVER LOG ---" >&2 |
| exit 1 |
| fi |
| RS_PORT=$(cat "${RS_PORT_FILE}") |
| CAS_PORT=$(cat "${CAS_PORT_FILE}") |
| |
| # Create a config file that points to the fake server. |
| cat > "${RS_CFG_FILE}" <<EOF |
| rs_service=localhost:${RS_PORT} |
| rs_instance=projects/p/instances/i |
| cas_service=localhost:${CAS_PORT} |
| cas_instance=projects/p/instances/i |
| service_no_security=true |
| EOF |
| |
| # Run the wrapper script with the provided command. |
| # Capture the exit code and compare it against the expected value. |
| exit_code=0 |
| "${RSPROXY_WRAP_SH}" \ |
| "${WRAPPER_OPTIONS[@]}" \ |
| --log-dir "${LOG_DIR}" \ |
| --rsproxy_options --cfg="${RS_CFG_FILE}" \ |
| -- \ |
| "$@" \ |
| || exit_code=$? |
| |
| if [[ "${exit_code}" -ne "${EXPECTED_EXIT_CODE}" ]]; then |
| echo "Error: rsproxy-wrap.sh exited with status ${exit_code}, expected ${EXPECTED_EXIT_CODE}." >&2 |
| exit 1 |
| fi |
| |
| echo "Test passed" |