Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 ===============
0004 Getting Started
0005 ===============
0006 
0007 Installing Dependencies
0008 =======================
0009 KUnit has the same dependencies as the Linux kernel. As long as you can
0010 build the kernel, you can run KUnit.
0011 
0012 Running tests with kunit_tool
0013 =============================
0014 kunit_tool is a Python script, which configures and builds a kernel, runs
0015 tests, and formats the test results. From the kernel repository, you
0016 can run kunit_tool:
0017 
0018 .. code-block:: bash
0019 
0020         ./tools/testing/kunit/kunit.py run
0021 
0022 For more information on this wrapper, see:
0023 Documentation/dev-tools/kunit/run_wrapper.rst.
0024 
0025 Creating a ``.kunitconfig``
0026 ---------------------------
0027 
0028 By default, kunit_tool runs a selection of tests. However, you can specify which
0029 unit tests to run by creating a ``.kunitconfig`` file with kernel config options
0030 that enable only a specific set of tests and their dependencies.
0031 The ``.kunitconfig`` file contains a list of kconfig options which are required
0032 to run the desired targets. The ``.kunitconfig`` also contains any other test
0033 specific config options, such as test dependencies. For example: the
0034 ``FAT_FS`` tests - ``FAT_KUNIT_TEST``, depends on
0035 ``FAT_FS``. ``FAT_FS`` can be enabled by selecting either ``MSDOS_FS``
0036 or ``VFAT_FS``. To run ``FAT_KUNIT_TEST``, the ``.kunitconfig`` has:
0037 
0038 .. code-block:: none
0039 
0040         CONFIG_KUNIT=y
0041         CONFIG_MSDOS_FS=y
0042         CONFIG_FAT_KUNIT_TEST=y
0043 
0044 1. A good starting point for the ``.kunitconfig`` is the KUnit default config.
0045    You can generate it by running:
0046 
0047 .. code-block:: bash
0048 
0049         cd $PATH_TO_LINUX_REPO
0050         tools/testing/kunit/kunit.py config
0051         cat .kunit/.kunitconfig
0052 
0053 .. note ::
0054    ``.kunitconfig`` lives in the ``--build_dir`` used by kunit.py, which is
0055    ``.kunit`` by default.
0056 
0057 .. note ::
0058    You may want to remove CONFIG_KUNIT_ALL_TESTS from the ``.kunitconfig`` as
0059    it will enable a number of additional tests that you may not want.
0060 
0061 2. You can then add any other Kconfig options, for example:
0062 
0063 .. code-block:: none
0064 
0065         CONFIG_LIST_KUNIT_TEST=y
0066 
0067 Before running the tests, kunit_tool ensures that all config options
0068 set in ``.kunitconfig`` are set in the kernel ``.config``. It will warn
0069 you if you have not included dependencies for the options used.
0070 
0071 .. note ::
0072    If you change the ``.kunitconfig``, kunit.py will trigger a rebuild of the
0073    ``.config`` file. But you can edit the ``.config`` file directly or with
0074    tools like ``make menuconfig O=.kunit``. As long as its a superset of
0075    ``.kunitconfig``, kunit.py won't overwrite your changes.
0076 
0077 Running Tests (KUnit Wrapper)
0078 -----------------------------
0079 1. To make sure that everything is set up correctly, invoke the Python
0080    wrapper from your kernel repository:
0081 
0082 .. code-block:: bash
0083 
0084         ./tools/testing/kunit/kunit.py run
0085 
0086 If everything worked correctly, you should see the following:
0087 
0088 .. code-block::
0089 
0090         Generating .config ...
0091         Building KUnit Kernel ...
0092         Starting KUnit Kernel ...
0093 
0094 The tests will pass or fail.
0095 
0096 .. note ::
0097    Because it is building a lot of sources for the first time, the
0098    ``Building KUnit kernel`` may take a while.
0099 
0100 Running Tests without the KUnit Wrapper
0101 =======================================
0102 If you do not want to use the KUnit Wrapper (for example: you want code
0103 under test to integrate with other systems, or use a different/
0104 unsupported architecture or configuration), KUnit can be included in
0105 any kernel, and the results are read out and parsed manually.
0106 
0107 .. note ::
0108    ``CONFIG_KUNIT`` should not be enabled in a production environment.
0109    Enabling KUnit disables Kernel Address-Space Layout Randomization
0110    (KASLR), and tests may affect the state of the kernel in ways not
0111    suitable for production.
0112 
0113 Configuring the Kernel
0114 ----------------------
0115 To enable KUnit itself, you need to enable the ``CONFIG_KUNIT`` Kconfig
0116 option (under Kernel Hacking/Kernel Testing and Coverage in
0117 ``menuconfig``). From there, you can enable any KUnit tests. They
0118 usually have config options ending in ``_KUNIT_TEST``.
0119 
0120 KUnit and KUnit tests can be compiled as modules. The tests in a module
0121 will run when the module is loaded.
0122 
0123 Running Tests (without KUnit Wrapper)
0124 -------------------------------------
0125 Build and run your kernel. In the kernel log, the test output is printed
0126 out in the TAP format. This will only happen by default if KUnit/tests
0127 are built-in. Otherwise the module will need to be loaded.
0128 
0129 .. note ::
0130    Some lines and/or data may get interspersed in the TAP output.
0131 
0132 Writing Your First Test
0133 =======================
0134 In your kernel repository, let's add some code that we can test.
0135 
0136 1. Create a file ``drivers/misc/example.h``, which includes:
0137 
0138 .. code-block:: c
0139 
0140         int misc_example_add(int left, int right);
0141 
0142 2. Create a file ``drivers/misc/example.c``, which includes:
0143 
0144 .. code-block:: c
0145 
0146         #include <linux/errno.h>
0147 
0148         #include "example.h"
0149 
0150         int misc_example_add(int left, int right)
0151         {
0152                 return left + right;
0153         }
0154 
0155 3. Add the following lines to ``drivers/misc/Kconfig``:
0156 
0157 .. code-block:: kconfig
0158 
0159         config MISC_EXAMPLE
0160                 bool "My example"
0161 
0162 4. Add the following lines to ``drivers/misc/Makefile``:
0163 
0164 .. code-block:: make
0165 
0166         obj-$(CONFIG_MISC_EXAMPLE) += example.o
0167 
0168 Now we are ready to write the test cases.
0169 
0170 1. Add the below test case in ``drivers/misc/example_test.c``:
0171 
0172 .. code-block:: c
0173 
0174         #include <kunit/test.h>
0175         #include "example.h"
0176 
0177         /* Define the test cases. */
0178 
0179         static void misc_example_add_test_basic(struct kunit *test)
0180         {
0181                 KUNIT_EXPECT_EQ(test, 1, misc_example_add(1, 0));
0182                 KUNIT_EXPECT_EQ(test, 2, misc_example_add(1, 1));
0183                 KUNIT_EXPECT_EQ(test, 0, misc_example_add(-1, 1));
0184                 KUNIT_EXPECT_EQ(test, INT_MAX, misc_example_add(0, INT_MAX));
0185                 KUNIT_EXPECT_EQ(test, -1, misc_example_add(INT_MAX, INT_MIN));
0186         }
0187 
0188         static void misc_example_test_failure(struct kunit *test)
0189         {
0190                 KUNIT_FAIL(test, "This test never passes.");
0191         }
0192 
0193         static struct kunit_case misc_example_test_cases[] = {
0194                 KUNIT_CASE(misc_example_add_test_basic),
0195                 KUNIT_CASE(misc_example_test_failure),
0196                 {}
0197         };
0198 
0199         static struct kunit_suite misc_example_test_suite = {
0200                 .name = "misc-example",
0201                 .test_cases = misc_example_test_cases,
0202         };
0203         kunit_test_suite(misc_example_test_suite);
0204 
0205 2. Add the following lines to ``drivers/misc/Kconfig``:
0206 
0207 .. code-block:: kconfig
0208 
0209         config MISC_EXAMPLE_TEST
0210                 tristate "Test for my example" if !KUNIT_ALL_TESTS
0211                 depends on MISC_EXAMPLE && KUNIT=y
0212                 default KUNIT_ALL_TESTS
0213 
0214 3. Add the following lines to ``drivers/misc/Makefile``:
0215 
0216 .. code-block:: make
0217 
0218         obj-$(CONFIG_MISC_EXAMPLE_TEST) += example_test.o
0219 
0220 4. Add the following lines to ``.kunitconfig``:
0221 
0222 .. code-block:: none
0223 
0224         CONFIG_MISC_EXAMPLE=y
0225         CONFIG_MISC_EXAMPLE_TEST=y
0226 
0227 5. Run the test:
0228 
0229 .. code-block:: bash
0230 
0231         ./tools/testing/kunit/kunit.py run
0232 
0233 You should see the following failure:
0234 
0235 .. code-block:: none
0236 
0237         ...
0238         [16:08:57] [PASSED] misc-example:misc_example_add_test_basic
0239         [16:08:57] [FAILED] misc-example:misc_example_test_failure
0240         [16:08:57] EXPECTATION FAILED at drivers/misc/example-test.c:17
0241         [16:08:57]      This test never passes.
0242         ...
0243 
0244 Congrats! You just wrote your first KUnit test.
0245 
0246 Next Steps
0247 ==========
0248 
0249 *   Documentation/dev-tools/kunit/architecture.rst - KUnit architecture.
0250 *   Documentation/dev-tools/kunit/run_wrapper.rst - run kunit_tool.
0251 *   Documentation/dev-tools/kunit/run_manual.rst - run tests without kunit_tool.
0252 *   Documentation/dev-tools/kunit/usage.rst - write tests.
0253 *   Documentation/dev-tools/kunit/tips.rst - best practices with
0254     examples.
0255 *   Documentation/dev-tools/kunit/api/index.rst - KUnit APIs
0256     used for testing.
0257 *   Documentation/dev-tools/kunit/kunit-tool.rst - kunit_tool helper
0258     script.
0259 *   Documentation/dev-tools/kunit/faq.rst - KUnit common questions and
0260     answers.