{"id":7131,"date":"2019-01-04T02:26:08","date_gmt":"2019-01-04T02:26:08","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=7131"},"modified":"2019-01-08T11:38:17","modified_gmt":"2019-01-08T11:38:17","slug":"unit-testing-in-the-linux-kernel","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/04\/unit-testing-in-the-linux-kernel\/","title":{"rendered":"Unit Testing in the Linux Kernel"},"content":{"rendered":"<p>Brendan Higgins recently proposed adding unit tests to the Linux kernel,<br \/>\nsupplementing other development infrastructure such as<br \/>\nperf, autotest and<br \/>\nkselftest. The whole issue of testing is very dear to kernel developers&#8217;<br \/>\nhearts, because Linux sits at the core of the system and often has a very<br \/>\nstrong stability\/security requirement. Hosts of automated tests regularly<br \/>\nchurn through kernel source code, reporting any oddities to the mailing<br \/>\nlist.<\/p>\n<p>Unit tests, Brendan said, specialize in testing standalone code snippets.<br \/>\nIt was not necessary to run a whole kernel, or even to compile the kernel<br \/>\nsource tree, in order to perform unit tests. The code to be tested could be<br \/>\ncompletely extracted from the tree and tested independently. Among other<br \/>\nbenefits, this meant that dozens of unit tests could be performed in less<br \/>\nthan a second, he explained.<\/p>\n<p>Giving credit where credit was due, Brendan identified<br \/>\nJUnit, Python&#8217;s<br \/>\nunittest.mock and Googletest\/Googlemock<br \/>\nfor C++ as the inspirations for<br \/>\nthis new KUnit testing idea.<\/p>\n<p>Brendan also pointed out that since all code being unit-tested is<br \/>\nstandalone and has no dependencies, this meant the tests also<br \/>\nwere deterministic. Unlike on a running Linux system, where any number of pieces<br \/>\nof the running system might be responsible for a given problem, unit tests<br \/>\nwould identify problem code with repeatable certainty.<\/p>\n<p>Daniel Vetter replied extremely enthusiastically to Brendan&#8217;s work. In<br \/>\nparticular, he said, &#8220;Having proper and standardized infrastructure for<br \/>\nkernel unit tests sounds terrific. In other words: I want.&#8221; He added that<br \/>\nhe and some others already had been working on a much more specialized set<br \/>\nof unit tests for the Direct Rendering Manager (DRM) driver. Brendan&#8217;s<br \/>\napproach, he said, would be much more convenient than his own more<br \/>\nlocalized efforts.<\/p>\n<p>Dan Williams was also very excited about Brendan&#8217;s work,<br \/>\nand he said he had<br \/>\nbeen doing a half-way job of unit tests on the libnvdimm (non-volatile<br \/>\ndevice) project code. He felt Brendan&#8217;s work was much more general-purpose,<br \/>\nand he wanted to convert his own tests to use KUnit.<\/p>\n<p>Tim Bird replied to Brendan&#8217;s initial email as well, saying he thought unit<br \/>\ntests could be useful, but he wanted to make sure the behaviors were<br \/>\ncorrect. In particular, he wanted clarification on just how it was possible<br \/>\nto test standalone code. If the code were to be compiled independently,<br \/>\nwould it then run on the local system? What if the local system had a<br \/>\ndifferent hardware architecture from the system for which the code was<br \/>\nintended?<br \/>\nAlso, who would maintain unit tests, and where would the tests live, within<br \/>\nthe source tree? Would they clutter up the directory being tested, or would<br \/>\nthey live<br \/>\nfar away in a special directory reserved for test code? And finally, would<br \/>\ntest code be easier to write than the code being tested? In other words,<br \/>\ncould new developers cut their teeth on a project by writing test code, as<br \/>\na gateway to helping work on a given driver or subsystem? Or would unit<br \/>\ntests have to be written by people who had total expertise in the area<br \/>\nalready?<\/p>\n<p>Brendan attempted to address each of those issues in turn. To start, he<br \/>\nconfirmed that the test code was indeed extracted and compiled on the local<br \/>\nsystem. Eventually, he said, each test would compile into its own<br \/>\ncompletely independent test binary, although for the moment, they were all<br \/>\nlumped together into a single user-mode-linux (UML) binary.<\/p>\n<p>In terms of cross-compiling test code for other architectures, Brendan felt<br \/>\nthis would be hard to maintain and had decided not to support it. Tests<br \/>\nwould run locally and would not depend on architecture-specific<br \/>\ncharacteristics.<\/p>\n<p>In terms of where the unit tests would live, Brendan said they would be in<br \/>\nthe same directory as the code being tested. So every directory would have<br \/>\nits own set of unit tests readily available and visible. The same person<br \/>\nmaintaining the code being tested would maintain the tests themselves. The<br \/>\nunit tests, essentially, would become an additional element of every<br \/>\nproject. That maintainer would then presumably require that all patches to<br \/>\nthat driver or subsystem pass all the unit tests before they could be<br \/>\naccepted into the tree.<\/p>\n<p>In terms of who was qualified to write unit tests for a given project,<br \/>\nBrendan explained:<\/p>\n<blockquote><p>In order to write a unit test, the person who writes<br \/>\nthe test must understand what the code they are testing is supposed to do.<br \/>\nTo some extent that will probably require someone with some expertise to<br \/>\nensure that the test makes sense, and indeed a change that breaks a test<br \/>\nshould be accompanied by an update to the test. On the other hand, I think<br \/>\nunderstanding what pre-existing code does and is supposed to do is much<br \/>\neasier than writing new code from scratch, and probably doesn&#8217;t require too<br \/>\nmuch expertise.<\/p><\/blockquote>\n<p>Brendan added that unit tests would probably reduce, rather than increase,<br \/>\na maintainer&#8217;s workload. In spite of representing more code overall:<\/p>\n<blockquote><p>Code<br \/>\nwith unit tests is usually cleaner, the tests tell me exactly what the code<br \/>\nis supposed to do, and I can run the tests (or ideally have an automated<br \/>\nservice run the tests) that tell me that the code actually does what the<br \/>\ntests say it should. Even when it comes to writing code, I find that writing<br \/>\ncode with unit tests ends up saving me time.<\/p><\/blockquote>\n<p>Overall, Brendan was very pleased by all the positive interest, and said he<br \/>\nplanned to do additional releases to address the various technical<br \/>\nsuggestions that came up during the course of discussion.<br \/>\nNo voices really were raised in opposition to any of Brendan&#8217;s ideas. It<br \/>\nappears that unit tests may soon become a standard part of many drivers and<br \/>\nsubsystems.<\/p>\n<p><em>Note: if you&#8217;re mentioned above and want to post a response above the comment section, send a message with your response text to ljeditor@linuxjournal.com.<\/em><\/p>\n<p><a href=\"https:\/\/www.linuxjournal.com\/content\/unit-testing-linux-kernel\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brendan Higgins recently proposed adding unit tests to the Linux kernel, supplementing other development infrastructure such as perf, autotest and kselftest. The whole issue of testing is very dear to kernel developers&#8217; hearts, because Linux sits at the core of the system and often has a very strong stability\/security requirement. Hosts of automated tests regularly &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/04\/unit-testing-in-the-linux-kernel\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Unit Testing in the Linux Kernel&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7131","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/7131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=7131"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/7131\/revisions"}],"predecessor-version":[{"id":7463,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/7131\/revisions\/7463"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=7131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=7131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=7131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}