Skip to contentSkip to conversation

lsmod01: Add kernel module

PATCH New Joerg Vehlow 2 participants 5 messages Dec 10, 2019 – Mar 5, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 lsmod01: Add kernel module

Now that the naming debate for tst_needs vs. tst_require is done, I post the patch, that started all of it again.

This patch adds a dummy module to lsmod01, to make the test run successfully, if no modules are loaded at the time of test execution.

To have no impact on the test requirements, when modules are already loaded, the root test and module search has to be done conditionally. Therefor the library had to be extended/modified to add tst_require_root and tst_require_module.

 doc/test-writing-guidelines.txt        |  6 ++++-
 testcases/commands/.gitignore          |  1 +
 testcases/commands/lsmod/Makefile      | 15 ++++++++++++
 testcases/commands/lsmod/lsmod01.sh    | 33 ++++++++++++++++++++++++++
 testcases/commands/lsmod/ltp_lsmod01.c | 28 ++++++++++++++++++++++
 testcases/lib/tst_net.sh               | 12 ++--------
 testcases/lib/tst_security.sh          |  4 ++--
 testcases/lib/tst_test.sh              | 43 +++++++++++++++++++---------------
 8 files changed, 110 insertions(+), 32 deletions(-)
#1 lore raw

PATCH v4 1/3 tst_test.sh: Add tst_require_module command

From: Joerg Vehlow <[email protected]>

Adds a new library function tst_require_module, that can be used, when a test needs a module dynamically at runtime

---
 doc/test-writing-guidelines.txt |  3 +++
 testcases/lib/tst_test.sh       | 39 +++++++++++++++++++--------------
 2 files changed, 25 insertions(+), 17 deletions(-)
doc/test-writing-guidelines.txt +3 -0
index 546bb7a49..3360f0920 100644
@@ -2184,6 +2184,9 @@ module in a few possible paths.
 If module was found the path to it will be stored into '$TST_MODPATH'
 variable, if module wasn't found the test will exit with 'TCONF'.
 
+Alternatively the 'tst_require_module()' function can be used to do the same
+at runtime.
+
 2.3.3 Optional command line parameters
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
testcases/lib/tst_test.sh +22 -17
index 70c1ef2e3..afee6aac5 100644
@@ -461,6 +461,27 @@ _tst_require_root()
 	fi
 }
 
