From: Masatake YAMATO Date: Fri, 7 Mar 2025 22:27:31 +0000 (+0900) Subject: tests: (lsns) verify NETNSID column for namespaces with no process running X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=18d585b59900e98511024a46a92ab6d7958613d4;p=thirdparty%2Futil-linux.git tests: (lsns) verify NETNSID column for namespaces with no process running 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 --- diff --git a/tests/expected/lsns/netnsid-for-persistent-namespaces-NETNSID b/tests/expected/lsns/netnsid-for-persistent-namespaces-NETNSID new file mode 100644 index 000000000..d86bac9de --- /dev/null +++ b/tests/expected/lsns/netnsid-for-persistent-namespaces-NETNSID @@ -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 index 000000000..d86bac9de --- /dev/null +++ b/tests/expected/lsns/netnsid-for-persistent-namespaces-NSFS @@ -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 index 000000000..d86bac9de --- /dev/null +++ b/tests/expected/lsns/netnsid-for-persistent-namespaces-TYPE @@ -0,0 +1 @@ +OK diff --git a/tests/functions.sh b/tests/functions.sh index 1d7a425f6..d6bc84cbb 100644 --- a/tests/functions.sh +++ b/tests/functions.sh @@ -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" +} diff --git a/tests/ts/lsns/netnsid b/tests/ts/lsns/netnsid index 80ccd9b54..62e6a9666 100755 --- a/tests/ts/lsns/netnsid +++ b/tests/ts/lsns/netnsid @@ -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 index 000000000..232fd6fc4 --- /dev/null +++ b/tests/ts/lsns/netnsid-for-persistent-namespaces @@ -0,0 +1,89 @@ +#!/bin/bash +# +# Copyright (C) 2025 Masatake YAMATO +# +# 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 +