Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | ========================== |
| 4 | Frequently Asked Questions |
| 5 | ========================== |
| 6 | |
Harinder Singh | b360644 | 2021-12-17 04:49:11 +0000 | [diff] [blame] | 7 | How is this different from Autotest, kselftest, and so on? |
| 8 | ========================================================== |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 9 | KUnit is a unit testing framework. Autotest, kselftest (and some others) are |
| 10 | not. |
| 11 | |
| 12 | A `unit test <https://martinfowler.com/bliki/UnitTest.html>`_ is supposed to |
Harinder Singh | b360644 | 2021-12-17 04:49:11 +0000 | [diff] [blame] | 13 | test a single unit of code in isolation and hence the name *unit test*. A unit |
| 14 | test should be the finest granularity of testing and should allow all possible |
| 15 | code paths to be tested in the code under test. This is only possible if the |
| 16 | code under test is small and does not have any external dependencies outside of |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 17 | the test's control like hardware. |
| 18 | |
| 19 | There are no testing frameworks currently available for the kernel that do not |
Harinder Singh | b360644 | 2021-12-17 04:49:11 +0000 | [diff] [blame] | 20 | require installing the kernel on a test machine or in a virtual machine. All |
| 21 | testing frameworks require tests to be written in userspace and run on the |
| 22 | kernel under test. This is true for Autotest, kselftest, and some others, |
| 23 | disqualifying any of them from being considered unit testing frameworks. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 24 | |
| 25 | Does KUnit support running on architectures other than UML? |
| 26 | =========================================================== |
| 27 | |
Harinder Singh | b360644 | 2021-12-17 04:49:11 +0000 | [diff] [blame] | 28 | Yes, mostly. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 29 | |
Harinder Singh | b360644 | 2021-12-17 04:49:11 +0000 | [diff] [blame] | 30 | For the most part, the KUnit core framework (what we use to write the tests) |
| 31 | can compile to any architecture. It compiles like just another part of the |
Alan Maguire | 6ae2bfd | 2020-01-06 22:28:23 +0000 | [diff] [blame] | 32 | kernel and runs when the kernel boots, or when built as a module, when the |
Harinder Singh | b360644 | 2021-12-17 04:49:11 +0000 | [diff] [blame] | 33 | module is loaded. However, there is infrastructure, like the KUnit Wrapper |
| 34 | (``tools/testing/kunit/kunit.py``) that does not support other architectures. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 35 | |
Harinder Singh | b360644 | 2021-12-17 04:49:11 +0000 | [diff] [blame] | 36 | In short, yes, you can run KUnit on other architectures, but it might require |
| 37 | more work than using KUnit on UML. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 38 | |
| 39 | For more information, see :ref:`kunit-on-non-uml`. |
| 40 | |
Harinder Singh | b360644 | 2021-12-17 04:49:11 +0000 | [diff] [blame] | 41 | What is the difference between a unit test and other kinds of tests? |
| 42 | ==================================================================== |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 43 | Most existing tests for the Linux kernel would be categorized as an integration |
| 44 | test, or an end-to-end test. |
| 45 | |
Harinder Singh | b360644 | 2021-12-17 04:49:11 +0000 | [diff] [blame] | 46 | - A unit test is supposed to test a single unit of code in isolation. A unit |
| 47 | test should be the finest granularity of testing and, as such, allows all |
| 48 | possible code paths to be tested in the code under test. This is only possible |
| 49 | if the code under test is small and does not have any external dependencies |
| 50 | outside of the test's control like hardware. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 51 | - An integration test tests the interaction between a minimal set of components, |
| 52 | usually just two or three. For example, someone might write an integration |
| 53 | test to test the interaction between a driver and a piece of hardware, or to |
| 54 | test the interaction between the userspace libraries the kernel provides and |
Harinder Singh | b360644 | 2021-12-17 04:49:11 +0000 | [diff] [blame] | 55 | the kernel itself. However, one of these tests would probably not test the |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 56 | entire kernel along with hardware interactions and interactions with the |
| 57 | userspace. |
| 58 | - An end-to-end test usually tests the entire system from the perspective of the |
| 59 | code under test. For example, someone might write an end-to-end test for the |
| 60 | kernel by installing a production configuration of the kernel on production |
| 61 | hardware with a production userspace and then trying to exercise some behavior |
| 62 | that depends on interactions between the hardware, the kernel, and userspace. |
David Gow | c63d2dd | 2020-06-02 20:37:25 -0700 | [diff] [blame] | 63 | |
Harinder Singh | b360644 | 2021-12-17 04:49:11 +0000 | [diff] [blame] | 64 | KUnit is not working, what should I do? |
| 65 | ======================================= |
David Gow | c63d2dd | 2020-06-02 20:37:25 -0700 | [diff] [blame] | 66 | |
| 67 | Unfortunately, there are a number of things which can break, but here are some |
| 68 | things to try. |
| 69 | |
Harinder Singh | b360644 | 2021-12-17 04:49:11 +0000 | [diff] [blame] | 70 | 1. Run ``./tools/testing/kunit/kunit.py run`` with the ``--raw_output`` |
David Gow | c63d2dd | 2020-06-02 20:37:25 -0700 | [diff] [blame] | 71 | parameter. This might show details or error messages hidden by the kunit_tool |
| 72 | parser. |
| 73 | 2. Instead of running ``kunit.py run``, try running ``kunit.py config``, |
| 74 | ``kunit.py build``, and ``kunit.py exec`` independently. This can help track |
| 75 | down where an issue is occurring. (If you think the parser is at fault, you |
Harinder Singh | b360644 | 2021-12-17 04:49:11 +0000 | [diff] [blame] | 76 | can run it manually against ``stdin`` or a file with ``kunit.py parse``.) |
| 77 | 3. Running the UML kernel directly can often reveal issues or error messages, |
| 78 | ``kunit_tool`` ignores. This should be as simple as running ``./vmlinux`` |
| 79 | after building the UML kernel (for example, by using ``kunit.py build``). |
| 80 | Note that UML has some unusual requirements (such as the host having a tmpfs |
| 81 | filesystem mounted), and has had issues in the past when built statically and |
| 82 | the host has KASLR enabled. (On older host kernels, you may need to run |
| 83 | ``setarch `uname -m` -R ./vmlinux`` to disable KASLR.) |
David Gow | c63d2dd | 2020-06-02 20:37:25 -0700 | [diff] [blame] | 84 | 4. Make sure the kernel .config has ``CONFIG_KUNIT=y`` and at least one test |
| 85 | (e.g. ``CONFIG_KUNIT_EXAMPLE_TEST=y``). kunit_tool will keep its .config |
| 86 | around, so you can see what config was used after running ``kunit.py run``. |
| 87 | It also preserves any config changes you might make, so you can |
| 88 | enable/disable things with ``make ARCH=um menuconfig`` or similar, and then |
| 89 | re-run kunit_tool. |
| 90 | 5. Try to run ``make ARCH=um defconfig`` before running ``kunit.py run``. This |
| 91 | may help clean up any residual config items which could be causing problems. |
Randy Dunlap | f776642 | 2020-10-27 11:48:53 -0700 | [diff] [blame] | 92 | 6. Finally, try running KUnit outside UML. KUnit and KUnit tests can be |
David Gow | c63d2dd | 2020-06-02 20:37:25 -0700 | [diff] [blame] | 93 | built into any kernel, or can be built as a module and loaded at runtime. |
| 94 | Doing so should allow you to determine if UML is causing the issue you're |
| 95 | seeing. When tests are built-in, they will execute when the kernel boots, and |
| 96 | modules will automatically execute associated tests when loaded. Test results |
| 97 | can be collected from ``/sys/kernel/debug/kunit/<test suite>/results``, and |
| 98 | can be parsed with ``kunit.py parse``. For more details, see "KUnit on |
Mauro Carvalho Chehab | 654a5bd | 2021-06-16 08:27:25 +0200 | [diff] [blame] | 99 | non-UML architectures" in Documentation/dev-tools/kunit/usage.rst. |
David Gow | c63d2dd | 2020-06-02 20:37:25 -0700 | [diff] [blame] | 100 | |
| 101 | If none of the above tricks help, you are always welcome to email any issues to |
| 102 | kunit-dev@googlegroups.com. |