]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | #!/usr/bin/env bash |
2 | ||
3 | testdir=$(readlink -f $(dirname $0)) | |
4 | rootdir=$(readlink -f $testdir/../../..) | |
5 | source $rootdir/test/common/autotest_common.sh | |
6 | source $rootdir/test/iscsi_tgt/common.sh | |
7 | ||
f67539c2 TL |
8 | function waitfortcp() { |
9 | local addr="$2" | |
10 | ||
11 | if hash ip &> /dev/null; then | |
12 | local have_ip_cmd=true | |
13 | else | |
14 | local have_ip_cmd=false | |
15 | fi | |
16 | ||
17 | if hash ss &> /dev/null; then | |
18 | local have_ss_cmd=true | |
19 | else | |
20 | local have_ss_cmd=false | |
21 | fi | |
22 | ||
23 | echo "Waiting for process to start up and listen on address $addr..." | |
24 | # turn off trace for this loop | |
25 | xtrace_disable | |
26 | local ret=0 | |
27 | local i | |
28 | for ((i = 40; i != 0; i--)); do | |
29 | # if the process is no longer running, then exit the script | |
30 | # since it means the application crashed | |
31 | if ! kill -s 0 $1; then | |
32 | echo "ERROR: process (pid: $1) is no longer running" | |
33 | ret=1 | |
34 | break | |
35 | fi | |
36 | ||
37 | if $have_ip_cmd; then | |
38 | namespace=$(ip netns identify $1) | |
39 | if [ -n "$namespace" ]; then | |
40 | ns_cmd="ip netns exec $namespace" | |
41 | fi | |
42 | fi | |
43 | ||
44 | if $have_ss_cmd; then | |
45 | if $ns_cmd ss -ln | grep -E -q "\s+$addr\s+"; then | |
46 | break | |
47 | fi | |
48 | elif [[ "$(uname -s)" == "Linux" ]]; then | |
49 | # For Linux, if system doesn't have ss, just assume it has netstat | |
50 | if $ns_cmd netstat -an | grep -iw LISTENING | grep -E -q "\s+$addr\$"; then | |
51 | break | |
52 | fi | |
53 | fi | |
54 | sleep 0.5 | |
55 | done | |
56 | ||
57 | xtrace_restore | |
58 | if ((i == 0)); then | |
59 | echo "ERROR: timeout while waiting for process (pid: $1) to start listening on '$addr'" | |
60 | ret=1 | |
61 | fi | |
62 | return $ret | |
63 | } | |
64 | ||
9f95a23c TL |
65 | # $1 = "iso" - triggers isolation mode (setting up required environment). |
66 | # $2 = test type posix or vpp. defaults to posix. | |
67 | iscsitestinit $1 $2 | |
68 | ||
f67539c2 TL |
69 | if [ "$1" == "iso" ]; then |
70 | TEST_TYPE=$2 | |
71 | else | |
72 | TEST_TYPE=$1 | |
73 | fi | |
74 | ||
75 | if [ -z "$TEST_TYPE" ]; then | |
76 | TEST_TYPE="posix" | |
77 | fi | |
78 | ||
79 | if [ "$TEST_TYPE" != "posix" ] && [ "$TEST_TYPE" != "vpp" ]; then | |
80 | echo "No correct sock implmentation specified" | |
81 | exit 1 | |
82 | fi | |
83 | ||
84 | HELLO_SOCK_APP="${TARGET_NS_CMD[*]} $SPDK_EXAMPLE_DIR/hello_sock" | |
85 | if [ $SPDK_TEST_VPP -eq 1 ]; then | |
86 | HELLO_SOCK_APP+=" -L sock_vpp" | |
87 | fi | |
9f95a23c TL |
88 | SOCAT_APP="socat" |
89 | ||
90 | # ---------------- | |
91 | # Test client path | |
92 | # ---------------- | |
93 | timing_enter sock_client | |
94 | echo "Testing client path" | |
95 | ||
96 | # start echo server using socat | |
f67539c2 TL |
97 | $SOCAT_APP tcp-l:$ISCSI_PORT,fork,bind=$INITIATOR_IP exec:'/bin/cat' & |
98 | server_pid=$! | |
99 | trap 'killprocess $server_pid;iscsitestfini $1 $2; exit 1' SIGINT SIGTERM EXIT | |
9f95a23c | 100 | |
f67539c2 | 101 | waitfortcp $server_pid $INITIATOR_IP:$ISCSI_PORT |
9f95a23c TL |
102 | |
103 | # send message using hello_sock client | |
104 | message="**MESSAGE:This is a test message from the client**" | |
f67539c2 | 105 | response=$(echo $message | $HELLO_SOCK_APP -H $INITIATOR_IP -P $ISCSI_PORT -N $TEST_TYPE) |
9f95a23c TL |
106 | |
107 | if ! echo "$response" | grep -q "$message"; then | |
108 | exit 1 | |
109 | fi | |
110 | ||
111 | trap '-' SIGINT SIGTERM EXIT | |
112 | # NOTE: socat returns code 143 on SIGINT | |
113 | killprocess $server_pid || true | |
114 | ||
9f95a23c TL |
115 | timing_exit sock_client |
116 | ||
117 | # ---------------- | |
118 | # Test server path | |
119 | # ---------------- | |
120 | ||
121 | timing_enter sock_server | |
122 | ||
123 | # start echo server using hello_sock echo server | |
f67539c2 TL |
124 | $HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT -S -N $TEST_TYPE & |
125 | server_pid=$! | |
126 | trap 'killprocess $server_pid; iscsitestfini $1 $2; exit 1' SIGINT SIGTERM EXIT | |
9f95a23c TL |
127 | waitforlisten $server_pid |
128 | ||
129 | # send message to server using socat | |
130 | message="**MESSAGE:This is a test message to the server**" | |
f67539c2 | 131 | response=$(echo $message | $SOCAT_APP - tcp:$TARGET_IP:$ISCSI_PORT 2> /dev/null) |
9f95a23c TL |
132 | |
133 | if [ "$message" != "$response" ]; then | |
134 | exit 1 | |
135 | fi | |
136 | ||
137 | trap - SIGINT SIGTERM EXIT | |
138 | ||
139 | killprocess $server_pid | |
140 | ||
141 | iscsitestfini $1 $2 | |
9f95a23c | 142 | timing_exit sock_server |