#!/bin/bash ssh -o StrictHostKeyChecking=no \ -o ConnectTimeout=5 \ -o PreferredAuthentications=hostbased,publickey \ $machine exit &> /dev/null # This script will clean all the environment for testing. # Basically, this script will release all the ports needed for # test purposes. MGSRCDIR= INDEX=1 TESTPROPERTIESFILE="test/resources/test.properties" TEST_BASE=2 TEST_ARG=$[TEST_BASE + 1]; TEST_OGSOURCE=$[TEST_BASE + 2]; TEST_TESTPROPERTIES=$[TEST_BASE + 3]; TEST_PORTALREADYINUSE=$[TEST_BASE + 4]; TEST_IFMACHINEISALIVE=$[TEST_BASE + 5]; TEST_MACHINESSH=$[TEST_BASE + 6]; TEST_PORTAVAILABILITY=$[TEST_BASE + 7]; TEST_LOCALTEMPFILE=$[TEST_BASE + 8]; TEST_PORTISFREE=$[TEST_BASE + 9]; TESTCHECK=`basename $0` BOOTUP=color RES_COL=65 MOVE_TO_COL="echo -en \\033[${RES_COL}G" SETCOLOR_SUCCESS="echo -en \\033[1;32m" SETCOLOR_FAILURE="echo -en \\033[1;31m" SETCOLOR_WARNING="echo -en \\033[1;33m" SETCOLOR_NORMAL="echo -en \\033[0;39m" gprintf() { if [ -x /bin/gettext -a -n "$1" ]; then if [ -n "$GP_LANG" ]; then local TEXT=`LANGUAGE=$GP_LANG gettext -e --domain=$TEXTDOMAIN "$1"` else local TEXT=`gettext -e --domain=$TEXTDOMAIN "$1"` fi else local TEXT=$1 fi [ "${1#*\\n}" ] || TEXT="$TEXT\n" shift printf "$TEXT" "$@" } echo_success() { [ "$BOOTUP" = "color" ] && $MOVE_TO_COL echo -n "[" [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS gprintf " OK " [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL echo -n "]" echo -ne "\r" echo return 0 } echo_failure() { FAILURE=1 [ "$BOOTUP" = "color" ] && $MOVE_TO_COL echo -n "[" [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE gprintf "FAILED" [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL echo -n "]" echo -ne "\r" echo return 1 } is_executable() { BIN=$MGSRCDIR/bin MYTASK=$MGSRCDIR/test/ui/mytask.sh OLDPWD=`pwd`; cd $BIN for i in `ls $BIN`; do if [ ! -x $i ]; then chmod u+x $i fi done if [ ! -x $MYTASK ]; then chmod u+x $MYTASK fi cd $OLDPWD } usage() { echo "Usage: $TESTCHECK" echo "Check the test environment using $TESTPROPERTIESFILE" echo echo "-h this help message" } print_question(){ code=$1 message=$2 port=$3 case "$code" in $TEST_LOCALTEMPFILE) echo -n "($INDEX) Write permission on temp dir '$message'?" ;; $TEST_PORTAVAILABILITY) echo -n "($INDEX) Is port '$port' free on $message?" ;; $TEST_IFMACHINEISALIVE) echo -n "($INDEX) Is machine '$message' alive?" ;; $TEST_MACHINESSH) echo -n "($INDEX) Is '$message' accepting ssh connection?" ;; $TEST_PORTISFREE) echo -n "($INDEX) Is port '$port' free on '$message'?" ;; $ERR_TESTFILE) echo -n "($INDEX) Does the file '$message' does not exist?" ;; esac INDEX=$[INDEX + 1] } function check_testfile () { ogsource=$1; if [ ! -f "$ogsource/$TESTPROPERTIESFILE" ]; then print_question $ERR_TESTFILE $ogsource/$TESTPROPERTIESFILE echo_failure exit 1 fi } vrfy_minimal_requirements() { testproperties=$1 # All minimal requeriments are set? if [ -z "`grep -e 'mg.homemachine *= *[0-9A-Za-z]\+' $testproperties | cut -d= -f2`" ] then print_question $TEST_TESTPROPERTIES \ `sed -n -e '/mg.homemachine/p' $testproperties` echo_failure fi if [ -z "`grep -e 'mg.port *= *[0-9]\+' $testproperties | awk -F'=' '{print $2}'`" ] then print_question $TEST_TESTPROPERTIES \ "`sed -n -e '/mg.port/p' $testproperties`" echo_failure fi if [ -z "`grep -e 'peer.port *= *[0-9]\+' $testproperties | cut -d= -f2`" ] then print_question $TEST_TESTPROPERTIES \ "`sed -n -e '/peer.port *=/p' $testproperties`" echo_failure fi if [ -z "`grep -e 'peer.port.II *= *[0-9]\+' $testproperties | cut -d= -f2`" ] then print_question $TEST_TESTPROPERTIES \ "`sed -n -e '/peer.port.II *=/p' $testproperties`" echo_failure fi } kill_processes() { testproperties=$1 # All needed ports are in use? mgport=`grep -e '^ *mg.port *= *[0-9]\+' $testproperties | awk -F'=' '{ print $2 }' | tr -d '[:space:]'` guiport=`grep -e '^ *gui.port *= *[0-9]\+' $testproperties | awk -F'=' '{ print $2 }' | tr -d '[:space:]'` peerport=`grep -e '^ *peer.port *= *[0-9]\+' $testproperties | awk -F'=' '{ print $2 }' | tr -d '[:space:]'` peerportII=`grep -e '^ *peer.port.II *= *[0-9]\+' $testproperties | awk -F'=' '{ print $2 }' | tr -d '[:space:]'` peercorepeerport=`grep -e '^ *peer.corepeerport *= *[0-9]\+' $testproperties | awk -F'=' '{ print $2 }' | tr -d '[:space:]'` localhost=`grep -e '^ *mygump.machine.localhost.1 *= *\+' $testproperties | awk -F'=' '{ print $2 }' | tr -d '[:space:]' | tr -d '}' | awk -F',' '{print $2}'` pid1=`(netstat -ltnp | grep ":$mgport " | awk '{print $NF}' | awk -F'/' '{ print $1}') 2> /dev/null` pid2=`(netstat -ltnp | grep ":$peerport " | awk '{print $NF}' | awk -F'/' '{ print $1}') 2> /dev/null` pid3=`(netstat -ltnp | grep ":$peerportII " | awk '{print $NF}' | awk -F'/' '{ print $1}') 2> /dev/null` pid4=`(netstat -ltnp | grep ":$peercorepeerport " | awk '{print $NF}' | awk -F'/' '{ print $1}') 2> /dev/null` pid5=`(netstat -ltnp | grep ":$localhost " | awk '{print $NF}' | awk -F'/' '{ print $1}') 2> /dev/null` pid6=`(netstat -ltnp | grep ":$guiport " | awk '{print $NF}' | awk -F'/' '{ print $1}') 2> /dev/null` echo "Killing $pid1 $pid2 $pid3 $pid4 $pid5 $pid6" kill $pid1 $pid2 $pid3 $pid4 $pid5 $pid6 2> /dev/null } check_each_property() { testproperties=$1 # Counting machines... # Can u write in System Temp Directory? tempdir=`cat $testproperties | grep system.temp.dir | cut -d= -f2` echo "Killing previous started processes..." kill_processes $testproperties print_question $TEST_LOCALTEMPFILE $tempdir tempfile=$tempdir/test.$$ touch $tempfile if [ $? -eq 1 ]; then echo_failure else echo_success fi rm $tempfile # Are the ports free? mgport=`grep mg.port $testproperties | cut -d= -f2` guiport=`grep gui.port $testproperties | cut -d= -f2` ogport=`grep -e 'peer.port *=' $testproperties | cut -d= -f2` ogport2=`grep -e 'peer.port.II *=' $testproperties | cut -d= -f2` print_question $TEST_PORTAVAILABILITY "localhost" $mgport if [ -n "`(netstat -ltn | grep :$mgport) 2> /dev/null`" ]; then echo_failure else echo_success fi print_question $TEST_PORTAVAILABILITY "localhost" $guiport if [ -n "`(netstat -ltn | grep :$guiport) 2> /dev/null`" ]; then echo_failure else echo_success fi print_question $TEST_PORTAVAILABILITY "localhost" $ogport if [ -n "`(netstat -ltn | grep :$ogport) 2> /dev/null`" ]; then echo_failure else echo_success fi print_question $TEST_PORTAVAILABILITY "localhost" $ogport2 if [ -n "`(netstat -ltn | grep :$ogport2) 2> /dev/null`" ]; then echo_failure else echo_success fi machineslist=`grep -e '^mygump.machine.' $testproperties | tr -d ' ' | \ cut -d'{' -f2 | cut -d'}' -f 1` mygumpport=`grep -e '^mygump.default.port' $testproperties | \ cut -d= -f2` for machine in $machineslist do machinename=`echo $machine | awk -F',' '{ print $1 }'` machineport=`echo $$machine | awk -F',' '{ print $2 }'` if [ -z "$machineport" ]; then machineport=$mygumpport fi #Can connect to it? print_question $TEST_MACHINESSH $machine ssh -o StrictHostKeyChecking=no \ -o ConnectTimeout=5 \ -o PreferredAuthentications=hostbased,publickey \ $machinename exit &> /dev/null if [ $? -ne 0 ]; then echo_failure else echo_success fi #Ports are free print_question $TEST_PORTISFREE "$machinename" "$machineport" ssh -o StrictHostKeyChecking=no \ -o ConnectTimeout=5 \ -o PreferredAuthentications=hostbased,publickey \ $machinename exit &> /dev/null if [ $? -ne 0 ]; then echo_failure else echo_success fi done } findmgroot(){ PRG="$0" while [ -h "$PRG" ]; do ls=`ls -ld "$PRG"` link=`expr "$ls" : '.*-> \(.*\)$'` if expr "$link" : '.*/.*' > /dev/null; then PRG="$link" else PRG=`dirname "$PRG"`/"$link" fi done PRGDIR=`dirname "$PRG"` MGROOT=`cd "$PRGDIR/.." ; pwd` if [ -z "$MGROOT" ]; then echo "TEST_MGROOT_NOT_DEFINED" return $TEST_MGROOT_NOT_DEFINED else return $OK fi } #function main() { findmgroot MGSRCDIR=$MGROOT if [ -z "$MGSRCDIR" ]; then print_question 2 "MGROOT not defined" fi clear echo "--------------------------------------------------------------------------" echo " Testing all properties from the $TESTPROPERTIESFILE file " echo "--------------------------------------------------------------------------" check_testfile $MGSRCDIR is_executable vrfy_minimal_requirements $MGSRCDIR/$TESTPROPERTIESFILE check_each_property $MGSRCDIR/$TESTPROPERTIESFILE echo "--------------------------------------------------------------------------" if [ "$FAILURE" = 1 ]; then $SETCOLOR_FAILURE gprintf " Some problems were found in your test environment." echo gprintf " Please, try to correct them and re-run this script" echo $SETCOLOR_NORMAL else $SETCOLOR_SUCCESS gprintf " All pre-conditions were satisfied." echo gprintf " Now, you can go to the $MGSRCDIR and run 'ant commit'." echo $SETCOLOR_NORMAL fi echo "--------------------------------------------------------------------------"