#!/usr/bin/env bash
# tests/phase-04/run-tests.sh -- offline regression for phase-04-network-verify.sh.
# Behavior-tests the REAL script against fake maas/openstack data shims + real jq.
# No live infra. Needs python3, bash, and jq on PATH.
set -euo pipefail
IFS=$'\n\t'
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPTS="$(cd "$HERE/../../scripts" && pwd)"
TARGET="$SCRIPTS/phase-04-network-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 (install jq)" >&2; exit 1; }
[ -f "$TARGET" ] || { echo "FAIL: target missing: $TARGET" >&2; exit 1; }
chmod +x "$BIN/maas" "$BIN/openstack" 2>/dev/null || true   # git/Windows drops exec bits

WORK="$(mktemp -d)"; trap 'rm -rf "$WORK"' EXIT
python3 "$HERE/make_fixtures.py" "$WORK" >/dev/null
rc_all=0

# run <want_rc> <want_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" >"$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]  %-46s exit %s | %s\n' "$label" "$rc" "$v"
  else
    printf '  [XX]  %-46s exit %s (want %s) | %s\n' "$label" "$rc" "$want" "$v"
    sed 's/^/        /' "$WORK/out"; rc_all=1
  fi
}

echo "=== phase-04-network-verify.sh (fake maas/openstack + real jq) ==="

# PRE clear, network not yet created -> PROCEED
run 0 'PROCEED' "pre clear, net absent" \
  FIX_SUBNETS="$WORK/subnets-good.json" FIX_IPRANGES="$WORK/ipranges-good.json"

# POST pass, allocation_pools as list-of-objects
run 0 'PASS' "post pass (object pool)" \
  FIX_SUBNETS="$WORK/subnets-good.json" FIX_IPRANGES="$WORK/ipranges-good.json" \
  FIX_NET="$WORK/net-good.json" FIX_SUBNET="$WORK/subnet-good-objpool.json"

# DOCFIX-213: MAAS_PROFILE passed through (fails red if the SUT reverts to hardcoded 'admin':
# stub keyed to EXPECT_PROFILE=vr1-dc0-region would go silent -> 'not JSON' precondition exit 2).
run 0 'PROCEED' "MAAS_PROFILE=vr1-dc0-region passed through" \
  MAAS_PROFILE=vr1-dc0-region EXPECT_PROFILE=vr1-dc0-region \
  FIX_SUBNETS="$WORK/subnets-good.json" FIX_IPRANGES="$WORK/ipranges-good.json"

# POST pass, allocation_pools as list-of-strings (tolerance proof)
run 0 'PASS' "post pass (string pool)" \
  FIX_SUBNETS="$WORK/subnets-good.json" FIX_IPRANGES="$WORK/ipranges-good.json" \
  FIX_NET="$WORK/net-good.json" FIX_SUBNET="$WORK/subnet-good-strpool.json"

# FIP pool not reserved -> HOLD
run 1 'HOLD|FAIL' "pre fail: FIP pool not reserved" \
  FIX_SUBNETS="$WORK/subnets-good.json" FIX_IPRANGES="$WORK/ipranges-nofip.json"

# wrong provider gateway -> HOLD/FAIL
run 1 'HOLD|FAIL' "pre fail: wrong provider gateway" \
  FIX_SUBNETS="$WORK/subnets-badgw.json" FIX_IPRANGES="$WORK/ipranges-good.json"

# provider subnet absent (by CIDR) -> HOLD
run 1 'HOLD' "pre fail: provider subnet absent" \
  FIX_SUBNETS="$WORK/subnets-noprovider.json" FIX_IPRANGES="$WORK/ipranges-good.json"

# network exists but shared=true (Option B violation) -> FAIL
run 1 'HOLD|FAIL' "post fail: provider-ext shared=true" \
  FIX_SUBNETS="$WORK/subnets-good.json" FIX_IPRANGES="$WORK/ipranges-good.json" \
  FIX_NET="$WORK/net-bad-shared.json" FIX_SUBNET="$WORK/subnet-good-objpool.json"

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