Newer
Older
openstack-caracal-ipv4 / tests / provider-vip-standup / run-tests.sh
#!/usr/bin/env bash
# Behavior regression for provider-vip-standup.sh (D-057). Fake `maas` + real jq.
# Drives the script in DRY-RUN and asserts the emitted WOULD/SKIP/fail behaviour.
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT="$(cd "$HERE/../../scripts" && pwd)/provider-vip-standup.sh"
BIN="$HERE/fakebin"; FIX="$HERE/fix"
chmod +x "$BIN"/* 2>/dev/null || true   # GitHub Desktop lands files mode 100644
command -v jq >/dev/null || { echo "FAIL: jq required"; exit 1; }
rc_all=0; OUT="$(mktemp)"

run() {  # <want_exit> <label> <subnets> <spaces> <vlans> <ipranges>
  local want="$1" label="$2"
  PATH="$BIN:$PATH" FIX_SUBNETS="$3" FIX_SPACES="$4" FIX_VLANS="$5" FIX_IPRANGES="$6" \
    bash "$SCRIPT" >"$OUT" 2>&1
  local rc=$?
  if [ "$rc" -ne "$want" ]; then
    printf '  [XX] %-38s exit %s (want %s)\n' "$label" "$rc" "$want"; sed 's/^/        /' "$OUT"; rc_all=1; return 1
  fi
  printf '  [ok] %-38s exit %s\n' "$label" "$rc"; return 0
}
has()   { grep -qE "$1" "$OUT" || { printf '       MISS expected /%s/\n' "$1"; rc_all=1; }; }
absent(){ grep -qE "$1" "$OUT" && { printf '       LEAK forbidden /%s/\n' "$1"; rc_all=1; } || true; }
count() { local n; n=$(grep -cE "$1" "$OUT"); [ "$n" -eq "$2" ] || { printf '       COUNT /%s/ = %s (want %s)\n' "$1" "$n" "$2"; rc_all=1; }; }

echo "=== provider-vip-standup.sh -- D-057 plane stand-up (fake maas + real jq) ==="

run 0 "fresh: full create sequence" "$FIX/sub_fresh.json" "$FIX/spaces_fresh.json" "$FIX/vlans_fresh.json" "$FIX/ipr_fresh.json"
has 'provider fabric_id = 1'
has 'WOULD: create space provider-vip'
has 'WOULD: create VLAN vid=104 name=provider-vip mtu=1500 on fabric 1'
absent 'mtu=9000'
has 'WOULD: assign fabric 1 vid 104 -> space provider-vip'
has 'WOULD: create subnet 10\.12\.8\.0/22 vlan=<vid-104-vlan-obj-id>'
has 'WOULD: subnet 10\.12\.8\.0/22 -> gateway_ip=10\.12\.8\.1'
has 'WOULD: subnet 10\.12\.8\.0/22 -> managed=true'
has 'WOULD: subnet 10\.12\.8\.0/22 -> dns_servers=10\.12\.12\.1'
has 'WOULD: create reserved range 10\.12\.8\.2-10\.12\.8\.100'
absent 'hardcoded|10\.12\.8\.0/22 vlan=5004'   # vlan obj must be a live lookup / placeholder, never a literal id

run 0 "idempotent: all present -> all SKIP" "$FIX/sub_done.json" "$FIX/spaces_done.json" "$FIX/vlans_done.json" "$FIX/ipr_done.json"
has 'space provider-vip exists -- SKIP'
has 'VID 104 already on fabric 1 -- SKIP'
has 'already in space provider-vip -- SKIP'
has 'subnet 10\.12\.8\.0/22 exists -- SKIP'
has 'gateway_ip already 10\.12\.8\.1 -- SKIP'
has 'reserved range starting 10\.12\.8\.2 exists -- SKIP'
count 'WOULD:' 0

run 1 "provider fabric missing -> fail" "$FIX/sub_nofab.json" "$FIX/spaces_fresh.json" "$FIX/vlans_fresh.json" "$FIX/ipr_fresh.json"
has 'provider fabric not found'

run 1 "subnet on wrong VID -> fail" "$FIX/sub_wrongvid.json" "$FIX/spaces_fresh.json" "$FIX/vlans_fresh.json" "$FIX/ipr_fresh.json"
has "subnet 10\.12\.8\.0/22 exists on VID '99', expected 104"

echo
[ "$rc_all" -eq 0 ] && echo "ALL PASS" || echo "SOME FAILED"
rm -f "$OUT"; exit "$rc_all"