]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: (lsns) verify NETNSID column for namespaces with no process running
authorMasatake YAMATO <yamato@redhat.com>
Fri, 7 Mar 2025 22:27:31 +0000 (07:27 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Fri, 7 Mar 2025 23:05:11 +0000 (08:05 +0900)
This case tests the two changes about lsns:

- make "-Q NETNSID ..." work even if NETNSID column is not enabled, and
- show NETNSID for namespaces with no process running

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
tests/expected/lsns/netnsid-for-persistent-namespaces-NETNSID [new file with mode: 0644]
tests/expected/lsns/netnsid-for-persistent-namespaces-NSFS [new file with mode: 0644]
tests/expected/lsns/netnsid-for-persistent-namespaces-TYPE [new file with mode: 0644]
tests/functions.sh
tests/ts/lsns/netnsid
tests/ts/lsns/netnsid-for-persistent-namespaces [new file with mode: 0755]

diff --git a/tests/expected/lsns/netnsid-for-persistent-namespaces-NETNSID b/tests/expected/lsns/netnsid-for-persistent-namespaces-NETNSID
new file mode 100644 (file)
index 0000000..d86bac9
--- /dev/null
@@ -0,0 +1 @@
+OK
diff --git a/tests/expected/lsns/netnsid-for-persistent-namespaces-NSFS b/tests/expected/lsns/netnsid-for-persistent-namespaces-NSFS
new file mode 100644 (file)
index 0000000..d86bac9
--- /dev/null
@@ -0,0 +1 @@
+OK
diff --git a/tests/expected/lsns/netnsid-for-persistent-namespaces-TYPE b/tests/expected/lsns/netnsid-for-persistent-namespaces-TYPE
new file mode 100644 (file)
index 0000000..d86bac9
--- /dev/null
@@ -0,0 +1 @@
+OK
index 1d7a425f692286aad5492621d22ab8b3acaea925..d6bc84cbb80e6d6814ff65201598c26a46c4d2fc 100644 (file)
@@ -1227,3 +1227,7 @@ function ts_check_ipv6 {
                ts_skip "IPv6 is not supported"
        fi
 }
+
+function ts_skip_netns {
+       grep -q '#define HAVE_LINUX_NET_NAMESPACE_H' ${top_builddir}/config.h || ts_skip "no netns support"
+}
index 80ccd9b5437c28de81e006261d08d63a5357054a..62e6a96668bd5c9f362c2434a73ffc71eecf16e5 100755 (executable)
@@ -28,7 +28,7 @@ ts_check_prog "mkfifo"
 ts_check_prog "sed"
 ts_skip_nonroot
 
-grep -q '#define HAVE_LINUX_NET_NAMESPACE_H' ${top_builddir}/config.h || ts_skip "no netns support"
+ts_skip_netns
 ts_cd "$TS_OUTDIR"
 
 vetha=lsns-vetha
diff --git a/tests/ts/lsns/netnsid-for-persistent-namespaces b/tests/ts/lsns/netnsid-for-persistent-namespaces
new file mode 100755 (executable)
index 0000000..232fd6f
--- /dev/null
@@ -0,0 +1,89 @@
+#!/bin/bash
+#
+# Copyright (C) 2025 Masatake YAMATO <yamato@redhat.com>
+#
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="NETNSID of a namespace where no process belongs to"
+
+. "$TS_TOPDIR"/functions.sh
+ts_init "$*"
+
+ts_check_test_command "$TS_CMD_LSNS"
+ts_check_prog "ip"
+ts_skip_nonroot
+
+ts_skip_netns
+ts_cd "$TS_OUTDIR"
+
+NETNSID=$$
+NS=LSNS-TEST-NETNSID-NS-FOR-PN-"$NETNSID"
+NULL=/dev/null
+LOG=/dev/null #/root/foo.log
+
+function cleanup {
+    ip netns delete "$NS" 2> $NULL || :
+}
+
+echo "==Cleanup" >> $LOG
+cleanup
+
+echo "==Netns ADD" >> $LOG
+if ip netns add "$NS" &&
+       ip netns set "$NS" "$NETNSID"; then
+    echo "===Netns EXEC" >> $LOG
+else
+    cleanup
+    ts_skip "failed to initialize"
+fi
+
+function netnsid_subtest {
+    local column=$1
+    local eval_func=eval_"$column"
+    local O
+    
+    ts_init_subtest "$1"
+    if O=$($TS_CMD_LSNS --raw --noheadings -Q "NETNSID == '$NETNSID'" -o $1); then
+       if "$eval_func" "$O"; then
+           echo OK
+       else
+           echo ERROR
+           echo "$O"
+       fi
+    else
+       echo ERROR $?
+    fi > "$TS_OUTPUT" 2>&1
+    ts_finalize_subtest
+}
+
+function eval_NETNSID {
+    [[ "$1" == "$NETNSID" ]]
+}
+
+function eval_TYPE {
+    [[ "$1" == "net" ]]
+}
+
+function eval_NSFS {
+    [[ ${1##*/} == "$NS" ]]
+}
+
+netnsid_subtest NETNSID
+netnsid_subtest TYPE
+netnsid_subtest NSFS
+
+cleanup
+ts_finalize
+