[LTP] [PATCH 1/1] ns_exec: Move to testcases/lib/

Petr Vorel pvorel@suse.cz
Fri Mar 3 17:03:12 CET 2023


It's not only a test, but also tool for tst_net.sh therefore better to
be in common tools directory.

Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
Not sure if we want to keep doc/namespaces-helper-tools.txt,
if yes, is it wort to convert it to wiki page? (as a separate effort)

Kind regards,
Petr

 doc/namespaces-helper-tools.txt                  |  6 +++---
 testcases/kernel/containers/netns/netns_lib.sh   | 16 ++++++++--------
 testcases/kernel/containers/netns/netns_sysfs.sh | 11 ++++++-----
 testcases/kernel/containers/share/.gitignore     |  1 -
 testcases/kernel/fs/fs_bind/fs_bind_lib.sh       |  5 +++--
 testcases/lib/.gitignore                         |  1 +
 testcases/lib/Makefile                           |  2 +-
 testcases/lib/tst_net.sh                         | 12 ++++++------
 .../share/ns_exec.c => lib/tst_ns_exec.c}        |  6 +++---
 9 files changed, 31 insertions(+), 29 deletions(-)
 rename testcases/{kernel/containers/share/ns_exec.c => lib/tst_ns_exec.c} (92%)

diff --git a/doc/namespaces-helper-tools.txt b/doc/namespaces-helper-tools.txt
index 81eec4d6c..e5a39657d 100644
--- a/doc/namespaces-helper-tools.txt
+++ b/doc/namespaces-helper-tools.txt
@@ -14,7 +14,7 @@ located in ltp/testcases/kernel/containers/share directory and include:
 ** PID of the daemonized child process is printed on the stdout
 ** the new namespace(s) is(are) maintained by the daemonized child process
 ** namespace(s) can be removed by killing the daemonized process
-* ns_exec
+* tst_ns_exec
 ** enters the namespace(s) of a process specified by a PID
 ** then executes the indicated program inside that namespace(s)
 * ns_ifmove
@@ -39,13 +39,13 @@ myns=$(ns_create net,ipc)
 ip link add veth0 type veth peer name veth1
 
 # Executes command 'ip a' inside the namespace specified by PID in myns variable
-ns_exec $myns net,ipc ip a
+tst_ns_exec $myns net,ipc ip a
 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 
 # Moves interface veth1 into the namespace specified by PID in myns variable
 ns_ifmove veth1 $myns
-ns_exec $myns net,ipc ip a
+tst_ns_exec $myns net,ipc ip a
 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 6: veth1: <BROADCAST> mtu 1500 qdisc noop state DOWN qlen 1000
diff --git a/testcases/kernel/containers/netns/netns_lib.sh b/testcases/kernel/containers/netns/netns_lib.sh
index 039631e9f..f5ce3b5e3 100755
--- a/testcases/kernel/containers/netns/netns_lib.sh
+++ b/testcases/kernel/containers/netns/netns_lib.sh
@@ -1,7 +1,7 @@
 #!/bin/sh
 # SPDX-License-Identifier: GPL-2.0-or-later
 # Copyright (c) 2022 Petr Vorel <pvorel@suse.cz>
-# Copyright (c) Linux Test Project, 2014-2021
+# Copyright (c) Linux Test Project, 2014-2023
 # Copyright (c) 2015 Red Hat, Inc.
 
 TST_NEEDS_ROOT=1
@@ -20,7 +20,7 @@ TST_NET_SKIP_VARIABLE_INIT=1
 IPV4_NET16_UNUSED="10.23"
 IPV6_NET32_UNUSED="fd00:23"
 
-# Set to "net" for ns_create/ns_exec as their options requires
+# Set to "net" for ns_create/tst_ns_exec as their options requires
 # to specify a namespace type. Empty for ip command.
 NS_TYPE=
 
@@ -28,7 +28,7 @@ NS_TYPE=
 tping=
 
 # Network namespaces handles for manipulating and executing commands inside
