2019-09-23 17:02:45 +08:00
|
|
|
.. SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
===============
|
|
|
|
Getting Started
|
|
|
|
===============
|
|
|
|
|
|
|
|
Installing dependencies
|
|
|
|
=======================
|
|
|
|
KUnit has the same dependencies as the Linux kernel. As long as you can build
|
|
|
|
the kernel, you can run KUnit.
|
|
|
|
|
2020-02-29 03:18:21 +08:00
|
|
|
Running tests with the KUnit Wrapper
|
|
|
|
====================================
|
|
|
|
Included with KUnit is a simple Python wrapper which runs tests under User Mode
|
|
|
|
Linux, and formats the test results.
|
2019-09-23 17:02:45 +08:00
|
|
|
|
|
|
|
The wrapper can be run with:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2020-07-07 12:31:20 +08:00
|
|
|
./tools/testing/kunit/kunit.py run
|
2019-11-20 09:17:00 +08:00
|
|
|
|
2020-02-29 03:18:21 +08:00
|
|
|
For more information on this wrapper (also called kunit_tool) check out the
|
2019-11-20 09:17:00 +08:00
|
|
|
:doc:`kunit-tool` page.
|
2019-09-23 17:02:45 +08:00
|
|
|
|
2019-12-20 13:14:07 +08:00
|
|
|
Creating a .kunitconfig
|
2020-02-29 03:18:21 +08:00
|
|
|
-----------------------
|
|
|
|
If you want to run a specific set of tests (rather than those listed in the
|
|
|
|
KUnit defconfig), you can provide Kconfig options in the ``.kunitconfig`` file.
|
|
|
|
This file essentially contains the regular Kernel config, with the specific
|
|
|
|
test targets as well. The ``.kunitconfig`` should also contain any other config
|
|
|
|
options required by the tests.
|
|
|
|
|
|
|
|
A good starting point for a ``.kunitconfig`` is the KUnit defconfig:
|
2020-04-16 04:16:53 +08:00
|
|
|
|
2019-09-23 17:02:45 +08:00
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
cd $PATH_TO_LINUX_REPO
|
2019-12-20 13:14:07 +08:00
|
|
|
cp arch/um/configs/kunit_defconfig .kunitconfig
|
2019-09-23 17:02:45 +08:00
|
|
|
|
2020-02-29 03:18:21 +08:00
|
|
|
You can then add any other Kconfig options you wish, e.g.:
|
2020-04-16 04:16:53 +08:00
|
|
|
|
2020-02-29 03:18:21 +08:00
|
|
|
.. code-block:: none
|
|
|
|
|
2020-04-16 04:16:53 +08:00
|
|
|
CONFIG_LIST_KUNIT_TEST=y
|
2020-02-29 03:18:21 +08:00
|
|
|
|
|
|
|
:doc:`kunit_tool <kunit-tool>` will ensure that all config options set in
|
|
|
|
``.kunitconfig`` are set in the kernel ``.config`` before running the tests.
|
|
|
|
It'll warn you if you haven't included the dependencies of the options you're
|
|
|
|
using.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
Note that removing something from the ``.kunitconfig`` will not trigger a
|
|
|
|
rebuild of the ``.config`` file: the configuration is only updated if the
|
|
|
|
``.kunitconfig`` is not a subset of ``.config``. This means that you can use
|
|
|
|
other tools (such as make menuconfig) to adjust other config options.
|
|
|
|
|
|
|
|
|
2020-04-16 04:16:53 +08:00
|
|
|
Running the tests (KUnit Wrapper)
|
|
|
|
---------------------------------
|
2019-09-23 17:02:45 +08:00
|
|
|
|
|
|
|
To make sure that everything is set up correctly, simply invoke the Python
|
|
|
|
wrapper from your kernel repo:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2019-09-08 05:01:31 +08:00
|
|
|
./tools/testing/kunit/kunit.py run
|
2019-09-23 17:02:45 +08:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
You may want to run ``make mrproper`` first.
|
|
|
|
|
|
|
|
If everything worked correctly, you should see the following:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
Generating .config ...
|
|
|
|
Building KUnit Kernel ...
|
|
|
|
Starting KUnit Kernel ...
|
|
|
|
|
|
|
|
followed by a list of tests that are run. All of them should be passing.
|
|
|
|
|
|
|
|
.. note::
|
2019-11-20 07:38:10 +08:00
|
|
|
Because it is building a lot of sources for the first time, the
|
|
|
|
``Building KUnit kernel`` step may take a while.
|
2019-09-23 17:02:45 +08:00
|
|
|
|
2020-02-29 03:18:21 +08:00
|
|
|
Running tests without the KUnit Wrapper
|
|
|
|
=======================================
|
|
|
|
|
|
|
|
If you'd rather not use the KUnit Wrapper (if, for example, you need to
|
|
|
|
integrate with other systems, or use an architecture other than UML), KUnit can
|
|
|
|
be included in any kernel, and the results read out and parsed manually.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
KUnit is not designed for use in a production system, and it's possible that
|
|
|
|
tests may reduce the stability or security of the system.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Configuring the kernel
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
In order to enable KUnit itself, you simply need to enable the ``CONFIG_KUNIT``
|
|
|
|
Kconfig option (it's under Kernel Hacking/Kernel Testing and Coverage in
|
|
|
|
menuconfig). From there, you can enable any KUnit tests you want: they usually
|
|
|
|
have config options ending in ``_KUNIT_TEST``.
|
|
|
|
|
|
|
|
KUnit and KUnit tests can be compiled as modules: in this case the tests in a
|
|
|
|
module will be run when the module is loaded.
|
|
|
|
|
2020-04-16 04:16:53 +08:00
|
|
|
|
|
|
|
Running the tests (w/o KUnit Wrapper)
|
|
|
|
-------------------------------------
|
2020-02-29 03:18:21 +08:00
|
|
|
|
|
|
|
Build and run your kernel as usual. Test output will be written to the kernel
|
|
|
|
log in `TAP <https://testanything.org/>`_ format.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
It's possible that there will be other lines and/or data interspersed in the
|
|
|
|
TAP output.
|
|
|
|
|
|
|
|
|
2019-09-23 17:02:45 +08:00
|
|
|
Writing your first test
|
|
|
|
=======================
|
|
|
|
|
|
|
|
In your kernel repo let's add some code that we can test. Create a file
|
|
|
|
``drivers/misc/example.h`` with the contents:
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
int misc_example_add(int left, int right);
|
|
|
|
|
|
|
|
create a file ``drivers/misc/example.c``:
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
#include <linux/errno.h>
|
|
|
|
|
|
|
|
#include "example.h"
|
|
|
|
|
|
|
|
int misc_example_add(int left, int right)
|
|
|
|
{
|
|
|
|
return left + right;
|
|
|
|
}
|
|
|
|
|
|
|
|
Now add the following lines to ``drivers/misc/Kconfig``:
|
|
|
|
|
|
|
|
.. code-block:: kconfig
|
|
|
|
|
|
|
|
config MISC_EXAMPLE
|
|
|
|
bool "My example"
|
|
|
|
|
|
|
|
and the following lines to ``drivers/misc/Makefile``:
|
|
|
|
|
|
|
|
.. code-block:: make
|
|
|
|
|
|
|
|
obj-$(CONFIG_MISC_EXAMPLE) += example.o
|
|
|
|
|
|
|
|
Now we are ready to write the test. The test will be in
|
|
|
|
``drivers/misc/example-test.c``:
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
#include <kunit/test.h>
|
|
|
|
#include "example.h"
|
|
|
|
|
|
|
|
/* Define the test cases. */
|
|
|
|
|
|
|
|
static void misc_example_add_test_basic(struct kunit *test)
|
|
|
|
{
|
|
|
|
KUNIT_EXPECT_EQ(test, 1, misc_example_add(1, 0));
|
|
|
|
KUNIT_EXPECT_EQ(test, 2, misc_example_add(1, 1));
|
|
|
|
KUNIT_EXPECT_EQ(test, 0, misc_example_add(-1, 1));
|
|
|
|
KUNIT_EXPECT_EQ(test, INT_MAX, misc_example_add(0, INT_MAX));
|
|
|
|
KUNIT_EXPECT_EQ(test, -1, misc_example_add(INT_MAX, INT_MIN));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void misc_example_test_failure(struct kunit *test)
|
|
|
|
{
|
|
|
|
KUNIT_FAIL(test, "This test never passes.");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct kunit_case misc_example_test_cases[] = {
|
|
|
|
KUNIT_CASE(misc_example_add_test_basic),
|
|
|
|
KUNIT_CASE(misc_example_test_failure),
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct kunit_suite misc_example_test_suite = {
|
|
|
|
.name = "misc-example",
|
|
|
|
.test_cases = misc_example_test_cases,
|
|
|
|
};
|
|
|
|
kunit_test_suite(misc_example_test_suite);
|
|
|
|
|
|
|
|
Now add the following to ``drivers/misc/Kconfig``:
|
|
|
|
|
|
|
|
.. code-block:: kconfig
|
|
|
|
|
|
|
|
config MISC_EXAMPLE_TEST
|
2021-01-20 07:52:26 +08:00
|
|
|
tristate "Test for my example" if !KUNIT_ALL_TESTS
|
2020-10-22 03:25:18 +08:00
|
|
|
depends on MISC_EXAMPLE && KUNIT=y
|
2021-01-20 07:52:26 +08:00
|
|
|
default KUNIT_ALL_TESTS
|
2019-09-23 17:02:45 +08:00
|
|
|
|
|
|
|
and the following to ``drivers/misc/Makefile``:
|
|
|
|
|
|
|
|
.. code-block:: make
|
|
|
|
|
|
|
|
obj-$(CONFIG_MISC_EXAMPLE_TEST) += example-test.o
|
|
|
|
|
2019-12-20 13:14:07 +08:00
|
|
|
Now add it to your ``.kunitconfig``:
|
2019-09-23 17:02:45 +08:00
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
CONFIG_MISC_EXAMPLE=y
|
|
|
|
CONFIG_MISC_EXAMPLE_TEST=y
|
|
|
|
|
|
|
|
Now you can run the test:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2019-11-20 07:38:10 +08:00
|
|
|
./tools/testing/kunit/kunit.py run
|
2019-09-23 17:02:45 +08:00
|
|
|
|
|
|
|
You should see the following failure:
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
...
|
|
|
|
[16:08:57] [PASSED] misc-example:misc_example_add_test_basic
|
|
|
|
[16:08:57] [FAILED] misc-example:misc_example_test_failure
|
|
|
|
[16:08:57] EXPECTATION FAILED at drivers/misc/example-test.c:17
|
|
|
|
[16:08:57] This test never passes.
|
|
|
|
...
|
|
|
|
|
|
|
|
Congrats! You just wrote your first KUnit test!
|
|
|
|
|
|
|
|
Next Steps
|
|
|
|
==========
|
2021-01-26 02:53:33 +08:00
|
|
|
* Check out the :doc:`tips` page for tips on
|
|
|
|
writing idiomatic KUnit tests.
|
|
|
|
* Optional: see the :doc:`usage` page for a more
|
2019-09-23 17:02:45 +08:00
|
|
|
in-depth explanation of KUnit.
|