architecture.rst 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ==================
  3. KUnit Architecture
  4. ==================
  5. The KUnit architecture is divided into two parts:
  6. - `In-Kernel Testing Framework`_
  7. - `kunit_tool (Command-line Test Harness)`_
  8. In-Kernel Testing Framework
  9. ===========================
  10. The kernel testing library supports KUnit tests written in C using
  11. KUnit. These KUnit tests are kernel code. KUnit performs the following
  12. tasks:
  13. - Organizes tests
  14. - Reports test results
  15. - Provides test utilities
  16. Test Cases
  17. ----------
  18. The test case is the fundamental unit in KUnit. KUnit test cases are organised
  19. into suites. A KUnit test case is a function with type signature
  20. ``void (*)(struct kunit *test)``. These test case functions are wrapped in a
  21. struct called struct kunit_case.
  22. .. note:
  23. ``generate_params`` is optional for non-parameterized tests.
  24. Each KUnit test case receives a ``struct kunit`` context object that tracks a
  25. running test. The KUnit assertion macros and other KUnit utilities use the
  26. ``struct kunit`` context object. As an exception, there are two fields:
  27. - ``->priv``: The setup functions can use it to store arbitrary test
  28. user data.
  29. - ``->param_value``: It contains the parameter value which can be
  30. retrieved in the parameterized tests.
  31. Test Suites
  32. -----------
  33. A KUnit suite includes a collection of test cases. The KUnit suites
  34. are represented by the ``struct kunit_suite``. For example:
  35. .. code-block:: c
  36. static struct kunit_case example_test_cases[] = {
  37. KUNIT_CASE(example_test_foo),
  38. KUNIT_CASE(example_test_bar),
  39. KUNIT_CASE(example_test_baz),
  40. {}
  41. };
  42. static struct kunit_suite example_test_suite = {
  43. .name = "example",
  44. .init = example_test_init,
  45. .exit = example_test_exit,
  46. .test_cases = example_test_cases,
  47. };
  48. kunit_test_suite(example_test_suite);
  49. In the above example, the test suite ``example_test_suite``, runs the
  50. test cases ``example_test_foo``, ``example_test_bar``, and
  51. ``example_test_baz``. Before running the test, the ``example_test_init``
  52. is called and after running the test, ``example_test_exit`` is called.
  53. The ``kunit_test_suite(example_test_suite)`` registers the test suite
  54. with the KUnit test framework.
  55. Executor
  56. --------
  57. The KUnit executor can list and run built-in KUnit tests on boot.
  58. The Test suites are stored in a linker section
  59. called ``.kunit_test_suites``. For the code, see ``KUNIT_TABLE()`` macro
  60. definition in
  61. `include/asm-generic/vmlinux.lds.h <https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/asm-generic/vmlinux.lds.h?h=v6.0#n950>`_.
  62. The linker section consists of an array of pointers to
  63. ``struct kunit_suite``, and is populated by the ``kunit_test_suites()``
  64. macro. The KUnit executor iterates over the linker section array in order to
  65. run all the tests that are compiled into the kernel.
  66. .. kernel-figure:: kunit_suitememorydiagram.svg
  67. :alt: KUnit Suite Memory
  68. KUnit Suite Memory Diagram
  69. On the kernel boot, the KUnit executor uses the start and end addresses
  70. of this section to iterate over and run all tests. For the implementation of the
  71. executor, see
  72. `lib/kunit/executor.c <https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/kunit/executor.c>`_.
  73. When built as a module, the ``kunit_test_suites()`` macro defines a
  74. ``module_init()`` function, which runs all the tests in the compilation
  75. unit instead of utilizing the executor.
  76. In KUnit tests, some error classes do not affect other tests
  77. or parts of the kernel, each KUnit case executes in a separate thread
  78. context. See the ``kunit_try_catch_run()`` function in
  79. `lib/kunit/try-catch.c <https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/kunit/try-catch.c?h=v5.15#n58>`_.
  80. Assertion Macros
  81. ----------------
  82. KUnit tests verify state using expectations/assertions.
  83. All expectations/assertions are formatted as:
  84. ``KUNIT_{EXPECT|ASSERT}_<op>[_MSG](kunit, property[, message])``
  85. - ``{EXPECT|ASSERT}`` determines whether the check is an assertion or an
  86. expectation.
  87. In the event of a failure, the testing flow differs as follows:
  88. - For expectations, the test is marked as failed and the failure is logged.
  89. - Failing assertions, on the other hand, result in the test case being
  90. terminated immediately.
  91. - Assertions call the function:
  92. ``void __noreturn __kunit_abort(struct kunit *)``.
  93. - ``__kunit_abort`` calls the function:
  94. ``void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch)``.
  95. - ``kunit_try_catch_throw`` calls the function:
  96. ``void kthread_complete_and_exit(struct completion *, long) __noreturn;``
  97. and terminates the special thread context.
  98. - ``<op>`` denotes a check with options: ``TRUE`` (supplied property
  99. has the boolean value "true"), ``EQ`` (two supplied properties are
  100. equal), ``NOT_ERR_OR_NULL`` (supplied pointer is not null and does not
  101. contain an "err" value).
  102. - ``[_MSG]`` prints a custom message on failure.
  103. Test Result Reporting
  104. ---------------------
  105. KUnit prints the test results in KTAP format. KTAP is based on TAP14, see
  106. Documentation/dev-tools/ktap.rst.
  107. KTAP works with KUnit and Kselftest. The KUnit executor prints KTAP results to
  108. dmesg, and debugfs (if configured).
  109. Parameterized Tests
  110. -------------------
  111. Each KUnit parameterized test is associated with a collection of
  112. parameters. The test is invoked multiple times, once for each parameter
  113. value and the parameter is stored in the ``param_value`` field.
  114. The test case includes a KUNIT_CASE_PARAM() macro that accepts a
  115. generator function. The generator function is passed the previous parameter
  116. and returns the next parameter. It also includes a macro for generating
  117. array-based common-case generators.
  118. kunit_tool (Command-line Test Harness)
  119. ======================================
  120. ``kunit_tool`` is a Python script, found in ``tools/testing/kunit/kunit.py``. It
  121. is used to configure, build, execute, parse test results and run all of the
  122. previous commands in correct order (i.e., configure, build, execute and parse).
  123. You have two options for running KUnit tests: either build the kernel with KUnit
  124. enabled and manually parse the results (see
  125. Documentation/dev-tools/kunit/run_manual.rst) or use ``kunit_tool``
  126. (see Documentation/dev-tools/kunit/run_wrapper.rst).
  127. - ``configure`` command generates the kernel ``.config`` from a
  128. ``.kunitconfig`` file (and any architecture-specific options).
  129. The Python scripts available in ``qemu_configs`` folder
  130. (for example, ``tools/testing/kunit/qemu configs/powerpc.py``) contains
  131. additional configuration options for specific architectures.
  132. It parses both the existing ``.config`` and the ``.kunitconfig`` files
  133. to ensure that ``.config`` is a superset of ``.kunitconfig``.
  134. If not, it will combine the two and run ``make olddefconfig`` to regenerate
  135. the ``.config`` file. It then checks to see if ``.config`` has become a superset.
  136. This verifies that all the Kconfig dependencies are correctly specified in the
  137. file ``.kunitconfig``. The ``kunit_config.py`` script contains the code for parsing
  138. Kconfigs. The code which runs ``make olddefconfig`` is part of the
  139. ``kunit_kernel.py`` script. You can invoke this command through:
  140. ``./tools/testing/kunit/kunit.py config`` and
  141. generate a ``.config`` file.
  142. - ``build`` runs ``make`` on the kernel tree with required options
  143. (depends on the architecture and some options, for example: build_dir)
  144. and reports any errors.
  145. To build a KUnit kernel from the current ``.config``, you can use the
  146. ``build`` argument: ``./tools/testing/kunit/kunit.py build``.
  147. - ``exec`` command executes kernel results either directly (using
  148. User-mode Linux configuration), or through an emulator such
  149. as QEMU. It reads results from the log using standard
  150. output (stdout), and passes them to ``parse`` to be parsed.
  151. If you already have built a kernel with built-in KUnit tests,
  152. you can run the kernel and display the test results with the ``exec``
  153. argument: ``./tools/testing/kunit/kunit.py exec``.
  154. - ``parse`` extracts the KTAP output from a kernel log, parses
  155. the test results, and prints a summary. For failed tests, any
  156. diagnostic output will be included.