-# namespaces. For 'ns_exec' handles are PIDs of daemonized processes running
+# namespaces. For 'tst_ns_exec' handles are PIDs of daemonized processes running
 # in namespaces.
 NS_HANDLE0=
 NS_HANDLE1=
@@ -40,7 +40,7 @@ NS_HANDLE1=
 IFCONF_IN6_ARG=
 
 # Program which will be used to enter and run other commands inside a network namespace.
-# (ns_exec|ip)
+# (tst_ns_exec|ip)
 NS_EXEC="ip"
 
 # Communication type between kernel and user space for basic setup: enabling and
@@ -54,7 +54,7 @@ do_cleanup=
 netns_parse_args()
 {
 	case $1 in
-	e) NS_EXEC="ns_exec" ;;
+	e) NS_EXEC="tst_ns_exec" ;;
 	I) COMM_TYPE="ioctl"; tst_require_cmds ifconfig ;;
 	esac
 }
@@ -63,7 +63,7 @@ netns_usage()
 {
 	echo "usage: $0 [ -e ] [ -I ]"
 	echo "OPTIONS"
-	echo "-e      Use ns_exec instead of ip"
+	echo "-e      Use tst_ns_exec instead of ip"
 	echo "-I      Test ioctl (with ifconfig) instead of netlink (with ip)"
 }
 
@@ -105,7 +105,7 @@ netns_cleanup()
 	fi
 }
 
-# Sets up NS_EXEC to use 'ns_exec', creates two network namespaces and stores
+# Sets up NS_EXEC to use 'tst_ns_exec', creates two network namespaces and stores
 # their handles into NS_HANDLE0 and NS_HANDLE1 variables (in this case handles
 # are PIDs of daemonized processes running in these namespaces). Virtual
 # ethernet device is then created for each namespace.
@@ -113,7 +113,7 @@ netns_ns_exec_setup()
 {
 	local ret
 
-	NS_EXEC="ns_exec"
+	NS_EXEC="tst_ns_exec"
 
 	NS_HANDLE0=$(ns_create $NS_TYPE)
 	if [ $? -eq 1 ]; then
diff --git a/testcases/kernel/containers/netns/netns_sysfs.sh b/testcases/kernel/containers/netns/netns_sysfs.sh
index 34ef3b8ee..aca339bbf 100755
--- a/testcases/kernel/containers/netns/netns_sysfs.sh
+++ b/testcases/kernel/containers/netns/netns_sysfs.sh
@@ -2,6 +2,7 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 # Copyright (c) Köry Maincent <kory.maincent@bootlin.com> 2020
 # Copyright (c) 2015 Red Hat, Inc.
+# Copyright (c) Linux Test Project, 2015-2023
 #
 # Tests that a separate network namespace cannot affect sysfs contents
 # of the main namespace.
@@ -27,10 +28,10 @@ do_setup()
 	ip link add $DUMMYDEV_HOST type dummy || \
 		tst_brk TBROK "failed to add a new (host) dummy device"
 
-	ns_exec $NS_HANDLE $NS_TYPE mount --make-rprivate /sys
-	ns_exec $NS_HANDLE $NS_TYPE ip link add $DUMMYDEV type dummy || \
+	tst_ns_exec $NS_HANDLE $NS_TYPE mount --make-rprivate /sys
+	tst_ns_exec $NS_HANDLE $NS_TYPE ip link add $DUMMYDEV type dummy || \
 		tst_brk TBROK "failed to add a new dummy device"
-	ns_exec $NS_HANDLE $NS_TYPE mount -t sysfs none /sys 2>/dev/null
+	tst_ns_exec $NS_HANDLE $NS_TYPE mount -t sysfs none /sys 2>/dev/null
 }
 
 do_cleanup()
