#!/bin/sh

set -e

. ./helpers

ME=$(basename $0)

setup_sandbox
sed -i -e 's/^DHCP_SERVER_DAEMON=.*/DHCP_SERVER_DAEMON=isc-dhcp/' "${SANDBOX}/etc/ikvswitch.conf"

run_setup start
assert_eq "ikvswitch-setup start rc" "${SETUP_RC}" "0"

CONF=${SANDBOX}/out/leaf1/etc/dhcp/dhcpd.conf
if ! [ -r "${CONF}" ] ; then
	echo "${ME}: missing generated dhcpd.conf: ${CONF}"
	exit 1
fi

# Pool bounds, lease times and options all come from ikvswitch.conf.
assert_file_contains "dhcpd.conf" "${CONF}" "default-lease-time 600;"
assert_file_contains "dhcpd.conf" "${CONF}" "max-lease-time 7200;"
assert_file_contains "dhcpd.conf" "${CONF}" "range 192.168.110.20 192.168.110.250;"
assert_file_contains "dhcpd.conf" "${CONF}" "option domain-name \"example.com\";"
assert_file_contains "dhcpd.conf" "${CONF}" "option domain-name-servers 1.1.1.1,9.9.9.9;"
assert_file_contains "dhcpd.conf" "${CONF}" "option routers 192.168.110.1;"
assert_file_contains "dhcpd.conf" "${CONF}" "option broadcast-address 192.168.110.255;"
assert_file_contains "dhcpd.conf" "${CONF}" "next-server 192.168.100.2;"

# Boot entries: iPXE first, then the client architectures, including
# native UEFI HTTP Boot (00:10), then the fallback.
assert_file_contains "dhcpd.conf" "${CONF}" "if exists user-class and option user-class = \"iPXE\" {"
assert_file_contains "dhcpd.conf" "${CONF}" "filename \"http://192.168.100.2/oci/ipxe.php\";"
assert_file_contains "dhcpd.conf" "${CONF}" "if option pxe-system-type = 00:00 {"
assert_file_contains "dhcpd.conf" "${CONF}" "filename \"lpxelinux.0\";"
assert_file_contains "dhcpd.conf" "${CONF}" "option pxe-system-type = 00:07 {"
assert_file_contains "dhcpd.conf" "${CONF}" "option pxe-system-type = 00:09 {"
assert_file_contains "dhcpd.conf" "${CONF}" "option pxe-system-type = 00:10 {"
assert_file_contains "dhcpd.conf" "${CONF}" "filename \"http://192.168.100.2/openstack-cluster-installer/tftp/shimx64.efi.signed\";"
assert_file_contains "dhcpd.conf" "${CONF}" "filename \"pxelinux.0\";"

# Only the odd leaves carry a DHCP server, and there is no dnsmasq
# leftover in isc-dhcp mode.
assert_file_contains "leaf5 config" "${SANDBOX}/out/leaf5/etc/dhcp/dhcpd.conf" "range 192.168.112.20 192.168.112.250;"
assert_file_absent "leaf2 dhcpd.conf" "${SANDBOX}/out/leaf2/etc/dhcp/dhcpd.conf"
assert_file_absent "leaf4 dhcpd.conf" "${SANDBOX}/out/leaf4/etc/dhcp/dhcpd.conf"
assert_file_absent "leaf6 dhcpd.conf" "${SANDBOX}/out/leaf6/etc/dhcp/dhcpd.conf"
assert_file_absent "leaf1 dnsmasq config" "${SANDBOX}/out/leaf1/etc/dnsmasq.d/ikvswitch.conf"

exit 0
