1# common.sh - an include file for commonly used functions for test code. 2# BSD licensed (see LICENSE file). 3# 4# Version 3 5# 2011-02-23: get_pcat for PCAT, PCAT_DIFF and PCAT_PRINT defines. 6# 2011-02-18: ports check on BSD,Solaris. wait_nsd_up. 7# 2011-02-11: first version. 8# 9# include this file from a tdir script with 10# . ../common.sh 11# 12# overview of functions available: 13# error x : print error and exit 14# info x : print info 15# test_tool_avail x : see if program in path and complain, exit if not. 16# get_ldns_testns : set LDNS_TESTNS to executable ldns-testns 17# get_make : set MAKE to gmake or make tool. 18# get_gcc : set cc or gcc in CC 19# get_pcat : set PCAT, PCAT_DIFF and PCAT_PRINT executables. 20# set_doxygen_path : set doxygen path 21# skip_if_in_list : set SKIP=1 if name in list and tool not available. 22# get_random_port x : get RND_PORT a sequence of free random port numbers. 23# wait_server_up : wait on logfile to see when server comes up. 24# wait_ldns_testns_up : wait for ldns-testns to come up. 25# wait_unbound_up : wait for unbound to come up. 26# wait_petal_up : wait for petal to come up. 27# wait_nsd_up : wait for nsd to come up. 28# wait_server_up_or_fail: wait for server to come up or print a failure string 29# kill_pid : kill a server, make sure and wait for it to go down. 30 31 32# print error and exit 33# $0: name of program 34# $1: error to printout. 35error () { 36 echo "$0: error: $1" >&2 37 exit 1 38} 39 40# print info 41# $0: name of program 42# $1: to printout. 43info () { 44 echo "$0: info: $1" 45} 46 47# test if 'tool' is available in path and complain otherwise. 48# $1: tool 49test_tool_avail () { 50 if test ! -x "`which $1 2>&1`"; then 51 echo No "$1" in path 52 exit 1 53 fi 54} 55 56# get ldns-testns tool in LDNS_TESTNS variable. 57get_ldns_testns () { 58 if test -x "`which ldns-testns 2>&1`"; then 59 LDNS_TESTNS=ldns-testns 60 else 61 LDNS_TESTNS=/home/wouter/bin/ldns-testns 62 fi 63} 64 65# get make tool in MAKE variable, gmake is used if present. 66get_make () { 67 if test -x "`which gmake 2>&1`"; then 68 MAKE=gmake 69 else 70 MAKE=make 71 fi 72} 73 74# get cc tool in CC variable, gcc is used if present. 75get_gcc () { 76 if test -x "`which gcc 2>&1`"; then 77 CC=gcc 78 else 79 CC=cc 80 fi 81} 82 83# get pcat, pcat-print and pcat-diff 84get_pcat () { 85 PCAT=`which pcat` 86 PCAT_PRINT=`which pcat-print` 87 PCAT_DIFF=`which pcat-diff` 88} 89 90# set SKIP=1 if the name is in list and tool is not available. 91# $1: name of package to check. 92# $2: list of packages that need the tool. 93# #3: name of the tool required. 94skip_if_in_list () { 95 if echo $2 | grep $1 >/dev/null; then 96 if test ! -x "`which $3 2>&1`"; then 97 SKIP=1; 98 fi 99 fi 100} 101 102# function to get a number of random port numbers. 103# $1: number of random ports. 104# RND_PORT is returned as the starting port number 105get_random_port () { 106 local plist 107 local cont 108 local collisions 109 local i 110 local MAXCOLLISION=1000 111 cont=1 112 collisions=0 113 while test "$cont" = 1; do 114 #netstat -n -A ip -A ip6 -a | sed -e "s/^.*:\([0-9]*\) .*$/\1/" 115 RND_PORT=$(( $RANDOM + 5354 )) 116 # depending on uname try to check for collisions in port numbers 117 case "`uname`" in 118 linux|Linux) 119 plist=`netstat -n -A ip -A ip6 -a 2>/dev/null | sed -e 's/^.*:\([0-9]*\) .*$/\1/'` 120 ;; 121 FreeBSD|freebsd|NetBSD|netbsd|OpenBSD|openbsd) 122 plist=`netstat -n -a | grep "^[ut][dc]p[46] " | sed -e 's/^.*\.\([0-9]*\) .*$/\1/'` 123 ;; 124 Solaris|SunOS) 125 plist=`netstat -n -a | sed -e 's/^.*\.\([0-9]*\) .*$/\1/' | grep '^[0-9]*$'` 126 ;; 127 *) 128 plist="" 129 ;; 130 esac 131 cont=0 132 for (( i=0 ; i < $1 ; i++ )); do 133 if echo "$plist" | grep '^'`expr $i + $RND_PORT`'$' >/dev/null 2>&1; then 134 cont=1; 135 collisions=`expr $collisions + 1` 136 fi 137 done 138 if test $collisions = $MAXCOLLISION; then 139 error "too many collisions getting random port number" 140 fi 141 done 142} 143 144# wait for server to go up, pass <logfilename> <string to watch> 145# $1 : logfilename 146# $2 : string to watch for. 147# exits with failure if it does not come up 148wait_server_up () { 149 local MAX_UP_TRY=120 150 local WAIT_THRES=30 151 local try 152 for (( try=0 ; try <= $MAX_UP_TRY ; try++ )) ; do 153 if test -f $1 && fgrep "$2" $1 >/dev/null; then 154 #echo "done on try $try" 155 break; 156 fi 157 if test $try -eq $MAX_UP_TRY; then 158 echo "Server in $1 did not go up!" 159 cat $1 160 exit 1; 161 fi 162 if test $try -ge $WAIT_THRES; then 163 sleep 1 164 fi 165 done 166} 167 168# wait for ldns-testns to come up 169# $1 : logfilename that is watched. 170wait_ldns_testns_up () { 171 wait_server_up "$1" "Listening on port" 172} 173 174# wait for unbound to come up 175# string 'Start of service' in log. 176# $1 : logfilename that is watched. 177wait_unbound_up () { 178 wait_server_up "$1" "start of service" 179} 180 181# wait for petal to come up 182# string 'petal start' in log. 183# $1 : logfilename that is watched. 184wait_petal_up () { 185 wait_server_up "$1" "petal start" 186} 187 188# wait for nsd to come up 189# string nsd start in log. 190# $1 : logfilename that is watched. 191wait_nsd_up () { 192 wait_server_up "$1" " started (NSD " 193} 194 195# wait for server to go up, pass <logfilename> <string to watch> <badstr> 196# $1 : logfile 197# $2 : success string 198# $3 : failure string 199wait_server_up_or_fail () { 200 local MAX_UP_TRY=120 201 local WAIT_THRES=30 202 local try 203 for (( try=0 ; try <= $MAX_UP_TRY ; try++ )) ; do 204 if test -f $1 && fgrep "$2" $1 >/dev/null; then 205 echo "done on try $try" 206 break; 207 fi 208 if test -f $1 && fgrep "$3" $1 >/dev/null; then 209 echo "failed on try $try" 210 break; 211 fi 212 if test $try -eq $MAX_UP_TRY; then 213 echo "Server in $1 did not go up!" 214 cat $1 215 exit 1; 216 fi 217 if test $try -ge $WAIT_THRES; then 218 sleep 1 219 fi 220 done 221} 222 223# kill a pid, make sure and wait for it to go down. 224# $1 : pid to kill 225kill_pid () { 226 local MAX_DOWN_TRY=120 227 local WAIT_THRES=30 228 local try 229 kill $1 230 for (( try=0 ; try <= $MAX_DOWN_TRY ; try++ )) ; do 231 if kill -0 $1 >/dev/null 2>&1; then 232 : 233 else 234 #echo "done on try $try" 235 break; 236 fi 237 if test $try -eq $MAX_DOWN_TRY; then 238 echo "Server in $1 did not go down! Send SIGKILL" 239 kill -9 $1 >/dev/null 2>&1 240 fi 241 if test $try -ge $WAIT_THRES; then 242 sleep 1 243 fi 244 # re-send the signal 245 kill $1 >/dev/null 2>&1 246 done 247 return 0 248} 249 250# set doxygen path, so that make doc can find doxygen 251set_doxygen_path () { 252 if test -x '/home/wouter/bin/doxygen'; then 253 export PATH="/home/wouter/bin:$PATH" 254 fi 255} 256 257