#! /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

signal=$PCP_BINADM_DIR/pmsignal
status=1
trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15
$sudo rm -f $here/$seq.full

_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 '/Error: .*() failed/s/Broken pipe/[PMDA died]/' \
    	-e '/Error: .*() failed/s/PDU empty, PMDA may have died/[PMDA died]/' \
	-e "s;$tmp;TMP;g" \
	-e 's/(pid: [0-9][0-9]*)/(pid: PID)/' \
	-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/' \
    # end

# 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

echo "=== Install broken namespace ==="
cd $here/pmdas/broken
if pmnsmerge $PCP_VAR_DIR/pmns/root ./root $tmp.pmns
then
    :
else
    echo "Error: pmnsmerge failed!"
    exit
fi
$sudo cp $tmp.pmns $PCP_VAR_DIR/pmns/root
$sudo $signal -a -s HUP pmcd >/dev/null 2>&1
# ensure everything is built
if [ -f GNUmakefile.install ]
then
    $sudo $PCP_MAKE_PROG -f GNUmakefile.install >>$here/$seq.full 2>&1
else
    $sudo $PCP_MAKE_PROG >>$here/$seq.full 2>&1
fi
cd $here

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

trap "_remove; rm -f $tmp.*; 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
if [ -f $tmp.log ]
then
    cat $tmp.log | _filter2
    echo "=== PMDA should survive tests ===" >>$here/$seq.full
    cat $tmp.log >>$here/$seq.full
    rm -f $tmp.log
fi

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
if [ -f $tmp.log ]
then
    cat $tmp.log | _filter2
    echo >>$here/$seq.full
    echo "=== broken_pmda_2_1 ===" >>$here/$seq.full
    cat $tmp.log >>$here/$seq.full
    rm -f $tmp.log
fi

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
if [ -f $tmp.log ]
then
    cat $tmp.log | _filter2
    echo >>$here/$seq.full
    echo "=== broken_pmda_2_2 ===" >>$here/$seq.full
    cat $tmp.log >>$here/$seq.full
    rm -f $tmp.log
fi

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
if [ -f $tmp.log ]
then
    cat $tmp.log | _filter2
    echo >>$here/$seq.full
    echo "=== broken_pmda_2_3 ===" >>$here/$seq.full
    cat $tmp.log >>$here/$seq.full
    rm -f $tmp.log
fi

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
if [ -f $tmp.log ]
then
    cat $tmp.log | _filter2
    echo >>$here/$seq.full
    echo "=== broken_pmda_2_4 ===" >>$here/$seq.full
    cat $tmp.log >>$here/$seq.full
    rm -f $tmp.log
fi

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
if [ -f $tmp.log ]
then
    cat $tmp.log | _filter2
    echo >>$here/$seq.full
    echo "=== broken_pmda_2_5 ===" >>$here/$seq.full
    cat $tmp.log >>$here/$seq.full
    rm -f $tmp.log
fi

# all done
status=0
exit