+tst_require_module()
+{
+	local _tst_module=$1
+
+	for tst_module in "$_tst_module" \
+	                  "$LTPROOT/testcases/bin/$_tst_module" \
+	                  "$TST_STARTWD/$_tst_module"; do
+
+			if [ -f "$tst_module" ]; then
+				TST_MODPATH="$tst_module"
+				break
+			fi
+	done
+	
+	if [ -n "$TST_MODPATH" ]; then
+		tst_res TINFO "Found module at '$TST_MODPATH'"
+	else
+		tst_brk TCONF "Failed to find module '$_tst_module'"
+	fi
+}
+
 tst_run()
 {
 	local _tst_i
@@ -552,23 +573,7 @@ tst_run()
 		TST_DEVICE_FLAG=1
 	fi
 
-	if [ -n "$TST_NEEDS_MODULE" ]; then
-		for tst_module in "$TST_NEEDS_MODULE" \
-		                  "$LTPROOT/testcases/bin/$TST_NEEDS_MODULE" \
-		                  "$TST_STARTWD/$TST_NEEDS_MODULE"; do
-
-				if [ -f "$tst_module" ]; then
-					TST_MODPATH="$tst_module"
-					break
-				fi
-		done
-
-		if [ -z "$TST_MODPATH" ]; then
-			tst_brk TCONF "Failed to find module '$TST_NEEDS_MODULE'"
-		else
-			tst_res TINFO "Found module at '$TST_MODPATH'"
-		fi
-	fi
+	[ -n "$TST_NEEDS_MODULE" ] && tst_require_module "$TST_NEEDS_MODULE"
 
 	if [ -n "$TST_SETUP" ]; then
 		$TST_SETUP
Signature · 1 line
2.20.1
#2 lore raw

PATCH v4 2/3 tst_test.sh: Make public tst_require_root command public

In reply to Joerg Vehlow

From: Joerg Vehlow <[email protected]>

If a test requires root only under certain circumstances, TST_NEEDS_ROOT is not sufficient, because it always requires root.

---
 doc/test-writing-guidelines.txt |  3 ++-
 testcases/lib/tst_net.sh        | 12 ++----------
 testcases/lib/tst_security.sh   |  4 ++--
 testcases/lib/tst_test.sh       |  4 ++--
 4 files changed, 8 insertions(+), 15 deletions(-)
doc/test-writing-guidelines.txt +2 -1
index 3360f0920..d0c49dc9c 100644
@@ -2118,7 +2118,8 @@ simply by setting right '$TST_NEEDS_FOO'.
 [options="header"]
 |=============================================================================
 | Variable name      | Action done
-| 'TST_NEEDS_ROOT'   | Exit the test with 'TCONF' unless executed under root
+| 'TST_NEEDS_ROOT'   | Exit the test with 'TCONF' unless executed under root.
+|                    | Alternatively the tst_require_root command can be used
 | 'TST_NEEDS_TMPDIR' | Create test temporary directory and cd into it.
 | 'TST_NEEDS_DEVICE' | Prepare test temporary device, the path to testing
                        device is stored in '$TST_DEVICE' variable.
testcases/lib/tst_net.sh +2 -10
index 59ceb3352..dd0c712c3 100644
@@ -97,14 +97,6 @@ tst_brk_()
 {
 	[ -z "$TST_USE_LEGACY_API" ] && tst_brk $@ || tst_brkm $@
 }
-tst_require_root_()
-{
-	if [ -z "$TST_USE_LEGACY_API" ]; then
-		_tst_require_root
-	else
-		tst_require_root
-	fi
-}
 
 init_ltp_netspace()
 {
@@ -112,7 +104,7 @@ init_ltp_netspace()
 
 	if [ ! -f /var/run/netns/ltp_ns -a -z "$LTP_NETNS" ]; then
 		tst_require_cmds ip
-		tst_require_root_
+		tst_require_root
 
 		ROD ip li add name ltp_ns_veth1 type veth peer name ltp_ns_veth2
 		pid="$(ROD ns_create net,mnt)"
@@ -577,7 +569,7 @@ tst_del_ipaddr()
 tst_restore_ipaddr()
 {
 	tst_require_cmds ip
-	tst_require_root_
+	tst_require_root
 
 	local type="${1:-lhost}"
 	local link_num="${2:-0}"
testcases/lib/tst_security.sh +2 -2
index 7d033bbc5..af7c81bb5 100644
@@ -95,7 +95,7 @@ tst_selinux_used_profile()
 tst_disable_apparmor()
 {
 	tst_res TINFO "trying to disable AppArmor (requires super/root)"
-	_tst_require_root
+	tst_require_root
 
 	local f="aa-teardown"
 	local action
@@ -115,7 +115,7 @@ tst_disable_apparmor()
 tst_disable_selinux()
 {
 	tst_res TINFO "trying to disable SELinux (requires super/root)"
-	_tst_require_root
+	tst_require_root
 
 	local f="$(_tst_get_enforce)"
 
testcases/lib/tst_test.sh +2 -2
index afee6aac5..c93ab7dbe 100644
@@ -454,7 +454,7 @@ _tst_setup_timer()
 	_tst_setup_timer_pid=$!
 }
 
-_tst_require_root()
+tst_require_root()
 {
 	if [ "$(id -ru)" != 0 ]; then
 		tst_brk TCONF "Must be super/root for this test!"
@@ -529,7 +529,7 @@ tst_run()
 		tst_brk TBROK "Number of iterations (-i) must be > 0"
 	fi
 
-	[ "$TST_NEEDS_ROOT" = 1 ] && _tst_require_root
+	[ "$TST_NEEDS_ROOT" = 1 ] && tst_require_root
 
 	[ "$TST_DISABLE_APPARMOR" = 1 ] && tst_disable_apparmor
 	[ "$TST_DISABLE_SELINUX" = 1 ] && tst_disable_selinux
Signature · 1 line
2.20.1
#3 lore raw

PATCH v4 3/3 lsmod01: Add kernel module

In reply to Joerg Vehlow

From: Joerg Vehlow <[email protected]>

The test fails, if no kernel module is loaded. Now at least one module is always loaded.

---
 testcases/commands/.gitignore          |  1 +
 testcases/commands/lsmod/Makefile      | 15 ++++++++++++
 testcases/commands/lsmod/lsmod01.sh    | 33 ++++++++++++++++++++++++++
 testcases/commands/lsmod/ltp_lsmod01.c | 28 ++++++++++++++++++++++
 4 files changed, 77 insertions(+)
 create mode 100644 testcases/commands/lsmod/ltp_lsmod01.c
testcases/commands/.gitignore +1 -0
index 0ed343881..ed5e13e29 100644
@@ -2,3 +2,4 @@
 /ldd/datafiles/*.obj.so
 /eject/eject_check_tray
 /insmod/ltp_insmod01.ko
+/lsmod/ltp_lsmod01.ko
testcases/commands/lsmod/Makefile +15 -0
index 2af91b3de..8fc3b1436 100644
@@ -13,10 +13,25 @@
 #    GNU General Public License for more details.
 #
 
+ifneq ($(KERNELRELEASE),)
+
+obj-m := ltp_lsmod01.o
+
+else
+
 top_srcdir		?= ../../..
 
+include $(top_srcdir)/include/mk/testcases.mk
+
+REQ_VERSION_MAJOR       := 2
+REQ_VERSION_PATCH       := 6
+MAKE_TARGETS            := ltp_lsmod01.ko
+
 include $(top_srcdir)/include/mk/env_pre.mk
 
 INSTALL_TARGETS		:= lsmod01.sh
 
+include $(top_srcdir)/include/mk/module.mk
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
+
+endif
testcases/commands/lsmod/lsmod01.sh +33 -0
index 0f5b6aaae..992199fa8 100755
@@ -5,11 +5,44 @@
 #
 # Test basic functionality of lsmod command.
 
+TST_CLEANUP=cleanup
+TST_SETUP=setup
 TST_TESTFUNC=lsmod_test
 TST_NEEDS_TMPDIR=1
 TST_NEEDS_CMDS="lsmod"
 . tst_test.sh
 
+module_inserted=0
+
+setup()
+{
+	if [ -z "$(cat /proc/modules)"  ]; then
+		tst_res TINFO "Loading dummy kernel module"
+		tst_require_module "ltp_lsmod01.ko"
+		tst_require_root
+		tst_require_cmds insmod
+		insmod "$TST_MODPATH"
+		if [ $? -ne 0 ]; then
+			tst_res TBROK "insmod failed"
+			return
+		fi
+
+		module_inserted=1
+	fi
+}
+
+cleanup()
+{
+	if [ $module_inserted -ne 0 ]; then
+		tst_res TINFO "Unloading dummy kernel module"
+		rmmod ltp_lsmod01
+		if [ $? -ne 0 ]; then
+			tst_res TWARN "rmmod failed"
+		fi
+		module_inserted=0
+	fi
+}
+
 lsmod_matches_proc_modules()
 {
 	lsmod_output=$(lsmod | awk '!/Module/{print $1, $2, $3}' | sort)
testcases/commands/lsmod/ltp_lsmod01.c new file +28 -0
new file mode 100644
index 000000000..19f9d9145
@@ -0,0 +1,28 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright (c) 2016 Fujitsu Ltd.
+ * Author: Guangwen Feng <[email protected]>
+ *
+ * Description:
+ *  This is a kernel loadable module programme used by lssmod01.sh
+ *  testcase which inserts this module for test of lsmod command.
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+
+static int test_init(void)
+{
+	return 0;
+}
+
+static void test_exit(void)
+{
+
+}
+
+MODULE_LICENSE("GPL");
+
+module_init(test_init);
+module_exit(test_exit);
Signature · 1 line
2.20.1
#4 lore raw

PATCH v4 3/3 lsmod01: Add kernel module

Hi Joerg,

patchset merged, with tiny style changes.
Sorry for the delay with reviewing it.

...

Quoted 1 line

+module_inserted=0

We prefer in the shell library empty over 0.

Quoted 17 lines
testcases/commands/lsmod/lsmod01.sh +17 -0
+
+setup()
+{
+	if [ -z "$(cat /proc/modules)"  ]; then
+		tst_res TINFO "Loading dummy kernel module"
+		tst_require_module "ltp_lsmod01.ko"
+		tst_require_root
+		tst_require_cmds insmod
+		insmod "$TST_MODPATH"
+		if [ $? -ne 0 ]; then
+			tst_res TBROK "insmod failed"
+			return
+		fi
+
+		module_inserted=1
+	fi
+}

testcases/commands/lsmod/lsmod01.sh line 42

Quoted 10 lines
testcases/commands/lsmod/lsmod01.sh +10 -0
+
+cleanup()
+{
+	if [ $module_inserted -ne 0 ]; then
+		tst_res TINFO "Unloading dummy kernel module"
+		rmmod ltp_lsmod01
+		if [ $? -ne 0 ]; then
+			tst_res TWARN "rmmod failed"
+		fi
+		module_inserted=0

This is not needed, thus omitted.

Kind regards,
Petr