#! /bin/sh
# PCP QA Test No. 457
# checks basic pmdalogger functionality
#
# Copyright (c) 2011 Aconex.  All Rights Reserved.
#
seq=`basename $0`
echo "QA output created by $seq"

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

[ -d $PCP_PMDAS_DIR/logger ] || _notrun "No pmdalogger installed"

_cleanup()
{
    if $_needclean
    then
	if [ -f $PCP_VAR_DIR/config/logger/logger.conf.$seq ]
	then
	    _restore_config $PCP_VAR_DIR/config/logger/logger.conf
	else
	    # don't leave our one behind here!
	    #
	    $sudo rm -f $PCP_VAR_DIR/config/logger/logger.conf
	fi
	_restore_pmda_install logger NOINSTALL
	if $install_on_cleanup
	then
	    ( cd $PCP_PMDAS_DIR/logger; $sudo ./Install <$tmp.input >/dev/null 2>&1 )
	else
	    ( cd $PCP_PMDAS_DIR/logger; $sudo ./Remove </dev/null >/dev/null 2>&1 )
	fi
	_needclean=false
    fi
    $sudo rm -fr $tmp.dir
    $sudo rm -f $tmp.*
    exit $status
}

_testdata()
{
    # generate data in a single write, for pmda determinism
    echo \
"TeSt DaTa ... TeSt DaTa ...
TeSt2 DaTa2 ... TeSt2 DaTa2"
}

_filter()
{
    sed \
	-e "s,$tmp,TMPFILE,g" \
	-e "s,$PCP_VAR_DIR,\$PCP_VAR_DIR,g" \
	-e 's/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\.[0-9][0-9]*[0-9]/TIMESTAMP/g' \
	-e '/^# Installed/s/ on .*/ on .../' \
    # end
}

_filter_dir()
{
    # size is filesystem and blocksize dependent
    _filter | sed '/^logger\.perfile\.dir\.size$/ {
N
s:[0-9][0-9]*:SIZE:
}'
}

$sudo rm -fr $tmp.dir

# setup pmdalogger configuration file
echo "
# Installed by PCP QA test $seq on `date`
reg	n	$tmp.reg
fifo	n	$tmp.fifo
none	n	$tmp.none
empty	n	$tmp.empty
dir	n	$tmp.dir
pipe	n	$tmp.pipe|
" >$tmp.conf

# and reinstall PMDA Install script (if needed)
cat >$tmp.input <<End-of-File
1
y
End-of-File

install_on_cleanup=false
if pminfo -v logger >>$seq_full 2>&1
then
    if [ -f "$PCP_VAR_DIR/config/logger/logger.conf" ]
    then
	install_on_cleanup=true
    else
	# guard against a broken setup on entry ... the pminfo above
	# will return with status 0 if the PMDA is installed (even if
	# no metric values are available), but if logger.conf is not
	# there the Install in _cleanup() is doomed ...
	#
	echo "Botched install at test startup ... $PCP_VAR_DIR/config/logger/logger.conf is missing" >>$seq_full
    fi
fi
echo "install_on_cleanup=$install_on_cleanup" >>$seq_full

status=1	# failure is the default!
_needclean=true
trap "_cleanup" 0 1 2 3 15

# real QA test starts here

# need to save PMDA config (if it exists) now, before
# _prepare_pmda_install() that runs make clobber and may
# remove it
#
[ -d $PCP_VAR_DIR/config/logger ] || $sudo mkdir -p $PCP_VAR_DIR/config/logger
[ -f $PCP_VAR_DIR/config/logger/logger.conf ] && \
_save_config $PCP_VAR_DIR/config/logger/logger.conf

_prepare_pmda_install logger || _exit 1

# prepare test case files (see below)
_testdata > $tmp.reg
wc $tmp.reg >>$seq_full
cat $tmp.reg >>$seq_full
mkdir $tmp.dir
mkfifo $tmp.fifo
touch $tmp.empty
$sudo rm -f $tmp.pipe
cat <<End-of-File >$tmp.pipe
#!/bin/sh
# block and print something when SIGUSR1 delivered
echo \$\$ > $tmp.pipe.pid
trap "echo child" USR1	# refactor-ok
read book
End-of-File
chmod 755 $tmp.pipe

# start out with the "pid" file empty and world-writeable (logger PMDA is
# running as user pcp)
#
$sudo rm -f $tmp.pipe.pid

# $tmp.pipe.pid needs to be writeable by user $PCP_USER
#
touch $tmp.pipe.pid
$sudo chown $PCP_USER:$PCP_GROUP $tmp.pipe.pid
$sudo chmod 664 $tmp.pipe.pid

#debug# echo "--- start script ---" >>$seq_full
#debug# cat $tmp.pipe >>$seq_full
#debug# echo "--- end script ---" >>$seq_full
#debug# ls -l $tmp.pipe >>$seq_full

