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

Matthieu Baerts (NGI0) posted 9 patches 3 months, 3 weeks ago
There is a newer version of this series
[PATCH mptcp-next v5 7/9] selftests: net: lib: rename ns in setup_ns
Posted by Matthieu Baerts (NGI0) 3 months, 3 weeks ago
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 --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[@]}")
 }

-- 
2.43.0