* [LTP] [PATCH v4 1/3] tst_test.sh: Add tst_require_module command
2019-12-10 8:04 [LTP] [PATCH v4] lsmod01: Add kernel module Joerg Vehlow
@ 2019-12-10 8:04 ` Joerg Vehlow
2019-12-10 8:04 ` [LTP] [PATCH v4 2/3] tst_test.sh: Make public tst_require_root command public Joerg Vehlow
2019-12-10 8:04 ` [LTP] [PATCH v4 3/3] lsmod01: Add kernel module Joerg Vehlow
2 siblings, 0 replies; 5+ messages in thread
From: Joerg Vehlow @ 2019-12-10 8:04 UTC (permalink / raw)
To: ltp
From: Joerg Vehlow <joerg.vehlow@aox-tech.de>
Adds a new library function tst_require_module, that can be used, when a
test needs a module dynamically at runtime
Signed-off-by: Joerg Vehlow <joerg.vehlow@aox-tech.de>
---
doc/test-writing-guidelines.txt | 3 +++
testcases/lib/tst_test.sh | 39 +++++++++++++++++++--------------
2 files changed, 25 insertions(+), 17 deletions(-)
diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt
index 546bb7a49..3360f0920 100644
--- a/doc/test-writing-guidelines.txt
+++ b/doc/test-writing-guidelines.txt
@@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/testcases/lib/tst_test.sh b/testcases/lib/tst_test.sh
index 70c1ef2e3..afee6aac5 100644
--- a/testcases/lib/tst_test.sh
+++ b/testcases/lib/tst_test.sh
@@ -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
--
2.20.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* [LTP] [PATCH v4 2/3] tst_test.sh: Make public tst_require_root command public
2019-12-10 8:04 [LTP] [PATCH v4] lsmod01: Add kernel module Joerg Vehlow
2019-12-10 8:04 ` [LTP] [PATCH v4 1/3] tst_test.sh: Add tst_require_module command Joerg Vehlow
@ 2019-12-10 8:04 ` Joerg Vehlow
2019-12-10 8:04 ` [LTP] [PATCH v4 3/3] lsmod01: Add kernel module Joerg Vehlow
2 siblings, 0 replies; 5+ messages in thread
From: Joerg Vehlow @ 2019-12-10 8:04 UTC (permalink / raw)
To: ltp
From: Joerg Vehlow <joerg.vehlow@aox-tech.de>
If a test requires root only under certain circumstances, TST_NEEDS_ROOT
is not sufficient, because it always requires root.
Signed-off-by: Joerg Vehlow <joerg.vehlow@aox-tech.de>
---
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(-)
diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt
index 3360f0920..d0c49dc9c 100644
--- a/doc/test-writing-guidelines.txt
+++ b/doc/test-writing-guidelines.txt
@@ -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.
diff --git a/testcases/lib/tst_net.sh b/testcases/lib/tst_net.sh
index 59ceb3352..dd0c712c3 100644
--- a/testcases/lib/tst_net.sh
+++ b/testcases/lib/tst_net.sh
@@ -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}"
diff --git a/testcases/lib/tst_security.sh b/testcases/lib/tst_security.sh
index 7d033bbc5..af7c81bb5 100644
--- a/testcases/lib/tst_security.sh
+++ b/testcases/lib/tst_security.sh
@@ -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)"
diff --git a/testcases/lib/tst_test.sh b/testcases/lib/tst_test.sh
index afee6aac5..c93ab7dbe 100644
--- a/testcases/lib/tst_test.sh
+++ b/testcases/lib/tst_test.sh
@@ -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
--
2.20.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* [LTP] [PATCH v4 3/3] lsmod01: Add kernel module
2019-12-10 8:04 [LTP] [PATCH v4] lsmod01: Add kernel module Joerg Vehlow
2019-12-10 8:04 ` [LTP] [PATCH v4 1/3] tst_test.sh: Add tst_require_module command Joerg Vehlow
2019-12-10 8:04 ` [LTP] [PATCH v4 2/3] tst_test.sh: Make public tst_require_root command public Joerg Vehlow
@ 2019-12-10 8:04 ` Joerg Vehlow
2020-03-05 13:08 ` Petr Vorel
2 siblings, 1 reply; 5+ messages in thread
From: Joerg Vehlow @ 2019-12-10 8:04 UTC (permalink / raw)
To: ltp
From: Joerg Vehlow <joerg.vehlow@aox-tech.de>
The test fails, if no kernel module is loaded. Now at least one module is
always loaded.
Signed-off-by: Joerg Vehlow <joerg.vehlow@aox-tech.de>
---
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
diff --git a/testcases/commands/.gitignore b/testcases/commands/.gitignore
index 0ed343881..ed5e13e29 100644
--- a/testcases/commands/.gitignore
+++ b/testcases/commands/.gitignore
@@ -2,3 +2,4 @@
/ldd/datafiles/*.obj.so
/eject/eject_check_tray
/insmod/ltp_insmod01.ko
+/lsmod/ltp_lsmod01.ko
diff --git a/testcases/commands/lsmod/Makefile b/testcases/commands/lsmod/Makefile
index 2af91b3de..8fc3b1436 100644
--- a/testcases/commands/lsmod/Makefile
+++ b/testcases/commands/lsmod/Makefile
@@ -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
diff --git a/testcases/commands/lsmod/lsmod01.sh b/testcases/commands/lsmod/lsmod01.sh
index 0f5b6aaae..992199fa8 100755
--- a/testcases/commands/lsmod/lsmod01.sh
+++ b/testcases/commands/lsmod/lsmod01.sh
@@ -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)
diff --git a/testcases/commands/lsmod/ltp_lsmod01.c b/testcases/commands/lsmod/ltp_lsmod01.c
new file mode 100644
index 000000000..19f9d9145
--- /dev/null
+++ b/testcases/commands/lsmod/ltp_lsmod01.c
@@ -0,0 +1,28 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright (c) 2016 Fujitsu Ltd.
+ * Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
+ *
+ * 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);
--
2.20.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* [LTP] [PATCH v4 3/3] lsmod01: Add kernel module
2019-12-10 8:04 ` [LTP] [PATCH v4 3/3] lsmod01: Add kernel module Joerg Vehlow
@ 2020-03-05 13:08 ` Petr Vorel
0 siblings, 0 replies; 5+ messages in thread
From: Petr Vorel @ 2020-03-05 13:08 UTC (permalink / raw)
To: ltp
Hi Joerg,
patchset merged, with tiny style changes.
Sorry for the delay with reviewing it.
...
> +module_inserted=0
We prefer in the shell library empty over 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
This is not needed, thus omitted.
Kind regards,
Petr
^ permalink raw reply [flat|nested] 5+ messages in thread