#!/bin/bash
#
# Tests that we can start, stop, and restart.

set -e
. "${0%/*}"/acommon

test-prep

rsyslog-capture-daemon
service rsyslog force-reload

perl <test/test.cfg >/etc/hippotat/main.cfg -pe '
	s{^addrs *=.*}{addrs = 127.0.0.1};
'

cat >>/etc/default/hippotatd <<END
set -x
CHECK_FIREWALL=false
USER=root
END

# hippotatd is not actually running now because the init script check
# if we have any configured endpoints, and doesn't run it if not.
# With sysvinit, "start" works to start the deaemon.
# With systemd, "start" doesn't work because systemd thought it
# started it when it was installed.  There's apparently a "pidfile"
# thing we could perhaps put in the LSB headers, but I couldn't find
# any formal documentation of it.  Instead, "restart" ought to DTRT.
service hippotatd restart

curl http://localhost:8099/ | tee /dev/stderr | grep hippotat

dpid=$(pidof hippotatd)

service hippotatd restart

dpid2=$(pidof hippotatd)

test $dpid != $dpid2

service hippotatd stop

( LC_MESSAGES=C nc -v localhost 8099 2>&1 ||: ) \
    | tee /dev/stderr | grep 'Connection refused'

t-ok
