architecture.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ==================
  3. KUnit Architecture
  4. ==================
  5. The KUnit architecture can be 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. KUnit tests are kernel code. KUnit does several things:
  12. - Organizes tests
  13. - Reports test results
  14. - Provides test utilities
  15. Test Cases
  16. ----------
  17. The fundamental unit in KUnit is the test case. The KUnit test cases are
  18. grouped into KUnit suites. A KUnit test case is a function with type
  19. signature ``void (*)(struct kunit *test)``.
  20. These test case functions are wrapped in a struct called
  21. struct kunit_case.
  22. .. note:
  23. ``generate_params`` is optional for non-parameterized tests.
  24. Each KUnit test case gets a ``struct kunit`` context
  25. object passed to it that tracks a running test. The KUnit assertion
  26. macros and other KUnit utilities use the ``struct kunit`` context
  27. object. As an exception, there are two fields:
  28. - ``->priv``: The setup functions can use it to store arbitrary test
  29. user data.
  30. - ``->param_value``: It contains the parameter value which can be
  31. retrieved in the parameterized tests.
  32. Test Suites
  33. -----------
  34. A KUnit suite includes a collection of test cases. The KUnit suites
  35. are represented by the ``struct kunit_suite``. For example:
  36. .. code-block:: c
  37. static struct kunit_case example_test_cases[] = {
  38. KUNIT_CASE(example_test_foo),
  39. KUNIT_CASE(example_test_bar),
  40. KUNIT_CASE(example_test_baz),
  41. {}
  42. };
  43. static struct kunit_suite example_test_suite = {
  44. .name = "example",
  45. .init = example_test_init,
  46. .exit = example_test_exit,
  47. .test_cases = example_test_cases,
  48. };
  49. kunit_test_suite(example_test_suite);
  50. In the above example, the test suite ``example_test_suite``, runs the
  51. test cases ``example_test_foo``, ``example_test_bar``, and
  52. ``example_test_baz``. Before running the test, the ``example_test_init``
  53. is called and after running the test, ``example_test_exit`` is called.
  54. The ``kunit_test_suite(example_test_suite)`` registers the test suite
  55. with the KUnit test framework.
  56. Executor
  57. --------
  58. The KUnit executor can list and run built-in KUnit tests on boot.
  59. The Test suites are stored in a linker section
  60. called ``.kunit_test_suites``. For code, see:
  61. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/asm-generic/vmlinux.lds.h?h=v5.15#n945.
  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. To run all tests compiled into the kernel, the KUnit executor
  65. iterates over the linker section array.
  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 code, see:
  71. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/kunit/executor.c
  72. When built as a module, the ``kunit_test_suites()`` macro defines a
  73. ``module_init()`` function, which runs all the tests in the compilation
  74. unit instead of utilizing the executor.
  75. In KUnit tests, some error classes do not affect other tests
  76. or parts of the kernel, each KUnit case executes in a separate thread
  77. context. For code, see:
  78. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/kunit/try-catch.c?h=v5.15#n58
  79. Assertion Macros
  80. ----------------
  81. KUnit tests verify state using expectations/assertions.
  82. All expectations/assertions are formatted as:
  83. ``KUNIT_{EXPECT|ASSERT}_<op>[_MSG](kunit, property[, message])``
  84. - ``{EXPECT|ASSERT}`` determines whether the check is an assertion or an
  85. expectation.
  86. - For an expectation, if the check fails, marks the test as failed
  87. and logs the failure.
  88. - An assertion, on failure, causes the test case to terminate
  89. immediately.
  90. - Assertions call function:
  91. ``void __noreturn kunit_abort(struct kunit *)``.
  92. - ``kunit_abort`` calls function:
  93. ``void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch)``.
  94. - ``kunit_try_catch_throw`` calls function:
  95. ``void kthread_complete_and_exit(struct completion *, long) __noreturn;``
  96. and terminates the special thread context.
  97. - ``<op>`` denotes a check with options: ``TRUE`` (supplied property
  98. has the boolean value “true”), ``EQ`` (two supplied properties are
  99. equal), ``NOT_ERR_OR_NULL`` (supplied pointer is not null and does not
  100. contain an “err” value).
  101. - ``[_MSG]`` prints a custom message on failure.
  102. Test Result Reporting
  103. ---------------------
  104. KUnit prints test results in KTAP format. KTAP is based on TAP14, see:
  105. https://github.com/isaacs/testanything.github.io/blob/tap14/tap-version-14-specification.md.
  106. KTAP (yet to be standardized format) works with KUnit and Kselftest.
  107. The KUnit executor prints KTAP results to dmesg, and debugfs
  108. (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.
  116. The generator function is passed the previous parameter and returns the next
  117. parameter. It also provides a macro to generate common-case generators based on
  118. arrays.
  119. kunit_tool (Command Line Test Harness)
  120. ======================================
  121. kunit_tool is a Python script ``(tools/testing/kunit/kunit.py)``
  122. that can be used to configure, build, exec, parse and run (runs other
  123. commands in order) test results. You can either run KUnit tests using
  124. kunit_tool or can include KUnit in kernel and parse manually.
  125. - ``configure`` command generates the kernel ``.config`` from a
  126. ``.kunitconfig`` file (and any architecture-specific options).
  127. For some architectures, additional config options are specified in the
  128. ``qemu_config`` Python script
  129. (For example: ``tools/testing/kunit/qemu_configs/powerpc.py``).
  130. It parses both the existing ``.config`` and the ``.kunitconfig`` files
  131. and ensures that ``.config`` is a superset of ``.kunitconfig``.
  132. If this is not the case, it will combine the two and run
  133. ``make olddefconfig`` to regenerate the ``.config`` file. It then
  134. verifies that ``.config`` is now a superset. This checks if all
  135. Kconfig dependencies are correctly specified in ``.kunitconfig``.
  136. ``kunit_config.py`` includes the parsing Kconfigs code. The code which
  137. runs ``make olddefconfig`` is a part of ``kunit_kernel.py``. You can
  138. invoke this command via: ``./tools/testing/kunit/kunit.py config`` and
  139. generate a ``.config`` file.
  140. - ``build`` runs ``make`` on the kernel tree with required options
  141. (depends on the architecture and some options, for example: build_dir)
  142. and reports any errors.
  143. To build a KUnit kernel from the current ``.config``, you can use the
  144. ``build`` argument: ``./tools/testing/kunit/kunit.py build``.
  145. - ``exec`` command executes kernel results either directly (using
  146. User-mode Linux configuration), or via an emulator such
  147. as QEMU. It reads results from the log via standard
  148. output (stdout), and passes them to ``parse`` to be parsed.
  149. If you already have built a kernel with built-in KUnit tests,
  150. you can run the kernel and display the test results with the ``exec``
  151. argument: ``./tools/testing/kunit/kunit.py exec``.
  152. - ``parse`` extracts the KTAP output from a kernel log, parses
  153. the test results, and prints a summary. For failed tests, any
  154. diagnostic output will be included.