All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] commands/unshare: Add new testcase to test unshare(1)
@ 2017-11-02  9:34 Xiao Yang
  2017-11-13  6:46 ` Xiao Yang
  2017-11-30 14:20 ` Cyril Hrubis
  0 siblings, 2 replies; 5+ messages in thread
From: Xiao Yang @ 2017-11-02  9:34 UTC (permalink / raw)
  To: ltp

Test unshare(1) command with some basic options.

Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com>
---
 runtest/commands                        |   1 +
 testcases/commands/unshare/Makefile     |  22 +++++
 testcases/commands/unshare/unshare01.sh | 150 ++++++++++++++++++++++++++++++++
 3 files changed, 173 insertions(+)
 create mode 100644 testcases/commands/unshare/Makefile
 create mode 100755 testcases/commands/unshare/unshare01.sh

diff --git a/runtest/commands b/runtest/commands
index 00ae0d2..92df3af 100644
--- a/runtest/commands
+++ b/runtest/commands
@@ -43,3 +43,4 @@ insmod01 insmod01.sh
 wc01 wc01.sh
 keyctl01 keyctl01.sh
 gdb01 gdb01.sh
+unshare01 unshare01.sh
diff --git a/testcases/commands/unshare/Makefile b/testcases/commands/unshare/Makefile
new file mode 100644
index 0000000..a175291
--- /dev/null
+++ b/testcases/commands/unshare/Makefile
@@ -0,0 +1,22 @@
+#
+# Copyright (c) 2017 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
+#
+# This program 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 program 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.
+#
+
+top_srcdir		?= ../../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+
+INSTALL_TARGETS		:= unshare01.sh
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/commands/unshare/unshare01.sh b/testcases/commands/unshare/unshare01.sh
new file mode 100755
index 0000000..ba379df
--- /dev/null
+++ b/testcases/commands/unshare/unshare01.sh
@@ -0,0 +1,150 @@
+#!/bin/sh
+#
+# Copyright (c) 2017 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
+#
+# This program 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 program 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.
+#
+# Test unshare command with some basic options.
+# 1) If we run unshare with "--user", UID in the newly created user namespace
+#    is set to 65534.
+# 2) If we run unshare with "--user", GID in the newly created user namespace
+#    is set to 65534.
+# 3) If we run with "--user --map-root-user", UID in the newly created user
+#    namespace is set to 0.
+# 4) If we run with "--user --map-root-user", GID in the newly created user
+#    is set to 0.
+# 5) If we run with "--mount", mount and unmount events do not propagate to
+#    its parent mount namespace.
+# 6) If we run with "--mount --propagation shared", mount and unmount events
+#    propagate to its parent mount namespace.
+# 7) If we run with "--user --map-root-user --mount", mount and unmount events
+#    do not propagate to its parent mount namespace.
+# 8) Even if we run with "--user --map-root-user --mount --propagation shared",
+#    mount and unmount events do not propagate to its parent mount namespace
+#    because the shared mount is reduced to a slave mount.
+#
+#    Please see the following URL for detailed information:
+#    http://man7.org/linux/man-pages/man7/user_namespaces.7.html
+#    http://man7.org/linux/man-pages/man7/mount_namespaces.7.html
+#
+
+TST_CNT=8
+TST_SETUP=setup
+TST_CLEANUP=cleanup
+TST_TESTFUNC=do_test
+TST_NEEDS_ROOT=1
+TST_NEEDS_TMPDIR=1
+TST_NEEDS_CMDS="unshare id mount umount"
+. tst_test.sh
+
+max_userns_path="/proc/sys/user/max_user_namespaces"
+max_mntns_path="/proc/sys/user/max_mnt_namespaces"
+max_userns_zero=0
+max_mntns_zero=0
+
+setup()
+{
+	# On some distributions(e.g RHEL7.4), the default value of
+	# max_user_namespaces or max_mnt_namespaces is set to 0.
+	# We need to change the default value to run unshare command.
+	if [ -f "${max_userns_path}" ]; then
+		if [ $(cat "${max_userns_path}") -eq 0 ]; then
+			echo 1024 > "${max_userns_path}"
+			max_userns_zero=1
+		fi
+	fi
+
+	if [ -f "${max_mntns_path}" ]; then
+		if [ $(cat "${max_mntns_path}") -eq 0 ]; then
+			echo 1024 > "${max_mntns_path}"
+			max_mntns_zero=1
+		fi
+	fi
+
+	mkdir -p test_A test_B
+	touch test_A/A test_B/B
+}
+
+cleanup()
+{
+	# Restore the default value to 0.
+	[ ${max_userns_zero} -eq 1 ] && echo 0 > "${max_userns_path}"
+	[ ${max_mntns_zero} -eq 1 ] && echo 0 > "${max_mntns_zero}"
+}
+
+unshare_test()
+{
+	local unshare_opts=$1
+	local verify_cmd=$2
+	local exp_result=$3
+
+	local unshare_cmd="unshare ${unshare_opts} ${verify_cmd}"
+
+	eval ${unshare_cmd} > temp 2>&1
+	if [ $? -ne 0 ]; then
+		# unrecognized option or invalid option is returned if the
+		# option is not supported by unshare command(e.g. RHEL6).
+		# Invalid argument or Operation not permitted is returned
+		# if the feature is not supported by kernel(e.g. RHEL7).
+		grep -q -E "unrecognized option|invalid option|Invalid argument|Operation not permitted" temp
+		if [ $? -eq 0 ]; then
+			tst_res TCONF "${unshare_cmd} not supported."
+		else
+			tst_res TFAIL "${unshare_cmd} failed."
+		fi
+		return
+	fi
+
+	if [[ "${verify_cmd}" =~ 'id' ]]; then
+		if [ $(cat temp) -ne ${exp_result} ]; then
+			tst_res TFAIL "${unshare_cmd} got wrong uid/gid"
+			return
+		fi
+	fi
+
+	if [[ "${verify_cmd}" =~ 'mount' ]]; then
+		if [ "${exp_result}" = "unmounted" ]; then
+			if ls test_B | grep -q 'A'; then
+				tst_res TFAIL "${unshare_cmd} got bind info"
+				umount test_B
+				return
+			fi
+		else
+			if ! ls test_B | grep -q 'A'; then
+				tst_res TFAIL "${unshare_cmd} did not get bind info"
+				return
+			fi
+			umount test_B
+		fi
+	fi
+
+	tst_res TPASS "${unshare_cmd} succeeded as expected"
+}
+
+do_test()
+{
+	case $1 in
+	1) unshare_test "--user" "id -u" "65534";;
+	2) unshare_test "--user" "id -g" "65534";;
+	3) unshare_test "--user --map-root-user" "id -u" "0";;
+	4) unshare_test "--user --map-root-user" "id -g" "0";;
+	5) unshare_test "--mount" "mount --bind test_A test_B" "unmounted";;
+	6) unshare_test "--mount --propagation shared" \
+			"mount --bind test_A test_B" "mounted";;
+	7) unshare_test "--user --map-root-user --mount" \
+			"mount --bind test_A test_B" "unmounted";;
+	8) unshare_test "--user --map-root-user --mount --propagation shared" \
+			"mount --bind test_A test_B" "unmounted";;
+	esac
+}
+
+tst_run
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH] commands/unshare: Add new testcase to test unshare(1)
  2017-11-02  9:34 [LTP] [PATCH] commands/unshare: Add new testcase to test unshare(1) Xiao Yang
