start.rst 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===============
  3. Getting Started
  4. ===============
  5. This page contains an overview of the kunit_tool and KUnit framework,
  6. teaching how to run existing tests and then how to write a simple test case,
  7. and covers common problems users face when using KUnit for the first time.
  8. Installing Dependencies
  9. =======================
  10. KUnit has the same dependencies as the Linux kernel. As long as you can
  11. build the kernel, you can run KUnit.
  12. Running tests with kunit_tool
  13. =============================
  14. kunit_tool is a Python script, which configures and builds a kernel, runs
  15. tests, and formats the test results. From the kernel repository, you
  16. can run kunit_tool:
  17. .. code-block:: bash
  18. ./tools/testing/kunit/kunit.py run
  19. .. note ::
  20. You may see the following error:
  21. "The source tree is not clean, please run 'make ARCH=um mrproper'"
  22. This happens because internally kunit.py specifies ``.kunit``
  23. (default option) as the build directory in the command ``make O=output/dir``
  24. through the argument ``--build_dir``. Hence, before starting an
  25. out-of-tree build, the source tree must be clean.
  26. There is also the same caveat mentioned in the "Build directory for
  27. the kernel" section of the :doc:`admin-guide </admin-guide/README>`,
  28. that is, its use, it must be used for all invocations of ``make``.
  29. The good news is that it can indeed be solved by running
  30. ``make ARCH=um mrproper``, just be aware that this will delete the
  31. current configuration and all generated files.
  32. If everything worked correctly, you should see the following:
  33. .. code-block::
  34. Configuring KUnit Kernel ...
  35. Building KUnit Kernel ...
  36. Starting KUnit Kernel ...
  37. The tests will pass or fail.
  38. .. note ::
  39. Because it is building a lot of sources for the first time,
  40. the ``Building KUnit Kernel`` step may take a while.
  41. For detailed information on this wrapper, see:
  42. Documentation/dev-tools/kunit/run_wrapper.rst.
  43. Selecting which tests to run
  44. ----------------------------
  45. By default, kunit_tool runs all tests reachable with minimal configuration,
  46. that is, using default values for most of the kconfig options. However,
  47. you can select which tests to run by:
  48. - `Customizing Kconfig`_ used to compile the kernel, or
  49. - `Filtering tests by name`_ to select specifically which compiled tests to run.
  50. Customizing Kconfig
  51. ~~~~~~~~~~~~~~~~~~~
  52. A good starting point for the ``.kunitconfig`` is the KUnit default config.
  53. If you didn't run ``kunit.py run`` yet, you can generate it by running:
  54. .. code-block:: bash
  55. cd $PATH_TO_LINUX_REPO
  56. tools/testing/kunit/kunit.py config
  57. cat .kunit/.kunitconfig
  58. .. note ::
  59. ``.kunitconfig`` lives in the ``--build_dir`` used by kunit.py, which is
  60. ``.kunit`` by default.
  61. Before running the tests, kunit_tool ensures that all config options
  62. set in ``.kunitconfig`` are set in the kernel ``.config``. It will warn
  63. you if you have not included dependencies for the options used.
  64. There are many ways to customize the configurations:
  65. a. Edit ``.kunit/.kunitconfig``. The file should contain the list of kconfig
  66. options required to run the desired tests, including their dependencies.
  67. You may want to remove CONFIG_KUNIT_ALL_TESTS from the ``.kunitconfig`` as
  68. it will enable a number of additional tests that you may not want.
  69. If you need to run on an architecture other than UML see :ref:`kunit-on-qemu`.
  70. b. Enable additional kconfig options on top of ``.kunit/.kunitconfig``.
  71. For example, to include the kernel's linked-list test you can run::
  72. ./tools/testing/kunit/kunit.py run \
  73. --kconfig_add CONFIG_LIST_KUNIT_TEST=y
  74. c. Provide the path of one or more .kunitconfig files from the tree.
  75. For example, to run only ``FAT_FS`` and ``EXT4`` tests you can run::
  76. ./tools/testing/kunit/kunit.py run \
  77. --kunitconfig ./fs/fat/.kunitconfig \
  78. --kunitconfig ./fs/ext4/.kunitconfig
  79. d. If you change the ``.kunitconfig``, kunit.py will trigger a rebuild of the
  80. ``.config`` file. But you can edit the ``.config`` file directly or with
  81. tools like ``make menuconfig O=.kunit``. As long as its a superset of
  82. ``.kunitconfig``, kunit.py won't overwrite your changes.
  83. .. note ::
  84. To save a .kunitconfig after finding a satisfactory configuration::
  85. make savedefconfig O=.kunit
  86. cp .kunit/defconfig .kunit/.kunitconfig
  87. Filtering tests by name
  88. ~~~~~~~~~~~~~~~~~~~~~~~
  89. If you want to be more specific than Kconfig can provide, it is also possible
  90. to select which tests to execute at boot-time by passing a glob filter
  91. (read instructions regarding the pattern in the manpage :manpage:`glob(7)`).
  92. If there is a ``"."`` (period) in the filter, it will be interpreted as a
  93. separator between the name of the test suite and the test case,
  94. otherwise, it will be interpreted as the name of the test suite.
  95. For example, let's assume we are using the default config:
  96. a. inform the name of a test suite, like ``"kunit_executor_test"``,
  97. to run every test case it contains::
  98. ./tools/testing/kunit/kunit.py run "kunit_executor_test"
  99. b. inform the name of a test case prefixed by its test suite,
  100. like ``"example.example_simple_test"``, to run specifically that test case::
  101. ./tools/testing/kunit/kunit.py run "example.example_simple_test"
  102. c. use wildcard characters (``*?[``) to run any test case that matches the pattern,
  103. like ``"*.*64*"`` to run test cases containing ``"64"`` in the name inside
  104. any test suite::
  105. ./tools/testing/kunit/kunit.py run "*.*64*"
  106. Running Tests without the KUnit Wrapper
  107. =======================================
  108. If you do not want to use the KUnit Wrapper (for example: you want code
  109. under test to integrate with other systems, or use a different/
  110. unsupported architecture or configuration), KUnit can be included in
  111. any kernel, and the results are read out and parsed manually.
  112. .. note ::
  113. ``CONFIG_KUNIT`` should not be enabled in a production environment.
  114. Enabling KUnit disables Kernel Address-Space Layout Randomization
  115. (KASLR), and tests may affect the state of the kernel in ways not
  116. suitable for production.
  117. Configuring the Kernel
  118. ----------------------
  119. To enable KUnit itself, you need to enable the ``CONFIG_KUNIT`` Kconfig
  120. option (under Kernel Hacking/Kernel Testing and Coverage in
  121. ``menuconfig``). From there, you can enable any KUnit tests. They
  122. usually have config options ending in ``_KUNIT_TEST``.
  123. KUnit and KUnit tests can be compiled as modules. The tests in a module
  124. will run when the module is loaded.
  125. Running Tests (without KUnit Wrapper)
  126. -------------------------------------
  127. Build and run your kernel. In the kernel log, the test output is printed
  128. out in the TAP format. This will only happen by default if KUnit/tests
  129. are built-in. Otherwise the module will need to be loaded.
  130. .. note ::
  131. Some lines and/or data may get interspersed in the TAP output.
  132. Writing Your First Test
  133. =======================
  134. In your kernel repository, let's add some code that we can test.
  135. 1. Create a file ``drivers/misc/example.h``, which includes:
  136. .. code-block:: c
  137. int misc_example_add(int left, int right);
  138. 2. Create a file ``drivers/misc/example.c``, which includes:
  139. .. code-block:: c
  140. #include <linux/errno.h>
  141. #include "example.h"
  142. int misc_example_add(int left, int right)
  143. {
  144. return left + right;
  145. }
  146. 3. Add the following lines to ``drivers/misc/Kconfig``:
  147. .. code-block:: kconfig
  148. config MISC_EXAMPLE
  149. bool "My example"
  150. 4. Add the following lines to ``drivers/misc/Makefile``:
  151. .. code-block:: make
  152. obj-$(CONFIG_MISC_EXAMPLE) += example.o
  153. Now we are ready to write the test cases.
  154. 1. Add the below test case in ``drivers/misc/example_test.c``:
  155. .. code-block:: c
  156. #include <kunit/test.h>
  157. #include "example.h"
  158. /* Define the test cases. */
  159. static void misc_example_add_test_basic(struct kunit *test)
  160. {
  161. KUNIT_EXPECT_EQ(test, 1, misc_example_add(1, 0));
  162. KUNIT_EXPECT_EQ(test, 2, misc_example_add(1, 1));
  163. KUNIT_EXPECT_EQ(test, 0, misc_example_add(-1, 1));
  164. KUNIT_EXPECT_EQ(test, INT_MAX, misc_example_add(0, INT_MAX));
  165. KUNIT_EXPECT_EQ(test, -1, misc_example_add(INT_MAX, INT_MIN));
  166. }
  167. static void misc_example_test_failure(struct kunit *test)
  168. {
  169. KUNIT_FAIL(test, "This test never passes.");
  170. }
  171. static struct kunit_case misc_example_test_cases[] = {
  172. KUNIT_CASE(misc_example_add_test_basic),
  173. KUNIT_CASE(misc_example_test_failure),
  174. {}
  175. };
  176. static struct kunit_suite misc_example_test_suite = {
  177. .name = "misc-example",
  178. .test_cases = misc_example_test_cases,
  179. };
  180. kunit_test_suite(misc_example_test_suite);
  181. 2. Add the following lines to ``drivers/misc/Kconfig``:
  182. .. code-block:: kconfig
  183. config MISC_EXAMPLE_TEST
  184. tristate "Test for my example" if !KUNIT_ALL_TESTS
  185. depends on MISC_EXAMPLE && KUNIT=y
  186. default KUNIT_ALL_TESTS
  187. 3. Add the following lines to ``drivers/misc/Makefile``:
  188. .. code-block:: make
  189. obj-$(CONFIG_MISC_EXAMPLE_TEST) += example_test.o
  190. 4. Add the following lines to ``.kunit/.kunitconfig``:
  191. .. code-block:: none
  192. CONFIG_MISC_EXAMPLE=y
  193. CONFIG_MISC_EXAMPLE_TEST=y
  194. 5. Run the test:
  195. .. code-block:: bash
  196. ./tools/testing/kunit/kunit.py run
  197. You should see the following failure:
  198. .. code-block:: none
  199. ...
  200. [16:08:57] [PASSED] misc-example:misc_example_add_test_basic
  201. [16:08:57] [FAILED] misc-example:misc_example_test_failure
  202. [16:08:57] EXPECTATION FAILED at drivers/misc/example-test.c:17
  203. [16:08:57] This test never passes.
  204. ...
  205. Congrats! You just wrote your first KUnit test.
  206. Next Steps
  207. ==========
  208. * Documentation/dev-tools/kunit/architecture.rst - KUnit architecture.
  209. * Documentation/dev-tools/kunit/run_wrapper.rst - run kunit_tool.
  210. * Documentation/dev-tools/kunit/run_manual.rst - run tests without kunit_tool.
  211. * Documentation/dev-tools/kunit/usage.rst - write tests.
  212. * Documentation/dev-tools/kunit/tips.rst - best practices with
  213. examples.
  214. * Documentation/dev-tools/kunit/api/index.rst - KUnit APIs
  215. used for testing.
  216. * Documentation/dev-tools/kunit/faq.rst - KUnit common questions and
  217. answers.