# shellcheck shell=bash # ============================================================================ # The sandbox and the assertions for test_watchdog.sh. # # Split out when the two together crossed the shared 250-line cap. Sourced, # never executed: it defines the stub PATH the suite runs against and the # expect_* helpers, and it owns WORK/FAILURES set by its caller. # ============================================================================ # Rebuild a pristine sandbox: stub binaries, stub answers, empty state. setup() { rm -rf "$WORK" mkdir -p "$WORK/bin" "$WORK/state" printf '200' >"$WORK/bot_code" printf '200' >"$WORK/api_code" printf '0' >"$WORK/fix_on_restart" cat >"$WORK/bin/curl" <"$WORK/bin/docker" <> "$WORK/docker.log" if [[ "\$(cat "$WORK/fix_on_restart")" == "1" ]]; then printf '200' > "$WORK/bot_code" printf '200' > "$WORK/api_code" fi EOF # Stubbed because they are NOT stubbed by default: notify-send and logger # resolve to the real binaries, so this suite used to fire a dozen genuine # critical popups per run -- and it runs from pre-commit, which made every # commit spam the desktop. notify.log is now also what proves the dedup. cat >"$WORK/bin/notify-send" <> "$WORK/notify.log" echo 4242 EOF cat >"$WORK/bin/logger" <> "$WORK/logger.log" EOF chmod +x "$WORK/bin/curl" "$WORK/bin/docker" "$WORK/bin/notify-send" "$WORK/bin/logger" } notify_count() { [[ -f "$WORK/notify.log" ]] || { printf '0' return 0 } grep -c '' "$WORK/notify.log" } expect_notifications() { local actual actual="$(notify_count)" if [[ "$actual" == "$1" ]]; then ok "$2" else fail "$2 -- got $actual popup(s), expected $1" fi } run_watchdog() { PATH="$WORK/bin:$PATH" \ PHONE_NUMBER="+15550000000" \ STATE_DIRECTORY="$WORK/state" \ SETTLE_SECONDS=0 \ FAILURES_BEFORE_RESTART=2 \ MAX_RESTARTS=2 \ RESTART_WINDOW=3600 \ "$WATCHDOG" 2>&1 } expect_contains() { if [[ "$1" == *"$2"* ]]; then ok "$3" else fail "$3 -- got: $1" fi } expect_missing() { if [[ "$1" != *"$2"* ]]; then ok "$3" else fail "$3 -- unexpectedly found $2" fi } expect_file() { local actual actual="$(cat "$1" 2>/dev/null || echo MISSING)" if [[ "$actual" == "$2" ]]; then ok "$3" else fail "$3 -- $1 is '$actual', expected '$2'" fi }