test: hsr: Move common code to hsr_common.sh file
authorLukasz Majewski <lukma@denx.de>
Tue, 23 Apr 2024 12:49:06 +0000 (14:49 +0200)
committerPaolo Abeni <pabeni@redhat.com>
Fri, 26 Apr 2024 10:04:43 +0000 (12:04 +0200)
Some of the code already present in the hsr_ping.sh test program can be
moved to a separate script file, so it can be reused by other HSR
functionality (like HSR-SAN) tests.

Signed-off-by: Lukasz Majewski <lukma@denx.de>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
tools/testing/selftests/net/hsr/Makefile
tools/testing/selftests/net/hsr/hsr_common.sh [new file with mode: 0644]
tools/testing/selftests/net/hsr/hsr_ping.sh

index 92c1d9d080cd57144aa14681526ec183fc72312e..c782ad19e01179ee2c92e7710acdc112b21f72c1 100644 (file)
@@ -3,5 +3,6 @@
 top_srcdir = ../../../../..
 
 TEST_PROGS := hsr_ping.sh
+TEST_FILES += hsr_common.sh
 
 include ../../lib.mk
diff --git a/tools/testing/selftests/net/hsr/hsr_common.sh b/tools/testing/selftests/net/hsr/hsr_common.sh
new file mode 100644 (file)
index 0000000..be4ad07
--- /dev/null
@@ -0,0 +1,83 @@
+# SPDX-License-Identifier: GPL-2.0
+# Common code for HSR testing scripts
+
+source ../lib.sh
+ret=0
+ksft_skip=4
+
+# $1: IP address
+is_v6()
+{
+       [ -z "${1##*:*}" ]
+}
+
+do_ping()
+{
+       local netns="$1"
+       local connect_addr="$2"
+       local ping_args="-q -c 2"
+
+       if is_v6 "${connect_addr}"; then
+               $ipv6 || return 0
+               ping_args="${ping_args} -6"
+       fi
+
+       ip netns exec ${netns} ping ${ping_args} $connect_addr >/dev/null
+       if [ $? -ne 0 ] ; then
+               echo "$netns -> $connect_addr connectivity [ FAIL ]" 1>&2
+               ret=1
+               return 1
+       fi
+
+       return 0
+}
+
+do_ping_long()
+{
+       local netns="$1"
+       local connect_addr="$2"
+       local ping_args="-q -c 10"
+
+       if is_v6 "${connect_addr}"; then
+               $ipv6 || return 0
+               ping_args="${ping_args} -6"
+       fi
+
+       OUT="$(LANG=C ip netns exec ${netns} ping ${ping_args} $connect_addr | grep received)"
+       if [ $? -ne 0 ] ; then
+               echo "$netns -> $connect_addr ping [ FAIL ]" 1>&2
+               ret=1
+               return 1
+       fi
+
+       VAL="$(echo $OUT | cut -d' ' -f1-8)"
+       if [ "$VAL" != "10 packets transmitted, 10 received, 0% packet loss," ]
+       then
+               echo "$netns -> $connect_addr ping TEST [ FAIL ]"
+               echo "Expect to send and receive 10 packets and no duplicates."
+               echo "Full message: ${OUT}."
+               ret=1
+               return 1
+       fi
+
+       return 0
+}
+
+stop_if_error()
+{
+       local msg="$1"
+
+       if [ ${ret} -ne 0 ]; then
+               echo "FAIL: ${msg}" 1>&2
+               exit ${ret}
+       fi
+}
+
+check_prerequisites()
+{
+       ip -Version > /dev/null 2>&1
+       if [ $? -ne 0 ];then
+               echo "SKIP: Could not run test without ip tool"
+               exit $ksft_skip
+       fi
+}
index bd7c4b8f07b8678e8723ce8d0a92b18ffbbd73ff..790294c8af83271d0962e8ddecfc132449bc89fb 100755 (executable)
@@ -1,11 +1,10 @@
 #!/bin/bash
 # SPDX-License-Identifier: GPL-2.0
 
-source ../lib.sh
-ret=0
-ksft_skip=4
 ipv6=true
 
+source ./hsr_common.sh
+
 optstring="h4"
 usage() {
        echo "Usage: $0 [OPTION]"
@@ -28,76 +27,6 @@ while getopts "$optstring" option;do
 esac
 done
 
-setup_ns ns1 ns2 ns3
-
-# $1: IP address
-is_v6()
-{
-       [ -z "${1##*:*}" ]
-}
-
-do_ping()
-{
-       local netns="$1"
-       local connect_addr="$2"
-       local ping_args="-q -c 2"
-
-       if is_v6 "${connect_addr}"; then
-               $ipv6 || return 0
-               ping_args="${ping_args} -6"
-       fi
-
-       ip netns exec ${netns} ping ${ping_args} $connect_addr >/dev/null
-       if [ $? -ne 0 ] ; then
-               echo "$netns -> $connect_addr connectivity [ FAIL ]" 1>&2
-               ret=1
-               return 1
-       fi
-
-       return 0
-}
-
-do_ping_long()
-{
-       local netns="$1"
-       local connect_addr="$2"
-       local ping_args="-q -c 10"
-
-       if is_v6 "${connect_addr}"; then
-               $ipv6 || return 0
-               ping_args="${ping_args} -6"
-       fi
-
-       OUT="$(LANG=C ip netns exec ${netns} ping ${ping_args} $connect_addr | grep received)"
-       if [ $? -ne 0 ] ; then
-               echo "$netns -> $connect_addr ping [ FAIL ]" 1>&2
-               ret=1
-               return 1
-       fi
-
-       VAL="$(echo $OUT | cut -d' ' -f1-8)"
-       if [ "$VAL" != "10 packets transmitted, 10 received, 0% packet loss," ]
-       then
-               echo "$netns -> $connect_addr ping TEST [ FAIL ]"
-               echo "Expect to send and receive 10 packets and no duplicates."
-               echo "Full message: ${OUT}."
-               ret=1
-               return 1
-       fi
-
-       return 0
-}
-
-stop_if_error()
-{
-       local msg="$1"
-
-       if [ ${ret} -ne 0 ]; then
-               echo "FAIL: ${msg}" 1>&2
-               exit ${ret}
-       fi
-}
-
 do_complete_ping_test()
 {
        echo "INFO: Initial validation ping."
@@ -237,11 +166,8 @@ setup_hsr_interfaces()
        ip -net "$ns3" link set hsr3 up
 }
 
-ip -Version > /dev/null 2>&1
-if [ $? -ne 0 ];then
-       echo "SKIP: Could not run test without ip tool"
-       exit $ksft_skip
-fi
+check_prerequisites
+setup_ns ns1 ns2 ns3
 
 trap cleanup_all_ns EXIT