#
# Test case for configuring gfarm
#
load "gfarm_v2::check_gfsd_registered"
load "gfarm_v2::check_gfmd_slave"
load "gfarm_v2::check_journal_seqnum_max"
load "gfarm_v2::generate_testdata"
load "gfarm_v2::check_file_location"
load "gfarm_v2::check_replica_num"

#
# Test parameters
#
# GFARM_HOME:	A directory path of Gfarm which this test will be executed
# RETRY_COUNT:	Retry number to check master/slave
# FILE_SIZE:	File size of each file to be read
#
setup_configuration()
{
	# default parameter
	GFARM_HOME=${GFARM_HOME-"/"}
	RETRY_COUNT=${RETRY_COUNT-"120"}
	FILE_SIZE=${FILE_SIZE-"1024"}

	# prepare: check whether required hosts are defined
	$GFSERVICE systest::gfarm_v2::check_hostids_defined - \
		"gfmd1 gfmd2 gfsd1 gfsd2 gfsd3 client1"
	[ $? -ne 0 ] && return $RESULT_UNSUPPORTED

	# prepare: configure master gfmd
	$GFSERVICE config-gfarm gfmd1
	[ $? -ne 0 ] && log_warn "$TESTNAME: config-gfarm on gfmd1 failed"

	# prepare: config-gfsd
	$GFSERVICE config-gfsd gfsd1
	[ $? -ne 0 ] && log_warn "$TESTNAME: config-gfsd on gfsd1 failed"

	# prepare: check gfsd is added
	check_gfsd_registered gfsd1 client1
	[ $? -ne 0 ] && log_warn "$TESTNAME: gfsd is not configured correctly"

	#
	# section: configure gfsd
	#

	return $RESULT_PASS
}

