#! /bin/sh
# PCP QA Test No. 102
# pmlc parser/scanner/error handling
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

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

# get standard filters
. ./common.product
. ./common.filter
. ./common.check

_cleanup()
{
    _restore_primary_logger
    $sudo rm -f $tmp $tmp.*
    exit $status
}

status=1
rm -f $seq.full
trap "_cleanup" 0 1 2 3 15

_filter_pmlc()
{
    # error message mapping is due to differences between IRIX and Linux
    #
    sed \
	-e 's/connect [0-9][0-9]*/connect PID/' \
	-e 's/Version [0-9].*/Version VERSION/' \
	-e 's/^parse error/Syntax error/' \
	-e 's/line [0-9]*/line NUM/g' \
	-e 's/^syntax error/Syntax error/' \
	-e 's/^Reason: Unknown or illegal.*$/Reason: No PMCD agent for domain of request/'
}

signal=$PCP_BINADM_DIR/pmsignal

# real QA test starts here
_writable_primary_logger
ls -l $PCP_VAR_DIR/config/pmlogger/config.default >>$seq.full
cat $PCP_VAR_DIR/config/pmlogger/config.default >>$seq.full
pmlc -P </dev/null 2>&1 >>$seq.full
$sudo $PCP_RC_DIR/pcp restart | _filter_pcp_start
_wait_for_pmcd
_wait_for_pmlogger

pmlc -ie <<End-of-File >$tmp.out 2>$tmp.err
# not connected
query sample.drift
advisory on 1 hour sample.drift

connect primary
# incomplete - syntax error
query sample.bin [
query { 
sample.drift
# eat input
# up to
# next } -- curly brace
}
query {
barf.no.metric
# suck to } ... not that one, this one ..
}

End-of-File
_filter_pmlc <$tmp.err
_filter_pmlc <$tmp.out

for delta in "-1 msec" "75 hours" "599 hours" "1200 hours"
do
    $sudo rm -f $tmp.*
    cat <<End-of-File >$tmp.config
log advisory on $delta sample.bin [100]
End-of-File
    # pmlogger -c $tmp.config -T 4 $tmp
    $sudo rm -f $tmp.*
    _start_up_pmlogger -L -c /dev/null -l $tmp.log -T 4 $tmp
    pmsleep 0.5
    pmlc -ie <<End-of-File >$tmp.out 2>$tmp.err
connect $pid
log advisory on $delta sample.bin [100]
End-of-File
_filter_pmlc <$tmp.err
_filter_pmlc <$tmp.out
    $sudo $signal -s TERM $pid
    _wait_pmlogger_end $pid
    _filter_pmlogger_log <$tmp.log
done
pmsleep 0.5

pmlc -ie <<End-of-File >$tmp.out 2>$tmp.err
connect primary
log mandatory on 1 hour { sample.bin, sample.drift, sample.seconds }
query sample.bin[333]
End-of-File
_filter_pmlc <$tmp.err
_filter_pmlc <$tmp.out

$sudo $signal -a pmdasample
pmsleep 0.5				# time to cleanup
pminfo -f sample >/dev/null 2>&1	# and make sure pmcd notices

pmlc -ie <<End-of-File >$tmp.out 2>$tmp.err
connect primary
query sample.bin[100]
query sample.bin[100]
End-of-File
_filter_pmlc <$tmp.err
_filter_pmlc <$tmp.out

$sudo $signal -a -s HUP pmcd
pmsleep 0.5				# time to cleanup
pmlc -ie <<End-of-File >$tmp.out 2>$tmp.err
connect primary
query sample.bin[100]

query { sample.bin,sample.drift sample.seconds }
query { sample.bin sample.drift,sample.seconds }
query sample.bin [100,200,300 400 500,700]
query sample.bin [100,"bin-200"]
End-of-File
_filter_pmlc <$tmp.err
_filter_pmlc <$tmp.out

pmlc -ie <<End-of-File >$tmp.out 2>$tmp.err
connect primary
log mandatory maybe {
    sample.bin
    sample.drift, sample.seconds
}
End-of-File
_filter_pmlc <$tmp.err
_filter_pmlc <$tmp.out

status=0
exit
