#!/usr/bin/env bash
# tests/phase-05/run-tests.sh -- offline regression for phase-05-octavia-verify.sh.
# Behavior-tests the REAL script against fake juju/openstack shims + real jq.
# No live infra. Needs python3, bash, jq.
set -euo pipefail
IFS=$'\n\t'
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPTS="$(cd "$HERE/../../scripts" && pwd)"
TARGET="$SCRIPTS/phase-05-octavia-verify.sh"
BIN="$HERE/fakebin"

command -v python3 >/dev/null 2>&1 || { echo "FAIL: python3 required" >&2; exit 1; }
command -v jq      >/dev/null 2>&1 || { echo "FAIL: jq required" >&2; exit 1; }
[ -f "$TARGET" ] || { echo "FAIL: target missing: $TARGET" >&2; exit 1; }
chmod +x "$BIN/juju" "$BIN/openstack" 2>/dev/null || true

WORK="$(mktemp -d)"; trap 'rm -rf "$WORK"' EXIT
python3 "$HERE/make_fixtures.py" "$WORK" >/dev/null
UP_OHM0='o-hm0            UP             fc00:3f8c:7162:d105:f816:3eff:feea:7e45/64'
UNK_OHM0='o-hm0            UNKNOWN        fc00:7bab:3c0b:ae43:f816:3eff:fe2a:f741/64 fe80::d860:d1ff:fe6f:1822/64'  # the real-world value (OVS internal port)
rc_all=0

# run <want_rc> <summary_regex> <label> KEY=VAL ...
run() {
  local want="$1" wantsum="$2" label="$3"; shift 3
  local rc v
  set +e
  PATH="$BIN:$PATH" OS_AUTH_URL="http://fake:5000/v3" env "$@" bash "$TARGET" openstack >"$WORK/out" 2>&1
  rc=$?
  set -e
  v="$(grep -E '^Summary:' "$WORK/out" | head -1 || true)"
  if [ "$rc" -eq "$want" ] && printf '%s' "$v" | grep -qE "$wantsum"; then
    printf '  [OK]  %-44s exit %s | %s\n' "$label" "$rc" "$v"
  else
    printf '  [XX]  %-44s exit %s (want %s) | %s\n' "$label" "$rc" "$want" "$v"
    sed 's/^/        /' "$WORK/out"; rc_all=1
  fi
}

echo "=== phase-05-octavia-verify.sh (fake juju/openstack + real jq) ==="

# PRE: octavia blocked, resources empty -> PROCEED
run 0 'PROCEED' "pre: blocked + empty -> run 5.1" \
  FIX_JUJU_STATUS="$WORK/status-blocked.json" \
  FIX_NETS="$WORK/nets-empty" FIX_SUBS="$WORK/subs-empty" FIX_SGS="$WORK/sgs-empty" FIX_IMGS="$WORK/imgs-empty"

# POST 5.1 done, no amphora yet -> CONTROL-PLANE-DONE (real-world o-hm0 state = UNKNOWN)
run 0 'CONTROL-PLANE-DONE' "post: active + resources + o-hm0(UNKNOWN), no image" \
  FIX_JUJU_STATUS="$WORK/status-active.json" OHM0_OUT="$UNK_OHM0" \
  FIX_NETS="$WORK/nets-present" FIX_SUBS="$WORK/subs-present" FIX_SGS="$WORK/sgs-present" FIX_IMGS="$WORK/imgs-empty"

# Full PASS: 5.1 + 5.2 done
run 0 'PASS' "post: active + resources + o-hm0 + amphora" \
  FIX_JUJU_STATUS="$WORK/status-active.json" OHM0_OUT="$UP_OHM0" \
  FIX_NETS="$WORK/nets-present" FIX_SUBS="$WORK/subs-present" FIX_SGS="$WORK/sgs-present" FIX_IMGS="$WORK/imgs-active"

# config gate FAIL: amp-image-tag mismatch
run 1 'HOLD|FAIL' "config fail: amp-image-tag mismatch" \
  FIX_JUJU_STATUS="$WORK/status-blocked.json" CFG_OTAG="octavia-amphora-x" \
  FIX_NETS="$WORK/nets-empty" FIX_SUBS="$WORK/subs-empty" FIX_SGS="$WORK/sgs-empty" FIX_IMGS="$WORK/imgs-empty"

# config gate FAIL: image-format not raw
run 1 'HOLD|FAIL' "config fail: image-format qcow2" \
  FIX_JUJU_STATUS="$WORK/status-blocked.json" CFG_IMGFMT="qcow2" \
  FIX_NETS="$WORK/nets-empty" FIX_SUBS="$WORK/subs-empty" FIX_SGS="$WORK/sgs-empty" FIX_IMGS="$WORK/imgs-empty"

# POST FAIL: active but lb-mgmt resources missing
run 1 'HOLD|FAIL' "post fail: active but resources missing" \
  FIX_JUJU_STATUS="$WORK/status-active.json" OHM0_OUT="$UP_OHM0" \
  FIX_NETS="$WORK/nets-empty" FIX_SUBS="$WORK/subs-empty" FIX_SGS="$WORK/sgs-empty" FIX_IMGS="$WORK/imgs-empty"

# POST FAIL: o-hm0 not up
run 1 'HOLD|FAIL' "post fail: o-hm0 down" \
  FIX_JUJU_STATUS="$WORK/status-active.json" OHM0_OUT="o-hm0 DOWN" \
  FIX_NETS="$WORK/nets-present" FIX_SUBS="$WORK/subs-present" FIX_SGS="$WORK/sgs-present" FIX_IMGS="$WORK/imgs-empty"

# POST FAIL: o-hm0 absent (juju exec returned nothing)
run 1 'HOLD|FAIL' "post fail: o-hm0 absent" \
  FIX_JUJU_STATUS="$WORK/status-active.json" OHM0_OUT="" \
  FIX_NETS="$WORK/nets-present" FIX_SUBS="$WORK/subs-present" FIX_SGS="$WORK/sgs-present" FIX_IMGS="$WORK/imgs-empty"

# PRE anomaly: blocked but resources already present
run 1 'HOLD|FAIL' "pre anomaly: blocked but resources exist" \
  FIX_JUJU_STATUS="$WORK/status-blocked.json" \
  FIX_NETS="$WORK/nets-present" FIX_SUBS="$WORK/subs-present" FIX_SGS="$WORK/sgs-present" FIX_IMGS="$WORK/imgs-empty"

echo
if [ "$rc_all" -eq 0 ]; then echo "ALL PASS"; else echo "SOME FAILED"; fi
exit "$rc_all"
