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