test_configuration()
{
	RESULT=$RESULT_PASS

	# step: config-gfsd
	$GFSERVICE config-gfsd gfsd2
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: config-gfsd on gfsd1 failed"
		set_result RESULT $RESULT_FAIL
	fi

	# step: check gfsd is added
	check_gfsd_registered gfsd2 client1
		if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: gfsd is not configured correctly"
		set_result RESULT $RESULT_FAIL
	fi

	#
	# section: configure gfmd slave
	#
	# step: config-gfarm-update -r
	$GFSERVICE systest::gfarm_v2::config_gfarm_update_r gfmd1
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: config-gfarm-update on gfmd1 failed"
		set_result RESULT $RESULT_FAIL
	fi

	# step: config-gfarm-slave
	$GFSERVICE config-gfarm-slave gfmd2 gfmd1
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: config-gfarm-slave on gfmd2 failed"
		set_result RESULT $RESULT_FAIL
	fi

	# step: restart-gfsd
	$GFSERVICE restart-gfsd gfsd1
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: restart-gfsd on gfsd1 failed"
		set_result RESULT $RESULT_FAIL
	fi
	$GFSERVICE restart-gfsd gfsd2
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: restart-gfsd on gfsd2 failed"
		set_result RESULT $RESULT_FAIL
	fi

	# step: check whether gfmd on gfmd2 is slave
	check_gfmd_slave gfmd2 client1 $RETRY_COUNT
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: gfmd2 in not slave"
		set_result RESULT $RESULT_FAIL
	fi

	# step: make directory to update metadata
	$GFSERVICE gfcmd client1 gfmkdir $GFARM_HOME/$TEST_EXEC_ID

	# step: check whether journal seqnum max is same
	check_journal_seqnum_max gfmd1 gfmd2
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: journal seqnum max is not same"
		set_result RESULT $RESULT_FAIL
	fi

	#
	# section: add another gfsd
	#
	# step: config-gfsd
	$GFSERVICE config-gfsd gfsd3
	[ $? -ne 0 ] && log_warn "$TESTNAME: config-gfsd on gfsd3 failed"

	# step: check gfsd is added
	check_gfsd_registered gfsd3 client1
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: gfsd3 is not configured correctly"
		set_result RESULT $RESULT_FAIL
	fi

	# step: set directory to enable file replication
	$GFSERVICE gfcmd client1 gfncopy -s 2 $GFARM_HOME/$TEST_EXEC_ID

	# step: create file
	TMP_FILE=gfservice.$$
	trap "rm -f $TMP_FILE; exit 1" 1 2 3 15
	generate_testdata $FILE_SIZE > $TMP_FILE

	GFARM_FILE=$GFARM_HOME/$TEST_EXEC_ID/test.txt
	cat $TMP_FILE | $GFSERVICE systest::gfarm_v2::read_and_register client1 $GFARM_FILE \
		gfmd1 gfsd3
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: failed to register file"
		set_result RESULT $RESULT_FAIL
	fi

	# step: check file with gfls
	$GFSERVICE gfcmd client1 gfls $GFARM_FILE
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: gfls failed"
		set_result RESULT $RESULT_FAIL
	fi

	# step: check whether file is created on expected node
	check_file_location $GFARM_FILE gfsd3 client1
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: file not found on specified host"
		set_result RESULT $RESULT_FAIL
	fi

	# step: check spool
	SPOOL_PATH=`$GFSERVICE gfcmd client1 gfspoolpath $GFARM_FILE`
	cat $TMP_FILE | $GFSERVICE systest::gfarm_v2::compare_spool_file gfsd3 $SPOOL_PATH
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: file in spool directory is not same"
		set_result RESULT $RESULT_FAIL
	fi

	# step: check file num
	check_replica_num client1 $GFARM_FILE 2 $RETRY_COUNT
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: number of replica files did not match"
		set_result RESULT $RESULT_FAIL
	fi

	#
	# section: remove gfsd
	#
	# step: gfhost -d
	$GFSERVICE unconfig-gfsd gfsd3
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: failed to delete host information"
		set_result RESULT $RESULT_FAIL
	fi

	# step: check registered
	check_gfsd_registered gfsd3 client1
	if [ $? -ne 1 ]; then
		log_warn "$TESTNAME: gfsd is not deleted correctly"
		set_result RESULT $RESULT_FAIL
	fi

	# step: check file num
	check_replica_num client1 $GFARM_FILE 2 $RETRY_COUNT
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: number of replica files did not match"
		set_result RESULT $RESULT_XFAIL
	fi

	# step: create file
	GFARM_FILE2=$GFARM_HOME/$TEST_EXEC_ID/test2.txt
	cat $TMP_FILE | $GFSERVICE systest::gfarm_v2::read_and_register client1 \
		$GFARM_FILE2 gfmd1
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: failed to register file"
		set_result RESULT $RESULT_FAIL
	fi

	# step: check file with gfls
	$GFSERVICE gfcmd client1 gfls $GFARM_FILE2
	if [ $? -ne 0 ]; then
		log_warn "$TESTNAME: gfls failed"
		set_result RESULT $RESULT_FAIL
	fi

	# step: check whether file is not created on deleted node
	check_file_location $GFARM_FILE gfsd3 client1
	if [ $? -eq 0 ]; then
		log_warn "$TESTNAME: found a file on deleted host"
		set_result RESULT $RESULT_FAIL
	fi

	# step: check spool
	HOSTS=`$GFSERVICE gfcmd client1 gfwhere $GFARM_FILE2`
	SPOOL_PATH=`$GFSERVICE gfcmd client1 gfspoolpath $GFARM_FILE2`
	for HOST in $HOSTS; do
		ID=`$GFSERVICE systest::gfarm_v2::get_gfsd_hostid_by_name $HOST`
		cat $TMP_FILE | $GFSERVICE systest::gfarm_v2::compare_spool_file $ID \
			$SPOOL_PATH
		if [ $? -ne 0 ]; then
			log_warn "$TESTNAME: file in spool directory is not same"
			set_result RESULT $RESULT_FAIL
		fi
	done

	rm -f $TMP_FILE

	return $RESULT
}

teardown_configuration()
{
	# cleanup: remove file
	$GFSERVICE gfcmd client1 gfrm $GFARM_FILE $GFARM_FILE2

	# cleanup: remove directory
	$GFSERVICE gfcmd client1 gfrmdir $GFARM_HOME/$TEST_EXEC_ID

	# cleanup: unconfigure gfsd
	$GFSERVICE unconfig-gfsd gfsd1

	# cleanup: unconfigure gfsd
	$GFSERVICE unconfig-gfsd gfsd2

	# cleanup: unconfigure slave gfmd
	$GFSERVICE unconfig-gfarm-slave gfmd2 gfmd1

	# cleanup: unconfigure master gfmd
	$GFSERVICE unconfig-gfarm gfmd1

	return $RESULT_PASS
}
