#!/bin/bash # test_proxmox_monitor.sh — Stub-driven tests for PBS GC liveness leg set -uo pipefail SCRIPT="$(cd "$(dirname "$0")" && pwd)/proxmox-monitor.sh" PASS=0 FAIL=0 # ── Helpers ──────────────────────────────────────────────────────────────── assert() { local desc="$1" cond="$2" if eval "$cond" 2>/dev/null; then echo " ✅ $desc" PASS=$((PASS+1)) else echo " 🔴 $desc" FAIL=$((FAIL+1)) fi } # ── 1. Healthy: fresh GC, 0 B pending ───────────────────────────────────── TMP_DIR=$(mktemp -d) FRESH_ENDTIME=$(( $(date -u +%s) - (1 * 3600) )) # 1 hour ago cat > "$TMP_DIR/ssh" << EOF #!/bin/bash # Stub: return valid JSON with fresh endtime echo '[{"store":"storepve-datastore","last-run-endtime":$FRESH_ENDTIME,"pending-bytes":0}]' exit 0 EOF chmod +x "$TMP_DIR/ssh" OUT=$(PATH="$TMP_DIR:$PATH" bash "$SCRIPT" 2>&1) PBS_LINE=$(echo "$OUT" | grep "PBS GC:" || echo "") assert "Healthy: PBS line exists" '[[ -n "$PBS_LINE" ]]' assert "Healthy: shows healthy verdict" '[[ "$PBS_LINE" == *"healthy"* ]]' assert "Healthy: shows pending-bytes 0 B" '[[ "$PBS_LINE" == *"pending-bytes: 0 B"* ]]' rm -rf "$TMP_DIR" # ── 2. Stale: GC >48h old ───────────────────────────────────────────────── TMP_DIR=$(mktemp -d) OLD_ENDTIME=$(( $(date -u +%s) - (49 * 3600) )) # 49 hours ago cat > "$TMP_DIR/ssh" << EOF #!/bin/bash echo '[{"store":"storepve-datastore","last-run-endtime":$OLD_ENDTIME,"pending-bytes":1048576}]' exit 0 EOF chmod +x "$TMP_DIR/ssh" OUT=$(PATH="$TMP_DIR:$PATH" bash "$SCRIPT" 2>&1) PBS_LINE=$(echo "$OUT" | grep "PBS GC:" || echo "") assert "Stale: PBS line exists" '[[ -n "$PBS_LINE" ]]' assert "Stale: shows stale verdict" '[[ "$PBS_LINE" == *"stale"* ]]' assert "Stale: shows pending-bytes 1048576 B" '[[ "$PBS_LINE" == *"pending-bytes: 1048576 B"* ]]' rm -rf "$TMP_DIR" # ── 3. Probe-failed: empty output ───────────────────────────────────────── TMP_DIR=$(mktemp -d) cat > "$TMP_DIR/ssh" << 'EOF' #!/bin/bash exit 0 EOF chmod +x "$TMP_DIR/ssh" OUT=$(PATH="$TMP_DIR:$PATH" bash "$SCRIPT" 2>&1) PBS_LINE=$(echo "$OUT" | grep "PBS GC:" || echo "") assert "Probe-failed (empty): PBS line exists" '[[ -n "$PBS_LINE" ]]' assert "Probe-failed (empty): shows probe-failed" '[[ "$PBS_LINE" == *"probe-failed"* ]]' rm -rf "$TMP_DIR" # ── 4. Probe-failed: unparseable output ─────────────────────────────────── TMP_DIR=$(mktemp -d) cat > "$TMP_DIR/ssh" << 'EOF' #!/bin/bash echo "proxmox-backup-manager: command not found" exit 0 EOF chmod +x "$TMP_DIR/ssh" OUT=$(PATH="$TMP_DIR:$PATH" bash "$SCRIPT" 2>&1) PBS_LINE=$(echo "$OUT" | grep "PBS GC:" || echo "") assert "Probe-failed (unparseable): PBS line exists" '[[ -n "$PBS_LINE" ]]' assert "Probe-failed (unparseable): shows probe-failed" '[[ "$PBS_LINE" == *"probe-failed"* ]]' rm -rf "$TMP_DIR" # ── 5. Null endtime: never-run ──────────────────────────────────────────── TMP_DIR=$(mktemp -d) cat > "$TMP_DIR/ssh" << 'EOF' #!/bin/bash echo '[{"store":"storepve-datastore","last-run-endtime":null,"pending-bytes":0}]' exit 0 EOF chmod +x "$TMP_DIR/ssh" OUT=$(PATH="$TMP_DIR:$PATH" bash "$SCRIPT" 2>&1) PBS_LINE=$(echo "$OUT" | grep "PBS GC:" || echo "") assert "Null endtime: PBS line exists" '[[ -n "$PBS_LINE" ]]' assert "Null endtime: shows never-run" '[[ "$PBS_LINE" == *"never-run"* ]]' rm -rf "$TMP_DIR" # ── 6. Datastore absent ─────────────────────────────────────────────────── TMP_DIR=$(mktemp -d) cat > "$TMP_DIR/ssh" << 'EOF' #!/bin/bash echo '[{"store":"s3-archive","last-run-endtime":null,"pending-bytes":0}]' exit 0 EOF chmod +x "$TMP_DIR/ssh" OUT=$(PATH="$TMP_DIR:$PATH" bash "$SCRIPT" 2>&1) PBS_LINE=$(echo "$OUT" | grep "PBS GC:" || echo "") assert "Datastore absent: PBS line exists" '[[ -n "$PBS_LINE" ]]' assert "Datastore absent: shows never-run" '[[ "$PBS_LINE" == *"never-run"* ]]' rm -rf "$TMP_DIR" # ── Summary ──────────────────────────────────────────────────────────────── echo "" echo "Results: ${PASS} passed, ${FAIL} failed" if [ $FAIL -gt 0 ]; then echo " 🔴 TESTS FAILED" exit 1 else echo " ✅ ALL TESTS PASSED" exit 0 fi