#!/bin/sh
# PCP QA Test No. 987
# Test XFS PMDA works in DSO mode for many PDU types.
#
# Copyright (c) 2014 Red Hat.
#

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

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

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

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

_filter()
{
    $here/src/sortinst \
    | sed -e '/inst /s/\[[01] /[0-or-1 /'
}

# real QA test starts here
root=$tmp.root
XFS_STATSPATH=$root; export XFS_STATSPATH
pmda=$PCP_PMDAS_DIR/xfs/pmda_xfs.so,xfs_init

# make order of metric names deterministic
#
pminfo xfs | LC_COLLATE=POSIX sort >$tmp.names

iter=1
for tgz in $here/linux/xfs-*.tgz
do
    [ $tgz = "linux/xfs-*" ] && continue

    base=`basename $tgz`
    echo "== $base - proc/fs/xfs/stat ==" >>$seq_full

    $sudo rm -fr $root
    mkdir $root || _fail "root in use when processing $tgz"
    cd $root
    $sudo tar xzf $tgz
    # if the tarball has restrictive mode for . (aka $root) we're
    # screwed, so apply the big hammer
    #
    $sudo chmod 755 .
    # now there are multiple cases of badness here ...
    # 1. the files/dirs get unpacked with permissions that prevent
    #    a non-root user from reading proc/fs/xfs/stat
    # 2. the stat file is null-byte padded to a 4Kbyte boundary
    #    (this may be a red herring, 1. may be the real problem,
    #    but fix it to be sure, to be sure
    #
    find proc -type d -exec $sudo chmod 755 {} \;
    find proc -type f -exec $sudo chmod 644 {} \;
    if [ ! -f proc/fs/xfs/stat ]
    then
	echo "Arrgh! no proc/fs/xfs/stat"
	ls -lR `pwd`
	tar ztvf $tgz
	_exit 1
    fi
    tr -d '\0' <proc/fs/xfs/stat >$tmp.stat
    if cmp -s proc/fs/xfs/stat $tmp.stat
    then
	echo "No null byte padding, OK" >>$seq_full
    else
	echo "Null byte padding ..." >>$seq_full
	ls -l $tmp.stat proc/fs/xfs/stat >>$seq_full
	if ! $sudo cp $tmp.stat proc/fs/xfs/stat
	then
	    echo "Arrgh! cannot fix proc/fs/xfs/stat"
	    _exit 1
	fi
    fi
    cat proc/fs/xfs/stat >>$seq_full

    echo "== Checking metric descriptors and values - $base"
    _triage_wait_point $seq.wait.$iter "+ XFS_STATSPATH=$root pminfo -L -K clear -K add,11,$pmda ... \`cat $tmp.names\`"
    pminfo -L -K clear -K add,11,$pmda -dfmtT `cat $tmp.names` | _filter
    echo && echo "== done" && echo
    cd $here
    iter=`expr $iter + 1`
done

# success, all done
status=0
exit
