#!/bin/sh

# Verify that ntp-ctl validates the shipped default configuration and a richer
# hand-written one, and that it rejects an invalid configuration.

set -e

. debian/tests/helper-functions

printf "Checking that the default config %s is installed: " "${NTPD_RS_CONFIG}"
test -f "${NTPD_RS_CONFIG}" && __test_ok || __test_fail

printf "Validating the default configuration: "
ntp-ctl validate -c "${NTPD_RS_CONFIG}" && __test_ok || __test_fail

rich_config="${AUTOPKGTEST_TMP:-/tmp}/rich.toml"
cat <<EOF > "${rich_config}"
[observability]
log-level = "warn"
observation-path = "/run/ntpd-rs/observe"

[[source]]
mode = "server"
address = "ntpd-rs.pool.ntp.org"

[[server]]
listen = "127.0.0.1:123"

[synchronization]
minimum-agreeing-sources = 1
single-step-panic-threshold = 1800
EOF

printf "Validating a richer configuration (server + source + thresholds): "
ntp-ctl validate -c "${rich_config}" && __test_ok || __test_fail

bad_config="${AUTOPKGTEST_TMP:-/tmp}/invalid.toml"
cat <<EOF > "${bad_config}"
[observability]
log-level = "info"
does-not-exist = true
EOF

printf "Checking that an invalid configuration is rejected: "
if ntp-ctl validate -c "${bad_config}" >/dev/null 2>&1; then
    __test_fail
else
    __test_ok
fi

exit 0
