#!/usr/bin/env bash
# Recovery tests for the seat reconciler. Runs ON a buildbox against a private $HOME so it can
# fabricate the exact on-disk shape a reboot leaves behind, without rebooting anything.
#
#   modules/harness/seat/test/seat-recovery.test.sh [debian1]
#
# Everything the reconciler touches (~/.rb/seats, ~/.rb/jobs, the mirrors named in the seat
# records) hangs off HOME, so pointing HOME at a temp dir isolates the run from the box's real
# jobs completely. The container reap is the one exception and is asserted against a container
# this test creates and names itself.
set -uo pipefail

HOST="${1:-debian1}"
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SEAT_DIR="$(cd "$HERE/.." && pwd)"
SSH=(ssh -F /dev/null -i "$HOME/.ssh/id_ed25519_buildbox" -p 2222 "user@$HOST")
SCP=(scp -q -F /dev/null -i "$HOME/.ssh/id_ed25519_buildbox" -P 2222)

STAGE="$("${SSH[@]}" 'mktemp -d "$HOME/builds/seat-recovery-XXXX"')" || { echo "cannot stage on $HOST"; exit 1; }
trap '"${SSH[@]}" "rm -rf $STAGE"' EXIT
"${SCP[@]}" "$SEAT_DIR/seat-reconcile.sh" "$HERE/seat-recovery-remote.sh" "user@$HOST:$STAGE/" || exit 1

"${SSH[@]}" "bash $STAGE/seat-recovery-remote.sh $STAGE"
RC=$?
echo "---- seat-recovery on $HOST: exit $RC"
exit "$RC"
