Skip to contentSkip to conversation

lsmod01: Add kernel module

PATCH Superseded Joerg Vehlow 2 participants 2 messages Oct 7, 2019

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 lsmod01: Add kernel module

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    | 32 ++++++++++++++++++++++++++
 testcases/commands/lsmod/ltp_lsmod01.c | 28 ++++++++++++++++++++++
 4 files changed, 76 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 +32 -0
index ad170dcd4..c761a91af 100755
@@ -5,11 +5,43 @@
 #
 # 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_NEEDS_MODULE="ltp_lsmod01.ko"
 . tst_test.sh
 
+inserted=0
+
+setup()
+{
+	if [ -z "$(cat /proc/modules)" ]; then
+		tst_res TINFO "Loading dummy kernel module"
+		insmod "$TST_MODPATH"
+		if [ $? -ne 0 ]; then
+			tst_res TBROK "insmod failed"
+			return
+		fi
+
+		inserted=1
+	fi
+}
+
+cleanup()
+{
+	if [ $inserted -ne 0 ]; then
+		tst_res TINFO "Unloading dummy kernel module"
+		rmmod ltp_lsmod01
+		if [ $? -ne 0 ]; then
+			tst_res TWARN "rmmod failed"
+		fi
+		inserted=0
+	fi
+}
+
+
 lsmod_test()
 {
 	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
#1 lore raw

PATCH v3 1/1 lsmod01: Add kernel module

Hi!

Quoted 5 lines
testcases/commands/lsmod/lsmod01.sh +0 -0
index ad170dcd4..c761a91af 100755

testcases/commands/lsmod/lsmod01.sh line 13

Quoted 9 lines
testcases/commands/lsmod/lsmod01.sh +3 -0
@@ -5,11 +5,43 @@
 #
 # 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_NEEDS_MODULE="ltp_lsmod01.ko"

Sigh, there is another problem here, this causes the library to exit the test if the module does not exist, which disables the test in a case that the module wasn't compiled i.e. kernel-devel wasn't installed, but in the most cases the test will run just fine on such configuration.

I guess that we need to patch the library so that we can get a path to a module without exitting the test. Maybe we should add TST_GET_MODPATH="foo.ko" which will do the same as TST_NEEDS_MODULE but will not exit the test if not found. Then we can check if "$TST_MODPATH" is empty in the test setup just before we attempt insmod.

Quoted 71 lines
testcases/commands/lsmod/lsmod01.sh +29 -0
 . tst_test.sh
 
+inserted=0
+
+setup()
+{
+	if [ -z "$(cat /proc/modules)" ]; then
+		tst_res TINFO "Loading dummy kernel module"
+		insmod "$TST_MODPATH"
+		if [ $? -ne 0 ]; then
+			tst_res TBROK "insmod failed"
+			return
+		fi
+
+		inserted=1
+	fi
+}
+
+cleanup()
+{
+	if [ $inserted -ne 0 ]; then
+		tst_res TINFO "Unloading dummy kernel module"
+		rmmod ltp_lsmod01
+		if [ $? -ne 0 ]; then
+			tst_res TWARN "rmmod failed"
+		fi
+		inserted=0
+	fi
+}
+
+
 lsmod_test()
 {
 	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
Signature · 2 lines
Cyril Hrubis
[email protected]