#!/bin/sh
# PCP QA Test No. 1896
# test pmlogger SIGUSR2/reexec handling and strftime substitution
#
# Copyright (c) 2020 Red Hat.  All Rights Reserved.
#

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

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

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
$sudo rm -rf $tmp $tmp.* $seq.full
trap "_cleanup; exit \$status" 0 1 2 3 15
mkdir -p $tmp
PMLOGGER=$PCP_BIN_DIR/pmlogger
spec=qa-$seq-%Y%m%d.%H.%M

echo == checking strftime archive name substitution | tee -a $seq.full
base=`pmdate $spec`
nextbase=`pmdate +1M $spec`
$PMLOGGER -s10 -t 1s -c config.default -l $tmp/pmlogger.log $tmp/$spec 2>$tmp/$seq.err &
pid=$!; sleep 5; kill -TERM $pid
echo === log === >>$seq.full; cat $tmp/pmlogger.log >>$seq.full
echo === err === >>$seq.full; cat $tmp/$seq.err >>$seq.full
if [ -f $tmp/$base.index ]
then
    echo "found base: `ls -l $tmp/$base.index`" >>$seq.full
elif [ -f $nextbase.index ]
then
    echo "found nextbase: `ls -l $tmp/$nextbase.index`" >>$seq.full
else
    echo "FAILED, expected $base.index or $nextbase.index" | tee -a $seq.full
    ls -l $tmp >>$seq.full
    exit
fi

echo == checking SIGUSR2 starts a new log as non-root user | tee -a $seq.full
base=`pmdate $spec`
nextbase=`pmdate +1M $spec`
$PMLOGGER -U$username -s30 -t 1s -c config.default -l $tmp/pmlogger.log $tmp/$spec 2>$tmp/$seq.err &
pid=$!; sleep 5; 
$sudo -u $username kill -USR2 $pid; sleep 5
echo === log === >>$seq.full; cat $tmp/pmlogger.log >>$seq.full
echo === err === >>$seq.full; cat $tmp/$seq.err >>$seq.full
if [ -f $tmp/$base.index ]
then
    echo "found 1st base: `ls -l $tmp/$base.index`" >>$seq.full
elif [ -f $nextbase.index ]
then
    echo "found 1st nextbase: `ls -l $tmp/$nextbase.index`" >>$seq.full
else
    echo "FAILED, expected to find 1st $base.index or $nextbase.index" | tee -a $seq.full
    ls -l $tmp >>$seq.full
    exit
fi
if [ -f $tmp/$base-00.index ]
then
    echo "found 2nd base: `ls -l $tmp/$base-00.index`" >>$seq.full
elif [ -f $nextbase.index ]
then
    echo "found 2nd nextbase: `ls -l $tmp/$nextbase.index`" >>$seq.full
elif [ -f $nextbase-00.index ]
then
    echo "found 2nd nextbase: `ls -l $tmp/$nextbase-00.index`" >>$seq.full
else
    echo "FAILED, expected to find 2nd $base-00.index or $nextbase.index or $nextbase-00.index" | tee -a $seq.full
    ls -l $tmp >>$seq.full
    exit
fi

echo == check pmlc can connect to pmlogger after reexec
(echo status | pmlc $pid >>$seq.full) || echo pmlc failed to connect to pid $pid

# kill it off
$sudo -u $username kill -TERM $pid

# success, all done
status=0
exit
