#!/bin/sh
# PCP QA Test No. 1061
# Testing pmdalio
#
# Copyright (c) 2017 Red Hat Inc.  All Rights Reserved.
#

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

. ./common.python

pmda_path="$PCP_PMDAS_DIR/lio"
pmda_script="$pmda_path/pmdalio.python"

[ -d $pmda_path ] || _notrun "LIO PMDA is not installed"

$python -c "from pcp import pmda" >>$seq_full 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmda module not installed"

$python -c "import rtslib_fb" >>$seq_full 2>&1
[ $? -eq 0 ] || _notrun "python rtslib_fb module not installed"

# "utils" submodule may be hiding in different modules ...
# mimic the logic from the lio PMDA here
#
if ! $python -c "import rtslib_fb.utils" >>$seq_full 2>&1
then
    $python -c "import rtslib.utils" >>$seq_full 2>&1
    [ $? -eq 0 ] || _notrun "neither python module rtslib_fb.utils nor rtslib.utils is installed"
    echo "Found rtslib.utils" >>$seq_full
else
    echo "Found rtslib_fb.utils" >>$seq_full
fi

which targetcli >>$seq_full 2>&1 || _notrun "targetcli not installed"
which tc >>$seq_full 2>&1 || _notrun "tc not installed"

$sudo modprobe target_core_mod >>$seq_full 2>&1
[ $? -eq 0 ] || _notrun "target_core_mod kernel module unavailable"

_clean_iscsi()
{
    if [ -d /sys/kernel/config/target/iscsi/iqn.2003-01.com.pcpio.test:iscsi-gateway ]
    then
	# leftover turds from one of our lio PMDA tests failing
	# ... cleanup
	#
	$sudo targetcli iscsi/ delete iqn.2003-01.com.pcpio.test:iscsi-gateway >>$seq_full 2>&1
    fi
    # Now force-remove the ramdisks behind the iscsi targets
    #
    rm -f $tmp.fail
    for i in 1 2 3
    do
	if ! $sudo targetcli backstores/ramdisk/ delete ramdisk$i >>$seq_full 2>&1
	then
	    touch $tmp.fail
	fi
    done
    if [ -f $tmp.fail ]
    then
	# targetcli failed ... either nothing to do, or a mismatch between
	# the kernel and the Python code behind targetcli
	# this big hammer solution comes from Claude
	#
	touch $tmp.ok
	for i in 1 2 3
	do
	    find /sys/kernel/config/target/iscsi -lname "*ramdisk$i" >$tmp.out 2>/dev/null
	    if [ -s $tmp.out ]
	    then
		cat $tmp.out
		echo "Arrgh ramdisk$i still configured, I give up"
		rm -f $tmp.ok
	    fi
	    # if no /sys/kernel/config/target/core/rd_mcp_* dirs exist
	    # there is nothing to do, so safe to throw stderr away
	    #
	    find /sys/kernel/config/target/core/rd_mcp_* -name "ramdisk$i" -type d 2>/dev/null \
	    | while read dir
	    do
		if $sudo rmdir "$dir"
		then
		    echo "ramdisk$i: forced removal" >>$seq_full
		else
		    echo "Arrgh cannot make ramdisk$i go away, I give up"
		    rm -f $tmp.ok
		fi
	    done
	done
	[ -f $tmp.ok ] || _exit 1
    fi
}

_clean_iscsi

status=1	# failure is the default!
trap "_cleanup_pmda lio; _clean_iscsi; exit \$status" 0 1 2 3 15

pmdalio_filter()
{
    sed \
	-e "s/Ran 5 tests in [0-9]\.[0-9][0-9]*[0-9]s/Ran 5 tests in 0.XXXs/" \
	-e "s/\.ISCSITests\..*) /.ISCSITests) /g" \
	-e "/^$/d" \
    # end
}

pmdalio_remove()
{
    cd $pmda_path
    echo
    echo "=== remove LIO agent ==="
    $sudo ./Remove >$tmp.out 2>&1
}

pmdalio_install()
{
    cd $pmda_path
    $sudo ./Remove >/dev/null 2>&1

    echo
    echo "=== LIO agent installation ===" | tee -a $seq_full
    $sudo ./Install </dev/null >$tmp.out 2>&1
    cat $tmp.out >>$seq_full
    cd $here
}

# real QA test starts here
_prepare_pmda lio

pmdalio_install
_triage_wait_point "lio PMDA should be installed now"
if _avail_metric lio.summary.total_iops
then
    $sudo $python lio/lio_test.py 2>&1 | LC_COLLATE=POSIX sort | pmdalio_filter
else
    echo "Arrgh, something went wrong ..."
    pminfo -fd lio
fi

pmdalio_remove

# success, all done
status=0
exit
