#!/bin/sh

set -e

ME=$(basename $0)
SCRIPT=$(readlink -f ../../../odi-gen-net-config)

T=$(mktemp -d /tmp/odi-gen-net-config-valid.XXXXXX)
trap 'rm -rf ${T}' EXIT
mkdir -p ${T}/root/etc/frr ${T}/root/etc/network
printf 'bgpd=no\nzebra=yes\nospfd=no\n' > ${T}/root/etc/frr/daemons

expect_fail () {
	DESC=${1}
	shift
	set +e
	"${SCRIPT}" --root ${T}/root --release bookworm "$@" >${T}/out 2>&1
	RC=$?
	set -e
	if [ "${RC}" = "0" ] ; then
		echo "${ME}: ${DESC} must fail, but exited 0:"
		cat ${T}/out
		exit 1
	fi
}

expect_fail "ovnvrf 0 (below OVN's range)" \
	--static-iface type=bgp,iface0=enp0s4,addr=192.168.101.131/24,asnum=65499,addbgpi=192.168.101.131,ovnvrf=0
expect_fail "ovnvrf above 4294967295" \
	--static-iface type=bgp,iface0=enp0s4,addr=192.168.101.131/24,asnum=65499,addbgpi=192.168.101.131,ovnvrf=4294967296
expect_fail "ovnvrf not an integer" \
	--static-iface type=bgp,iface0=enp0s4,addr=192.168.101.131/24,asnum=65499,addbgpi=192.168.101.131,ovnvrf=abc
expect_fail "conflicting ovnvrf values across specs" \
	--static-iface type=bgp,iface0=enp0s4,addr=192.168.101.131/24,asnum=65499,addbgpi=192.168.101.131,ovnvrf=42 \
	--static-iface type=bgp,iface0=enp0s4,addr=192.168.101.131/24,asnum=65499,addbgpi=192.168.101.131,ovnvrf=43
expect_fail "type=bgp without addr, addr6 or addbgpi" \
	--static-iface type=bgp,iface0=enp0s4,asnum=65499
expect_fail "type=forcebrexmac without ovsbr=" \
	--static-iface type=forcebrexmac,iface0=enp0s4,brexmacaddr=08:00:27:06:cc:c5,addr=192.168.101.131/24
expect_fail "invalid router-id (not a dotted quad)" \
	--static-iface type=normal,iface0=enp0s4,addr=192.168.101.131/24,addr6=2001:db8::1/64,router-id=not.a.quad
expect_fail "router-id with an octet above 255" \
	--static-iface type=normal,iface0=enp0s4,addr=192.168.101.131/24,addr6=2001:db8::1/64,router-id=192.168.101.999
expect_fail "unknown option" \
	--this-option-does-not-exist
expect_fail "missing --static-iface value" \
	--static-iface

# A warning-only case: an unrecognized --static-iface sub-parameter is
# reported on stderr but does not abort (pre-existing semantics).
set +e
"${SCRIPT}" --root ${T}/root --release bookworm \
	--static-iface type=normal,iface0=enp0s4,addr=192.168.101.131/24,bogusparam=yes >${T}/out 2>&1
RC=$?
set -e
if [ "${RC}" != "0" ] || ! grep -q "Unrecognized parameter" ${T}/out ; then
	echo "${ME}: the bogus sub-parameter must warn and continue"
	cat ${T}/out
	exit 1
fi

# ovnvrf= without any BGP setup is silently accepted and ignored: no
# frr.conf is written (the render is gated on the BGP/force-frr flow).
# A fresh root is used, since the failed cases above may have written
# partial output into the shared one.
rm -rf ${T}/fresh
mkdir -p ${T}/fresh/etc/frr ${T}/fresh/etc/network
printf 'bgpd=no\nzebra=yes\nospfd=no\n' > ${T}/fresh/etc/frr/daemons
"${SCRIPT}" --root ${T}/fresh --release bookworm \
	--static-iface type=normal,iface0=enp0s4,addr=192.168.101.131/24,ovnvrf=42
if [ -e ${T}/fresh/etc/frr/frr.conf ] ; then
	echo "${ME}: ovnvrf= without a BGP setup must not write frr.conf"
	exit 1
fi

exit 0
