#! /bin/sh
# PCP QA Test No. 274
# Throw a broken PMDA at libpcp_pmda and dbpmda
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

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

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

status=1
trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15

_filter()
{
    # "dbpmda> $" filter for Linux
    sed \
    	-e '/pmResult/s/0x[^ ]*/ADDR/g' \
    	-e '/pmResult/s/amp: 0.000000 .* numpmid:/amp: EPOCH in LOCALTIME numpmid:/' \
	-e 's/\/tmp.*/logfile/g' \
	-e 's/\(broken_pmda_2_[0-9]\)([0-9][0-9]*)/\1(PID)/' \
	-e 's/[A-Z][a-z][a-z] [A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/DATE/' \
	-e 's/ [0-9\.]*e-[0-9]* 0x/ REAL 0x/g' \
	-e 's/ 0 0x/ REAL 0x/g' \
        -e '/dbpmda> $/d' \
	-e '/Error PDU:/s/One-line/Oneline/'

# last sed substitution thanks to pv:937345 which changed the wording
# ... preserving the old form so QA passes
}

_filter2()
{
    sed \
    	-e 's/^\[.*\] broken_pmda_2_.([0-9]*)/broken pmda/g' \
        -e '/Info: direct map  =/d' \
        -e '/Info: metric map  =/d' \
	-e 's/^Log for.*/Log started/g' \
	-e 's/^Log finished.*/Log finished/g'
}

_filter_ins()
{
    sed \
	-e 's/.* \(hash table entries\)/  NNN \1/' \
	-e 's/.* \(non-leaf nodes\)/  NNN \1/' \
	-e 's/.* \(leaf nodes\)/  NNN \1/' \
	-e 's/.* \(bytes of symbol table\)/  NNN \1/'
}

_filterStartBrokenPMDA25 () {
    #  This filter came about as a result of a different ordering of
    #  output in both stdout and stderr from dbpmda in PCP 2.1. This
    #  filter makes it appear as it would in PCP 2.2 onwards so that
    #  the test can detect changes to the text itself rather than the
    #  position of the line "Start broken_pmda_2_5 PMDA..." in the
    #  output for the dbpmda "open" command.
    $PCP_AWK_PROG '
BEGIN   { found = false }
/Start broken_pmda_2_5 PMDA: / {
            if (found = false) found = $0
            else found = found $0
            next
        }
/dbpmda> / {
            if ( found != false ) {
                print found
                found = false
            }
            print $0
            next
        }
        { print }
'
}


# real QA test starts here
unset ROOT MAKEFLAGS

# Install the PMDA
cd $here/pmdas/broken
$sudo ./broken_Install -N < /dev/null | _filter_ins
cd $here

# Remove the PMDA
_remove()
{
    cd $here/pmdas/broken
    $sudo ./broken_Remove -N
    cd $here
}

trap "_remove; exit \$status" 0 1 2 3 15

dbpmda -ie 2>&1 <<End-of-File | _filter
open pipe pmdas/broken/broken_pmda_2_0 -D47104 -l $tmp.log
status
# Test the valid metrics
fetch broken.valid.one broken.valid.two broken.valid.three
text broken.valid.one
# Test for metric not defined in table but in the PMNS and fetch callback
fetch broken.bogus.one
# Test for metric not defined in table and PMNS but in the fetch callback
fetch broken.bogus.two
# Test for metric defined in table and fetch, but not the pmns
fetch broken.no.pmns
# Test for multiple fetch with an invalid metric between valid metrics
fetch broken.valid.one broken.bogus.one broken.valid.two
# Test for illegal data types
fetch broken.bad.type
# Test for metric with no fetch support
fetch broken.no.fetch
# Test for metric with no help
fetch broken.no.help broken.no.shorthelp broken.no.longhelp
text broken.no.help
text broken.no.shorthelp
text broken.no.longhelp
# Test for handling of bad semantics
fetch broken.bad.semantics
# Test for handling of bad scale
fetch broken.bad.scale
# Test for handling of empty instance lists
fetch broken.no.instances
# Test for handling of missing instances in fetch
fetch broken.no.instfetch
close
quit
End-of-File

sleep 1
[ -f $tmp.log ] && cat $tmp.log | _filter2
rm -f $tmp.log

dbpmda -ie 2>&1 <<End-of-File | _filter
open pipe pmdas/broken/broken_pmda_2_1 -D47104 -l $tmp.log
status
wait 5
fetch broken.bad.indom
close
quit
End-of-File

sleep 1
[ -f $tmp.log ] && cat $tmp.log | _filter2
rm -f $tmp.log

dbpmda -ie 2>&1 <<End-of-File | _filter
open pipe pmdas/broken/broken_pmda_2_2 -D47104 -l $tmp.log
status
close
quit
End-of-File

sleep 1
[ -f $tmp.log ] && cat $tmp.log | _filter2
rm -f $tmp.log

dbpmda -ie 2>&1 <<End-of-File | _filter
open pipe pmdas/broken/broken_pmda_2_3 -D47104 -l $tmp.log
status
close
quit
End-of-File

sleep 1
[ -f $tmp.log ] && cat $tmp.log | _filter2
rm -f $tmp.log

dbpmda -ie 2>&1 <<End-of-File | _filter
open pipe pmdas/broken/broken_pmda_2_4 -D47104 -l $tmp.log
status
text broken.valid.one
close
quit
End-of-File

sleep 1
[ -f $tmp.log ] && cat $tmp.log | _filter2
rm -f $tmp.log

dbpmda -ie 2>&1 <<End-of-File | _filter | _filterStartBrokenPMDA25
open pipe pmdas/broken/broken_pmda_2_5 -D47104 -l $tmp.log
status
text broken.valid.one
close
quit
End-of-File

sleep 1
[ -f $tmp.log ] && cat $tmp.log | _filter2
rm -f $tmp.log

# all done
status=0
exit
