selftests: mptcp: add mptcp_lib_check_tools helper
authorGeliang Tang <tanggeliang@kylinos.cn>
Wed, 6 Mar 2024 09:42:51 +0000 (10:42 +0100)
committerJakub Kicinski <kuba@kernel.org>
Fri, 8 Mar 2024 05:06:51 +0000 (21:06 -0800)
This patch exports check_tools() helper from mptcp_join.sh into
mptcp_lib.sh as a public one mptcp_lib_check_tools(). The arguments
"ip", "ss", "iptables" and "ip6tables" are passed into this helper
to indicate whether to check ip tool, ss tool, iptables and ip6tables
tools.

This helper can be used in every scripts.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
Reviewed-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
Signed-off-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
Link: https://lore.kernel.org/r/20240306-upstream-net-next-20240304-selftests-mptcp-shared-code-shellcheck-v2-2-bc79e6e5e6a0@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/testing/selftests/net/mptcp/diag.sh
tools/testing/selftests/net/mptcp/mptcp_connect.sh
tools/testing/selftests/net/mptcp/mptcp_join.sh
tools/testing/selftests/net/mptcp/mptcp_lib.sh
tools/testing/selftests/net/mptcp/mptcp_sockopt.sh
tools/testing/selftests/net/mptcp/pm_netlink.sh
tools/testing/selftests/net/mptcp/simult_flows.sh
tools/testing/selftests/net/mptcp/userspace_pm.sh

index cc8bdf95b10408b265626fd54a4e2382edf3ba4c..16c115709a3795c265dce007a5314f2cbcfd88be 100755 (executable)
@@ -34,17 +34,7 @@ cleanup()
 }
 
 mptcp_lib_check_mptcp
-
-ip -Version > /dev/null 2>&1
-if [ $? -ne 0 ];then
-       echo "SKIP: Could not run test without ip tool"
-       exit $ksft_skip
-fi
-ss -h | grep -q MPTCP
-if [ $? -ne 0 ];then
-       echo "SKIP: ss tool does not support MPTCP"
-       exit $ksft_skip
-fi
+mptcp_lib_check_tools ip ss
 
 get_msk_inuse()
 {
index 7898d62fce0b5b3e7460a251274924cb59b97079..ea52110c3fbc93245f0c4557db6a17fb029407b5 100755 (executable)
@@ -147,12 +147,7 @@ cleanup()
 
 mptcp_lib_check_mptcp
 mptcp_lib_check_kallsyms
-
-ip -Version > /dev/null 2>&1
-if [ $? -ne 0 ];then
-       echo "SKIP: Could not run test without ip tool"
-       exit $ksft_skip
-fi
+mptcp_lib_check_tools ip
 
 sin=$(mktemp)
 sout=$(mktemp)
index 6a4af95cd4c33c48d83c667305084ec463ab1c10..aedc5698f26ac05ca106e097217f9674c4aaadb9 100755 (executable)
@@ -152,34 +152,12 @@ cleanup_partial()
        done
 }
 
