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