Skip to content

Commit c98323b

Browse files
authored
Merge pull request DeusData#2199 from astandrik/fix/1696-smoke-runtime-isolation
fix(test): isolate smoke harness daemon runtime
2 parents a5550e1 + f07c187 commit c98323b

3 files changed

Lines changed: 129 additions & 3 deletions

File tree

‎scripts/smoke-test.sh‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ they stage the release fixture, start the fixture server, and sandbox
2222
HOME/TEMP/agent-config destinations. Called bare, the download/checksum/
2323
install-script phases (12-13) SKIP for lack of a fixture server, and the run
2424
mutates the REAL profile — the venue-parity contract forbids that in any venue.
25+
The daemon runtime and cache are private to the run either way: every product
26+
process is started under a CBM_RUNTIME_DIR/CBM_CACHE_DIR this harness owns.
2527
2628
Arguments:
2729
<binary-path> product binary to smoke
@@ -41,6 +43,20 @@ if [ -n "$SMOKE_MODE" ] && [ "$SMOKE_MODE" != "--agent-config-only" ]; then
4143
fi
4244
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")/.." && pwd)"
4345

46+
# Every product process below — the phases, the install/update E2E and the
47+
# daemon retirements — must reach a daemon rendezvous this run owns. Only
48+
# CBM_RUNTIME_DIR moves that rendezvous; the wrappers' HOME/TMPDIR/CBM_CACHE_DIR
49+
# sandbox does not, so without this the retirements land on the operator's live
50+
# account daemon (#1691, #1696).
51+
# shellcheck source=test-runtime.sh
52+
source "$REPO_ROOT/scripts/test-runtime.sh"
53+
cbm_test_runtime_init
54+
# Armed here rather than only with the fixture trap below: the fixture mktemp
55+
# and its cygpath conversion sit between the two, and under `set -e` a failure
56+
# there would otherwise leave the private root behind. The fixture trap
57+
# replaces this one and keeps the same cleanup as its first step.
58+
trap 'cbm_test_runtime_cleanup "$BINARY"' EXIT
59+
4460
smoke_mktemp_file() {
4561
if [ -n "${SMOKE_TEMP_ROOT:-}" ]; then
4662
mktemp "$SMOKE_TEMP_ROOT/cbm-smoke.XXXXXX"
@@ -100,8 +116,8 @@ copy_smoke_binary() {
100116
cp "$BINARY" "$destination"
101117
}
102118

103-
# Retire the shared account daemon (if one is running) and wait until it
104-
# reports not-running. Install/uninstall flows leave an ephemeral daemon
119+
# Retire this run's private account daemon (if one is running) and wait until
120+
# it reports not-running. Install/uninstall flows leave an ephemeral daemon
105121
# draining asynchronously whose mapped generation backing and open logs
106122
# block rm on Windows (POSIX rm doesn't care) — so every cleanup of a
107123
# fixture HOME that received an install, and the final cache removal, must
@@ -149,7 +165,11 @@ CODEX_LIFECYCLE_HOME=""
149165
if command -v cygpath &>/dev/null; then
150166
TMPDIR=$(cygpath -m "$TMPDIR")
151167
fi
152-
trap 'smoke_rmtree "$TMPDIR" "${DRYRUN_HOME:-}" "${CODEX_LIFECYCLE_HOME:-}"' EXIT
168+
# Runtime cleanup first, so no earlier cleanup step stands between the exit
169+
# and the private daemon's retirement; on Windows that retirement is also what
170+
# unblocks the fixture rm (mapped binary, open logs). smoke_rmtree never fails,
171+
# so the fixture removal still runs after it.
172+
trap 'cbm_test_runtime_cleanup "$BINARY"; smoke_rmtree "$TMPDIR" "${DRYRUN_HOME:-}" "${CODEX_LIFECYCLE_HOME:-}"' EXIT
153173

154174
CLI_STDERR=$(smoke_mktemp_file)
155175
# 10 of the cli call sites assign directly (VAR=$(cli ...)). Under

‎scripts/test.sh‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ bash "$ROOT/tests/test_release_gate_chain_contract.sh"
272272
echo "=== Step 0t: test runtime isolation contract (#1691) ==="
273273
bash "$ROOT/tests/test_runtime_isolation_contract.sh"
274274

275+
echo "=== Step 0t2: smoke harness runtime isolation contract (#1696) ==="
276+
bash "$ROOT/tests/test_smoke_runtime_isolation_contract.sh"
277+
275278
echo "=== Step 0u: shell line-ending contract ==="
276279
bash "$ROOT/tests/test_shell_line_endings.sh"
277280

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)