run_wrapper.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. - ``--cross_compile``: Specifies the Kbuild toolchain. It passes the
  128. same argument as passed to the ``CROSS_COMPILE`` variable used by
  129. Kbuild. As a reminder, this will be the prefix for the toolchain
  130. binaries such as GCC. For example:
  131. - ``sparc64-linux-gnu`` if we have the sparc toolchain installed on
  132. our system.
  133. - ``$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux``
  134. if we have downloaded the microblaze toolchain from the 0-day
  135. website to a directory in our home directory called toolchains.
  136. This means that for most architectures, running under qemu is as simple as:
  137. .. code-block:: bash
  138. ./tools/testing/kunit/kunit.py run --arch=x86_64
  139. When cross-compiling, we'll likely need to specify a different toolchain, for
  140. example:
  141. .. code-block:: bash
  142. ./tools/testing/kunit/kunit.py run \
  143. --arch=s390 \
  144. --cross_compile=s390x-linux-gnu-
  145. If we want to run KUnit tests on an architecture not supported by
  146. the ``--arch`` flag, or want to run KUnit tests on qemu using a
  147. non-default configuration; then we can write our own``QemuConfig``.
  148. These ``QemuConfigs`` are written in Python. They have an import line
  149. ``from..qemu_config import QemuArchParams`` at the top of the file.
  150. The file must contain a variable called ``QEMU_ARCH`` that has an
  151. instance of ``QemuArchParams`` assigned to it. See example in:
  152. ``tools/testing/kunit/qemu_configs/x86_64.py``.
  153. Once we have a ``QemuConfig``, we can pass it into kunit_tool,
  154. using the ``--qemu_config`` flag. When used, this flag replaces the
  155. ``--arch`` flag. For example: using
  156. ``tools/testing/kunit/qemu_configs/x86_64.py``, the invocation appear
  157. as
  158. .. code-block:: bash
  159. ./tools/testing/kunit/kunit.py run \
  160. --timeout=60 \
  161. --jobs=12 \
  162. --qemu_config=./tools/testing/kunit/qemu_configs/x86_64.py
  163. Running command-line arguments
  164. ==============================
  165. kunit_tool has a number of other command-line arguments which can
  166. be useful for our test environment. Below are the most commonly used
  167. command line arguments:
  168. - ``--help``: Lists all available options. To list common options,
  169. place ``--help`` before the command. To list options specific to that
  170. command, place ``--help`` after the command.
  171. .. note:: Different commands (``config``, ``build``, ``run``, etc)
  172. have different supported options.
  173. - ``--build_dir``: Specifies kunit_tool build directory. It includes
  174. the ``.kunitconfig``, ``.config`` files and compiled kernel.
  175. - ``--make_options``: Specifies additional options to pass to make, when
  176. compiling a kernel (using ``build`` or ``run`` commands). For example:
  177. to enable compiler warnings, we can pass ``--make_options W=1``.
  178. - ``--alltests``: Enable a predefined set of options in order to build
  179. as many tests as possible.
  180. .. note:: The list of enabled options can be found in
  181. ``tools/testing/kunit/configs/all_tests.config``.
  182. If you only want to enable all tests with otherwise satisfied
  183. dependencies, instead add ``CONFIG_KUNIT_ALL_TESTS=y`` to your
  184. ``.kunitconfig``.
  185. - ``--kunitconfig``: Specifies the path or the directory of the ``.kunitconfig``
  186. file. For example:
  187. - ``lib/kunit/.kunitconfig`` can be the path of the file.
  188. - ``lib/kunit`` can be the directory in which the file is located.
  189. This file is used to build and run with a predefined set of tests
  190. and their dependencies. For example, to run tests for a given subsystem.
  191. - ``--kconfig_add``: Specifies additional configuration options to be
  192. appended to the ``.kunitconfig`` file. For example:
  193. .. code-block::
  194. ./tools/testing/kunit/kunit.py run --kconfig_add CONFIG_KASAN=y
  195. - ``--arch``: Runs tests on the specified architecture. The architecture
  196. argument is same as the Kbuild ARCH environment variable.
  197. For example, i386, x86_64, arm, um, etc. Non-UML architectures run on qemu.
  198. Default is `um`.
  199. - ``--cross_compile``: Specifies the Kbuild toolchain. It passes the
  200. same argument as passed to the ``CROSS_COMPILE`` variable used by
  201. Kbuild. This will be the prefix for the toolchain
  202. binaries such as GCC. For example:
  203. - ``sparc64-linux-gnu-`` if we have the sparc toolchain installed on
  204. our system.
  205. - ``$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux``
  206. if we have downloaded the microblaze toolchain from the 0-day
  207. website to a specified path in our home directory called toolchains.
  208. - ``--qemu_config``: Specifies the path to a file containing a
  209. custom qemu architecture definition. This should be a python file
  210. containing a `QemuArchParams` object.
  211. - ``--qemu_args``: Specifies additional qemu arguments, for example, ``-smp 8``.
  212. - ``--jobs``: Specifies the number of jobs (commands) to run simultaneously.
  213. By default, this is set to the number of cores on your system.
  214. - ``--timeout``: Specifies the maximum number of seconds allowed for all tests to run.
  215. This does not include the time taken to build the tests.
  216. - ``--kernel_args``: Specifies additional kernel command-line arguments. May be repeated.
  217. - ``--run_isolated``: If set, boots the kernel for each individual suite/test.
  218. This is useful for debugging a non-hermetic test, one that
  219. might pass/fail based on what ran before it.
  220. - ``--raw_output``: If set, generates unformatted output from kernel. Possible options are:
  221. - ``all``: To view the full kernel output, use ``--raw_output=all``.
  222. - ``kunit``: This is the default option and filters to KUnit output. Use ``--raw_output`` or ``--raw_output=kunit``.
  223. - ``--json``: If set, stores the test results in a JSON format and prints to `stdout` or
  224. saves to a file if a filename is specified.