1.. SPDX-License-Identifier: GPL-2.0 2 3=========== 4Using KUnit 5=========== 6 7The purpose of this document is to describe what KUnit is, how it works, how it 8is intended to be used, and all the concepts and terminology that are needed to 9understand it. This guide assumes a working knowledge of the Linux kernel and 10some basic knowledge of testing. 11 12For a high level introduction to KUnit, including setting up KUnit for your 13project, see :doc:`start`. 14 15Organization of this document 16============================= 17 18This document is organized into two main sections: Testing and Isolating 19Behavior. The first covers what unit tests are and how to use KUnit to write 20them. The second covers how to use KUnit to isolate code and make it possible 21to unit test code that was otherwise un-unit-testable. 22 23Testing 24======= 25 26What is KUnit? 27-------------- 28 29"K" is short for "kernel" so "KUnit" is the "(Linux) Kernel Unit Testing 30Framework." KUnit is intended first and foremost for writing unit tests; it is 31general enough that it can be used to write integration tests; however, this is 32a secondary goal. KUnit has no ambition of being the only testing framework for 33the kernel; for example, it does not intend to be an end-to-end testing 34framework. 35 36What is Unit Testing? 37--------------------- 38 39A `unit test <https://martinfowler.com/bliki/UnitTest.html>`_ is a test that 40tests code at the smallest possible scope, a *unit* of code. In the C 41programming language that's a function. 42 43Unit tests should be written for all the publicly exposed functions in a 44compilation unit; so that is all the functions that are exported in either a 45*class* (defined below) or all functions which are **not** static. 46 47Writing Tests 48------------- 49 50Test Cases 51~~~~~~~~~~ 52 53The fundamental unit in KUnit is the test case. A test case is a function with 54the signature ``void (*)(struct kunit *test)``. It calls a function to be tested 55and then sets *expectations* for what should happen. For example: 56 57.. code-block:: c 58 59 void example_test_success(struct kunit *test) 60 { 61 } 62 63 void example_test_failure(struct kunit *test) 64 { 65 KUNIT_FAIL(test, "This test never passes."); 66 } 67 68In the above example ``example_test_success`` always passes because it does 69nothing; no expectations are set, so all expectations pass. On the other hand 70``example_test_failure`` always fails because it calls ``KUNIT_FAIL``, which is 71a special expectation that logs a message and causes the test case to fail. 72 73Expectations 74~~~~~~~~~~~~ 75An *expectation* is a way to specify that you expect a piece of code to do 76something in a test. An expectation is called like a function. A test is made 77by setting expectations about the behavior of a piece of code under test; when 78one or more of the expectations fail, the test case fails and information about 79the failure is logged. For example: 80 81.. code-block:: c 82 83 void add_test_basic(struct kunit *test) 84 { 85 KUNIT_EXPECT_EQ(test, 1, add(1, 0)); 86 KUNIT_EXPECT_EQ(test, 2, add(1, 1)); 87 } 88 89In the above example ``add_test_basic`` makes a number of assertions about the 90behavior of a function called ``add``; the first parameter is always of type 91``struct kunit *``, which contains information about the current test context; 92the second parameter, in this case, is what the value is expected to be; the 93last value is what the value actually is. If ``add`` passes all of these 94expectations, the test case, ``add_test_basic`` will pass; if any one of these 95expectations fails, the test case will fail. 96 97It is important to understand that a test case *fails* when any expectation is 98violated; however, the test will continue running, potentially trying other 99expectations until the test case ends or is otherwise terminated. This is as 100opposed to *assertions* which are discussed later. 101 102To learn about more expectations supported by KUnit, see :doc:`api/test`. 103 104.. note:: 105 A single test case should be pretty short, pretty easy to understand, 106 focused on a single behavior. 107 108For example, if we wanted to properly test the add function above, we would 109create additional tests cases which would each test a different property that an 110add function should have like this: 111 112.. code-block:: c 113 114 void add_test_basic(struct kunit *test) 115 { 116 KUNIT_EXPECT_EQ(test, 1, add(1, 0)); 117 KUNIT_EXPECT_EQ(test, 2, add(1, 1)); 118 } 119 120 void add_test_negative(struct kunit *test) 121 { 122 KUNIT_EXPECT_EQ(test, 0, add(-1, 1)); 123 } 124 125 void add_test_max(struct kunit *test) 126 { 127 KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX)); 128 KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN)); 129 } 130 131 void add_test_overflow(struct kunit *test) 132 { 133 KUNIT_EXPECT_EQ(test, INT_MIN, add(INT_MAX, 1)); 134 } 135 136Notice how it is immediately obvious what all the properties that we are testing 137for are. 138 139Assertions 140~~~~~~~~~~ 141 142KUnit also has the concept of an *assertion*. An assertion is just like an 143expectation except the assertion immediately terminates the test case if it is 144not satisfied. 145 146For example: 147 148.. code-block:: c 149 150 static void mock_test_do_expect_default_return(struct kunit *test) 151 { 152 struct mock_test_context *ctx = test->priv; 153 struct mock *mock = ctx->mock; 154 int param0 = 5, param1 = -5; 155 const char *two_param_types[] = {"int", "int"}; 156 const void *two_params[] = {¶m0, ¶m1}; 157 const void *ret; 158 159 ret = mock->do_expect(mock, 160 "test_printk", test_printk, 161 two_param_types, two_params, 162 ARRAY_SIZE(two_params)); 163 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ret); 164 KUNIT_EXPECT_EQ(test, -4, *((int *) ret)); 165 } 166 167In this example, the method under test should return a pointer to a value, so 168if the pointer returned by the method is null or an errno, we don't want to 169bother continuing the test since the following expectation could crash the test 170case. `ASSERT_NOT_ERR_OR_NULL(...)` allows us to bail out of the test case if 171the appropriate conditions have not been satisfied to complete the test. 172 173Test Suites 174~~~~~~~~~~~ 175 176Now obviously one unit test isn't very helpful; the power comes from having 177many test cases covering all of a unit's behaviors. Consequently it is common 178to have many *similar* tests; in order to reduce duplication in these closely 179related tests most unit testing frameworks - including KUnit - provide the 180concept of a *test suite*. A *test suite* is just a collection of test cases 181for a unit of code with a set up function that gets invoked before every test 182case and then a tear down function that gets invoked after every test case 183completes. 184 185Example: 186 187.. code-block:: c 188 189 static struct kunit_case example_test_cases[] = { 190 KUNIT_CASE(example_test_foo), 191 KUNIT_CASE(example_test_bar), 192 KUNIT_CASE(example_test_baz), 193 {} 194 }; 195 196 static struct kunit_suite example_test_suite = { 197 .name = "example", 198 .init = example_test_init, 199 .exit = example_test_exit, 200 .test_cases = example_test_cases, 201 }; 202 kunit_test_suite(example_test_suite); 203 204In the above example the test suite, ``example_test_suite``, would run the test 205cases ``example_test_foo``, ``example_test_bar``, and ``example_test_baz``; 206each would have ``example_test_init`` called immediately before it and would 207have ``example_test_exit`` called immediately after it. 208``kunit_test_suite(example_test_suite)`` registers the test suite with the 209KUnit test framework. 210 211.. note:: 212 A test case will only be run if it is associated with a test suite. 213 214``kunit_test_suite(...)`` is a macro which tells the linker to put the specified 215test suite in a special linker section so that it can be run by KUnit either 216after late_init, or when the test module is loaded (depending on whether the 217test was built in or not). 218 219For more information on these types of things see the :doc:`api/test`. 220 221Isolating Behavior 222================== 223 224The most important aspect of unit testing that other forms of testing do not 225provide is the ability to limit the amount of code under test to a single unit. 226In practice, this is only possible by being able to control what code gets run 227when the unit under test calls a function and this is usually accomplished 228through some sort of indirection where a function is exposed as part of an API 229such that the definition of that function can be changed without affecting the 230rest of the code base. In the kernel this primarily comes from two constructs, 231classes, structs that contain function pointers that are provided by the 232implementer, and architecture-specific functions which have definitions selected 233at compile time. 234 235Classes 236------- 237 238Classes are not a construct that is built into the C programming language; 239however, it is an easily derived concept. Accordingly, pretty much every project 240that does not use a standardized object oriented library (like GNOME's GObject) 241has their own slightly different way of doing object oriented programming; the 242Linux kernel is no exception. 243 244The central concept in kernel object oriented programming is the class. In the 245kernel, a *class* is a struct that contains function pointers. This creates a 246contract between *implementers* and *users* since it forces them to use the 247same function signature without having to call the function directly. In order 248for it to truly be a class, the function pointers must specify that a pointer 249to the class, known as a *class handle*, be one of the parameters; this makes 250it possible for the member functions (also known as *methods*) to have access 251to member variables (more commonly known as *fields*) allowing the same 252implementation to have multiple *instances*. 253 254Typically a class can be *overridden* by *child classes* by embedding the 255*parent class* in the child class. Then when a method provided by the child 256class is called, the child implementation knows that the pointer passed to it is 257of a parent contained within the child; because of this, the child can compute 258the pointer to itself because the pointer to the parent is always a fixed offset 259from the pointer to the child; this offset is the offset of the parent contained 260in the child struct. For example: 261 262.. code-block:: c 263 264 struct shape { 265 int (*area)(struct shape *this); 266 }; 267 268 struct rectangle { 269 struct shape parent; 270 int length; 271 int width; 272 }; 273 274 int rectangle_area(struct shape *this) 275 { 276 struct rectangle *self = container_of(this, struct shape, parent); 277 278 return self->length * self->width; 279 }; 280 281 void rectangle_new(struct rectangle *self, int length, int width) 282 { 283 self->parent.area = rectangle_area; 284 self->length = length; 285 self->width = width; 286 } 287 288In this example (as in most kernel code) the operation of computing the pointer 289to the child from the pointer to the parent is done by ``container_of``. 290 291Faking Classes 292~~~~~~~~~~~~~~ 293 294In order to unit test a piece of code that calls a method in a class, the 295behavior of the method must be controllable, otherwise the test ceases to be a 296unit test and becomes an integration test. 297 298A fake just provides an implementation of a piece of code that is different than 299what runs in a production instance, but behaves identically from the standpoint 300of the callers; this is usually done to replace a dependency that is hard to 301deal with, or is slow. 302 303A good example for this might be implementing a fake EEPROM that just stores the 304"contents" in an internal buffer. For example, let's assume we have a class that 305represents an EEPROM: 306 307.. code-block:: c 308 309 struct eeprom { 310 ssize_t (*read)(struct eeprom *this, size_t offset, char *buffer, size_t count); 311 ssize_t (*write)(struct eeprom *this, size_t offset, const char *buffer, size_t count); 312 }; 313 314And we want to test some code that buffers writes to the EEPROM: 315 316.. code-block:: c 317 318 struct eeprom_buffer { 319 ssize_t (*write)(struct eeprom_buffer *this, const char *buffer, size_t count); 320 int flush(struct eeprom_buffer *this); 321 size_t flush_count; /* Flushes when buffer exceeds flush_count. */ 322 }; 323 324 struct eeprom_buffer *new_eeprom_buffer(struct eeprom *eeprom); 325 void destroy_eeprom_buffer(struct eeprom *eeprom); 326 327We can easily test this code by *faking out* the underlying EEPROM: 328 329.. code-block:: c 330 331 struct fake_eeprom { 332 struct eeprom parent; 333 char contents[FAKE_EEPROM_CONTENTS_SIZE]; 334 }; 335 336 ssize_t fake_eeprom_read(struct eeprom *parent, size_t offset, char *buffer, size_t count) 337 { 338 struct fake_eeprom *this = container_of(parent, struct fake_eeprom, parent); 339 340 count = min(count, FAKE_EEPROM_CONTENTS_SIZE - offset); 341 memcpy(buffer, this->contents + offset, count); 342 343 return count; 344 } 345 346 ssize_t fake_eeprom_write(struct eeprom *parent, size_t offset, const char *buffer, size_t count) 347 { 348 struct fake_eeprom *this = container_of(parent, struct fake_eeprom, parent); 349 350 count = min(count, FAKE_EEPROM_CONTENTS_SIZE - offset); 351 memcpy(this->contents + offset, buffer, count); 352 353 return count; 354 } 355 356 void fake_eeprom_init(struct fake_eeprom *this) 357 { 358 this->parent.read = fake_eeprom_read; 359 this->parent.write = fake_eeprom_write; 360 memset(this->contents, 0, FAKE_EEPROM_CONTENTS_SIZE); 361 } 362 363We can now use it to test ``struct eeprom_buffer``: 364 365.. code-block:: c 366 367 struct eeprom_buffer_test { 368 struct fake_eeprom *fake_eeprom; 369 struct eeprom_buffer *eeprom_buffer; 370 }; 371 372 static void eeprom_buffer_test_does_not_write_until_flush(struct kunit *test) 373 { 374 struct eeprom_buffer_test *ctx = test->priv; 375 struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer; 376 struct fake_eeprom *fake_eeprom = ctx->fake_eeprom; 377 char buffer[] = {0xff}; 378 379 eeprom_buffer->flush_count = SIZE_MAX; 380 381 eeprom_buffer->write(eeprom_buffer, buffer, 1); 382 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0); 383 384 eeprom_buffer->write(eeprom_buffer, buffer, 1); 385 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0); 386 387 eeprom_buffer->flush(eeprom_buffer); 388 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff); 389 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff); 390 } 391 392 static void eeprom_buffer_test_flushes_after_flush_count_met(struct kunit *test) 393 { 394 struct eeprom_buffer_test *ctx = test->priv; 395 struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer; 396 struct fake_eeprom *fake_eeprom = ctx->fake_eeprom; 397 char buffer[] = {0xff}; 398 399 eeprom_buffer->flush_count = 2; 400 401 eeprom_buffer->write(eeprom_buffer, buffer, 1); 402 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0); 403 404 eeprom_buffer->write(eeprom_buffer, buffer, 1); 405 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff); 406 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff); 407 } 408 409 static void eeprom_buffer_test_flushes_increments_of_flush_count(struct kunit *test) 410 { 411 struct eeprom_buffer_test *ctx = test->priv; 412 struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer; 413 struct fake_eeprom *fake_eeprom = ctx->fake_eeprom; 414 char buffer[] = {0xff, 0xff}; 415 416 eeprom_buffer->flush_count = 2; 417 418 eeprom_buffer->write(eeprom_buffer, buffer, 1); 419 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0); 420 421 eeprom_buffer->write(eeprom_buffer, buffer, 2); 422 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff); 423 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff); 424 /* Should have only flushed the first two bytes. */ 425 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[2], 0); 426 } 427 428 static int eeprom_buffer_test_init(struct kunit *test) 429 { 430 struct eeprom_buffer_test *ctx; 431 432 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); 433 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); 434 435 ctx->fake_eeprom = kunit_kzalloc(test, sizeof(*ctx->fake_eeprom), GFP_KERNEL); 436 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->fake_eeprom); 437 fake_eeprom_init(ctx->fake_eeprom); 438 439 ctx->eeprom_buffer = new_eeprom_buffer(&ctx->fake_eeprom->parent); 440 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->eeprom_buffer); 441 442 test->priv = ctx; 443 444 return 0; 445 } 446 447 static void eeprom_buffer_test_exit(struct kunit *test) 448 { 449 struct eeprom_buffer_test *ctx = test->priv; 450 451 destroy_eeprom_buffer(ctx->eeprom_buffer); 452 } 453 454.. _kunit-on-non-uml: 455 456KUnit on non-UML architectures 457============================== 458 459By default KUnit uses UML as a way to provide dependencies for code under test. 460Under most circumstances KUnit's usage of UML should be treated as an 461implementation detail of how KUnit works under the hood. Nevertheless, there 462are instances where being able to run architecture-specific code or test 463against real hardware is desirable. For these reasons KUnit supports running on 464other architectures. 465 466Running existing KUnit tests on non-UML architectures 467----------------------------------------------------- 468 469There are some special considerations when running existing KUnit tests on 470non-UML architectures: 471 472* Hardware may not be deterministic, so a test that always passes or fails 473 when run under UML may not always do so on real hardware. 474* Hardware and VM environments may not be hermetic. KUnit tries its best to 475 provide a hermetic environment to run tests; however, it cannot manage state 476 that it doesn't know about outside of the kernel. Consequently, tests that 477 may be hermetic on UML may not be hermetic on other architectures. 478* Some features and tooling may not be supported outside of UML. 479* Hardware and VMs are slower than UML. 480 481None of these are reasons not to run your KUnit tests on real hardware; they are 482only things to be aware of when doing so. 483 484The biggest impediment will likely be that certain KUnit features and 485infrastructure may not support your target environment. For example, at this 486time the KUnit Wrapper (``tools/testing/kunit/kunit.py``) does not work outside 487of UML. Unfortunately, there is no way around this. Using UML (or even just a 488particular architecture) allows us to make a lot of assumptions that make it 489possible to do things which might otherwise be impossible. 490 491Nevertheless, all core KUnit framework features are fully supported on all 492architectures, and using them is straightforward: all you need to do is to take 493your kunitconfig, your Kconfig options for the tests you would like to run, and 494merge them into whatever config your are using for your platform. That's it! 495 496For example, let's say you have the following kunitconfig: 497 498.. code-block:: none 499 500 CONFIG_KUNIT=y 501 CONFIG_KUNIT_EXAMPLE_TEST=y 502 503If you wanted to run this test on an x86 VM, you might add the following config 504options to your ``.config``: 505 506.. code-block:: none 507 508 CONFIG_KUNIT=y 509 CONFIG_KUNIT_EXAMPLE_TEST=y 510 CONFIG_SERIAL_8250=y 511 CONFIG_SERIAL_8250_CONSOLE=y 512 513All these new options do is enable support for a common serial console needed 514for logging. 515 516Next, you could build a kernel with these tests as follows: 517 518 519.. code-block:: bash 520 521 make ARCH=x86 olddefconfig 522 make ARCH=x86 523 524Once you have built a kernel, you could run it on QEMU as follows: 525 526.. code-block:: bash 527 528 qemu-system-x86_64 -enable-kvm \ 529 -m 1024 \ 530 -kernel arch/x86_64/boot/bzImage \ 531 -append 'console=ttyS0' \ 532 --nographic 533 534Interspersed in the kernel logs you might see the following: 535 536.. code-block:: none 537 538 TAP version 14 539 # Subtest: example 540 1..1 541 # example_simple_test: initializing 542 ok 1 - example_simple_test 543 ok 1 - example 544 545Congratulations, you just ran a KUnit test on the x86 architecture! 546 547In a similar manner, kunit and kunit tests can also be built as modules, 548so if you wanted to run tests in this way you might add the following config 549options to your ``.config``: 550 551.. code-block:: none 552 553 CONFIG_KUNIT=m 554 CONFIG_KUNIT_EXAMPLE_TEST=m 555 556Once the kernel is built and installed, a simple 557 558.. code-block:: bash 559 560 modprobe example-test 561 562...will run the tests. 563 564.. note:: 565 Note that you should make sure your test depends on ``KUNIT=y`` in Kconfig 566 if the test does not support module build. Otherwise, it will trigger 567 compile errors if ``CONFIG_KUNIT`` is ``m``. 568 569Writing new tests for other architectures 570----------------------------------------- 571 572The first thing you must do is ask yourself whether it is necessary to write a 573KUnit test for a specific architecture, and then whether it is necessary to 574write that test for a particular piece of hardware. In general, writing a test 575that depends on having access to a particular piece of hardware or software (not 576included in the Linux source repo) should be avoided at all costs. 577 578Even if you only ever plan on running your KUnit test on your hardware 579configuration, other people may want to run your tests and may not have access 580to your hardware. If you write your test to run on UML, then anyone can run your 581tests without knowing anything about your particular setup, and you can still 582run your tests on your hardware setup just by compiling for your architecture. 583 584.. important:: 585 Always prefer tests that run on UML to tests that only run under a particular 586 architecture, and always prefer tests that run under QEMU or another easy 587 (and monetarily free) to obtain software environment to a specific piece of 588 hardware. 589 590Nevertheless, there are still valid reasons to write an architecture or hardware 591specific test: for example, you might want to test some code that really belongs 592in ``arch/some-arch/*``. Even so, try your best to write the test so that it 593does not depend on physical hardware: if some of your test cases don't need the 594hardware, only require the hardware for tests that actually need it. 595 596Now that you have narrowed down exactly what bits are hardware specific, the 597actual procedure for writing and running the tests is pretty much the same as 598writing normal KUnit tests. One special caveat is that you have to reset 599hardware state in between test cases; if this is not possible, you may only be 600able to run one test case per invocation. 601 602.. TODO(brendanhiggins@google.com): Add an actual example of an architecture- 603 dependent KUnit test. 604 605KUnit debugfs representation 606============================ 607When kunit test suites are initialized, they create an associated directory 608in ``/sys/kernel/debug/kunit/<test-suite>``. The directory contains one file 609 610- results: "cat results" displays results of each test case and the results 611 of the entire suite for the last test run. 612 613The debugfs representation is primarily of use when kunit test suites are 614run in a native environment, either as modules or builtin. Having a way 615to display results like this is valuable as otherwise results can be 616intermixed with other events in dmesg output. The maximum size of each 617results file is KUNIT_LOG_SIZE bytes (defined in ``include/kunit/test.h``). 618