diff mbox series

[mptcp-next,v5,7/9] selftests: net: lib: rename ns in setup_ns

Message ID 20240524-selftests-net-lib-fixes-v5-7-b9e0968571a3@kernel.org (mailing list archive)
State Superseded, archived
Headers show
Series use helpers in lib.sh and net_helpers.sh | expand

Checks

Context Check Description
matttbe/KVM_Validation__normal success Success! ✅
matttbe/KVM_Validation__debug warning Unstable: 1 failed test(s): packetdrill_fastopen
matttbe/KVM_Validation__btf__only_bpftest_all_ success Success! ✅
matttbe/build success Build and static analysis OK
matttbe/checkpatch success total: 0 errors, 0 warnings, 0 checks, 40 lines checked
matttbe/shellcheck success MPTCP selftests files have not been modified

Commit Message

Matthieu Baerts (NGI0) May 24, 2024, 3:13 p.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

The helpers setup_ns() doesn't work when a net namespace named "ns" is
passed to them.

For example, in net/mptcp/diag.sh, the name of the namespace is "ns".
If "setup_ns ns" is used in it, diag.sh fails with errors:

  Invalid netns name "./mptcp_connect"
  Cannot open network namespace "10000": No such file or directory
  Cannot open network namespace "10000": No such file or directory

That is because "ns" is also a local variable in setup_ns, and it will
not set the value for the global variable that has been giving in
argument. To solve this, this patch renames the local variable "ns" as
"_ns", which is more unlikely to conflict with existing variables. "_ns"
name appears to be unused in the net selftests.

Also a check has been added to setup_ns: if "_ns" is passed, setup_ns()
helper will exit with an error.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 tools/testing/selftests/net/lib.sh | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/net/lib.sh b/tools/testing/selftests/net/lib.sh
index 114b927fee25..b883289ec4a1 100644
--- a/tools/testing/selftests/net/lib.sh
+++ b/tools/testing/selftests/net/lib.sh
@@ -167,26 +167,32 @@  cleanup_all_ns()
 # setup_ns local remote
 setup_ns()
 {
-	local ns=""
+	local _ns=""
 	local ns_name=""
 	local ns_list=()
 	for ns_name in "$@"; do
-		# Some test may setup/remove same netns multi times
-		if [ -z "${!ns_name}" ]; then
-			ns="${ns_name,,}-$(mktemp -u XXXXXX)"
-			eval "${ns_name}=${ns}"
-		else
-			ns="${!ns_name}"
-			cleanup_ns "$ns"
+		if [ "${ns_name}" = "_ns" ]; then
+			echo "Failed to setup namespace '${ns_name}': invalid name"
+			cleanup_ns "${ns_list[@]}"
+			exit $ksft_fail
 		fi
 
-		if ! ip netns add "$ns"; then
+		# Some test may setup/remove same netns multi times
+		if [ -z "${!ns_name}" ]; then
+			_ns="${ns_name,,}-$(mktemp -u XXXXXX)"
+			eval "${ns_name}=${_ns}"
+		else
+			_ns="${!ns_name}"
+			cleanup_ns "${_ns}"
+		fi
+
+		if ! ip netns add "${_ns}"; then
 			echo "Failed to create namespace $ns_name"
 			cleanup_ns "${ns_list[@]}"
 			return $ksft_skip
 		fi
-		ip -n "$ns" link set lo up
-		ns_list+=("$ns")
+		ip -n "${_ns}" link set lo up
+		ns_list+=("${_ns}")
 	done
 	NS_LIST+=("${ns_list[@]}")
 }