From fe2d4b0274912e2768dd00c98d4b87b6201da849 Mon Sep 17 00:00:00 2001 From: Darek Stojaczyk Date: Mon, 17 Jun 2019 06:04:58 +0200 Subject: [PATCH] test/iscsi/sock: add separate waitfortcp() function The test used waitforlisten() from autotest_common.sh to wait for a socat instance and happened to work only by a coincidence. As we can see in all the messages that waitforlisten prints, that function is meant for SPDK applications running an RPC server. We're about to simplify waitforlisten a lot, so for now just copy its current version to sock.sh and name it waitfortcp(). Note that it already had to be stripped of some rpc references. Change-Id: Ibbe7cf67d20fbc277d407b20073154a96ddaaade Signed-off-by: Darek Stojaczyk Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/458221 Tested-by: SPDK CI Jenkins Reviewed-by: Ben Walker Reviewed-by: Shuhei Matsumoto --- test/iscsi_tgt/sock/sock.sh | 59 ++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/test/iscsi_tgt/sock/sock.sh b/test/iscsi_tgt/sock/sock.sh index a8940cd07..c80c205d5 100755 --- a/test/iscsi_tgt/sock/sock.sh +++ b/test/iscsi_tgt/sock/sock.sh @@ -5,6 +5,63 @@ rootdir=$(readlink -f $testdir/../../..) source $rootdir/test/common/autotest_common.sh source $rootdir/test/iscsi_tgt/common.sh +function waitfortcp() { + local addr="$2" + + if hash ip &>/dev/null; then + local have_ip_cmd=true + else + local have_ip_cmd=false + fi + + if hash ss &>/dev/null; then + local have_ss_cmd=true + else + local have_ss_cmd=false + fi + + echo "Waiting for process to start up and listen on address $addr..." + # turn off trace for this loop + xtrace_disable + local ret=0 + local i + for (( i = 40; i != 0; i-- )); do + # if the process is no longer running, then exit the script + # since it means the application crashed + if ! kill -s 0 $1; then + echo "ERROR: process (pid: $1) is no longer running" + ret=1 + break + fi + + if $have_ip_cmd; then + namespace=$(ip netns identify $1) + if [ -n "$namespace" ]; then + ns_cmd="ip netns exec $namespace" + fi + fi + + if $have_ss_cmd; then + if $ns_cmd ss -ln | egrep -q "\s+$addr\s+"; then + break + fi + elif [[ "$(uname -s)" == "Linux" ]]; then + # For Linux, if system doesn't have ss, just assume it has netstat + if $ns_cmd netstat -an | grep -iw LISTENING | egrep -q "\s+$addr\$"; then + break + fi + fi + sleep 0.5 + done + + xtrace_restore + if (( i == 0 )); then + echo "ERROR: timeout while waiting for process (pid: $1) to start listening on '$addr'" + ret=1 + fi + return $ret +} + # $1 = "iso" - triggers isolation mode (setting up required environment). # $2 = test type posix or vpp. defaults to posix. iscsitestinit $1 $2 @@ -22,7 +79,7 @@ echo "Testing client path" $SOCAT_APP tcp-l:$ISCSI_PORT,fork,bind=$INITIATOR_IP exec:'/bin/cat' & server_pid=$! trap "killprocess $server_pid;iscsitestfini $1 $2; exit 1" SIGINT SIGTERM EXIT -waitforlisten $server_pid $INITIATOR_IP:$ISCSI_PORT +waitfortcp $server_pid $INITIATOR_IP:$ISCSI_PORT # send message using hello_sock client message="**MESSAGE:This is a test message from the client**"