#!/bin/sh
# PCP QA Test No. 990
# MMV format error injection and verification of pmdammv response.
#
# Copyright (c) 2016 Red Hat.
#

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

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

_check_valgrind

status=1
username=`id -u -n`
MMV_STATS_DIR=${PCP_TMP_DIR}/mmv
MMVDUMP=$PCP_PMDAS_DIR/mmv/mmvdump
pmda=${PCP_PMDAS_DIR}/mmv/pmda_mmv,mmv_init

_cleanup()
{
    cd $here
    [ -d ${MMV_STATS_DIR}.$seq ] && _restore_config ${MMV_STATS_DIR}
    rm -rf $tmp $tmp.*
}

$sudo rm -rf $tmp $tmp.* $seq.full
trap "_cleanup; exit \$status" 0 1 2 3 15

# move the MMV directory to restore contents later.
[ -d ${MMV_STATS_DIR} ] && _save_config ${MMV_STATS_DIR}

$sudo rm -rf ${MMV_STATS_DIR}
$sudo mkdir -m 755 ${MMV_STATS_DIR}
$sudo chown $username ${MMV_STATS_DIR}

_filter()
{
    sed \
	-e "s,$MMV_STATS_DIR,MMV_STATS_DIR,g" \
	-e "s,$PCP_PMDAS_DIR,PCP_PMDAS_DIR,g" \
    #end
}

_dump()
{
    section=$1

    echo
    echo "== generating bad $section data files =="
    $here/src/badmmv --$section

    files=`ls -1 ${MMV_STATS_DIR}/${section}-* | LC_COLLATE=POSIX sort`
    for file in $files
    do
	echo -- dump `basename $file`
	$PCP_PMDAS_DIR/mmv/mmvdump $file | _filter
	unlink $file
	echo
    done
}

_verify()
{
    section=$1

    echo
    echo "== generating bad $section data files =="
    $here/src/badmmv --$section

    # run valgrind once for all files in section (its slow to start)
    echo -- verify $section data files | tee -a $seq.full
    _run_valgrind pminfo -L -Kclear -Kadd,70,$pmda -v mmv \
		| _filter

    # this should always show zero (all badmmv generated files are invalid)
    pminfo -L -Kclear -Kadd,70,$pmda -f mmv.control.files

    # cleanup 
    rm -f ${MMV_STATS_DIR}/${section}-*
}

_dump header
_dump contents
_dump indoms
_dump metrics

_verify header
_verify contents
_verify indoms
_verify metrics

# success, all done
status=0
exit