@@ -42,8 +43,8 @@ do_cleanup()
 
 do_test()
 {
-	EXPECT_PASS ns_exec $NS_HANDLE $NS_TYPE test -e /sys/class/net/$DUMMYDEV
-	EXPECT_FAIL ns_exec $NS_HANDLE $NS_TYPE test -e /sys/class/net/$DUMMYDEV_HOST
+	EXPECT_PASS tst_ns_exec $NS_HANDLE $NS_TYPE test -e /sys/class/net/$DUMMYDEV
+	EXPECT_FAIL tst_ns_exec $NS_HANDLE $NS_TYPE test -e /sys/class/net/$DUMMYDEV_HOST
 	EXPECT_FAIL test -e /sys/class/net/$DUMMYDEV
 }
 
diff --git a/testcases/kernel/containers/share/.gitignore b/testcases/kernel/containers/share/.gitignore
index 0d5ecf069..e490e86a6 100644
--- a/testcases/kernel/containers/share/.gitignore
+++ b/testcases/kernel/containers/share/.gitignore
@@ -1,3 +1,2 @@
 /ns_ifmove
 /ns_create
-/ns_exec
diff --git a/testcases/kernel/fs/fs_bind/fs_bind_lib.sh b/testcases/kernel/fs/fs_bind/fs_bind_lib.sh
index 2dd9ef08f..2499ce68d 100644
--- a/testcases/kernel/fs/fs_bind/fs_bind_lib.sh
+++ b/testcases/kernel/fs/fs_bind/fs_bind_lib.sh
@@ -2,6 +2,7 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 # Copyright (c) International Business Machines  Corp., 2005
 # Copyright (c) 2021 Joerg Vehlow <joerg.vehlow@aox-tech.de>
+# Copyright (c) Linux Test Project, 2022-2023
 # Based on work by: Avantika Mathur (mathurav@us.ibm.com)
 
 TST_NEEDS_TMPDIR=1
@@ -109,7 +110,7 @@ fs_bind_check()
 	    fi
 
 		if [ $use_ns -eq 1 ]; then
-			output="$(ns_exec ${FS_BIND_MNTNS_PID} mnt diff -r "$PWD/$dir1" "$PWD/$dir2" 2> /dev/null)"
+			output="$(tst_ns_exec ${FS_BIND_MNTNS_PID} mnt diff -r "$PWD/$dir1" "$PWD/$dir2" 2> /dev/null)"
 		else
 			output="$(diff -r "$dir1" "$dir2" 2> /dev/null)"
 		fi
@@ -203,7 +204,7 @@ fs_bind_create_ns()
 fs_bind_exec_ns()
 {
 	[ -z "$FS_BIND_MNTNS_PID" ] && tst_brk TBROK "Namespace does not exist"
-	EXPECT_PASS ns_exec $FS_BIND_MNTNS_PID mnt "$@"
+	EXPECT_PASS tst_ns_exec $FS_BIND_MNTNS_PID mnt "$@"
 }
 
 fs_bind_destroy_ns()
diff --git a/testcases/lib/.gitignore b/testcases/lib/.gitignore
index 34dea272d..318aa9fb7 100644
--- a/testcases/lib/.gitignore
+++ b/testcases/lib/.gitignore
@@ -11,6 +11,7 @@
 /tst_net_iface_prefix
 /tst_net_ip_prefix
 /tst_net_vars
+/tst_ns_exec
 /tst_random
 /tst_rod
 /tst_sleep
diff --git a/testcases/lib/Makefile b/testcases/lib/Makefile
index f4f8c8524..e16a67c79 100644
--- a/testcases/lib/Makefile
+++ b/testcases/lib/Makefile
@@ -12,6 +12,6 @@ MAKE_TARGETS		:= tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\
 			   tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars\
 			   tst_getconf tst_supported_fs tst_check_drivers tst_get_unused_port\
 			   tst_get_median tst_hexdump tst_get_free_pids tst_timeout_kill\
-			   tst_check_kconfigs tst_cgctl
+			   tst_check_kconfigs tst_cgctl tst_ns_exec
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/lib/tst_net.sh b/testcases/lib/tst_net.sh
index fc64a588a..8c94d0f7b 100644
--- a/testcases/lib/tst_net.sh
+++ b/testcases/lib/tst_net.sh
@@ -1,7 +1,7 @@
 #!/bin/sh
 # SPDX-License-Identifier: GPL-2.0-or-later
 # Copyright (c) 2014-2017 Oracle and/or its affiliates. All Rights Reserved.
