#!/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 vr1-dc0 or vr1-dc1'  "T5 bootstrap-primary bad --dc token FAILS" bootstrap-primary --dc bogus --unit ceph-mon/0 --pool glance --site-name vr1-dc0
run 1 '\-\-unit is required'       "T6 bootstrap-primary missing --unit FAILS" bootstrap-primary --dc vr1-dc0
run 1 'do not assume .glance.'     "T7 bootstrap-primary missing --pool FAILS, cites no-assume-glance" bootstrap-primary --dc vr1-dc0 --unit ceph-mon/0 --site-name vr1-dc0
run 1 '\-\-site-name is required'  "T8 bootstrap-primary missing --site-name FAILS" bootstrap-primary --dc vr1-dc0 --unit ceph-mon/0 --pool glance
run 1 '\-\-direction is required'  "T9 bootstrap-secondary missing --direction FAILS" bootstrap-secondary --dc vr1-dc0 --unit ceph-mon/0 --pool glance --site-name vr1-dc1 --token-in /tmp/tok
run 1 '\-\-direction must be rx-only or rx-tx' "T10 bootstrap-secondary bad --direction FAILS" bootstrap-secondary --dc vr1-dc0 --unit ceph-mon/0 --pool glance --site-name vr1-dc1 --direction bogus --token-in /tmp/tok
run 1 '\-\-token-in is required'   "T11 bootstrap-secondary missing --token-in FAILS" bootstrap-secondary --dc vr1-dc0 --unit ceph-mon/0 --pool glance --site-name vr1-dc1 --direction rx-only

# --- dry-run plan content ---
BP_OUT="$(bash "$SCRIPT" bootstrap-primary --dc vr1-dc0 --unit ceph-mon/0 --pool glance --site-name vr1-dc0 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 vr1-dc0 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 vr1-dc0 --unit ceph-mon/1 --pool glance --site-name vr1-dc1 --direction rx-only --token-in /tmp/tok 2>&1)"
grep -q 'peer bootstrap import --site-name vr1-dc1 --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 vr1-dc0 --unit ceph-mon/1 --pool glance --site-name vr1-dc1 --direction rx-tx --token-in /tmp/tok2 2>&1)"
grep -q 'peer bootstrap import --site-name vr1-dc1 --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: vr1-dc1 now passes (literals RATIFIED, D-124 amendment
# 2026-07-21); the reminder must surface DC1's OWN replication plane, never
# dc0's (cross-DC value surfacing). The gate's refusal path (exit 3) is
# retained as defense-in-depth but no longer CLI-reachable.
DC1_DRY="$(bash "$SCRIPT" bootstrap-primary --dc vr1-dc1 --unit ceph-mon/0 --pool glance --site-name vr1-dc1 2>&1)"; DC1_DRY_RC=$?
[[ "$DC1_DRY_RC" == 0 ]] && grep -q 'gate: OK (vr1-dc1)' <<<"$DC1_DRY" && grep -q 'OK (dry-run)' <<<"$DC1_DRY" \
  && { echo "  PASS  T17 dc1 dry-run prints the plan (gate OK, D-124 ratified)"; PASS=$((PASS+1)); } || { echo "  FAIL  T17"; echo "$DC1_DRY" | sed 's/^/        /'; FAIL=$((FAIL+1)); }
grep -q 'REPLICATION plane (10.12.84.0/22' <<<"$DC1_DRY" \
  && { echo "  PASS  T17b dc1 reminder shows dc1's OWN replication CIDR (not dc0's)"; PASS=$((PASS+1)); } || { echo "  FAIL  T17b (cross-DC value surfaced)"; echo "$DC1_DRY" | sed 's/^/        /'; FAIL=$((FAIL+1)); }

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

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 vr1-dc0 --unit ceph-mon/0 --pool glance --site-name vr1-dc0 --apply
fi

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