#! /bin/sh
# PCP QA Test No. 041
# Test $PCP_RC_DIR/pcp start 
#
# 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

[ $PCP_PLATFORM = mingw ] && _notrun "Process stop signal not available on Win32"

_filter()
{
    tee -a $seq.full \
    | _filter_pmcd_log \
    | sed \
	-e '/^ *$/d' \
	-e 's/ -t [0-9][0-9]* / /' \
	-e '/UID/d' \
	-e '/UNAME/d' \
	-e '/USER/d' \
	-e '/host/s/host "[^"]*"/host SOMEHOST/' \
	-e 's/^ *root[^/]*\//    USERNAME ... \//' \
	-e 's/^ *pcp[^/]*\//    USERNAME ... \//' \
	-e 's/pmcd -.*/pmcd/' \
	-e '/__pmGetPDU:.*illegal PDU len=0/d' \
    | _filter_pcp_start
}

_expect()
{
    echo "" | tee -a $seq.full
    echo "### Expect: $* ###" | tee -a $seq.full
    echo "" | tee -a $seq.full
}

tmp=/var/tmp/qa041.$$
_needclean=true
TAG=000666000magic
status=1
[ -z "$PCP_PMLOGGERCONTROL_PATH" ] && \
	PCP_PMLOGGERCONTROL_PATH="$PCP_SYSCONF_DIR/pmlogger/control"

trap "_cleanup" 0 1 2 3 15

_stop_auto_restart pmcd

_cleanup()
{
    if $_needclean
    then
	[ -f $tmp.control ] && \
	    $sudo cp $tmp.control $PCP_PMLOGGERCONTROL_PATH
	_service pcp restart | _filter_pcp_start
	_restore_auto_restart pmcd
	_wait_for_pmcd
	_wait_for_pmlogger
	_needclean=false
    fi
    $sudo rm -f $tmp.*
    exit $status
}

if [ -d $PCP_LOG_DIR/pmlogger ]
then
    LOGGING_DIR=$PCP_LOG_DIR/pmlogger
else
    LOGGING_DIR=$PCP_LOG_DIR
fi

rm -f $seq.full

# real QA test starts here

# disable all pmloggers ...
$sudo cp $PCP_PMLOGGERCONTROL_PATH $tmp.control
cat <<End-of-File >$tmp.tmp
# dummy file created by qa/$seq on `date`
# the goal here is to have a controlled primary logger that does
# not make requests to pmcd!
\$version=1.1
LOCALHOSTNAME y n $LOGGING_DIR/LOCALHOSTNAME -c /dev/null
End-of-File
$sudo cp $tmp.tmp $PCP_PMLOGGERCONTROL_PATH

# I have no idea why this is required, but this test run
# after 040 sometimes produces garbled messages unless
# this is done!
_service pcp stop >$tmp.out 2>$tmp.err
cat $tmp.out >>$seq.full
_filter_pcp_stop <$tmp.out
cat $tmp.err

# allow pmcd's socket teardown to complete
#
sleep 2

rm -f $tmp.out $tmp.err
_expect "connection refused"
pminfo -f sample.long.million 2>&1 | _filter

# Note: need to use $PCP_RC_DIR/* here, not _service pcp so that
# systemd (if running) does not get to see the pmcd exit status
#
( $sudo $PCP_RC_DIR/pmcd restart; $sudo $PCP_RC_DIR/pmlogger restart ) >$tmp.one 2>&1
_wait_for_pmcd
_wait_for_pmlogger

_expect "one million retrieved OK from sample.long.million"
pminfo -f sample.long.million

sleep 3		# make sure pmlogger is not clobbered

pid=`_get_pids_by_name pmcd`
if [ -z "$pid" ]
then
    echo "Arrgh ... cannot find PID for pmcd!"
    ps $PCP_PS_ALL_FLAGS
    echo "=== pmcd.log ==="
    cat $PCP_LOG_DIR/pmcd/pmcd.log
    echo "=== pmcd.log.prev ==="
    cat $PCP_LOG_DIR/pmcd/pmcd.log.prev
    exit
fi
echo "pmcd pid=$pid" >>$seq.full
ps $PCP_PS_ALL_FLAGS | egrep "PID|$pid" >>$seq.full

$sudo kill -STOP $pid

sleep 3		# make sure pmcd gets the SIGSTOP

# this will hang, and then see a timeout failure when PMCD is
# killed off
$sudo rm -f $tmp.tmp
PMCD_CONNECT_TIMEOUT=1000
export PMCD_CONNECT_TIMEOUT
pminfo -f sample.long.million >$tmp.tmp 2>&1 &

_expect "Starting, Waiting, Forcing termination and Starting again"
ps $PCP_PS_ALL_FLAGS | egrep "PID|$pid" >>$seq.full
_service pcp restart >$tmp.two 2>&1
_wait_for_pmcd
_wait_for_pmlogger
wait

_filter_pcp_start <$tmp.one | _filter
_filter <$tmp.two
rm -f $tmp.out $tmp.err

_expect "Timeout waiting for a response from PMCD"
_filter <$tmp.tmp
rm -f $tmp.tmp

_expect "one million again"
pminfo -f sample.long.million

# all OK
status=0
exit
