Introduce a new helper function wait_local_port_listen() in helpers/lib.sh. Update the flowtables and nat_ftp test cases to use this helper. Signed-off-by: Yi Chen <yiche@xxxxxxxxxx> --- tests/shell/helpers/lib.sh | 21 +++++++++++++++++++++ tests/shell/testcases/packetpath/flowtables | 3 ++- tests/shell/testcases/packetpath/nat_ftp | 6 ++++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/tests/shell/helpers/lib.sh b/tests/shell/helpers/lib.sh index d2d20984..ce27aac3 100755 --- a/tests/shell/helpers/lib.sh +++ b/tests/shell/helpers/lib.sh @@ -28,3 +28,24 @@ assert_fail() echo "PASS: ${@}" fi } + +wait_local_port_listen() +{ + local listener_ns="${1}" + local port="${2}" + local protocol="${3}" + local pattern + local i + + pattern=":$(printf "%04X" "${port}") " + + # for tcp protocol additionally check the socket state + [ ${protocol} = "tcp" ] && pattern="${pattern}0A" + for i in $(seq 10); do + if ip netns exec "${listener_ns}" awk '{print $2" "$4}' \ + /proc/net/"${protocol}"* | grep -q "${pattern}"; then + break + fi + sleep 0.1 + done +} diff --git a/tests/shell/testcases/packetpath/flowtables b/tests/shell/testcases/packetpath/flowtables index f3580a5f..dbe470a8 100755 --- a/tests/shell/testcases/packetpath/flowtables +++ b/tests/shell/testcases/packetpath/flowtables @@ -83,7 +83,8 @@ assert_pass "set net.netfilter.nf_conntrack_tcp_timeout_established=86400" # A trick to control the timing to send a packet ip netns exec $S socat TCP6-LISTEN:10001 GOPEN:/tmp/socat-$rnd,ignoreeof & -sleep 1 +wait_local_port_listen $S 10001 tcp + ip netns exec $C socat -b 2048 PIPE:/tmp/pipefile-$rnd 'TCP:[2001:db8:ffff:22::1]:10001' & sleep 1 ip netns exec $C echo "send sth" >> /tmp/pipefile-$rnd ; assert_pass "send a packet" diff --git a/tests/shell/testcases/packetpath/nat_ftp b/tests/shell/testcases/packetpath/nat_ftp index 37ef14a3..3ba4029d 100755 --- a/tests/shell/testcases/packetpath/nat_ftp +++ b/tests/shell/testcases/packetpath/nat_ftp @@ -132,7 +132,8 @@ pam_service_name=vsftpd background=YES EOF ip netns exec $S vsftpd ${FTPCONF} -sleep 1 +wait_local_port_listen $S 21 tcp + ip netns exec $S ss -6ltnp | grep -q '*:21' assert_pass "start vsftpd server" @@ -141,7 +142,7 @@ assert_pass "start vsftpd server" reload_ruleset ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${PCAP} 2> /dev/null & pid=$! -sleep 1 +sleep 0.5 ip netns exec $C curl --no-progress-meter --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE assert_pass "curl ftp passive mode " @@ -158,6 +159,7 @@ reload_ruleset ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${PCAP} 2> /dev/null & pid=$! +sleep 0.5 ip netns exec $C curl --no-progress-meter -P - --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE assert_pass "curl ftp active mode " -- 2.49.0