Skip to content

Commit d346052

Browse files
Tobin C. HardingShuah Khan
authored andcommitted
kselftest: Add test runner creation script
Currently if we wish to use kselftest to run tests within a kernel module we write a small script to load/unload and do error reporting. There are a bunch of these under tools/testing/selftests/lib/ that are all identical except for the test name. We can reduce code duplication and improve maintainability if we have one version of this. However kselftest requires an executable for each test. We can move all the script logic to a central script then have each individual test script call the main script. Oneliner to call kselftest_module.sh courtesy of Kees, thanks! Add test runner creation script. Convert tools/testing/selftests/lib/*.sh to use new test creation script. Testing ------- Configure kselftests for lib/ then build and boot kernel. Then run kselftests as follows: $ cd /path/to/kernel/tree $ sudo make O=$output_path -C tools/testing/selftests TARGETS="lib" run_tests and also $ cd /path/to/kernel/tree $ cd tools/testing/selftests $ sudo make O=$output_path TARGETS="lib" run_tests and also $ cd /path/to/kernel/tree $ cd tools/testing/selftests $ sudo make TARGETS="lib" run_tests Acked-by: Kees Cook <[email protected]> Signed-off-by: Tobin C. Harding <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 6989808 commit d346052

File tree

4 files changed

+88
-50
lines changed

4 files changed

+88
-50
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0+
3+
4+
#
5+
# Runs an individual test module.
6+
#
7+
# kselftest expects a separate executable for each test, this can be
8+
# created by adding a script like this:
9+
#
10+
# #!/bin/sh
11+
# SPDX-License-Identifier: GPL-2.0+
12+
# $(dirname $0)/../kselftest_module.sh "description" module_name
13+
#
14+
# Example: tools/testing/selftests/lib/printf.sh
15+
16+
desc="" # Output prefix.
17+
module="" # Filename (without the .ko).
18+
args="" # modprobe arguments.
19+
20+
modprobe="/sbin/modprobe"
21+
22+
main() {
23+
parse_args "$@"
24+
assert_root
25+
assert_have_module
26+
run_module
27+
}
28+
29+
parse_args() {
30+
script=${0##*/}
31+
32+
if [ $# -lt 2 ]; then
33+
echo "Usage: $script <description> <module_name> [FAIL]"
34+
exit 1
35+
fi
36+
37+
desc="$1"
38+
shift || true
39+
module="$1"
40+
shift || true
41+
args="$@"
42+
}
43+
44+
assert_root() {
45+
if [ ! -w /dev ]; then
46+
skip "please run as root"
47+
fi
48+
}
49+
50+
assert_have_module() {
51+
if ! $modprobe -q -n $module; then
52+
skip "module $module is not found"
53+
fi
54+
}
55+
56+
run_module() {
57+
if $modprobe -q $module $args; then
58+
$modprobe -q -r $module
59+
say "ok"
60+
else
61+
fail ""
62+
fi
63+
}
64+
65+
say() {
66+
echo "$desc: $1"
67+
}
68+
69+
70+
fail() {
71+
say "$1 [FAIL]" >&2
72+
exit 1
73+
}
74+
75+
skip() {
76+
say "$1 [SKIP]" >&2
77+
# Kselftest framework requirement - SKIP code is 4.
78+
exit 4
79+
}
80+
81+
#
82+
# Main script
83+
#
84+
main "$@"

tools/testing/selftests/lib/bitmap.sh

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
11
#!/bin/sh
22
# SPDX-License-Identifier: GPL-2.0
3-
4-
# Kselftest framework requirement - SKIP code is 4.
5-
ksft_skip=4
6-
7-
# Runs bitmap infrastructure tests using test_bitmap kernel module
8-
if ! /sbin/modprobe -q -n test_bitmap; then
9-
echo "bitmap: module test_bitmap is not found [SKIP]"
10-
exit $ksft_skip
11-
fi
12-
13-
if /sbin/modprobe -q test_bitmap; then
14-
/sbin/modprobe -q -r test_bitmap
15-
echo "bitmap: ok"
16-
else
17-
echo "bitmap: [FAIL]"
18-
exit 1
19-
fi
3+
$(dirname $0)/../kselftest_module.sh "bitmap" test_bitmap
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
#!/bin/sh
22
# SPDX-License-Identifier: GPL-2.0
33
# Checks fast/slow prime_number generation for inconsistencies
4-
5-
# Kselftest framework requirement - SKIP code is 4.
6-
ksft_skip=4
7-
8-
if ! /sbin/modprobe -q -n prime_numbers; then
9-
echo "prime_numbers: module prime_numbers is not found [SKIP]"
10-
exit $ksft_skip
11-
fi
12-
13-
if /sbin/modprobe -q prime_numbers selftest=65536; then
14-
/sbin/modprobe -q -r prime_numbers
15-
echo "prime_numbers: ok"
16-
else
17-
echo "prime_numbers: [FAIL]"
18-
exit 1
19-
fi
4+
$(dirname $0)/../kselftest_module.sh "prime numbers" prime_numbers selftest=65536

tools/testing/selftests/lib/printf.sh

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
#!/bin/sh
22
# SPDX-License-Identifier: GPL-2.0
3-
# Runs printf infrastructure using test_printf kernel module
4-
5-
# Kselftest framework requirement - SKIP code is 4.
6-
ksft_skip=4
7-
8-
if ! /sbin/modprobe -q -n test_printf; then
9-
echo "printf: module test_printf is not found [SKIP]"
10-
exit $ksft_skip
11-
fi
12-
13-
if /sbin/modprobe -q test_printf; then
14-
/sbin/modprobe -q -r test_printf
15-
echo "printf: ok"
16-
else
17-
echo "printf: [FAIL]"
18-
exit 1
19-
fi
3+
# Tests the printf infrastructure using test_printf kernel module.
4+
$(dirname $0)/../kselftest_module.sh "printf" test_printf

0 commit comments

Comments
 (0)