-check_tools()
-{
-       mptcp_lib_check_mptcp
-       mptcp_lib_check_kallsyms
-
-       if ! ip -Version &> /dev/null; then
-               echo "SKIP: Could not run test without ip tool"
-               exit $ksft_skip
-       fi
-
-       if ! ss -h | grep -q MPTCP; then
-               echo "SKIP: ss tool does not support MPTCP"
-               exit $ksft_skip
-       fi
-
-       if ! "${iptables}" -V &> /dev/null; then
-               echo "SKIP: Could not run all tests without ${iptables} tool"
-               exit $ksft_skip
-       elif ! "${ip6tables}" -V &> /dev/null; then
-               echo "SKIP: Could not run all tests without ${ip6tables} tool"
-               exit $ksft_skip
-       fi
-}
-
 init() {
        init=1
 
-       check_tools
+       mptcp_lib_check_mptcp
+       mptcp_lib_check_kallsyms
+       mptcp_lib_check_tools ip ss "${iptables}" "${ip6tables}"
 
        sin=$(mktemp)
        sout=$(mktemp)
index 438f557aac90ce8f223dd3a55cc8ca04f825732c..b1fe354cfe960765b8d9b686186055d858bd85b7 100644 (file)
@@ -342,3 +342,34 @@ mptcp_lib_check_output() {
                return 1
        fi
 }
+
+mptcp_lib_check_tools() {
+       local tool
+
+       for tool in "${@}"; do
+               case "${tool}" in
+               "ip")
+                       if ! ip -Version &> /dev/null; then
+                               mptcp_lib_print_warn "SKIP: Could not run test without ip tool"
+                               exit ${KSFT_SKIP}
+                       fi
+                       ;;
+               "ss")
+                       if ! ss -h | grep -q MPTCP; then
+                               mptcp_lib_print_warn "SKIP: ss tool does not support MPTCP"
+                               exit ${KSFT_SKIP}
+                       fi
+                       ;;
+               "iptables"* | "ip6tables"*)
+                       if ! "${tool}" -V &> /dev/null; then
+                               mptcp_lib_print_warn "SKIP: Could not run all tests without ${tool}"
+                               exit ${KSFT_SKIP}
+                       fi
+                       ;;
+               *)
+                       mptcp_lib_print_err "Internal error: unsupported tool: ${tool}"
+                       exit ${KSFT_FAIL}
+                       ;;
+               esac
+       done
+}
index dac8e1fc7143ad94d12cf1a204b6ae5160679a18..fd7de1b3dc553b57cf018564d92c4d36fcc49094 100755 (executable)
@@ -89,20 +89,7 @@ cleanup()
 
 mptcp_lib_check_mptcp
 mptcp_lib_check_kallsyms
-
-ip -Version > /dev/null 2>&1
-if [ $? -ne 0 ];then
-       echo "SKIP: Could not run test without ip tool"
-       exit $ksft_skip
-fi
-
-if ! "${iptables}" -V &> /dev/null; then
-       echo "SKIP: Could not run all tests without ${iptables} tool"
-       exit $ksft_skip
-elif ! "${ip6tables}" -V &> /dev/null; then
-       echo "SKIP: Could not run all tests without ${ip6tables} tool"
-       exit $ksft_skip
-fi
+mptcp_lib_check_tools ip "${iptables}" "${ip6tables}"
 
 check_mark()
 {
index 705106d60db5a58f4f86fda57261187417aef1d9..1ec9d8622fc92d2e9e51d208206d7ff28adfcdde 100755 (executable)
@@ -36,12 +36,7 @@ cleanup()
 }
 
 mptcp_lib_check_mptcp
-
-ip -Version > /dev/null 2>&1
-if [ $? -ne 0 ];then
-       echo "SKIP: Could not run test without ip tool"
-       exit $ksft_skip
-fi
+mptcp_lib_check_tools ip
 
 trap cleanup EXIT
 
index ed0165c15a240a6bbf76e0e92712f522e5e7f320..dbbb13d1d74e3d482ad53d6e85d3ed0e2bfff6b1 100755 (executable)
@@ -43,12 +43,7 @@ cleanup()
 }
 
 mptcp_lib_check_mptcp
-
-ip -Version > /dev/null 2>&1
-if [ $? -ne 0 ];then
-       echo "SKIP: Could not run test without ip tool"
-       exit $ksft_skip
-fi
+mptcp_lib_check_tools ip
 
 #  "$ns1"              ns2                    ns3
 #     ns1eth1    ns2eth1   ns2eth3      ns3eth1
index 1b94a75604fee98788ba5792b384ca4870bdafbb..629fc5d0ecc5ee60eeab860048abf4789d845221 100755 (executable)
@@ -17,11 +17,7 @@ if ! mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
        echo "userspace pm tests are not supported by the kernel: SKIP"
        exit ${KSFT_SKIP}
 fi
-
-if ! ip -Version &> /dev/null; then
-       echo "SKIP: Cannot not run test without ip tool"
-       exit ${KSFT_SKIP}
-fi
+mptcp_lib_check_tools ip
 
 ANNOUNCED=6        # MPTCP_EVENT_ANNOUNCED
 REMOVED=7          # MPTCP_EVENT_REMOVED