Tests that required "UDP echo" service used custom logic to ensure
that the "nc" process and all of its children got killed on the cleanup.
This change extracts that custom logic into a reusable macro
NETNS_START_UDP_ECHO.

Signed-off-by: Martin Kalcok <[email protected]>
---
 tests/system-common-macros.at | 21 +++++++++++++++++++++
 tests/system-ovn.at           | 26 ++++++--------------------
 2 files changed, 27 insertions(+), 20 deletions(-)

diff --git a/tests/system-common-macros.at b/tests/system-common-macros.at
index 5243b3f1b..b32fc0b68 100644
--- a/tests/system-common-macros.at
+++ b/tests/system-common-macros.at
@@ -178,6 +178,27 @@ m4_define([NETNS_START_DHCPD],
     ]
 )
 
+# NETNS_START_UDP_ECHO([namespace], [ip-version], [port], [pidfile])
+#
+# Start a "persistent" UDP echo server within 'namespace', listening on 'port'
+# and echoing back any data it receives. 'ip-version' is either "4" or "6".
+# The server keeps running across multiple client connections (nc's "-k").
+#
+# nc's "-k" makes it fork child processes that keep the listening port open, so
+# killing only the parent leaves those children running and hangs the whole
+# test cleanup (especially on failures). This macro therefore installs a
+# cleanup hook that kills the child processes (pkill -P) before the parent.
+#
+# The parent PID is recorded in the 'pidfile' and the user can employ the same
+# (pkill -P && kill) technique to cleanly stop the server at will during the
+# test.
+#
+m4_define([NETNS_START_UDP_ECHO],
+   [ip netns exec $1 nc -$2 -k -l -u -p $3 -d 0.1 -c '/bin/cat' & echo $! > $4
+     echo "pkill -P \`cat $4\` && kill \`cat $4\`" >> cleanup
+   ]
+)
+
 # OVS_CHECK_CT_ZERO_SNAT()
 m4_define([OVS_CHECK_CT_ZERO_SNAT],
     [AT_SKIP_IF([! grep -q "Datapath supports ct_zero_snat" 
ovs-vswitchd.log])])
diff --git a/tests/system-ovn.at b/tests/system-ovn.at
index 9970951e8..a7700eee4 100644
--- a/tests/system-ovn.at
+++ b/tests/system-ovn.at
@@ -4565,19 +4565,12 @@ check ovn-nbctl --wait=hv lr-lb-add lr0 lb2
 # Wait until udp service_monitor are set to offline.
 wait_row_count Service_Monitor 2 status=offline protocol=udp
 
-# We need to use "-k" argument because we keep receiving health check probes.
-NETNS_DAEMONIZE([sw0-p1], [nc -4 -k -l -u -p 53 -d 0.1 -c '/bin/cat'], 
[udp1.pid])
-NETNS_DAEMONIZE([sw0-p2], [nc -4 -k -l -u -p 53 -d 0.1 -c '/bin/cat'], 
[udp2.pid])
+# Start persistent UDP service that keeps replying to the health-check probes.
+NETNS_START_UDP_ECHO([sw0-p1], [4], [53], [udp1.pid])
+NETNS_START_UDP_ECHO([sw0-p2], [4], [53], [udp2.pid])
 
-# UDP nc with "-k" is bit tricky to cleanup, especially on test failures, due 
to its
-# child processes keeping the listening port open. If they are not cleaned up 
properly,
-# the whole test cleanup hangs.
 udp1_pid=$(cat udp1.pid)
 udp2_pid=$(cat udp2.pid)
-echo "pkill -P $udp1_pid && kill $udp1_pid" >> cleanup
-echo "pkill -P $udp2_pid && kill $udp2_pid" >> cleanup
-:> udp1.pid
-:> udp2.pid
 
 wait_row_count Service_Monitor 2 status=online
 
@@ -4764,19 +4757,12 @@ check ovn-nbctl --wait=hv lr-lb-add lr0 lb2
 # Wait until udp service_monitor are set to offline.
 wait_row_count Service_Monitor 2 status=offline protocol=udp
 
-# We need to use "-k" argument because we keep receiving health check probes.
-NETNS_DAEMONIZE([sw0-p1], [nc -6 -k -l -u -p 53 -d 0.1 -c '/bin/cat'], 
[udp1.pid])
-NETNS_DAEMONIZE([sw0-p2], [nc -6 -k -l -u -p 53 -d 0.1 -c '/bin/cat'], 
[udp2.pid])
+# Start persistent UDP service that keeps replying to the health-check probes.
+NETNS_START_UDP_ECHO([sw0-p1], [6], [53], [udp1.pid])
+NETNS_START_UDP_ECHO([sw0-p2], [6], [53], [udp2.pid])
 
-# UDP nc with "-k" is bit tricky to cleanup, especially on test failures, due 
to its
-# child processes keeping the listening port open. If they are not cleaned up 
properly,
-# the whole test cleanup hangs.
 udp1_pid=$(cat udp1.pid)
 udp2_pid=$(cat udp2.pid)
-echo "pkill -P $udp1_pid && kill $udp1_pid" >> cleanup
-echo "pkill -P $udp2_pid && kill $udp2_pid" >> cleanup
-:> udp1.pid
-:> udp2.pid
 
 wait_row_count Service_Monitor 2 status=online
 
-- 
2.51.2

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to