strawberry/scripts/test_reduce_wrapper.sh
trvon 1ee208fb4b
Stage 7e: scheduled stop->reduce->start telemetry-reduction maintenance (opt-in)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-11 13:21:13 -07:00

73 lines
3 KiB
Bash
Executable file

#!/usr/bin/env bash
# Live-lane test for scripts/strawberry-reduce.sh using PATH shims for systemctl
# and the strawberry binary. Run inside the Docker live lane (run_live_tests_docker.sh).
# Asserts: ordered stop -> reduce -> start; gate off => no-op, never stops; a
# failing reduce STILL restarts the daemon (EXIT trap); a failing stop still restarts.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
WRAPPER="$ROOT/scripts/strawberry-reduce.sh"
T="$(mktemp -d)"
trap 'rm -rf -- "${T:?}"' EXIT
mkdir -p "$T/bin"
cat > "$T/bin/systemctl" <<'SH'
#!/usr/bin/env bash
echo "systemctl $*" >> "$CALLS"
case "$1" in
is-active) exit "${STUB_ACTIVE_RC:-0}" ;;
stop) exit "${STUB_STOP_RC:-0}" ;;
esac
exit 0
SH
cat > "$T/bin/strawberry" <<'SH'
#!/usr/bin/env bash
echo "strawberry $*" >> "$CALLS"
echo '{"ok": true, "records_before": 100, "records_after": 40, "problems": []}'
exit "${STUB_REDUCE_RC:-0}"
SH
chmod +x "$T/bin/systemctl" "$T/bin/strawberry"
export PATH="$T/bin:$PATH" STRAWBERRY_BIN="$T/bin/strawberry" STRAWBERRY_CONF="$T/conf" CALLS="$T/calls"
fail() { echo "FAIL: $*" >&2; echo "--- calls:" >&2; cat "$CALLS" >&2 || true; exit 1; }
run() { : > "$CALLS"; set +e; bash "$WRAPPER" > "$T/out" 2>&1; RC=$?; set -e; }
# 1. gate off (absent, commented, false) => exit 0, no stop, no reduce
for body in "" "#store_reduce_enabled=true" "store_reduce_enabled=false"; do
printf '%s\n' "$body" > "$T/conf"
run
[ "$RC" -eq 0 ] || fail "gate off rc=$RC ($body)"
grep -q 'disabled' "$T/out" || fail "gate off message ($body)"
[ ! -s "$CALLS" ] || fail "gate off made calls ($body)"
done
# 2. gate on: ordered stop -> reduce -> start
echo "store_reduce_enabled=true" > "$T/conf"
run
[ "$RC" -eq 0 ] || fail "enabled rc=$RC"
stop_n=$(grep -n 'systemctl stop strawberry.service' "$CALLS" | head -1 | cut -d: -f1)
red_n=$(grep -n 'strawberry store reduce --yes' "$CALLS" | head -1 | cut -d: -f1)
start_n=$(grep -n 'systemctl start strawberry.service' "$CALLS" | head -1 | cut -d: -f1)
[ -n "$stop_n" ] && [ -n "$red_n" ] && [ -n "$start_n" ] || fail "missing stop/reduce/start"
[ "$stop_n" -lt "$red_n" ] && [ "$red_n" -lt "$start_n" ] || fail "wrong order"
grep -q 'records 100 -> 40' "$T/out" || fail "summary line"
# 3. reduce failure: non-zero rc AND daemon still restarted
STUB_REDUCE_RC=1 run
[ "$RC" -ne 0 ] || fail "reduce failure must propagate rc"
grep -q 'systemctl start strawberry.service' "$CALLS" || fail "no restart after reduce failure"
# 4. stop failure: aborts, never reduces, daemon still restarted
STUB_STOP_RC=1 run
[ "$RC" -ne 0 ] || fail "stop failure must be non-zero"
grep -q 'strawberry store reduce' "$CALLS" && fail "reduce ran after failed stop"
grep -q 'systemctl start strawberry.service' "$CALLS" || fail "no restart after stop failure"
# 5. daemon not active: reduce runs, daemon is not started
STUB_ACTIVE_RC=3 run
[ "$RC" -eq 0 ] || fail "inactive rc=$RC"
grep -q 'systemctl stop' "$CALLS" && fail "stopped an inactive daemon"
grep -q 'systemctl start' "$CALLS" && fail "started a daemon that was not running"
echo "reduce wrapper: ALL PASSED"