module.sh 1.3 KB

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