#! /bin/bash
# FSQA Test No. 151
#
# Create three DMAPI sessions,
# verify they have been created,
# then distroy first session
#
#-----------------------------------------------------------------------
# Copyright (c) 2006 Silicon Graphics, Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#-----------------------------------------------------------------------
#

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

here=`pwd`
tmp=/tmp/$$
status=1    # failure is the default!
trap "exit \$status" 0 1 2 3 15

_dm_create_session()
{
    eval `${DMAPI_QASUITE1_DIR}../simple/dm_create_session`
    echo "RET=$ret NEWSID=NUM"
}

_filter_sid_list()
{
    $SED_PROG -e "s/sids=\"[0-9| ]*\"/sids=NUMS/"
}

_check_sids()
{
	for i in $@; do
		CHECKED_SID=`${DMAPI_QASUITE1_DIR}../simple/dm_getall_sessions \
			| perl -ne "if (/^sids.*[\"|\s]($i)[\"|\s].*/) {print \$1} ;"`
		if [ -z "$CHECKED_SID" ] ; then
			echo "ERROR: SID(s) not created"
			${DMAPI_QASUITE1_DIR}../simple/dm_getall_sessions >>$seqres.full
			return
		fi
	done
}

# get standard environment, filters and checks
. ./common/rc
. ./common/filter
. ./common/dmapi

# real QA test starts here
_supported_fs xfs
_supported_os Linux IRIX

_require_scratch
_scratch_mkfs_xfs >/dev/null 2>&1
_dmapi_scratch_mount

echo "*** create 3 sessions"
_dm_create_session
sid1=$newsid
_dm_create_session
sid2=$newsid
_dm_create_session
sid3=$newsid

echo "*** check 3 sessions exist"
_check_sids $sid1 $sid2 $sid3

echo "*** destroy first session"
${DMAPI_QASUITE1_DIR}../simple/dm_destroy_session -s $sid1

#should fail
echo "*** check first session no longer exists"
_check_sids $sid1

#should still remain
echo "*** check last 2 session exist"
_check_sids $sid2 $sid3

echo "*** query"
${DMAPI_QASUITE1_DIR}../simple/dm_query_session -s $sid2

echo "*** remove remaining sessions"
${DMAPI_QASUITE1_DIR}../simple/dm_destroy_session -s $sid2
${DMAPI_QASUITE1_DIR}../simple/dm_destroy_session -s $sid3

echo "*** check second session no longer exists"
_check_sids $sid2

echo "*** check third session no longer exists"
_check_sids $sid3

status=0
exit 
