#!/bin/bash
#----------------
# Testing agent against puppetmaster-passenger
#----------------
set -e

test_description="puppet agent -> puppet master (passenger, apache2)"
test_token='82cc6efd-19e3-4f20-9ca8-58364f8a10d0'

echo "PREPARE: ${test_description}"

##############################
echo " - configure puppet master"
server=$(facter fqdn)
cat > /etc/puppet/puppet.conf <<EOF
[main]
logdir=/var/log/puppet
vardir=/var/lib/puppet
ssldir=/var/lib/puppet/ssl
rundir=/var/run/puppet
factpath=/var/lib/puppet/lib/facter
templatedir=/etc/pupp/templates
server=${server}

[master]
ssl_client_header = SSL_CLIENT_S_DN 
ssl_client_verify_header = SSL_CLIENT_VERIFY
EOF

##############################
echo " - add manifest with notify: ${test_token}"
cat > /etc/puppet/manifests/site.pp <<EOF
node default {
  notify { '${test_token}': }
}
EOF

##############################
echo " - connect with puppet agent, and expect ${test_token}"
tempfile=$(tempfile)
puppet agent --enable
puppet agent --test --logdest console | tee "$tempfile"

if grep -q "$test_token" "$tempfile"; then
  echo "OK: ${test_description}"
else
  echo >&2 "FAIL: ${test_description}"
  exit 1
fi
