#
# Test case for check behavior when client network interface is down
# in writing files
#

#
# Test parameters
#
# GFARM_HOME:	A directory path of Gfarm which this test will be executed
# FILE_SIZE:	Size of a file to be created
# SLEEP_SEC:	Second to sleep before make network interface down
#
setup_client_ifdown_in_write()
{
	# define mount point
	MOUNT_POINT=mnt_$TEST_EXEC_ID

	# default parameter
	GFARM_HOME=${GFARM_HOME-"/"}
	FILE_SIZE=${FILE_SIZE-"100M"}
	SLEEP_SEC=${SLEEP_SEC-"3"}

	# miscellaneous parameters
	FILE_PATH=$MOUNT_POINT/$TEST_EXEC_ID

	# prepare: check whether required host is defined
	$GFSERVICE systest::gfarm_v2::check_hostids_defined - "gfmd1 client1"
	[ $? -ne 0 ] && return $RESULT_UNSUPPORTED

	# prepare: mount
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 mkdir $MOUNT_POINT
	$GFSERVICE mount client1 $MOUNT_POINT -o modules=subdir \
		-o subdir=$GFARM_HOME

	return $RESULT_PASS
}

test_client_ifdown_in_write()
{
	RESULT=$RESULT_PASS

	# step: make files in background
	$GFSERVICE systest::gfarm2fs::make_file client1 $FILE_PATH $FILE_SIZE &
	MAKE_PID=$!

	# wait a bit for a process starts making files
	sleep $SLEEP_SEC

	# step: make network interface down
	$GFSERVICE systest::gfarm2fs::ifdown client1
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: failed to execute ifdown on client1"
		return $RESULT_FATAL
	fi

	wait $MAKE_PID
	if [ $? -eq 0 ]; then
		log_warn "$TESTNAME: unexpectedly succeeded to make a file"
		set_result RESULT $RESULT_XPASS
	fi

	# step: make network interface up
	$GFSERVICE systest::gfarm2fs::ifup client1
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: failed to execute ifup on client1"
		return $RESULT_FATAL
	fi

	# step: make files
	$GFSERVICE systest::gfarm2fs::make_file client1 $FILE_PATH $FILE_SIZE
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: failed to write files"
		set_result RESULT $RESULT_FAIL
	fi

	return $RESULT
}

teardown_client_ifdown_in_write()
{
	# cleanup: make network interface up
	$GFSERVICE systest::gfarm2fs::ifup client1

	# cleanup: remove files
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 rm -f $FILE_PATH

	# cleanup: unmount
	$GFSERVICE umount client1 $MOUNT_POINT
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 rmdir $MOUNT_POINT

	return $RESULT_PASS
}
