#!/bin/sh

set -e

AGENT=named
CONFIGS=/usr/share/resource-agents/ocft/configs

# currently failing on Ubuntu autopkgtest setup for networking differences, so skip there
# Working in Debci, but compared to badtest retains all the other tests coverage
if [ "`lsb_release -is`" = "Ubuntu" ]; then
  exit 77
fi

cleanup () {
  ocft clean $AGENT
}

trap "cleanup" 0 2 3 15

# Prepare bind localhost config used to check service status.
if ! [ -f /etc/bind/db.local ]; then
  cat > /etc/bind/db.local <<EOF
;
; BIND data file for local loopback interface
;
\$TTL	604800
@	IN	SOA	localhost. root.localhost. (
			     2		; Serial
			604800		; Refresh
			 86400		; Retry
			2419200		; Expire
			604800 )	; Negative Cache TTL
;
@	IN	NS	localhost.
@	IN	A	127.0.0.1
@	IN	AAAA	::1
EOF

  grep -Fq "/etc/bind/db.local" /etc/bind/named.conf.local || \
  cat >> /etc/bind/named.conf.local <<EOF

zone "localhost" {
	type master;
	file "/etc/bind/db.local";
};
EOF
  rndc reload
fi

mkdir -p /run/resource-agents

echo "Running: ocft make -d $CONFIGS $AGENT"
ocft make -d $CONFIGS $AGENT

echo "Running: ocft test -v $AGENT"
ocft test -v $AGENT
