#!/bin/sh
# PCP QA Test No. 1546
# Exercise Python PMFG clearing.
#
# Copyright (c) 2018 Red Hat.
#

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

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
$python -c "from collections import OrderedDict" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python collections OrderedDict module not installed"

which pcp2xml >/dev/null 2>&1 || _notrun "No pcp2xml binary installed"

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
    $sudo rm -f $PCP_TMP_DIR/mmv/test
}

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

# real QA test starts here
$sudo rm -f $PCP_TMP_DIR/mmv/test
pminfo -f mmv > /dev/null 2>&1
pcp2xml -5 -t 1 -s 9 -x --names-change=update mmv > $seq.full 2>&1 &
pid=$!
pmsleep 1.5
for i in 1 2 3 4; do
    $sudo -u $PCP_USER $here/src/mmv2_genstats test 1
    $sudo rm -f $PCP_TMP_DIR/mmv/test
    pmsleep 1
    kill -0 $pid > /dev/null 2>&1 || break
done
wait

# Look for the closing tag pcp2xml will write on normal exit
grep '</pcp>' $seq.full > /dev/null 2>&1
if [ $? -eq 0 ]; then
  echo Test OK, pcp2xml finished ok
else
  echo Test FAILED, pcp2xml crashed
fi

$sudo rm -f $PCP_TMP_DIR/mmv/test

# success, all done
status=0
exit
