#!/bin/sh
# PCP QA Test No. 1033
# Exercise the named PMDA.
#
# Copyright (c) 2016 Red Hat.
#

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

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

[ -d "$PCP_PMDAS_DIR/named" ] || _notrun "named PMDA not installed"
perl -e "use PCP::PMDA" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "perl PCP::PMDA module not installed"

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

_cleanup()
{
    $sudo rm -f $here/named.log
    rm -f $tmp.*
}
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter()
{
    sed \
        -e "s;$PCP_PMDAS_DIR;\$PCP_PMDAS_DIR;" \
        -e "s;$script;pmdanamed.pl;" \
    | _filter_dumpresult
}

_filter_pmda_log()
{
    sed \
        -e 's/^\(Log for pmda[^ ]* on\) .*/\1 HOST .../' \
        -e 's/^\(Log finished\) .*/\1 .../' \
        -e '/^$/d' \
    #end
}


# real QA test starts here
script=$PCP_PMDAS_DIR/named/pmdanamed.pl

# create a set of commands for dbpmda
cat >$tmp.cmds <<End-of-File
open pipe /usr/bin/perl $script
children named
desc named.interval
fetch named.interval
End-of-File

export NAMED_STATS_USER=`id -u -n`
export NAMED_STATS_PROG=/bin/true

# iterate over all of the pre-canned staticstics files
test="$here/named"
for file in $test/*.txt
do
    export NAMED_STATS_FILE="$file"
    PCP_PERL_PMNS=root /usr/bin/perl $script > $tmp.root

    echo === Test case: `basename $file`
    $sudo rm -f named.log
    dbpmda -n $tmp.root -ie < $tmp.cmds 2>&1 | _filter
    _filter_pmda_log < named.log
done

# success, all done
status=0
exit