@ 2017-11-13  6:46 ` Xiao Yang
  2017-11-30 14:20 ` Cyril Hrubis
  1 sibling, 0 replies; 5+ messages in thread
From: Xiao Yang @ 2017-11-13  6:46 UTC (permalink / raw)
  To: ltp

Hi,

Ping :-)

Thanks,
Xiao Yang
On 2017/11/02 17:34, Xiao Yang wrote:
> Test unshare(1) command with some basic options.
>
> Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com>
> ---
>  runtest/commands                        |   1 +
>  testcases/commands/unshare/Makefile     |  22 +++++
>  testcases/commands/unshare/unshare01.sh | 150 ++++++++++++++++++++++++++++++++
>  3 files changed, 173 insertions(+)
>  create mode 100644 testcases/commands/unshare/Makefile
>  create mode 100755 testcases/commands/unshare/unshare01.sh
>
> diff --git a/runtest/commands b/runtest/commands
> index 00ae0d2..92df3af 100644
> --- a/runtest/commands
> +++ b/runtest/commands
> @@ -43,3 +43,4 @@ insmod01 insmod01.sh
>  wc01 wc01.sh
>  keyctl01 keyctl01.sh
>  gdb01 gdb01.sh
> +unshare01 unshare01.sh
> diff --git a/testcases/commands/unshare/Makefile b/testcases/commands/unshare/Makefile
> new file mode 100644
> index 0000000..a175291
> --- /dev/null
> +++ b/testcases/commands/unshare/Makefile
> @@ -0,0 +1,22 @@
> +#
> +# Copyright (c) 2017 FUJITSU LIMITED. All rights reserved.
> +# Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
> +#
> +# This program 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 program 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.
> +#
> +
> +top_srcdir		?= ../../..
> +
> +include $(top_srcdir)/include/mk/env_pre.mk
> +
> +INSTALL_TARGETS		:= unshare01.sh
> +
> +include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/commands/unshare/unshare01.sh b/testcases/commands/unshare/unshare01.sh
> new file mode 100755
> index 0000000..ba379df
> --- /dev/null
> +++ b/testcases/commands/unshare/unshare01.sh
> @@ -0,0 +1,150 @@
> +#!/bin/sh
> +#
> +# Copyright (c) 2017 FUJITSU LIMITED. All rights reserved.
> +# Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
> +#
> +# This program 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 program 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.
> +#
> +# Test unshare command with some basic options.
> +# 1) If we run unshare with "--user", UID in the newly created user namespace
> +#    is set to 65534.
> +# 2) If we run unshare with "--user", GID in the newly created user namespace
> +#    is set to 65534.
> +# 3) If we run with "--user --map-root-user", UID in the newly created user
> +#    namespace is set to 0.
> +# 4) If we run with "--user --map-root-user", GID in the newly created user
> +#    is set to 0.
> +# 5) If we run with "--mount", mount and unmount events do not propagate to
> +#    its parent mount namespace.
> +# 6) If we run with "--mount --propagation shared", mount and unmount events
> +#    propagate to its parent mount namespace.
> +# 7) If we run with "--user --map-root-user --mount", mount and unmount events
> +#    do not propagate to its parent mount namespace.
> +# 8) Even if we run with "--user --map-root-user --mount --propagation shared",
> +#    mount and unmount events do not propagate to its parent mount namespace
> +#    because the shared mount is reduced to a slave mount.
> +#
> +#    Please see the following URL for detailed information:
> +#    http://man7.org/linux/man-pages/man7/user_namespaces.7.html
> +#    http://man7.org/linux/man-pages/man7/mount_namespaces.7.html
> +#
> +
> +TST_CNT=8
> +TST_SETUP=setup
> +TST_CLEANUP=cleanup
> +TST_TESTFUNC=do_test
> +TST_NEEDS_ROOT=1
> +TST_NEEDS_TMPDIR=1
> +TST_NEEDS_CMDS="unshare id mount umount"
> +. tst_test.sh
> +
> +max_userns_path="/proc/sys/user/max_user_namespaces"
> +max_mntns_path="/proc/sys/user/max_mnt_namespaces"
> +max_userns_zero=0
> +max_mntns_zero=0
> +
> +setup()
> +{
> +	# On some distributions(e.g RHEL7.4), the default value of
> +	# max_user_namespaces or max_mnt_namespaces is set to 0.
> +	# We need to change the default value to run unshare command.
> +	if [ -f "${max_userns_path}" ]; then
> +		if [ $(cat "${max_userns_path}") -eq 0 ]; then
> +			echo 1024 > "${max_userns_path}"
> +			max_userns_zero=1
> +		fi
> +	fi
> +
> +	if [ -f "${max_mntns_path}" ]; then
> +		if [ $(cat "${max_mntns_path}") -eq 0 ]; then
> +			echo 1024 > "${max_mntns_path}"
> +			max_mntns_zero=1
> +		fi
> +	fi
> +
> +	mkdir -p test_A test_B
> +	touch test_A/A test_B/B
> +}
> +
> +cleanup()
> +{
> +	# Restore the default value to 0.
> +	[ ${max_userns_zero} -eq 1 ] && echo 0 > "${max_userns_path}"
> +	[ ${max_mntns_zero} -eq 1 ] && echo 0 > "${max_mntns_zero}"
> +}
> +
> +unshare_test()
> +{
> +	local unshare_opts=$1
> +	local verify_cmd=$2
> +	local exp_result=$3
> +
> +	local unshare_cmd="unshare ${unshare_opts} ${verify_cmd}"
> +
> +	eval ${unshare_cmd} > temp 2>&1
> +	if [ $? -ne 0 ]; then
> +		# unrecognized option or invalid option is returned if the
> +		# option is not supported by unshare command(e.g. RHEL6).
> +		# Invalid argument or Operation not permitted is returned
> +		# if the feature is not supported by kernel(e.g. RHEL7).
> +		grep -q -E "unrecognized option|invalid option|Invalid argument|Operation not permitted" temp
> +		if [ $? -eq 0 ]; then
> +			tst_res TCONF "${unshare_cmd} not supported."
> +		else
> +			tst_res TFAIL "${unshare_cmd} failed."
> +		fi
> +		return
> +	fi
> +
> +	if [[ "${verify_cmd}" =~ 'id' ]]; then
> +		if [ $(cat temp) -ne ${exp_result} ]; then
> +			tst_res TFAIL "${unshare_cmd} got wrong uid/gid"
> +			return
> +		fi
> +	fi
> +
> +	if [[ "${verify_cmd}" =~ 'mount' ]]; then
> +		if [ "${exp_result}" = "unmounted" ]; then
> +			if ls test_B | grep -q 'A'; then
> +				tst_res TFAIL "${unshare_cmd} got bind info"
> +				umount test_B
> +				return
> +			fi
> +		else
> +			if ! ls test_B | grep -q 'A'; then
> +				tst_res TFAIL "${unshare_cmd} did not get bind info"
> +				return
> +			fi
> +			umount test_B
> +		fi
> +	fi
> +
> +	tst_res TPASS "${unshare_cmd} succeeded as expected"
> +}
> +
> +do_test()
> +{
> +	case $1 in
> +	1) unshare_test "--user" "id -u" "65534";;
> +	2) unshare_test "--user" "id -g" "65534";;
> +	3) unshare_test "--user --map-root-user" "id -u" "0";;
> +	4) unshare_test "--user --map-root-user" "id -g" "0";;
> +	5) unshare_test "--mount" "mount --bind test_A test_B" "unmounted";;
> +	6) unshare_test "--mount --propagation shared" \
> +			"mount --bind test_A test_B" "mounted";;
> +	7) unshare_test "--user --map-root-user --mount" \
> +			"mount --bind test_A test_B" "unmounted";;
> +	8) unshare_test "--user --map-root-user --mount --propagation shared" \
> +			"mount --bind test_A test_B" "unmounted";;
> +	esac
> +}
> +
> +tst_run




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [LTP] [PATCH] commands/unshare: Add new testcase to test unshare(1)
  2017-11-02  9:34 [LTP] [PATCH] commands/unshare: Add new testcase to test unshare(1) Xiao Yang
  2017-11-13  6:46 ` Xiao Yang
@ 2017-11-30 14:20 ` Cyril Hrubis
  2017-12-04  5:35   ` [LTP] [PATCH v2] " Xiao Yang
  1 sibling, 1 reply; 5+ messages in thread
From: Cyril Hrubis @ 2017-11-30 14:20 UTC (permalink / raw)
  To: ltp

Hi!
> +	if [[ "${verify_cmd}" =~ 'id' ]]; then
> +		if [ $(cat temp) -ne ${exp_result} ]; then
> +			tst_res TFAIL "${unshare_cmd} got wrong uid/gid"
> +			return
> +		fi
> +	fi
> +
> +	if [[ "${verify_cmd}" =~ 'mount' ]]; then
> +		if [ "${exp_result}" = "unmounted" ]; then
> +			if ls test_B | grep -q 'A'; then
> +				tst_res TFAIL "${unshare_cmd} got bind info"
> +				umount test_B
> +				return
> +			fi
> +		else
> +			if ! ls test_B | grep -q 'A'; then
> +				tst_res TFAIL "${unshare_cmd} did not get bind info"
> +				return
> +			fi
> +			umount test_B
> +		fi
> +	fi

The double square operator is bash specific. The portable way who to
check for a start of a string is to use case as:

	case "$verify_cmd" in
	id*) echo starts with id;;
	mount*) echo starts with mount;;
	esac

And it may be a bit cleaner to put the checks into a separate functions
the indentation is getting out of the hand here.

> +	tst_res TPASS "${unshare_cmd} succeeded as expected"
> +}
> +
> +do_test()
> +{
> +	case $1 in
> +	1) unshare_test "--user" "id -u" "65534";;
> +	2) unshare_test "--user" "id -g" "65534";;
> +	3) unshare_test "--user --map-root-user" "id -u" "0";;
> +	4) unshare_test "--user --map-root-user" "id -g" "0";;
> +	5) unshare_test "--mount" "mount --bind test_A test_B" "unmounted";;
> +	6) unshare_test "--mount --propagation shared" \
> +			"mount --bind test_A test_B" "mounted";;
> +	7) unshare_test "--user --map-root-user --mount" \
> +			"mount --bind test_A test_B" "unmounted";;
> +	8) unshare_test "--user --map-root-user --mount --propagation shared" \
> +			"mount --bind test_A test_B" "unmounted";;
> +	esac
> +}
> +
> +tst_run
> -- 
> 1.8.3.1
> 
> 
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [LTP] [PATCH v2] commands/unshare: Add new testcase to test unshare(1)
  2017-11-30 14:20 ` Cyril Hrubis
