#!/usr/bin/env bash
# Behavior regression for carve-host-interfaces.sh D-057 provider-vip split.
# Fake `maas` (read-only fixtures) + real jq. Drives the carve in DRY-RUN for openstack0
# (octet .40) and asserts the emitted WOULD-actions (and absences).
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CARVE="$(cd "$HERE/../../scripts" && pwd)/carve-host-interfaces.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> <ifaces>
local want="$1" label="$2" subs="$3" ifs="$4"
PATH="$BIN:$PATH" FIX_MACHINES="$FIX/machines.json" FIX_MACHINE="$FIX/machine.json" \
FIX_SUBNETS="$subs" FIX_IFACES="$ifs" \
bash "$CARVE" openstack0 >"$OUT" 2>&1
local rc=$?
if [ "$rc" -ne "$want" ]; then
printf ' [XX] %-34s exit %s (want %s)\n' "$label" "$rc" "$want"; sed 's/^/ /' "$OUT"; rc_all=1; return
fi
printf ' [ok] %-34s exit %s\n' "$label" "$rc"
}
# assert a regex IS present / ABSENT in the last $OUT
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; }
echo "=== carve-host-interfaces.sh -- D-057 provider-vip split (fake maas + real jq) ==="
run 0 "fresh host: full carve emit" "$FIX/sub_ok.json" "$FIX/if_fresh.json"
has 'WOULD: unlink enp1s0\(id=100\) link id=900' # L3 stripped off uplink
has 'WOULD: create enp1s0\.104 \(VID 104' # tagged sub-if
has 'vlan=5004' # resolved by CIDR, not hardcoded
has 'WOULD: create br-prov-api' # the API bridge
has 'STATIC 10\.12\.8\.40 on subnet 4' # host static MOVED to .24.40
absent 'ip_address=10\.12\.4\.40' # OLD provider static must be GONE
absent 'STATIC 10\.12\.4\.40'
has 'enp1s0 already on VLAN 5001 -- SKIP vlan set' # idempotent vlan guard (already untagged)
run 1 "missing provider-vip subnet -> fail" "$FIX/sub_nopvip.json" "$FIX/if_fresh.json"
has 'no MAAS subnet for 10\.12\.8\.0/22'
run 1 "wrong provider-vip VID -> fail" "$FIX/sub_wrongvid.json" "$FIX/if_fresh.json"
has "provider-vip 10\.12\.8\.0/22 is VID '241', expected 104"
run 0 "idempotent: already split -> all SKIP" "$FIX/sub_ok.json" "$FIX/if_done.json"
has 'enp1s0\.104 exists -- SKIP'
has 'br-prov-api exists -- SKIP'
has 'br-prov-api already on 10\.12\.8\.0/22 -- SKIP'
absent 'WOULD: create enp1s0\.104'
absent 'WOULD: unlink enp1s0'
echo
[ "$rc_all" -eq 0 ] && echo "ALL PASS" || echo "SOME FAILED"
rm -f "$OUT"; exit "$rc_all"