#!/bin/sh

set -e

. ../helpers

ME=$(basename $0)

setup_sandbox
# The address plan lives in the stock configuration file, the
# user-owned values in ikvswitch-local.conf: customize one of each.
sed -i -e 's/^HOST_VIRTUAL_SUBNET_START_PREFIX=.*/HOST_VIRTUAL_SUBNET_START_PREFIX=192.170/' \
	"${SANDBOX}/etc/ikvswitch.conf"
sed -i -e 's/^MY_IP=.*/MY_IP=192.0.2.10/' \
	"${SANDBOX}/etc/ikvswitch-local.conf"

# A customized prefix must propagate to everything, with no hardcoded
# value left behind: the SNAT rule, the route towards the internet VM,
# and the host bridge address. The IPv6 plan is independent of the
# IPv4 one: it keeps deriving from HOST_VIRTUAL_SUBNET_START_PREFIX_V6
# (fd00:1:1) whatever the IPv4 base is.
run_net start
assert_eq "start with customized prefix rc" "${NET_RC}" "0"

mock_has "SNAT source subnet" "iptables -t nat -A POSTROUTING -s 192.170.96.0/19 ! -d 192.170.96.0/19 -j SNAT --to-source 192.0.2.10"
mock_has "route to the internet VM" "route replace 192.170.96/19 via 192.170.96.2"
mock_has "host bridge address" "ikhb 192.170.96.1 netmask 255.255.255.0 up"
mock_has "IPMI bridge address" "addr replace 192.170.109.1/24 dev bripmi"
mock_has "v6 route to the internet VM" "-6 route replace fd00:1:10::/59 via fd00:1:10::2 metric 1"
mock_has "v6 host bridge address" "-6 addr replace fd00:1:10::1/64 dev ikhb"

run_net stop
assert_eq "stop with customized prefix rc" "${NET_RC}" "0"
mock_has "host bridge address teardown" "addr del 192.170.96.1/24 dev ikhb"
mock_has "v6 host bridge address teardown" "-6 addr del fd00:1:10::1/64 dev ikhb"

exit 0
