diff options
Diffstat (limited to 'tests/scripts/common.sh')
-rw-r--r-- | tests/scripts/common.sh | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/tests/scripts/common.sh b/tests/scripts/common.sh index 3cf841c25b..518d8416bc 100644 --- a/tests/scripts/common.sh +++ b/tests/scripts/common.sh @@ -21,9 +21,50 @@ export TZ="UTC" -GETPORT='rc=0;myrandom=$(date +%N | sed 's/^0*//');while test $rc = 0;do PORT="$(((($$<<15)|$myrandom) % 63001 + 2000))"; - netstat -anl|grep "[\:\.]$PORT" >/dev/null 2>&1; - rc=$?;done;' +# Check for a utility to list ports. Both ss and netstat will list +# ports for normal users, and have similar semantics, so put the +# command in the caller's PFCMD, or exit, indicating an unsupported +# test. Prefer ss from iproute2 over the older netstat. +have_port_finder() { + for file in $(which ss) /*bin/ss /usr/*bin/ss /usr/local/*bin/ss;do + if test -x "$file";then + PFCMD="$file";return 0 + fi + done + + if test -z "$PFCMD";then + for file in $(which netstat) /bin/netstat /usr/bin/netstat /usr/local/bin/netstat;do + if test -x "$file";then + PFCMD="$file";return 0 + fi + done + fi + + if test -z "$PFCMD";then + echo "neither ss nor netstat found" + exit 1 + fi +} + +check_if_port_in_use() { + local PORT="$1" + local PFCMD; have_port_finder + $PFCMD -an|grep "[\:\.]$PORT" >/dev/null 2>&1 +} + +check_if_port_listening() { + local PORT="$1" + local PFCMD; have_port_finder + $PFCMD -anl|grep "[\:\.]$PORT"|grep LISTEN >/dev/null 2>&1 +} + +# Find a port number not currently in use. +GETPORT='rc=0; myrandom=$(date +%N | sed s/^0*//) + while test $rc = 0;do + PORT="$(((($$<<15)|$myrandom) % 63001 + 2000))" + check_if_port_in_use $PORT;rc=$? + done +' check_for_datefudge() { TSTAMP=`datefudge -s "2006-09-23" date -u +%s || true` @@ -49,10 +90,10 @@ wait_for_port() sleep 4 for i in 1 2 3 4 5 6;do - netstat -anl|grep "[\:\.]$PORT"|grep LISTEN >/dev/null 2>&1 + check_if_port_listening ${PORT} ret=$? if test $ret != 0;then - netstat -anl|grep "[\:\.]$PORT" + check_if_port_in_use ${PORT} echo try $i sleep 2 else @@ -68,7 +109,7 @@ wait_for_free_port() local PORT="$1" for i in 1 2 3 4 5 6;do - netstat -anl|grep "[\:\.]$PORT" >/dev/null 2>&1 + check_if_port_in_use ${PORT} ret=$? if test $ret != 0;then break |