lib/tst_test.c: add 'needs_drivers' option with tst_check_drivers cmd
PATCH v3 1/2 lib/tst_test.c: add 'needs_drivers' option with tst_check_drivers cmd
The drivers are checked with modprobe. If modprobe is not available on the system, the checks are silently skipped.
---
v3: * Changed the docs according to the Cyril's comment
* Added curly braces in tst_check_drivers.c
* Added dry-run option to modprobe
v2: * moved tst_check_driver() from tst_test.h to tst_kernel.h
* added the new option description to the doc and comment to tst_kernel.h
* iterating over the driver list moved out from tst_check_drivers(), the function renamed accordingly.
doc/test-writing-guidelines.txt | 11 +++++++++++ include/tst_kernel.h | 8 ++++++++ include/tst_test.h | 3 +++ lib/tst_kernel.c | 9 +++++++++ lib/tst_test.c | 9 +++++++++ testcases/lib/.gitignore | 1 + testcases/lib/Makefile | 2 +- testcases/lib/tst_check_drivers.c | 25 +++++++++++++++++++++++++ 8 files changed, 67 insertions(+), 1 deletions(-) create mode 100644 testcases/lib/tst_check_drivers.c
doc/test-writing-guidelines.txt +11 -0
@@ -1457,6 +1457,17 @@ https://www.kernel.org/doc/html/latest/admin-guide/tainted-kernels.html CRC32c checksum generation is supported by LTP. In order to use it, the test should include "tst_checksum.h" header, then can call tst_crc32c(). +2.2.26 Checking kernel for the driver support +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Some tests may need specific kernel drivers, either compiled in, or built +as a module. If .need_drivers points to a NULL-terminated array of kernel +module names these are all checked and the test exits with TCONF on the +first missing driver. + +Since it relies on modprobe command, the check will be skipped if the command +itself is not available on the system. + 2.3 Writing a testcase in shell ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/tst_kernel.h +8 -0
@@ -23,4 +23,12 @@*/ int tst_kernel_bits (void ); +/** +* Checks support for the kernel driver. +* +* @param name The name of the driver. +* @return Returns 0 if the kernel has the driver or modprobe is missing. +*/ +int tst_check_driver (const char *name); +#endif /* TST_KERNEL_H__ */
include/tst_test.h +3 -0
@@ -172,6 +172,9 @@ struct tst_test {/* NULL terminated array of resource file names */ const char *const *resource_files; + +/* NULL terminated array of needed kernel drivers */ +const char *const *needs_drivers; };/*
lib/tst_kernel.c +9 -0
@@ -45,3 +45,12 @@ int tst_kernel_bits(void)return kernel_bits; } + +int tst_check_driver (const char *name) +{ +const char *const argv[] = {"modprobe" ,"-n" , name,NULL }; +int res =tst_run_cmd_ (NULL , argv,"/dev/null" ,"/dev/null" ,1 ); + +/* 255 - it looks like modprobe not available */ +return (res ==255 ) ?0 : res; +}
lib/tst_test.c +9 -0
@@ -784,6 +784,15 @@ static void do_setup(int argc, char *argv[])if (tst_test->min_kver)check_kver (); +if (tst_test->needs_drivers) { +const char *name; +int i; + +for (i =0 ; (name = tst_test->needs_drivers[i]); ++i) +if (tst_check_driver (name)) +tst_brk (TCONF ,"%s driver not available" , name); + } +if (tst_test->format_device) tst_test->needs_device =1 ;
testcases/lib/.gitignore +1 -0
@@ -1,5 +1,6 @@ /tst_sleep /tst_random +/tst_check_drivers /tst_checkpoint /tst_rod /tst_kvcmp
testcases/lib/Makefile +1 -1
@@ -28,6 +28,6 @@ INSTALL_TARGETS := *.sh 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_getconf tst_supported_fs tst_check_drivers include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/lib/tst_check_drivers.c new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* Copyright (c) 2018 Oracle and/or its affiliates. All Rights Reserved. */ + +#include <stdio.h> +#include "tst_kernel.h" + +int main (int argc,const char *argv[]) +{ +const char *name; +int i; + +if (argc <2 ) { +fprintf (stderr,"Please provide kernel driver list\n" ); +return 1 ; + } + +for (i =1 ; (name = argv[i]); ++i) { +if (tst_check_driver (name)) { +fprintf (stderr,"%s" , name); +return 1 ; + } + } + +return 0 ; +}
Signature · 1 line
1.7.1
PATCH v3 2/2 lib/tst_test.sh: add TST_NEEDS_DRIVERS parameter
The drivers are checked with 'tst_check_drivers' command.
---
v3: check the command exit status instead of the returned error message
v2: add a check for empty TST_NEEDS_DRIVERS
doc/test-writing-guidelines.txt | 1 + testcases/lib/tst_test.sh | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 0 deletions(-)
doc/test-writing-guidelines.txt +1 -0
@@ -1662,6 +1662,7 @@ simply by setting right '$TST_NEEDS_FOO'. | 'TST_NEEDS_CMDS' | String with command names that has to be present for the test (see below). | 'TST_NEEDS_MODULE' | Test module name needed for the test (see below). +| 'TST_NEEDS_DRIVERS'| Checks kernel drivers support for the test. |============================================================================= Checking for presence of commands
testcases/lib/tst_test.sh +14 -0
@@ -302,6 +302,18 @@ tst_check_cmds() return 0 } +tst_test_drivers() +{ + [ $# -eq 0 ] && return 0 + + local drv + + drv="$(tst_check_drivers $@ 2>&1)" + + [ $? -ne 0 ] && tst_brk TCONF "$drv driver not available" + return 0 +} + tst_is_int() { [ "$1" -eq "$1" ] 2>/dev/null
@@ -349,6 +361,7 @@ tst_run() OPTS|USAGE|PARSE_ARGS|POS_ARGS);; NEEDS_ROOT|NEEDS_TMPDIR|NEEDS_DEVICE|DEVICE);; NEEDS_CMDS|NEEDS_MODULE|MODPATH|DATAROOT);; + NEEDS_DRIVERS);; IPV6|IPVER|TEST_DATA|TEST_DATA_IFS);; RETRY_FUNC|RETRY_FN_EXP_BACKOFF);; *) tst_res TWARN "Reserved variable TST_$_tst_i used!";;
@@ -386,6 +399,7 @@ tst_run() fi tst_test_cmds $TST_NEEDS_CMDS + tst_test_drivers $TST_NEEDS_DRIVERS if [ -n "$TST_MIN_KVER" ]; then tst_kvcmp -lt "$TST_MIN_KVER" && \
Signature · 1 line
1.7.1
PATCH v3 2/2 lib/tst_test.sh: add TST_NEEDS_DRIVERS parameter
Hi Alexey,
Quoted 3 lines
The drivers are checked with '
tst_check_drivers' command.Signed-off-by Alexey Kodanev <[email protected]>
Quoted 2 lines
---
v3: check the command exit status instead of the returned error message
Kind regards,
Petr
PATCH v3 2/2 lib/tst_test.sh: add TST_NEEDS_DRIVERS parameter
Quoted 6 lines · Petr Vorel
Hi Alexey,
Quoted 3 lines
The drivers are checked with '
tst_check_drivers' command.Signed-off-by Alexey Kodanev <[email protected]>Reviewed-by Petr Vorel <[email protected]>
Both patches applied, thanks for review!
PATCH v3 1/2 lib/tst_test.c: add 'needs_drivers' option with tst_check_drivers cmd
In reply to Alexey KodanevHi Alexey,
Quoted 4 lines
The drivers are checked with modprobe. If modprobe is not available on the system, the checks are silently skipped.
Signed-off-by Alexey Kodanev <[email protected]>
Quoted 4 lines
---
v3: * Changed the docs according to the Cyril's comment
* Added curly braces intst_check_drivers.c
* Added dry-run option to modprobe
I like this change as it allows to load module with parameters later.
Kind regards,
Petr