|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Runtime-isolation contract for the smoke harness (#1696, follow-up to #1691). |
| 5 | +# |
| 6 | +# scripts/smoke-test.sh is the process that actually starts the product during |
| 7 | +# a smoke run, and it retires "the account daemon" seven times through |
| 8 | +# `daemon stop`. Its wrappers sandbox HOME/XDG/TMPDIR and CBM_CACHE_DIR, but |
| 9 | +# only CBM_RUNTIME_DIR moves the daemon rendezvous (docs/CONFIGURATION.md), so |
| 10 | +# without a private runtime every one of those stops lands on the operator's |
| 11 | +# live daemon. Drive the harness with an environment-probe fixture and require |
| 12 | +# that no product process ever receives the caller's runtime or cache. |
| 13 | + |
| 14 | +ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 15 | +WORKDIR="$(mktemp -d)" |
| 16 | +trap 'rm -rf "$WORKDIR"' EXIT |
| 17 | + |
| 18 | +fail() { |
| 19 | + echo "FAIL: $*" >&2 |
| 20 | + exit 1 |
| 21 | +} |
| 22 | + |
| 23 | +normalize_path() { |
| 24 | + local path=${1%$'\r'} |
| 25 | + if command -v cygpath >/dev/null 2>&1; then |
| 26 | + cygpath -u "$path" 2>/dev/null && return 0 |
| 27 | + fi |
| 28 | + printf '%s\n' "${path//\\//}" |
| 29 | +} |
| 30 | + |
| 31 | +ENV_PROBE="$WORKDIR/environment-probe" |
| 32 | +cat > "$ENV_PROBE" <<'EOF' |
| 33 | +#!/usr/bin/env bash |
| 34 | +printf '%s\t%s\n' "${CBM_CACHE_DIR-}" "${CBM_RUNTIME_DIR-}" >> "$CBM_SMOKE_ENV_PROBE" |
| 35 | +case "${1-}" in |
| 36 | + --version) echo "v0.0.0-probe"; exit 0 ;; |
| 37 | + daemon) [[ "${2-}" == status ]] && exit 1; exit 0 ;; |
| 38 | +esac |
| 39 | +echo '{}' |
| 40 | +exit 0 |
| 41 | +EOF |
| 42 | +chmod +x "$ENV_PROBE" |
| 43 | + |
| 44 | +CALLER_CACHE="$WORKDIR/caller-cache" |
| 45 | +CALLER_RUNTIME="$WORKDIR/caller-runtime" |
| 46 | +ENV_LOG="$WORKDIR/environment.log" |
| 47 | +mkdir -p "$CALLER_CACHE" "$CALLER_RUNTIME" |
| 48 | + |
| 49 | +# The fixture answers nothing beyond --version, so the smoke fails early; only |
| 50 | +# the environment it handed to the product is under test here. |
| 51 | +CBM_CACHE_DIR="$CALLER_CACHE" \ |
| 52 | +CBM_RUNTIME_DIR="$CALLER_RUNTIME" \ |
| 53 | +CBM_SMOKE_ENV_PROBE="$ENV_LOG" \ |
| 54 | + "$ROOT/scripts/smoke-test.sh" "$ENV_PROBE" > "$WORKDIR/smoke.out" 2>&1 || true |
| 55 | + |
| 56 | +[[ -s "$ENV_LOG" ]] || fail "smoke-test did not execute the environment-probe fixture" |
| 57 | + |
| 58 | +CALLER_CACHE_NORMALIZED=$(normalize_path "$CALLER_CACHE") |
| 59 | +CALLER_RUNTIME_NORMALIZED=$(normalize_path "$CALLER_RUNTIME") |
| 60 | +private_root="" |
| 61 | +while IFS=$'\t' read -r child_cache_raw child_runtime_raw; do |
| 62 | + child_cache=$(normalize_path "$child_cache_raw") |
| 63 | + child_runtime=$(normalize_path "$child_runtime_raw") |
| 64 | + if [[ -z "$child_runtime" || "$child_runtime" == "$CALLER_RUNTIME_NORMALIZED" ]]; then |
| 65 | + fail "smoke-test exposed the caller CBM_RUNTIME_DIR to a product process" |
| 66 | + fi |
| 67 | + if [[ -z "$child_cache" || "$child_cache" == "$CALLER_CACHE_NORMALIZED" ]]; then |
| 68 | + fail "smoke-test exposed the caller CBM_CACHE_DIR to a product process" |
| 69 | + fi |
| 70 | + if [[ "${child_runtime%/*}" != "${child_cache%/*}" || |
| 71 | + "${child_runtime##*/}" != "runtime" || "${child_cache##*/}" != "cache" ]]; then |
| 72 | + fail "smoke runtime/cache were not isolated beneath one private root" |
| 73 | + fi |
| 74 | + if [[ -n "$private_root" && "$private_root" != "${child_runtime%/*}" ]]; then |
| 75 | + fail "smoke-test switched private roots mid-run" |
| 76 | + fi |
| 77 | + private_root="${child_runtime%/*}" |
| 78 | +done < "$ENV_LOG" |
| 79 | + |
| 80 | +[[ ! -e "$private_root" ]] || fail "smoke-test left its private root behind: $private_root" |
| 81 | + |
| 82 | +# The run above exits through the fixture trap, so it cannot show what happens |
| 83 | +# to the private root when the harness dies before that trap exists — the |
| 84 | +# fixture mktemp and its cygpath conversion are in that window, and under |
| 85 | +# `set -e` either can end the run. Reproducing that failure would mean scanning |
| 86 | +# the shared /tmp parent for orphaned roots, which races every other harness |
| 87 | +# test in the same suite, so pin the ordering instead: the cleanup trap is |
| 88 | +# armed between the init call and the first fixture work. |
| 89 | +smoke="$ROOT/scripts/smoke-test.sh" |
| 90 | +smoke_line_of() { |
| 91 | + # A missing pattern is the failure this check reports, not a reason to end |
| 92 | + # the test silently under `set -e`. |
| 93 | + grep -n "$1" "$smoke" | head -1 | cut -d: -f1 || true |
| 94 | +} |
| 95 | +init_line=$(smoke_line_of '^cbm_test_runtime_init$') |
| 96 | +early_trap_line=$(smoke_line_of "^trap 'cbm_test_runtime_cleanup \"\$BINARY\"' EXIT\$") |
| 97 | +fixture_line=$(smoke_line_of '^TMPDIR=\$(smoke_mktemp_dir)$') |
| 98 | +if [[ -z "$init_line" || -z "$early_trap_line" || -z "$fixture_line" ]] || |
| 99 | + ((early_trap_line < init_line || early_trap_line > fixture_line)); then |
| 100 | + fail "smoke-test must arm the runtime cleanup trap between cbm_test_runtime_init and its first fixture" |
| 101 | +fi |
| 102 | + |
| 103 | +echo "PASS: smoke harness isolates its daemon runtime and cache from the caller" |
0 commit comments