@ 2017-12-04  5:35   ` Xiao Yang
  2017-12-04 14:17     ` Cyril Hrubis
  0 siblings, 1 reply; 5+ messages in thread
From: Xiao Yang @ 2017-12-04  5:35 UTC (permalink / raw)
  To: ltp

Test unshare(1) command with some basic options.

Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com>
---
 runtest/commands                        |   1 +
 testcases/commands/unshare/Makefile     |  22 ++++
 testcases/commands/unshare/unshare01.sh | 171 ++++++++++++++++++++++++++++++++
 3 files changed, 194 insertions(+)
 create mode 100644 testcases/commands/unshare/Makefile
 create mode 100755 testcases/commands/unshare/unshare01.sh

diff --git a/runtest/commands b/runtest/commands
index 00ae0d2..92df3af 100644
--- a/runtest/commands
+++ b/runtest/commands
@@ -43,3 +43,4 @@ insmod01 insmod01.sh
 wc01 wc01.sh
 keyctl01 keyctl01.sh
 gdb01 gdb01.sh
+unshare01 unshare01.sh
diff --git a/testcases/commands/unshare/Makefile b/testcases/commands/unshare/Makefile
new file mode 100644
index 0000000..a175291
--- /dev/null
+++ b/testcases/commands/unshare/Makefile
@@ -0,0 +1,22 @@
+#
+# Copyright (c) 2017 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
+#
+# This program 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 program 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.
+#
+
+top_srcdir		?= ../../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+
+INSTALL_TARGETS		:= unshare01.sh
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/commands/unshare/unshare01.sh b/testcases/commands/unshare/unshare01.sh
new file mode 100755
index 0000000..71b50ba
--- /dev/null
+++ b/testcases/commands/unshare/unshare01.sh
@@ -0,0 +1,171 @@
+#!/bin/sh
+#
+# Copyright (c) 2017 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
+#
+# This program 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 program 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.
+#
+# Test unshare command with some basic options.
+# 1) If we run unshare with "--user", UID in the newly created user namespace
+#    is set to 65534.
+# 2) If we run unshare with "--user", GID in the newly created user namespace
+#    is set to 65534.
+# 3) If we run with "--user --map-root-user", UID in the newly created user
+#    namespace is set to 0.
+# 4) If we run with "--user --map-root-user", GID in the newly created user
+#    is set to 0.
+# 5) If we run with "--mount", mount and unmount events do not propagate to
+#    its parent mount namespace.
+# 6) If we run with "--mount --propagation shared", mount and unmount events
+#    propagate to its parent mount namespace.
+# 7) If we run with "--user --map-root-user --mount", mount and unmount events
+#    do not propagate to its parent mount namespace.
+# 8) Even if we run with "--user --map-root-user --mount --propagation shared",
+#    mount and unmount events do not propagate to its parent mount namespace
+#    because the shared mount is reduced to a slave mount.
+#
+#    Please see the following URL for detailed information:
+#    http://man7.org/linux/man-pages/man7/user_namespaces.7.html
+#    http://man7.org/linux/man-pages/man7/mount_namespaces.7.html
+#
+
+TST_CNT=8
+TST_SETUP=setup
+TST_CLEANUP=cleanup
+TST_TESTFUNC=do_test
+TST_NEEDS_ROOT=1
+TST_NEEDS_TMPDIR=1
+TST_NEEDS_CMDS="unshare id mount umount"
+. tst_test.sh
+
+max_userns_path="/proc/sys/user/max_user_namespaces"
+max_mntns_path="/proc/sys/user/max_mnt_namespaces"
+default_max_userns=-1
+default_max_mntns=-1
+
+setup()
+{
+	# On some distributions(e.g RHEL7.4), the default value of
+	# max_user_namespaces or max_mnt_namespaces is set to 0.
+	# We need to change the default value to run unshare command.
+	if [ -f "${max_userns_path}" ]; then
+		default_max_userns=$(cat "${max_userns_path}")
+		echo 1024 > "${max_userns_path}"
+	fi
+
+	if [ -f "${max_mntns_path}" ]; then
+		default_max_mntns=$(cat "${max_mntns_path}")
+		echo 1024 > "${max_mntns_path}"
+	fi
+
+	mkdir -p dir_A dir_B
+	touch dir_A/A dir_B/B
+}
+
+cleanup()
+{
+	# Restore the default value to 0.
+	[ ${default_max_userns} -ne -1 ] && \
+		echo ${default_max_userns} > "${max_userns_path}"
+	[ ${default_max_mntns} -ne -1 ] && \
+		echo ${default_max_mntns} > "${max_mntns_path}"
+}
+
+check_id()
+{
+	local act_id=$1
+	local exp_id=$2
+	local cmd=$3
+
+	if [ ${act_id} -ne ${exp_id} ]; then
+		tst_res TFAIL "$cmd got wrong uid/gid"
+	else
+		tst_res TPASS "$cmd got correct uid/gid"
+	fi
+}
+
+check_mount()
+{
+	local tst_dir=$1
+	local exp_stat=$2
+	local cmd=$3
+
+	case ${exp_stat} in
+	unmounted)
+		if ls "${tst_dir}" | grep -qw 'A'; then
+			tst_res TFAIL "$cmd got bind info"
+			umount ${tst_dir}
+			return
+		fi
+		;;
+	mounted)
+		if ! ls "${tst_dir}" | grep -qw 'A'; then
+			tst_res TFAIL "$cmd did not get bind info"
+			return
+		fi
+		umount ${tst_dir}
+		;;
+	esac
+
+	tst_res TPASS "$cmd got bind info as expected"
+}
+
+unshare_test()
+{
+	local unshare_opts=$1
+	local verify_cmd=$2
+	local exp_result=$3
+
+	local unshare_cmd="unshare ${unshare_opts} ${verify_cmd}"
+
+	eval ${unshare_cmd} > temp 2>&1
+	if [ $? -ne 0 ]; then
+		# unrecognized option or invalid option is returned if the
+		# option is not supported by unshare command(e.g. RHEL6).
+		# Invalid argument or Operation not permitted is returned
+		# if the feature is not supported by kernel(e.g. RHEL7).
+		grep -q -E "unrecognized option|invalid option|Invalid argument|Operation not permitted" temp
+		if [ $? -eq 0 ]; then
+			tst_res TCONF "${unshare_cmd} not supported."
+		else
+			tst_res TFAIL "${unshare_cmd} failed."
+		fi
+		return
+	fi
+
+	case ${verify_cmd} in
+	id*)
+		check_id "$(cat temp)" "${exp_result}" "${unshare_cmd}"
+		;;
+	mount*)
+		check_mount "dir_B" "${exp_result}" "${unshare_cmd}"
+		;;
+	esac
+}
+
+do_test()
+{
+	case $1 in
+	1) unshare_test "--user" "id -u" "65534";;
+	2) unshare_test "--user" "id -g" "65534";;
+	3) unshare_test "--user --map-root-user" "id -u" "0";;
+	4) unshare_test "--user --map-root-user" "id -g" "0";;
+	5) unshare_test "--mount" "mount --bind dir_A dir_B" "unmounted";;
+	6) unshare_test "--mount --propagation shared" \
+			"mount --bind dir_A dir_B" "mounted";;
+	7) unshare_test "--user --map-root-user --mount" \
+			"mount --bind dir_A dir_B" "unmounted";;
+	8) unshare_test "--user --map-root-user --mount --propagation shared" \
+			"mount --bind dir_A dir_B" "unmounted";;
+	esac
+}
+
+tst_run
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH v2] commands/unshare: Add new testcase to test unshare(1)
  2017-12-04  5:35   ` [LTP] [PATCH v2] " Xiao Yang
@ 2017-12-04 14:17     ` Cyril Hrubis
  0 siblings, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2017-12-04 14:17 UTC (permalink / raw)
  To: ltp

Hi!
Pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-12-04 14:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-02  9:34 [LTP] [PATCH] commands/unshare: Add new testcase to test unshare(1) Xiao Yang
2017-11-13  6:46 ` Xiao Yang
2017-11-30 14:20 ` Cyril Hrubis
2017-12-04  5:35   ` [LTP] [PATCH v2] " Xiao Yang
2017-12-04 14:17     ` Cyril Hrubis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.