linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: keescook at chromium.org (Kees Cook)
Subject: [PATCH v3 2/7] kselftest: Add test runner creation script
Date: Tue, 2 Apr 2019 14:27:21 -0700	[thread overview]
Message-ID: <CAGXu5j+=O3C6z7H5j93Z4mQkpS3f0ea_7T+LRQEnRCZCoKX8zw@mail.gmail.com> (raw)
In-Reply-To: <20190306214226.14598-3-tobin@kernel.org>

On Wed, Mar 6, 2019 at 1:43 PM Tobin C. Harding <tobin at 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 at 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 at chromium.org>

-- 
Kees Cook

WARNING: multiple messages have this Message-ID (diff)
From: keescook@chromium.org (Kees Cook)
Subject: [PATCH v3 2/7] kselftest: Add test runner creation script
Date: Tue, 2 Apr 2019 14:27:21 -0700	[thread overview]
Message-ID: <CAGXu5j+=O3C6z7H5j93Z4mQkpS3f0ea_7T+LRQEnRCZCoKX8zw@mail.gmail.com> (raw)
Message-ID: <20190402212721.OdW84dYjNIE3IgQRCnJ-MaUrv5VXgXLp73oA2-GSndY@z> (raw)
In-Reply-To: <20190306214226.14598-3-tobin@kernel.org>

On Wed, Mar 6, 2019@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 at 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 at chromium.org>

-- 
Kees Cook

  parent reply	other threads:[~2019-04-02 21:27 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-06 21:42 [PATCH v3 0/7] lib/string: Add strscpy_pad() function tobin
2019-03-06 21:42 ` Tobin C. Harding
2019-03-06 21:42 ` [PATCH v3 1/7] lib/test_printf: Add empty module_exit function tobin
2019-03-06 21:42   ` Tobin C. Harding
2019-04-02 21:24   ` keescook
2019-04-02 21:24     ` Kees Cook
2019-03-06 21:42 ` [PATCH v3 2/7] kselftest: Add test runner creation script tobin
2019-03-06 21:42   ` Tobin C. Harding
2019-04-02 21:27   ` keescook [this message]
2019-04-02 21:27     ` Kees Cook
2019-04-02 21:33     ` rdunlap
2019-04-02 21:33       ` Randy Dunlap
2019-04-04 23:16       ` me
2019-04-04 23:16         ` Tobin C. Harding
2019-03-06 21:42 ` [PATCH v3 3/7] kselftest/lib: Use new shell runner to define tests tobin
2019-03-06 21:42   ` Tobin C. Harding
2019-04-02 21:29   ` keescook
2019-04-02 21:29     ` Kees Cook
2019-04-02 21:45   ` keescook
2019-04-02 21:45     ` Kees Cook
2019-04-02 21:51     ` keescook
2019-04-02 21:51       ` Kees Cook
2019-03-06 21:42 ` [PATCH v3 4/7] kselftest: Add test module framework header tobin
2019-03-06 21:42   ` Tobin C. Harding
2019-04-02 21:31   ` keescook
2019-04-02 21:31     ` Kees Cook
2019-03-06 21:42 ` [PATCH v3 5/7] lib: Use new kselftest header tobin
2019-03-06 21:42   ` Tobin C. Harding
2019-04-02 21:32   ` keescook
2019-04-02 21:32     ` Kees Cook
2019-03-06 21:42 ` [PATCH v3 6/7] lib/string: Add strscpy_pad() function tobin
2019-03-06 21:42   ` Tobin C. Harding
2019-04-02 21:35   ` keescook
2019-04-02 21:35     ` Kees Cook
2019-03-06 21:42 ` [PATCH v3 7/7] lib: Add test module for strscpy_pad tobin
2019-03-06 21:42   ` Tobin C. Harding
2019-04-02 21:36   ` keescook
2019-04-02 21:36     ` Kees Cook
2019-03-06 21:49 ` [PATCH v3 0/7] lib/string: Add strscpy_pad() function me
2019-03-06 21:49   ` Tobin C. Harding
2019-03-07 21:18   ` me
2019-03-07 21:18     ` Tobin C. Harding
2019-03-07 22:43     ` keescook
2019-03-07 22:43       ` Kees Cook
2019-03-08  5:23       ` me
2019-03-08  5:23         ` Tobin C. Harding
2019-03-08 16:18         ` keescook
2019-03-08 16:18           ` Kees Cook
2019-04-02 21:37 ` keescook
2019-04-02 21:37   ` Kees Cook
2019-04-03  0:25   ` me
2019-04-03  0:25     ` Tobin C. Harding
2019-04-03  0:29     ` shuah
2019-04-03  0:29       ` shuah

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAGXu5j+=O3C6z7H5j93Z4mQkpS3f0ea_7T+LRQEnRCZCoKX8zw@mail.gmail.com' \
    --to=linux-kselftest@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).