<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: carmeli-tamir</title>
    <description>The latest articles on DEV Community by carmeli-tamir (@carmelitamir).</description>
    <link>https://dev.to/carmelitamir</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F285453%2F7cbdc872-de88-4fad-bd50-62305b047d8b.png</url>
      <title>DEV Community: carmeli-tamir</title>
      <link>https://dev.to/carmelitamir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/carmelitamir"/>
    <language>en</language>
    <item>
      <title>How to write python unit tests for kernel code</title>
      <dc:creator>carmeli-tamir</dc:creator>
      <pubDate>Wed, 04 Dec 2019 20:02:20 +0000</pubDate>
      <link>https://dev.to/carmelitamir/how-to-write-python-unit-tests-to-kernel-code-2g71</link>
      <guid>https://dev.to/carmelitamir/how-to-write-python-unit-tests-to-kernel-code-2g71</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;Testing is a crucial part of every software project, and the Linux Kernel is no exception. Not to repeat many great articles about the subject such as this one, &lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/alysivji" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--12h13fdS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--m5r3d_GR--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/25172/cd548863-b63f-4f90-b097-1f2396ab97d6.jpeg" alt="alysivji image"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/alysivji/testing-101-introduction-to-testing-5hdk" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Testing 101: Introduction to Testing&lt;/h2&gt;
      &lt;h3&gt;Aly Sivji ・ Oct 22 '18 ・ 16 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#testing&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#python&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;
 the article will try to cover some of the testing efforts in the Linux Kernel community, based on an LWN &lt;a href="https://lwn.net/Articles/804050/"&gt;article&lt;/a&gt; covering the topic.

&lt;p&gt;The solutions for end to end testing category include &lt;a href="https://github.com/google/syzkaller/blob/master/docs/syzbot.md"&gt;&lt;strong&gt;syzbot&lt;/strong&gt;&lt;/a&gt; which fuzzes the kernel to find crashes; &lt;a href="https://www.kernel.org/doc/Documentation/kselftest.txt"&gt;&lt;strong&gt;kselftests&lt;/strong&gt;&lt;/a&gt; and &lt;a href="https://linux-test-project.github.io/"&gt;&lt;strong&gt;LTP&lt;/strong&gt;&lt;/a&gt; that test kernel features such as cgroups, inotify, bpf, chmod and more; and &lt;a href="https://kernelci.org/"&gt;&lt;strong&gt;kernelCI&lt;/strong&gt;&lt;/a&gt; that tests the kernel boots on a variety of boards.&lt;/p&gt;

&lt;p&gt;As for unit tests we have the recently merged to mainline &lt;a href="https://lwn.net/Articles/780985/"&gt;&lt;strong&gt;KUnit&lt;/strong&gt;&lt;/a&gt; and Oracle's &lt;a href="https://github.com/oracle/ktf"&gt;&lt;strong&gt;KTF&lt;/strong&gt;&lt;/a&gt;. In both frameworks tests are written in C and execute within the Kernel.&lt;/p&gt;

&lt;p&gt;So, what's missing? Aside from having a couple of solutions that pretty much solve the same problem, it seems that the testing efforts are going in the right direction. However, if you want to unit test your code, you're going to have to write kernel code. Writing kernel code usually takes more time than writing python code, and certainly require a different skill and experience.&lt;/p&gt;
&lt;h1&gt;
  
  
  Skunk
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://github.com/carmeli-tamir/skunk"&gt;Skunk&lt;/a&gt; aims to bridge the mentioned gap of time and skill, and enables Python programmers to unit test kernel code. It basically "wraps" kernel function calls in userspace and sends them to the Skunk Kernel Module, which performs the actual call and returns the response to user space.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/carmeli-tamir/skunk"&gt;Skunk&lt;/a&gt; uses a version of &lt;a href="https://developers.google.com/protocol-buffers"&gt;Google Protobuf&lt;/a&gt; that is &lt;a href="https://github.com/carmeli-tamir/protobuf-c/tree/linux_kernel_support"&gt;ported&lt;/a&gt; to the Linux Kernel to pack and unpack function calls and return values. Generally speaking, parsing stuff in the kernel is considered a bad practice in terms of security since it might lead to all sorts of bugs and vulnerabilities. However, &lt;a href="https://github.com/carmeli-tamir/skunk"&gt;Skunk&lt;/a&gt; is only meant to run on a testing environment and definitely not on production.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/carmeli-tamir/skunk"&gt;Skunk&lt;/a&gt; architecture is roughly:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OG5pEXHP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/carmeli-tamir/skunk/blob/master/doc/skunk_arch.png%3Fraw%3Dtrue" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OG5pEXHP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/carmeli-tamir/skunk/blob/master/doc/skunk_arch.png%3Fraw%3Dtrue" alt="Skunk Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's consider this simple example of testing the kernel implementation of strstr:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sk = Skunk()
ret = sk.call_function_two_arg(
                                "strstr",                                               # Function name
                                2,                                                      # Number of arguments
                                skunk_pb2.FunctionCall.string,                          # Type of return value
                                "whatisthemeaningoflife", skunk_pb2.Argument.string,    # First argument value and type
                                "ning", skunk_pb2.Argument.string)                      # Second argument value and type

assert ret.string=="ningoflife", "error"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you might have guessed, Skunk is just starting, and with the help of the open source community we aim to make it much more comfortable to use. In the longer term we also aim to enable mocking.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;We reviewed some of the Linux Kernel testing solutions. We then introduced &lt;a href="https://github.com/carmeli-tamir/skunk"&gt;Skunk&lt;/a&gt; and explained that it might mitigate difficulties that currently exist when writing kernel UT.&lt;br&gt;
&lt;a href="https://github.com/carmeli-tamir/skunk"&gt;Skunk&lt;/a&gt; needs your help to move forward. There are already open issues suitable for new comers. Any help is appreciated!&lt;/p&gt;

</description>
      <category>linux</category>
      <category>python</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
