diff --git a/tests/test_pbs_gc_states.sh b/tests/test_pbs_gc_states.sh index f116471..12e692e 100755 --- a/tests/test_pbs_gc_states.sh +++ b/tests/test_pbs_gc_states.sh @@ -1,5 +1,6 @@ #!/bin/bash # test_pbs_gc_states.sh — Tests for PBS GC four-state logic +# Self-contained: inlines the SSH replacement logic set -uo pipefail @@ -10,11 +11,33 @@ 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 pattern="$3" - local behavior="$4" + local expected_behavior="$3" + local expected_pattern="$4" local wrapper monitor wrapper=$(mktemp /tmp/pbs-test-wrapper.XXXXXX) @@ -24,18 +47,20 @@ run_test() { cp "$PROXMOX_MONITOR" "$monitor" # Replace the SSH call with cat "$wrapper" - python3 /tmp/replace_ssh.py "$wrapper" "$monitor" + python3 "$REPLACE_SCRIPT" "$wrapper" "$monitor" local output exit_code output=$(bash "$monitor" 2>&1) exit_code=$? local ok=true - if [ "$behavior" = "fail" ]; then + 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 - if ! echo "$output" | grep -q "$pattern"; then ok=false; fi + # 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 @@ -56,26 +81,28 @@ echo "Script: $PROXMOX_MONITOR" echo "" echo "1. probe-failed (unparseable JSON)" -run_test "unparseable-json" "NOT JSON {{{" "probe-failed" "fail" +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}]' "probe-failed" "fail" +run_test "store-missing" '[{"store": "other", "last-run-endtime": 1000}]' "fail" "probe-failed" echo "3. probe-failed (empty body)" -run_test "empty-body" "" "probe-failed" "fail" +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}]' "⏳ PBS GC: running" "pass" +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}]" "stale" "fail" +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}]" "✅ PBS GC: healthy" "pass" +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