#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2006 Silicon Graphics, Inc.  All Rights Reserved.
#
# FSQA Test No. 151
#
# Create three DMAPI sessions,
# verify they have been created,
# then distroy first session
#
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

_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 
