#!/bin/sh
# PCP QA Test No. 964
# Ensure pmlogger not (re)started via PMDA Install.
#
# We cannot use a PMDA that _might_ provide metrics that the primary
# pmlogger is logging ... when we Remove that PMDA, then pmlogger will
# at some point notice the metrics have gone away and exit ... leaving
# no pmlogger to be tested in this test case.
#
# So we use the test_perl PMDA.
#
# Copyright (c) 2015-2016 Red Hat.
#

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

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

perl -e "use PCP::PMDA" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "perl PCP::PMDA module not installed"

status=1	# failure is the default!
_needclean=true
$sudo rm -rf $tmp $tmp.* $seq.full

_interrupt()
{
    status=1
}

_cleanup()
{
    cd $here
    if $_needclean
    then
	if pmprobe -I pmcd.agent.status | grep '"test_perl"' >/dev/null
	then
	    cd $here/pmdas/test_perl
	    $sudo ./Remove >>$here/$seq.full 2>&1
	    $sudo rm -f domain.h.perl
	    cd $here
	fi
	_needclean=false
    fi
    $sudo rm -f $tmp.*
    exit $status
}

trap "_cleanup" 0
trap "_interrupt; _cleanup" 1 2 3 15

# don't need to regenerate config.default with pmlogconf
#
export PMLOGGER_CHECK_SKIP_LOGCONF=yes

_stop_auto_restart pmcd

_find_primary_pmlogger()
{
    if [ -d $PCP_TMP_DIR/pmlogger ]
    then
	cd $PCP_TMP_DIR/pmlogger
	ls -l >>$here/$seq.full
	if [ -L primary ]
	then
	    echo "`ls -l primary | sed -e 's;.*/;;'`"
	else
	    echo "No primary logger file?" >>$here/$seq.full
	fi
    fi
}

_filter_test_perl_install()
{
    _filter_pmda_install \
    | _filter_pcp_stop \
    | tee -a $here/$seq.full \
    | $PCP_AWK_PROG '
/Check test_perl metrics have appeared/ { if ($7 >= 0) $7 = "X"
                                           if ($10 >= 0) $10 = "Y"
                                         }
                                         { print }'
}

# real QA test starts here

pmcd_pid=`_get_pids_by_name pmcd`
if [ -n "$pmcd_pid" ]
then
    echo "Found initial pmcd"
else
    echo "Error: initial pmcd missing?"
    exit
fi

pid=`_find_primary_pmlogger`
if [ -n "$pid" ]
then
    echo "Found primary pmlogger"
else
    echo "Error: primary pmlogger missing?"
fi
echo

cd pmdas/test_perl

# install a PMDA, which should NOT restart pmcd
$sudo ./Install </dev/null \
| _filter_test_perl_install

# verify pmcd not restarted
#
new_pmcd_pid=`_get_pids_by_name pmcd`
if [ -n "$new_pmcd_pid" ]
then
    if [ "$pmcd_pid" = "$new_pmcd_pid" ]
    then
	echo "Found same pmcd"
    else
	echo "Error: pmcd restarted? PID changed from $pmcd_pid to $new_pmcd_pid"
	pmcd_pid=$new_pmcd_pid
    fi
else
    echo "Error: pmcd missing?"
    exit
fi

# verify primary pmlogger still running
#
echo "Expect same primary pmlogger to be running ..."
newpid=`_find_primary_pmlogger`
if [ -n "$newpid" ]
then
    if [ "$pid" = "$newpid" ]
    then
	echo "Found same primary pmlogger"
    else
	echo "Found new primary pmlogger"
	echo "Error: PID changed from $pid to $newpid"
	pid=$newpid
    fi
else
    echo "Error: primary pmlogger missing?"
fi
echo

# install a PMDA, should restart pmcd ...
# historically this would have caused the primary pmlogger to
# be restarted, but these days pmlogger will reconnect to the new
# pmcd here ...
#
$sudo sh -c "PCPQA_RESTART_PMCD=true ./Install </dev/null" \
| _filter_test_perl_install

# verify pmcd restarted
#
new_pmcd_pid=`_get_pids_by_name pmcd`
if [ -n "$new_pmcd_pid" ]
then
    if [ "$pmcd_pid" = "$new_pmcd_pid" ]
    then
	echo "Error: found the same pmcd"
    else
	echo "Found a new pmcd"
    fi
else
    echo "Error: new pmcd missing?"
    exit
fi

# verify primary pmlogger still running
#
echo "Expect same primary pmlogger to be running ..."
newpid=`_find_primary_pmlogger`
if [ -n "$newpid" ]
then
    if [ "$pid" = "$newpid" ]
    then
	echo "Found same primary pmlogger"
    else
	echo "Found new primary pmlogger"
	echo "Error: PID changed from $pid to $newpid"
	pid=$newpid
    fi
else
    echo "Error: primary pmlogger missing?"
fi
echo

# success, all done
status=0
exit
