diff mbox series

[v3,2/7] kselftest: Add test runner creation script

Message ID 20190306214226.14598-3-tobin@kernel.org (mailing list archive)
State New, archived
Headers show
Series lib/string: Add strscpy_pad() function | expand

Commit Message

Tobin C. Harding March 6, 2019, 9:42 p.m. UTC
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
set the module name and call the main script.  There is a little bit of
boilerplate left in each script to handle building/running tests with
the O=/path/to/out make option.

Add test runner creation script.

Signed-off-by: Tobin C. Harding <tobin@kernel.org>
---
 tools/testing/selftests/kselftest_module.sh | 75 +++++++++++++++++++++
 1 file changed, 75 insertions(+)
 create mode 100755 tools/testing/selftests/kselftest_module.sh

Comments

Kees Cook April 2, 2019, 9:27 p.m. UTC | #1
On Wed, Mar 6, 2019 at 1:43 PM Tobin C. Harding <tobin@kernel.org> wrote:
>
> 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
> set the module name and call the main script.  There is a little bit of
> boilerplate left in each script to handle building/running tests with
> the O=/path/to/out make option.
>
> Add test runner creation script.
>
> Signed-off-by: Tobin C. Harding <tobin@kernel.org>
> ---
>  tools/testing/selftests/kselftest_module.sh | 75 +++++++++++++++++++++
>  1 file changed, 75 insertions(+)
>  create mode 100755 tools/testing/selftests/kselftest_module.sh
>
> diff --git a/tools/testing/selftests/kselftest_module.sh b/tools/testing/selftests/kselftest_module.sh
> new file mode 100755
> index 000000000000..b5d446738614
> --- /dev/null
> +++ b/tools/testing/selftests/kselftest_module.sh
> @@ -0,0 +1,75 @@
> +#!/bin/sh
> +# SPDX-License-Identifier: GPL-2.0+
> +
> +#
> +# Runs an individual test module.  kselftest expects a separate
> +# executable for each test.  So test should each have an individial
> +# script that can call this script.
> +#
> +
> +# Individual test scrits should define these:
> +module=""                      # filename (without the .ko).
> +desc=""                                # Output prefix.
> +
> +modprobe="/sbin/modprobe"
> +
> +main() {
> +    parse_args $@
> +    assert_root
> +    assert_have_module
> +    run_module
> +}
> +
> +parse_args() {
> +    script=${0##*/}
> +
> +    if [[ ! $# -eq 2 ]]; then
> +       echo "Usage: $script <module_name> <description> [FAIL]"
> +       exit 1
> +    fi
> +
> +    module=$1
> +    desc=$2
> +}
> +
> +assert_root() {
> +    if [[ $EUID -ne 0 ]]; then
> +       skip "please run as root"
> +    fi
> +}
> +
> +assert_have_module() {
> +    if ! $modprobe -q -n $module; then
> +       skip "module $module is not found"
> +    fi
> +}
> +
> +run_module() {
> +    if $modprobe -q $module; then
> +       $modprobe -q -r $module

Probably will need a way to pass arguments into the modprobe here, but
otherwise looks fine.

> +       say "ok"
> +    else
> +       fail ""
> +    fi
> +}
> +
> +say() {
> +    echo "$desc: $1"
> +}
> +
> +
> +fail() {
> +    say "$1 [FAIL]" >&2
> +    exit 1
> +}
> +
> +skip() {
> +    say "$1 [SKIP]" >&2
> +    # Kselftest framework requirement - SKIP code is 4.
> +    exit 4
> +}
> +
> +#
> +# Main script
> +#
> +main $@
> --
> 2.20.1
>

Reviewed-by: Kees Cook <keescook@chromium.org>
Randy Dunlap April 2, 2019, 9:33 p.m. UTC | #2
Hi,

nits/typos below:

On 4/2/19 2:27 PM, Kees Cook wrote:
> On Wed, Mar 6, 2019 at 1:43 PM Tobin C. Harding <tobin@kernel.org> wrote:
>>
>>
>> Add test runner creation script.
>>
>> Signed-off-by: Tobin C. Harding <tobin@kernel.org>
>> ---
>>  tools/testing/selftests/kselftest_module.sh | 75 +++++++++++++++++++++
>>  1 file changed, 75 insertions(+)
>>  create mode 100755 tools/testing/selftests/kselftest_module.sh
>>
>> diff --git a/tools/testing/selftests/kselftest_module.sh b/tools/testing/selftests/kselftest_module.sh
>> new file mode 100755
>> index 000000000000..b5d446738614
>> --- /dev/null
>> +++ b/tools/testing/selftests/kselftest_module.sh
>> @@ -0,0 +1,75 @@
>> +#!/bin/sh
>> +# SPDX-License-Identifier: GPL-2.0+
>> +
>> +#
>> +# Runs an individual test module.  kselftest expects a separate
>> +# executable for each test.  So test should each have an individial

                                                             individual

>> +# script that can call this script.
>> +#
>> +
>> +# Individual test scrits should define these:

                      scripts

>> +module=""                      # filename (without the .ko).
>> +desc=""                                # Output prefix.


cheers.
Tobin Harding April 4, 2019, 11:16 p.m. UTC | #3
On Tue, Apr 02, 2019 at 02:33:12PM -0700, Randy Dunlap wrote:
> Hi,
> 
> nits/typos below:

Hey thanks for reviewing this Randy, first time I read it I got mixed up
and thought the nits were against Kees' additions but I was wrong,
thanks again.

	Tobin
diff mbox series

Patch

diff --git a/tools/testing/selftests/kselftest_module.sh b/tools/testing/selftests/kselftest_module.sh
new file mode 100755
index 000000000000..b5d446738614
--- /dev/null
+++ b/tools/testing/selftests/kselftest_module.sh
@@ -0,0 +1,75 @@ 
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0+
+
+#
+# Runs an individual test module.  kselftest expects a separate
+# executable for each test.  So test should each have an individial
+# script that can call this script.
+#
+
+# Individual test scrits should define these:
+module=""			# filename (without the .ko).
+desc=""				# Output prefix.
+
+modprobe="/sbin/modprobe"
+
+main() {
+    parse_args $@
+    assert_root
+    assert_have_module
+    run_module
+}
+
+parse_args() {
+    script=${0##*/}
+
+    if [[ ! $# -eq 2 ]]; then
+	echo "Usage: $script <module_name> <description> [FAIL]"
+	exit 1
+    fi
+
+    module=$1
+    desc=$2
+}
+
+assert_root() {
+    if [[ $EUID -ne 0 ]]; then
+	skip "please run as root"
+    fi
+}
+
+assert_have_module() {
+    if ! $modprobe -q -n $module; then
+	skip "module $module is not found"
+    fi
+}
+
+run_module() {
+    if $modprobe -q $module; then
+	$modprobe -q -r $module
+	say "ok"
+    else
+	fail ""
+    fi
+}
+
+say() {
+    echo "$desc: $1"
+}
+
+
+fail() {
+    say "$1 [FAIL]" >&2
+    exit 1
+}
+
+skip() {
+    say "$1 [SKIP]" >&2
+    # Kselftest framework requirement - SKIP code is 4.
+    exit 4
+}
+
+#
+# Main script
+#
+main $@