From: Marcel Poul Date: Thu, 16 Feb 2012 17:11:35 +0000 (+0000) Subject: common definitions and functions for canl module tests X-Git-Tag: gridsite-core_R_1_7_17~88 X-Git-Url: http://scientific.zcu.cz/git/?a=commitdiff_plain;h=a7b4346705f98a5d691380cea23f75dcf08217fc;p=jra1mw.git common definitions and functions for canl module tests --- diff --git a/org.glite.testsuites.ctb/CANL-C/tests/canl-common.sh b/org.glite.testsuites.ctb/CANL-C/tests/canl-common.sh new file mode 100755 index 0000000..a75e0df --- /dev/null +++ b/org.glite.testsuites.ctb/CANL-C/tests/canl-common.sh @@ -0,0 +1,322 @@ +# $Header$ +# +# Copyright (c) Members of the EGEE Collaboration. 2004-2010. +# See http://www.eu-egee.org/partners for details on the copyright holders. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ------------------------------------------------------------------------------ +# Definitions of functions and variables common to LB test scripts +# +# ping_host() - basic network ping +# check_binaries() - check for binary executables, calls check_exec() +# check_socket() - TCPecho to host:port +# +# ------------------------------------------------------------------------------ + +# read common definitions and functions +TEST_COMMON=test-common.sh +if [ ! -r ${TEST_COMMON} ]; then + printf "Common definitions '${TEST_COMMON}' not found!\n" + exit 2 +fi +source ${TEST_COMMON} + +ulimit -c unlimited + +# define variables +GLITE_LB_LOCATION=${GLITE_LB_LOCATION:-$GLITE_LOCATION} +GLITE_LOCATION=${GLITE_LOCATION:-'/opt/glite'} +SAME_SENSOR_HOME=${SAME_SENSOR_HOME:-.} +for dir in $GLITE_LOCATION $GLITE_LOCATION/lib64/glite-lb $GLITE_LOCATION/lib/glite-lb; do + if test -d "$dir/examples"; then PATH="$dir/examples:$PATH"; fi +done +PATH="$GLITE_LOCATION/bin:$PATH" +export PATH + +#general grid binaries +GRIDPROXYINFO=grid-proxy-info + +# default LB ports +GLITE_LB_SERVER_PORT=${GLITE_LB_SERVER_PORT:-9000} +GLITE_LB_IL_SOCK=${GLITE_LB_IL_SOCK:-/tmp/interlogger.sock} +let GLITE_LB_SERVER_QPORT=${GLITE_LB_SERVER_PORT}+1 +if [ -z "${GLITE_LB_SERVER_WPORT}" ]; then + let GLITE_LB_SERVER_WPORT=${GLITE_LB_SERVER_PORT}+3 +fi + +GLITE_LB_LOGGER_PORT=${GLITE_LB_LOGGER_PORT:-9002} + +# default sockets +GLITE_WMS_LBPROXY_STORE_SOCK=${GLITE_WMS_LBPROXY_STORE_SOCK:-/tmp/lb proxy store.sock} + +# other binaries +TEST_SOCKET=$SAME_SENSOR_HOME/tests/testSocket +SYS_LSOF=lsof +SYS_GREP=grep +SYS_SED=sed +SYS_PS=ps +SYS_PIDOF=pidof +SYS_MYSQLD=mysqld +SYS_MYSQLADMIN=mysqladmin +SYS_PING=ping +SYS_AWK=awk +SYS_ECHO=echo +SYS_DOMAINNAME=domainname +SYS_CURL=curl +SYS_RM="rm -f" +SYS_CHMOD=chmod +SYS_LDAPSEARCH=ldapsearch +SYS_CAT=cat +SYS_NL=nl +SYS_TAIL=tail +SYS_DATE=date +SYS_EXPR=expr +SYS_BC=bc +SYS_SCP=scp +SYS_TOUCH=touch +SYS_HOSTNAME=hostname +SYS_RPM=rpm +SYS_WC=wc +SYS_LS=ls +SYS_STAT=stat + +#generate proxy shell script file +GEN_PROXY=./canl-generate-fake-proxy.sh +function check_credentials_and_generate_proxy() +{ + check_credentials + if [ $? != 0 ]; then + if [ "$1" != "" ]; then + PROXYLIFE=" --hours $1" + else + PROXYLIFE="" + fi + $GEN_PROXY $PROXYLIFE + if [ $? != 0 ]; then + test_failed + print_error "Proxy not created - process failed" + return 2 + fi + check_credentials + if [ $? != 0 ]; then + test_failed + print_error "Credentials still not passing check" + return 2 + fi + else + test_done + fi + return 0 +} + +# ping host +function ping_host() +{ + if [ -z $1 ]; then + set_error "No host to ping" + return $TEST_ERROR + fi + PING_HOST=$1 + # XXX: there might be a better way to test the network reachability + result=`${SYS_PING} -c 3 $PING_HOST 2>/dev/null | ${SYS_GREP} " 0% packet loss"| wc -l` + if [ $result -gt 0 ]; then + return $TEST_OK + else + return $TEST_ERROR + fi +} + + +# check the binaries +function check_exec() +{ + if [ -z $1 ]; then + set_error "No binary to check" + return $TEST_ERROR + fi + # XXX: maybe use bash's command type? + local ret=`which $1 2> /dev/null` + if [ -n "$ret" -a -x "$ret" ]; then + return $TEST_OK + else + return $TEST_ERROR + fi +} + +function check_binaries() +{ +# TODO: test only the binaries that are needed - it can differ in each test + local ret=$TEST_OK + for file in $@ + do + check_exec $file + if [ $? -gt 0 ]; then + print_error "command $file not found" + ret=$TEST_ERROR + fi + done + return $ret +} + +# check socket +function check_socket() +{ + if [ $# -lt 2 ]; then + set_error "No host:port to check" + return $TEST_ERROR + fi + $TEST_SOCKET $1 $2 2> $testerrfile + if [ $? -eq 0 ]; then + return $TEST_OK + else + return $TEST_ERROR + fi +} + +# Check listener +# Arguments: +# $1: program expected to listen on the given port +# $2: TCP port to check +function check_listener() +{ + req_program=$1 + req_port=$2 + if [ -z $1 ]; then + set_error "No program name entered" + return $TEST_ERROR + fi + + pid=`lsof -F p -i TCP:$req_port | sed "s/^p//"` + if [ -z $pid ]; then + return $TEST_ERROR + fi + program=`ps -p ${pid} -o args= | grep -E "[\/]*$req_program[ \t]*"` + if [ -z "$program" ]; then + return $TEST_ERROR + else + return $TEST_OK + fi +} + + +# Check socket listener +# Arguments: +# $1: program expected to listen on the given socket +# $2: socket to check +function check_socket_listener() +{ + req_program=$1 + req_socket=$2 + if [ -z $1 ]; then + set_error "No program name entered" + return $TEST_ERROR + fi + + pid=`lsof -F p $req_socket | sed "s/^p//" | head -n 1` + if [ -z $pid ]; then + return $TEST_ERROR + fi + program=`ps -p ${pid} -o args= | grep -E "[\/]*$req_program[ \t]*"` + if [ -z "$program" ]; then + return $TEST_ERROR + else + return $TEST_OK + fi +} + +#df /var/lib/mysql/ | tail -n 1 | awk '{ print $4 }' + +function try_purge() +{ + #Purge test job + joblist=$1 + + printf "Purging test job (Trying the best, result will not be tested)\n" + + ${LBPURGE} -j ${joblist} + + $SYS_RM ${joblist} + +} + +function test_args() +{ + echo $@ +} + +function check_credentials() +{ + my_GRIDPROXYINFO=${GRIDPROXYINFO} + if [ "$1" != "" ]; then + my_GRIDPROXYINFO="${GRIDPROXYINFO} -f $1" + fi + + timeleft=`${my_GRIDPROXYINFO} 2>/dev/null | ${SYS_GREP} -E "^timeleft" | ${SYS_SED} "s/timeleft\s*:\s//"` + if [ "$timeleft" = "" ]; then + printf "... No credentials... " + return 1 + fi + if [ "$timeleft" = "0:00:00" ]; then + printf "... Credentials expired... " + return 1 + fi + return 0 +} + +function check_srv_version() +{ + check_binaries $LBWSGETVERSION + if [ $? -gt 0 ]; then + return 2 + else + servername=`echo ${GLITE_WMS_QUERY_SERVER} | ${SYS_SED} "s/:.*//"` + wsglservver=`$LBWSGETVERSION -m ${servername}:${GLITE_LB_SERVER_WPORT} | $SYS_SED 's/^.*Server version:\s*//'` + if [ "$wsglservver" == "" ]; then + return 2 + else + + SRVVER=`$SYS_ECHO $wsglservver | $SYS_GREP -o -E "^[0-9]+\.[0-9]+"` + CHKVER=`$SYS_ECHO $2 | $SYS_GREP -o -E "^[0-9]*\.[0-9*]"` + + printf "Srv. version $SRVVER $1 $CHKVER? " + cresult=`$SYS_EXPR $SRVVER $1 $CHKVER` + if [ "$cresult" -eq "1" ]; then + printf "yes " + return 0 + else + printf "no " + return 1 + fi + fi + fi + return 0 +} + +function notif_wait() { + timeout="$1" + jobid="$2" + f="$3" + + while ! $SYS_GREP ${jobid} "$f" > /dev/null 2>&1; do + sleep 1 + printf "." + timeout=$((timeout-1)) + if [ $timeout -le 0 ]; then + printf "timeout" + break; + fi + done + echo + $SYS_GREP ${jobid} $$_notifications.txt > /dev/null +} diff --git a/org.glite.testsuites.ctb/CANL-C/tests/test-common.sh b/org.glite.testsuites.ctb/CANL-C/tests/test-common.sh new file mode 100644 index 0000000..520fb27 --- /dev/null +++ b/org.glite.testsuites.ctb/CANL-C/tests/test-common.sh @@ -0,0 +1,260 @@ +# $Header$ +# +# Copyright (c) Members of the EGEE Collaboration. 2004-2010. +# See http://www.eu-egee.org/partners for details on the copyright holders. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ------------------------------------------------------------------------------ +# Definition of test script return messages +# +# The test scripts should use the variables test_done and test_failed to +# report whether they failed or succeeded. +# +# The variable test_reset is used to turn off all attributes and switch +# to the standard character set. +# +# \033 ascii ESCape +# \033[G move to column (linux console, xterm, not vt100) +# \033[C move columns forward but only upto last column +# \033[D move columns backward but only upto first column +# \033[A move rows up +# \033[B move rows down +# \033[1m switch on bold +# \033[31m switch on red +# \033[32m switch on green +# \033[33m switch on yellow +# \033[m switch off color/bold +# \017 exit alternate mode (xterm, vt100, linux console) +# \033[10m exit alternate mode (linux console) +# \015 carriage return (without newline) +# +# See also United Linux or OpenSUSE /etc/rc.status script +# +# ------------------------------------------------------------------------------ + +# Do _not_ be fooled by non POSIX locale +LC_ALL=POSIX +export LC_ALL + +# Seek for terminal size and, if needed, set default size +if [ -z "${LINES}" -o -z "${COLUMNS}" ]; then + stty_size=`stty size 2> /dev/null` + if [ $? = 0 ]; then + LINES=`echo ${stty_size} | awk '{print $1}'` + COLUMNS=`echo ${stty_size} | awk '{print $2}'` + else + LINES=24 + if [ $LBTSTCOLS -gt 0 ]; then + COLUMNS=$LBTSTCOLS + else + COLUMNS=80 + fi + fi +fi +if [ ! $LINES -ge 0 ]; then LINES=24; fi +if [ ! $COLUMNS -ge 0 ]; then COLUMNS=80; fi +export LINES COLUMNS + +# default return values +TEST_ERROR=1 +TEST_OK=0 + +# test error file +testerrfile=$$.err + +function set_test() +{ +test_done="${spacefill}${begin_green}done${end_green}" +test_running="${spacefill}${begin_green}running${end_green}" +test_failed="${spacefill}${begin_red}-TEST FAILED-${end_red}" +test_missed="${spacefill}${begin_red}missing${end_red}" +test_skipped="${spacefill}${begin_yellow}skipped${end_yellow}" +test_warning="${spacefill}${begin_yellow}warning${end_yellow}" +test_dead="${spacefill}${begin_red}dead${end_red}" +test_unused="${spacefill}${begin_bold}unused${end_bold}" +test_unknown="${spacefill}${begin_yellow}unknown${end_yellow}" + +test_start="${spacefill}${begin_green}start${end_green}" +test_end="${spacefill}${begin_green}end${end_green}" +} + +function test_done() { printf "${test_done}${lf}"; } +function test_running() { printf "${test_running}${lf}"; } +function test_failed() { printf "${test_failed}${lf}"; } +function test_missed() { printf "${test_missed}${lf}"; } +function test_skipped() { printf "${test_skipped}${lf}"; } +function test_warning() { printf "${test_warning}${lf}"; } +function test_dead() { printf "${test_dead}${lf}"; } +function test_unused() { printf "${test_unused}${lf}"; } +function test_unknown { printf "${test_unknown}${lf}"; } +function test_start() { + syslog "${test_start}"; + reset_error +} +function test_end() { + syslog "${test_end}"; + reset_error +} + +# set output to ASCII (without colors) +function setOutputASCII() +{ +lf="\n" +spacefill="..." + +begin_bold="" +begin_black="" +begin_red="" +begin_green="" +begin_yellow="" +begin_blue="" +begin_magenta="" +begin_cyan="" +begin_white="" + +end_bold="" +end_black="" +end_red="" +end_green="" +end_yellow="" +end_blue="" +end_magenta="" +end_cyan="" +end_white="" + +set_test +} + +# set output to ASCII with ANSI colors +function setOutputColor() +{ +local esc=`echo -en "\033"` +local normal="${esc}[0m" # unsets color to term's fg color +lf="\n" +spacefill=`echo -en "\015${esc}[${COLUMNS}C${esc}[15D"` + +begin_bold="${esc}[0;1m" +begin_black="${esc}[0;30m" +begin_red="${esc}[0;31m" +begin_green="${esc}[0;32m" +begin_yellow="${esc}[0;33m" +begin_blue="${esc}[0;34m" +begin_magenta="${esc}[0;35m" +begin_cyan="${esc}[0;36m" +begin_white="${esc}[0;37m" + +end_bold="$normal" +end_black="$normal" +end_red="$normal" +end_green="$normal" +end_yellow="$normal" +end_blue="$normal" +end_magenta="$normal" +end_cyan="$normal" +end_white="$normal" + +set_test +} + +# set output to HTML +function setOutputHTML() +{ +local ENDFONT="" +lf="
\n" +spacefill="   " +is_html=1 + +begin_bold="" +begin_black="" +begin_red="" +begin_green="" +begin_yellow="" +begin_blue="" +begin_magenta="" +begin_cyan="" +begin_white="" + +end_bold="" +end_black="$ENDFONT" +end_red="$ENDFONT" +end_green="$ENDFONT" +end_yellow="$ENDFONT" +end_blue="$ENDFONT" +end_magenta="$ENDFONT" +end_cyan="$ENDFONT" +end_white="$ENDFONT" + +set_test +} + +function reset_error() +{ + rm -f $testerrfile +} + +function set_error() +{ + printf "%s ${lf}" "$*" > $testerrfile +} + +function update_error() +{ + printf "%s; " "$*" >> $testerrfile +} + +function print_error() +{ + printf "\t${begin_red}Error${end_red}: %s ${lf}" "$*" + + if [ -f $testerrfile ]; then + printf "\t${begin_red}Error${end_red}: %s ${lf}" "`cat $testerrfile`" + fi + reset_error +} + +function print_warning() +{ + printf "${begin_magenta}Warning${end_magenta}: %s ${lf}" "$*" +} + +function print_info() +{ + printf "${begin_blue}Info${end_blue}: %s ${lf}" "$*" +} + +function print_newline() +{ + printf "${lf}" +} + +function syslog() +{ + local tmp="`date +'%b %d %H:%M:%S'` `hostname` $progname" + printf "${begin_bold}${tmp}${end_bold}: %s ${lf}" "$*" +} + +function dprintf() +{ + if [ $DEBUG -gt 0 ]; then + printf "%s${lf}" "$*" + fi +} + +# by default set output to color if possible +if test -t 1 -a "$TERM" != "raw" -a "$TERM" != "dumb" && stty size <&1 > /dev/null 2>&1 ; then + setOutputColor +else + setOutputASCII +fi +