#!/bin/sh
# PCP QA Test No. 1226
# pmiectl - basic functionality
#
# see qa/1204 for the pmlogctl variant of this test
#
# Copyright (c) 2020 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

if pmiectl -c default status 2>/dev/null | grep ' default ' >/dev/null
then
    _notrun "at least one pmie already defined for \"default\" class"
fi

_cleanup()
{
    echo "_cleanup: ..." >>$seq.full
    cd $here
    $sudo pmiectl -f -c default destroy localhost >>$seq.full 2>&1
    $sudo pmiectl -a -f -c pmfind destroy localhost >>$seq.full 2>&1
    pmiectl status localhost >$tmp.tmp 2>&1
    cat $tmp.tmp >>$seq.full
    if grep 'localhost not defined in any pmie control file' $tmp.tmp >/dev/null
    then
	# all good
	:
    else
	# really make sure any errant pmie we started will be stopped
	#
	export PCPQA_KILL_SIGNAL=KILL
	$sudo pmiectl -f -c default destroy localhost >>$seq.full 2>&1
	$sudo pmiectl -a -f -c pmfind destroy localhost >>$seq.full 2>&1
    fi
    $sudo rm -rf $tmp $tmp.*
    $sudo rm -rf $PCP_LOG_DIR/pmie/localhost
    $sudo rm -rf $PCP_LOG_DIR/pmie/pmfind-localhost
    $sudo rm -rf $PCP_LOG_DIR/pmie/$seq-foo
    if [ "$pmie_state" = off ]
    then
	_change_config pmie off
	_service pmie stop >>$here/$seq.full 2>&1
    fi
}

status=1	# failure is the default!
$sudo rm -rf $tmp $tmp.* $seq.full
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter()
{
    tee -a $seq.full \
    | sed \
	-e '/^# created by pmiectl/s/ on .*/ on DATE/' \
	-e "s;$tmp\.;TMP.;g" \
	-e "s;$PCP_BINADM_DIR/;PCP_BINADM_DIR/;g" \
	-e "s;$PCP_LOG_DIR/pmie/;PCP_LOG_DIR/pmie/;g" \
	-e "s;$PCP_TMP_DIR/;PCP_TMP_DIR/;g" \
	-e "s;$PCP_TMPFILE_DIR/pmiectl\.[^/]*;PCP_TMPFILE_DIR/pmiectl.XXXXX;g" \
	-e "s;$PCP_ETC_DIR/;PCP_ETC_DIR/;g" \
    # end
}

# Build filter for any existing non-qa and non-primary pmie instances.
# The "pmcd Host" and "Class" fields from the pmiectl status output
# should suffice to uniquely identify each.
#
pmiectl status 2>&1 \
| $PCP_AWK_PROG >$tmp.awk '
NR == 1	{ next }
NF >= 5	{ if ($4 == "primary") next
	  print "$1 == \"" $1 "\" && $4 == \"" $4 "\" { next }"
	}
END	{ print "{ print }" }'

# Note status command output order is non-deterministic, hence the sort
# at the end
#
_filter_status()
{
    tee -a $seq.full \
    | $PCP_AWK_PROG -f $tmp.awk \
    | sed \
	-e "/^`hostname` .* primary /d" \
	-e 's/[ 	][ 	]*/ /g' \
    | $PCP_AWK_PROG '
$2 ~ /^[0-9][0-9]*$/	{ $2 = "<nrule>" }
$3 ~ /^[0-9][0-9]*$/	{ $3 = "<neval>" }
$5 ~ /^[0-9][0-9]*$/	{ $5 = "<pid>" }
			{ print }' >$tmp.tmp
    head -1 $tmp.tmp
    sed -e '1d' $tmp.tmp | LC_COLLATE=POSIX sort
}

# Need primary pmie running so pmiectl status output is deterministic
#
pmie_state=`_get_config pmie`
[ "$pmie_state" = off ] && _change_config pmie on
_service pmie start >>$here/$seq.full 2>&1

# real QA test starts here

echo "== usage"
pmiectl --usage 2>&1 | _filter

echo
echo '== -N -V create' | tee -a $seq.full
pmiectl -N -V create localhost 2>&1 | _filter

echo | tee -a $seq.full
echo '== create' | tee -a $seq.full
$sudo pmiectl create localhost 2>&1 | _filter
echo '== status' | tee -a $seq.full
pmiectl status 2>&1 | _filter_status
echo '== status localhost' | tee -a $seq.full
pmiectl status localhost 2>&1 | _filter_status

echo | tee -a $seq.full
echo '== stop' | tee -a $seq.full
$sudo pmiectl stop localhost 2>&1 | _filter
pmsleep 1sec
echo '== status' | tee -a $seq.full
pmiectl status 2>&1 | _filter_status
echo '== status localhost' | tee -a $seq.full
pmiectl status localhost 2>&1 | _filter_status

echo | tee -a $seq.full
echo '== start' | tee -a $seq.full
$sudo pmiectl start localhost 2>&1 | _filter
echo '== status' | tee -a $seq.full
pmiectl status 2>&1 | _filter_status
echo '== status localhost' | tee -a $seq.full
pmiectl status localhost 2>&1 | _filter_status

echo | tee -a $seq.full
echo '== restart' | tee -a $seq.full
$sudo pmiectl restart localhost 2>&1 | _filter
echo '== status' | tee -a $seq.full
pmiectl status 2>&1 | _filter_status
echo '== status localhost' | tee -a $seq.full
pmiectl status localhost 2>&1 | _filter_status

echo | tee -a $seq.full
echo '== destroy' | tee -a $seq.full
$sudo pmiectl destroy localhost 2>&1 | _filter
echo '== status' | tee -a $seq.full
pmiectl status 2>&1 | _filter_status
echo '== status localhost' | tee -a $seq.full
pmiectl status localhost 2>&1 | _filter_status

echo | tee -a $seq.full
echo '== cond-create' | tee -a $seq.full
$sudo pmiectl cond-create localhost 2>&1 | _filter
echo '== status' | tee -a $seq.full
pmiectl status 2>&1 | _filter_status
$sudo pmiectl -c pmfind destroy localhost 2>&1 | _filter

echo | tee -a $seq.full
echo "== cond-create -i $seq-foo" | tee -a $seq.full
$sudo pmiectl -i $seq-foo cond-create localhost 2>&1 | _filter
echo '== status' | tee -a $seq.full
pmiectl -V status 2>&1 | _filter_status

# success, all done
status=0
exit
