summaryrefslogtreecommitdiffstats
path: root/tests/ts/lsns
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ts/lsns')
-rwxr-xr-xtests/ts/lsns/ioctl_ns113
-rwxr-xr-xtests/ts/lsns/netnsid96
-rwxr-xr-xtests/ts/lsns/nsfs94
3 files changed, 303 insertions, 0 deletions
diff --git a/tests/ts/lsns/ioctl_ns b/tests/ts/lsns/ioctl_ns
new file mode 100755
index 0000000..fa626bf
--- /dev/null
+++ b/tests/ts/lsns/ioctl_ns
@@ -0,0 +1,113 @@
+#!/bin/bash
+#
+# Copyright (C) 2021 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="ownership and hierarchy"
+
+. $TS_TOPDIR/functions.sh
+ts_init "$*"
+
+# ts_skip_nonroot
+grep -q '#define HAVE_LINUX_NSFS_H' ${top_builddir}/config.h || ts_skip "no ioctl_ns support"
+
+[ -r /proc/self/ns/user ] || ts_skip "no USER namespace kernel support"
+[ -r /proc/self/ns/pid ] || ts_skip "no PID namespace kernel support"
+
+ts_check_test_command "$TS_CMD_LSNS"
+ts_check_test_command "$TS_CMD_UNSHARE"
+ts_check_prog "stat"
+ts_check_prog "mkfifo"
+ts_check_prog "touch"
+ts_check_prog "uniq"
+
+$TS_CMD_UNSHARE --user --pid --mount-proc --fork true &> /dev/null || ts_skip "no namespace support"
+
+ts_cd "$TS_OUTDIR"
+
+# The parent process receives namespaces ids via FIFO_DATA from bash
+# run by unshare. The parent can wait till the bash process provides
+# data.
+FIFO_DATA=$TS_OUTDIR/FIFO_DATA-HIERARCHY
+
+# The bash process run by unshare waits with this fifo till the parent
+# process exits. As a result, namespaecs referred by the bash
+# process can be alive till the parent process exits.
+FIFO_WAIT=$TS_OUTDIR/FIFO_WAIT-HIERARCHY
+
+function cleanup {
+ rm -f $FIFO_DATA
+ rm -f $FIFO_WAIT
+}
+
+function init {
+ cleanup
+ mkfifo $FIFO_DATA
+ mkfifo $FIFO_WAIT
+}
+
+init
+(
+ exec 4> $FIFO_WAIT
+ my_userns=$(stat -c %i -L /proc/self/ns/user)
+ my_pidns=$(stat -c %i -L /proc/self/ns/pid)
+ read child_userns < $FIFO_DATA
+ read child_pidns < $FIFO_DATA
+
+ expected="$child_userns $my_userns $my_userns"
+ actual=$("$TS_CMD_LSNS" -t user -n -o NS,PNS,ONS "$child_userns" | uniq)
+ test "$expected" = "$actual"
+ RESULT=$?
+ if [ $RESULT -ne 0 ]; then
+ echo
+ echo userns expected: "$expected"
+ echo userns actual: "$actual"
+ cleanup
+ exit $RESULT
+ fi
+
+ expected="$child_pidns $my_pidns $child_userns"
+ actual=$("$TS_CMD_LSNS" -t pid -n -o NS,PNS,ONS "$child_pidns" | uniq)
+ test "$expected" = "$actual"
+ RESULT=$?
+ if [ $RESULT -ne 0 ]; then
+ echo
+ echo pidns expected: "$expected"
+ echo pidns actual: "$actual"
+ cleanup
+ fi
+ exit $RESULT
+) &
+mainpid=$!
+(
+ exec 4< $FIFO_WAIT
+ $TS_CMD_UNSHARE --user --pid --mount-proc --fork bash -s > $FIFO_DATA <<'EOF'
+ stat -c %i -L /proc/self/ns/user
+ stat -c %i -L /proc/self/ns/pid
+ # Wait till FIFO_WAIT is clsoed.
+ read -u 4
+EOF
+) &
+subpid=$!
+
+wait $subpid
+wait $mainpid
+
+RESULT=$?
+echo $RESULT >> $TS_OUTPUT
+
+cleanup
+ts_finalize
diff --git a/tests/ts/lsns/netnsid b/tests/ts/lsns/netnsid
new file mode 100755
index 0000000..9d04f28
--- /dev/null
+++ b/tests/ts/lsns/netnsid
@@ -0,0 +1,96 @@
+#!/bin/bash
+#
+# Copyright (C) 2017 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 compare to ip-link"
+
+. $TS_TOPDIR/functions.sh
+ts_init "$*"
+
+ts_check_test_command "$TS_CMD_LSNS"
+ts_check_prog "ip"
+ts_check_prog "dd"
+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_cd "$TS_OUTDIR"
+
+vetha=lsns-vetha
+vethb=lsns-vethb
+NS=LSNS-TEST-NETNSID-NS
+FIFO=$TS_OUTDIR/FIFO-NETNSID
+NULL=/dev/null
+LOG=/dev/null #/root/foo.log
+
+function cleanup {
+ ip link delete $vetha 2> $NULL || :
+ ip netns delete $NS 2> $NULL || :
+ rm -f $FIFO
+}
+
+echo "==Cleanup" >> $LOG
+cleanup
+
+echo "==Create FIFO" >> $LOG
+mkfifo $FIFO
+
+echo "==Netns ADD" >> $LOG
+if ip netns add $NS &&
+ ip link add name $vetha type veth peer name $vethb &&
+ ip link set $vethb netns $NS; then
+ echo "===Netns EXEC" >> $LOG
+ ip netns exec $NS dd if=$FIFO bs=1 count=2 of=$NULL 2> $NULL &
+ PID=$!
+ echo "====PID=$PID" >> $LOG
+else
+ cleanup
+ ts_skip "failed to initialize"
+fi
+{
+ echo "==Write to FIFO" >> $LOG
+ dd if=/dev/zero bs=1 count=1 2> $NULL
+ {
+ echo "===IP output" >> $LOG
+ ip -o link show dev $vetha >> $LOG
+
+ IP_ID=$(ip -o link show dev $vetha | sed -ne 's/.* *link-netnsid *\([0-9]*\)/\1/p')
+ echo "====ip show: IP_ID=$IP_ID" >> $LOG
+
+ if [ "x$IP_ID" = "x" ]; then
+ echo "===IP output list id" >> $LOG
+ ip netns list-id >> $LOG
+
+ IP_ID=$(ip netns list-id | awk "/name: $NS/ { print \$2 }")
+ echo "====ip list-id: IP_ID=$IP_ID" >> $LOG
+ fi
+
+ echo "===LSNS output" >> $LOG
+ $TS_CMD_LSNS -o+NETNSID,NSFS --type net >> $LOG
+
+ LSNS_ID=$($TS_CMD_LSNS -n -o NETNSID --type net --task $PID | { read VAL; echo $VAL; } )
+ echo "===LSNS_ID=$LSNS_ID" >> $LOG
+ }
+ dd if=/dev/zero bs=1 count=1 2> $NULL
+} > $FIFO
+
+test "$IP_ID" = "$LSNS_ID"
+echo $? >> $TS_OUTPUT
+
+cleanup
+ts_finalize
diff --git a/tests/ts/lsns/nsfs b/tests/ts/lsns/nsfs
new file mode 100755
index 0000000..70cab2b
--- /dev/null
+++ b/tests/ts/lsns/nsfs
@@ -0,0 +1,94 @@
+#!/bin/bash
+#
+# Copyright (C) 2017 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="NSFS for ip-netns-add"
+
+. $TS_TOPDIR/functions.sh
+ts_init "$*"
+
+ts_skip_nonroot
+
+ts_check_test_command "$TS_CMD_LSNS"
+ts_check_test_command "$TS_CMD_MOUNT"
+ts_check_test_command "$TS_CMD_UMOUNT"
+ts_check_prog "ip"
+ts_check_prog "dd"
+ts_check_prog "touch"
+ts_check_prog "mkfifo"
+ts_check_prog "sed"
+
+grep -q '#define HAVE_LINUX_NET_NAMESPACE_H' ${top_builddir}/config.h || ts_skip "no netns support"
+
+ts_cd "$TS_OUTDIR"
+
+NAME1=ABC
+NAME2=XYZ
+PATH1=/run/netns/$NAME1
+PATH2=$TS_OUTDIR/$NAME2
+FIFO=$TS_OUTDIR/FIFO-NSFS
+NULL=/dev/null
+
+function cleanup {
+ rm -f $FIFO
+ $TS_CMD_UMOUNT $PATH2 2> /dev/null || :
+ rm -f $PATH2
+ ip netns delete $NAME1 2> /dev/null || :
+}
+
+cleanup
+mkfifo $FIFO
+
+if ip netns add $NAME1 &&
+ touch ${PATH2} &&
+ $TS_CMD_MOUNT -o bind ${PATH1} ${PATH2}; then
+ ip netns exec $NAME1 dd if=$FIFO bs=1 count=2 of=$NULL 2> $NULL &
+ PID=$!
+else
+ cleanup
+ ts_skip "failed to initialize"
+fi
+{
+ dd if=/dev/zero bs=1 count=1 2> $NULL
+ LSNS_FULL=$($TS_CMD_LSNS --type net)
+ NSFS_NAMES_MLINES=$($TS_CMD_LSNS -n -o NSFS --type net --task $PID | { while read VAL; do echo $VAL; done; } )
+ NSFS_NAMES_1LINE=$($TS_CMD_LSNS -n -o NSFS --nowrap --type net --task $PID | { while read VAL; do echo $VAL; done; } )
+ dd if=/dev/zero bs=1 count=1 2> $NULL
+} > $FIFO
+
+test "$NSFS_NAMES_MLINES" = "$PATH1
+$PATH2" && test "$NSFS_NAMES_1LINE" = "$PATH1,$PATH2"
+
+RESULT=$?
+echo $RESULT >> $TS_OUTPUT
+
+if [ $RESULT -ne 0 ]; then
+ echo
+ echo "**Full-report**"
+ echo "$LSNS_FULL"
+ echo "**Paths**"
+ echo "PATH1: $PATH1"
+ echo "PATH2: $PATH2"
+ echo "**Multi-line:**"
+ echo "$NSFS_NAMES_MLINES"
+ echo "**One-line:**"
+ echo "$NSFS_NAMES_1LINE"
+ echo "****"
+fi
+
+cleanup
+ts_finalize