#!/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!
trap "_cleanup; exit \$status" 0 1 2 3 15

if [ $PCP_PLATFORM = freebsd ]
then
    # workaround datetime.strftime() in Python that is busted when
    # the timezone|timeinfo is, er, Australia/Someplace, for at least
    # some parts of the year
    #
    version=`uname -rs | sed -e 's/FreeBSD //'`
    case "$version"
    in
	14.[0-3]-*)
	    export TZ=UTC
	    echo "Forcing TZ=UTC to dodge FreeBSD bug" >>$seq_full
	    ;;
    esac
fi

# real QA test starts here

# need mmv2_genstats someplace where user $PCP_USER can execute it
#
$sudo cp $here/src/mmv2_genstats $tmp.mmv2_genstats

$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 $tmp.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
