run_wrapper.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =============================
  3. Running tests with kunit_tool
  4. =============================
  5. We can either run KUnit tests using kunit_tool or can run tests
  6. manually, and then use kunit_tool to parse the results. To run tests
  7. manually, see: Documentation/dev-tools/kunit/run_manual.rst.
  8. As long as we can build the kernel, we can run KUnit.
  9. kunit_tool is a Python script which configures and builds a kernel, runs
  10. tests, and formats the test results.
  11. Run command:
  12. .. code-block::
  13. ./tools/testing/kunit/kunit.py run
  14. We should see the following:
  15. .. code-block::
  16. Configuring KUnit Kernel ...
  17. Building KUnit kernel...
  18. Starting KUnit kernel...
  19. We may want to use the following options:
  20. .. code-block::
  21. ./tools/testing/kunit/kunit.py run --timeout=30 --jobs=`nproc --all`
  22. - ``--timeout`` sets a maximum amount of time for tests to run.
  23. - ``--jobs`` sets the number of threads to build the kernel.
  24. kunit_tool will generate a ``.kunitconfig`` with a default
  25. configuration, if no other ``.kunitconfig`` file exists
  26. (in the build directory). In addition, it verifies that the
  27. generated ``.config`` file contains the ``CONFIG`` options in the
  28. ``.kunitconfig``.
  29. It is also possible to pass a separate ``.kunitconfig`` fragment to
  30. kunit_tool. This is useful if we have several different groups of
  31. tests we want to run independently, or if we want to use pre-defined
  32. test configs for certain subsystems.
  33. To use a different ``.kunitconfig`` file (such as one
  34. provided to test a particular subsystem), pass it as an option:
  35. .. code-block::
  36. ./tools/testing/kunit/kunit.py run --kunitconfig=fs/ext4/.kunitconfig
  37. To view kunit_tool flags (optional command-line arguments), run:
  38. .. code-block::
  39. ./tools/testing/kunit/kunit.py run --help
  40. Creating a ``.kunitconfig`` file
  41. ================================
  42. If we want to run a specific set of tests (rather than those listed
  43. in the KUnit ``defconfig``), we can provide Kconfig options in the
  44. ``.kunitconfig`` file. For default .kunitconfig, see:
  45. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/testing/kunit/configs/default.config.
  46. A ``.kunitconfig`` is a ``minconfig`` (a .config
  47. generated by running ``make savedefconfig``), used for running a
  48. specific set of tests. This file contains the regular Kernel configs
  49. with specific test targets. The ``.kunitconfig`` also
  50. contains any other config options required by the tests (For example:
  51. dependencies for features under tests, configs that enable/disable
  52. certain code blocks, arch configs and so on).
  53. To create a ``.kunitconfig``, using the KUnit ``defconfig``:
  54. .. code-block::
  55. cd $PATH_TO_LINUX_REPO
  56. cp tools/testing/kunit/configs/default.config .kunit/.kunitconfig
  57. We can then add any other Kconfig options. For example:
  58. .. code-block::
  59. CONFIG_LIST_KUNIT_TEST=y
  60. kunit_tool ensures that all config options in ``.kunitconfig`` are
  61. set in the kernel ``.config`` before running the tests. It warns if we
  62. have not included the options dependencies.
  63. .. note:: Removing something from the ``.kunitconfig`` will
  64. not rebuild the ``.config file``. The configuration is only
  65. updated if the ``.kunitconfig`` is not a subset of ``.config``.
  66. This means that we can use other tools
  67. (For example: ``make menuconfig``) to adjust other config options.
  68. The build dir needs to be set for ``make menuconfig`` to
  69. work, therefore by default use ``make O=.kunit menuconfig``.
  70. Configuring, building, and running tests
  71. ========================================
  72. If we want to make manual changes to the KUnit build process, we
  73. can run part of the KUnit build process independently.
  74. When running kunit_tool, from a ``.kunitconfig``, we can generate a
  75. ``.config`` by using the ``config`` argument:
  76. .. code-block::
  77. ./tools/testing/kunit/kunit.py config
  78. To build a KUnit kernel from the current ``.config``, we can use the
  79. ``build`` argument:
  80. .. code-block::
  81. ./tools/testing/kunit/kunit.py build
  82. If we already have built UML kernel with built-in KUnit tests, we
  83. can run the kernel, and display the test results with the ``exec``
  84. argument:
  85. .. code-block::
  86. ./tools/testing/kunit/kunit.py exec
  87. The ``run`` command discussed in section: **Running tests with kunit_tool**,
  88. is equivalent to running the above three commands in sequence.
  89. Parsing test results
  90. ====================
  91. KUnit tests output displays results in TAP (Test Anything Protocol)
  92. format. When running tests, kunit_tool parses this output and prints
  93. a summary. To see the raw test results in TAP format, we can pass the
  94. ``--raw_output`` argument:
  95. .. code-block::
  96. ./tools/testing/kunit/kunit.py run --raw_output
  97. If we have KUnit results in the raw TAP format, we can parse them and
  98. print the human-readable summary with the ``parse`` command for
  99. kunit_tool. This accepts a filename for an argument, or will read from
  100. standard input.
  101. .. code-block:: bash
  102. # Reading from a file
  103. ./tools/testing/kunit/kunit.py parse /var/log/dmesg
  104. # Reading from stdin
  105. dmesg | ./tools/testing/kunit/kunit.py parse
  106. Filtering tests
  107. ===============
  108. By passing a bash style glob filter to the ``exec`` or ``run``
  109. commands, we can run a subset of the tests built into a kernel . For
  110. example: if we only want to run KUnit resource tests, use:
  111. .. code-block::
  112. ./tools/testing/kunit/kunit.py run 'kunit-resource*'
  113. This uses the standard glob format with wildcard characters.
  114. .. _kunit-on-qemu:
  115. Running tests on QEMU
  116. =====================
  117. kunit_tool supports running tests on qemu as well as
  118. via UML. To run tests on qemu, by default it requires two flags:
  119. - ``--arch``: Selects a configs collection (Kconfig, qemu config options
  120. and so on), that allow KUnit tests to be run on the specified
  121. architecture in a minimal way. The architecture argument is same as
  122. the option name passed to the ``ARCH`` variable used by Kbuild.
  123. Not all architectures currently support this flag, but we can use
  124. ``--qemu_config`` to handle it. If ``um`` is passed (or this flag
  125. is ignored), the tests will run via UML. Non-UML architectures,
  126. for example: i386, x86_64, arm and so on; run on qemu.
  127. ``--arch help`` lists all valid ``--arch`` values.
  128. - ``--cross_compile``: Specifies the Kbuild toolchain. It passes the
  129. same argument as passed to the ``CROSS_COMPILE`` variable used by
  130. Kbuild. As a reminder, this will be the prefix for the toolchain
  131. binaries such as GCC. For example:
  132. - ``sparc64-linux-gnu`` if we have the sparc toolchain installed on
  133. our system.
  134. - ``$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux``
  135. if we have downloaded the microblaze toolchain from the 0-day
  136. website to a directory in our home directory called toolchains.
  137. This means that for most architectures, running under qemu is as simple as:
  138. .. code-block:: bash
  139. ./tools/testing/kunit/kunit.py run --arch=x86_64
  140. When cross-compiling, we'll likely need to specify a different toolchain, for
  141. example:
  142. .. code-block:: bash
  143. ./tools/testing/kunit/kunit.py run \
  144. --arch=s390 \
  145. --cross_compile=s390x-linux-gnu-
  146. If we want to run KUnit tests on an architecture not supported by
  147. the ``--arch`` flag, or want to run KUnit tests on qemu using a
  148. non-default configuration; then we can write our own``QemuConfig``.
  149. These ``QemuConfigs`` are written in Python. They have an import line
  150. ``from..qemu_config import QemuArchParams`` at the top of the file.
  151. The file must contain a variable called ``QEMU_ARCH`` that has an
  152. instance of ``QemuArchParams`` assigned to it. See example in:
  153. ``tools/testing/kunit/qemu_configs/x86_64.py``.
  154. Once we have a ``QemuConfig``, we can pass it into kunit_tool,
  155. using the ``--qemu_config`` flag. When used, this flag replaces the
  156. ``--arch`` flag. For example: using
  157. ``tools/testing/kunit/qemu_configs/x86_64.py``, the invocation appear
  158. as
  159. .. code-block:: bash
  160. ./tools/testing/kunit/kunit.py run \
  161. --timeout=60 \
  162. --jobs=12 \
  163. --qemu_config=./tools/testing/kunit/qemu_configs/x86_64.py
  164. Running command-line arguments
  165. ==============================
  166. kunit_tool has a number of other command-line arguments which can
  167. be useful for our test environment. Below are the most commonly used
  168. command line arguments:
  169. - ``--help``: Lists all available options. To list common options,
  170. place ``--help`` before the command. To list options specific to that
  171. command, place ``--help`` after the command.
  172. .. note:: Different commands (``config``, ``build``, ``run``, etc)
  173. have different supported options.
  174. - ``--build_dir``: Specifies kunit_tool build directory. It includes
  175. the ``.kunitconfig``, ``.config`` files and compiled kernel.
  176. - ``--make_options``: Specifies additional options to pass to make, when
  177. compiling a kernel (using ``build`` or ``run`` commands). For example:
  178. to enable compiler warnings, we can pass ``--make_options W=1``.
  179. - ``--alltests``: Enable a predefined set of options in order to build
  180. as many tests as possible.
  181. .. note:: The list of enabled options can be found in
  182. ``tools/testing/kunit/configs/all_tests.config``.
  183. If you only want to enable all tests with otherwise satisfied
  184. dependencies, instead add ``CONFIG_KUNIT_ALL_TESTS=y`` to your
  185. ``.kunitconfig``.
  186. - ``--kunitconfig``: Specifies the path or the directory of the ``.kunitconfig``
  187. file. For example:
  188. - ``lib/kunit/.kunitconfig`` can be the path of the file.
  189. - ``lib/kunit`` can be the directory in which the file is located.
  190. This file is used to build and run with a predefined set of tests
  191. and their dependencies. For example, to run tests for a given subsystem.
  192. - ``--kconfig_add``: Specifies additional configuration options to be
  193. appended to the ``.kunitconfig`` file. For example:
  194. .. code-block::
  195. ./tools/testing/kunit/kunit.py run --kconfig_add CONFIG_KASAN=y
  196. - ``--arch``: Runs tests on the specified architecture. The architecture
  197. argument is same as the Kbuild ARCH environment variable.
  198. For example, i386, x86_64, arm, um, etc. Non-UML architectures run on qemu.
  199. Default is `um`.
  200. - ``--cross_compile``: Specifies the Kbuild toolchain. It passes the
  201. same argument as passed to the ``CROSS_COMPILE`` variable used by
  202. Kbuild. This will be the prefix for the toolchain
  203. binaries such as GCC. For example:
  204. - ``sparc64-linux-gnu-`` if we have the sparc toolchain installed on
  205. our system.
  206. - ``$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux``
  207. if we have downloaded the microblaze toolchain from the 0-day
  208. website to a specified path in our home directory called toolchains.
  209. - ``--qemu_config``: Specifies the path to a file containing a
  210. custom qemu architecture definition. This should be a python file
  211. containing a `QemuArchParams` object.
  212. - ``--qemu_args``: Specifies additional qemu arguments, for example, ``-smp 8``.
  213. - ``--jobs``: Specifies the number of jobs (commands) to run simultaneously.
  214. By default, this is set to the number of cores on your system.
  215. - ``--timeout``: Specifies the maximum number of seconds allowed for all tests to run.
  216. This does not include the time taken to build the tests.
  217. - ``--kernel_args``: Specifies additional kernel command-line arguments. May be repeated.
  218. - ``--run_isolated``: If set, boots the kernel for each individual suite/test.
  219. This is useful for debugging a non-hermetic test, one that
  220. might pass/fail based on what ran before it.
  221. - ``--raw_output``: If set, generates unformatted output from kernel. Possible options are:
  222. - ``all``: To view the full kernel output, use ``--raw_output=all``.
  223. - ``kunit``: This is the default option and filters to KUnit output. Use ``--raw_output`` or ``--raw_output=kunit``.
  224. - ``--json``: If set, stores the test results in a JSON format and prints to `stdout` or
  225. saves to a file if a filename is specified.
  226. - ``--filter``: Specifies filters on test attributes, for example, ``speed!=slow``.
  227. Multiple filters can be used by wrapping input in quotes and separating filters
  228. by commas. Example: ``--filter "speed>slow, module=example"``.
  229. - ``--filter_action``: If set to ``skip``, filtered tests will be shown as skipped
  230. in the output rather than showing no output.
  231. - ``--list_tests``: If set, lists all tests that will be run.
  232. - ``--list_tests_attr``: If set, lists all tests that will be run and all of their
  233. attributes.
  234. - ``--list_suites``: If set, lists all suites that will be run.
  235. Command-line completion
  236. ==============================
  237. The kunit_tool comes with a bash completion script:
  238. .. code-block:: bash
  239. source tools/testing/kunit/kunit-completion.sh