-# Copyright (c) 2016-2022 Petr Vorel <pvorel@suse.cz>
+# Copyright (c) 2016-2023 Petr Vorel <pvorel@suse.cz>
 # Author: Alexey Kodanev <alexey.kodanev@oracle.com>
 
 [ -n "$TST_LIB_NET_LOADED" ] && return 0
@@ -115,7 +115,7 @@ init_ltp_netspace()
 	local pid
 
 	if [ ! -f /var/run/netns/ltp_ns -a -z "$LTP_NETNS" ]; then
-		tst_require_cmds ip ns_create ns_exec ns_ifmove
+		tst_require_cmds ip ns_create tst_ns_exec ns_ifmove
 		tst_require_root
 
 		tst_require_drivers veth
@@ -123,10 +123,10 @@ init_ltp_netspace()
 		pid="$(ROD ns_create net,mnt)"
 		mkdir -p /var/run/netns
 		ROD ln -s /proc/$pid/ns/net /var/run/netns/ltp_ns
-		ROD ns_exec $pid net,mnt mount --make-rprivate /sys
-		ROD ns_exec $pid net,mnt mount -t sysfs none /sys
+		ROD tst_ns_exec $pid net,mnt mount --make-rprivate /sys
+		ROD tst_ns_exec $pid net,mnt mount -t sysfs none /sys
 		ROD ns_ifmove ltp_ns_veth1 $pid
-		ROD ns_exec $pid net,mnt ip link set lo up
+		ROD tst_ns_exec $pid net,mnt ip link set lo up
 	elif [ -n "$LTP_NETNS" ]; then
 		tst_res_ TINFO "using not default LTP netns: '$LTP_NETNS'"
 	fi
@@ -135,7 +135,7 @@ init_ltp_netspace()
 	RHOST_IFACES="${RHOST_IFACES:-ltp_ns_veth1}"
 
 	pid="$(echo $(readlink /var/run/netns/ltp_ns) | cut -f3 -d'/')"
-	export LTP_NETNS="${LTP_NETNS:-ns_exec $pid net,mnt}"
+	export LTP_NETNS="${LTP_NETNS:-tst_ns_exec $pid net,mnt}"
 
 	tst_restore_ipaddr
 	tst_restore_ipaddr rhost
diff --git a/testcases/kernel/containers/share/ns_exec.c b/testcases/lib/tst_ns_exec.c
similarity index 92%
rename from testcases/kernel/containers/share/ns_exec.c
rename to testcases/lib/tst_ns_exec.c
index b802bec9d..ead77fbfb 100644
--- a/testcases/kernel/containers/share/ns_exec.c
+++ b/testcases/lib/tst_ns_exec.c
@@ -2,7 +2,7 @@
 /*
  * Copyright (c) 2015 Red Hat, Inc.
  *               Matus Marhefka <mmarhefk@redhat.com>
- * Copyright (c) Linux Test Project, 2015-2022
+ * Copyright (c) Linux Test Project, 2015-2023
  * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
  */
 
@@ -33,7 +33,7 @@ static void print_help(void)
 {
 	int i;
 
-	printf("usage: ns_exec <NS_PID> <%s", params[0].name);
+	printf("usage: tst_ns_exec <NS_PID> <%s", params[0].name);
 
 	for (i = 1; params[i].name; i++)
 		printf("|,%s", params[i].name);
@@ -41,7 +41,7 @@ static void print_help(void)
 	printf("> <PROGRAM> [ARGS]\nSecond argument indicates the types"
 		" of a namespaces maintained by NS_PID\nand is specified"
 		" as a comma separated list.\n"
-		"Example: ns_exec 1234 net,ipc ip a\n");
+		"Example: tst_ns_exec 1234 net,ipc ip a\n");
 }
 
 static void open_ns_fd(const char *pid, const char *ns)
-- 
2.39.1



More information about the ltp mailing list