#!/bin/sh

set -e

. ./helpers

ME=$(basename $0)

setup_sandbox

run_setup start
assert_eq "ikvswitch-setup start rc" "${SETUP_RC}" "0"
assert_contains "ikvswitch-setup start output" "${OUT}" "===> All done <==="

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

# The daemon is only a DHCP server: no DNS resolver, and it binds
# exclusively on the dhcp0 bridge.
assert_file_contains "dnsmasq config" "${CONF}" "port=0"
assert_file_contains "dnsmasq config" "${CONF}" "interface=dhcp0"
assert_file_contains "dnsmasq config" "${CONF}" "bind-dynamic"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-authoritative"

# Pool bounds, lease time and options all come from ikvswitch.conf.
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-range=192.168.110.20,192.168.110.250,255.255.255.0,600"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-option=3,192.168.110.1"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-option=6,1.1.1.1,9.9.9.9"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-option=15,example.com"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-option=28,192.168.110.255"

# Boot entries: the iPXE one first in the list of matches, and all of
# the client-arch ones guarded with tag:!ipxe, as an iPXE client also
# sends a value in option 93, and dnsmasq matches dhcp-boot entries in
# the reverse order of the configuration file.
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-userclass=set:ipxe,iPXE"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-match=set:bios,option:client-arch,0"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-match=set:efi64,option:client-arch,7"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-match=set:efi64_9,option:client-arch,9"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-match=set:httpboot,option:client-arch,16"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-boot=tag:!ipxe,tag:bios,lpxelinux.0,,192.168.100.2"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-boot=tag:!ipxe,tag:efi64,shimx64.efi.signed,,192.168.100.2"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-boot=tag:!ipxe,tag:efi64_9,shimx64.efi.signed,,192.168.100.2"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-boot=tag:!ipxe,tag:httpboot,http://192.168.100.2/openstack-cluster-installer/tftp/shimx64.efi.signed"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-boot=tag:ipxe,http://192.168.100.2/oci/ipxe.php"
assert_file_contains "dnsmasq config" "${CONF}" "dhcp-boot=pxelinux.0,,192.168.100.2"

# Only the odd leaves carry a DHCP server.
assert_file_contains "leaf3 config" "${SANDBOX}/out/leaf3/etc/dnsmasq.d/ikvswitch.conf" "dhcp-range=192.168.111.20,192.168.111.250,255.255.255.0,600"
assert_file_contains "leaf5 config" "${SANDBOX}/out/leaf5/etc/dnsmasq.d/ikvswitch.conf" "dhcp-range=192.168.112.20,192.168.112.250,255.255.255.0,600"
assert_file_absent "leaf2 dhcp" "${SANDBOX}/out/leaf2/etc/dnsmasq.d/ikvswitch.conf"
assert_file_absent "leaf4 dhcp" "${SANDBOX}/out/leaf4/etc/dnsmasq.d/ikvswitch.conf"
assert_file_absent "leaf6 dhcp" "${SANDBOX}/out/leaf6/etc/dnsmasq.d/ikvswitch.conf"

# No isc-dhcp leftovers in dnsmasq mode.
assert_file_absent "leaf1 dhcpd.conf" "${SANDBOX}/out/leaf1/etc/dhcp/dhcpd.conf"

# Syntax check, when dnsmasq is available.
if command -v dnsmasq >/dev/null 2>&1 ; then
	if ! dnsmasq --test -C "${CONF}" >"${SANDBOX}/dnsmasq-test.out" 2>&1 ; then
		echo "${ME}: dnsmasq --test rejected the generated config:"
		cat "${SANDBOX}/dnsmasq-test.out"
		exit 1
	fi
	assert_contains "dnsmasq --test" "$(cat "${SANDBOX}/dnsmasq-test.out")" "syntax check OK"
fi

exit 0
