#!/usr/bin/env bash
# tests/dc-dc-rbd-mirror/run-tests.sh -- offline harness for
# scripts/dc-dc-rbd-mirror.sh. Only tests THIS SCRIPT's own logic: argument
# parsing / guard clauses (including the required, never-defaulted
# --direction choice), --dry-run plan output, the $DC gate (informational in
# dry-run, blocking before --apply), and the token-file handling discipline
# (never printed/captured into this session). Does NOT and CANNOT validate
# real rbd-mirror daemon behavior -- no live cluster exists this session
# (2026-07-10, prep-only).
# Exit: 0 all pass | 1 any case failed.  ASCII + LF.
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT="$HERE/../../scripts/dc-dc-rbd-mirror.sh"
PASS=0; FAIL=0

run() { # run <want_rc> <regex> <label> -- args...
  local want="$1" rx="$2" label="$3"; shift 3
  local out rc
  out="$(bash "$SCRIPT" "$@" 2>&1)"; rc=$?
  if [[ "$rc" == "$want" ]] && grep -qE "$rx" <<<"$out"; then
    echo "  PASS  $label"; PASS=$((PASS+1))
  else
    echo "  FAIL  $label (rc=$rc want=$want)"; echo "$out" | sed 's/^/        /'; FAIL=$((FAIL+1))
  fi
}

run 1 'a subcommand is required'   "T1 no subcommand FAILS (rc 1)"
run 1 'unknown subcommand'         "T2 bogus subcommand FAILS (rc 1)" bogus
run 0 'usage:'                     "T3 --help exits 0 with usage" --help
run 1 '\-\-dc is required'         "T4 bootstrap-primary missing --dc FAILS" bootstrap-primary
run 1 '\-\-dc must be dc1 or dc2'  "T5 bootstrap-primary bad --dc token FAILS" bootstrap-primary --dc bogus --unit ceph-mon/0 --pool glance --site-name dc1
run 1 '\-\-unit is required'       "T6 bootstrap-primary missing --unit FAILS" bootstrap-primary --dc dc1
run 1 'do not assume .glance.'     "T7 bootstrap-primary missing --pool FAILS, cites no-assume-glance" bootstrap-primary --dc dc1 --unit ceph-mon/0 --site-name dc1
run 1 '\-\-site-name is required'  "T8 bootstrap-primary missing --site-name FAILS" bootstrap-primary --dc dc1 --unit ceph-mon/0 --pool glance
run 1 '\-\-direction is required'  "T9 bootstrap-secondary missing --direction FAILS" bootstrap-secondary --dc dc1 --unit ceph-mon/0 --pool glance --site-name dc2 --token-in /tmp/tok
run 1 '\-\-direction must be rx-only or rx-tx' "T10 bootstrap-secondary bad --direction FAILS" bootstrap-secondary --dc dc1 --unit ceph-mon/0 --pool glance --site-name dc2 --direction bogus --token-in /tmp/tok
run 1 '\-\-token-in is required'   "T11 bootstrap-secondary missing --token-in FAILS" bootstrap-secondary --dc dc1 --unit ceph-mon/0 --pool glance --site-name dc2 --direction rx-only

# --- dry-run plan content ---
BP_OUT="$(bash "$SCRIPT" bootstrap-primary --dc dc1 --unit ceph-mon/0 --pool glance --site-name dc1 2>&1)"; BP_RC=$?
[[ "$BP_RC" == 0 ]] && grep -q 'OK (dry-run)' <<<"$BP_OUT" && { echo "  PASS  T12 bootstrap-primary dry-run rc 0 + OK marker"; PASS=$((PASS+1)); } || { echo "  FAIL  T12"; echo "$BP_OUT" | sed 's/^/        /'; FAIL=$((FAIL+1)); }
grep -q 'rbd mirror pool enable glance image' <<<"$BP_OUT" && grep -q 'peer bootstrap create --site-name dc1 glance' <<<"$BP_OUT" \
  && { echo "  PASS  T13 bootstrap-primary plan contains enable + peer-bootstrap-create"; PASS=$((PASS+1)); } || { echo "  FAIL  T13"; echo "$BP_OUT" | sed 's/^/        /'; FAIL=$((FAIL+1)); }
grep -q 'transfer it to the DC2 unit OUT OF BAND' <<<"$BP_OUT" && { echo "  PASS  T14 bootstrap-primary reminds out-of-band token transfer"; PASS=$((PASS+1)); } || { echo "  FAIL  T14"; FAIL=$((FAIL+1)); }

BS_RXONLY="$(bash "$SCRIPT" bootstrap-secondary --dc dc1 --unit ceph-mon/1 --pool glance --site-name dc2 --direction rx-only --token-in /tmp/tok 2>&1)"
grep -q 'peer bootstrap import --site-name dc2 --direction rx-only glance /tmp/tok' <<<"$BS_RXONLY" \
  && { echo "  PASS  T15 bootstrap-secondary rx-only plan is correct"; PASS=$((PASS+1)); } || { echo "  FAIL  T15"; echo "$BS_RXONLY" | sed 's/^/        /'; FAIL=$((FAIL+1)); }

BS_RXTX="$(bash "$SCRIPT" bootstrap-secondary --dc dc1 --unit ceph-mon/1 --pool glance --site-name dc2 --direction rx-tx --token-in /tmp/tok2 2>&1)"
grep -q 'peer bootstrap import --site-name dc2 --direction rx-tx glance /tmp/tok2' <<<"$BS_RXTX" \
  && { echo "  PASS  T16 bootstrap-secondary rx-tx (two-way) plan is correct"; PASS=$((PASS+1)); } || { echo "  FAIL  T16"; echo "$BS_RXTX" | sed 's/^/        /'; FAIL=$((FAIL+1)); }

# --- $DC gate: informational in dry-run, blocking before --apply ---
DC2_DRY="$(bash "$SCRIPT" bootstrap-primary --dc dc2 --unit ceph-mon/0 --pool glance --site-name dc2 2>&1)"; DC2_DRY_RC=$?
[[ "$DC2_DRY_RC" == 0 ]] && grep -q 'gate: FAILED' <<<"$DC2_DRY" && grep -q 'OK (dry-run)' <<<"$DC2_DRY" \
  && { echo "  PASS  T17 dc2 dry-run still prints the plan (gate is informational here)"; PASS=$((PASS+1)); } || { echo "  FAIL  T17"; echo "$DC2_DRY" | sed 's/^/        /'; FAIL=$((FAIL+1)); }

run 3 '\$DC gate refused' "T18 dc2 --apply is BLOCKED by the gate (rc 3)" bootstrap-primary --dc dc2 --unit ceph-mon/0 --pool glance --site-name dc2 --apply

if command -v juju >/dev/null 2>&1; then
  echo "  SKIP  T19 juju-missing case (juju IS present in this environment -- can't exercise the missing-tool guard here)"
else
  run 2 'juju required on PATH' "T19 --apply with dc1 (gate OK) but juju absent FAILS (rc 2)" bootstrap-primary --dc dc1 --unit ceph-mon/0 --pool glance --site-name dc1 --apply
fi

echo; echo "RESULT: PASS=$PASS FAIL=$FAIL"
[[ "$FAIL" -eq 0 ]] && { echo "ALL PASS"; exit 0; } || exit 1
