#!/bin/sh
# PCP QA Test No. 1545
# Exercise python handling PMCD metric-names-change notification.
#
# 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"
$python -c "from lxml import etree" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python lxml etree module not installed"

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

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

_filter_xml()
{
    sed \
        -e 's,<host .*>,<host>,g' \
        -e 's,<timezone.*/,<timezone>UTC</,g' \
        -e 's,<timestamp.*>,<timestamp>,g' \
        -e 's,[0-9]\{4\,7\},xxxxxxx,g' \
    | pmpython src/sort_xml.python \
    | sed -e 's/^  *//'
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

# real QA test starts here
_prepare_pmda_mmv

echo "--- before mmv_genstats started " >>$seq_full
pminfo -f mmv.test >>$seq_full 2>&1

# test names-change=update with pcp2xml
echo testing names-change=update with pcp2xml
pcp2xml --samples=4 --interval=4 --ignore-unknown --raw --names-change=update mmv.control.files mmv.test | _filter_xml &

# timing here is a bit tricky ... need long enough for pcp2xml to get
# started and at least one sample done, but we need to run mmv_genstats
# before pcp2xml gets to the second sample
#
pmsleep 2.5

# and the mmv stats "interval" is 5 secs (2nd arg to mmv_genstats)
#
src/mmv_genstats test 5 &
pmsleep 1
pmstore mmv.control.reload 1 >>$seq_full
echo "--- after mmv_genstats started and mmv reload" >>$seq_full
pminfo -f mmv.test >>$seq_full 2>&1

wait

echo "--- after pcp2xml is done" >>$seq_full
pminfo -f mmv.test >>$seq_full 2>&1

# success, all done
status=0
exit
