#!/bin/sh
# 20180205
# Jan Mojzis
# Public domain.

#change directory to $AUTOPKGTEST_TMP
cd "${AUTOPKGTEST_TMP}"

cleanup() {
  ex=$?
  if [ "${ex}" -ne 0 ]; then
    (
      exec >&2
      echo "log:"
      cat log || :
      echo "id:"
      id || :
      echo
      echo "systemctl status dqcache:"
      systemctl status dqcache || :
      echo
      echo "journalctl -x --no-tail --no-pager :"
      journalctl -x --no-tail --no-pager  || :
      echo "dqcache test failed !!!"
    )
  fi
  exit "${ex}"
}
trap "cleanup" EXIT TERM INT


# dqcache handles localhost internally, giving it an A record of 127.0.0.1 (UDP query)
dq a localhost 127.0.0.1 1>log 2>&1

exp="answer: localhost 655360 A 127.0.0.1"
out="`grep '^answer: ' log`"

if [ x"${exp}" != x"${out}" ]; then
  echo "dq - unexpected output for query dq a localhost 127.0.0.1:" >&2
  exit 2
fi

# dqcache handles localhost internally, giving it an A record of 127.0.0.1 (TCP query)
dq -t a localhost 127.0.0.1 1>log 2>&1

exp="answer: localhost 655360 A 127.0.0.1"
out="`grep '^answer: ' log`"

if [ x"${exp}" != x"${out}" ]; then
  echo "dq - unexpected output for query dq -t a localhost 127.0.0.1:" >&2
  exit 3
fi


# test if dqcache runs after reboot
if [ x"${AUTOPKGTEST_REBOOT_MARK}" = x ]; then
  /tmp/autopkgtest-reboot rebootmark
fi

echo "dqcachetest works"
exit 0
