Skip to content

Commit 7932217

Browse files
matttbekuba-moo
authored andcommitted
selftests: net: lib: avoid error removing empty netns name
If there is an error to create the first netns with 'setup_ns()', 'cleanup_ns()' will be called with an empty string as first parameter. The consequences is that 'cleanup_ns()' will try to delete an invalid netns, and wait 20 seconds if the netns list is empty. Instead of just checking if the name is not empty, convert the string separated by spaces to an array. Manipulating the array is cleaner, and calling 'cleanup_ns()' with an empty array will be a no-op. Fixes: 25ae948 ("selftests/net: add lib.sh") Cc: stable@vger.kernel.org Acked-by: Geliang Tang <geliang@kernel.org> Signed-off-by: Matthieu Baerts (NGI0) <matttbe@kernel.org> Reviewed-by: Petr Machata <petrm@nvidia.com> Reviewed-by: Hangbin Liu <liuhangbin@gmail.com> Link: https://lore.kernel.org/r/20240605-upstream-net-20240605-selftests-net-lib-fixes-v1-2-b3afadd368c9@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 41b02ea commit 7932217

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

  • tools/testing/selftests/net

tools/testing/selftests/net/lib.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ksft_xfail=2
1515
ksft_skip=4
1616

1717
# namespace list created by setup_ns
18-
NS_LIST=""
18+
NS_LIST=()
1919

2020
##############################################################################
2121
# Helpers
@@ -137,6 +137,7 @@ cleanup_ns()
137137
fi
138138

139139
for ns in "$@"; do
140+
[ -z "${ns}" ] && continue
140141
ip netns delete "${ns}" &> /dev/null
141142
if ! busywait $BUSYWAIT_TIMEOUT ip netns list \| grep -vq "^$ns$" &> /dev/null; then
142143
echo "Warn: Failed to remove namespace $ns"
@@ -150,7 +151,7 @@ cleanup_ns()
150151

151152
cleanup_all_ns()
152153
{
153-
cleanup_ns $NS_LIST
154+
cleanup_ns "${NS_LIST[@]}"
154155
}
155156

156157
# setup netns with given names as prefix. e.g
@@ -159,7 +160,7 @@ setup_ns()
159160
{
160161
local ns=""
161162
local ns_name=""
162-
local ns_list=""
163+
local ns_list=()
163164
local ns_exist=
164165
for ns_name in "$@"; do
165166
# Some test may setup/remove same netns multi times
@@ -175,13 +176,13 @@ setup_ns()
175176

176177
if ! ip netns add "$ns"; then
177178
echo "Failed to create namespace $ns_name"
178-
cleanup_ns "$ns_list"
179+
cleanup_ns "${ns_list[@]}"
179180
return $ksft_skip
180181
fi
181182
ip -n "$ns" link set lo up
182-
! $ns_exist && ns_list="$ns_list $ns"
183+
! $ns_exist && ns_list+=("$ns")
183184
done
184-
NS_LIST="$NS_LIST $ns_list"
185+
NS_LIST+=("${ns_list[@]}")
185186
}
186187

187188
tc_rule_stats_get()

0 commit comments

Comments
 (0)