#!/bin/sh
# PCP QA Test No. 439
# Additional coverage for __pmConnectLogger
#
# Copyright (c) 2011 Ken McDonell.  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=0	# success is the default!
$sudo rm -rf $tmp.* $seq.full
trap "rm -f $tmp.*; $sudo rm -f $PCP_TMP_DIR/pmlogger/$$; exit \$status" 0 1 2 3 15
primary_port=`sed -e 1q $PCP_TMP_DIR/pmlogger/primary`

_filter()
{
    sed \
	-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/logcontrol([0-9][0-9]*)/logcontrol(PID)/' \
	-e "s/$$/MYPID/g" \
	-e "s;$PCP_TMP_DIR;\$PCP_TMP_DIR;g" \
	-e 's/TCP connection reset by peer/Connection refused/'
}

# real QA test starts here
export PMLOGGER_REQUEST_TIMEOUT=10
src/logcontrol -P $primary_port enquire sample.long.write_me

echo
export PMLOGGER_REQUEST_TIMEOUT=10ABC
src/logcontrol -P$primary_port enquire sample.long.write_me 2>&1 \
| _filter
unset PMLOGGER_REQUEST_TIMEOUT

echo
# $PM_LOG_ALL_PIDS is not useful
src/logcontrol -Dcontext -p-1 enquire sample.long.write_me 2>&1 \
| egrep '(__pmConnectLogger)|(logcontrol)'

echo
# punt that pid 1 is not pmlogger!
src/logcontrol -Dcontext -p1 enquire sample.long.write_me 2>&1 \
| egrep '(__pmConnectLogger)|(logcontrol)' \
| _filter

# start playing with libpcp's mind by munging files in
# $PCP_TMP_DIR/pmlogger
touch $tmp.ctl
$sudo cp $tmp.ctl $PCP_TMP_DIR/pmlogger/$$
src/logcontrol -Dcontext,log -p$$ enquire sample.long.write_me 2>&1 \
| egrep '(/__pmConnectLogger)|(__pmLog)|(logcontrol)' \
| _filter
echo "bad port" >$tmp.ctl
$sudo cp $tmp.ctl $PCP_TMP_DIR/pmlogger/$$
src/logcontrol -Dcontext,log -p$$ enquire sample.long.write_me 2>&1 \
| egrep '(/__pmConnectLogger)|(__pmLog)|(logcontrol)' \
| _filter
echo "123456" >$tmp.ctl
$sudo cp $tmp.ctl $PCP_TMP_DIR/pmlogger/$$
src/logcontrol -Dcontext,log -p$$ enquire sample.long.write_me 2>&1 \
| egrep '(/__pmConnectLogger)|(__pmLog)|(logcontrol)' \
| _filter
cat <<End-of-File >$tmp.ctl
123456
no.such.host.pcp.io
End-of-File
$sudo cp $tmp.ctl $PCP_TMP_DIR/pmlogger/$$
src/logcontrol -Dcontext,log -p$$ enquire sample.long.write_me 2>&1 \
| egrep '(/__pmConnectLogger)|(__pmLog)|(logcontrol)' \
| _filter
cat <<End-of-File >$tmp.ctl
123456
no.such.host.pcp.io
/no/such/file
End-of-File
$sudo cp $tmp.ctl $PCP_TMP_DIR/pmlogger/$$
src/logcontrol -Dcontext,log -p$$ enquire sample.long.write_me 2>&1 \
| egrep '(/__pmConnectLogger)|(__pmLog)|(logcontrol)' \
| _filter


# success, all done
exit
