#!/bin/bash # test_pbs_gc_states.sh — Tests for PBS GC four-state logic # Self-contained: inlines the SSH replacement logic set -uo pipefail TEST_DIR="$(cd "$(dirname "$0")" && pwd)" SCRIPTS_DIR="$(dirname "$TEST_DIR")/scripts" PROXMOX_MONITOR="${1:-${SCRIPTS_DIR}/proxmox-monitor.sh}" PASS=0 FAIL=0 # Create the Python replacement script REPLACE_SCRIPT=$(mktemp /tmp/replace_ssh_XXXXXX.py) cat > "$REPLACE_SCRIPT" << 'PYEOF' import sys import re wrapper = sys.argv[1] monitor = sys.argv[2] with open(monitor) as f: c = f.read() pattern = r'PBS_GC_OUTPUT=\$\(ssh -o ConnectTimeout=5 -o BatchMode=yes root@192\.168\.68\.6 \\\n "/sbin/pct exec 107 -- /sbin/proxmox-backup-manager garbage-collection list --output-format json" 2>/dev/null\)' replacement = 'PBS_GC_OUTPUT=$(cat "' + wrapper + '")' if re.search(pattern, c): c = re.sub(pattern, replacement, c) with open(monitor, 'w') as f: f.write(c) PYEOF run_test() { local name="$1" local json="$2" local expected_behavior="$3" local expected_pattern="$4" local wrapper monitor wrapper=$(mktemp /tmp/pbs-test-wrapper.XXXXXX) monitor=$(mktemp /tmp/pbs-test-monitor.XXXXXX) printf '%s\n' "$json" > "$wrapper" cp "$PROXMOX_MONITOR" "$monitor" # Replace the SSH call with cat "$wrapper" python3 "$REPLACE_SCRIPT" "$wrapper" "$monitor" local output exit_code output=$(bash "$monitor" 2>&1) exit_code=$? local ok=true if [ "$expected_behavior" = "fail" ]; then # Should fail with PBS GC error if ! echo "$output" | grep -q "🔴 PBS GC"; then ok=false; fi if [ $exit_code -eq 0 ]; then ok=false; fi else # Should pass with expected pattern if ! echo "$output" | grep -q "$expected_pattern"; then ok=false; fi if echo "$output" | grep -q "🔴 PBS GC"; then ok=false; fi fi if $ok; then echo " ✅ $name" PASS=$((PASS + 1)) else echo " 🔴 $name FAILED (exit=$exit_code)" echo "$output" | grep "PBS GC" | sed 's/^/ /' FAIL=$((FAIL + 1)) fi rm -f "$wrapper" "$monitor" } echo "=== PBS GC Four-State Tests ===" echo "Script: $PROXMOX_MONITOR" echo "" echo "1. probe-failed (unparseable JSON)" run_test "unparseable-json" 'NOT JSON {{{' "fail" "probe-failed" echo "2. probe-failed (store not found)" run_test "store-missing" '[{"store": "other", "last-run-endtime": 1000}]' "fail" "probe-failed" echo "3. probe-failed (empty body)" run_test "empty-body" "" "fail" "probe-failed" echo "4. running (in progress - upid set, no last-run-endtime)" run_test "running" '[{"store": "storepve-datastore", "upid": "UPID:123:1:456:gc:root@pam", "pending-bytes": 100}]' "pass" "⏳ PBS GC: running" echo "5. stale (last run >48h)" STALE=$(date -u -d "50 hours ago" +%s) run_test "stale" "[{\"store\": \"storepve-datastore\", \"last-run-endtime\": $STALE, \"pending-bytes\": 200}]" "fail" "stale" echo "6. healthy (completed <48h)" HEALTHY=$(date -u -d "1 hour ago" +%s) run_test "healthy" "[{\"store\": \"storepve-datastore\", \"last-run-endtime\": $HEALTHY, \"pending-bytes\": 0}]" "pass" "✅ PBS GC: healthy" echo "" echo "=== Results: $PASS passed, $FAIL failed ===" [ $FAIL -eq 0 ] && echo "✅ All passed" || echo "🔴 Some failed" rm -f "$REPLACE_SCRIPT" [ $FAIL -eq 0 ] && exit 0 || exit 1