faq.rst 5.6 KB

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