run_manual.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ============================
  3. Run Tests without kunit_tool
  4. ============================
  5. If we do not want to use kunit_tool (For example: we want to integrate
  6. with other systems, or run tests on real hardware), we can
  7. include KUnit in any kernel, read out results, and parse manually.
  8. .. note:: KUnit is not designed for use in a production system. It is
  9. possible that tests may reduce the stability or security of
  10. the system.
  11. Configure the Kernel
  12. ====================
  13. KUnit tests can run without kunit_tool. This can be useful, if:
  14. - We have an existing kernel configuration to test.
  15. - Need to run on real hardware (or using an emulator/VM kunit_tool
  16. does not support).
  17. - Wish to integrate with some existing testing systems.
  18. KUnit is configured with the ``CONFIG_KUNIT`` option, and individual
  19. tests can also be built by enabling their config options in our
  20. ``.config``. KUnit tests usually (but don't always) have config options
  21. ending in ``_KUNIT_TEST``. Most tests can either be built as a module,
  22. or be built into the kernel.
  23. .. note ::
  24. We can enable the ``KUNIT_ALL_TESTS`` config option to
  25. automatically enable all tests with satisfied dependencies. This is
  26. a good way of quickly testing everything applicable to the current
  27. config.
  28. KUnit can be enabled or disabled at boot time, and this behavior is
  29. controlled by the kunit.enable kernel parameter.
  30. By default, kunit.enable is set to 1 because KUNIT_DEFAULT_ENABLED is
  31. enabled by default. To ensure that tests are executed as expected,
  32. verify that kunit.enable=1 at boot time.
  33. Once we have built our kernel (and/or modules), it is simple to run
  34. the tests. If the tests are built-in, they will run automatically on the
  35. kernel boot. The results will be written to the kernel log (``dmesg``)
  36. in TAP format.
  37. If the tests are built as modules, they will run when the module is
  38. loaded.
  39. .. code-block :: bash
  40. # modprobe example-test
  41. The results will appear in TAP format in ``dmesg``.
  42. debugfs
  43. =======
  44. KUnit can be accessed from userspace via the debugfs filesystem (See more
  45. information about debugfs at Documentation/filesystems/debugfs.rst).
  46. If ``CONFIG_KUNIT_DEBUGFS`` is enabled, the KUnit debugfs filesystem is
  47. mounted at /sys/kernel/debug/kunit. You can use this filesystem to perform
  48. the following actions.
  49. Retrieve Test Results
  50. =====================
  51. You can use debugfs to retrieve KUnit test results. The test results are
  52. accessible from the debugfs filesystem in the following read-only file:
  53. .. code-block :: bash
  54. /sys/kernel/debug/kunit/<test_suite>/results
  55. The test results are printed in a KTAP document. Note this document is separate
  56. to the kernel log and thus, may have different test suite numbering.
  57. Run Tests After Kernel Has Booted
  58. =================================
  59. You can use the debugfs filesystem to trigger built-in tests to run after
  60. boot. To run the test suite, you can use the following command to write to
  61. the ``/sys/kernel/debug/kunit/<test_suite>/run`` file:
  62. .. code-block :: bash
  63. echo "any string" > /sys/kernel/debugfs/kunit/<test_suite>/run
  64. As a result, the test suite runs and the results are printed to the kernel
  65. log.
  66. However, this feature is not available with KUnit suites that use init data,
  67. because init data may have been discarded after the kernel boots. KUnit
  68. suites that use init data should be defined using the
  69. kunit_test_init_section_suites() macro.
  70. Also, you cannot use this feature to run tests concurrently. Instead a test
  71. will wait to run until other tests have completed or failed.
  72. .. note ::
  73. For test authors, to use this feature, tests will need to correctly initialise
  74. and/or clean up any data, so the test runs correctly a second time.