#!/bin/sh
#
#File: STONITHDBasicSanityCheck.in
#Description: a basic sanity test script for STONITH deamon.
#
# Author: Sun Jiang Dong <sunjd@cn.ibm.com>
# Copyright (c) 2005 International Business Machines
#
# 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; either
# version 2.1 of the License, or (at your option) any later version.
# 
# This software is distributed in the hope that it will 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 library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

HBLIB=/usr/local/lib/heartbeat
STONITHD=$HBLIB/stonithd
LRMD=$HBLIB/lrmd
LRMADMIN=$HBLIB/lrmadmin
APITEST=/usr/local/lib/heartbeat/stonithdtest/apitest
ERR_COUNT=0
MYSELF=`uname -n`

run_test()
{
	TIMEOUT=4000
	QUERY=0
	RESET=1
	POWEROFF=3

	# Add stonith resource.
	$LRMADMIN -A s1 stonith null NULL hostlist=$MYSELF
	[ $? -eq 0 ] || let ERR_COUNT++ 

	# Start stonith resource, equal to initializing the stonith device.
	$LRMADMIN -E s1 start 0 0 0
	[ $? -eq 0 ] || let ERR_COUNT++ 

	# Test stonith operation: query
	$APITEST $QUERY $MYSELF $TIMEOUT 0
	[ $? -eq 0 ] || let ERR_COUNT++ 

	# Test stonith operation: reset
	$APITEST $RESET $MYSELF $TIMEOUT 0
	[ $? -eq 0 ] || let ERR_COUNT++ 

	# Test stonithd's error handling - to stonith an unaccessible host
	$APITEST $POWEROFF notexist $TIMEOUT 2
	[ $? -eq 0 ] || let ERR_COUNT++ 
}


check_files()
{
	if [ ! -f $LRMD ]; then
		echo $LRMD does not exist
		exit 1
	fi

	if [ ! -f $LRMADMIN ]; then
		echo $LRMADMIN does not exist
		exit 1
	fi

	if [ ! -f $STONITHD ]; then
		echo $STONITHD does not exist
		exit 1
	fi

	if [ ! -f $APITEST ]; then
		echo $APITEST for stonithd does not exist
		exit 1
	fi
}
	
	
start_stonithd()
{
	#kill the previous stonithd instance
	$STONITHD -k
	sleep 1
	#start the new stonithd instance 
	$STONITHD -t &
	sleep 1
	$STONITHD -s
	if [ $? -ne 0 ]; then
		echo Can not start stonithd
		exit 1
	fi
}

start_lrmd()
{
	#kill the previous instance lrm
	$LRMD -k
	#start the new instance of lrm
	$LRMD -r &
	sleep 1
	#check is the new instance running
	$LRMD -s
	if [ $? -ne 0 ]; then
		echo Can not start lrmd
		exit 1
	fi
}

clean_work()
{
	$LRMD -k
	$STONITHD -k
}

check_files
start_stonithd
start_lrmd
run_test
clean_work

if [ $ERR_COUNT -eq 0 ]; then
	echo "All tests for stonithd are ok."
else
	echo "There are $ERR_COUNT ERRORs when testing stonithd."
	exit $ERR_COUNT
fi

exit 0
