[PATCH ipset 5/7] tests: setlist_ns.sh: use local ipset binary and don't rely on userns

Florian Westphal <[email protected]>
Newsgroups gmane.comp.security.firewalls.netfilter.devel
Message-ID <[email protected]>
The script runs command in network+user namespace with remapped uid0.

In case this fails (e.g. because user namespaces are disabled), we
should re-try with plain "-n" before giving up and returning an error.

Also, in case 'ipset' isn't in $PATH, this fails.
Let runtest.sh export the ipset binary to use and refer to that.

While at it, parallelize the test and also run it 124 times, not just
4 times - missing $() resulted in 'for x in a b c d ..'.

Signed-off-by: Florian Westphal <[email protected]>

x
---
 tests/runtest.sh    |  1 +
 tests/setlist_ns.sh | 29 +++++++++++++++++++++++++----
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/tests/runtest.sh b/tests/runtest.sh
index 766335fda768..ba4683f59e5d 100755
--- a/tests/runtest.sh
+++ b/tests/runtest.sh
@@ -22,6 +22,7 @@ tests="$tests comment setlist restore"
 if [ "$IPSET_UNSHARED" = "" ]; then
 	# Re-execute in new network namespace
 	export IPSET_UNSHARED="yes"
+	export IPSET_BIN="$ipset"
 	sysctl net.netfilter.nf_log_all_netns=1
 	exec unshare -n -- "$0" "$@"
 fi
diff --git a/tests/setlist_ns.sh b/tests/setlist_ns.sh
index 9e47d65e95dc..67232acdd906 100755
--- a/tests/setlist_ns.sh
+++ b/tests/setlist_ns.sh
@@ -2,21 +2,42 @@
 
 set -e
 
+[ -z "$IPSET_BIN" ] && IPSET_BIN=ipset
+
 CMD=$(cat <<EOF
 for x in \$(seq 0 127); do
     echo "create h\$x hash:ip"
     echo "create l\$x list:set timeout 10 comment"
-done | ipset restore
+done | $IPSET_BIN restore
 for x in \$(seq 0 127); do
     for y in \$(seq 0 127); do
         echo "add l\$x h\$y timeout 1000 comment \"l\$x h\$y\""
     done
-done | ipset restore
+done | $IPSET_BIN restore
 # Wait for GC
 sleep 15
 EOF
 )
 
-for x in seq 0 123; do
-    unshare -Urn bash -c "$CMD"
+# First try with user namespaces and remapped-root.
+options="-Urn"
+if ! unshare "$options" bash -c "$CMD"; then
+	# Doesn't work - try with plain network namespaces.
+	if unshare -n bash -c "$CMD"; then
+		# "-Ur" unsupported
+		options="-n"
+	else
+		# Either network namespaces are not
+		# supported at all or $CMD is failing.
+		exit 1
+	fi
+fi
+
+parallel=16
+for x in $(seq 1 123); do
+    unshare "$options" bash -c "$CMD" &
+
+    [ $((x % $parallel)) -eq 0 ] && wait
 done
+
+wait
-- 
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.