#!/bin/sh
# PCP QA Test No. 1634
# Test pmdasockets
#
# Copyright (c) 2021 Red Hat.  All Rights Reserved.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

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

[ $PCP_PLATFORM = linux ] || _notrun "pmdasockets is Linux-specific"
[ -f $PCP_PMDAS_DIR/sockets/pmdasockets ] || _notrun "sockets pmda not installed"

# _notrun if valgrind is not installed
_check_valgrind

# Override the pmda's filter.conf
[ -f $PCP_PMDAS_DIR/sockets/filter.conf ] && \
_save_config $PCP_SYSCONF_DIR/sockets/filter.conf
echo "state connected" >$tmp.filter
$sudo cp $tmp.filter $PCP_SYSCONF_DIR/sockets/filter.conf

_cleanup()
{
    cd $here
    _restore_config $PCP_SYSCONF_DIR/sockets/filter.conf

    $sudo rm -rf $tmp $tmp.*
}

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

_filter()
{
    sed -e "s;$tmp;TMP;g" -e '/Unable to open help text file/d'
}

# real QA test starts here
[ ! -d "$tmp" ] && mkdir -p $tmp
qadomain=251 # FORQA
sed -e "/^root/i#undef SOCKETS\n#define SOCKETS $qadomain" <$PCP_PMDAS_DIR/sockets/root >$tmp/root
cp $PCP_PMDAS_DIR/sockets/pmns $tmp/pmns
pmns=$tmp/root
pmda=$PCP_PMDAS_DIR/sockets/pmda_sockets.$DSO_SUFFIX,sockets_init

for f in `ls -1 sockets/*.txt | LC_COLLATE=POSIX sort`; do
    echo === testing $f ===
    $sudo rm -f $PCP_VAR_DIR/config/pmda/$qadomain.0 # reset indom
    export PCPQA_PMDA_SOCKETS="$f"
    _run_valgrind pminfo -L -K clear -K add,$qadomain,$pmda -dfm -n $pmns network.persocket | _filter
done

# success, all done
exit