$sudo cp $tmp.conf $PCP_VAR_DIR/config/logger/logger.conf

# install PMDA accepting the defaults (esp config file)
$sudo ./Remove < /dev/null >/dev/null 2>&1
export PCP_DEBUG=appl0,appl1
$sudo ./Install < /dev/null >$tmp.out 2>&1
unset PCP_DEBUG
cat $tmp.out | _filter | _filter_pmda_install

# wait up to 20 seconds for $tmp.pipe.pid to appear (meaning the $tmp.pipe
# command has started)
#
i=0
while [ $i -lt 20 ]
do
    echo "wait #$i" >>$seq_full
    $PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep -E "[P]PID|[.]pipe" >>$seq_full
    [ -s $tmp.pipe.pid ] && break
    sleep 1
    i=`expr $i + 1`
done
if [ ! -s $tmp.pipe.pid ]
then
    echo "Arrgh ..."
    $PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep -E "[P]PID|[.]pipe|pmda[l]ogger|$seq[.]"
    cat $PCP_LOG_DIR/pmcd/logger.log >>$seq_full
    read x </dev/tty
    _fail "PID file from command pipe not found"
fi


echo
echo "=== 1. simple working case ===" | tee -a $seq_full
$PCP_ECHO_PROG $PCP_ECHO_N "Checking initial data:""$PCP_ECHO_C"
pminfo -f logger.perfile.reg | _filter
_testdata >> $tmp.reg
wc $tmp.reg >>$seq_full
$PCP_ECHO_PROG $PCP_ECHO_N "Checking appended data:""$PCP_ECHO_C"
pminfo -f logger.perfile.reg | _filter

echo
echo "=== 2. named pipe (fifo) ===" | tee -a $seq_full
$PCP_ECHO_PROG $PCP_ECHO_N "Check initial pipe""$PCP_ECHO_C"
pminfo -f logger.perfile.fifo | _filter
$PCP_ECHO_PROG $PCP_ECHO_N "Checking new pipe data""$PCP_ECHO_C"
_testdata >> $tmp.fifo
pminfo -f logger.perfile.fifo | _filter
$PCP_ECHO_PROG $PCP_ECHO_N "Unlink the fifo""$PCP_ECHO_C"
rm -f $tmp.fifo
pminfo -f logger.perfile.fifo | _filter

echo
echo "=== 3. log file rotation ===" | tee -a $seq_full
$PCP_ECHO_PROG $PCP_ECHO_N "Checking removed file""$PCP_ECHO_C"
rm -f $tmp.reg
sleep 3	# pmda interval timer is 2 seconds
pminfo -f logger.perfile.reg  | _filter
$PCP_ECHO_PROG $PCP_ECHO_N "Checking new log file""$PCP_ECHO_C"
_testdata >> $tmp.reg
wc $tmp.reg >>$seq_full
sleep 3	# pmda interval timer is 2 seconds
pminfo -f logger.perfile.reg | _filter
wc $tmp.reg >>$seq_full
od -c $tmp.reg >>$seq_full

echo
echo "=== 4. non-existant file ===" | tee -a $seq_full
$PCP_ECHO_PROG $PCP_ECHO_N "Check a missing file""$PCP_ECHO_C"
pminfo -f logger.perfile.none | _filter
_testdata >> $tmp.none
wc $tmp.none >>$seq_full
sleep 3	# pmda interval timer is 2 seconds
$PCP_ECHO_PROG $PCP_ECHO_N "Checking new log file""$PCP_ECHO_C"
pminfo -f logger.perfile.none | _filter

echo
echo "=== 5. empty file ===" | tee -a $seq_full
$PCP_ECHO_PROG $PCP_ECHO_N "Check an empty file""$PCP_ECHO_C"
pminfo -f logger.perfile.empty | _filter
_testdata >> $tmp.empty
wc $tmp.empty >>$seq_full
sleep 3	# pmda interval timer is 2 seconds
$PCP_ECHO_PROG $PCP_ECHO_N "Checking new log file""$PCP_ECHO_C"
pminfo -f logger.perfile.empty | _filter

echo
echo "=== 6. directory ===" | tee -a $seq_full
$PCP_ECHO_PROG $PCP_ECHO_N "Check a directory""$PCP_ECHO_C"
pminfo -f logger.perfile.dir | _filter_dir

echo
echo "=== 7. command pipe ===" | tee -a $seq_full
pid=`cat $tmp.pipe.pid`
$PCP_ECHO_PROG $PCP_ECHO_N "Check a piped command""$PCP_ECHO_C"
pminfo -f logger.perfile.pipe | _filter
$PCP_ECHO_PROG $PCP_ECHO_N "Signal the command""$PCP_ECHO_C"
pmsignal -s USR1 $pid >/dev/null 2>&1
sleep 3	# some time for it to be delivered
pminfo -f logger.perfile.pipe | _filter

status=0
exit
