146201d47SHarinder Singh.. SPDX-License-Identifier: GPL-2.0
246201d47SHarinder Singh
32327f7e9STales Aparecida=============================
42327f7e9STales AparecidaRunning tests with kunit_tool
52327f7e9STales Aparecida=============================
646201d47SHarinder Singh
746201d47SHarinder SinghWe can either run KUnit tests using kunit_tool or can run tests
846201d47SHarinder Singhmanually, and then use kunit_tool to parse the results. To run tests
946201d47SHarinder Singhmanually, see: Documentation/dev-tools/kunit/run_manual.rst.
1046201d47SHarinder SinghAs long as we can build the kernel, we can run KUnit.
1146201d47SHarinder Singh
1246201d47SHarinder Singhkunit_tool is a Python script which configures and builds a kernel, runs
1346201d47SHarinder Singhtests, and formats the test results.
1446201d47SHarinder Singh
1546201d47SHarinder SinghRun command:
1646201d47SHarinder Singh
1746201d47SHarinder Singh.. code-block::
1846201d47SHarinder Singh
1946201d47SHarinder Singh	./tools/testing/kunit/kunit.py run
2046201d47SHarinder Singh
2146201d47SHarinder SinghWe should see the following:
2246201d47SHarinder Singh
2346201d47SHarinder Singh.. code-block::
2446201d47SHarinder Singh
255ceb9a25STales Aparecida	Configuring KUnit Kernel ...
2646201d47SHarinder Singh	Building KUnit kernel...
2746201d47SHarinder Singh	Starting KUnit kernel...
2846201d47SHarinder Singh
2946201d47SHarinder SinghWe may want to use the following options:
3046201d47SHarinder Singh
3146201d47SHarinder Singh.. code-block::
3246201d47SHarinder Singh
337e2d6194STales Aparecida	./tools/testing/kunit/kunit.py run --timeout=30 --jobs=`nproc --all`
3446201d47SHarinder Singh
3546201d47SHarinder Singh- ``--timeout`` sets a maximum amount of time for tests to run.
3646201d47SHarinder Singh- ``--jobs`` sets the number of threads to build the kernel.
3746201d47SHarinder Singh
3846201d47SHarinder Singhkunit_tool will generate a ``.kunitconfig`` with a default
3946201d47SHarinder Singhconfiguration, if no other ``.kunitconfig`` file exists
4046201d47SHarinder Singh(in the build directory). In addition, it verifies that the
4146201d47SHarinder Singhgenerated ``.config`` file contains the ``CONFIG`` options in the
4246201d47SHarinder Singh``.kunitconfig``.
4346201d47SHarinder SinghIt is also possible to pass a separate ``.kunitconfig`` fragment to
4446201d47SHarinder Singhkunit_tool. This is useful if we have several different groups of
4546201d47SHarinder Singhtests we want to run independently, or if we want to use pre-defined
4646201d47SHarinder Singhtest configs for certain subsystems.
4746201d47SHarinder Singh
4846201d47SHarinder SinghTo use a different ``.kunitconfig`` file (such as one
4946201d47SHarinder Singhprovided to test a particular subsystem), pass it as an option:
5046201d47SHarinder Singh
5146201d47SHarinder Singh.. code-block::
5246201d47SHarinder Singh
5346201d47SHarinder Singh	./tools/testing/kunit/kunit.py run --kunitconfig=fs/ext4/.kunitconfig
5446201d47SHarinder Singh
5546201d47SHarinder SinghTo view kunit_tool flags (optional command-line arguments), run:
5646201d47SHarinder Singh
5746201d47SHarinder Singh.. code-block::
5846201d47SHarinder Singh
5946201d47SHarinder Singh	./tools/testing/kunit/kunit.py run --help
6046201d47SHarinder Singh
612327f7e9STales AparecidaCreating a ``.kunitconfig`` file
622327f7e9STales Aparecida================================
6346201d47SHarinder Singh
6446201d47SHarinder SinghIf we want to run a specific set of tests (rather than those listed
6546201d47SHarinder Singhin the KUnit ``defconfig``), we can provide Kconfig options in the
6646201d47SHarinder Singh``.kunitconfig`` file. For default .kunitconfig, see:
6746201d47SHarinder Singhhttps://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/testing/kunit/configs/default.config.
6846201d47SHarinder SinghA ``.kunitconfig`` is a ``minconfig`` (a .config
6946201d47SHarinder Singhgenerated by running ``make savedefconfig``), used for running a
7046201d47SHarinder Singhspecific set of tests. This file contains the regular Kernel configs
7146201d47SHarinder Singhwith specific test targets. The ``.kunitconfig`` also
7246201d47SHarinder Singhcontains any other config options required by the tests (For example:
7346201d47SHarinder Singhdependencies for features under tests, configs that enable/disable
7446201d47SHarinder Singhcertain code blocks, arch configs and so on).
7546201d47SHarinder Singh
7646201d47SHarinder SinghTo create a ``.kunitconfig``, using the KUnit ``defconfig``:
7746201d47SHarinder Singh
7846201d47SHarinder Singh.. code-block::
7946201d47SHarinder Singh
8046201d47SHarinder Singh	cd $PATH_TO_LINUX_REPO
8146201d47SHarinder Singh	cp tools/testing/kunit/configs/default.config .kunit/.kunitconfig
8246201d47SHarinder Singh
8346201d47SHarinder SinghWe can then add any other Kconfig options. For example:
8446201d47SHarinder Singh
8546201d47SHarinder Singh.. code-block::
8646201d47SHarinder Singh
8746201d47SHarinder Singh	CONFIG_LIST_KUNIT_TEST=y
8846201d47SHarinder Singh
8946201d47SHarinder Singhkunit_tool ensures that all config options in ``.kunitconfig`` are
9046201d47SHarinder Singhset in the kernel ``.config`` before running the tests. It warns if we
9146201d47SHarinder Singhhave not included the options dependencies.
9246201d47SHarinder Singh
9346201d47SHarinder Singh.. note:: Removing something from the ``.kunitconfig`` will
9446201d47SHarinder Singh   not rebuild the ``.config file``. The configuration is only
9546201d47SHarinder Singh   updated if the ``.kunitconfig`` is not a subset of ``.config``.
9646201d47SHarinder Singh   This means that we can use other tools
9746201d47SHarinder Singh   (For example: ``make menuconfig``) to adjust other config options.
9846201d47SHarinder Singh   The build dir needs to be set for ``make menuconfig`` to
9946201d47SHarinder Singh   work, therefore  by default use ``make O=.kunit menuconfig``.
10046201d47SHarinder Singh
1012327f7e9STales AparecidaConfiguring, building, and running tests
1022327f7e9STales Aparecida========================================
10346201d47SHarinder Singh
10446201d47SHarinder SinghIf we want to make manual changes to the KUnit build process, we
10546201d47SHarinder Singhcan run part of the KUnit build process independently.
10646201d47SHarinder SinghWhen running kunit_tool, from a ``.kunitconfig``, we can generate a
10746201d47SHarinder Singh``.config`` by using the ``config`` argument:
10846201d47SHarinder Singh
10946201d47SHarinder Singh.. code-block::
11046201d47SHarinder Singh
11146201d47SHarinder Singh	./tools/testing/kunit/kunit.py config
11246201d47SHarinder Singh
11346201d47SHarinder SinghTo build a KUnit kernel from the current ``.config``, we can use the
11446201d47SHarinder Singh``build`` argument:
11546201d47SHarinder Singh
11646201d47SHarinder Singh.. code-block::
11746201d47SHarinder Singh
11846201d47SHarinder Singh	./tools/testing/kunit/kunit.py build
11946201d47SHarinder Singh
12046201d47SHarinder SinghIf we already have built UML kernel with built-in KUnit tests, we
12146201d47SHarinder Singhcan run the kernel, and display the test results with the ``exec``
12246201d47SHarinder Singhargument:
12346201d47SHarinder Singh
12446201d47SHarinder Singh.. code-block::
12546201d47SHarinder Singh
12646201d47SHarinder Singh	./tools/testing/kunit/kunit.py exec
12746201d47SHarinder Singh
1282327f7e9STales AparecidaThe ``run`` command discussed in section: **Running tests with kunit_tool**,
12946201d47SHarinder Singhis equivalent to running the above three commands in sequence.
13046201d47SHarinder Singh
1312327f7e9STales AparecidaParsing test results
1322327f7e9STales Aparecida====================
13346201d47SHarinder Singh
13446201d47SHarinder SinghKUnit tests output displays results in TAP (Test Anything Protocol)
13546201d47SHarinder Singhformat. When running tests, kunit_tool parses this output and prints
13646201d47SHarinder Singha summary. To see the raw test results in TAP format, we can pass the
13746201d47SHarinder Singh``--raw_output`` argument:
13846201d47SHarinder Singh
13946201d47SHarinder Singh.. code-block::
14046201d47SHarinder Singh
14146201d47SHarinder Singh	./tools/testing/kunit/kunit.py run --raw_output
14246201d47SHarinder Singh
14346201d47SHarinder SinghIf we have KUnit results in the raw TAP format, we can parse them and
14446201d47SHarinder Singhprint the human-readable summary with the ``parse`` command for
14546201d47SHarinder Singhkunit_tool. This accepts a filename for an argument, or will read from
14646201d47SHarinder Singhstandard input.
14746201d47SHarinder Singh
14846201d47SHarinder Singh.. code-block:: bash
14946201d47SHarinder Singh
15046201d47SHarinder Singh	# Reading from a file
15146201d47SHarinder Singh	./tools/testing/kunit/kunit.py parse /var/log/dmesg
15246201d47SHarinder Singh	# Reading from stdin
15346201d47SHarinder Singh	dmesg | ./tools/testing/kunit/kunit.py parse
15446201d47SHarinder Singh
1552327f7e9STales AparecidaFiltering tests
1562327f7e9STales Aparecida===============
15746201d47SHarinder Singh
15846201d47SHarinder SinghBy passing a bash style glob filter to the ``exec`` or ``run``
15946201d47SHarinder Singhcommands, we can run a subset of the tests built into a kernel . For
16046201d47SHarinder Singhexample: if we only want to run KUnit resource tests, use:
16146201d47SHarinder Singh
16246201d47SHarinder Singh.. code-block::
16346201d47SHarinder Singh
16446201d47SHarinder Singh	./tools/testing/kunit/kunit.py run 'kunit-resource*'
16546201d47SHarinder Singh
16646201d47SHarinder SinghThis uses the standard glob format with wildcard characters.
16746201d47SHarinder Singh
16843ca52a9STales Aparecida.. _kunit-on-qemu:
16943ca52a9STales Aparecida
1702327f7e9STales AparecidaRunning tests on QEMU
1712327f7e9STales Aparecida=====================
17246201d47SHarinder Singh
17346201d47SHarinder Singhkunit_tool supports running tests on  qemu as well as
17446201d47SHarinder Singhvia UML. To run tests on qemu, by default it requires two flags:
17546201d47SHarinder Singh
17646201d47SHarinder Singh- ``--arch``: Selects a configs collection (Kconfig, qemu config options
17746201d47SHarinder Singh  and so on), that allow KUnit tests to be run on the specified
17846201d47SHarinder Singh  architecture in a minimal way. The architecture argument is same as
17946201d47SHarinder Singh  the option name passed to the ``ARCH`` variable used by Kbuild.
18046201d47SHarinder Singh  Not all architectures currently support this flag, but we can use
18146201d47SHarinder Singh  ``--qemu_config`` to handle it. If ``um`` is passed (or this flag
18246201d47SHarinder Singh  is ignored), the tests will run via UML. Non-UML architectures,
18346201d47SHarinder Singh  for example: i386, x86_64, arm and so on; run on qemu.
18446201d47SHarinder Singh
18546201d47SHarinder Singh- ``--cross_compile``: Specifies the Kbuild toolchain. It passes the
18646201d47SHarinder Singh  same argument as passed to the ``CROSS_COMPILE`` variable used by
18746201d47SHarinder Singh  Kbuild. As a reminder, this will be the prefix for the toolchain
18846201d47SHarinder Singh  binaries such as GCC. For example:
18946201d47SHarinder Singh
19046201d47SHarinder Singh  - ``sparc64-linux-gnu`` if we have the sparc toolchain installed on
19146201d47SHarinder Singh    our system.
19246201d47SHarinder Singh
19346201d47SHarinder Singh  - ``$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux``
19446201d47SHarinder Singh    if we have downloaded the microblaze toolchain from the 0-day
19546201d47SHarinder Singh    website to a directory in our home directory called toolchains.
19646201d47SHarinder Singh
1977635778bSDavid GowThis means that for most architectures, running under qemu is as simple as:
1987635778bSDavid Gow
1997635778bSDavid Gow.. code-block:: bash
2007635778bSDavid Gow
2017635778bSDavid Gow	./tools/testing/kunit/kunit.py run --arch=x86_64
2027635778bSDavid Gow
2037635778bSDavid GowWhen cross-compiling, we'll likely need to specify a different toolchain, for
2047635778bSDavid Gowexample:
2057635778bSDavid Gow
2067635778bSDavid Gow.. code-block:: bash
2077635778bSDavid Gow
2087635778bSDavid Gow	./tools/testing/kunit/kunit.py run \
2097635778bSDavid Gow		--arch=s390 \
2107635778bSDavid Gow		--cross_compile=s390x-linux-gnu-
2117635778bSDavid Gow
21246201d47SHarinder SinghIf we want to run KUnit tests on an architecture not supported by
21346201d47SHarinder Singhthe ``--arch`` flag, or want to run KUnit tests on qemu using a
21446201d47SHarinder Singhnon-default configuration; then we can write our own``QemuConfig``.
21546201d47SHarinder SinghThese ``QemuConfigs`` are written in Python. They have an import line
21646201d47SHarinder Singh``from..qemu_config import QemuArchParams`` at the top of the file.
21746201d47SHarinder SinghThe file must contain a variable called ``QEMU_ARCH`` that has an
21846201d47SHarinder Singhinstance of ``QemuArchParams`` assigned to it. See example in:
21946201d47SHarinder Singh``tools/testing/kunit/qemu_configs/x86_64.py``.
22046201d47SHarinder Singh
22146201d47SHarinder SinghOnce we have a ``QemuConfig``, we can pass it into kunit_tool,
22246201d47SHarinder Singhusing the ``--qemu_config`` flag. When used, this flag replaces the
22346201d47SHarinder Singh``--arch`` flag. For example: using
22446201d47SHarinder Singh``tools/testing/kunit/qemu_configs/x86_64.py``, the invocation appear
22546201d47SHarinder Singhas
22646201d47SHarinder Singh
22746201d47SHarinder Singh.. code-block:: bash
22846201d47SHarinder Singh
22946201d47SHarinder Singh	./tools/testing/kunit/kunit.py run \
23046201d47SHarinder Singh		--timeout=60 \
23146201d47SHarinder Singh		--jobs=12 \
23246201d47SHarinder Singh		--qemu_config=./tools/testing/kunit/qemu_configs/x86_64.py
23346201d47SHarinder Singh
2342327f7e9STales AparecidaRunning command-line arguments
2352327f7e9STales Aparecida==============================
23646201d47SHarinder Singh
23746201d47SHarinder Singhkunit_tool has a number of other command-line arguments which can
23895706387SSadiya Kazibe useful for our test environment. Below are the most commonly used
23946201d47SHarinder Singhcommand line arguments:
24046201d47SHarinder Singh
24146201d47SHarinder Singh- ``--help``: Lists all available options. To list common options,
24246201d47SHarinder Singh  place ``--help`` before the command. To list options specific to that
24346201d47SHarinder Singh  command, place ``--help`` after the command.
24446201d47SHarinder Singh
24546201d47SHarinder Singh  .. note:: Different commands (``config``, ``build``, ``run``, etc)
24646201d47SHarinder Singh            have different supported options.
24746201d47SHarinder Singh- ``--build_dir``: Specifies kunit_tool build directory. It includes
24846201d47SHarinder Singh  the ``.kunitconfig``, ``.config`` files and compiled kernel.
24946201d47SHarinder Singh
25046201d47SHarinder Singh- ``--make_options``: Specifies additional options to pass to make, when
25146201d47SHarinder Singh  compiling a kernel (using ``build`` or ``run`` commands). For example:
25246201d47SHarinder Singh  to enable compiler warnings, we can pass ``--make_options W=1``.
25346201d47SHarinder Singh
254e98c4f6aSDavid Gow- ``--alltests``: Enable a predefined set of options in order to build
255e98c4f6aSDavid Gow  as many tests as possible.
25646201d47SHarinder Singh
257e98c4f6aSDavid Gow  .. note:: The list of enabled options can be found in
258e98c4f6aSDavid Gow            ``tools/testing/kunit/configs/all_tests.config``.
259e98c4f6aSDavid Gow
260e98c4f6aSDavid Gow            If you only want to enable all tests with otherwise satisfied
261e98c4f6aSDavid Gow            dependencies, instead add ``CONFIG_KUNIT_ALL_TESTS=y`` to your
262e98c4f6aSDavid Gow            ``.kunitconfig``.
26395706387SSadiya Kazi
26495706387SSadiya Kazi- ``--kunitconfig``: Specifies the path or the directory of the ``.kunitconfig``
26595706387SSadiya Kazi  file. For example:
26695706387SSadiya Kazi
26795706387SSadiya Kazi  - ``lib/kunit/.kunitconfig`` can be the path of the file.
26895706387SSadiya Kazi
26995706387SSadiya Kazi  - ``lib/kunit`` can be the directory in which the file is located.
27095706387SSadiya Kazi
27195706387SSadiya Kazi  This file is used to build and run with a predefined set of tests
27295706387SSadiya Kazi  and their dependencies. For example, to run tests for a given subsystem.
27395706387SSadiya Kazi
27495706387SSadiya Kazi- ``--kconfig_add``: Specifies additional configuration options to be
27595706387SSadiya Kazi  appended to the ``.kunitconfig`` file. For example:
27695706387SSadiya Kazi
27795706387SSadiya Kazi  .. code-block::
27895706387SSadiya Kazi
27995706387SSadiya Kazi	./tools/testing/kunit/kunit.py run --kconfig_add CONFIG_KASAN=y
28095706387SSadiya Kazi
28195706387SSadiya Kazi- ``--arch``: Runs tests on the specified architecture. The architecture
28295706387SSadiya Kazi  argument is same as the Kbuild ARCH environment variable.
28395706387SSadiya Kazi  For example, i386, x86_64, arm, um, etc. Non-UML architectures run on qemu.
28495706387SSadiya Kazi  Default is `um`.
28595706387SSadiya Kazi
28695706387SSadiya Kazi- ``--cross_compile``: Specifies the Kbuild toolchain. It passes the
28795706387SSadiya Kazi  same argument as passed to the ``CROSS_COMPILE`` variable used by
28895706387SSadiya Kazi  Kbuild. This will be the prefix for the toolchain
28995706387SSadiya Kazi  binaries such as GCC. For example:
29095706387SSadiya Kazi
29195706387SSadiya Kazi  - ``sparc64-linux-gnu-`` if we have the sparc toolchain installed on
29295706387SSadiya Kazi    our system.
29395706387SSadiya Kazi
29495706387SSadiya Kazi  - ``$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux``
29595706387SSadiya Kazi    if we have downloaded the microblaze toolchain from the 0-day
29695706387SSadiya Kazi    website to a specified path in our home directory called toolchains.
29795706387SSadiya Kazi
29895706387SSadiya Kazi- ``--qemu_config``: Specifies the path to a file containing a
29995706387SSadiya Kazi  custom qemu architecture definition. This should be a python file
30095706387SSadiya Kazi  containing a `QemuArchParams` object.
30195706387SSadiya Kazi
30295706387SSadiya Kazi- ``--qemu_args``: Specifies additional qemu arguments, for example, ``-smp 8``.
30395706387SSadiya Kazi
30495706387SSadiya Kazi- ``--jobs``: Specifies the number of jobs (commands) to run simultaneously.
30595706387SSadiya Kazi  By default, this is set to the number of cores on your system.
30695706387SSadiya Kazi
30795706387SSadiya Kazi- ``--timeout``: Specifies the maximum number of seconds allowed for all tests to run.
30895706387SSadiya Kazi  This does not include the time taken to build the tests.
30995706387SSadiya Kazi
31095706387SSadiya Kazi- ``--kernel_args``: Specifies additional kernel command-line arguments. May be repeated.
31195706387SSadiya Kazi
31295706387SSadiya Kazi- ``--run_isolated``: If set, boots the kernel for each individual suite/test.
31395706387SSadiya Kazi  This is useful for debugging a non-hermetic test, one that
31495706387SSadiya Kazi  might pass/fail based on what ran before it.
31595706387SSadiya Kazi
31695706387SSadiya Kazi- ``--raw_output``: If set, generates unformatted output from kernel. Possible options are:
31795706387SSadiya Kazi
31895706387SSadiya Kazi   - ``all``: To view the full kernel output, use ``--raw_output=all``.
31995706387SSadiya Kazi
32095706387SSadiya Kazi   - ``kunit``: This is the default option and filters to KUnit output. Use ``--raw_output`` or ``--raw_output=kunit``.
32195706387SSadiya Kazi
32295706387SSadiya Kazi- ``--json``: If set, stores the test results in a JSON format and prints to `stdout` or
32395706387SSadiya Kazi  saves to a file if a filename is specified.
324*e5885866SRae Moar
325*e5885866SRae Moar- ``--filter``: Specifies filters on test attributes, for example, ``speed!=slow``.
326*e5885866SRae Moar  Multiple filters can be used by wrapping input in quotes and separating filters
327*e5885866SRae Moar  by commas. Example: ``--filter "speed>slow, module=example"``.
328*e5885866SRae Moar
329*e5885866SRae Moar- ``--filter_action``: If set to ``skip``, filtered tests will be shown as skipped
330*e5885866SRae Moar  in the output rather than showing no output.
331*e5885866SRae Moar
332*e5885866SRae Moar- ``--list_tests``: If set, lists all tests that will be run.
333*e5885866SRae Moar
334*e5885866SRae Moar- ``--list_tests_attr``: If set, lists all tests that will be run and all of their
335*e5885866SRae Moar  attributes.
336