#! /bin/sh
# PCP QA Test No. 540
# Check Linux hinv.map.lvname metric
#
# Copyright (c) 2013 Red Hat.
#

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

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

[ $PCP_PLATFORM = linux ] || _notrun "Depends on Linux logical volume manager"
[ $PCP_VER -ge 3801 ] || _notrun "Requires more recent Linux kernel PMDA"
[ -d /dev/mapper ] || _notrun "Requires kernel support for Device Mapper"

status=1	# failure is the default!
trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15

# real QA test starts here
echo >$seq.full

# Get the device mapper names from /dev/mapper
# ls -l ----->
# total 0
# crw------- 1 root root 10, 236 Sep  9 09:47 control
# or
# total 0
# crw-------. 1 root root 10, 236 Sep  3 16:32 control
# lrwxrwxrwx. 1 root root       7 Sep  3 16:32 fedora-root -> ../dm-1
# lrwxrwxrwx. 1 root root       7 Sep  3 16:32 fedora-swap -> ../dm-0
#
# Need to dodge this one
# total 0
# lrwxrwxrwx 1 root root 16 2014-08-23 16:24 control -> ../device-mapper
#
echo "== /dev/mapper" >>$seq.full
ls -l /dev/mapper | tee -a $seq.full >$tmp.tmp
if grep '^total [0-9]' $tmp.tmp >/dev/null 2>&1
then
    sed <$tmp.tmp -n -e '/->/{
s/\([^ ][^ ]*\) -> \([^ ][^ ]*\)/|\2 \1/
s/.*|//
s/^\.\.\///
/device-mapper control/d
p
}' \
    | LC_COLLATE=POSIX sort >$tmp.dev.mapper
else
    touch $tmp.dev.mapper
fi
echo "--- map ---" >>$seq.full
cat $tmp.dev.mapper >>$seq.full

# Get the device mapper names from pmdalinux
#
echo "== pmdalinux" >>$seq.full
pminfo -f hinv.map.lvname | tee -a $seq.full \
    | $PCP_AWK_PROG '/inst/ { print $4, $6 }' \
    | tr -d '"]' \
    | LC_COLLATE=POSIX sort \
    >$tmp.pcp.indom
echo "--- map ---" >>$seq.full
cat $tmp.pcp.indom >>$seq.full

if diff $tmp.pcp.indom $tmp.dev.mapper >$tmp.indom.diff
then
    echo "Device mapper instance matches expected values"
    status=0
else
    echo "Instance domain for hinv.map.lvname incorrect:"
    echo "diff output (PCP first, then ifconfig):"
    cat $tmp.indom.diff
fi

exit
