#!/bin/sh
# PCP QA Test No. 759
# Exercise the Unbound PMDA installation and removal.
#
# Copyright (c) 2014 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

test -e $PCP_PMDAS_DIR/unbound || _notrun "unbound PMDA not installed"
if which unbound-control >/dev/null 2>&1
then
    :
else
    _notrun "unbound-control not installed"
fi

status=1	# failure is the default!
$sudo rm -rf $tmp.* $seq.full

unbound_remove()
{
    echo
    echo "=== remove unbound agent ==="
    $sudo ./Remove >$tmp.out 2>&1
    _filter_pmda_remove <$tmp.out
}

unbound_install()
{
    # start from known starting points
    cd $PCP_PMDAS_DIR/unbound
    $sudo ./Remove >/dev/null 2>&1

    echo
    echo "=== $iam agent installation ==="
    $sudo ./Install </dev/null >$tmp.out 2>&1
    # Check unbound metrics have appeared ... X metrics and Y values
    _filter_pmda_install <$tmp.out \
    | sed \
	-e '/^Waiting for pmcd/s/\.\.\.[. ]*$/DOTS/' \
    | $PCP_AWK_PROG '
/Check unbound metrics have appeared/ { if ($7 >= 100) $7 = "X"
                                        if ($10 >= 100) $10 = "Y"
                                      }
                                      { print }'
}

unbound_cleanup()
{
    # note: _restore_auto_restart pmcd done in _cleanup_pmda()
    _cleanup_pmda unbound
}

_prepare_pmda unbound
trap "unbound_cleanup; exit \$status" 0 1 2 3 15

_stop_auto_restart pmcd

_filter_unbound()
{
    # filter metric values for deterministic output
    tee -a $here/$seq.full | sed \
        -e 's/value [0-9][0-9]*/value NUMBER/'
}

# real QA test starts here
unbound_install

# pmdaunbound should have been started by the Install process - check
if pminfo -v unbound > $tmp.info 2> $tmp.err
then
    :
else
    echo "... failed! ... here is the Install log ..."
    cat $tmp.out
fi
cat $tmp.info $tmp.err | _filter_unbound

# Check that the correct metrics are present
pminfo unbound

unbound_remove
# success, all done
status=0
exit
