#
# Test case for operation when client network interface is down
#

#
# Test parameters
#
# GFARM_HOME:	A directory path of Gfarm which this test will be executed
#
setup_operation_in_client_ifdown()
{
	# define mount point
	MOUNT_POINT=mnt_$TEST_EXEC_ID

	# default parameter
	GFARM_HOME=${GFARM_HOME-"/"}

	# miscellaneous parameters
	GFARM_FILE1=$MOUNT_POINT/${TEST_EXEC_ID}_1
	GFARM_FILE2=$MOUNT_POINT/${TEST_EXEC_ID}_2
	GFARM_FILE3=$MOUNT_POINT/${TEST_EXEC_ID}_3

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

	# prepare: create mount point
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 mkdir $MOUNT_POINT

	# prepare: mount
	$GFSERVICE mount client1 $MOUNT_POINT

	# prepare: write file
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 \
		"echo ${TEST_EXEC_ID}_1 > $GFARM_FILE1"

	# prepare: 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

	return $RESULT_PASS
}

test_operation_in_client_ifdown()
{
	RESULT=$RESULT_PASS

	# step: write file
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 \
		"echo ${TEST_EXEC_ID}_2 > $GFARM_FILE2"
	if [ $? -eq 0 ]; then
		log_warn "$TESTNAME: unexpectedly suceeded to write a file"
		set_result RESULT $RESULT_XPASS
	fi

	# step: read file
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 "cat $GFARM_FILE1"
	if [ $? -eq 0 ]; then
		log_warn "$TESTNAME: unexpectedly suceeded to read 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: write file
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 \
		"echo ${TEST_EXEC_ID}_3 > $GFARM_FILE3"
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: failed to create a file"
		set_result RESULT $RESULT_FAIL
	fi

	# step: read file
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 "cat $GFARM_FILE1"
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: failed to read a file"
		set_result RESULT $RESULT_FAIL
	fi

	return $RESULT
}

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

	# cleanup: remove files
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 rm -f $GFARM_FILE1 $GFARM_FILE2 \
		$GFARM_FILE3

	# cleanup: unmount
	$GFSERVICE umount client1 $MOUNT_POINT

	# cleanup: remove mount point
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 rmdir $MOUNT_POINT

	return $RESULT_PASS
}
