#!/bin/sh
# PCP QA Test No. 1531
# Test Linux ZFS PMDA pool metrics with valgrind
#

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

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

_check_valgrind
[ $PCP_PLATFORM = linux ] || _notrun "Linux ZFS PMDA is Linux-specific (uses procfs)"

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

_filter()
{
    $PCP_AWK_PROG '
BEGIN			{ outfile = "'$tmp.0'" }
/^=== std err ===$/	{ outfile = "'$tmp.1'" }
			{ print >outfile }'
    if [ ! -f $tmp.0 ]
    then
	echo "Botch: no part 0 output?"
	touch $tmp.0
    fi
    if [ ! -f $tmp.1 ]
    then
	echo "Botch: no part 1 output?"
	touch $tmp.1
    fi
    ( $here/src/sortinst <$tmp.0; cat $tmp.1 ) \
    | sed \
        -e '/inst /s/\[[01] /[0-or-1 /' \
	-e "s,$PCP_PMDAS_DIR,PCP_PMDAS_DIR,g" \
	-e "s,$PCP_VAR_DIR,PCP_VAR_DIR,g" \
    #end
}

root=$tmp.root
pmns=$PCP_VAR_DIR/pmns/root_zfs
pmda=$PCP_PMDAS_DIR/zfs/pmda_zfs.so,zfs_init
tgz=$here/linux/zfs-stats.v0.tgz
poolname=pmdazfs_test
$sudo rm -f $PCP_VAR_DIR/config/pmda/153.*
$sudo rm -fr $root
mkdir $root || _fail "root in use when processing $tgz"
cd $root
$sudo tar xzf $tgz
export ZFS_PATH=$root/zfs
echo "== Valgrind checking metric descriptors and values with 1 pool"
_run_valgrind pminfo -L -K clear -K add,153,$pmda -dfmtT -n $pmns zfs.pool | _filter
echo "== Valgrind checking metric descriptors and values with 2 pools"
$sudo cp -R $root/zfs/$poolname $root/zfs/${poolname}1
_run_valgrind pminfo -L -K clear -K add,153,$pmda -dfmtT -n $pmns zfs.pool | _filter
echo "== Valgrind checking metric descriptors and values with 1 pool"
$sudo rm -rf $root/zfs/${poolname}1
_run_valgrind pminfo -L -K clear -K add,153,$pmda -dfmtT -n $pmns zfs.pool | _filter
echo && echo "== done" && echo
cd $here

# success, all done
status=0
exit
