kunit: introduce KUnit, the Linux kernel unit testing framework
Subsystems (7)
v4 cover kunit: introduce KUnit, the Linux kernel unit testing framework
This patch set proposes KUnit, a lightweight unit testing and mocking framework for the Linux kernel.
Unlike Autotest and kselftest, KUnit is a true unit testing framework; it does not require installing the kernel on a test machine or in a VM and does not require tests to be written in userspace running on a host kernel. Additionally, KUnit is fast: From invocation to completion KUnit can run several dozen tests in under a second. Currently, the entire KUnit test suite for KUnit runs in under a second from the initial invocation (build time excluded).
KUnit is heavily inspired by JUnit, Python's unittest.mock, and Googletest/Googlemock for C++. KUnit provides facilities for defining unit test cases, grouping related test cases into test suites, providing common infrastructure for running tests, mocking, spying, and much more.
## What's so special about unit testing?
A unit test is supposed to test a single unit of code in isolation, hence the name. There should be no dependencies outside the control of the test; this means no external dependencies, which makes tests orders of magnitudes faster. Likewise, since there are no external dependencies, there are no hoops to jump through to run the tests. Additionally, this makes unit tests deterministic: a failing unit test always indicates a problem. Finally, because unit tests necessarily have finer granularity, they are able to test all code paths easily solving the classic problem of difficulty in exercising error handling code.
## Is KUnit trying to replace other testing frameworks for the kernel?
No. Most existing tests for the Linux kernel are end-to-end tests, which have their place. A well tested system has lots of unit tests, a reasonable number of integration tests, and some end-to-end tests. KUnit is just trying to address the unit test space which is currently not being addressed.
## More information on KUnit
There is a bunch of documentation near the end of this patch set that describes how to use KUnit and best practices for writing unit tests. For convenience I am hosting the compiled docs here:
https://google.github.io/kunit-docs/third_party/kernel/docs/
Additionally for convenience, I have applied these patches to a branch:
https://kunit.googlesource.com/linux/+/kunit/rfc/5.0-rc5/v4
The repo may be cloned with:
git clone https://kunit.googlesource.com/linux
This patchset is on the kunit/rfc/5.0-rc5/v4 branch.
## Changes Since Last Version
- Got KUnit working on (hypothetically) all architectures (tested on x86), as per Rob's (and other's) request
- Punting all KUnit features/patches depending on UML for now.
- Broke out UML specific support into arch/um/* as per "[RFC v3 01/19]
kunit: test: add KUnit test runner core", as requested by Luis.
- Added support to kunit_tool to allow it to build kernels in external directories, as suggested by Kieran.
- Added a UML defconfig, and a config fragment for KUnit as suggested by Kieran and Luis.
- Cleaned up, and reformatted a bunch of stuff.
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
RFC v4 1/17 kunit: test: add KUnit test runner core
Add core facilities for defining unit tests; this provides a common way to define test cases, functions that execute code which is under test and determine whether the code under test behaves as expected; this also provides a way to group together related test cases in test suites (here we call them test_modules).
Just define test cases and how to execute them for now; setting expectations on code will be defined later.
--- include/kunit/test.h | 165 ++++++++++++++++++++++++++++++++++++++++++ kunit/Kconfig | 16 +++++ kunit/Makefile | 1 + kunit/test.c | 168 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 350 insertions(+) create mode 100644 include/kunit/test.h create mode 100644 kunit/Kconfig create mode 100644 kunit/Makefile create mode 100644 kunit/test.c
include/kunit/test.h new file +165 -0
@@ -0,0 +1,165 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Base unit test (KUnit) API. + * + * Copyright (C) 2019, Google LLC. + * Author: Brendan Higgins <[email protected]> + */ + +#ifndef _KUNIT_TEST_H +#define _KUNIT_TEST_H + +#include <linux/types.h> +#include <linux/slab.h> + +struct kunit; + +/** + * struct kunit_case - represents an individual test case. + * @run_case: the function representing the actual test case. + * @name: the name of the test case. + * + * A test case is a function with the signature, ``void (*)(struct kunit *)`` + * that makes expectations (see KUNIT_EXPECT_TRUE()) about code under test. Each + * test case is associated with a &struct kunit_module and will be run after the + * module's init function and followed by the module's exit function. + * + * A test case should be static and should only be created with the KUNIT_CASE() + * macro; additionally, every array of test cases should be terminated with an + * empty test case. + * + * Example: + * + * .. code-block:: c + * + * void add_test_basic(struct kunit *test) + * { + * KUNIT_EXPECT_EQ(test, 1, add(1, 0)); + * KUNIT_EXPECT_EQ(test, 2, add(1, 1)); + * KUNIT_EXPECT_EQ(test, 0, add(-1, 1)); + * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX)); + * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN)); + * } + * + * static struct kunit_case example_test_cases[] = { + * KUNIT_CASE(add_test_basic), + * {}, + * }; + * + */ +struct kunit_case { + void (*run_case)(struct kunit *test); + const char name[256]; + + /* private: internal use only. */ + bool success; +}; + +/** + * KUNIT_CASE - A helper for creating a &struct kunit_case + * @test_name: a reference to a test case function. + * + * Takes a symbol for a function representing a test case and creates a + * &struct kunit_case object from it. See the documentation for + * &struct kunit_case for an example on how to use it. + */ +#define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name } + +/** + * struct kunit_module - describes a related collection of &struct kunit_case s. + * @name: the name of the test. Purely informational. + * @init: called before every test case. + * @exit: called after every test case. + * @test_cases: a null terminated array of test cases. + * + * A kunit_module is a collection of related &struct kunit_case s, such that + * @init is called before every test case and @exit is called after every test + * case, similar to the notion of a *test fixture* or a *test class* in other + * unit testing frameworks like JUnit or Googletest. + * + * Every &struct kunit_case must be associated with a kunit_module for KUnit to + * run it. + */ +struct kunit_module { + const char name[256]; + int (*init)(struct kunit *test); + void (*exit)(struct kunit *test); + struct kunit_case *test_cases; +}; + +/** + * struct kunit - represents a running instance of a test. + * @priv: for user to store arbitrary data. Commonly used to pass data created + * in the init function (see &struct kunit_module). + * + * Used to store information about the current context under which the test is + * running. Most of this data is private and should only be accessed indirectly + * via public functions; the one exception is @priv which can be used by the + * test writer to store arbitrary data. + */ +struct kunit { + void *priv; + + /* private: internal use only. */ + const char *name; /* Read only after initialization! */ + spinlock_t lock; /* Gaurds all mutable test state. */ + bool success; /* Protected by lock. */ + void (*vprintk)(const struct kunit *test, + const char *level, + struct va_format *vaf); +}; + +int kunit_init_test(struct kunit *test, const char *name); + +int kunit_run_tests(struct kunit_module *module); + +/** + * module_test() - used to register a &struct kunit_module with KUnit. + * @module: a statically allocated &struct kunit_module. + * + * Registers @module with the test framework. See &struct kunit_module for more + * information. + */ +#define module_test(module) \ + static int module_kunit_init##module(void) \ + { \ + return kunit_run_tests(&module); \ + } \ + late_initcall(module_kunit_init##module) + +void __printf(3, 4) kunit_printk(const char *level, + const struct kunit *test, + const char *fmt, ...); + +/** + * kunit_info() - Prints an INFO level message associated with the current test. + * @test: The test context object. + * @fmt: A printk() style format string. + * + * Prints an info level message associated with the test module being run. Takes + * a variable number of format parameters just like printk(). + */ +#define kunit_info(test, fmt, ...) \ + kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) + +/** + * kunit_warn() - Prints a WARN level message associated with the current test. + * @test: The test context object. + * @fmt: A printk() style format string. + * + * See kunit_info(). + */ +#define kunit_warn(test, fmt, ...) \ + kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__) + +/** + * kunit_err() - Prints an ERROR level message associated with the current test. + * @test: The test context object. + * @fmt: A printk() style format string. + * + * See kunit_info(). + */ +#define kunit_err(test, fmt, ...) \ + kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) + +#endif /* _KUNIT_TEST_H */
kunit/Kconfig new file +16 -0
@@ -0,0 +1,16 @@ +# +# KUnit base configuration +# + +menu "KUnit support" + +config KUNIT + bool "Enable support for unit tests (KUnit)" + help + Enables support for kernel unit tests (KUnit), a lightweight unit + testing and mocking framework for the Linux kernel. These tests are + able to be run locally on a developer's workstation without a VM or + special hardware. For more information, please see + Documentation/kunit/ + +endmenu
kunit/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_KUNIT) += test.o
kunit/test.c new file +168 -0
168 lines not on this page · raw message
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
RFC v4 2/17 kunit: test: add test resource management API
In reply to Brendan HigginsCreate a common API for test managed resources like memory and test objects. A lot of times a test will want to set up infrastructure to be used in test cases; this could be anything from just wanting to allocate some memory to setting up a driver stack; this defines facilities for creating "test resources" which are managed by the test infrastructure and are automatically cleaned up at the conclusion of the test.
--- include/kunit/test.h | 109 +++++++++++++++++++++++++++++++++++++++++++ kunit/test.c | 95 +++++++++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+)
include/kunit/test.h +109 -0
@@ -12,6 +12,69 @@#include <linux/types.h> #include <linux/slab.h> +struct kunit_resource ; + +typedef int (*kunit_resource_init_t )(struct kunit_resource *,void *); +typedef void (*kunit_resource_free_t )(struct kunit_resource *); + +/** +* struct kunit_resource - represents a *test managed resource* +* @allocation: for the user to store arbitrary data. +* @free: a user supplied function to free the resource. Populated by +* kunit_alloc_resource(). +* +* Represents a *test managed resource*, a resource which will automatically be +* cleaned up at the end of a test case. +* +* Example: +* +* .. code-block:: c +* +* struct kunit_kmalloc_params { +* size_t size; +* gfp_t gfp; +* }; +* +* static int kunit_kmalloc_init(struct kunit_resource *res, void *context) +* { +* struct kunit_kmalloc_params *params = context; +* res->allocation = kmalloc(params->size, params->gfp); +* +* if (!res->allocation) +* return -ENOMEM; +* +* return 0; +* } +* +* static void kunit_kmalloc_free(struct kunit_resource *res) +* { +* kfree(res->allocation); +* } +* +* void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) +* { +* struct kunit_kmalloc_params params; +* struct kunit_resource *res; +* +* params.size = size; +* params.gfp = gfp; +* +* res = kunit_alloc_resource(test, kunit_kmalloc_init, +* kunit_kmalloc_free, ¶ms); +* if (res) +* return res->allocation; +* else +* return NULL; +* } +*/ +struct kunit_resource { +void *allocation; +kunit_resource_free_t free; + +/* private: internal use only. */ +struct list_head node; +}; +struct kunit ;/**
@@ -104,6 +167,7 @@ struct kunit {const char *name;/* Read only after initialization! */ spinlock_t lock;/* Gaurds all mutable test state. */ bool success;/* Protected by lock. */ +struct list_head resources;/* Protected by lock. */ void (*vprintk)(const struct kunit *test,const char *level,struct va_format *vaf);
@@ -127,6 +191,51 @@ int kunit_run_tests(struct kunit_module *module); } \ late_initcall(module_kunit_init##module) +/** +* kunit_alloc_resource() - Allocates a *test managed resource*. +* @test: The test context object. +* @init: a user supplied function to initialize the resource. +* @free: a user supplied function to free the resource. +* @context: for the user to pass in arbitrary data. +* +* Allocates a *test managed resource*, a resource which will automatically be +* cleaned up at the end of a test case. See &struct kunit_resource for an +* example. +*/ +struct kunit_resource *kunit_alloc_resource (struct kunit *test, +kunit_resource_init_t init, +kunit_resource_free_t free, +void *context); + +void kunit_free_resource (struct kunit *test,struct kunit_resource *res); + +/** +* kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*. +* @test: The test context object. +* @size: The size in bytes of the desired memory. +* @gfp: flags passed to underlying kmalloc(). +* +* Just like `kmalloc(...)`, except the allocation is managed by the test case +* and is automatically cleaned up after the test case concludes. See &struct +* kunit_resource for more information. +*/ +void *kunit_kmalloc (struct kunit *test,size_t size,gfp_t gfp); + +/** +* kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation. +* @test: The test context object. +* @size: The size in bytes of the desired memory. +* @gfp: flags passed to underlying kmalloc(). +* +* See kzalloc() and kunit_kmalloc() for more information. +*/ +static inline void *kunit_kzalloc (struct kunit *test,size_t size,gfp_t gfp) +{ +return kunit_kmalloc (test, size, gfp | __GFP_ZERO); +} + +void kunit_cleanup (struct kunit *test); + void __printf(3, 4) kunit_printk(const char *level, const struct kunit *test, const char *fmt, ...);
kunit/test.c +95 -0
119 lines not on this page · raw message
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
Re: [RFC v4 02/17] kunit: test: add test resource management API
Quoting Brendan Higgins (2019-02-14 13:37:14)
Quoted 9 lines
include/kunit/test.h +1
-0@@ -104,6 +167,7 @@ struct kunit {const char *name;/* Read only after initialization! */ spinlock_t lock;/* Gaurds all mutable test state. */ bool success;/* Protected by lock. */ +struct list_head resources;/* Protected by lock. */ void (*vprintk)(const struct kunit *test,const char *level,struct va_format *vaf);
include/kunit/test.h int kunit_run_tests(struct kunit_module *module); · line 199
Quoted 9 lines
include/kunit/test.h +6
-0@@ -127,6 +191,51 @@ int kunit_run_tests(struct kunit_module *module); } \ late_initcall(module_kunit_init##module) +/** +* kunit_alloc_resource() - Allocates a *test managed resource*. +* @test: The test context object. +* @init: a user supplied function to initialize the resource. +* @free: a user supplied function to free the resource. +* @context: for the user to pass in arbitrary data.
Nitpick: "pass in arbitrary data to the init function"? Maybe that provides some more clarity.
Quoted 5 lines
include/kunit/test.h +5
-0+* +* Allocates a *test managed resource*, a resource which will automatically be +* cleaned up at the end of a test case. See &struct kunit_resource for an +* example. +*/
Re: [RFC v4 02/17] kunit: test: add test resource management API
Quoted 14 lines · Stephen Boyd
Quoting Brendan Higgins (2019-02-14 13:37:14)
Quoted 12 lines
include/kunit/test.h +1
-0@@ -104,6 +167,7 @@ struct kunit {const char *name;/* Read only after initialization! */ spinlock_t lock;/* Gaurds all mutable test state. */ bool success;/* Protected by lock. */ +struct list_head resources;/* Protected by lock. */ void (*vprintk)(const struct kunit *test,const char *level,struct va_format *vaf);@@ -127,6 +191,51 @@ int kunit_run_tests(struct kunit_module *module); } \ late_initcall(module_kunit_init##module)
Quoted 9 lines
include/kunit/test.h int kunit_run_tests(struct kunit_module *module); · line 199
Quoted 6 lines
include/kunit/test.h +6
-0+/** +* kunit_alloc_resource() - Allocates a *test managed resource*. +* @test: The test context object. +* @init: a user supplied function to initialize the resource. +* @free: a user supplied function to free the resource. +* @context: for the user to pass in arbitrary data. Nitpick: "pass in arbitrary data to the init function"? Maybe that provides some more clarity.
I think that makes sense. Will fix in next revision.
RFC v4 3/17 kunit: test: add string_stream a std::stream like string builder
In reply to Brendan HigginsA number of test features need to do pretty complicated string printing where it may not be possible to rely on a single preallocated string with parameters.
So provide a library for constructing the string as you go similar to C++'s std::string.
---
Changes Since Last Version
- None. There was some discussion about maybe trying to generalize this or replace it with something existing, but it didn't seem feasible to generalize this, and there wasn't really anything that is a great replacement.
--- include/kunit/string-stream.h | 44 ++++++++++ kunit/Makefile | 3 +- kunit/string-stream.c | 149 ++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 include/kunit/string-stream.h create mode 100644 kunit/string-stream.c
include/kunit/string-stream.h new file +44 -0
@@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* C++ stream style string builder used in KUnit for building messages. +* +* Copyright (C) 2019, Google LLC. +* Author: Brendan Higgins <[email protected]> +*/ + +#ifndef _KUNIT_STRING_STREAM_H +#define _KUNIT_STRING_STREAM_H + +#include <linux/types.h> +#include <linux/spinlock.h> +#include <linux/kref.h> +#include <stdarg.h> + +struct string_stream_fragment { +struct list_head node; +char *fragment; +}; + +struct string_stream { +size_t length; +struct list_head fragments; + +/* length and fragments are protected by this lock */ +spinlock_t lock; +struct kref refcount; +int (*add)(struct string_stream *this,const char *fmt, ...); +int (*vadd)(struct string_stream *this,const char *fmt,va_list args); +char *(*get_string)(struct string_stream *this); +void (*clear)(struct string_stream *this); +bool (*is_empty)(struct string_stream *this); +}; + +struct string_stream *new_string_stream (void ); + +void destroy_string_stream (struct string_stream *stream); + +void string_stream_get (struct string_stream *stream); + +int string_stream_put (struct string_stream *stream); + +#endif /* _KUNIT_STRING_STREAM_H */
kunit/Makefile +2 -1
@@ -1 +1,2 @@ -obj-$(CONFIG_KUNIT) += test.o +obj-$(CONFIG_KUNIT) += test.o \ + string-stream.o
kunit/string-stream.c new file +149 -0
149 lines not on this page · raw message
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
RFC v4 4/17 kunit: test: add test_stream a std::stream like logger
In reply to Brendan HigginsA lot of the expectation and assertion infrastructure prints out fairly complicated test failure messages, so add a C++ style log library for for logging test results.
--- include/kunit/kunit-stream.h | 50 ++++++++++++ include/kunit/test.h | 2 + kunit/Makefile | 3 +- kunit/kunit-stream.c | 153 +++++++++++++++++++++++++++++++++++ kunit/test.c | 8 ++ 5 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 include/kunit/kunit-stream.h create mode 100644 kunit/kunit-stream.c
include/kunit/kunit-stream.h new file +50 -0
@@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* C++ stream style string formatter and printer used in KUnit for outputting +* KUnit messages. +* +* Copyright (C) 2019, Google LLC. +* Author: Brendan Higgins <[email protected]> +*/ + +#ifndef _KUNIT_KUNIT_STREAM_H +#define _KUNIT_KUNIT_STREAM_H + +#include <linux/types.h> +#include <kunit/string-stream.h> + +struct kunit ; + +/** +* struct kunit_stream - a std::stream style string builder. +* @set_level: sets the level that this string should be printed at. +* @add: adds the formatted input to the internal buffer. +* @append: adds the contents of other to this. +* @commit: prints out the internal buffer to the user. +* @clear: clears the internal buffer. +* +* A std::stream style string builder. Allows messages to be built up and +* printed all at once. +*/ +struct kunit_stream { +void (*set_level)(struct kunit_stream *this,const char *level); +void (*add)(struct kunit_stream *this,const char *fmt, ...); +void (*append)(struct kunit_stream *this,struct kunit_stream *other); +void (*commit)(struct kunit_stream *this); +void (*clear)(struct kunit_stream *this); +/* private: internal use only. */ +struct kunit *test; +spinlock_t lock;/* Guards level. */ +const char *level; +struct string_stream *internal_stream; +}; + +/** +* kunit_new_stream() - constructs a new &struct kunit_stream. +* @test: The test context object. +* +* Constructs a new test managed &struct kunit_stream. +*/ +struct kunit_stream *kunit_new_stream (struct kunit *test); + +#endif /* _KUNIT_KUNIT_STREAM_H */
include/kunit/test.h +2 -0
@@ -11,6 +11,7 @@#include <linux/types.h> #include <linux/slab.h> +#include <kunit/kunit-stream.h> struct kunit_resource ;
@@ -171,6 +172,7 @@ struct kunit {void (*vprintk)(const struct kunit *test,const char *level,struct va_format *vaf); +void (*fail)(struct kunit *test,struct kunit_stream *stream); };int kunit_init_test (struct kunit *test,const char *name);
kunit/Makefile +2 -1
@@ -1,2 +1,3 @@ obj-$(CONFIG_KUNIT) += test.o \ - string-stream.o + string-stream.o \ + kunit-stream.o
kunit/kunit-stream.c new file +153 -0
153 lines not on this page · raw message
kunit/test.c +8 -0
@@ -63,12 +63,20 @@ static void kunit_vprintk(const struct kunit *test, "kunit %s: %pV", test->name, vaf); } +static void kunit_fail (struct kunit *test,struct kunit_stream *stream) +{ +kunit_set_success (test, false); + stream->set_level (stream,KERN_ERR ); + stream->commit (stream); +} +int kunit_init_test (struct kunit *test,const char *name) {spin_lock_init (&test->lock);INIT_LIST_HEAD (&test->resources); test->name = name; test->vprintk = kunit_vprintk; + test->fail = kunit_fail;return 0 ; }
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
RFC v4 5/17 kunit: test: add the concept of expectations
In reply to Brendan HigginsAdd support for expectations, which allow properties to be specified and then verified in tests.
--- include/kunit/test.h | 415 +++++++++++++++++++++++++++++++++++++++++++ kunit/test.c | 34 ++++ 2 files changed, 449 insertions(+)
include/kunit/test.h +415 -0
@@ -273,4 +273,419 @@ void __printf(3, 4) kunit_printk(const char *level,#define kunit_err (test, fmt, ...) \ kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) +static inline struct kunit_stream *kunit_expect_start (struct kunit *test, +const char *file, +const char *line) +{ +struct kunit_stream *stream =kunit_new_stream (test); + + stream->add (stream,"EXPECTATION FAILED at %s:%s\n\t" , file, line); + +return stream; +} + +static inline void kunit_expect_end (struct kunit *test, +bool success, +struct kunit_stream *stream) +{ +if (!success) + test->fail (test, stream); +else + stream->clear (stream); +} + +#define KUNIT_EXPECT_START (test) \ + kunit_expect_start(test, __FILE__, __stringify(__LINE__)) + +#define KUNIT_EXPECT_END (test, success, stream) \ + kunit_expect_end(test, success, stream) + +#define KUNIT_EXPECT_MSG (test, success, message, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + \ + __stream->add(__stream, message); \ + __stream->add(__stream, fmt, ##__VA_ARGS__); \ + KUNIT_EXPECT_END(test, success, __stream); \ +} while (0) + +#define KUNIT_EXPECT (test, success, message) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + \ + __stream->add(__stream, message); \ + KUNIT_EXPECT_END(test, success, __stream); \ +} while (0) + +/** +* KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity. +* @test: The test context object. +* +* The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other +* words, it does nothing and only exists for code clarity. See +* KUNIT_EXPECT_TRUE() for more information. +*/ +#define KUNIT_SUCCEED (test) do {} while (0) + +/** +* KUNIT_FAIL() - Always causes a test to fail when evaluated. +* @test: The test context object. +* @fmt: an informational message to be printed when the assertion is made. +* @...: string format arguments. +* +* The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In +* other words, it always results in a failed expectation, and consequently +* always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE() +* for more information. +*/ +#define KUNIT_FAIL (test, fmt, ...) \ + KUNIT_EXPECT_MSG(test, false, "", fmt, ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true. +* @test: The test context object. +* @condition: an arbitrary boolean expression. The test fails when this does +* not evaluate to true. +* +* This and expectations of the form `KUNIT_EXPECT_*` will cause the test case +* to fail when the specified condition is not met; however, it will not prevent +* the test case from continuing to run; this is otherwise known as an +* *expectation failure*. +*/ +#define KUNIT_EXPECT_TRUE (test, condition) \ + KUNIT_EXPECT(test, (condition), \ + "Expected " #condition " is true, but is false.") + +#define KUNIT_EXPECT_TRUE_MSG (test, condition, fmt, ...) \ + KUNIT_EXPECT_MSG(test, (condition), \ + "Expected " #condition " is true, but is false.\n",\ + fmt, ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false. +* @test: The test context object. +* @condition: an arbitrary boolean expression. The test fails when this does +* not evaluate to false. +* +* Sets an expectation that @condition evaluates to false. See +* KUNIT_EXPECT_TRUE() for more information. +*/ +#define KUNIT_EXPECT_FALSE (test, condition) \ + KUNIT_EXPECT(test, !(condition), \ + "Expected " #condition " is false, but is true.") + +#define KUNIT_EXPECT_FALSE_MSG (test, condition, fmt, ...) \ + KUNIT_EXPECT_MSG(test, !(condition), \ + "Expected " #condition " is false, but is true.\n",\ + fmt, ##__VA_ARGS__) + +void kunit_expect_binary_msg (struct kunit *test, +long long left,const char *left_name, +long long right,const char *right_name, +bool compare_result, +const char *compare_name, +const char *file, +const char *line, +const char *fmt, ...); + +static inline void kunit_expect_binary (struct kunit *test, +long long left,const char *left_name, +long long right,const char *right_name, +bool compare_result, +const char *compare_name, +const char *file, +const char *line) +{ +struct kunit_stream *stream =kunit_expect_start (test, file, line); + + stream->add (stream, +"Expected %s %s %s, but\n" , + left_name, compare_name, right_name); + stream->add (stream,"\t\t%s == %lld\n" , left_name, left); + stream->add (stream,"\t\t%s == %lld" , right_name, right); + +kunit_expect_end (test, compare_result, stream); +} + +/* +* A factory macro for defining the expectations for the basic comparisons +* defined for the built in types. +* +* Unfortunately, there is no common type that all types can be promoted to for +* which all the binary operators behave the same way as for the actual types +* (for example, there is no type that long long and unsigned long long can +* both be cast to where the comparison result is preserved for all values). So +* the best we can do is do the comparison in the original types and then coerce +* everything to long long for printing; this way, the comparison behaves +* correctly and the printed out value usually makes sense without +* interpretation, but can always be interpretted to figure out the actual +* value. +*/ +#define KUNIT_EXPECT_BINARY (test, left, condition, right) do { \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + kunit_expect_binary(test, \ + (long long) __left, #left, \ + (long long) __right, #right, \ + __left condition __right, #condition, \ + __FILE__, __stringify(__LINE__)); \ +} while (0) + +#define KUNIT_EXPECT_BINARY_MSG (test, left, condition, right, fmt, ...) do { \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + kunit_expect_binary_msg(test, \ + (long long) __left, #left, \ + (long long) __right, #right, \ + __left condition __right, #condition, \ + __FILE__, __stringify(__LINE__), \ + fmt, ##__VA_ARGS__); \ +} while (0) + +/** +* KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a primitive C type. +* @right: an arbitrary expression that evaluates to a primitive C type. +* +* Sets an expectation that the values that @left and @right evaluate to are +* equal. This is semantically equivalent to +* KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_EQ (test, left, right) \ + KUNIT_EXPECT_BINARY(test, left, ==, right) + +#define KUNIT_EXPECT_EQ_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_BINARY_MSG(test, \ + left, \ + ==, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a primitive C type. +* @right: an arbitrary expression that evaluates to a primitive C type. +* +* Sets an expectation that the values that @left and @right evaluate to are not +* equal. This is semantically equivalent to +* KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_NE (test, left, right) \ + KUNIT_EXPECT_BINARY(test, left, !=, right) + +#define KUNIT_EXPECT_NE_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_BINARY_MSG(test, \ + left, \ + !=, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_LT() - An expectation that @left is less than @right. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a primitive C type. +* @right: an arbitrary expression that evaluates to a primitive C type. +* +* Sets an expectation that the value that @left evaluates to is less than the +* value that @right evaluates to. This is semantically equivalent to +* KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_LT (test, left, right) \ + KUNIT_EXPECT_BINARY(test, left, <, right) + +#define KUNIT_EXPECT_LT_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_BINARY_MSG(test, \ + left, \ + <, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a primitive C type. +* @right: an arbitrary expression that evaluates to a primitive C type. +* +* Sets an expectation that the value that @left evaluates to is less than or +* equal to the value that @right evaluates to. Semantically this is equivalent +* to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_LE (test, left, right) \ + KUNIT_EXPECT_BINARY(test, left, <=, right) + +#define KUNIT_EXPECT_LE_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_BINARY_MSG(test, \ + left, \ + <=, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_GT() - An expectation that @left is greater than @right. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a primitive C type. +* @right: an arbitrary expression that evaluates to a primitive C type. +* +* Sets an expectation that the value that @left evaluates to is greater than +* the value that @right evaluates to. This is semantically equivalent to +* KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_GT (test, left, right) \ + KUNIT_EXPECT_BINARY(test, left, >, right) + +#define KUNIT_EXPECT_GT_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_BINARY_MSG(test, \ + left, \ + >, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a primitive C type. +* @right: an arbitrary expression that evaluates to a primitive C type. +* +* Sets an expectation that the value that @left evaluates to is greater than +* the value that @right evaluates to. This is semantically equivalent to +* KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_GE (test, left, right) \ + KUNIT_EXPECT_BINARY(test, left, >=, right) + +#define KUNIT_EXPECT_GE_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_BINARY_MSG(test, \ + left, \ + >=, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a null terminated string. +* @right: an arbitrary expression that evaluates to a null terminated string. +* +* Sets an expectation that the values that @left and @right evaluate to are +* equal. This is semantically equivalent to +* KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() +* for more information. +*/ +#define KUNIT_EXPECT_STREQ (test, left, right) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + __stream->add(__stream, "Expected " #left " == " #right ", but\n"); \ + __stream->add(__stream, "\t\t%s == %s\n", #left, __left); \ + __stream->add(__stream, "\t\t%s == %s\n", #right, __right); \ + \ + KUNIT_EXPECT_END(test, !strcmp(left, right), __stream); \ +} while (0) + +#define KUNIT_EXPECT_STREQ_MSG (test, left, right, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + __stream->add(__stream, "Expected " #left " == " #right ", but\n"); \ + __stream->add(__stream, "\t\t%s == %s\n", #left, __left); \ + __stream->add(__stream, "\t\t%s == %s\n", #right, __right); \ + __stream->add(__stream, fmt, ##__VA_ARGS__); \ + \ + KUNIT_EXPECT_END(test, !strcmp(left, right), __stream); \ +} while (0) + +/** +* KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a null terminated string. +* @right: an arbitrary expression that evaluates to a null terminated string. +* +* Sets an expectation that the values that @left and @right evaluate to are +* not equal. This is semantically equivalent to +* KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() +* for more information. +*/ +#define KUNIT_EXPECT_STRNEQ (test, left, right) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + __stream->add(__stream, "Expected " #left " == " #right ", but\n"); \ + __stream->add(__stream, "\t\t%s == %s\n", #left, __left); \ + __stream->add(__stream, "\t\t%s == %s\n", #right, __right); \ + \ + KUNIT_EXPECT_END(test, strcmp(left, right), __stream); \ +} while (0) + +#define KUNIT_EXPECT_STRNEQ_MSG (test, left, right, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + __stream->add(__stream, "Expected " #left " == " #right ", but\n"); \ + __stream->add(__stream, "\t\t%s == %s\n", #left, __left); \ + __stream->add(__stream, "\t\t%s == %s\n", #right, __right); \ + __stream->add(__stream, fmt, ##__VA_ARGS__); \ + \ + KUNIT_EXPECT_END(test, strcmp(left, right), __stream); \ +} while (0) + +/** +* KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err. +* @test: The test context object. +* @ptr: an arbitrary pointer. +* +* Sets an expectation that the value that @ptr evaluates to is not null and not +* an errno stored in a pointer. This is semantically equivalent to +* KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_NOT_ERR_OR_NULL (test, ptr) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + typeof(ptr) __ptr = (ptr); \ + \ + if (!__ptr) \ + __stream->add(__stream, \ + "Expected " #ptr " is not null, but is."); \ + if (IS_ERR(__ptr)) \ + __stream->add(__stream, \ + "Expected " #ptr " is not error, but is: %ld", \ + PTR_ERR(__ptr)); \ + \ + KUNIT_EXPECT_END(test, !IS_ERR_OR_NULL(__ptr), __stream); \ +} while (0) + +#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG (test, ptr, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + typeof(ptr) __ptr = (ptr); \ + \ + if (!__ptr) { \ + __stream->add(__stream, \ + "Expected " #ptr " is not null, but is."); \ + __stream->add(__stream, fmt, ##__VA_ARGS__); \ + } \ + if (IS_ERR(__ptr)) { \ + __stream->add(__stream, \ + "Expected " #ptr " is not error, but is: %ld", \ + PTR_ERR(__ptr)); \ + \ + __stream->add(__stream, fmt, ##__VA_ARGS__); \ + } \ + KUNIT_EXPECT_END(test, !IS_ERR_OR_NULL(__ptr), __stream); \ +} while (0) +#endif /* _KUNIT_TEST_H */
kunit/test.c +34 -0
@@ -269,3 +269,37 @@ void kunit_printk(const char *level,va_end (args); } + +void kunit_expect_binary_msg (struct kunit *test, +long long left,const char *left_name, +long long right,const char *right_name, +bool compare_result, +const char *compare_name, +const char *file, +const char *line, +const char *fmt, ...) +{ +struct kunit_stream *stream =kunit_expect_start (test, file, line); +struct va_format vaf; +va_list args; + + stream->add (stream, +"Expected %s %s %s, but\n" , + left_name, compare_name, right_name); + stream->add (stream,"\t\t%s == %lld\n" , left_name, left); + stream->add (stream,"\t\t%s == %lld" , right_name, right); + +if (fmt) { +va_start (args, fmt); + + vaf.fmt = fmt; + vaf.va = &args; + + stream->add (stream,"\n%pV" , &vaf); + +va_end (args); + } + +kunit_expect_end (test, compare_result, stream); +} +
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
RFC v4 6/17 kbuild: enable building KUnit
In reply to Brendan HigginsAdd KUnit to root Kconfig and Makefile allowing it to actually be built.
---
Changes Since Last Version
- Rewrote patch description. This was previously called "[RFC v3 06/19]
arch: um: enable running kunit from User Mode Linux," which was incorrect since this patch does not have any UML specific bits in it.
--- Kconfig | 2 ++ Makefile | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)
Kconfig +2 -0
@@ -30,3 +30,5 @@ source "crypto/Kconfig" source "lib/Kconfig" source "lib/Kconfig.debug" + +source "kunit/Kconfig"
Makefile +1 -1
@@ -958,7 +958,7 @@ endif PHONY += prepare0 ifeq ($(KBUILD_EXTMOD),) -core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/ +core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/ kunit/ vmlinux-dirs := $(patsubst %/,%,$(filter %/, $(init-y) $(init-m) \ $(core-y) $(core-m) $(drivers-y) $(drivers-m) \
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
RFC v4 7/17 kunit: test: add initial tests
In reply to Brendan HigginsAdd a test for string stream along with a simpler example.
--- kunit/Kconfig | 12 ++++++ kunit/Makefile | 4 ++ kunit/example-test.c | 88 ++++++++++++++++++++++++++++++++++++++ kunit/string-stream-test.c | 61 ++++++++++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 kunit/example-test.c create mode 100644 kunit/string-stream-test.c
kunit/Kconfig +12 -0
@@ -13,4 +13,16 @@ config KUNIT special hardware. For more information, please see Documentation/kunit/ +config KUNIT_TEST + bool "KUnit test for KUnit" + depends on KUNIT + help + Enables KUnit test to test KUnit. + +config KUNIT_EXAMPLE_TEST + bool "Example test for KUnit" + depends on KUNIT + help + Enables example KUnit test to demo features of KUnit. + endmenu
kunit/Makefile +4 -0
@@ -1,3 +1,7 @@ obj-$(CONFIG_KUNIT) += test.o \ string-stream.o \ kunit-stream.o + +obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o + +obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += example-test.o
kunit/example-test.c new file +88 -0
88 lines not on this page · raw message
kunit/string-stream-test.c new file +61 -0
61 lines not on this page · raw message
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
RFC v4 8/17 kunit: test: add support for test abort
In reply to Brendan HigginsAdd support for aborting/bailing out of test cases. Needed for implementing assertions.
---
Changes Since Last Version
- This patch is new introducing a new cross-architecture way to abort out of a test case (needed for KUNIT_ASSERT_*, see next patch for details).
- On a side note, this is not a complete replacement for the UML abort mechanism, but covers the majority of necessary functionality. UML architecture specific featurs have been dropped from the initial patchset.
--- include/kunit/test.h | 24 +++++ kunit/Makefile | 3 +- kunit/test-test.c | 127 ++++++++++++++++++++++++++ kunit/test.c | 208 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 353 insertions(+), 9 deletions(-) create mode 100644 kunit/test-test.c
include/kunit/test.h +24 -0
@@ -151,6 +151,26 @@ struct kunit_module {struct kunit_case *test_cases; }; +struct kunit_try_catch_context { +struct kunit *test; +struct kunit_module *module; +struct kunit_case *test_case; +struct completion *try_completion; +int try_result; +}; + +struct kunit_try_catch { +void (*run)(struct kunit_try_catch *try_catch); +void (*throw)(struct kunit_try_catch *try_catch); +struct kunit_try_catch_context context; +void (*try)(struct kunit_try_catch_context *context); +void (*catch)(struct kunit_try_catch_context *context); +}; + +void kunit_try_catch_init (struct kunit_try_catch *try_catch); + +void kunit_generic_try_catch_init (struct kunit_try_catch *try_catch); +/** * struct kunit - represents a running instance of a test. * @priv: for user to store arbitrary data. Commonly used to pass data created
@@ -166,13 +186,17 @@ struct kunit {/* private: internal use only. */ const char *name;/* Read only after initialization! */ +struct kunit_try_catch try_catch;spinlock_t lock;/* Gaurds all mutable test state. */ bool success;/* Protected by lock. */ +bool death_test;/* Protected by lock. */ struct list_head resources;/* Protected by lock. */ +void (*set_death_test)(struct kunit *test,bool death_test);void (*vprintk)(const struct kunit *test,const char *level,struct va_format *vaf);void (*fail)(struct kunit *test,struct kunit_stream *stream); +void (*abort)(struct kunit *test); };int kunit_init_test (struct kunit *test,const char *name);
kunit/Makefile +2 -1
@@ -2,6 +2,7 @@ obj-$(CONFIG_KUNIT) += test.o \ string-stream.o \ kunit-stream.o -obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o +obj-$(CONFIG_KUNIT_TEST) += test-test.o \ + string-stream-test.o obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += example-test.o
kunit/test-test.c new file +127 -0
127 lines not on this page · raw message
kunit/test.c +200 -8
251 lines not on this page · raw message
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoted 20 lines · Brendan Higgins
Add support for aborting/bailing out of test cases. Needed for implementing assertions.
Signed-off-by Brendan Higgins <[email protected]>---
Changes Since Last Version
- This patch is new introducing a new cross-architecture way to abort out of a test case (needed for KUNIT_ASSERT_*, see next patch for details).
- On a side note, this is not a complete replacement for the UML abort mechanism, but covers the majority of necessary functionality. UML architecture specific featurs have been dropped from the initial patchset.--- include/kunit/test.h | 24 +++++ kunit/Makefile | 3 +- kunit/test-test.c | 127 ++++++++++++++++++++++++++ kunit/test.c | 208 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 353 insertions(+), 9 deletions(-) create mode 100644 kunit/test-test.c
< snip >
Quoted 50 lines
kunit/test.c +25
-2@@ -6,9 +6,9 @@* Author: Brendan Higgins <[email protected]> */ -#include <linux/sched.h> #include <linux/sched/debug.h> -#include <os.h> +#include <linux/completion.h> +#include <linux/kthread.h> #include <kunit/test.h> static bool kunit_get_success (struct kunit *test)@@ -32,6 +32,27 @@ static void kunit_set_success(struct kunit *test, bool success)spin_unlock_irqrestore (&test->lock, flags); } +static bool kunit_get_death_test (struct kunit *test) +{ +unsigned long flags; +bool death_test; + +spin_lock_irqsave (&test->lock, flags); + death_test = test->death_test; +spin_unlock_irqrestore (&test->lock, flags); + +return death_test; +} + +static void kunit_set_death_test (struct kunit *test,bool death_test) +{ +unsigned long flags; + +spin_lock_irqsave (&test->lock, flags); + test->death_test = death_test; +spin_unlock_irqrestore (&test->lock, flags); +} + static int kunit_vprintk_emit(const struct kunit *test, int level, const char *fmt,@@ -70,13 +91,29 @@ static void kunit_fail(struct kunit *test, struct kunit_stream *stream) stream->commit (stream); } +static void __noreturn kunit_abort (struct kunit *test) +{
kunit/test.c static void kunit_fail(struct kunit *test, struct kunit_stream *stream) · line 105
Quoted 10 lines
kunit/test.c +10
-0+kunit_set_death_test (test, true); + + test->try_catch.throw (&test->try_catch); + +/* +* Throw could not abort from test. +*/ +kunit_err (test,"Throw could not abort from test!" ); +show_stack (NULL ,NULL ); +BUG ();
kunit_abort() is what will be call as the result of an assert failure.
BUG(), which is a panic, which is crashing the system is not acceptable in the Linux kernel. You will just annoy Linus if you submit this.
-Frank
< snip >
Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoted 73 lines · Frank Rowand
Quoted 20 lines · Brendan Higgins
Add support for aborting/bailing out of test cases. Needed for implementing assertions.
Signed-off-by Brendan Higgins <[email protected]>---
Changes Since Last Version
- This patch is new introducing a new cross-architecture way to abort out of a test case (needed for KUNIT_ASSERT_*, see next patch for details).
- On a side note, this is not a complete replacement for the UML abort mechanism, but covers the majority of necessary functionality. UML architecture specific featurs have been dropped from the initial patchset.--- include/kunit/test.h | 24 +++++ kunit/Makefile | 3 +- kunit/test-test.c | 127 ++++++++++++++++++++++++++ kunit/test.c | 208 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 353 insertions(+), 9 deletions(-) create mode 100644 kunit/test-test.c< snip >
Quoted 48 lines
kunit/test.c +23
-2@@ -6,9 +6,9 @@* Author: Brendan Higgins <[email protected]> */ -#include <linux/sched.h> #include <linux/sched/debug.h> -#include <os.h> +#include <linux/completion.h> +#include <linux/kthread.h> #include <kunit/test.h> static bool kunit_get_success (struct kunit *test)@@ -32,6 +32,27 @@ static void kunit_set_success(struct kunit *test, bool success)spin_unlock_irqrestore (&test->lock, flags); } +static bool kunit_get_death_test (struct kunit *test) +{ +unsigned long flags; +bool death_test; + +spin_lock_irqsave (&test->lock, flags); + death_test = test->death_test; +spin_unlock_irqrestore (&test->lock, flags); + +return death_test; +} + +static void kunit_set_death_test (struct kunit *test,bool death_test) +{ +unsigned long flags; + +spin_lock_irqsave (&test->lock, flags); + test->death_test = death_test; +spin_unlock_irqrestore (&test->lock, flags); +} + static int kunit_vprintk_emit(const struct kunit *test, int level, const char *fmt,@@ -70,13 +91,29 @@ static void kunit_fail(struct kunit *test, struct kunit_stream *stream) stream->commit (stream); }
Quoted 14 lines
kunit/test.c static void kunit_fail(struct kunit *test, struct kunit_stream *stream) · line 105
Quoted 12 lines
kunit/test.c +12
-0+static void __noreturn kunit_abort (struct kunit *test) +{ +kunit_set_death_test (test, true); + + test->try_catch.throw (&test->try_catch); + +/* +* Throw could not abort from test. +*/ +kunit_err (test,"Throw could not abort from test!" ); +show_stack (NULL ,NULL ); +BUG ();
kunit_abort()is what will be call as the result of an assert failure.
Yep. Does that need clarified somewhere?
Quoted 3 lines
BUG(), which is a panic, which is crashing the system is not acceptable in the Linux kernel. You will just annoy Linus if you submit this.
Sorry, I thought this was an acceptable use case since, a) this should never be compiled in a production kernel, b) we are in a pretty bad, unpredictable state if we get here and keep going. I think you might have said elsewhere that you think "a" is not valid? In any case, I can replace this with a WARN, would that be acceptable?
Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoted 91 lines · Brendan Higgins
Quoted 73 lines · Frank Rowand
Quoted 20 lines · Brendan Higgins
Add support for aborting/bailing out of test cases. Needed for implementing assertions.
Signed-off-by Brendan Higgins <[email protected]>---
Changes Since Last Version
- This patch is new introducing a new cross-architecture way to abort out of a test case (needed for KUNIT_ASSERT_*, see next patch for details).
- On a side note, this is not a complete replacement for the UML abort mechanism, but covers the majority of necessary functionality. UML architecture specific featurs have been dropped from the initial patchset.--- include/kunit/test.h | 24 +++++ kunit/Makefile | 3 +- kunit/test-test.c | 127 ++++++++++++++++++++++++++ kunit/test.c | 208 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 353 insertions(+), 9 deletions(-) create mode 100644 kunit/test-test.c< snip >
Quoted 48 lines
kunit/test.c +23
-2@@ -6,9 +6,9 @@* Author: Brendan Higgins <[email protected]> */ -#include <linux/sched.h> #include <linux/sched/debug.h> -#include <os.h> +#include <linux/completion.h> +#include <linux/kthread.h> #include <kunit/test.h> static bool kunit_get_success (struct kunit *test)@@ -32,6 +32,27 @@ static void kunit_set_success(struct kunit *test, bool success)spin_unlock_irqrestore (&test->lock, flags); } +static bool kunit_get_death_test (struct kunit *test) +{ +unsigned long flags; +bool death_test; + +spin_lock_irqsave (&test->lock, flags); + death_test = test->death_test; +spin_unlock_irqrestore (&test->lock, flags); + +return death_test; +} + +static void kunit_set_death_test (struct kunit *test,bool death_test) +{ +unsigned long flags; + +spin_lock_irqsave (&test->lock, flags); + test->death_test = death_test; +spin_unlock_irqrestore (&test->lock, flags); +} + static int kunit_vprintk_emit(const struct kunit *test, int level, const char *fmt,@@ -70,13 +91,29 @@ static void kunit_fail(struct kunit *test, struct kunit_stream *stream) stream->commit (stream); }Quoted 14 lines
kunit/test.c static void kunit_fail(struct kunit *test, struct kunit_stream *stream) · line 105
Quoted 12 lines
kunit/test.c +12
-0+static void __noreturn kunit_abort (struct kunit *test) +{ +kunit_set_death_test (test, true); + + test->try_catch.throw (&test->try_catch); + +/* +* Throw could not abort from test. +*/ +kunit_err (test,"Throw could not abort from test!" ); +show_stack (NULL ,NULL ); +BUG ();
kunit_abort()is what will be call as the result of an assert failure.Yep. Does that need clarified somewhere.
Quoted 1 line
Quoted 8 lines
Quoted 2 lines
BUG(), which is a panic, which is crashing the system is not acceptable in the Linux kernel. You will just annoy Linus if you submit this.Sorry, I thought this was an acceptable use case since, a) this should never be compiled in a production kernel, b) we are in a pretty bad, unpredictable state if we get here and keep going. I think you might have said elsewhere that you think "a" is not valid? In any case, I can replace this with a WARN, would that be acceptable?
A WARN may or may not make sense, depending on the context. It may be sufficient to simply report a test failure (as in the old version of case (2) below.
Answers to "a)" and "b)":
a) it might be in a production kernel
a') it is not acceptable in my development kernel either
b) No. You don't crash a developer's kernel either unless it is required to avoid data corruption.
b') And you can not do replacements like:
(1) in of_unittest_check_tree_linkage()
----- old -----
if (!of_root)
return;----- new -----
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, of_root);
(2) in of_unittest_property_string()
----- old -----
/* of_property_read_string_index() tests */
rc = of_property_read_string_index(np, "string-property", 0, strings);
unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);----- new -----
/* of_property_read_string_index() tests */
rc = of_property_read_string_index(np, "string-property", 0, strings);
KUNIT_ASSERT_EQ(test, rc, 0);
KUNIT_EXPECT_STREQ(test, strings[0], "foobar");If a test fails, that is no reason to abort testing. The remainder of the unit tests can still run. There may be cascading failures, but that is ok.
Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoted 102 lines · Frank Rowand
Quoted 91 lines · Brendan Higgins
Quoted 73 lines · Frank Rowand
Quoted 20 lines · Brendan Higgins
Add support for aborting/bailing out of test cases. Needed for implementing assertions.
Signed-off-by Brendan Higgins <[email protected]>---
Changes Since Last Version
- This patch is new introducing a new cross-architecture way to abort out of a test case (needed for KUNIT_ASSERT_*, see next patch for details).
- On a side note, this is not a complete replacement for the UML abort mechanism, but covers the majority of necessary functionality. UML architecture specific featurs have been dropped from the initial patchset.--- include/kunit/test.h | 24 +++++ kunit/Makefile | 3 +- kunit/test-test.c | 127 ++++++++++++++++++++++++++ kunit/test.c | 208 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 353 insertions(+), 9 deletions(-) create mode 100644 kunit/test-test.c< snip >
Quoted 48 lines
kunit/test.c +23
-2@@ -6,9 +6,9 @@* Author: Brendan Higgins <[email protected]> */ -#include <linux/sched.h> #include <linux/sched/debug.h> -#include <os.h> +#include <linux/completion.h> +#include <linux/kthread.h> #include <kunit/test.h> static bool kunit_get_success (struct kunit *test)@@ -32,6 +32,27 @@ static void kunit_set_success(struct kunit *test, bool success)spin_unlock_irqrestore (&test->lock, flags); } +static bool kunit_get_death_test (struct kunit *test) +{ +unsigned long flags; +bool death_test; + +spin_lock_irqsave (&test->lock, flags); + death_test = test->death_test; +spin_unlock_irqrestore (&test->lock, flags); + +return death_test; +} + +static void kunit_set_death_test (struct kunit *test,bool death_test) +{ +unsigned long flags; + +spin_lock_irqsave (&test->lock, flags); + test->death_test = death_test; +spin_unlock_irqrestore (&test->lock, flags); +} + static int kunit_vprintk_emit(const struct kunit *test, int level, const char *fmt,@@ -70,13 +91,29 @@ static void kunit_fail(struct kunit *test, struct kunit_stream *stream) stream->commit (stream); }Quoted 14 lines
kunit/test.c static void kunit_fail(struct kunit *test, struct kunit_stream *stream) · line 105
Quoted 12 lines
kunit/test.c +12
-0+static void __noreturn kunit_abort (struct kunit *test) +{ +kunit_set_death_test (test, true); + + test->try_catch.throw (&test->try_catch); + +/* +* Throw could not abort from test. +*/ +kunit_err (test,"Throw could not abort from test!" ); +show_stack (NULL ,NULL ); +BUG ();
kunit_abort()is what will be call as the result of an assert failure.Yep. Does that need clarified somewhere.
Quoted 1 line
Quoted 8 lines
Quoted 2 lines
BUG(), which is a panic, which is crashing the system is not acceptable in the Linux kernel. You will just annoy Linus if you submit this.Sorry, I thought this was an acceptable use case since, a) this should never be compiled in a production kernel, b) we are in a pretty bad, unpredictable state if we get here and keep going. I think you might have said elsewhere that you think "a" is not valid? In any case, I can replace this with a WARN, would that be acceptable?
Quoted 7 lines
A WARN may or may not make sense, depending on the context. It may be sufficient to simply report a test failure (as in the old version of case (2) below.
Answers to "a)" and "b)":
a) it might be in a production kernel
Sorry for a possibly stupid question, how might it be so? Why would someone intentionally build unit tests into a production kernel?
Quoted 2 lines
a') it is not acceptable in my development kernel either
Fair enough.
Quoted 3 lines
b) No. You don't crash a developer's kernel either unless it is required to avoid data corruption.
Alright, I thought that was one of those cases, but I am not going to push the point. Also, in case it wasn't clear, the path where BUG() gets called only happens if there is a bug in KUnit itself, not just because a test case fails catastrophically.
Quoted 24 lines
b') And you can not do replacements like:
(1) in
of_unittest_check_tree_linkage()----- old -----
if (!of_root) return;----- new -----
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, of_root);(2) in
of_unittest_property_string()----- old -----
/* of_property_read_string_index() tests */ rc = of_property_read_string_index(np, "string-property", 0, strings); unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);----- new -----
Quoted 8 lines
/* of_property_read_string_index() tests */ rc = of_property_read_string_index(np, "string-property", 0, strings); KUNIT_ASSERT_EQ(test, rc, 0); KUNIT_EXPECT_STREQ(test, strings[0], "foobar");If a test fails, that is no reason to abort testing. The remainder of the unit tests can still run. There may be cascading failures, but that is ok.
Sure, that's what I am trying to do. I don't see how (1) changes anything, a failed KUNIT_ASSERT_* only bails on the current test case, it does not quit the entire test suite let alone crash the kernel.
In case it wasn't clear above,
Quoted 1 line
Quoted 1 line
Quoted 1 line
kunit/test.c static void kunit_fail(struct kunit *test, struct kunit_stream *stream) · line 98
Quoted 1 line
kunit/test.c +1
-0+ test->try_catch.throw (&test->try_catch);
should never, ever return. The only time it would, would be if KUnit was very broken. This should never actually happen, even if the assertion that called it was violated. KUNIT_ASSERT_* just says, "this is a prerequisite property for this test case"; if it is violated, the test case should fail and bail because the preconditions for the test case cannot be satisfied. Nevertheless, other tests cases will still run.
Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoted 107 lines · Brendan Higgins
Quoted 106 lines · Frank Rowand
Quoted 91 lines · Brendan Higgins
Quoted 73 lines · Frank Rowand
Quoted 20 lines · Brendan Higgins
Add support for aborting/bailing out of test cases. Needed for implementing assertions.
Signed-off-by Brendan Higgins <[email protected]>---
Changes Since Last Version
- This patch is new introducing a new cross-architecture way to abort out of a test case (needed for KUNIT_ASSERT_*, see next patch for details).
- On a side note, this is not a complete replacement for the UML abort mechanism, but covers the majority of necessary functionality. UML architecture specific featurs have been dropped from the initial patchset.--- include/kunit/test.h | 24 +++++ kunit/Makefile | 3 +- kunit/test-test.c | 127 ++++++++++++++++++++++++++ kunit/test.c | 208 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 353 insertions(+), 9 deletions(-) create mode 100644 kunit/test-test.c< snip >
Quoted 48 lines
kunit/test.c +23
-2@@ -6,9 +6,9 @@* Author: Brendan Higgins <[email protected]> */ -#include <linux/sched.h> #include <linux/sched/debug.h> -#include <os.h> +#include <linux/completion.h> +#include <linux/kthread.h> #include <kunit/test.h> static bool kunit_get_success (struct kunit *test)@@ -32,6 +32,27 @@ static void kunit_set_success(struct kunit *test, bool success)spin_unlock_irqrestore (&test->lock, flags); } +static bool kunit_get_death_test (struct kunit *test) +{ +unsigned long flags; +bool death_test; + +spin_lock_irqsave (&test->lock, flags); + death_test = test->death_test; +spin_unlock_irqrestore (&test->lock, flags); + +return death_test; +} + +static void kunit_set_death_test (struct kunit *test,bool death_test) +{ +unsigned long flags; + +spin_lock_irqsave (&test->lock, flags); + test->death_test = death_test; +spin_unlock_irqrestore (&test->lock, flags); +} + static int kunit_vprintk_emit(const struct kunit *test, int level, const char *fmt,@@ -70,13 +91,29 @@ static void kunit_fail(struct kunit *test, struct kunit_stream *stream) stream->commit (stream); }Quoted 14 lines
kunit/test.c static void kunit_fail(struct kunit *test, struct kunit_stream *stream) · line 105
Quoted 12 lines
kunit/test.c +12
-0+static void __noreturn kunit_abort (struct kunit *test) +{ +kunit_set_death_test (test, true); + + test->try_catch.throw (&test->try_catch); + +/* +* Throw could not abort from test. +*/ +kunit_err (test,"Throw could not abort from test!" ); +show_stack (NULL ,NULL ); +BUG ();
kunit_abort()is what will be call as the result of an assert failure.Yep. Does that need clarified somewhere.
Quoted 1 line
Quoted 8 lines
Quoted 2 lines
BUG(), which is a panic, which is crashing the system is not acceptable in the Linux kernel. You will just annoy Linus if you submit this.Sorry, I thought this was an acceptable use case since, a) this should never be compiled in a production kernel, b) we are in a pretty bad, unpredictable state if we get here and keep going. I think you might have said elsewhere that you think "a" is not valid? In any case, I can replace this with a WARN, would that be acceptable?
A WARN may or may not make sense, depending on the context. It may be sufficient to simply report a test failure (as in the old version of case (2) below.
Quoted 6 lines
Quoted 3 lines
Answers to "a)" and "b)":
a) it might be in a production kernel
Sorry for a possibly stupid question, how might it be so? Why would someone intentionally build unit tests into a production kernel?
People do things. Just expect it.
Quoted 6 lines
Quoted 2 lines
a') it is not acceptable in my development kernel either
Fair enough.
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
b) No. You don't crash a developer's kernel either unless it is required to avoid data corruption.
Alright, I thought that was one of those cases, but I am not going to push the point. Also, in case it wasn't clear, the path where
BUG()gets called only happens if there is a bug in KUnit itself, not just because a test case fails catastrophically.
Still not out of the woods. Still facing Lions and Tigers and Bears, Oh my!
So kunit_abort() is normally called as the result of an assert failure (as written many lines further above).
kunit_abort()
test->try_catch.throw(&test->try_catch)
// this is really kunit_generic_throw(), yes?
complete_and_exit()
if (comp)
// comp is test_case_completion?
complete(comp)
do_exit()
// void __noreturn do_exit(long code)
// depending on the task, either panic
// or the task diesI did not read through enough of the code to understand what is going on here. Is each kunit_module executed in a newly created thread? And if kunit_abort() is called then that thread dies? Or something else?
Quoted 30 lines
Quoted 30 lines
b') And you can not do replacements like:
(1) in
of_unittest_check_tree_linkage()----- old -----
if (!of_root) return;----- new -----
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, of_root);(2) in
of_unittest_property_string()----- old -----
/* of_property_read_string_index() tests */ rc = of_property_read_string_index(np, "string-property", 0, strings); unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);----- new -----
/* of_property_read_string_index() tests */ rc = of_property_read_string_index(np, "string-property", 0, strings); KUNIT_ASSERT_EQ(test, rc, 0); KUNIT_EXPECT_STREQ(test, strings[0], "foobar");
Quoted 6 lines
Quoted 2 lines
If a test fails, that is no reason to abort testing. The remainder of the unit tests can still run. There may be cascading failures, but that is ok.
Sure, that's what I am trying to do. I don't see how (1) changes anything, a failed KUNIT_ASSERT_* only bails on the current test case, it does not quit the entire test suite let alone crash the kernel.
This may be another case of whether a kunit_module is approximately a single KUNIT_EXPECT_*() or a larger number of them.
I still want, for example, of_unittest_property_string() to include a large number of KUNIT_EXPECT_*() instances. In that case I still want the rest of the tests in the kunit_module to be executed even after a KUNIT_ASSERT_*() fails. The existing test code has that property.
Quoted 11 lines
In case it wasn't clear above,
Quoted 1 line
Quoted 1 line
Quoted 1 line
kunit/test.c static void kunit_fail(struct kunit *test, struct kunit_stream *stream) · line 98
Quoted 1 line
kunit/test.c +1
-0+ test->try_catch.throw (&test->try_catch);should never, ever return. The only time it would, would be if KUnit was very broken. This should never actually happen, even if the assertion that called it was violated. KUNIT_ASSERT_* just says, "this is a prerequisite property for this test case"; if it is violated, the test case should fail and bail because the preconditions for the test case cannot be satisfied. Nevertheless, other tests cases will still run.
Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoted 109 lines · Frank Rowand
Quoted 107 lines · Brendan Higgins
Quoted 106 lines · Frank Rowand
Quoted 91 lines · Brendan Higgins
Quoted 73 lines · Frank Rowand
Quoted 20 lines · Brendan Higgins
Add support for aborting/bailing out of test cases. Needed for implementing assertions.
Signed-off-by Brendan Higgins <[email protected]>---
Changes Since Last Version
- This patch is new introducing a new cross-architecture way to abort out of a test case (needed for KUNIT_ASSERT_*, see next patch for details).
- On a side note, this is not a complete replacement for the UML abort mechanism, but covers the majority of necessary functionality. UML architecture specific featurs have been dropped from the initial patchset.--- include/kunit/test.h | 24 +++++ kunit/Makefile | 3 +- kunit/test-test.c | 127 ++++++++++++++++++++++++++ kunit/test.c | 208 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 353 insertions(+), 9 deletions(-) create mode 100644 kunit/test-test.c< snip >
Quoted 48 lines
kunit/test.c +23
-2@@ -6,9 +6,9 @@* Author: Brendan Higgins <[email protected]> */ -#include <linux/sched.h> #include <linux/sched/debug.h> -#include <os.h> +#include <linux/completion.h> +#include <linux/kthread.h> #include <kunit/test.h> static bool kunit_get_success (struct kunit *test)@@ -32,6 +32,27 @@ static void kunit_set_success(struct kunit *test, bool success)spin_unlock_irqrestore (&test->lock, flags); } +static bool kunit_get_death_test (struct kunit *test) +{ +unsigned long flags; +bool death_test; + +spin_lock_irqsave (&test->lock, flags); + death_test = test->death_test; +spin_unlock_irqrestore (&test->lock, flags); + +return death_test; +} + +static void kunit_set_death_test (struct kunit *test,bool death_test) +{ +unsigned long flags; + +spin_lock_irqsave (&test->lock, flags); + test->death_test = death_test; +spin_unlock_irqrestore (&test->lock, flags); +} + static int kunit_vprintk_emit(const struct kunit *test, int level, const char *fmt,@@ -70,13 +91,29 @@ static void kunit_fail(struct kunit *test, struct kunit_stream *stream) stream->commit (stream); }Quoted 14 lines
kunit/test.c static void kunit_fail(struct kunit *test, struct kunit_stream *stream) · line 105
Quoted 12 lines
kunit/test.c +12
-0+static void __noreturn kunit_abort (struct kunit *test) +{ +kunit_set_death_test (test, true); + + test->try_catch.throw (&test->try_catch); + +/* +* Throw could not abort from test. +*/ +kunit_err (test,"Throw could not abort from test!" ); +show_stack (NULL ,NULL ); +BUG ();
kunit_abort()is what will be call as the result of an assert failure.Yep. Does that need clarified somewhere.
Quoted 1 line
Quoted 8 lines
Quoted 2 lines
BUG(), which is a panic, which is crashing the system is not acceptable in the Linux kernel. You will just annoy Linus if you submit this.Sorry, I thought this was an acceptable use case since, a) this should never be compiled in a production kernel, b) we are in a pretty bad, unpredictable state if we get here and keep going. I think you might have said elsewhere that you think "a" is not valid? In any case, I can replace this with a WARN, would that be acceptable?
A WARN may or may not make sense, depending on the context. It may be sufficient to simply report a test failure (as in the old version of case (2) below.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Answers to "a)" and "b)":
a) it might be in a production kernel
Sorry for a possibly stupid question, how might it be so? Why would someone intentionally build unit tests into a production kernel?
People do things. Just expect it.
Huh, alright. I will take your word for it then.
Quoted 10 lines
Quoted 10 lines
Quoted 3 lines
a') it is not acceptable in my development kernel either
Fair enough.
Quoted 3 lines
b) No. You don't crash a developer's kernel either unless it is required to avoid data corruption.
Quoted 7 lines
Quoted 4 lines
Alright, I thought that was one of those cases, but I am not going to push the point. Also, in case it wasn't clear, the path where
BUG()gets called only happens if there is a bug in KUnit itself, not just because a test case fails catastrophically.Still not out of the woods. Still facing Lions and Tigers and Bears, Oh my!
Nope, I guess not :-)
Quoted 7 lines
So
kunit_abort()is normally called as the result of an assert failure (as written many lines further above).
kunit_abort()
test->try_catch.throw(&test->try_catch)
// this is reallykunit_generic_throw(), yes?
Quoted 8 lines
complete_and_exit()
if (comp)
// comp is test_case_completion?
complete(comp)
do_exit()
// void __noreturn do_exit(long code)
// depending on the task, either panic
// or the task diesYou are right up until after it calls do_exit().
KUnit actually spawns a thread for the test case to run in so that when exit is called, only the test case thread dies. The thread that started KUnit is never affected.
Quoted 5 lines
I did not read through enough of the code to understand what is going on here. Is each
kunit_moduleexecuted in a newly created thread? And ifkunit_abort()is called then that thread dies? Or something else?
Mostly right, each kunit_case (not kunit_module) gets executed in its own newly created thread. If kunit_abort() is called in that thread, the kunit_case thread dies. The parent thread keeps going, and other test cases are executed.
Quoted 39 lines
Quoted 32 lines
Quoted 32 lines
b') And you can not do replacements like:
(1) in
of_unittest_check_tree_linkage()----- old -----
if (!of_root) return;----- new -----
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, of_root);(2) in
of_unittest_property_string()----- old -----
/* of_property_read_string_index() tests */ rc = of_property_read_string_index(np, "string-property", 0, strings); unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);----- new -----
/* of_property_read_string_index() tests */ rc = of_property_read_string_index(np, "string-property", 0, strings); KUNIT_ASSERT_EQ(test, rc, 0); KUNIT_EXPECT_STREQ(test, strings[0], "foobar");Quoted 6 lines
Quoted 2 lines
If a test fails, that is no reason to abort testing. The remainder of the unit tests can still run. There may be cascading failures, but that is ok.
Sure, that's what I am trying to do. I don't see how (1) changes anything, a failed KUNIT_ASSERT_* only bails on the current test case, it does not quit the entire test suite let alone crash the kernel.
Quoted 7 lines
This may be another case of whether a
kunit_moduleis approximately a single KUNIT_EXPECT_*() or a larger number of them.I still want, for example,
of_unittest_property_string()to include a large number of KUNIT_EXPECT_*() instances. In that case I still want the rest of the tests in thekunit_moduleto be executed even after a KUNIT_ASSERT_*() fails. The existing test code has that property.
Sure, in the context of the reply you just sent me on the DT unittest thread, that makes sense. I can pull out all but the ones that would have terminated the collection of test cases (where you return early), if that makes it better.
Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoted 121 lines · Brendan Higgins
Quoted 120 lines · Frank Rowand
Quoted 119 lines · Brendan Higgins
Quoted 117 lines · Frank Rowand
Quoted 104 lines · Brendan Higgins
Quoted 81 lines · Frank Rowand
Quoted 26 lines · Brendan Higgins
Add support for aborting/bailing out of test cases. Needed for implementing assertions.
Signed-off-by Brendan Higgins <[email protected]>---
Changes Since Last Version
- This patch is new introducing a new cross-architecture way to
abort
out of a test case (needed for KUNIT_ASSERT_*, see next patch
for
details).
- On a side note, this is not a complete replacement for the UML
abort
mechanism, but covers the majority of necessary functionality.
UML
architecture specific featurs have been dropped from the
initial
patchset.--- include/kunit/test.h | 24 +++++ kunit/Makefile | 3 +- kunit/test-test.c | 127 ++++++++++++++++++++++++++ kunit/test.c | 208 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 353 insertions(+), 9 deletions(-) create mode 100644 kunit/test-test.c< snip >
Quoted 51 lines
kunit/test.c +23
-2@@ -6,9 +6,9 @@* Author: Brendan Higgins <[email protected]> */ -#include <linux/sched.h> #include <linux/sched/debug.h> -#include <os.h> +#include <linux/completion.h> +#include <linux/kthread.h> #include <kunit/test.h> static bool kunit_get_success (struct kunit *test)@@ -32,6 +32,27 @@ static void kunit_set_success(struct kunit *test, bool success)spin_unlock_irqrestore (&test->lock, flags); } +static bool kunit_get_death_test (struct kunit *test) +{ +unsigned long flags; +bool death_test; + +spin_lock_irqsave (&test->lock, flags); + death_test = test->death_test; +spin_unlock_irqrestore (&test->lock, flags); + +return death_test; +} + +static void kunit_set_death_test (struct kunit *test,bool death_test) +{ +unsigned long flags; + +spin_lock_irqsave (&test->lock, flags); + test->death_test = death_test; +spin_unlock_irqrestore (&test->lock, flags); +} + static int kunit_vprintk_emit(const struct kunit *test,int level,
const char *fmt,kunit/test.c +0
-0@@ -70,13 +91,29 @@ static void kunit_fail(struct kunit *test, struct kunit_stream *stream) stream->commit(stream); }Quoted 15 lines
kunit/test.c static void kunit_fail(struct kunit *test, · line 106
Quoted 12 lines
kunit/test.c +12
-0+static void __noreturn kunit_abort (struct kunit *test) +{ +kunit_set_death_test (test, true); + + test->try_catch.throw (&test->try_catch); + +/* +* Throw could not abort from test. +*/ +kunit_err (test,"Throw could not abort from test!" ); +show_stack (NULL ,NULL ); +BUG ();
kunit_abort()is what will be call as the result of an assert failure.Yep. Does that need clarified somewhere.
Quoted 3 lines
BUG(), which is a panic, which is crashing the system is not acceptable
in the Linux kernel. You will just annoy Linus if you submit this.Quoted 5 lines
Sorry, I thought this was an acceptable use case since, a) this should never be compiled in a production kernel, b) we are in a pretty bad, unpredictable state if we get here and keep going. I think you might have said elsewhere that you think "a" is not valid? In any case, I can replace this with a WARN, would that be acceptable?
A WARN may or may not make sense, depending on the context. It may be sufficient to simply report a test failure (as in the old version of case (2) below.
Answers to "a)" and "b)":
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
a) it might be in a production kernel
Sorry for a possibly stupid question, how might it be so? Why would someone intentionally build unit tests into a production kernel?
People do things. Just expect it.
Huh, alright. I will take your word for it then.
I have a better explanation: Production kernels have bugs, unfortunately. And sometimes those need to be investigated on systems than cannot be brought down or affected more than absolutely necessary, maybe via a third party doing the execution. A light weight, precise test (well tested ahead :) ) might be a way of proving or disproving assumptions that can lead to the development and application of a fix.
IMHO you're confusing "building into" with temporary applying, then removing again - like the difference between running a local user space program vs installing it under /usr and have it in everyone's PATH.
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
a') it is not acceptable in my development kernel either
I think one of the fundamental properties of a good test framework is that it should not require changes to the code under test by itself.
Knut
Quoted 98 lines
Quoted 11 lines
Quoted 8 lines
Fair enough.
Quoted 2 lines
b) No. You don't crash a developer's kernel either unless it is required to avoid data corruption.
Alright, I thought that was one of those cases, but I am not going to push the point. Also, in case it wasn't clear, the path where
BUG()gets called only happens if there is a bug in KUnit itself, not just because a test case fails catastrophically.Still not out of the woods. Still facing Lions and Tigers and Bears, Oh my!
Nope, I guess not :-)
Quoted 6 lines
So
kunit_abort()is normally called as the result of an assert failure (as written many lines further above).
kunit_abort()
test->try_catch.throw(&test->try_catch)
// this is reallykunit_generic_throw(), yes?Quoted 8 lines
complete_and_exit() if (comp) // comp is test_case_completion? complete(comp) do_exit() // void __noreturn do_exit(long code) // depending on the task, either panic // or the task diesYou are right up until after it calls
do_exit().KUnit actually spawns a thread for the test case to run in so that when exit is called, only the test case thread dies. The thread that started KUnit is never affected.
Quoted 4 lines
I did not read through enough of the code to understand what is going on here. Is each
kunit_moduleexecuted in a newly created thread? And ifkunit_abort()is called then that thread dies? Or something else?Mostly right, each
kunit_case(notkunit_module) gets executed in its own newly created thread. Ifkunit_abort()is called in that thread, thekunit_casethread dies. The parent thread keeps going, and other test cases are executed.Quoted 41 lines
Quoted 33 lines
Quoted 33 lines
b') And you can not do replacements like:
(1) in
of_unittest_check_tree_linkage()----- old -----
if (!of_root) return;----- new -----
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, of_root);(2) in
of_unittest_property_string()----- old -----
/*
of_property_read_string_index()tests */
rc = of_property_read_string_index(np, "string-property", 0,strings); unittest(rc == 0 && !strcmp(strings[0], "foobar"),"
of_property_read_string_index()failure; rc=%i\n", rc);----- new -----
/*
of_property_read_string_index()tests */
rc = of_property_read_string_index(np, "string-property", 0,strings); KUNIT_ASSERT_EQ(test, rc, 0); KUNIT_EXPECT_STREQ(test, strings[0], "foobar");Quoted 7 lines
Quoted 3 lines
If a test fails, that is no reason to abort testing. The remainder of the unit
tests can still run. There may be cascading failures, but that is ok.Sure, that's what I am trying to do. I don't see how (1) changes anything, a failed KUNIT_ASSERT_* only bails on the current test case, it does not quit the entire test suite let alone crash the kernel.
Quoted 7 lines
This may be another case of whether a
kunit_moduleis approximately a single KUNIT_EXPECT_*() or a larger number of them.I still want, for example,
of_unittest_property_string()to include a large number of KUNIT_EXPECT_*() instances. In that case I still want the rest of the tests in thekunit_moduleto be executed even after a KUNIT_ASSERT_*() fails. The existing test code has that property.Sure, in the context of the reply you just sent me on the DT unittest thread, that makes sense. I can pull out all but the ones that would have terminated the collection of test cases (where you return early), if that makes it better.
Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoted 10 lines · Knut Omang
Quoted 8 lines · Brendan Higgins
Quoted 7 lines · Frank Rowand
Quoted 6 lines · Brendan Higgins
Quoted 4 lines · Frank Rowand
Quoted 3 lines · Brendan Higgins
Quoted 1 line · Frank Rowand
On 2/14/19 1:37 PM, Brendan Higgins wrote:
< snip >
Quoted 27 lines
Quoted 27 lines
Quoted 26 lines
Quoted 18 lines
Quoted 18 lines
Quoted 8 lines
Quoted 2 lines
kunit_abort()is what will be call as the result of an assert failure.Yep. Does that need clarified somewhere.
Quoted 3 lines
BUG(), which is a panic, which is crashing the system is not acceptable
in the Linux kernel. You will just annoy Linus if you submit this.Quoted 5 lines
Sorry, I thought this was an acceptable use case since, a) this should never be compiled in a production kernel, b) we are in a pretty bad, unpredictable state if we get here and keep going. I think you might have said elsewhere that you think "a" is not valid? In any case, I can replace this with a WARN, would that be acceptable?
A WARN may or may not make sense, depending on the context. It may be sufficient to simply report a test failure (as in the old version of case (2) below.
Quoted 6 lines
Quoted 3 lines
Answers to "a)" and "b)":
a) it might be in a production kernel
Sorry for a possibly stupid question, how might it be so? Why would someone intentionally build unit tests into a production kernel?
People do things. Just expect it.
Quoted 8 lines
Quoted 1 line
Huh, alright. I will take your word for it then.
I have a better explanation: Production kernels have bugs, unfortunately. And sometimes those need to be investigated on systems than cannot be brought down or affected more than absolutely necessary, maybe via a third party doing the execution. A light weight, precise test (well tested ahead :) ) might be a way of proving or disproving assumptions that can lead to the development and application of a fix.
Sorry, you are not suggesting testing in production are you? To be clear, I am not concerned about someone using testing, KUnit, or whatever in a *production-like* environment: that's not what we are talking about here. My assumption is that no one will deploy tests into actual production.
Quoted 4 lines
IMHO you're confusing "building into" with temporary applying, then removing again - like the difference between running a local user space program vs installing it under /usr and have it in everyone's PATH.
I don't really see the point of distinguishing between "building into" and "temporary applying" in this case; that's part of my point. Maybe it makes sense in whitebox end-to-end testing, but in the case of unit testing, I don't think so.
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
a') it is not acceptable in my development kernel either
I think one of the fundamental properties of a good test framework is that it should not require changes to the code under test by itself.
Sure, but that has nothing to do with the environment the code/tests are running in.
< snip >
Cheers
Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoted 47 lines · Brendan Higgins
Quoted 9 lines · Knut Omang
Quoted 8 lines · Brendan Higgins
Quoted 7 lines · Frank Rowand
Quoted 6 lines · Brendan Higgins
Quoted 4 lines · Frank Rowand
Quoted 3 lines · Brendan Higgins
Quoted 1 line · Frank Rowand
On 2/14/19 1:37 PM, Brendan Higgins wrote:
< snip >
Quoted 27 lines
Quoted 27 lines
Quoted 26 lines
Quoted 18 lines
Quoted 18 lines
Quoted 8 lines
Quoted 2 lines
kunit_abort()is what will be call as the result of an assert failure.Yep. Does that need clarified somewhere.
Quoted 3 lines
BUG(), which is a panic, which is crashing the system is not acceptable
in the Linux kernel. You will just annoy Linus if you submit this.Quoted 5 lines
Sorry, I thought this was an acceptable use case since, a) this should never be compiled in a production kernel, b) we are in a pretty bad, unpredictable state if we get here and keep going. I think you might have said elsewhere that you think "a" is not valid? In any case, I can replace this with a WARN, would that be acceptable?
A WARN may or may not make sense, depending on the context. It may be sufficient to simply report a test failure (as in the old version of case (2) below.
Quoted 6 lines
Quoted 3 lines
Answers to "a)" and "b)":
a) it might be in a production kernel
Sorry for a possibly stupid question, how might it be so? Why would someone intentionally build unit tests into a production kernel?
People do things. Just expect it.
Quoted 8 lines
Quoted 1 line
Huh, alright. I will take your word for it then.
I have a better explanation: Production kernels have bugs, unfortunately. And sometimes those need to be investigated on systems than cannot be brought down or affected more than absolutely necessary, maybe via a third party doing the execution. A light weight, precise test (well tested ahead :) ) might be a way of proving or disproving assumptions that can lead to the development and application of a fix.
Quoted 5 lines
Sorry, you are not suggesting testing in production are you? To be clear, I am not concerned about someone using testing, KUnit, or whatever in a *production-like* environment: that's not what we are talking about here. My assumption is that no one will deploy tests into actual production.
And my take is that you should not make such assumptions. Even the cost of bringing down a "production-like" environment can be significant, and the test infrastructure shouldn't think of itself as important enough to justify doing such things.
Quoted 9 lines
Quoted 3 lines
IMHO you're confusing "building into" with temporary applying, then removing again - like the difference between running a local user space program vs installing it under /usr and have it in everyone's PATH.
I don't really see the point of distinguishing between "building into" and "temporary applying" in this case; that's part of my point. Maybe it makes sense in whitebox end-to-end testing, but in the case of unit testing, I don't think so.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
a') it is not acceptable in my development kernel either
I think one of the fundamental properties of a good test framework is that it should not require changes to the code under test by itself.
Sure, but that has nothing to do with the environment the code/tests are running in.
Well, just that if the tests require a special environment to run, you limit the usability of the tests in detecting or ruling out real issues.
Thanks,
Knut
Quoted 4 lines
< snip >
Cheers
Re: [RFC v4 08/17] kunit: test: add support for test abort
In reply to Brendan HigginsQuoting Brendan Higgins (2019-02-14 13:37:20)
Quoted 2 lines
Add support for aborting/bailing out of test cases. Needed for implementing assertions.
Can you add some more text here with the motivating reasons for implementing assertions and bailing out of test cases?
For example, I wonder why unit tests can't just return with a failure when they need to abort and then the test runner would detect that error via the return value from the 'run test' function. That would be a more direct approach, but also more verbose than a single KUNIT_ASSERT() line. It would be more kernel idiomatic too because the control flow isn't hidden inside a macro and it isn't intimately connected with kthreads and completions.
Quoted 2 lines
Signed-off-by Brendan Higgins <[email protected]>
[...]
kunit/test-test.c
Quoted 3 lines
kunit/test-test.c new file +0
-0
Could this whole file be another patch? It seems to be a test for the try catch mechanism.
Quoted 4 lines
kunit/test.c +0
-0
[...]
Quoted 4 lines
kunit/test.c +4
-0+ +static void kunit_generic_throw (struct kunit_try_catch *try_catch) +{ + try_catch->context.try_result = -EFAULT ;
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 190
Quoted 10 lines
kunit/test.c +9
-0+complete_and_exit (try_catch->context.try_completion, -EFAULT ); +} + +static int kunit_generic_run_threadfn_adapter (void *data) +{ +struct kunit_try_catch *try_catch = data; + try_catch->try (&try_catch->context); + +complete_and_exit (try_catch->context.try_completion,0 );
The exit code doesn't matter, right? If so, it might be clearer to just return 0 from this function because kthreads exit themselves as far as I recall.
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 198
Quoted 8 lines
kunit/test.c +8
-0+} + +static void kunit_generic_run_try_catch (struct kunit_try_catch *try_catch) +{ +struct task_struct *task_struct; +struct kunit *test = try_catch->context.test; +int exit_code, wake_result; +DECLARE_COMPLETION (test_case_completion);
DECLARE_COMPLETION_ONSTACK()?
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 205
Quoted 7 lines
kunit/test.c +7
-0+ + try_catch->context.try_completion = &test_case_completion; + try_catch->context.try_result =0 ; + task_struct =kthread_create (kunit_generic_run_threadfn_adapter, + try_catch, +"kunit_try_catch_thread" ); +if (IS_ERR_OR_NULL (task_struct)) {
It looks like NULL is never returned from kthread_create(), so don't check for it here.
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 211
Quoted 6 lines
kunit/test.c +6
-0+ try_catch->catch (&try_catch->context); +return ; + } + + wake_result =wake_up_process (task_struct); +if (wake_result !=0 && wake_result !=1 ) {
These are the only two possible return values of wake_up_process(), so why not just use kthread_run() and check for an error on the process creation?
Quoted 11 lines
kunit/test.c +11
-0+kunit_err (test,"task was not woken properly: %d" , wake_result); + try_catch->catch (&try_catch->context); + } + +/* +* TODO([email protected]): We should probably have some type of +* timeout here. The only question is what that timeout value should be. +* +* The intention has always been, at some point, to be able to label +* tests with some type of size bucket (unit/small, integration/medium, +* large/system/end-to-end, etc), where each size bucket would get a
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 232
Quoted 10 lines
kunit/test.c +10
-0+* default timeout value kind of like what Bazel does: +* https://docs.bazel.build/versions/master/be/common-definitions.html#test.size +* There is still some debate to be had on exactly how we do this. (For +* one, we probably want to have some sort of test runner level +* timeout.) +* +* For more background on this topic, see: +* https://mike-bland.com/2011/11/01/small-medium-large.html +*/ +wait_for_completion (&test_case_completion);
It doesn't seem like a bad idea to make this have some arbitrarily large timeout value to start with. Just to make sure the whole thing doesn't get wedged. It's a timeout, so in theory it should never trigger if it's large enough.
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 238
Quoted 6 lines
kunit/test.c +6
-0+ + exit_code = try_catch->context.try_result; +if (exit_code == -EFAULT ) + try_catch->catch (&try_catch->context); +else if (exit_code == -EINTR ) +kunit_err (test,"wake_up_process() was never called." );
Does kunit_err() add newlines? It would be good to stick to the rest of kernel style (printk, tracing, etc.) and rely on the callers to have the newlines they want. Also, remove the full-stop because it isn't really necessary to have those in error logs.
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 245
Quoted 7 lines
kunit/test.c +7
-0+else if (exit_code) +kunit_err (test,"Unknown error: %d" , exit_code); +} + +void kunit_generic_try_catch_init (struct kunit_try_catch *try_catch) +{ + try_catch->run = kunit_generic_run_try_catch;
Is the idea that 'run' would be anything besides 'kunit_generic_run_try_catch'? If it isn't going to be different, then maybe it's simpler to just have a function like kunit_generic_run_try_catch() that is called by the unit tests instead of having to write 'try_catch->run(try_catch)' and indirect for the basic case. Maybe I've missed the point entirely though and this is all scaffolding for more complicated exception handling later on.
Quoted 8 lines
kunit/test.c +8
-0+ try_catch->throw = kunit_generic_throw; +} + +void __weak kunit_try_catch_init (struct kunit_try_catch *try_catch) +{ +kunit_generic_try_catch_init (try_catch); +} +
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 263
Quoted 10 lines
kunit/test.c +10
-0+static void kunit_try_run_case (struct kunit_try_catch_context *context) +{ +struct kunit_try_catch_context *ctx = context; +struct kunit *test = ctx->test; +struct kunit_module *module = ctx->module; +struct kunit_case *test_case = ctx->test_case; + +/* +* kunit_run_case_internal may encounter a fatal error; if it does, we +* will jump to ENTER_HANDLER above instead of continuing normal control
Where is 'ENTER_HANDLER' above?
Quoted 6 lines
kunit/test.c +4
-0+* flow. +*/ kunit_run_case_internal (test, module, test_case); +/* This line may never be reached. */ kunit_run_case_cleanup (test, module, test_case); +}
Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoted 7 lines · Stephen Boyd
Quoting Brendan Higgins (2019-02-14 13:37:20)
Quoted 2 lines
Add support for aborting/bailing out of test cases. Needed for implementing assertions.
Can you add some more text here with the motivating reasons for implementing assertions and bailing out of test cases?
Sure. Yeah, this comes before the commit that adds assertions, so I should probably put a better explanation here.
Quoted 2 lines
For example, I wonder why unit tests can't just return with a failure
Well, you could. You can just do the check as you would without KUnit, except call KUNIT_FAIL(...) before you return. For example, instead of:
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
you could do:
if (IS_ERR_OR_NULL(ptr)) {
KUNIT_FAIL(test, "ptr is an errno or null: %ld", ptr);
return;
}Quoted 4 lines
when they need to abort and then the test runner would detect that error via the return value from the 'run test' function. That would be a more direct approach, but also more verbose than a single
KUNIT_ASSERT()line. It would be more kernel idiomatic too because the control flow
Yeah, I was intentionally going against that idiom. I think that idiom makes test logic more complicated than it needs to be, especially if the assertion failure happens in a helper function; then you have to pass that error all the way back up. It is important that test code should be as simple as possible to the point of being immediately obviously correct at first glance because there are no tests for tests.
The idea with assertions is that you use them to state all the preconditions for your test. Logically speaking, these are the premises of the test case, so if a premise isn't true, there is no point in continuing the test case because there are no conclusions that can be drawn without the premises. Whereas, the expectation is the thing you are trying to prove. It is not used universally in x-unit style test frameworks, but I really like it as a convention. You could still express the idea of a premise using the above idiom, but I think KUNIT_ASSERT_* states the intended idea perfectly.
Quoted 2 lines
isn't hidden inside a macro and it isn't intimately connected with kthreads and completions.
Yeah, I wasn't a fan of that myself, but it was broadly available. My previous version (still the architecture specific version for UML, not in this patchset though) relies on UML_LONGJMP, but is obviously only works on UML. A number of people wanted support for other architectures. Rob and Luis specifically wanted me to provide a version of abort that would work on any architecture, even if it only had reduced functionality; I thought this fit the bill okay.
Quoted 10 lines
Quoted 3 lines
Signed-off-by Brendan Higgins <[email protected]>[...]
kunit/test-test.c
Quoted 3 lines
kunit/test-test.c new file +0
-0Could this whole file be another patch? It seems to be a test for the try catch mechanism.
Sure.
Quoted 17 lines
Quoted 5 lines
kunit/test.c +0
-0[...]
Quoted 11 lines
kunit/test.c +10
-0+ +static void kunit_generic_throw (struct kunit_try_catch *try_catch) +{ + try_catch->context.try_result = -EFAULT ; +complete_and_exit (try_catch->context.try_completion, -EFAULT ); +} + +static int kunit_generic_run_threadfn_adapter (void *data) +{ +struct kunit_try_catch *try_catch = data;
Quoted 7 lines
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 190
Quoted 3 lines
kunit/test.c +3
-0+ try_catch->try (&try_catch->context); + +complete_and_exit (try_catch->context.try_completion,0 );The exit code doesn't matter, right? If so, it might be clearer to just return 0 from this function because kthreads exit themselves as far as I recall.
You mean complete and then return?
Quoted 11 lines
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 198
Quoted 9 lines
kunit/test.c +8
-0+} + +static void kunit_generic_run_try_catch (struct kunit_try_catch *try_catch) +{ +struct task_struct *task_struct; +struct kunit *test = try_catch->context.test; +int exit_code, wake_result; +DECLARE_COMPLETION (test_case_completion);
DECLARE_COMPLETION_ONSTACK()?
Whoops, yeah, that one.
Quoted 11 lines
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 205
Quoted 8 lines
kunit/test.c +7
-0+ + try_catch->context.try_completion = &test_case_completion; + try_catch->context.try_result =0 ; + task_struct =kthread_create (kunit_generic_run_threadfn_adapter, + try_catch, +"kunit_try_catch_thread" ); +if (IS_ERR_OR_NULL (task_struct)) {It looks like NULL is never returned from
kthread_create(), so don't check for it here.
Bad habit, sorry.
Quoted 11 lines
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 211
Quoted 7 lines
kunit/test.c +6
-0+ try_catch->catch (&try_catch->context); +return ; + } + + wake_result =wake_up_process (task_struct); +if (wake_result !=0 && wake_result !=1 ) {These are the only two possible return values of
wake_up_process(), so why not just usekthread_run()and check for an error on the process creation?
Good point, I am not sure why I did that.
Quoted 23 lines
Quoted 12 lines
kunit/test.c +11
-0+kunit_err (test,"task was not woken properly: %d" , wake_result); + try_catch->catch (&try_catch->context); + } + +/* +* TODO([email protected]): We should probably have some type of +* timeout here. The only question is what that timeout value should be. +* +* The intention has always been, at some point, to be able to label +* tests with some type of size bucket (unit/small, integration/medium, +* large/system/end-to-end, etc), where each size bucket would get a kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 232
Quoted 10 lines
kunit/test.c +10
-0+* default timeout value kind of like what Bazel does: +* https://docs.bazel.build/versions/master/be/common-definitions.html#test.size +* There is still some debate to be had on exactly how we do this. (For +* one, we probably want to have some sort of test runner level +* timeout.) +* +* For more background on this topic, see: +* https://mike-bland.com/2011/11/01/small-medium-large.html +*/ +wait_for_completion (&test_case_completion);
Quoted 4 lines
It doesn't seem like a bad idea to make this have some arbitrarily large timeout value to start with. Just to make sure the whole thing doesn't get wedged. It's a timeout, so in theory it should never trigger if it's large enough.
Seems reasonable.
Quoted 8 lines
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 238
Quoted 7 lines
kunit/test.c +6
-0+ + exit_code = try_catch->context.try_result; +if (exit_code == -EFAULT ) + try_catch->catch (&try_catch->context); +else if (exit_code == -EINTR ) +kunit_err (test,"wake_up_process() was never called." );
Quoted 4 lines
Does
kunit_err()add newlines? It would be good to stick to the rest of kernel style (printk, tracing, etc.) and rely on the callers to have the newlines they want. Also, remove the full-stop because it isn't really necessary to have those in error logs.
Will do.
Quoted 11 lines
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 245
Quoted 8 lines
kunit/test.c +7
-0+else if (exit_code) +kunit_err (test,"Unknown error: %d" , exit_code); +} + +void kunit_generic_try_catch_init (struct kunit_try_catch *try_catch) +{ + try_catch->run = kunit_generic_run_try_catch;Is the idea that 'run' would be anything besides '
kunit_generic_run_try_catch'? If it isn't going to be different, then
Yeah, it can be overridden with an architecture specific version.
Quoted 5 lines
maybe it's simpler to just have a function like
kunit_generic_run_try_catch()that is called by the unit tests instead of having to write 'try_catch->run(try_catch)' and indirect for the basic case. Maybe I've missed the point entirely though and this is all scaffolding for more complicated exception handling later on.
Yeah, the idea is that different architectures can override exception handling with their own implementation. This is just the generic one. For example, UML has one that doesn't depend on kthreads or completions; the UML version also allows recovery from some segfault conditions.
Quoted 13 lines
Quoted 13 lines
kunit/test.c +12
-0+ try_catch->throw = kunit_generic_throw; +} + +void __weak kunit_try_catch_init (struct kunit_try_catch *try_catch) +{ +kunit_generic_try_catch_init (try_catch); +} + +static void kunit_try_run_case (struct kunit_try_catch_context *context) +{ +struct kunit_try_catch_context *ctx = context; +struct kunit *test = ctx->test;
Quoted 8 lines
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 263
Quoted 6 lines
kunit/test.c +6
-0+struct kunit_module *module = ctx->module; +struct kunit_case *test_case = ctx->test_case; + +/* +* kunit_run_case_internal may encounter a fatal error; if it does, we +* will jump to ENTER_HANDLER above instead of continuing normal control Where is '
ENTER_HANDLER' above?
Whoops, sorry, that is left over from v3. Will remove.
Quoted 7 lines
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 269
Quoted 7 lines
kunit/test.c +4
-0+* flow. +*/ kunit_run_case_internal (test, module, test_case); +/* This line may never be reached. */ kunit_run_case_cleanup (test, module, test_case); +}
Thanks!
Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoted 6 lines · Brendan Higgins
you could do:
if (IS_ERR_OR_NULL(ptr)) { KUNIT_FAIL(test, "ptr is an errno or null: %ld", ptr); return; }
It's best to not mix error pointers and NULL but when we do mix them, it means that NULL is a special kind of success. Like we try to load a feature and we get back:
valid pointer <-- success
null <-- feature is disabled. not an error.
error pointer <-- feature is broken. fail.
regards,
dan carpenter
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoted 9 lines · Dan Carpenter
Quoted 6 lines · Brendan Higgins
you could do:
if (IS_ERR_OR_NULL(ptr)) { KUNIT_FAIL(test, "ptr is an errno or null: %ld", ptr); return; }
Quoted 7 lines
It's best to not mix error pointers and NULL but when we do mix them, it means that NULL is a special kind of success. Like we try to load a feature and we get back:
valid pointer <-- success
null <-- feature is disabled. not an error.
error pointer <-- feature is broken. fail.
Thanks for pointing that out! Will fix.
A message that is not in the archive (sent off-list or not yet received): CAFd5g46QCisA4PHQXo57-LC6sNdk=OUjBE0O1s4005uBz_Vo6Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoting Brendan Higgins (2019-02-28 01:03:24)
Quoted 15 lines · Stephen Boyd
Quoted 5 lines
when they need to abort and then the test runner would detect that error via the return value from the 'run test' function. That would be a more direct approach, but also more verbose than a single
KUNIT_ASSERT()line. It would be more kernel idiomatic too because the control flowYeah, I was intentionally going against that idiom. I think that idiom makes test logic more complicated than it needs to be, especially if the assertion failure happens in a helper function; then you have to pass that error all the way back up. It is important that test code should be as simple as possible to the point of being immediately obviously correct at first glance because there are no tests for tests.
Quoted 9 lines
The idea with assertions is that you use them to state all the preconditions for your test. Logically speaking, these are the premises of the test case, so if a premise isn't true, there is no point in continuing the test case because there are no conclusions that can be drawn without the premises. Whereas, the expectation is the thing you are trying to prove. It is not used universally in x-unit style test frameworks, but I really like it as a convention. You could still express the idea of a premise using the above idiom, but I think KUNIT_ASSERT_* states the intended idea perfectly.
Fair enough. It would be great if these sorts of things were described in the commit text.
Is the assumption that things like held locks and refcounted elements won't exist when one of these assertions is made? It sounds like some of the cleanup logic could be fairly complicated if a helper function changes some state and then an assert fails and we have to unwind all the state from a corrupt location. A similar problem exists for a test timeout too. How do we get back to a sane state if the test locks up for a long time? Just don't try?
Quoted 11 lines
Quoted 3 lines
isn't hidden inside a macro and it isn't intimately connected with kthreads and completions.
Yeah, I wasn't a fan of that myself, but it was broadly available. My previous version (still the architecture specific version for UML, not in this patchset though) relies on
UML_LONGJMP, but is obviously only works on UML. A number of people wanted support for other architectures. Rob and Luis specifically wanted me to provide a version of abort that would work on any architecture, even if it only had reduced functionality; I thought this fit the bill okay.
Ok.
Quoted 27 lines
Quoted 19 lines
Quoted 7 lines
kunit/test.c +0
-0[...]
Quoted 11 lines
kunit/test.c +10
-0+ +static void kunit_generic_throw (struct kunit_try_catch *try_catch) +{ + try_catch->context.try_result = -EFAULT ; +complete_and_exit (try_catch->context.try_completion, -EFAULT ); +} + +static int kunit_generic_run_threadfn_adapter (void *data) +{ +struct kunit_try_catch *try_catch = data;Quoted 7 lines
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 190
Quoted 3 lines
kunit/test.c +3
-0+ try_catch->try (&try_catch->context); + +complete_and_exit (try_catch->context.try_completion,0 );The exit code doesn't matter, right? If so, it might be clearer to just return 0 from this function because kthreads exit themselves as far as I recall.
Quoted 1 line
You mean complete and then return?
Yes. I was confused for a minute because I thought the exit code was checked, but it isn't. Instead, the try_catch->context.try_result is where the test result goes, so calling exit explicitly doesn't seem to be important here, but it is important in the throw case.
Quoted 21 lines
Quoted 12 lines
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 245
Quoted 9 lines
kunit/test.c +7
-0+else if (exit_code) +kunit_err (test,"Unknown error: %d" , exit_code); +} + +void kunit_generic_try_catch_init (struct kunit_try_catch *try_catch) +{ + try_catch->run = kunit_generic_run_try_catch;Is the idea that 'run' would be anything besides '
kunit_generic_run_try_catch'? If it isn't going to be different, thenYeah, it can be overridden with an architecture specific version.
Quoted 5 lines
maybe it's simpler to just have a function like
kunit_generic_run_try_catch()that is called by the unit tests instead of having to write 'try_catch->run(try_catch)' and indirect for the basic case. Maybe I've missed the point entirely though and this is all scaffolding for more complicated exception handling later on.
Quoted 5 lines
Yeah, the idea is that different architectures can override exception handling with their own implementation. This is just the generic one. For example, UML has one that doesn't depend on kthreads or completions; the UML version also allows recovery from some segfault conditions.
Ok, got it. It may still be nice to have a wrapper or macro for that try_catch->run(try_catch) statement so we don't have to know that a try_catch struct has a run member.
static inline void kunit_run_try_catch(struct kunit_try_catch *try_catch)
{
try_catch->run(try_catch);
}Re: [RFC v4 08/17] kunit: test: add support for test abort
Quoted 27 lines · Stephen Boyd
Quoting Brendan Higgins (2019-02-28 01:03:24)
Quoted 15 lines · Stephen Boyd
Quoted 5 lines
when they need to abort and then the test runner would detect that error via the return value from the 'run test' function. That would be a more direct approach, but also more verbose than a single
KUNIT_ASSERT()line. It would be more kernel idiomatic too because the control flowYeah, I was intentionally going against that idiom. I think that idiom makes test logic more complicated than it needs to be, especially if the assertion failure happens in a helper function; then you have to pass that error all the way back up. It is important that test code should be as simple as possible to the point of being immediately obviously correct at first glance because there are no tests for tests.
Quoted 9 lines
The idea with assertions is that you use them to state all the preconditions for your test. Logically speaking, these are the premises of the test case, so if a premise isn't true, there is no point in continuing the test case because there are no conclusions that can be drawn without the premises. Whereas, the expectation is the thing you are trying to prove. It is not used universally in x-unit style test frameworks, but I really like it as a convention. You could still express the idea of a premise using the above idiom, but I think KUNIT_ASSERT_* states the intended idea perfectly.
Quoted 2 lines
Fair enough. It would be great if these sorts of things were described in the commit text.
Good point. Will fix.
Quoted 8 lines
Is the assumption that things like held locks and refcounted elements won't exist when one of these assertions is made? It sounds like some of the cleanup logic could be fairly complicated if a helper function changes some state and then an assert fails and we have to unwind all the state from a corrupt location. A similar problem exists for a test timeout too. How do we get back to a sane state if the test locks up for a long time? Just don't try?
It depends on the situation, if it is part of a KUnit test itself (probably not code under test), then you can use the kunit_resource
API: https://lkml.org/lkml/2019/2/14/1125; it is inspired by the devm_* family of functions, such that when a KUnit test case ends, for any reason, all the kunit_resources are automatically cleaned up. Similarly, kunit_module.exit is called at the end of every test case, regardless of how it terminates.
Quoted 42 lines
Quoted 5 lines
Quoted 4 lines
isn't hidden inside a macro and it isn't intimately connected with kthreads and completions.
Quoted 7 lines
Yeah, I wasn't a fan of that myself, but it was broadly available. My previous version (still the architecture specific version for UML, not in this patchset though) relies on
UML_LONGJMP, but is obviously only works on UML. A number of people wanted support for other architectures. Rob and Luis specifically wanted me to provide a version of abort that would work on any architecture, even if it only had reduced functionality; I thought this fit the bill okay.Ok.
Quoted 27 lines
Quoted 26 lines
Quoted 7 lines
kunit/test.c +0
-0[...]
Quoted 4 lines
kunit/test.c +4
-0+ +static void kunit_generic_throw (struct kunit_try_catch *try_catch) +{ + try_catch->context.try_result = -EFAULT ;kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 190
Quoted 10 lines
kunit/test.c +9
-0+complete_and_exit (try_catch->context.try_completion, -EFAULT ); +} + +static int kunit_generic_run_threadfn_adapter (void *data) +{ +struct kunit_try_catch *try_catch = data; + try_catch->try (&try_catch->context); + +complete_and_exit (try_catch->context.try_completion,0 );The exit code doesn't matter, right? If so, it might be clearer to just return 0 from this function because kthreads exit themselves as far as I recall.
Quoted 6 lines
Quoted 1 line
You mean complete and then return?
Yes. I was confused for a minute because I thought the exit code was checked, but it isn't. Instead, the
try_catch->context.try_resultis where the test result goes, so calling exit explicitly doesn't seem to be important here, but it is important in the throw case.
Yep.
Quoted 28 lines
Quoted 22 lines
Quoted 13 lines
kunit/test.c static void kunit_run_case_cleanup(struct kunit *test, · line 245
Quoted 10 lines
kunit/test.c +7
-0+else if (exit_code) +kunit_err (test,"Unknown error: %d" , exit_code); +} + +void kunit_generic_try_catch_init (struct kunit_try_catch *try_catch) +{ + try_catch->run = kunit_generic_run_try_catch;Is the idea that 'run' would be anything besides '
kunit_generic_run_try_catch'? If it isn't going to be different, thenYeah, it can be overridden with an architecture specific version.
Quoted 5 lines
maybe it's simpler to just have a function like
kunit_generic_run_try_catch()that is called by the unit tests instead of having to write 'try_catch->run(try_catch)' and indirect for the basic case. Maybe I've missed the point entirely though and this is all scaffolding for more complicated exception handling later on.Quoted 5 lines
Yeah, the idea is that different architectures can override exception handling with their own implementation. This is just the generic one. For example, UML has one that doesn't depend on kthreads or completions; the UML version also allows recovery from some segfault conditions.
Quoted 8 lines
Ok, got it. It may still be nice to have a wrapper or macro for that
try_catch->run(try_catch)statement so we don't have to know that atry_catchstruct has a run member.static inline void kunit_run_try_catch(struct kunit_try_catch *try_catch) { try_catch->run(try_catch); }
Makes sense. Will fix in the next revision.
RFC v4 9/17 kunit: test: add the concept of assertions
In reply to Brendan HigginsAdd support for assertions which are like expectations except the test terminates if the assertion is not satisfied.
--- include/kunit/test.h | 397 ++++++++++++++++++++++++++++++++++++- kunit/string-stream-test.c | 12 +- kunit/test-test.c | 2 + kunit/test.c | 33 +++ 4 files changed, 435 insertions(+), 9 deletions(-)
include/kunit/test.h +394 -3
@@ -84,9 +84,10 @@ struct kunit;* @name: the name of the test case. * * A test case is a function with the signature, ``void (*)(struct kunit *)`` -* that makes expectations (see KUNIT_EXPECT_TRUE()) about code under test. Each -* test case is associated with a &struct kunit_module and will be run after the -* module's init function and followed by the module's exit function. +* that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and +* KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated with +* a &struct kunit_module and will be run after the module's init function and +* followed by the module's exit function. * * A test case should be static and should only be created with the KUNIT_CASE() * macro; additionally, every array of test cases should be terminated with an
@@ -712,4 +713,394 @@ static inline void kunit_expect_binary(struct kunit *test,KUNIT_EXPECT_END (test, !IS_ERR_OR_NULL (__ptr), __stream); \ } while (0) +static inline struct kunit_stream *kunit_assert_start(struct kunit *test, +const char *file, +const char *line) +{ +struct kunit_stream *stream =kunit_new_stream (test); + + stream->add (stream,"ASSERTION FAILED at %s:%s\n\t" , file, line); + +return stream; +} + +static inline void kunit_assert_end (struct kunit *test, +bool success, +struct kunit_stream *stream) +{ +if (!success) { + test->fail (test, stream); + test->abort (test); + }else { + stream->clear (stream); + } +} + +#define KUNIT_ASSERT_START (test) \ + kunit_assert_start(test, __FILE__, __stringify(__LINE__)) + +#define KUNIT_ASSERT_END (test, success, stream) \ + kunit_assert_end(test, success, stream) + +#define KUNIT_ASSERT (test, success, message) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + \ + __stream->add(__stream, message); \ + KUNIT_ASSERT_END(test, success, __stream); \ +} while (0) + +#define KUNIT_ASSERT_MSG (test, success, message, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + \ + __stream->add(__stream, message); \ + __stream->add(__stream, fmt, ##__VA_ARGS__); \ + KUNIT_ASSERT_END(test, success, __stream); \ +} while (0) + +#define KUNIT_ASSERT_FAILURE (test, fmt, ...) \ + KUNIT_ASSERT_MSG(test, false, "", fmt, ##__VA_ARGS__) + +/** +* KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true. +* @test: The test context object. +* @condition: an arbitrary boolean expression. The test fails and aborts when +* this does not evaluate to true. +* +* This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to +* fail *and immediately abort* when the specified condition is not met. Unlike +* an expectation failure, it will prevent the test case from continuing to run; +* this is otherwise known as an *assertion failure*. +*/ +#define KUNIT_ASSERT_TRUE (test, condition) \ + KUNIT_ASSERT(test, (condition), \ + "Asserted " #condition " is true, but is false.") + +#define KUNIT_ASSERT_TRUE_MSG (test, condition, fmt, ...) \ + KUNIT_ASSERT_MSG(test, (condition), \ + "Asserted " #condition " is true, but is false.\n",\ + fmt, ##__VA_ARGS__) + +/** +* KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false. +* @test: The test context object. +* @condition: an arbitrary boolean expression. +* +* Sets an assertion that the value that @condition evaluates to is false. This +* is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure +* (see KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_FALSE (test, condition) \ + KUNIT_ASSERT(test, !(condition), \ + "Asserted " #condition " is false, but is true.") + +#define KUNIT_ASSERT_FALSE_MSG (test, condition, fmt, ...) \ + KUNIT_ASSERT_MSG(test, !(condition), \ + "Asserted " #condition " is false, but is true.\n",\ + fmt, ##__VA_ARGS__) + +void kunit_assert_binary_msg (struct kunit *test, +long long left,const char *left_name, +long long right,const char *right_name, +bool compare_result, +const char *compare_name, +const char *file, +const char *line, +const char *fmt, ...); + +static inline void kunit_assert_binary (struct kunit *test, +long long left,const char *left_name, +long long right,const char *right_name, +bool compare_result, +const char *compare_name, +const char *file, +const char *line) +{ +kunit_assert_binary_msg (test, + left, left_name, + right, right_name, + compare_result, + compare_name, + file, + line, +NULL ); +} + +/* +* A factory macro for defining the expectations for the basic comparisons +* defined for the built in types. +* +* Unfortunately, there is no common type that all types can be promoted to for +* which all the binary operators behave the same way as for the actual types +* (for example, there is no type that long long and unsigned long long can +* both be cast to where the comparison result is preserved for all values). So +* the best we can do is do the comparison in the original types and then coerce +* everything to long long for printing; this way, the comparison behaves +* correctly and the printed out value usually makes sense without +* interpretation, but can always be interpretted to figure out the actual +* value. +*/ +#define KUNIT_ASSERT_BINARY (test, left, condition, right) do { \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + kunit_assert_binary(test, \ + (long long) __left, #left, \ + (long long) __right, #right, \ + __left condition __right, #condition, \ + __FILE__, __stringify(__LINE__)); \ +} while (0) + +#define KUNIT_ASSERT_BINARY_MSG (test, left, condition, right, fmt, ...) do { \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + kunit_assert_binary_msg(test, \ + (long long) __left, #left, \ + (long long) __right, #right, \ + __left condition __right, #condition, \ + __FILE__, __stringify(__LINE__), \ + fmt, ##__VA_ARGS__); \ +} while (0) + +/** +* KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a primitive C type. +* @right: an arbitrary expression that evaluates to a primitive C type. +* +* Sets an assertion that the values that @left and @right evaluate to are +* equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion +* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_EQ (test, left, right) \ + KUNIT_ASSERT_BINARY(test, left, ==, right) + +#define KUNIT_ASSERT_EQ_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_BINARY_MSG(test, \ + left, \ + ==, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a primitive C type. +* @right: an arbitrary expression that evaluates to a primitive C type. +* +* Sets an assertion that the values that @left and @right evaluate to are not +* equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion +* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_NE (test, left, right) \ + KUNIT_ASSERT_BINARY(test, left, !=, right) + +#define KUNIT_ASSERT_NE_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_BINARY_MSG(test, \ + left, \ + !=, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_ASSERT_LT() - An assertion that @left is less than @right. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a primitive C type. +* @right: an arbitrary expression that evaluates to a primitive C type. +* +* Sets an assertion that the value that @left evaluates to is less than the +* value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except +* it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion +* is not met. +*/ +#define KUNIT_ASSERT_LT (test, left, right) \ + KUNIT_ASSERT_BINARY(test, left, <, right) + +#define KUNIT_ASSERT_LT_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_BINARY_MSG(test, \ + left, \ + <, \ + right, \ + fmt, \ + ##__VA_ARGS__) +/** +* KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a primitive C type. +* @right: an arbitrary expression that evaluates to a primitive C type. +* +* Sets an assertion that the value that @left evaluates to is less than or +* equal to the value that @right evaluates to. This is the same as +* KUNIT_EXPECT_LE(), except it causes an assertion failure (see +* KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_LE (test, left, right) \ + KUNIT_ASSERT_BINARY(test, left, <=, right) + +#define KUNIT_ASSERT_LE_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_BINARY_MSG(test, \ + left, \ + <=, \ + right, \ + fmt, \ + ##__VA_ARGS__) +/** +* KUNIT_ASSERT_GT() - An assertion that @left is greater than @right. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a primitive C type. +* @right: an arbitrary expression that evaluates to a primitive C type. +* +* Sets an assertion that the value that @left evaluates to is greater than the +* value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except +* it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion +* is not met. +*/ +#define KUNIT_ASSERT_GT (test, left, right) \ + KUNIT_ASSERT_BINARY(test, left, >, right) + +#define KUNIT_ASSERT_GT_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_BINARY_MSG(test, \ + left, \ + >, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a primitive C type. +* @right: an arbitrary expression that evaluates to a primitive C type. +* +* Sets an assertion that the value that @left evaluates to is greater than the +* value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except +* it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion +* is not met. +*/ +#define KUNIT_ASSERT_GE (test, left, right) \ + KUNIT_ASSERT_BINARY(test, left, >=, right) + +#define KUNIT_ASSERT_GE_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_BINARY_MSG(test, \ + left, \ + >=, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a null terminated string. +* @right: an arbitrary expression that evaluates to a null terminated string. +* +* Sets an assertion that the values that @left and @right evaluate to are +* equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an +* assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_STREQ (test, left, right) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + __stream->add(__stream, "Asserted " #left " == " #right ", but\n"); \ + __stream->add(__stream, "\t\t%s == %s\n", #left, __left); \ + __stream->add(__stream, "\t\t%s == %s\n", #right, __right); \ + \ + KUNIT_ASSERT_END(test, !strcmp(left, right), __stream); \ +} while (0) + +#define KUNIT_ASSERT_STREQ_MSG (test, left, right, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + __stream->add(__stream, "Asserted " #left " == " #right ", but\n"); \ + __stream->add(__stream, "\t\t%s == %s\n", #left, __left); \ + __stream->add(__stream, "\t\t%s == %s\n", #right, __right); \ + __stream->add(__stream, fmt, ##__VA_ARGS__); \ + \ + KUNIT_ASSERT_END(test, !strcmp(left, right), __stream); \ +} while (0) + +/** +* KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a null terminated string. +* @right: an arbitrary expression that evaluates to a null terminated string. +* +* Sets an expectation that the values that @left and @right evaluate to are +* not equal. This is semantically equivalent to +* KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE() +* for more information. +*/ +#define KUNIT_ASSERT_STRNEQ (test, left, right) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + __stream->add(__stream, "Asserted " #left " == " #right ", but\n"); \ + __stream->add(__stream, "\t\t%s == %s\n", #left, __left); \ + __stream->add(__stream, "\t\t%s == %s\n", #right, __right); \ + \ + KUNIT_ASSERT_END(test, strcmp(left, right), __stream); \ +} while (0) + +#define KUNIT_ASSERT_STRNEQ_MSG (test, left, right, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + __stream->add(__stream, "Asserted " #left " == " #right ", but\n"); \ + __stream->add(__stream, "\t\t%s == %s\n", #left, __left); \ + __stream->add(__stream, "\t\t%s == %s\n", #right, __right); \ + __stream->add(__stream, fmt, ##__VA_ARGS__); \ + \ + KUNIT_ASSERT_END(test, strcmp(left, right), __stream); \ +} while (0) + +/** +* KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err. +* @test: The test context object. +* @ptr: an arbitrary pointer. +* +* Sets an assertion that the value that @ptr evaluates to is not null and not +* an errno stored in a pointer. This is the same as +* KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see +* KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ptr) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + typeof(ptr) __ptr = (ptr); \ + \ + if (!__ptr) \ + __stream->add(__stream, \ + "Asserted " #ptr " is not null, but is."); \ + if (IS_ERR(__ptr)) \ + __stream->add(__stream, \ + "Asserted " #ptr " is not error, but is: %ld", \ + PTR_ERR(__ptr)); \ + \ + KUNIT_ASSERT_END(test, !IS_ERR_OR_NULL(__ptr), __stream); \ +} while (0) + +#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG (test, ptr, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + typeof(ptr) __ptr = (ptr); \ + \ + if (!__ptr) { \ + __stream->add(__stream, \ + "Asserted " #ptr " is not null, but is."); \ + __stream->add(__stream, fmt, ##__VA_ARGS__); \ + } \ + if (IS_ERR(__ptr)) { \ + __stream->add(__stream, \ + "Asserted " #ptr " is not error, but is: %ld", \ + PTR_ERR(__ptr)); \ + \ + __stream->add(__stream, fmt, ##__VA_ARGS__); \ + } \ + KUNIT_ASSERT_END(test, !IS_ERR_OR_NULL(__ptr), __stream); \ +} while (0) +#endif /* _KUNIT_TEST_H */
kunit/string-stream-test.c +6 -6
@@ -19,7 +19,7 @@ static void string_stream_test_get_string(struct kunit *test) stream->add (stream," %s" ,"bar" ); output = stream->get_string (stream); -KUNIT_EXPECT_STREQ (test, output,"Foo bar" ); +KUNIT_ASSERT_STREQ (test, output,"Foo bar" );kfree (output);destroy_string_stream (stream); }
@@ -34,16 +34,16 @@ static void string_stream_test_add_and_clear(struct kunit *test) stream->add (stream,"A" ); output = stream->get_string (stream); -KUNIT_EXPECT_STREQ (test, output,"AAAAAAAAAA" ); -KUNIT_EXPECT_EQ (test, stream->length,10 ); -KUNIT_EXPECT_FALSE (test, stream->is_empty (stream)); +KUNIT_ASSERT_STREQ (test, output,"AAAAAAAAAA" ); +KUNIT_ASSERT_EQ (test, stream->length,10 ); +KUNIT_ASSERT_FALSE (test, stream->is_empty (stream));kfree (output); stream->clear (stream); output = stream->get_string (stream); -KUNIT_EXPECT_STREQ (test, output,"" ); -KUNIT_EXPECT_TRUE (test, stream->is_empty (stream)); +KUNIT_ASSERT_STREQ (test, output,"" ); +KUNIT_ASSERT_TRUE (test, stream->is_empty (stream));destroy_string_stream (stream); }
kunit/test-test.c +2 -0
@@ -100,11 +100,13 @@ static int kunit_try_catch_test_init(struct kunit *test)struct kunit_try_catch_test_context *ctx; ctx =kunit_kzalloc (test,sizeof (*ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx); test->priv = ctx; ctx->try_catch =kunit_kmalloc (test,sizeof (*ctx->try_catch),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx->try_catch);kunit_try_catch_init (ctx->try_catch); ctx->try_catch->context.test = test;
kunit/test.c +33 -0
@@ -495,3 +495,36 @@ void kunit_expect_binary_msg(struct kunit *test,kunit_expect_end (test, compare_result, stream); } +void kunit_assert_binary_msg (struct kunit *test, +long long left,const char *left_name, +long long right,const char *right_name, +bool compare_result, +const char *compare_name, +const char *file, +const char *line, +const char *fmt, ...) +{ +struct kunit_stream *stream =kunit_assert_start (test, file, line); +struct va_format vaf; +va_list args; + + stream->add (stream, +"Asserted %s %s %s, but\n" , + left_name, compare_name, right_name); + stream->add (stream,"\t\t%s == %lld\n" , left_name, left); + stream->add (stream,"\t\t%s == %lld" , right_name, right); + +if (fmt) { +va_start (args, fmt); + + vaf.fmt = fmt; + vaf.va = &args; + + stream->add (stream,"\n%pV" , &vaf); + +va_end (args); + } + +kunit_assert_end (test, compare_result, stream); +} +
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
RFC v4 10/17 kunit: test: add test managed resource tests
In reply to Brendan HigginsTests how tests interact with test managed resources in their lifetime.
--- kunit/test-test.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+)
kunit/test-test.c +121 -0
@@ -127,3 +127,124 @@ static struct kunit_module kunit_try_catch_test_module = { .test_cases = kunit_try_catch_test_cases, };module_test (kunit_try_catch_test_module); + +/* +* Context for testing test managed resources +* is_resource_initialized is used to test arbitrary resources +*/ +struct kunit_test_resource_context { +struct kunit test; +bool is_resource_initialized; +}; + +static int fake_resource_init (struct kunit_resource *res,void *context) +{ +struct kunit_test_resource_context *ctx = context; + + res->allocation = &ctx->is_resource_initialized; + ctx->is_resource_initialized = true; +return 0 ; +} + +static void fake_resource_free (struct kunit_resource *res) +{ +bool *is_resource_initialized = res->allocation; + + *is_resource_initialized = false; +} + +static void kunit_resource_test_init_resources (struct kunit *test) +{ +struct kunit_test_resource_context *ctx = test->priv; + +kunit_init_test (&ctx->test,"testing_test_init_test" ); + +KUNIT_EXPECT_TRUE (test,list_empty (&ctx->test.resources)); +} + +static void kunit_resource_test_alloc_resource (struct kunit *test) +{ +struct kunit_test_resource_context *ctx = test->priv; +struct kunit_resource *res; +kunit_resource_free_t free = fake_resource_free; + + res =kunit_alloc_resource (&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, res); +KUNIT_EXPECT_EQ (test, &ctx->is_resource_initialized, res->allocation); +KUNIT_EXPECT_TRUE (test,list_is_last (&res->node, &ctx->test.resources)); +KUNIT_EXPECT_EQ (test, free, res->free); +} + +static void kunit_resource_test_free_resource (struct kunit *test) +{ +struct kunit_test_resource_context *ctx = test->priv; +struct kunit_resource *res =kunit_alloc_resource (&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + +kunit_free_resource (&ctx->test, res); + +KUNIT_EXPECT_EQ (test, false, ctx->is_resource_initialized); +KUNIT_EXPECT_TRUE (test,list_empty (&ctx->test.resources)); +} + +#define KUNIT_RESOURCE_NUM 5 +static void kunit_resource_test_cleanup_resources (struct kunit *test) +{ +int i; +struct kunit_test_resource_context *ctx = test->priv; +struct kunit_resource *resources[KUNIT_RESOURCE_NUM ]; + +for (i =0 ; i <KUNIT_RESOURCE_NUM ; i++) { + resources[i] =kunit_alloc_resource (&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + } + +kunit_cleanup (&ctx->test); + +KUNIT_EXPECT_TRUE (test,list_empty (&ctx->test.resources)); +} + +static int kunit_resource_test_init (struct kunit *test) +{ +struct kunit_test_resource_context *ctx = +kzalloc (sizeof (*ctx),GFP_KERNEL ); + +if (!ctx) +return -ENOMEM ; + test->priv = ctx; + +kunit_init_test (&ctx->test,"test_test_context" ); +return 0 ; +} + +static void kunit_resource_test_exit (struct kunit *test) +{ +struct kunit_test_resource_context *ctx = test->priv; + +kunit_cleanup (&ctx->test); +kfree (ctx); +} + +static struct kunit_case kunit_resource_test_cases[] = { +KUNIT_CASE (kunit_resource_test_init_resources), +KUNIT_CASE (kunit_resource_test_alloc_resource), +KUNIT_CASE (kunit_resource_test_free_resource), +KUNIT_CASE (kunit_resource_test_cleanup_resources), + {}, +}; + +static struct kunit_module kunit_resource_test_module = { + .name ="kunit-resource-test" , + .init = kunit_resource_test_init, + .exit = kunit_resource_test_exit, + .test_cases = kunit_resource_test_cases, +}; +module_test (kunit_resource_test_module);
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
Re: [RFC v4 10/17] kunit: test: add test managed resource tests
Quoting Brendan Higgins (2019-02-14 13:37:22)
Quoted 4 lines
kunit/test-test.c +0
-0
[...]
kunit/test-test.c static struct kunit_module kunit_try_catch_test_module = { · line 203
Quoted 9 lines
kunit/test-test.c +9
-0+ +#define KUNIT_RESOURCE_NUM 5 +static void kunit_resource_test_cleanup_resources (struct kunit *test) +{ +int i; +struct kunit_test_resource_context *ctx = test->priv; +struct kunit_resource *resources[KUNIT_RESOURCE_NUM ]; + +for (i =0 ; i <KUNIT_RESOURCE_NUM ; i++) {
Nitpick: This could use ARRAY_SIZE(resources) and then the #define could be dropped.
Quoted 11 lines
kunit/test-test.c +11
-0+ resources[i] =kunit_alloc_resource (&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + } + +kunit_cleanup (&ctx->test); + +KUNIT_EXPECT_TRUE (test,list_empty (&ctx->test.resources)); +} +
[...]
kunit/test-test.c
Quoted 2 lines
kunit/test-test.c +2
-0+ +static struct kunit_case kunit_resource_test_cases[] = {
Can these arrays be const?
Quoted 6 lines
kunit/test-test.c +6
-0+KUNIT_CASE (kunit_resource_test_init_resources), +KUNIT_CASE (kunit_resource_test_alloc_resource), +KUNIT_CASE (kunit_resource_test_free_resource), + KUNIT_CASE(kunit_resource_test_cleanup_resources), + {}, +};
Re: [RFC v4 10/17] kunit: test: add test managed resource tests
Quoted 7 lines · Stephen Boyd
Quoting Brendan Higgins (2019-02-14 13:37:22)
Quoted 4 lines
kunit/test-test.c +0
-0[...]
Quoted 12 lines
kunit/test-test.c static struct kunit_module kunit_try_catch_test_module = { · line 203
Quoted 9 lines
kunit/test-test.c +9
-0+ +#define KUNIT_RESOURCE_NUM 5 +static void kunit_resource_test_cleanup_resources (struct kunit *test) +{ +int i; +struct kunit_test_resource_context *ctx = test->priv; +struct kunit_resource *resources[KUNIT_RESOURCE_NUM ]; + +for (i =0 ; i <KUNIT_RESOURCE_NUM ; i++) {Nitpick: This could use
ARRAY_SIZE(resources)and then the #define could be dropped.
Noted. Will fix in next revision.
Quoted 13 lines
Quoted 12 lines
kunit/test-test.c +11
-0+ resources[i] =kunit_alloc_resource (&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + } + +kunit_cleanup (&ctx->test); + +KUNIT_EXPECT_TRUE (test,list_empty (&ctx->test.resources)); +} +[...]
Quoted 4 lines
kunit/test-test.c
Quoted 2 lines
kunit/test-test.c +2
-0+ +static struct kunit_case kunit_resource_test_cases[] = {Can these arrays be const?
There is some private mutable state inside of struct kunit_case that would be kind of annoying to pull out; I don't think it would make it cleaner.
Re: [RFC v4 10/17] kunit: test: add test managed resource tests
Quoting Brendan Higgins (2019-02-19 15:20:18)
Quoted 11 lines · Stephen Boyd
Quoted 6 lines
Quoting Brendan Higgins (2019-02-14 13:37:22)
Review
Quoted 2 lines
(quoted diff) +2
-0+ +static struct kunit_case kunit_resource_test_cases[] = {Can these arrays be const?
There is some private mutable state inside of
struct kunit_casethat would be kind of annoying to pull out; I don't think it would make it cleaner.
Fair enough. Thanks for checking.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
RFC v4 11/17 kunit: tool: add Python wrappers for running KUnit tests
In reply to Brendan HigginsFrom: Felix Guo <[email protected]>
The ultimate goal is to create minimal isolated test binaries; in the meantime we are using UML to provide the infrastructure to run tests, so define an abstract way to configure and run tests that allow us to change the context in which tests are built without affecting the user. This also makes pretty and dynamic error reporting, and a lot of other nice features easier.
kunit_config.py:
- parse .config and Kconfig files.
kunit_kernel.py: provides helper functions to:
- configure the kernel using kunitconfig.
- build the kernel with the appropriate configuration.
- provide function to invoke the kernel and stream the output back.
---
Changes Since Last Version
- Added support for building and running tests in an external directory.
- Squashed with most other kunit_tool commits, since most did not represent a coherent new feature.
--- tools/testing/kunit/.gitignore | 3 + tools/testing/kunit/kunit.py | 78 +++++++++++++++ tools/testing/kunit/kunit_config.py | 66 +++++++++++++ tools/testing/kunit/kunit_kernel.py | 148 ++++++++++++++++++++++++++++ tools/testing/kunit/kunit_parser.py | 119 ++++++++++++++++++++++ 5 files changed, 414 insertions(+) create mode 100644 tools/testing/kunit/.gitignore create mode 100755 tools/testing/kunit/kunit.py create mode 100644 tools/testing/kunit/kunit_config.py create mode 100644 tools/testing/kunit/kunit_kernel.py create mode 100644 tools/testing/kunit/kunit_parser.py
tools/testing/kunit/.gitignore new file +3 -0
@@ -0,0 +1,3 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod]
tools/testing/kunit/kunit.py new file +78 -0
78 lines not on this page · raw message
tools/testing/kunit/kunit_config.py new file +66 -0
66 lines not on this page · raw message
tools/testing/kunit/kunit_kernel.py new file +148 -0
148 lines not on this page · raw message
tools/testing/kunit/kunit_parser.py new file +119 -0
119 lines not on this page · raw message
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
RFC v4 12/17 kunit: defconfig: add defconfigs for building KUnit tests
In reply to Brendan HigginsAdd defconfig for UML and a fragment that can be used to configure other architectures for building KUnit tests. Add option to kunit_tool to use a defconfig to create the kunitconfig.
---
Changes Since Last Version
- This patch is new adding default configs to build KUnit.
- NOTE: there is still some discussion to be had here about whether we should go with a defconfig, a config fragment, or both.
--- arch/um/configs/kunit_defconfig | 8 ++++++++ tools/testing/kunit/configs/all_tests.config | 8 ++++++++ tools/testing/kunit/kunit.py | 17 +++++++++++++++-- tools/testing/kunit/kunit_kernel.py | 3 ++- 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 arch/um/configs/kunit_defconfig create mode 100644 tools/testing/kunit/configs/all_tests.config
arch/um/configs/kunit_defconfig new file +8 -0
@@ -0,0 +1,8 @@ +CONFIG_OF=y +CONFIG_OF_UNITTEST=y +CONFIG_OF_OVERLAY=y +CONFIG_I2C=y +CONFIG_I2C_MUX=y +CONFIG_KUNIT=y +CONFIG_KUNIT_TEST=y +CONFIG_KUNIT_EXAMPLE_TEST=y
tools/testing/kunit/configs/all_tests.config new file +8 -0
@@ -0,0 +1,8 @@ +CONFIG_OF=y +CONFIG_OF_UNITTEST=y +CONFIG_OF_OVERLAY=y +CONFIG_I2C=y +CONFIG_I2C_MUX=y +CONFIG_KUNIT=y +CONFIG_KUNIT_TEST=y +CONFIG_KUNIT_EXAMPLE_TEST=y
tools/testing/kunit/kunit.py +15 -2
@@ -11,6 +11,7 @@ import argparse import sys import os import time +import shutil import kunit_config import kunit_kernel
@@ -36,14 +37,26 @@ parser.add_argument('--build_dir', 'directory.', type=str, default=None, metavar='build_dir') -cli_args = parser.parse_args() +parser.add_argument('--defconfig', + help='Uses a default kunitconfig.', + action='store_true') -linux = kunit_kernel.LinuxSourceTree() +def create_default_kunitconfig(): + if not os.path.exists(kunit_kernel.KUNITCONFIG_PATH): + shutil.copyfile('arch/um/configs/kunit_defconfig', + kunit_kernel.KUNITCONFIG_PATH) + +cli_args = parser.parse_args() build_dir = None if cli_args.build_dir: build_dir = cli_args.build_dir +if cli_args.defconfig: + create_default_kunitconfig() + +linux = kunit_kernel.LinuxSourceTree() + config_start = time.time() success = linux.build_reconfig(build_dir) config_end = time.time()
tools/testing/kunit/kunit_kernel.py +2 -1
@@ -14,6 +14,7 @@ import os import kunit_config KCONFIG_PATH = '.config' +KUNITCONFIG_PATH = 'kunitconfig' class ConfigError(Exception): """Represents an error trying to configure the Linux kernel."""
@@ -81,7 +82,7 @@ class LinuxSourceTree(object): def __init__(self): self._kconfig = kunit_config.Kconfig() - self._kconfig.read_from_file('kunitconfig') + self._kconfig.read_from_file(KUNITCONFIG_PATH) self._ops = LinuxSourceTreeOperations() def clean(self):
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
RFC v4 13/17 Documentation: kunit: add documentation for KUnit
In reply to Brendan HigginsAdd documentation for KUnit, the Linux kernel unit testing framework.
- Add intro and usage guide for KUnit
- Add API reference
--- Documentation/index.rst | 1 + Documentation/kunit/api/index.rst | 16 ++ Documentation/kunit/api/test.rst | 15 + Documentation/kunit/faq.rst | 46 +++ Documentation/kunit/index.rst | 80 ++++++ Documentation/kunit/start.rst | 180 ++++++++++++ Documentation/kunit/usage.rst | 447 ++++++++++++++++++++++++++++++ 7 files changed, 785 insertions(+) create mode 100644 Documentation/kunit/api/index.rst create mode 100644 Documentation/kunit/api/test.rst create mode 100644 Documentation/kunit/faq.rst create mode 100644 Documentation/kunit/index.rst create mode 100644 Documentation/kunit/start.rst create mode 100644 Documentation/kunit/usage.rst
Documentation/index.rst +1 -0
@@ -65,6 +65,7 @@ merged much easier. kernel-hacking/index trace/index maintainer/index + kunit/index Kernel API documentation ------------------------
Documentation/kunit/api/index.rst new file +16 -0
@@ -0,0 +1,16 @@ +.. SPDX-License-Identifier: GPL-2.0 + +============= +API Reference +============= +.. toctree:: + + test + +This section documents the KUnit kernel testing API. It is divided into 3 +sections: + +================================= ============================================== +:doc:`test` documents all of the standard testing API + excluding mocking or mocking related features. +================================= ==============================================
Documentation/kunit/api/test.rst new file +15 -0
@@ -0,0 +1,15 @@ +.. SPDX-License-Identifier: GPL-2.0 + +======== +Test API +======== + +This file documents all of the standard testing API excluding mocking or mocking +related features. + +.. kernel-doc:: include/kunit/test.h + :internal: + +.. kernel-doc:: include/kunit/kunit-stream.h + :internal: +
Documentation/kunit/faq.rst new file +46 -0
46 lines not on this page · raw message
Documentation/kunit/index.rst new file +80 -0
80 lines not on this page · raw message
Documentation/kunit/start.rst new file +180 -0
180 lines not on this page · raw message
Documentation/kunit/usage.rst new file +447 -0
447 lines not on this page · raw message
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
RFC v4 14/17 MAINTAINERS: add entry for KUnit the unit testing framework
In reply to Brendan HigginsAdd myself as maintainer of KUnit, the Linux kernel's unit testing framework.
--- MAINTAINERS | 10 ++++++++++ 1 file changed, 10 insertions(+)
MAINTAINERS +10 -0
@@ -8267,6 +8267,16 @@ S: Maintained F: tools/testing/selftests/ F: Documentation/dev-tools/kselftest* +KERNEL UNIT TESTING FRAMEWORK (KUnit) +M: Brendan Higgins <[email protected]> +L: kunit-dev-/[email protected] +W: https://google.github.io/kunit-docs/third_party/kernel/docs/ +S: Maintained +F: Documentation/kunit/ +F: include/kunit/ +F: kunit/ +F: tools/testing/kunit/ + KERNEL USERMODE HELPER M: Luis Chamberlain <[email protected]> L: [email protected]
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
RFC v4 15/17 of: unittest: migrate tests to run on KUnit
In reply to Brendan HigginsMigrate tests without any cleanup, or modifying test logic in anyway to run under KUnit using the KUnit expectation and assertion API.
--- drivers/of/Kconfig | 1 + drivers/of/unittest.c | 1310 +++++++++++++++++++++-------------------- 2 files changed, 671 insertions(+), 640 deletions(-)
drivers/of/Kconfig +1 -0
@@ -15,6 +15,7 @@ if OF config OF_UNITTEST bool "Device Tree runtime unit tests" depends on !SPARC + depends on KUNIT select IRQ_DOMAIN select OF_EARLY_FLATTREE select OF_RESOLVE
drivers/of/unittest.c +670 -640
2,043 lines not on this page · raw message
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
Re: [RFC v4 15/17] of: unittest: migrate tests to run on KUnit
drivers/of/unittest.c
Quoted 22 lines · Brendan Higgins
Migrate tests without any cleanup, or modifying test logic in anyway to run under KUnit using the KUnit expectation and assertion API.
Signed-off-by Brendan Higgins <[email protected]>--- drivers/of/Kconfig | 1 + drivers/of/unittest.c | 1310 +++++++++++++++++++++-------------------- 2 files changed, 671 insertions(+), 640 deletions(-)drivers/of/Kconfig +1
-0@@ -15,6 +15,7 @@ if OF config OF_UNITTEST bool "Device Tree runtime unit tests" depends on !SPARC + depends on KUNIT select IRQ_DOMAIN select OF_EARLY_FLATTREE select OF_RESOLVEdrivers/of/unittest.c +0
-0
These comments are from applying the patches to 5.0-rc3.
The final hunk of this patch fails to apply because it depends upon
[PATCH v1 0/1] of: unittest: unflatten device tree on UML when testing.
If I apply that patch then I can apply patches 15 through 17.
If I apply patches 1 through 14 and boot the uml kernel then the devicetree unittest result is:
### dt-test ### FAIL of_unittest_overlay_high_level():2372 overlay_base_root not initialized ### dt-test ### end of unittest - 219 passed, 1 failed
This is as expected from your previous reports, and is fixed after applying
[PATCH v1 0/1] of: unittest: unflatten device tree on UML when testing.
with the devicetree unittest result of:
### dt-test ### end of unittest - 224 passed, 0 failed
After adding patch 15, there are a lot of "unittest internal error" messages.
-Frank
Quoted 2,081 lines
index
effa4e2b9d992..96de69ccb3e63100644drivers/of/unittest.c +670
-640@@ -26,186 +26,189 @@#include <linux/bitops.h> +#include <kunit/test.h>### dt-test ### end of unittest - 224 passed, 0 failed +#include "of_private.h" -static struct unittest_results { -int passed; -int failed; -} unittest_results; - -#define unittest (result, fmt, ...) ({ \ - bool failed = !(result); \ - if (failed) { \ - unittest_results.failed++; \ - pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \ - } else { \ - unittest_results.passed++; \ - pr_debug("pass %s():%i\n", __func__, __LINE__); \ - } \ - failed; \ -}) - -static void __init of_unittest_find_node_by_name (void ) +static void of_unittest_find_node_by_name (struct kunit *test) {struct device_node *np;const char *options, *name; np =of_find_node_by_path ("/testcase-data" ); name =kasprintf (GFP_KERNEL ,"%pOF" , np); -unittest (np && !strcmp ("/testcase-data" , name), -"find /testcase-data failed\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_STREQ_MSG (test,"/testcase-data" , name, +"find /testcase-data failed\n" );of_node_put (np);kfree (name);/* Test if trailing '/' works */ - np =of_find_node_by_path ("/testcase-data/" ); -unittest (!np,"trailing '/' on /testcase-data/ should fail\n" ); +KUNIT_EXPECT_EQ_MSG (test,of_find_node_by_path ("/testcase-data/" ),NULL , +"trailing '/' on /testcase-data/ should fail\n" ); np =of_find_node_by_path ("/testcase-data/phandle-tests/consumer-a" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); name =kasprintf (GFP_KERNEL ,"%pOF" , np); -unittest (np && !strcmp ("/testcase-data/phandle-tests/consumer-a" , name), +KUNIT_EXPECT_STREQ_MSG ( + test,"/testcase-data/phandle-tests/consumer-a" , name,"find /testcase-data/phandle-tests/consumer-a failed\n" );of_node_put (np);kfree (name); np =of_find_node_by_path ("testcase-alias" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); name =kasprintf (GFP_KERNEL ,"%pOF" , np); -unittest (np && !strcmp ("/testcase-data" , name), -"find testcase-alias failed\n" ); +KUNIT_EXPECT_STREQ_MSG (test,"/testcase-data" , name, +"find testcase-alias failed\n" );of_node_put (np);kfree (name);/* Test if trailing '/' works on aliases */ - np =of_find_node_by_path ("testcase-alias/" ); -unittest (!np,"trailing '/' on testcase-alias/ should fail\n" ); +KUNIT_EXPECT_EQ_MSG (test,of_find_node_by_path ("testcase-alias/" ),NULL , +"trailing '/' on testcase-alias/ should fail\n" ); np =of_find_node_by_path ("testcase-alias/phandle-tests/consumer-a" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); name =kasprintf (GFP_KERNEL ,"%pOF" , np); -unittest (np && !strcmp ("/testcase-data/phandle-tests/consumer-a" , name), +KUNIT_EXPECT_STREQ_MSG ( + test,"/testcase-data/phandle-tests/consumer-a" , name,"find testcase-alias/phandle-tests/consumer-a failed\n" );of_node_put (np);kfree (name); - np =of_find_node_by_path ("/testcase-data/missing-path" ); -unittest (!np,"non-existent path returned node %pOF\n" , np); +KUNIT_EXPECT_EQ_MSG ( + test, + np =of_find_node_by_path ("/testcase-data/missing-path" ),NULL , +"non-existent path returned node %pOF\n" , np);of_node_put (np); - np =of_find_node_by_path ("missing-alias" ); -unittest (!np,"non-existent alias returned node %pOF\n" , np); +KUNIT_EXPECT_EQ_MSG ( + test, np =of_find_node_by_path ("missing-alias" ),NULL , +"non-existent alias returned node %pOF\n" , np);of_node_put (np); - np =of_find_node_by_path ("testcase-alias/missing-path" ); -unittest (!np,"non-existent alias with relative path returned node %pOF\n" , np); +KUNIT_EXPECT_EQ_MSG ( + test, + np =of_find_node_by_path ("testcase-alias/missing-path" ),NULL , +"non-existent alias with relative path returned node %pOF\n" , + np);of_node_put (np); np =of_find_node_opts_by_path ("/testcase-data:testoption" , &options); -unittest (np && !strcmp ("testoption" , options), -"option path test failed\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_STREQ_MSG (test,"testoption" , options, +"option path test failed\n" );of_node_put (np); np =of_find_node_opts_by_path ("/testcase-data:test/option" , &options); -unittest (np && !strcmp ("test/option" , options), -"option path test, subcase #1 failed\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_STREQ_MSG (test,"test/option" , options, +"option path test, subcase #1 failed\n" );of_node_put (np); np =of_find_node_opts_by_path ("/testcase-data/testcase-device1:test/option" , &options); -unittest (np && !strcmp ("test/option" , options), -"option path test, subcase #2 failed\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_STREQ_MSG (test,"test/option" , options, +"option path test, subcase #2 failed\n" );of_node_put (np); np =of_find_node_opts_by_path ("/testcase-data:testoption" ,NULL ); -unittest (np,"NULL option path test failed\n" ); +KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG (test, np, +"NULL option path test failed\n" );of_node_put (np); np =of_find_node_opts_by_path ("testcase-alias:testaliasoption" , &options); -unittest (np && !strcmp ("testaliasoption" , options), -"option alias path test failed\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_STREQ_MSG (test,"testaliasoption" , options, +"option alias path test failed\n" );of_node_put (np); np =of_find_node_opts_by_path ("testcase-alias:test/alias/option" , &options); -unittest (np && !strcmp ("test/alias/option" , options), -"option alias path test, subcase #1 failed\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_STREQ_MSG (test,"test/alias/option" , options, +"option alias path test, subcase #1 failed\n" );of_node_put (np); np =of_find_node_opts_by_path ("testcase-alias:testaliasoption" ,NULL ); -unittest (np,"NULL option alias path test failed\n" ); +KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG ( + test, np,"NULL option alias path test failed\n" );of_node_put (np); options ="testoption" ; np =of_find_node_opts_by_path ("testcase-alias" , &options); -unittest (np && !options,"option clearing test failed\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_EQ_MSG (test, options,NULL , +"option clearing test failed\n" );of_node_put (np); options ="testoption" ; np =of_find_node_opts_by_path ("/" , &options); -unittest (np && !options,"option clearing root node test failed\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_EQ_MSG (test, options,NULL , +"option clearing root node test failed\n" );of_node_put (np); } -static void __init of_unittest_dynamic (void ) +static void of_unittest_dynamic (struct kunit *test) {struct device_node *np;struct property *prop; np =of_find_node_by_path ("/testcase-data" ); -if (!np) { -pr_err ("missing testcase data\n" ); -return ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np);/* Array of 4 properties for the purpose of testing */ prop =kcalloc (4 ,sizeof (*prop),GFP_KERNEL ); -if (!prop) { -unittest (0 ,"kzalloc() failed\n" ); -return ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, prop);/* Add a new property - should pass*/ prop->name ="new-property" ; prop->value ="new-property-data" ; prop->length =strlen (prop->value) +1 ; -unittest (of_add_property (np, prop) ==0 ,"Adding a new property failed\n" ); +KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop),0 , +"Adding a new property failed\n" );/* Try to add an existing property - should fail */ prop++; prop->name ="new-property" ; prop->value ="new-property-data-should-fail" ; prop->length =strlen (prop->value) +1 ; -unittest (of_add_property (np, prop) !=0 , -"Adding an existing property should have failed\n" ); +KUNIT_EXPECT_NE_MSG (test,of_add_property (np, prop),0 , +"Adding an existing property should have failed\n" );/* Try to modify an existing property - should pass */ prop->value ="modify-property-data-should-pass" ; prop->length =strlen (prop->value) +1 ; -unittest (of_update_property (np, prop) ==0 , -"Updating an existing property should have passed\n" ); +KUNIT_EXPECT_EQ_MSG ( + test,of_update_property (np, prop),0 , +"Updating an existing property should have passed\n" );/* Try to modify non-existent property - should pass*/ prop++; prop->name ="modify-property" ; prop->value ="modify-missing-property-data-should-pass" ; prop->length =strlen (prop->value) +1 ; -unittest (of_update_property (np, prop) ==0 , -"Updating a missing property should have passed\n" ); +KUNIT_EXPECT_EQ_MSG (test,of_update_property (np, prop),0 , +"Updating a missing property should have passed\n" );/* Remove property - should pass */ -unittest (of_remove_property (np, prop) ==0 , -"Removing a property should have passed\n" ); +KUNIT_EXPECT_EQ_MSG (test,of_remove_property (np, prop),0 , +"Removing a property should have passed\n" );/* Adding very large property - should pass */ prop++; prop->name ="large-property-PAGE_SIZEx8" ; prop->length =PAGE_SIZE *8 ; prop->value =kzalloc (prop->length,GFP_KERNEL ); -unittest (prop->value !=NULL ,"Unable to allocate large buffer\n" ); -if (prop->value) -unittest (of_add_property (np, prop) ==0 , -"Adding a large property should have passed\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, prop->value); +KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop),0 , +"Adding a large property should have passed\n" ); } -static int __init of_unittest_check_node_linkage (struct device_node *np) +static int of_unittest_check_node_linkage (struct device_node *np) {struct device_node *child;int count =0 , rc;@@ -230,27 +233,30 @@ static int __init of_unittest_check_node_linkage(struct device_node *np)return rc; } -static void __init of_unittest_check_tree_linkage (void ) +static void of_unittest_check_tree_linkage (struct kunit *test) {struct device_node *np;int allnode_count =0 , child_count; -if (!of_root) -return ; +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, of_root);for_each_of_allnodes (np) allnode_count++; child_count =of_unittest_check_node_linkage (of_root); -unittest (child_count >0 ,"Device node data structure is corrupted\n" ); -unittest (child_count == allnode_count, -"allnodes list size (%i) doesn't match sibling lists size (%i)\n" , - allnode_count, child_count); +KUNIT_EXPECT_GT_MSG (test, child_count,0 , +"Device node data structure is corrupted\n" ); +KUNIT_EXPECT_EQ_MSG ( + test, child_count, allnode_count, +"allnodes list size (%i) doesn't match sibling lists size (%i)\n" , + allnode_count, child_count);pr_debug ("allnodes list size (%i); sibling lists size (%i)\n" , allnode_count, child_count); } -static void __init of_unittest_printf_one (struct device_node *np,const char *fmt, -const char *expected) +static void of_unittest_printf_one (struct kunit *test, +struct device_node *np, +const char *fmt, +const char *expected) {unsigned char *buf;int buf_size;@@ -265,8 +271,12 @@ static void __init of_unittest_printf_one(struct device_node *np, const char *fmmemset (buf,0xff , buf_size); size =snprintf (buf, buf_size -2 , fmt, np); -/* use strcmp() instead of strncmp() here to be absolutely sure strings match */ -unittest ((strcmp (buf, expected) ==0 ) && (buf[size+1 ] ==0xff ), +KUNIT_EXPECT_STREQ_MSG ( + test, buf, expected, +"sprintf failed; fmt='%s' expected='%s' rslt='%s'\n" , + fmt, expected, buf); +KUNIT_EXPECT_EQ_MSG ( + test, buf[size+1 ],0xff ,"sprintf failed; fmt='%s' expected='%s' rslt='%s'\n" , fmt, expected, buf);@@ -276,44 +286,49 @@ static void __init of_unittest_printf_one(struct device_node *np, const char *fm/* Clear the buffer, and make sure it works correctly still */ memset (buf,0xff , buf_size);snprintf (buf, size+1 , fmt, np); -unittest (strncmp (buf, expected, size) ==0 && (buf[size+1 ] ==0xff ), +KUNIT_EXPECT_STREQ_MSG ( + test, buf, expected, +"snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n" , + size, fmt, expected, buf); +KUNIT_EXPECT_EQ_MSG ( + test, buf[size+1 ],0xff ,"snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n" , size, fmt, expected, buf); }kfree (buf); } -static void __init of_unittest_printf (void ) +static void of_unittest_printf (struct kunit *test) {struct device_node *np;const char *full_name ="/testcase-data/platform-tests/test-device@1/dev@100" ;char phandle_str[16 ] ="" ; np =of_find_node_by_path (full_name); -if (!np) { -unittest (np,"testcase data missing\n" ); -return ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np);num_to_str (phandle_str,sizeof (phandle_str), np->phandle,0 ); -of_unittest_printf_one (np,"%pOF" , full_name); -of_unittest_printf_one (np,"%pOFf" , full_name); -of_unittest_printf_one (np,"%pOFn" ,"dev" ); -of_unittest_printf_one (np,"%2pOFn" ,"dev" ); -of_unittest_printf_one (np,"%5pOFn" ," dev" ); -of_unittest_printf_one (np,"%pOFnc" ,"dev:test-sub-device" ); -of_unittest_printf_one (np,"%pOFp" , phandle_str); -of_unittest_printf_one (np,"%pOFP" ,"dev@100" ); -of_unittest_printf_one (np,"ABC %pOFP ABC" ,"ABC dev@100 ABC" ); -of_unittest_printf_one (np,"%10pOFP" ," dev@100" ); -of_unittest_printf_one (np,"%-10pOFP" ,"dev@100 " ); -of_unittest_printf_one (of_root,"%pOFP" ,"/" ); -of_unittest_printf_one (np,"%pOFF" ,"----" ); -of_unittest_printf_one (np,"%pOFPF" ,"dev@100:----" ); -of_unittest_printf_one (np,"%pOFPFPc" ,"dev@100:----:dev@100:test-sub-device" ); -of_unittest_printf_one (np,"%pOFc" ,"test-sub-device" ); -of_unittest_printf_one (np,"%pOFC" , +of_unittest_printf_one (test, np,"%pOF" , full_name); +of_unittest_printf_one (test, np,"%pOFf" , full_name); +of_unittest_printf_one (test, np,"%pOFn" ,"dev" ); +of_unittest_printf_one (test, np,"%2pOFn" ,"dev" ); +of_unittest_printf_one (test, np,"%5pOFn" ," dev" ); +of_unittest_printf_one (test, np,"%pOFnc" ,"dev:test-sub-device" ); +of_unittest_printf_one (test, np,"%pOFp" , phandle_str); +of_unittest_printf_one (test, np,"%pOFP" ,"dev@100" ); +of_unittest_printf_one (test, np,"ABC %pOFP ABC" ,"ABC dev@100 ABC" ); +of_unittest_printf_one (test, np,"%10pOFP" ," dev@100" ); +of_unittest_printf_one (test, np,"%-10pOFP" ,"dev@100 " ); +of_unittest_printf_one (test, of_root,"%pOFP" ,"/" ); +of_unittest_printf_one (test, np,"%pOFF" ,"----" ); +of_unittest_printf_one (test, np,"%pOFPF" ,"dev@100:----" ); +of_unittest_printf_one (test, + np, +"%pOFPFPc" , +"dev@100:----:dev@100:test-sub-device" ); +of_unittest_printf_one (test, np,"%pOFc" ,"test-sub-device" ); +of_unittest_printf_one (test, np,"%pOFC" ,"\"test-sub-device\",\"test-compat2\",\"test-compat3\"" ); }@@ -323,7 +338,7 @@ struct node_hash { }; static DEFINE_HASHTABLE(phandle_ht, 8); -static void __init of_unittest_check_phandles (void ) +static void of_unittest_check_phandles (struct kunit *test) {struct device_node *np;struct node_hash *nh;@@ -335,24 +350,26 @@ static void __init of_unittest_check_phandles(void)continue ;hash_for_each_possible (phandle_ht, nh, node, np->phandle) { +KUNIT_EXPECT_NE_MSG ( + test, nh->np->phandle, np->phandle, +"Duplicate phandle! %i used by %pOF and %pOF\n" , + np->phandle, nh->np, np);if (nh->np->phandle == np->phandle) { -pr_info ("Duplicate phandle! %i used by %pOF and %pOF\n" , - np->phandle, nh->np, np); dup_count++;break ; } } nh =kzalloc (sizeof (*nh),GFP_KERNEL ); -if (WARN_ON (!nh)) -return ; +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, nh); nh->np = np;hash_add (phandle_ht, &nh->node, np->phandle); phandle_count++; } -unittest (dup_count ==0 ,"Found %i duplicates in %i phandles\n" , - dup_count, phandle_count); +KUNIT_EXPECT_EQ_MSG (test, dup_count,0 , +"Found %i duplicates in %i phandles\n" , + dup_count, phandle_count);/* Clean up */ hash_for_each_safe (phandle_ht, i, tmp, nh, node) {@@ -361,20 +378,21 @@ static void __init of_unittest_check_phandles(void) } } -static void __init of_unittest_parse_phandle_with_args (void ) +static void of_unittest_parse_phandle_with_args (struct kunit *test) {struct device_node *np;struct of_phandle_args args; -int i, rc; +int i, rc =0 ; np =of_find_node_by_path ("/testcase-data/phandle-tests/consumer-a" ); -if (!np) { -pr_err ("missing testcase data\n" ); -return ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); - rc =of_count_phandle_with_args (np,"phandle-list" ,"#phandle-cells" ); -unittest (rc ==7 ,"of_count_phandle_with_args() returned %i, expected 7\n" , rc); +KUNIT_EXPECT_EQ_MSG ( + test, +of_count_phandle_with_args ( + np,"phandle-list" ,"#phandle-cells" ), +7 , +"of_count_phandle_with_args() returned %i, expected 7\n" , rc);for (i =0 ; i <8 ; i++) {bool passed = true;@@ -428,85 +446,91 @@ static void __init of_unittest_parse_phandle_with_args(void) passed = false; } -unittest (passed,"index %i - data error on node %pOF rc=%i\n" , - i, args.np, rc); +KUNIT_EXPECT_TRUE_MSG ( + test, passed, +"index %i - data error on node %pOF rc=%i\n" , + i, args.np, rc); }/* Check for missing list property */ memset (&args,0 ,sizeof (args)); - rc =of_parse_phandle_with_args (np,"phandle-list-missing" , -"#phandle-cells" ,0 , &args); -unittest (rc == -ENOENT ,"expected:%i got:%i\n" , -ENOENT , rc); - rc =of_count_phandle_with_args (np,"phandle-list-missing" , -"#phandle-cells" ); -unittest (rc == -ENOENT ,"expected:%i got:%i\n" , -ENOENT , rc); +KUNIT_EXPECT_EQ ( + test, +of_parse_phandle_with_args ( + np,"phandle-list-missing" ,"#phandle-cells" ,0 , &args), + -ENOENT ); +KUNIT_EXPECT_EQ ( + test, +of_count_phandle_with_args ( + np,"phandle-list-missing" ,"#phandle-cells" ), + -ENOENT );/* Check for missing cells property */ memset (&args,0 ,sizeof (args)); - rc =of_parse_phandle_with_args (np,"phandle-list" , -"#phandle-cells-missing" ,0 , &args); -unittest (rc == -EINVAL ,"expected:%i got:%i\n" , -EINVAL , rc); - rc =of_count_phandle_with_args (np,"phandle-list" , -"#phandle-cells-missing" ); -unittest (rc == -EINVAL ,"expected:%i got:%i\n" , -EINVAL , rc); +KUNIT_EXPECT_EQ ( + test, +of_parse_phandle_with_args ( + np,"phandle-list" ,"#phandle-cells-missing" ,0 , &args), + -EINVAL ); +KUNIT_EXPECT_EQ ( + test, +of_count_phandle_with_args ( + np,"phandle-list" ,"#phandle-cells-missing" ), + -EINVAL );/* Check for bad phandle in list */ memset (&args,0 ,sizeof (args)); - rc =of_parse_phandle_with_args (np,"phandle-list-bad-phandle" , -"#phandle-cells" ,0 , &args); -unittest (rc == -EINVAL ,"expected:%i got:%i\n" , -EINVAL , rc); - rc =of_count_phandle_with_args (np,"phandle-list-bad-phandle" , -"#phandle-cells" ); -unittest (rc == -EINVAL ,"expected:%i got:%i\n" , -EINVAL , rc); +KUNIT_EXPECT_EQ ( + test, +of_parse_phandle_with_args (np,"phandle-list-bad-phandle" , +"#phandle-cells" ,0 , &args), + -EINVAL ); +KUNIT_EXPECT_EQ ( + test, +of_count_phandle_with_args ( + np,"phandle-list-bad-phandle" ,"#phandle-cells" ), + -EINVAL );/* Check for incorrectly formed argument list */ memset (&args,0 ,sizeof (args)); - rc =of_parse_phandle_with_args (np,"phandle-list-bad-args" , -"#phandle-cells" ,1 , &args); -unittest (rc == -EINVAL ,"expected:%i got:%i\n" , -EINVAL , rc); - rc =of_count_phandle_with_args (np,"phandle-list-bad-args" , -"#phandle-cells" ); -unittest (rc == -EINVAL ,"expected:%i got:%i\n" , -EINVAL , rc); +KUNIT_EXPECT_EQ ( + test, +of_parse_phandle_with_args (np,"phandle-list-bad-args" , +"#phandle-cells" ,1 , &args), + -EINVAL ); +KUNIT_EXPECT_EQ ( + test, +of_count_phandle_with_args ( + np,"phandle-list-bad-args" ,"#phandle-cells" ), + -EINVAL ); } -static void __init of_unittest_parse_phandle_with_args_map (void ) +static void of_unittest_parse_phandle_with_args_map (struct kunit *test) {struct device_node *np, *p0, *p1, *p2, *p3;struct of_phandle_args args;int i, rc; np =of_find_node_by_path ("/testcase-data/phandle-tests/consumer-b" ); -if (!np) { -pr_err ("missing testcase data\n" ); -return ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); p0 =of_find_node_by_path ("/testcase-data/phandle-tests/provider0" ); -if (!p0) { -pr_err ("missing testcase data\n" ); -return ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, p0); p1 =of_find_node_by_path ("/testcase-data/phandle-tests/provider1" ); -if (!p1) { -pr_err ("missing testcase data\n" ); -return ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, p1); p2 =of_find_node_by_path ("/testcase-data/phandle-tests/provider2" ); -if (!p2) { -pr_err ("missing testcase data\n" ); -return ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, p2); p3 =of_find_node_by_path ("/testcase-data/phandle-tests/provider3" ); -if (!p3) { -pr_err ("missing testcase data\n" ); -return ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, p3); - rc =of_count_phandle_with_args (np,"phandle-list" ,"#phandle-cells" ); -unittest (rc ==7 ,"of_count_phandle_with_args() returned %i, expected 7\n" , rc); +KUNIT_EXPECT_EQ (test, +of_count_phandle_with_args (np, +"phandle-list" , +"#phandle-cells" ), +7 );for (i =0 ; i <8 ; i++) {bool passed = true;@@ -564,121 +588,186 @@ static void __init of_unittest_parse_phandle_with_args_map(void) passed = false; } -unittest (passed,"index %i - data error on node %s rc=%i\n" , - i, args.np->full_name, rc); +KUNIT_EXPECT_TRUE_MSG ( + test, passed, +"index %i - data error on node %s rc=%i\n" , + i, (args.np ? args.np->full_name :"missing np" ), rc); }/* Check for missing list property */ memset (&args,0 ,sizeof (args)); - rc =of_parse_phandle_with_args_map (np,"phandle-list-missing" , -"phandle" ,0 , &args); -unittest (rc == -ENOENT ,"expected:%i got:%i\n" , -ENOENT , rc); +KUNIT_EXPECT_EQ ( + test, +of_parse_phandle_with_args_map ( + np,"phandle-list-missing" ,"phandle" ,0 , &args), + -ENOENT );/* Check for missing cells,map,mask property */ memset (&args,0 ,sizeof (args)); - rc =of_parse_phandle_with_args_map (np,"phandle-list" , -"phandle-missing" ,0 , &args); -unittest (rc == -EINVAL ,"expected:%i got:%i\n" , -EINVAL , rc); +KUNIT_EXPECT_EQ ( + test, +of_parse_phandle_with_args_map ( + np,"phandle-list" ,"phandle-missing" ,0 , &args), + -EINVAL );/* Check for bad phandle in list */ memset (&args,0 ,sizeof (args)); - rc =of_parse_phandle_with_args_map (np,"phandle-list-bad-phandle" , -"phandle" ,0 , &args); -unittest (rc == -EINVAL ,"expected:%i got:%i\n" , -EINVAL , rc); +KUNIT_EXPECT_EQ ( + test, +of_parse_phandle_with_args_map ( + np,"phandle-list-bad-phandle" ,"phandle" ,0 , &args), + -EINVAL );/* Check for incorrectly formed argument list */ memset (&args,0 ,sizeof (args)); - rc =of_parse_phandle_with_args_map (np,"phandle-list-bad-args" , -"phandle" ,1 , &args); -unittest (rc == -EINVAL ,"expected:%i got:%i\n" , -EINVAL , rc); +KUNIT_EXPECT_EQ ( + test, +of_parse_phandle_with_args_map ( + np,"phandle-list-bad-args" ,"phandle" ,1 , &args), + -EINVAL ); } -static void __init of_unittest_property_string (void ) +static void of_unittest_property_string (struct kunit *test) {const char *strings[4 ];struct device_node *np;int rc; np =of_find_node_by_path ("/testcase-data/phandle-tests/consumer-a" ); -if (!np) { -pr_err ("No testcase data in device tree\n" ); -return ; - } - - rc =of_property_match_string (np,"phandle-list-names" ,"first" ); -unittest (rc ==0 ,"first expected:0 got:%i\n" , rc); - rc =of_property_match_string (np,"phandle-list-names" ,"second" ); -unittest (rc ==1 ,"second expected:1 got:%i\n" , rc); - rc =of_property_match_string (np,"phandle-list-names" ,"third" ); -unittest (rc ==2 ,"third expected:2 got:%i\n" , rc); - rc =of_property_match_string (np,"phandle-list-names" ,"fourth" ); -unittest (rc == -ENODATA ,"unmatched string; rc=%i\n" , rc); - rc =of_property_match_string (np,"missing-property" ,"blah" ); -unittest (rc == -EINVAL ,"missing property; rc=%i\n" , rc); - rc =of_property_match_string (np,"empty-property" ,"blah" ); -unittest (rc == -ENODATA ,"empty property; rc=%i\n" , rc); - rc =of_property_match_string (np,"unterminated-string" ,"blah" ); -unittest (rc == -EILSEQ ,"unterminated string; rc=%i\n" , rc); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); + +KUNIT_EXPECT_EQ ( + test, +of_property_match_string (np,"phandle-list-names" ,"first" ), +0 ); +KUNIT_EXPECT_EQ ( + test, +of_property_match_string (np,"phandle-list-names" ,"second" ), +1 ); +KUNIT_EXPECT_EQ ( + test, +of_property_match_string (np,"phandle-list-names" ,"third" ), +2 ); +KUNIT_EXPECT_EQ_MSG ( + test, +of_property_match_string (np,"phandle-list-names" ,"fourth" ), + -ENODATA , +"unmatched string" ); +KUNIT_EXPECT_EQ_MSG ( + test, +of_property_match_string (np,"missing-property" ,"blah" ), + -EINVAL , +"missing property" ); +KUNIT_EXPECT_EQ_MSG ( + test, +of_property_match_string (np,"empty-property" ,"blah" ), + -ENODATA , +"empty property" ); +KUNIT_EXPECT_EQ_MSG ( + test, +of_property_match_string (np,"unterminated-string" ,"blah" ), + -EILSEQ , +"unterminated string" );/* of_property_count_strings() tests */ - rc =of_property_count_strings (np,"string-property" ); -unittest (rc ==1 ,"Incorrect string count; rc=%i\n" , rc); - rc =of_property_count_strings (np,"phandle-list-names" ); -unittest (rc ==3 ,"Incorrect string count; rc=%i\n" , rc); - rc =of_property_count_strings (np,"unterminated-string" ); -unittest (rc == -EILSEQ ,"unterminated string; rc=%i\n" , rc); - rc =of_property_count_strings (np,"unterminated-string-list" ); -unittest (rc == -EILSEQ ,"unterminated string array; rc=%i\n" , rc); +KUNIT_EXPECT_EQ (test, +of_property_count_strings (np,"string-property" ),1 ); +KUNIT_EXPECT_EQ (test, +of_property_count_strings (np,"phandle-list-names" ),3 ); +KUNIT_EXPECT_EQ_MSG ( + test, +of_property_count_strings (np,"unterminated-string" ), -EILSEQ , +"unterminated string" ); +KUNIT_EXPECT_EQ_MSG ( + test, +of_property_count_strings (np,"unterminated-string-list" ), + -EILSEQ , +"unterminated string array" );/* of_property_read_string_index() tests */ rc =of_property_read_string_index (np,"string-property" ,0 , strings); -unittest (rc ==0 && !strcmp (strings[0 ],"foobar" ),"of_property_read_string_index() failure; rc=%i\n" , rc); +KUNIT_ASSERT_EQ (test, rc,0 ); +KUNIT_EXPECT_STREQ (test, strings[0 ],"foobar" ); + strings[0 ] =NULL ; rc =of_property_read_string_index (np,"string-property" ,1 , strings); -unittest (rc == -ENODATA && strings[0 ] ==NULL ,"of_property_read_string_index() failure; rc=%i\n" , rc); +KUNIT_EXPECT_EQ (test, rc, -ENODATA ); +KUNIT_EXPECT_EQ (test, strings[0 ],NULL ); + rc =of_property_read_string_index (np,"phandle-list-names" ,0 , strings); -unittest (rc ==0 && !strcmp (strings[0 ],"first" ),"of_property_read_string_index() failure; rc=%i\n" , rc); +KUNIT_ASSERT_EQ (test, rc,0 ); +KUNIT_EXPECT_STREQ (test, strings[0 ],"first" ); + rc =of_property_read_string_index (np,"phandle-list-names" ,1 , strings); -unittest (rc ==0 && !strcmp (strings[0 ],"second" ),"of_property_read_string_index() failure; rc=%i\n" , rc); +KUNIT_ASSERT_EQ (test, rc,0 ); +KUNIT_EXPECT_STREQ (test, strings[0 ],"second" ); + rc =of_property_read_string_index (np,"phandle-list-names" ,2 , strings); -unittest (rc ==0 && !strcmp (strings[0 ],"third" ),"of_property_read_string_index() failure; rc=%i\n" , rc); +KUNIT_ASSERT_EQ (test, rc,0 ); +KUNIT_EXPECT_STREQ (test, strings[0 ],"third" ); + strings[0 ] =NULL ; rc =of_property_read_string_index (np,"phandle-list-names" ,3 , strings); -unittest (rc == -ENODATA && strings[0 ] ==NULL ,"of_property_read_string_index() failure; rc=%i\n" , rc); +KUNIT_EXPECT_EQ (test, rc, -ENODATA ); +KUNIT_EXPECT_EQ (test, strings[0 ],NULL ); + strings[0 ] =NULL ; rc =of_property_read_string_index (np,"unterminated-string" ,0 , strings); -unittest (rc == -EILSEQ && strings[0 ] ==NULL ,"of_property_read_string_index() failure; rc=%i\n" , rc); +KUNIT_EXPECT_EQ (test, rc, -EILSEQ ); +KUNIT_EXPECT_EQ (test, strings[0 ],NULL ); + rc =of_property_read_string_index (np,"unterminated-string-list" ,0 , strings); -unittest (rc ==0 && !strcmp (strings[0 ],"first" ),"of_property_read_string_index() failure; rc=%i\n" , rc); +KUNIT_ASSERT_EQ (test, rc,0 ); +KUNIT_EXPECT_STREQ (test, strings[0 ],"first" ); + strings[0 ] =NULL ; rc =of_property_read_string_index (np,"unterminated-string-list" ,2 , strings);/* should fail */ -unittest (rc == -EILSEQ && strings[0 ] ==NULL ,"of_property_read_string_index() failure; rc=%i\n" , rc); - strings[1 ] =NULL ; +KUNIT_EXPECT_EQ (test, rc, -EILSEQ ); +KUNIT_EXPECT_EQ (test, strings[0 ],NULL );/* of_property_read_string_array() tests */ - rc =of_property_read_string_array (np,"string-property" , strings,4 ); -unittest (rc ==1 ,"Incorrect string count; rc=%i\n" , rc); - rc =of_property_read_string_array (np,"phandle-list-names" , strings,4 ); -unittest (rc ==3 ,"Incorrect string count; rc=%i\n" , rc); - rc =of_property_read_string_array (np,"unterminated-string" , strings,4 ); -unittest (rc == -EILSEQ ,"unterminated string; rc=%i\n" , rc); + strings[1 ] =NULL ; +KUNIT_EXPECT_EQ ( + test, +of_property_read_string_array ( + np,"string-property" , strings,4 ), +1 ); +KUNIT_EXPECT_EQ ( + test, +of_property_read_string_array ( + np,"phandle-list-names" , strings,4 ), +3 ); +KUNIT_EXPECT_EQ_MSG ( + test, +of_property_read_string_array ( + np,"unterminated-string" , strings,4 ), + -EILSEQ , +"unterminated string" );/* -- An incorrectly formed string should cause a failure */ - rc =of_property_read_string_array (np,"unterminated-string-list" , strings,4 ); -unittest (rc == -EILSEQ ,"unterminated string array; rc=%i\n" , rc); +KUNIT_EXPECT_EQ_MSG ( + test, +of_property_read_string_array ( + np,"unterminated-string-list" , strings,4 ), + -EILSEQ , +"unterminated string array" );/* -- parsing the correctly formed strings should still work: */ strings[2 ] =NULL ; rc =of_property_read_string_array (np,"unterminated-string-list" , strings,2 ); -unittest (rc ==2 && strings[2 ] ==NULL ,"of_property_read_string_array() failure; rc=%i\n" , rc); +KUNIT_EXPECT_EQ (test, rc,2 ); +KUNIT_EXPECT_EQ (test, strings[2 ],NULL ); + strings[1 ] =NULL ; rc =of_property_read_string_array (np,"phandle-list-names" , strings,1 ); -unittest (rc ==1 && strings[1 ] ==NULL ,"Overwrote end of string array; rc=%i, str='%s'\n" , rc, strings[1 ]); +KUNIT_ASSERT_EQ (test, rc,1 ); +KUNIT_EXPECT_EQ_MSG (test, strings[1 ],NULL , +"Overwrote end of string array" ); }#define propcmp (p1, p2) (((p1)->length == (p2)->length) && \ (p1)->value && (p2)->value && \ !memcmp((p1)->value, (p2)->value, (p1)->length) && \ !strcmp((p1)->name, (p2)->name)) -static void __init of_unittest_property_copy (void ) +static void of_unittest_property_copy (struct kunit *test) {#ifdef CONFIG_OF_DYNAMIC struct property p1 = { .name ="p1" , .length =0 , .value ="" };@@ -686,20 +775,24 @@ static void __init of_unittest_property_copy(void)struct property *new; new =__of_prop_dup (&p1,GFP_KERNEL ); -unittest (new &&propcmp (&p1, new),"empty property didn't copy correctly\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, new); +KUNIT_EXPECT_TRUE_MSG (test,propcmp (&p1, new), +"empty property didn't copy correctly" );kfree (new->value);kfree (new->name);kfree (new); new =__of_prop_dup (&p2,GFP_KERNEL ); -unittest (new &&propcmp (&p2, new),"non-empty property didn't copy correctly\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, new); +KUNIT_EXPECT_TRUE_MSG (test,propcmp (&p2, new), +"non-empty property didn't copy correctly" );kfree (new->value);kfree (new->name);kfree (new);#endif } -static void __init of_unittest_changeset (void ) +static void of_unittest_changeset (struct kunit *test) {#ifdef CONFIG_OF_DYNAMIC struct property *ppadd, padd = { .name ="prop-add" , .length =1 , .value ="" };@@ -712,32 +805,32 @@ static void __init of_unittest_changeset(void)struct of_changeset chgset; n1 =__of_node_dup (NULL ,"n1" ); -unittest (n1,"testcase setup failure\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, n1); n2 =__of_node_dup (NULL ,"n2" ); -unittest (n2,"testcase setup failure\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, n2); n21 =__of_node_dup (NULL ,"n21" ); -unittest (n21,"testcase setup failure %p\n" , n21); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, n21); nchangeset =of_find_node_by_path ("/testcase-data/changeset" ); nremove =of_get_child_by_name (nchangeset,"node-remove" ); -unittest (nremove,"testcase setup failure\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, nremove); ppadd =__of_prop_dup (&padd,GFP_KERNEL ); -unittest (ppadd,"testcase setup failure\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ppadd); ppname_n1 =__of_prop_dup (&pname_n1,GFP_KERNEL ); -unittest (ppname_n1,"testcase setup failure\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ppname_n1); ppname_n2 =__of_prop_dup (&pname_n2,GFP_KERNEL ); -unittest (ppname_n2,"testcase setup failure\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ppname_n2); ppname_n21 =__of_prop_dup (&pname_n21,GFP_KERNEL ); -unittest (ppname_n21,"testcase setup failure\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ppname_n21); ppupdate =__of_prop_dup (&pupdate,GFP_KERNEL ); -unittest (ppupdate,"testcase setup failure\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ppupdate); parent = nchangeset; n1->parent = parent;@@ -745,54 +838,72 @@ static void __init of_unittest_changeset(void) n21->parent = n2; ppremove =of_find_property (parent,"prop-remove" ,NULL ); -unittest (ppremove,"failed to find removal prop" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ppremove);of_changeset_init (&chgset); -unittest (!of_changeset_attach_node (&chgset, n1),"fail attach n1\n" ); -unittest (!of_changeset_add_property (&chgset, n1, ppname_n1),"fail add prop name\n" ); - -unittest (!of_changeset_attach_node (&chgset, n2),"fail attach n2\n" ); -unittest (!of_changeset_add_property (&chgset, n2, ppname_n2),"fail add prop name\n" ); - -unittest (!of_changeset_detach_node (&chgset, nremove),"fail remove node\n" ); -unittest (!of_changeset_add_property (&chgset, n21, ppname_n21),"fail add prop name\n" ); - -unittest (!of_changeset_attach_node (&chgset, n21),"fail attach n21\n" ); - -unittest (!of_changeset_add_property (&chgset, parent, ppadd),"fail add prop prop-add\n" ); -unittest (!of_changeset_update_property (&chgset, parent, ppupdate),"fail update prop\n" ); -unittest (!of_changeset_remove_property (&chgset, parent, ppremove),"fail remove prop\n" ); - -unittest (!of_changeset_apply (&chgset),"apply failed\n" ); +KUNIT_EXPECT_FALSE_MSG (test,of_changeset_attach_node (&chgset, n1), +"fail attach n1\n" ); +KUNIT_EXPECT_FALSE_MSG ( + test,of_changeset_add_property (&chgset, n1, ppname_n1), +"fail add prop name\n" ); + +KUNIT_EXPECT_FALSE_MSG (test,of_changeset_attach_node (&chgset, n2), +"fail attach n2\n" ); +KUNIT_EXPECT_FALSE_MSG ( + test,of_changeset_add_property (&chgset, n2, ppname_n2), +"fail add prop name\n" ); + +KUNIT_EXPECT_FALSE_MSG (test,of_changeset_detach_node (&chgset, nremove), +"fail remove node\n" ); +KUNIT_EXPECT_FALSE_MSG ( + test,of_changeset_add_property (&chgset, n21, ppname_n21), +"fail add prop name\n" ); + +KUNIT_EXPECT_FALSE_MSG (test,of_changeset_attach_node (&chgset, n21), +"fail attach n21\n" ); + +KUNIT_EXPECT_FALSE_MSG ( + test, +of_changeset_add_property (&chgset, parent, ppadd), +"fail add prop prop-add\n" ); +KUNIT_EXPECT_FALSE_MSG ( + test, +of_changeset_update_property (&chgset, parent, ppupdate), +"fail update prop\n" ); +KUNIT_EXPECT_FALSE_MSG ( + test, +of_changeset_remove_property (&chgset, parent, ppremove), +"fail remove prop\n" ); + +KUNIT_EXPECT_FALSE_MSG (test,of_changeset_apply (&chgset), +"apply failed\n" );of_node_put (nchangeset);/* Make sure node names are constructed correctly */ -unittest ((np =of_find_node_by_path ("/testcase-data/changeset/n2/n21" )), -"'%pOF' not added\n" , n21); +KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG ( + test, + np =of_find_node_by_path ("/testcase-data/changeset/n2/n21" ), +"'%pOF' not added\n" , n21);of_node_put (np); -unittest (!of_changeset_revert (&chgset),"revert failed\n" ); +KUNIT_EXPECT_FALSE (test,of_changeset_revert (&chgset));of_changeset_destroy (&chgset);#endif } -static void __init of_unittest_parse_interrupts (void ) +static void of_unittest_parse_interrupts (struct kunit *test) {struct device_node *np;struct of_phandle_args args;int i, rc; -if (of_irq_workarounds &OF_IMAP_OLDWORLD_MAC ) -return ; +KUNIT_ASSERT_FALSE (test, of_irq_workarounds &OF_IMAP_OLDWORLD_MAC ); np =of_find_node_by_path ("/testcase-data/interrupts/interrupts0" ); -if (!np) { -pr_err ("missing testcase data\n" ); -return ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np);for (i =0 ; i <4 ; i++) {bool passed = true;@@ -804,16 +915,15 @@ static void __init of_unittest_parse_interrupts(void) passed &= (args.args_count ==1 ); passed &= (args.args[0 ] == (i +1 )); -unittest (passed,"index %i - data error on node %pOF rc=%i\n" , - i, args.np, rc); +KUNIT_EXPECT_TRUE_MSG ( + test, passed, +"index %i - data error on node %pOF rc=%i\n" , + i, args.np, rc); }of_node_put (np); np =of_find_node_by_path ("/testcase-data/interrupts/interrupts1" ); -if (!np) { -pr_err ("missing testcase data\n" ); -return ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np);for (i =0 ; i <4 ; i++) {bool passed = true;@@ -850,26 +960,24 @@ static void __init of_unittest_parse_interrupts(void)default : passed = false; } -unittest (passed,"index %i - data error on node %pOF rc=%i\n" , - i, args.np, rc); +KUNIT_EXPECT_TRUE_MSG ( + test, passed, +"index %i - data error on node %pOF rc=%i\n" , + i, args.np, rc); }of_node_put (np); } -static void __init of_unittest_parse_interrupts_extended (void ) +static void of_unittest_parse_interrupts_extended (struct kunit *test) {struct device_node *np;struct of_phandle_args args;int i, rc; -if (of_irq_workarounds &OF_IMAP_OLDWORLD_MAC ) -return ; +KUNIT_ASSERT_FALSE (test, of_irq_workarounds &OF_IMAP_OLDWORLD_MAC ); np =of_find_node_by_path ("/testcase-data/interrupts/interrupts-extended0" ); -if (!np) { -pr_err ("missing testcase data\n" ); -return ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np);for (i =0 ; i <7 ; i++) {bool passed = true;@@ -924,8 +1032,10 @@ static void __init of_unittest_parse_interrupts_extended(void) passed = false; } -unittest (passed,"index %i - data error on node %pOF rc=%i\n" , - i, args.np, rc); +KUNIT_EXPECT_TRUE_MSG ( + test, passed, +"index %i - data error on node %pOF rc=%i\n" , + i, args.np, rc); }of_node_put (np); }@@ -965,7 +1075,7 @@ static struct { { .path = "/testcase-data/match-node/name9", .data = "K", }, }; -static void __init of_unittest_match_node (void ) +static void of_unittest_match_node (struct kunit *test) {struct device_node *np;const struct of_device_id *match;@@ -973,26 +1083,19 @@ static void __init of_unittest_match_node(void)for (i =0 ; i <ARRAY_SIZE (match_node_tests); i++) { np =of_find_node_by_path (match_node_tests[i].path); -if (!np) { -unittest (0 ,"missing testcase node %s\n" , - match_node_tests[i].path); -continue ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); match =of_match_node (match_node_table, np); -if (!match) { -unittest (0 ,"%s didn't match anything\n" , - match_node_tests[i].path); -continue ; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG (test, np, +"%s didn't match anything" , + match_node_tests[i].path); -if (strcmp (match->data, match_node_tests[i].data) !=0 ) { -unittest (0 ,"%s got wrong match. expected %s, got %s\n" , - match_node_tests[i].path, match_node_tests[i].data, - (const char *)match->data); -continue ; - } -unittest (1 ,"passed" ); +KUNIT_EXPECT_STREQ_MSG ( + test, + match->data, match_node_tests[i].data, +"%s got wrong match. expected %s, got %s\n" , + match_node_tests[i].path, match_node_tests[i].data, + (const char *)match->data); } }@@ -1004,9 +1107,9 @@ static struct resource test_bus_res = {static const struct platform_device_info test_bus_info = { .name ="unittest-bus" , }; -static void __init of_unittest_platform_populate (void ) +static void of_unittest_platform_populate (struct kunit *test) { -int irq, rc; +int irq;struct device_node *np, *child, *grandchild;struct platform_device *pdev, *test_bus;const struct of_device_id match[] = {@@ -1020,32 +1123,27 @@ static void __init of_unittest_platform_populate(void)/* Test that a missing irq domain returns -EPROBE_DEFER */ np =of_find_node_by_path ("/testcase-data/testcase-device1" ); pdev =of_find_device_by_node (np); -unittest (pdev,"device 1 creation failed\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, pdev);if (!(of_irq_workarounds &OF_IMAP_OLDWORLD_MAC )) { irq =platform_get_irq (pdev,0 ); -unittest (irq == -EPROBE_DEFER , -"device deferred probe failed - %d\n" , irq); +KUNIT_ASSERT_EQ (test, irq, -EPROBE_DEFER );/* Test that a parsing failure does not return -EPROBE_DEFER */ np =of_find_node_by_path ("/testcase-data/testcase-device2" ); pdev =of_find_device_by_node (np); -unittest (pdev,"device 2 creation failed\n" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, pdev); irq =platform_get_irq (pdev,0 ); -unittest (irq <0 && irq != -EPROBE_DEFER , -"device parsing error failed - %d\n" , irq); +KUNIT_ASSERT_TRUE_MSG (test, irq <0 && irq != -EPROBE_DEFER , +"device parsing error failed - %d\n" , + irq); } np =of_find_node_by_path ("/testcase-data/platform-tests" ); -unittest (np,"No testcase data in device tree\n" ); -if (!np) -return ; +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); test_bus =platform_device_register_full (&test_bus_info); - rc =PTR_ERR_OR_ZERO (test_bus); -unittest (!rc,"testbus registration failed; rc=%i\n" , rc); -if (rc) -return ; +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, test_bus); test_bus->dev.of_node = np;/* @@ -1060,17 +1158,19 @@ static void __init of_unittest_platform_populate(void)of_platform_populate (np, match,NULL , &test_bus->dev);for_each_child_of_node (np, child) {for_each_child_of_node (child, grandchild) -unittest (of_find_device_by_node (grandchild), -"Could not create device for node '%pOFn'\n" , - grandchild); +KUNIT_EXPECT_TRUE_MSG ( + test,of_find_device_by_node (grandchild), +"Could not create device for node '%pOFn'\n" , + grandchild); }of_platform_depopulate (&test_bus->dev);for_each_child_of_node (np, child) {for_each_child_of_node (child, grandchild) -unittest (!of_find_device_by_node (grandchild), -"device didn't get destroyed '%pOFn'\n" , - grandchild); +KUNIT_EXPECT_FALSE_MSG ( + test,of_find_device_by_node (grandchild), +"device didn't get destroyed '%pOFn'\n" , + grandchild); }platform_device_unregister (test_bus);@@ -1171,7 +1271,7 @@ static void attach_node_and_children(struct device_node *np)* unittest_data_add - Reads, copies data from * linked tree and attaches it to the live tree */ -static int __init unittest_data_add (void ) +static int unittest_data_add (void ) {void *unittest_data;struct device_node *unittest_data_node, *np;@@ -1242,7 +1342,7 @@ static int __init unittest_data_add(void) }#ifdef CONFIG_OF_OVERLAY -static int __init overlay_data_apply (const char *overlay_name,int *overlay_id); +static int overlay_data_apply (const char *overlay_name,int *overlay_id);static int unittest_probe (struct platform_device *pdev) {@@ -1471,172 +1571,146 @@ static void of_unittest_destroy_tracked_overlays(void) }while (defers >0 ); } -static int __init of_unittest_apply_overlay (int overlay_nr,int *overlay_id) +static int of_unittest_apply_overlay (struct kunit *test, +int overlay_nr, +int *overlay_id) {const char *overlay_name; overlay_name =overlay_name_from_nr (overlay_nr); -if (!overlay_data_apply (overlay_name, overlay_id)) { -unittest (0 ,"could not apply overlay \"%s\"\n" , - overlay_name); -return -EFAULT ; - } +KUNIT_ASSERT_TRUE_MSG (test, +overlay_data_apply (overlay_name, overlay_id), +"could not apply overlay \"%s\"\n" , overlay_name);of_unittest_track_overlay (*overlay_id);return 0 ; }/* apply an overlay while checking before and after states */ -static int __init of_unittest_apply_overlay_check (int overlay_nr, +static int of_unittest_apply_overlay_check (struct kunit *test,int overlay_nr,int unittest_nr,int before,int after,enum overlay_type ovtype) {int ret, ovcs_id;/* unittest device must not be in before state */ -if (of_unittest_device_exists (unittest_nr, ovtype) != before) { -unittest (0 ,"%s with device @\"%s\" %s\n" , -overlay_name_from_nr (overlay_nr), -unittest_path (unittest_nr, ovtype), - !before ?"enabled" :"disabled" ); -return -EINVAL ; - } +KUNIT_ASSERT_EQ_MSG ( + test,of_unittest_device_exists (unittest_nr, ovtype), before, +"%s with device @\"%s\" %s\n" ,overlay_name_from_nr (overlay_nr), +unittest_path (unittest_nr, ovtype), + !before ?"enabled" :"disabled" ); ovcs_id =0 ; - ret =of_unittest_apply_overlay (overlay_nr, &ovcs_id); + ret =of_unittest_apply_overlay (test, overlay_nr, &ovcs_id);if (ret !=0 ) { -/* of_unittest_apply_overlay already called unittest() */ +/* of_unittest_apply_overlay already set expectation */ return ret; }/* unittest device must be to set to after state */ -if (of_unittest_device_exists (unittest_nr, ovtype) != after) { -unittest (0 ,"%s failed to create @\"%s\" %s\n" , -overlay_name_from_nr (overlay_nr), -unittest_path (unittest_nr, ovtype), - !after ?"enabled" :"disabled" ); -return -EINVAL ; - } +KUNIT_ASSERT_EQ_MSG ( + test,of_unittest_device_exists (unittest_nr, ovtype), after, +"%s failed to create @\"%s\" %s\n" , +overlay_name_from_nr (overlay_nr), +unittest_path (unittest_nr, ovtype), + !after ?"enabled" :"disabled" );return 0 ; }/* apply an overlay and then revert it while checking before, after states */ -static int __init of_unittest_apply_revert_overlay_check (int overlay_nr, +static int of_unittest_apply_revert_overlay_check ( +struct kunit *test,int overlay_nr,int unittest_nr,int before,int after,enum overlay_type ovtype) {int ret, ovcs_id;/* unittest device must be in before state */ -if (of_unittest_device_exists (unittest_nr, ovtype) != before) { -unittest (0 ,"%s with device @\"%s\" %s\n" , -overlay_name_from_nr (overlay_nr), -unittest_path (unittest_nr, ovtype), - !before ?"enabled" :"disabled" ); -return -EINVAL ; - } +KUNIT_ASSERT_EQ_MSG ( + test,of_unittest_device_exists (unittest_nr, ovtype), before, +"%s with device @\"%s\" %s\n" ,overlay_name_from_nr (overlay_nr), +unittest_path (unittest_nr, ovtype), + !before ?"enabled" :"disabled" );/* apply the overlay */ ovcs_id =0 ; - ret =of_unittest_apply_overlay (overlay_nr, &ovcs_id); + ret =of_unittest_apply_overlay (test, overlay_nr, &ovcs_id);if (ret !=0 ) { -/* of_unittest_apply_overlay already called unittest() */ +/* of_unittest_apply_overlay already set expectation. */ return ret; }/* unittest device must be in after state */ -if (of_unittest_device_exists (unittest_nr, ovtype) != after) { -unittest (0 ,"%s failed to create @\"%s\" %s\n" , -overlay_name_from_nr (overlay_nr), -unittest_path (unittest_nr, ovtype), - !after ?"enabled" :"disabled" ); -return -EINVAL ; - } - - ret =of_overlay_remove (&ovcs_id); -if (ret !=0 ) { -unittest (0 ,"%s failed to be destroyed @\"%s\"\n" , -overlay_name_from_nr (overlay_nr), -unittest_path (unittest_nr, ovtype)); -return ret; - } +KUNIT_ASSERT_EQ_MSG ( + test,of_unittest_device_exists (unittest_nr, ovtype), after, +"%s failed to create @\"%s\" %s\n" , +overlay_name_from_nr (overlay_nr), +unittest_path (unittest_nr, ovtype), + !after ?"enabled" :"disabled" ); + +KUNIT_ASSERT_EQ_MSG (test,of_overlay_remove (&ovcs_id),0 , +"%s failed to be destroyed @\"%s\"\n" , +overlay_name_from_nr (overlay_nr), +unittest_path (unittest_nr, ovtype));/* unittest device must be again in before state */ -if (of_unittest_device_exists (unittest_nr,PDEV_OVERLAY ) != before) { -unittest (0 ,"%s with device @\"%s\" %s\n" , -overlay_name_from_nr (overlay_nr), -unittest_path (unittest_nr, ovtype), - !before ?"enabled" :"disabled" ); -return -EINVAL ; - } +KUNIT_ASSERT_EQ_MSG ( + test, +of_unittest_device_exists (unittest_nr,PDEV_OVERLAY ), before, +"%s with device @\"%s\" %s\n" , +overlay_name_from_nr (overlay_nr), +unittest_path (unittest_nr, ovtype), + !before ?"enabled" :"disabled" );return 0 ; }/* test activation of device */ -static void __init of_unittest_overlay_0 (void ) +static void of_unittest_overlay_0 (struct kunit *test) {/* device should enable */ -if (of_unittest_apply_overlay_check (0 ,0 ,0 ,1 ,PDEV_OVERLAY )) -return ; - -unittest (1 ,"overlay test %d passed\n" ,0 ); +of_unittest_apply_overlay_check (test,0 ,0 ,0 ,1 ,PDEV_OVERLAY ); }/* test deactivation of device */ -static void __init of_unittest_overlay_1 (void ) +static void of_unittest_overlay_1 (struct kunit *test) {/* device should disable */ -if (of_unittest_apply_overlay_check (1 ,1 ,1 ,0 ,PDEV_OVERLAY )) -return ; - -unittest (1 ,"overlay test %d passed\n" ,1 ); +of_unittest_apply_overlay_check (test,1 ,1 ,1 ,0 ,PDEV_OVERLAY ); }/* test activation of device */ -static void __init of_unittest_overlay_2 (void ) +static void of_unittest_overlay_2 (struct kunit *test) {/* device should enable */ -if (of_unittest_apply_overlay_check (2 ,2 ,0 ,1 ,PDEV_OVERLAY )) -return ; - -unittest (1 ,"overlay test %d passed\n" ,2 ); +of_unittest_apply_overlay_check (test,2 ,2 ,0 ,1 ,PDEV_OVERLAY ); }/* test deactivation of device */ -static void __init of_unittest_overlay_3 (void ) +static void of_unittest_overlay_3 (struct kunit *test) {/* device should disable */ -if (of_unittest_apply_overlay_check (3 ,3 ,1 ,0 ,PDEV_OVERLAY )) -return ; - -unittest (1 ,"overlay test %d passed\n" ,3 ); +of_unittest_apply_overlay_check (test,3 ,3 ,1 ,0 ,PDEV_OVERLAY ); }/* test activation of a full device node */ -static void __init of_unittest_overlay_4 (void ) +static void of_unittest_overlay_4 (struct kunit *test) {/* device should disable */ -if (of_unittest_apply_overlay_check (4 ,4 ,0 ,1 ,PDEV_OVERLAY )) -return ; - -unittest (1 ,"overlay test %d passed\n" ,4 ); +of_unittest_apply_overlay_check (test,4 ,4 ,0 ,1 ,PDEV_OVERLAY ); }/* test overlay apply/revert sequence */ -static void __init of_unittest_overlay_5 (void ) +static void of_unittest_overlay_5 (struct kunit *test) {/* device should disable */ -if (of_unittest_apply_revert_overlay_check (5 ,5 ,0 ,1 ,PDEV_OVERLAY )) -return ; - -unittest (1 ,"overlay test %d passed\n" ,5 ); +of_unittest_apply_revert_overlay_check (test,5 ,5 ,0 ,1 ,PDEV_OVERLAY ); }/* test overlay application in sequence */ -static void __init of_unittest_overlay_6 (void ) +static void of_unittest_overlay_6 (struct kunit *test) {int i, ov_id[2 ], ovcs_id;int overlay_nr =6 , unittest_nr =6 ;@@ -1645,74 +1719,67 @@ static void __init of_unittest_overlay_6(void)/* unittest device must be in before state */ for (i =0 ; i <2 ; i++) { -if (of_unittest_device_exists (unittest_nr + i,PDEV_OVERLAY ) - != before) { -unittest (0 ,"%s with device @\"%s\" %s\n" , -overlay_name_from_nr (overlay_nr + i), -unittest_path (unittest_nr + i, -PDEV_OVERLAY ), - !before ?"enabled" :"disabled" ); -return ; - } +KUNIT_ASSERT_EQ_MSG (test, +of_unittest_device_exists (unittest_nr + i, +PDEV_OVERLAY ), + before, +"%s with device @\"%s\" %s\n" , +overlay_name_from_nr (overlay_nr + i), +unittest_path (unittest_nr + i, +PDEV_OVERLAY ), + !before ?"enabled" :"disabled" ); }/* apply the overlays */ for (i =0 ; i <2 ; i++) { - overlay_name =overlay_name_from_nr (overlay_nr + i); -if (!overlay_data_apply (overlay_name, &ovcs_id)) { -unittest (0 ,"could not apply overlay \"%s\"\n" , - overlay_name); -return ; - } +KUNIT_ASSERT_TRUE_MSG ( + test,overlay_data_apply (overlay_name, &ovcs_id), +"could not apply overlay \"%s\"\n" , overlay_name); ov_id[i] = ovcs_id;of_unittest_track_overlay (ov_id[i]); }for (i =0 ; i <2 ; i++) {/* unittest device must be in after state */ -if (of_unittest_device_exists (unittest_nr + i,PDEV_OVERLAY ) - != after) { -unittest (0 ,"overlay @\"%s\" failed @\"%s\" %s\n" , -overlay_name_from_nr (overlay_nr + i), -unittest_path (unittest_nr + i, -PDEV_OVERLAY ), - !after ?"enabled" :"disabled" ); -return ; - } +KUNIT_ASSERT_EQ_MSG (test, +of_unittest_device_exists (unittest_nr + i, +PDEV_OVERLAY ), + after, +"overlay @\"%s\" failed @\"%s\" %s\n" , +overlay_name_from_nr (overlay_nr + i), +unittest_path (unittest_nr + i, +PDEV_OVERLAY ), + !after ?"enabled" :"disabled" ); }for (i =1 ; i >=0 ; i--) { ovcs_id = ov_id[i]; -if (of_overlay_remove (&ovcs_id)) { -unittest (0 ,"%s failed destroy @\"%s\"\n" , -overlay_name_from_nr (overlay_nr + i), -unittest_path (unittest_nr + i, -PDEV_OVERLAY )); -return ; - } +KUNIT_ASSERT_FALSE_MSG ( + test,of_overlay_remove (&ovcs_id), +"%s failed destroy @\"%s\"\n" , +overlay_name_from_nr (overlay_nr + i), +unittest_path (unittest_nr + i,PDEV_OVERLAY ));of_unittest_untrack_overlay (ov_id[i]); }for (i =0 ; i <2 ; i++) {/* unittest device must be again in before state */ -if (of_unittest_device_exists (unittest_nr + i,PDEV_OVERLAY ) - != before) { -unittest (0 ,"%s with device @\"%s\" %s\n" , -overlay_name_from_nr (overlay_nr + i), -unittest_path (unittest_nr + i, -PDEV_OVERLAY ), - !before ?"enabled" :"disabled" ); -return ; - } +KUNIT_ASSERT_EQ_MSG (test, +of_unittest_device_exists (unittest_nr + i, +PDEV_OVERLAY ), + before, +"%s with device @\"%s\" %s\n" , +overlay_name_from_nr (overlay_nr + i), +unittest_path (unittest_nr + i, +PDEV_OVERLAY ), + !before ?"enabled" :"disabled" ); } - -unittest (1 ,"overlay test %d passed\n" ,6 ); }/* test overlay application in sequence */ -static void __init of_unittest_overlay_8 (void ) +static void of_unittest_overlay_8 (struct kunit *test) {int i, ov_id[2 ], ovcs_id;int overlay_nr =8 , unittest_nr =8 ;@@ -1722,76 +1789,64 @@ static void __init of_unittest_overlay_8(void)/* apply the overlays */ for (i =0 ; i <2 ; i++) { - overlay_name =overlay_name_from_nr (overlay_nr + i); -if (!overlay_data_apply (overlay_name, &ovcs_id)) { -unittest (0 ,"could not apply overlay \"%s\"\n" , - overlay_name); -return ; - } +KUNIT_ASSERT_TRUE_MSG ( + test,overlay_data_apply (overlay_name, &ovcs_id), +"could not apply overlay \"%s\"\n" , overlay_name); ov_id[i] = ovcs_id;of_unittest_track_overlay (ov_id[i]); }/* now try to remove first overlay (it should fail) */ ovcs_id = ov_id[0 ]; -if (!of_overlay_remove (&ovcs_id)) { -unittest (0 ,"%s was destroyed @\"%s\"\n" , -overlay_name_from_nr (overlay_nr +0 ), -unittest_path (unittest_nr, -PDEV_OVERLAY )); -return ; - } +KUNIT_ASSERT_TRUE_MSG ( + test,of_overlay_remove (&ovcs_id), +"%s was destroyed @\"%s\"\n" , +overlay_name_from_nr (overlay_nr +0 ), +unittest_path (unittest_nr,PDEV_OVERLAY ));/* removing them in order should work */ for (i =1 ; i >=0 ; i--) { ovcs_id = ov_id[i]; -if (of_overlay_remove (&ovcs_id)) { -unittest (0 ,"%s not destroyed @\"%s\"\n" , -overlay_name_from_nr (overlay_nr + i), -unittest_path (unittest_nr, -PDEV_OVERLAY )); -return ; - } +KUNIT_ASSERT_FALSE_MSG ( + test,of_overlay_remove (&ovcs_id), +"%s not destroyed @\"%s\"\n" , +overlay_name_from_nr (overlay_nr + i), +unittest_path (unittest_nr,PDEV_OVERLAY ));of_unittest_untrack_overlay (ov_id[i]); } - -unittest (1 ,"overlay test %d passed\n" ,8 ); }/* test insertion of a bus with parent devices */ -static void __init of_unittest_overlay_10 (void ) +static void of_unittest_overlay_10 (struct kunit *test) { -int ret;char *child_path;/* device should disable */ - ret =of_unittest_apply_overlay_check (10 ,10 ,0 ,1 ,PDEV_OVERLAY ); -if (unittest (ret ==0 , -"overlay test %d failed; overlay application\n" ,10 )) -return ; +KUNIT_ASSERT_EQ_MSG ( + test, +of_unittest_apply_overlay_check ( + test,10 ,10 ,0 ,1 ,PDEV_OVERLAY ), +0 , +"overlay test %d failed; overlay application\n" ,10 ); child_path =kasprintf (GFP_KERNEL ,"%s/test-unittest101" ,unittest_path (10 ,PDEV_OVERLAY )); -if (unittest (child_path,"overlay test %d failed; kasprintf\n" ,10 )) -return ; +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, child_path); - ret =of_path_device_type_exists (child_path,PDEV_OVERLAY ); +KUNIT_EXPECT_TRUE_MSG ( + test,of_path_device_type_exists (child_path,PDEV_OVERLAY ), +"overlay test %d failed; no child device\n" ,10 );kfree (child_path); - -unittest (ret,"overlay test %d failed; no child device\n" ,10 ); }/* test insertion of a bus with parent devices (and revert) */ -static void __init of_unittest_overlay_11 (void ) +static void of_unittest_overlay_11 (struct kunit *test) { -int ret; -/* device should disable */ - ret =of_unittest_apply_revert_overlay_check (11 ,11 ,0 ,1 , -PDEV_OVERLAY ); -unittest (ret ==0 ,"overlay test %d failed; overlay apply\n" ,11 ); +KUNIT_EXPECT_FALSE (test,of_unittest_apply_revert_overlay_check ( + test,11 ,11 ,0 ,1 ,PDEV_OVERLAY )); }#if IS_BUILTIN (CONFIG_I2C ) &&IS_ENABLED (CONFIG_OF_OVERLAY )@@ -2013,25 +2068,18 @@ static struct i2c_driver unittest_i2c_mux_driver = {#endif -static int of_unittest_overlay_i2c_init (void ) +static int of_unittest_overlay_i2c_init (struct kunit *test) { -int ret; - - ret =i2c_add_driver (&unittest_i2c_dev_driver); -if (unittest (ret ==0 , -"could not register unittest i2c device driver\n" )) -return ret; +KUNIT_ASSERT_EQ_MSG (test,i2c_add_driver (&unittest_i2c_dev_driver),0 , +"could not register unittest i2c device driver\n" ); - ret =platform_driver_register (&unittest_i2c_bus_driver); -if (unittest (ret ==0 , -"could not register unittest i2c bus driver\n" )) -return ret; +KUNIT_ASSERT_EQ_MSG ( + test,platform_driver_register (&unittest_i2c_bus_driver),0 , +"could not register unittest i2c bus driver\n" );#if IS_BUILTIN (CONFIG_I2C_MUX ) - ret =i2c_add_driver (&unittest_i2c_mux_driver); -if (unittest (ret ==0 , -"could not register unittest i2c mux driver\n" )) -return ret; +KUNIT_ASSERT_EQ_MSG (test,i2c_add_driver (&unittest_i2c_mux_driver),0 , +"could not register unittest i2c mux driver\n" );#endif return 0 ;@@ -2046,101 +2094,85 @@ static void of_unittest_overlay_i2c_cleanup(void)i2c_del_driver (&unittest_i2c_dev_driver); } -static void __init of_unittest_overlay_i2c_12 (void ) +static void of_unittest_overlay_i2c_12 (struct kunit *test) {/* device should enable */ -if (of_unittest_apply_overlay_check (12 ,12 ,0 ,1 ,I2C_OVERLAY )) -return ; - -unittest (1 ,"overlay test %d passed\n" ,12 ); +of_unittest_apply_overlay_check (test,12 ,12 ,0 ,1 ,I2C_OVERLAY ); }/* test deactivation of device */ -static void __init of_unittest_overlay_i2c_13 (void ) +static void of_unittest_overlay_i2c_13 (struct kunit *test) {/* device should disable */ -if (of_unittest_apply_overlay_check (13 ,13 ,1 ,0 ,I2C_OVERLAY )) -return ; - -unittest (1 ,"overlay test %d passed\n" ,13 ); +of_unittest_apply_overlay_check (test,13 ,13 ,1 ,0 ,I2C_OVERLAY ); }/* just check for i2c mux existence */ -static void of_unittest_overlay_i2c_14 (void ) +static void of_unittest_overlay_i2c_14 (struct kunit *test) { +KUNIT_SUCCEED (test); } -static void __init of_unittest_overlay_i2c_15 (void ) +static void of_unittest_overlay_i2c_15 (struct kunit *test) {/* device should enable */ -if (of_unittest_apply_overlay_check (15 ,15 ,0 ,1 ,I2C_OVERLAY )) -return ; - -unittest (1 ,"overlay test %d passed\n" ,15 ); +of_unittest_apply_overlay_check (test,15 ,15 ,0 ,1 ,I2C_OVERLAY ); }#else -static inline void of_unittest_overlay_i2c_14 (void ) { } -static inline void of_unittest_overlay_i2c_15 (void ) { } +static inline void of_unittest_overlay_i2c_14 (struct kunit *test) { } +static inline void of_unittest_overlay_i2c_15 (struct kunit *test) { }#endif -static void __init of_unittest_overlay (void ) +static void of_unittest_overlay (struct kunit *test) {struct device_node *bus_np =NULL ; -if (platform_driver_register (&unittest_driver)) { -unittest (0 ,"could not register unittest driver\n" ); - goto out; - } +KUNIT_ASSERT_FALSE_MSG (test,platform_driver_register (&unittest_driver), +"could not register unittest driver\n" ); bus_np =of_find_node_by_path (bus_path); -if (bus_np ==NULL ) { -unittest (0 ,"could not find bus_path \"%s\"\n" , bus_path); - goto out; - } +KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG ( + test, bus_np,"could not find bus_path \"%s\"\n" , bus_path); -if (of_platform_default_populate (bus_np,NULL ,NULL )) { -unittest (0 ,"could not populate bus @ \"%s\"\n" , bus_path); - goto out; - } - -if (!of_unittest_device_exists (100 ,PDEV_OVERLAY )) { -unittest (0 ,"could not find unittest0 @ \"%s\"\n" , -unittest_path (100 ,PDEV_OVERLAY )); - goto out; - } +KUNIT_ASSERT_FALSE_MSG ( + test,of_platform_default_populate (bus_np,NULL ,NULL ), +"could not populate bus @ \"%s\"\n" , bus_path); -if (of_unittest_device_exists (101 ,PDEV_OVERLAY )) { -unittest (0 ,"unittest1 @ \"%s\" should not exist\n" , -unittest_path (101 ,PDEV_OVERLAY )); - goto out; - } +KUNIT_ASSERT_TRUE_MSG ( + test,of_unittest_device_exists (100 ,PDEV_OVERLAY ), +"could not find unittest0 @ \"%s\"\n" , +unittest_path (100 ,PDEV_OVERLAY )); -unittest (1 ,"basic infrastructure of overlays passed" ); +KUNIT_ASSERT_FALSE_MSG ( + test,of_unittest_device_exists (101 ,PDEV_OVERLAY ), +"unittest1 @ \"%s\" should not exist\n" , +unittest_path (101 ,PDEV_OVERLAY ));/* tests in sequence */ -of_unittest_overlay_0 (); -of_unittest_overlay_1 (); -of_unittest_overlay_2 (); -of_unittest_overlay_3 (); -of_unittest_overlay_4 (); -of_unittest_overlay_5 (); -of_unittest_overlay_6 (); -of_unittest_overlay_8 (); - -of_unittest_overlay_10 (); -of_unittest_overlay_11 (); +of_unittest_overlay_0 (test); +of_unittest_overlay_1 (test); +of_unittest_overlay_2 (test); +of_unittest_overlay_3 (test); +of_unittest_overlay_4 (test); +of_unittest_overlay_5 (test); +of_unittest_overlay_6 (test); +of_unittest_overlay_8 (test); + +of_unittest_overlay_10 (test); +of_unittest_overlay_11 (test);#if IS_BUILTIN (CONFIG_I2C ) -if (unittest (of_unittest_overlay_i2c_init () ==0 ,"i2c init failed\n" )) - goto out; +KUNIT_ASSERT_EQ_MSG (test,of_unittest_overlay_i2c_init (test),0 , +"i2c init failed\n" ); + goto out; -of_unittest_overlay_i2c_12 (); -of_unittest_overlay_i2c_13 (); -of_unittest_overlay_i2c_14 (); -of_unittest_overlay_i2c_15 (); +of_unittest_overlay_i2c_12 (test); +of_unittest_overlay_i2c_13 (test); +of_unittest_overlay_i2c_14 (test); +of_unittest_overlay_i2c_15 (test);of_unittest_overlay_i2c_cleanup ();#endif @@ -2152,7 +2184,7 @@ static void __init of_unittest_overlay(void) }#else -static inline void __init of_unittest_overlay (void ) { } +static inline void of_unittest_overlay (struct kunit *test) { }#endif #ifdef CONFIG_OF_OVERLAY @@ -2313,7 +2345,7 @@ void __init unittest_unflatten_overlay_base(void)* * Return 0 on unexpected error. */ -static int __init overlay_data_apply (const char *overlay_name,int *overlay_id) +static int overlay_data_apply (const char *overlay_name,int *overlay_id) {struct overlay_info *info;int found =0 ;@@ -2359,19 +2391,17 @@ static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)* The first part of the function is _not_ normal overlay usage; it is * finishing splicing the base overlay device tree into the live tree. */ -static __init void of_unittest_overlay_high_level (void ) +static void of_unittest_overlay_high_level (struct kunit *test) {struct device_node *last_sibling;struct device_node *np;struct device_node *of_symbols; -struct device_node *overlay_base_symbols; +struct device_node *overlay_base_symbols =0 ;struct device_node **pprev;struct property *prop; -if (!overlay_base_root) { -unittest (0 ,"overlay_base_root not initialized\n" ); -return ; - } +KUNIT_ASSERT_TRUE_MSG (test, overlay_base_root, +"overlay_base_root not initialized\n" );/* * Could not fixup phandles in unittest_unflatten_overlay_base() @@ -2418,11 +2448,9 @@ static __init void of_unittest_overlay_high_level(void)for_each_child_of_node (overlay_base_root, np) {struct device_node *base_child;for_each_child_of_node (of_root, base_child) { -if (!strcmp (np->full_name, base_child->full_name)) { -unittest (0 ,"illegal node name in overlay_base %pOFn" , - np); -return ; - } +KUNIT_ASSERT_STRNEQ_MSG ( + test, np->full_name, base_child->full_name, +"illegal node name in overlay_base %pOFn" , np); } }@@ -2456,21 +2484,24 @@ static __init void of_unittest_overlay_high_level(void) new_prop =__of_prop_dup (prop,GFP_KERNEL );if (!new_prop) { -unittest (0 ,"__of_prop_dup() of '%s' from overlay_base node __symbols__" , - prop->name); +KUNIT_FAIL (test, +"__of_prop_dup() of '%s' from overlay_base node __symbols__" , + prop->name); goto err_unlock; }if (__of_add_property (of_symbols, new_prop)) {/* "name" auto-generated by unflatten */ if (!strcmp (new_prop->name,"name" ))continue ; -unittest (0 ,"duplicate property '%s' in overlay_base node __symbols__" , - prop->name); +KUNIT_FAIL (test, +"duplicate property '%s' in overlay_base node __symbols__" , + prop->name); goto err_unlock; }if (__of_add_property_sysfs (of_symbols, new_prop)) { -unittest (0 ,"unable to add property '%s' in overlay_base node __symbols__ to sysfs" , - prop->name); +KUNIT_FAIL (test, +"unable to add property '%s' in overlay_base node __symbols__ to sysfs" , + prop->name); goto err_unlock; } }@@ -2481,20 +2512,24 @@ static __init void of_unittest_overlay_high_level(void)/* now do the normal overlay usage test */ -unittest (overlay_data_apply ("overlay" ,NULL ), -"Adding overlay 'overlay' failed\n" ); +KUNIT_EXPECT_TRUE_MSG (test,overlay_data_apply ("overlay" ,NULL ), +"Adding overlay 'overlay' failed\n" ); -unittest (overlay_data_apply ("overlay_bad_add_dup_node" ,NULL ), -"Adding overlay 'overlay_bad_add_dup_node' failed\n" ); +KUNIT_EXPECT_TRUE_MSG ( + test,overlay_data_apply ("overlay_bad_add_dup_node" ,NULL ), +"Adding overlay 'overlay_bad_add_dup_node' failed\n" ); -unittest (overlay_data_apply ("overlay_bad_add_dup_prop" ,NULL ), -"Adding overlay 'overlay_bad_add_dup_prop' failed\n" ); +KUNIT_EXPECT_TRUE_MSG ( + test,overlay_data_apply ("overlay_bad_add_dup_prop" ,NULL ), +"Adding overlay 'overlay_bad_add_dup_prop' failed\n" ); -unittest (overlay_data_apply ("overlay_bad_phandle" ,NULL ), -"Adding overlay 'overlay_bad_phandle' failed\n" ); +KUNIT_EXPECT_TRUE_MSG ( + test,overlay_data_apply ("overlay_bad_phandle" ,NULL ), +"Adding overlay 'overlay_bad_phandle' failed\n" ); -unittest (overlay_data_apply ("overlay_bad_symbol" ,NULL ), -"Adding overlay 'overlay_bad_symbol' failed\n" ); +KUNIT_EXPECT_TRUE_MSG ( + test,overlay_data_apply ("overlay_bad_symbol" ,NULL ), +"Adding overlay 'overlay_bad_symbol' failed\n" );return ;@@ -2504,57 +2539,52 @@ static __init void of_unittest_overlay_high_level(void)#else -static inline __init void of_unittest_overlay_high_level (void ) {} +static inline void of_unittest_overlay_high_level (struct kunit *test) {}#endif -static int __init of_unittest (void ) +static int of_test_init (struct kunit *test) { -struct device_node *np; -int res; -/* adding data for unittest */ - res =unittest_data_add (); -if (res) -return res; +KUNIT_ASSERT_EQ (test,0 ,unittest_data_add ()); +if (!of_aliases) of_aliases =of_find_node_by_path ("/aliases" ); - np =of_find_node_by_path ("/testcase-data/phandle-tests/consumer-a" ); -if (!np) { -pr_info ("No testcase data in device tree; not running tests\n" ); -return 0 ; - } -of_node_put (np); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test,of_find_node_by_path ( +"/testcase-data/phandle-tests/consumer-a" ));if (IS_ENABLED (CONFIG_UML ))unflatten_device_tree (); -pr_info ("start of unittest - you will see error messages\n" ); -of_unittest_check_tree_linkage (); -of_unittest_check_phandles (); -of_unittest_find_node_by_name (); -of_unittest_dynamic (); -of_unittest_parse_phandle_with_args (); -of_unittest_parse_phandle_with_args_map (); -of_unittest_printf (); -of_unittest_property_string (); -of_unittest_property_copy (); -of_unittest_changeset (); -of_unittest_parse_interrupts (); -of_unittest_parse_interrupts_extended (); -of_unittest_match_node (); -of_unittest_platform_populate (); -of_unittest_overlay (); +return 0 ; +} +static struct kunit_case of_test_cases[] = { +KUNIT_CASE (of_unittest_check_tree_linkage), +KUNIT_CASE (of_unittest_check_phandles), +KUNIT_CASE (of_unittest_find_node_by_name), +KUNIT_CASE (of_unittest_dynamic), +KUNIT_CASE (of_unittest_parse_phandle_with_args), +KUNIT_CASE (of_unittest_parse_phandle_with_args_map), +KUNIT_CASE (of_unittest_printf), +KUNIT_CASE (of_unittest_property_string), +KUNIT_CASE (of_unittest_property_copy), +KUNIT_CASE (of_unittest_changeset), +KUNIT_CASE (of_unittest_parse_interrupts), +KUNIT_CASE (of_unittest_parse_interrupts_extended), +KUNIT_CASE (of_unittest_match_node), +KUNIT_CASE (of_unittest_platform_populate), +KUNIT_CASE (of_unittest_overlay),/* Double check linkage after removing testcase data */ -of_unittest_check_tree_linkage (); - -of_unittest_overlay_high_level (); - -pr_info ("end of unittest - %i passed, %i failed\n" , - unittest_results.passed, unittest_results.failed); +KUNIT_CASE (of_unittest_check_tree_linkage), +KUNIT_CASE (of_unittest_overlay_high_level), + {}, +}; -return 0 ; -} -late_initcall (of_unittest); +static struct kunit_module of_test_module = { + .name ="of-test" , + .init = of_test_init, + .test_cases = of_test_cases, +}; +module_test (of_test_module);
Re: [RFC v4 15/17] of: unittest: migrate tests to run on KUnit
Quoted 27 lines · Frank Rowand
drivers/of/unittest.c
Quoted 22 lines · Brendan Higgins
Migrate tests without any cleanup, or modifying test logic in anyway to run under KUnit using the KUnit expectation and assertion API.
Signed-off-by Brendan Higgins <[email protected]>--- drivers/of/Kconfig | 1 + drivers/of/unittest.c | 1310 +++++++++++++++++++++-------------------- 2 files changed, 671 insertions(+), 640 deletions(-)drivers/of/Kconfig +1
-0@@ -15,6 +15,7 @@ if OF config OF_UNITTEST bool "Device Tree runtime unit tests" depends on !SPARC + depends on KUNIT select IRQ_DOMAIN select OF_EARLY_FLATTREE select OF_RESOLVEdrivers/of/unittest.c +0
-0These comments are from applying the patches to 5.0-rc3.
Quoted 4 lines
The final hunk of this patch fails to apply because it depends upon
[PATCH v1 0/1] of: unittest: unflatten device tree on UML when testing.
Whoops, I probably should have made a note of that in the commit description or cover letter, sorry.
Quoted 11 lines
If I apply that patch then I can apply patches 15 through 17.
If I apply patches 1 through 14 and boot the uml kernel then the devicetree unittest result is:
### dt-test ### FAIL of_unittest_overlay_high_level():2372
overlay_base_rootnot initialized ### dt-test ### end of unittest - 219 passed, 1 failedThis is as expected from your previous reports, and is fixed after applying
Quoted 7 lines
[PATCH v1 0/1] of: unittest: unflatten device tree on UML when testing.
with the devicetree unittest result of:
### dt-test ### end of unittest - 224 passed, 0 failed
After adding patch 15, there are a lot of "unittest internal error" messages.
Yeah, I meant to ask you about that. I thought it was due to a change you made, but after further examination, just now, I found it was my fault. Sorry for not mentioning that anywhere. I will fix it in v5.
Thanks!
RFC v4 16/17 of: unittest: split out a couple of test cases from unittest
In reply to Brendan HigginsSplit out a couple of test cases that these features in base.c from the unittest.c monolith. The intention is that we will eventually split out all test cases and group them together based on what portion of device tree they test.
--- drivers/of/Makefile | 2 +- drivers/of/base-test.c | 214 ++++++++++++++++++++++++ drivers/of/test-common.c | 175 ++++++++++++++++++++ drivers/of/test-common.h | 16 ++ drivers/of/unittest.c | 345 +-------------------------------------- 5 files changed, 407 insertions(+), 345 deletions(-) create mode 100644 drivers/of/base-test.c create mode 100644 drivers/of/test-common.c create mode 100644 drivers/of/test-common.h
drivers/of/Makefile +1 -1
@@ -8,7 +8,7 @@ obj-$(CONFIG_OF_PROMTREE) += pdt.o obj-$(CONFIG_OF_ADDRESS) += address.o obj-$(CONFIG_OF_IRQ) += irq.o obj-$(CONFIG_OF_NET) += of_net.o -obj-$(CONFIG_OF_UNITTEST) += unittest.o +obj-$(CONFIG_OF_UNITTEST) += unittest.o base-test.o test-common.o obj-$(CONFIG_OF_MDIO) += of_mdio.o obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o obj-$(CONFIG_OF_RESOLVE) += resolver.o
drivers/of/base-test.c new file +214 -0
214 lines not on this page · raw message
drivers/of/test-common.c new file +175 -0
175 lines not on this page · raw message
drivers/of/test-common.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* +* Common code to be used by unit tests. +*/ +#ifndef _LINUX_OF_TEST_COMMON_H +#define _LINUX_OF_TEST_COMMON_H + +#include <linux/of.h> + +/** +* unittest_data_add - Reads, copies data from +* linked tree and attaches it to the live tree +*/ +int unittest_data_add (void ); + +#endif /* _LINUX_OF_TEST_COMMON_H */
drivers/of/unittest.c +1 -344
363 lines not on this page · raw message
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
Re: [RFC v4 16/17] of: unittest: split out a couple of test cases from unittest
Quoted 4 lines · Brendan Higgins
Split out a couple of test cases that these features in
base.cfrom theunittest.cmonolith. The intention is that we will eventually split out all test cases and group them together based on what portion of device tree they test.
I still object to this patch. I do not want this code scattered into additional files.
-Frank
Quoted 820 lines
Signed-off-by Brendan Higgins <[email protected]>--- drivers/of/Makefile | 2 +- drivers/of/base-test.c | 214 ++++++++++++++++++++++++ drivers/of/test-common.c | 175 ++++++++++++++++++++ drivers/of/test-common.h | 16 ++ drivers/of/unittest.c | 345 +-------------------------------------- 5 files changed, 407 insertions(+), 345 deletions(-) create mode 100644 drivers/of/base-test.c create mode 100644 drivers/of/test-common.c create mode 100644 drivers/of/test-common.hdrivers/of/Makefile +1
-1@@ -8,7 +8,7 @@ obj-$(CONFIG_OF_PROMTREE) += pdt.o obj-$(CONFIG_OF_ADDRESS) += address.o obj-$(CONFIG_OF_IRQ) += irq.o obj-$(CONFIG_OF_NET) += of_net.o -obj-$(CONFIG_OF_UNITTEST) += unittest.o +obj-$(CONFIG_OF_UNITTEST) += unittest.o base-test.o test-common.o obj-$(CONFIG_OF_MDIO) += of_mdio.o obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o obj-$(CONFIG_OF_RESOLVE) += resolver.odrivers/of/base-test.c new file +214
-0@@ -0,0 +1,214 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Unit tests for functions defined in base.c. +*/ +#include <linux/of.h> + +#include <kunit/test.h> + +#include "test-common.h" + +static void of_unittest_find_node_by_name (struct kunit *test) +{ +struct device_node *np; +const char *options, *name; + + np =of_find_node_by_path ("/testcase-data" ); + name =kasprintf (GFP_KERNEL ,"%pOF" , np); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_STREQ_MSG (test,"/testcase-data" , name, +"find /testcase-data failed\n" ); +of_node_put (np); +kfree (name); + +/* Test if trailing '/' works */ +KUNIT_EXPECT_EQ_MSG (test,of_find_node_by_path ("/testcase-data/" ),NULL , +"trailing '/' on /testcase-data/ should fail\n" ); + + np =of_find_node_by_path ("/testcase-data/phandle-tests/consumer-a" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); + name =kasprintf (GFP_KERNEL ,"%pOF" , np); +KUNIT_EXPECT_STREQ_MSG ( + test,"/testcase-data/phandle-tests/consumer-a" , name, +"find /testcase-data/phandle-tests/consumer-a failed\n" ); +of_node_put (np); +kfree (name); + + np =of_find_node_by_path ("testcase-alias" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); + name =kasprintf (GFP_KERNEL ,"%pOF" , np); +KUNIT_EXPECT_STREQ_MSG (test,"/testcase-data" , name, +"find testcase-alias failed\n" ); +of_node_put (np); +kfree (name); + +/* Test if trailing '/' works on aliases */ +KUNIT_EXPECT_EQ_MSG (test,of_find_node_by_path ("testcase-alias/" ),NULL , +"trailing '/' on testcase-alias/ should fail\n" ); + + np =of_find_node_by_path ("testcase-alias/phandle-tests/consumer-a" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); + name =kasprintf (GFP_KERNEL ,"%pOF" , np); +KUNIT_EXPECT_STREQ_MSG ( + test,"/testcase-data/phandle-tests/consumer-a" , name, +"find testcase-alias/phandle-tests/consumer-a failed\n" ); +of_node_put (np); +kfree (name); + +KUNIT_EXPECT_EQ_MSG ( + test, + np =of_find_node_by_path ("/testcase-data/missing-path" ),NULL , +"non-existent path returned node %pOF\n" , np); +of_node_put (np); + +KUNIT_EXPECT_EQ_MSG ( + test, np =of_find_node_by_path ("missing-alias" ),NULL , +"non-existent alias returned node %pOF\n" , np); +of_node_put (np); + +KUNIT_EXPECT_EQ_MSG ( + test, + np =of_find_node_by_path ("testcase-alias/missing-path" ),NULL , +"non-existent alias with relative path returned node %pOF\n" , + np); +of_node_put (np); + + np =of_find_node_opts_by_path ("/testcase-data:testoption" , &options); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_STREQ_MSG (test,"testoption" , options, +"option path test failed\n" ); +of_node_put (np); + + np =of_find_node_opts_by_path ("/testcase-data:test/option" , &options); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_STREQ_MSG (test,"test/option" , options, +"option path test, subcase #1 failed\n" ); +of_node_put (np); + + np =of_find_node_opts_by_path ("/testcase-data/testcase-device1:test/option" , &options); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_STREQ_MSG (test,"test/option" , options, +"option path test, subcase #2 failed\n" ); +of_node_put (np); + + np =of_find_node_opts_by_path ("/testcase-data:testoption" ,NULL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG (test, np, +"NULL option path test failed\n" ); +of_node_put (np); + + np =of_find_node_opts_by_path ("testcase-alias:testaliasoption" , + &options); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_STREQ_MSG (test,"testaliasoption" , options, +"option alias path test failed\n" ); +of_node_put (np); + + np =of_find_node_opts_by_path ("testcase-alias:test/alias/option" , + &options); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_STREQ_MSG (test,"test/alias/option" , options, +"option alias path test, subcase #1 failed\n" ); +of_node_put (np); + + np =of_find_node_opts_by_path ("testcase-alias:testaliasoption" ,NULL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG ( + test, np,"NULL option alias path test failed\n" ); +of_node_put (np); + + options ="testoption" ; + np =of_find_node_opts_by_path ("testcase-alias" , &options); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_EQ_MSG (test, options,NULL , +"option clearing test failed\n" ); +of_node_put (np); + + options ="testoption" ; + np =of_find_node_opts_by_path ("/" , &options); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +KUNIT_EXPECT_EQ_MSG (test, options,NULL , +"option clearing root node test failed\n" ); +of_node_put (np); +} + +static void of_unittest_dynamic (struct kunit *test) +{ +struct device_node *np; +struct property *prop; + + np =of_find_node_by_path ("/testcase-data" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); + +/* Array of 4 properties for the purpose of testing */ + prop =kcalloc (4 ,sizeof (*prop),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, prop); + +/* Add a new property - should pass*/ + prop->name ="new-property" ; + prop->value ="new-property-data" ; + prop->length =strlen (prop->value) +1 ; +KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop),0 , +"Adding a new property failed\n" ); + +/* Try to add an existing property - should fail */ + prop++; + prop->name ="new-property" ; + prop->value ="new-property-data-should-fail" ; + prop->length =strlen (prop->value) +1 ; +KUNIT_EXPECT_NE_MSG (test,of_add_property (np, prop),0 , +"Adding an existing property should have failed\n" ); + +/* Try to modify an existing property - should pass */ + prop->value ="modify-property-data-should-pass" ; + prop->length =strlen (prop->value) +1 ; +KUNIT_EXPECT_EQ_MSG ( + test,of_update_property (np, prop),0 , +"Updating an existing property should have passed\n" ); + +/* Try to modify non-existent property - should pass*/ + prop++; + prop->name ="modify-property" ; + prop->value ="modify-missing-property-data-should-pass" ; + prop->length =strlen (prop->value) +1 ; +KUNIT_EXPECT_EQ_MSG (test,of_update_property (np, prop),0 , +"Updating a missing property should have passed\n" ); + +/* Remove property - should pass */ +KUNIT_EXPECT_EQ_MSG (test,of_remove_property (np, prop),0 , +"Removing a property should have passed\n" ); + +/* Adding very large property - should pass */ + prop++; + prop->name ="large-property-PAGE_SIZEx8" ; + prop->length =PAGE_SIZE *8 ; + prop->value =kzalloc (prop->length,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, prop->value); +KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop),0 , +"Adding a large property should have passed\n" ); +} + +static int of_test_init (struct kunit *test) +{ +/* adding data for unittest */ +KUNIT_ASSERT_EQ (test,0 ,unittest_data_add ()); + +if (!of_aliases) + of_aliases =of_find_node_by_path ("/aliases" ); + +KUNIT_ASSERT_NOT_ERR_OR_NULL (test,of_find_node_by_path ( +"/testcase-data/phandle-tests/consumer-a" )); + +return 0 ; +} + +static struct kunit_case of_test_cases[] = { +KUNIT_CASE (of_unittest_find_node_by_name), +KUNIT_CASE (of_unittest_dynamic), + {}, +}; + +static struct kunit_module of_test_module = { + .name ="of-base-test" , + .init = of_test_init, + .test_cases = of_test_cases, +}; +module_test (of_test_module);drivers/of/test-common.c new file +175
-0@@ -0,0 +1,175 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Common code to be used by unit tests. +*/ +#include "test-common.h" + +#include <linux/of_fdt.h> +#include <linux/slab.h> + +#include "of_private.h" + +/** +* update_node_properties - adds the properties +* of np into dup node (present in live tree) and +* updates parent of children of np to dup. +* +* @np: node whose properties are being added to the live tree +* @dup: node present in live tree to be updated +*/ +static void update_node_properties (struct device_node *np, +struct device_node *dup) +{ +struct property *prop; +struct property *save_next; +struct device_node *child; +int ret; + +for_each_child_of_node (np, child) + child->parent = dup; + +/* +* "unittest internal error: unable to add testdata property" +* +* If this message reports a property in node '/__symbols__' then +* the respective unittest overlay contains a label that has the +* same name as a label in the live devicetree. The label will +* be in the live devicetree only if the devicetree source was +* compiled with the '-@' option. If you encounter this error, +* please consider renaming __all__ of the labels in the unittest +* overlay dts files with an odd prefix that is unlikely to be +* used in a real devicetree. +*/ + +/* +* open code for_each_property_of_node() because of_add_property() +* sets prop->next to NULL +*/ +for (prop = np->properties; prop !=NULL ; prop = save_next) { + save_next = prop->next; + ret =of_add_property (dup, prop); +if (ret) +pr_err ("unittest internal error: unable to add testdata property %pOF/%s" , + np, prop->name); + } +} + +/** +* attach_node_and_children - attaches nodes +* and its children to live tree +* +* @np: Node to attach to live tree +*/ +static void attach_node_and_children (struct device_node *np) +{ +struct device_node *next, *dup, *child; +unsigned long flags; +const char *full_name; + + full_name =kasprintf (GFP_KERNEL ,"%pOF" , np); + +if (!strcmp (full_name,"/__local_fixups__" ) || + !strcmp (full_name,"/__fixups__" )) +return ; + + dup =of_find_node_by_path (full_name); +kfree (full_name); +if (dup) { +update_node_properties (np, dup); +return ; + } + + child = np->child; + np->child =NULL ; + +mutex_lock (&of_mutex); +raw_spin_lock_irqsave (&devtree_lock, flags); + np->sibling = np->parent->child; + np->parent->child = np; +of_node_clear_flag (np,OF_DETACHED ); +raw_spin_unlock_irqrestore (&devtree_lock, flags); + +__of_attach_node_sysfs (np); +mutex_unlock (&of_mutex); + +while (child) { + next = child->sibling; +attach_node_and_children (child); + child = next; + } +} + +/** +* unittest_data_add - Reads, copies data from +* linked tree and attaches it to the live tree +*/ +int unittest_data_add (void ) +{ +void *unittest_data; +struct device_node *unittest_data_node, *np; +/* +* __dtb_testcases_begin[] and __dtb_testcases_end[] are magically +* created by cmd_dt_S_dtb in scripts/Makefile.lib +*/ +extern uint8_t __dtb_testcases_begin[]; +extern uint8_t __dtb_testcases_end[]; +const int size = __dtb_testcases_end - __dtb_testcases_begin; +int rc; + +if (!size) { +pr_warn ("%s: No testcase data to attach; not running tests\n" , + __func__); +return -ENODATA ; + } + +/* creating copy */ + unittest_data =kmemdup (__dtb_testcases_begin, size,GFP_KERNEL ); + +if (!unittest_data) { +pr_warn ("%s: Failed to allocate memory for unittest_data; " +"not running tests\n" , __func__); +return -ENOMEM ; + } +of_fdt_unflatten_tree (unittest_data,NULL , &unittest_data_node); +if (!unittest_data_node) { +pr_warn ("%s: No tree to attach; not running tests\n" , __func__); +return -ENODATA ; + } + +/* +* This lock normally encloses of_resolve_phandles() +*/ +of_overlay_mutex_lock (); + + rc =of_resolve_phandles (unittest_data_node); +if (rc) { +pr_err ("%s: Failed to resolve phandles (rc=%i)\n" , __func__, rc); +of_overlay_mutex_unlock (); +return -EINVAL ; + } + +if (!of_root) { + of_root = unittest_data_node; +for_each_of_allnodes (np) +__of_attach_node_sysfs (np); + of_aliases =of_find_node_by_path ("/aliases" ); + of_chosen =of_find_node_by_path ("/chosen" ); +of_overlay_mutex_unlock (); +return 0 ; + } + +/* attach the sub-tree to live tree */ + np = unittest_data_node->child; +while (np) { +struct device_node *next = np->sibling; + + np->parent = of_root; +attach_node_and_children (np); + np = next; + } + +of_overlay_mutex_unlock (); + +return 0 ; +} +drivers/of/test-common.h new file +16
-0@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* +* Common code to be used by unit tests. +*/ +#ifndef _LINUX_OF_TEST_COMMON_H +#define _LINUX_OF_TEST_COMMON_H + +#include <linux/of.h> + +/** +* unittest_data_add - Reads, copies data from +* linked tree and attaches it to the live tree +*/ +int unittest_data_add (void ); + +#endif /* _LINUX_OF_TEST_COMMON_H */ drivers/of/unittest.c +1
-344@@ -29,184 +29,7 @@#include <kunit/test.h> #include "of_private.h" - -static void of_unittest_find_node_by_name (struct kunit *test) -{ -struct device_node *np; -const char *options, *name; - - np =of_find_node_by_path ("/testcase-data" ); - name =kasprintf (GFP_KERNEL ,"%pOF" , np); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); -KUNIT_EXPECT_STREQ_MSG (test,"/testcase-data" , name, -"find /testcase-data failed\n" ); -of_node_put (np); -kfree (name); - -/* Test if trailing '/' works */ -KUNIT_EXPECT_EQ_MSG (test,of_find_node_by_path ("/testcase-data/" ),NULL , -"trailing '/' on /testcase-data/ should fail\n" ); - - np =of_find_node_by_path ("/testcase-data/phandle-tests/consumer-a" ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); - name =kasprintf (GFP_KERNEL ,"%pOF" , np); -KUNIT_EXPECT_STREQ_MSG ( - test,"/testcase-data/phandle-tests/consumer-a" , name, -"find /testcase-data/phandle-tests/consumer-a failed\n" ); -of_node_put (np); -kfree (name); - - np =of_find_node_by_path ("testcase-alias" ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); - name =kasprintf (GFP_KERNEL ,"%pOF" , np); -KUNIT_EXPECT_STREQ_MSG (test,"/testcase-data" , name, -"find testcase-alias failed\n" ); -of_node_put (np); -kfree (name); - -/* Test if trailing '/' works on aliases */ -KUNIT_EXPECT_EQ_MSG (test,of_find_node_by_path ("testcase-alias/" ),NULL , -"trailing '/' on testcase-alias/ should fail\n" ); - - np =of_find_node_by_path ("testcase-alias/phandle-tests/consumer-a" ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); - name =kasprintf (GFP_KERNEL ,"%pOF" , np); -KUNIT_EXPECT_STREQ_MSG ( - test,"/testcase-data/phandle-tests/consumer-a" , name, -"find testcase-alias/phandle-tests/consumer-a failed\n" ); -of_node_put (np); -kfree (name); - -KUNIT_EXPECT_EQ_MSG ( - test, - np =of_find_node_by_path ("/testcase-data/missing-path" ),NULL , -"non-existent path returned node %pOF\n" , np); -of_node_put (np); - -KUNIT_EXPECT_EQ_MSG ( - test, np =of_find_node_by_path ("missing-alias" ),NULL , -"non-existent alias returned node %pOF\n" , np); -of_node_put (np); - -KUNIT_EXPECT_EQ_MSG ( - test, - np =of_find_node_by_path ("testcase-alias/missing-path" ),NULL , -"non-existent alias with relative path returned node %pOF\n" , - np); -of_node_put (np); - - np =of_find_node_opts_by_path ("/testcase-data:testoption" , &options); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); -KUNIT_EXPECT_STREQ_MSG (test,"testoption" , options, -"option path test failed\n" ); -of_node_put (np); - - np =of_find_node_opts_by_path ("/testcase-data:test/option" , &options); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); -KUNIT_EXPECT_STREQ_MSG (test,"test/option" , options, -"option path test, subcase #1 failed\n" ); -of_node_put (np); - - np =of_find_node_opts_by_path ("/testcase-data/testcase-device1:test/option" , &options); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); -KUNIT_EXPECT_STREQ_MSG (test,"test/option" , options, -"option path test, subcase #2 failed\n" ); -of_node_put (np); - - np =of_find_node_opts_by_path ("/testcase-data:testoption" ,NULL ); -KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG (test, np, -"NULL option path test failed\n" ); -of_node_put (np); - - np =of_find_node_opts_by_path ("testcase-alias:testaliasoption" , - &options); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); -KUNIT_EXPECT_STREQ_MSG (test,"testaliasoption" , options, -"option alias path test failed\n" ); -of_node_put (np); - - np =of_find_node_opts_by_path ("testcase-alias:test/alias/option" , - &options); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); -KUNIT_EXPECT_STREQ_MSG (test,"test/alias/option" , options, -"option alias path test, subcase #1 failed\n" ); -of_node_put (np); - - np =of_find_node_opts_by_path ("testcase-alias:testaliasoption" ,NULL ); -KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG ( - test, np,"NULL option alias path test failed\n" ); -of_node_put (np); - - options ="testoption" ; - np =of_find_node_opts_by_path ("testcase-alias" , &options); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); -KUNIT_EXPECT_EQ_MSG (test, options,NULL , -"option clearing test failed\n" ); -of_node_put (np); - - options ="testoption" ; - np =of_find_node_opts_by_path ("/" , &options); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); -KUNIT_EXPECT_EQ_MSG (test, options,NULL , -"option clearing root node test failed\n" ); -of_node_put (np); -} - -static void of_unittest_dynamic (struct kunit *test) -{ -struct device_node *np; -struct property *prop; - - np =of_find_node_by_path ("/testcase-data" ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); - -/* Array of 4 properties for the purpose of testing */ - prop =kcalloc (4 ,sizeof (*prop),GFP_KERNEL ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, prop); - -/* Add a new property - should pass*/ - prop->name ="new-property" ; - prop->value ="new-property-data" ; - prop->length =strlen (prop->value) +1 ; -KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop),0 , -"Adding a new property failed\n" ); - -/* Try to add an existing property - should fail */ - prop++; - prop->name ="new-property" ; - prop->value ="new-property-data-should-fail" ; - prop->length =strlen (prop->value) +1 ; -KUNIT_EXPECT_NE_MSG (test,of_add_property (np, prop),0 , -"Adding an existing property should have failed\n" ); - -/* Try to modify an existing property - should pass */ - prop->value ="modify-property-data-should-pass" ; - prop->length =strlen (prop->value) +1 ; -KUNIT_EXPECT_EQ_MSG ( - test,of_update_property (np, prop),0 , -"Updating an existing property should have passed\n" ); - -/* Try to modify non-existent property - should pass*/ - prop++; - prop->name ="modify-property" ; - prop->value ="modify-missing-property-data-should-pass" ; - prop->length =strlen (prop->value) +1 ; -KUNIT_EXPECT_EQ_MSG (test,of_update_property (np, prop),0 , -"Updating a missing property should have passed\n" ); - -/* Remove property - should pass */ -KUNIT_EXPECT_EQ_MSG (test,of_remove_property (np, prop),0 , -"Removing a property should have passed\n" ); - -/* Adding very large property - should pass */ - prop++; - prop->name ="large-property-PAGE_SIZEx8" ; - prop->length =PAGE_SIZE *8 ; - prop->value =kzalloc (prop->length,GFP_KERNEL ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, prop->value); -KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop),0 , -"Adding a large property should have passed\n" ); -} +#include "test-common.h" static int of_unittest_check_node_linkage (struct device_node *np) {@@ -1177,170 +1000,6 @@ static void of_unittest_platform_populate(struct kunit *test)of_node_put (np); } -/** -* update_node_properties - adds the properties -* of np into dup node (present in live tree) and -* updates parent of children of np to dup. -* -* @np: node whose properties are being added to the live tree -* @dup: node present in live tree to be updated -*/ -static void update_node_properties (struct device_node *np, -struct device_node *dup) -{ -struct property *prop; -struct property *save_next; -struct device_node *child; -int ret; - -for_each_child_of_node (np, child) - child->parent = dup; - -/* -* "unittest internal error: unable to add testdata property" -* -* If this message reports a property in node '/__symbols__' then -* the respective unittest overlay contains a label that has the -* same name as a label in the live devicetree. The label will -* be in the live devicetree only if the devicetree source was -* compiled with the '-@' option. If you encounter this error, -* please consider renaming __all__ of the labels in the unittest -* overlay dts files with an odd prefix that is unlikely to be -* used in a real devicetree. -*/ - -/* -* open code for_each_property_of_node() because of_add_property() -* sets prop->next to NULL -*/ -for (prop = np->properties; prop !=NULL ; prop = save_next) { - save_next = prop->next; - ret =of_add_property (dup, prop); -if (ret) -pr_err ("unittest internal error: unable to add testdata property %pOF/%s" , - np, prop->name); - } -} - -/** -* attach_node_and_children - attaches nodes -* and its children to live tree -* -* @np: Node to attach to live tree -*/ -static void attach_node_and_children (struct device_node *np) -{ -struct device_node *next, *dup, *child; -unsigned long flags; -const char *full_name; - - full_name =kasprintf (GFP_KERNEL ,"%pOF" , np); - -if (!strcmp (full_name,"/__local_fixups__" ) || - !strcmp (full_name,"/__fixups__" )) -return ; - - dup =of_find_node_by_path (full_name); -kfree (full_name); -if (dup) { -update_node_properties (np, dup); -return ; - } - - child = np->child; - np->child =NULL ; - -mutex_lock (&of_mutex); -raw_spin_lock_irqsave (&devtree_lock, flags); - np->sibling = np->parent->child; - np->parent->child = np; -of_node_clear_flag (np,OF_DETACHED ); -raw_spin_unlock_irqrestore (&devtree_lock, flags); - -__of_attach_node_sysfs (np); -mutex_unlock (&of_mutex); - -while (child) { - next = child->sibling; -attach_node_and_children (child); - child = next; - } -} - -/** -* unittest_data_add - Reads, copies data from -* linked tree and attaches it to the live tree -*/ -static int unittest_data_add (void ) -{ -void *unittest_data; -struct device_node *unittest_data_node, *np; -/* -* __dtb_testcases_begin[] and __dtb_testcases_end[] are magically -* created by cmd_dt_S_dtb in scripts/Makefile.lib -*/ -extern uint8_t __dtb_testcases_begin[]; -extern uint8_t __dtb_testcases_end[]; -const int size = __dtb_testcases_end - __dtb_testcases_begin; -int rc; - -if (!size) { -pr_warn ("%s: No testcase data to attach; not running tests\n" , - __func__); -return -ENODATA ; - } - -/* creating copy */ - unittest_data =kmemdup (__dtb_testcases_begin, size,GFP_KERNEL ); - -if (!unittest_data) { -pr_warn ("%s: Failed to allocate memory for unittest_data; " -"not running tests\n" , __func__); -return -ENOMEM ; - } -of_fdt_unflatten_tree (unittest_data,NULL , &unittest_data_node); -if (!unittest_data_node) { -pr_warn ("%s: No tree to attach; not running tests\n" , __func__); -return -ENODATA ; - } - -/* -* This lock normally encloses of_resolve_phandles() -*/ -of_overlay_mutex_lock (); - - rc =of_resolve_phandles (unittest_data_node); -if (rc) { -pr_err ("%s: Failed to resolve phandles (rc=%i)\n" , __func__, rc); -of_overlay_mutex_unlock (); -return -EINVAL ; - } - -if (!of_root) { - of_root = unittest_data_node; -for_each_of_allnodes (np) -__of_attach_node_sysfs (np); - of_aliases =of_find_node_by_path ("/aliases" ); - of_chosen =of_find_node_by_path ("/chosen" ); -of_overlay_mutex_unlock (); -return 0 ; - } - -/* attach the sub-tree to live tree */ - np = unittest_data_node->child; -while (np) { -struct device_node *next = np->sibling; - - np->parent = of_root; -attach_node_and_children (np); - np = next; - } - -of_overlay_mutex_unlock (); - -return 0 ; -} -#ifdef CONFIG_OF_OVERLAY static int overlay_data_apply (const char *overlay_name,int *overlay_id);@@ -2563,8 +2222,6 @@ static int of_test_init(struct kunit *test)static struct kunit_case of_test_cases[] = {KUNIT_CASE (of_unittest_check_tree_linkage),KUNIT_CASE (of_unittest_check_phandles), -KUNIT_CASE (of_unittest_find_node_by_name), -KUNIT_CASE (of_unittest_dynamic),KUNIT_CASE (of_unittest_parse_phandle_with_args),KUNIT_CASE (of_unittest_parse_phandle_with_args_map),KUNIT_CASE (of_unittest_printf),
Re: [RFC v4 16/17] of: unittest: split out a couple of test cases from unittest
Quoted 9 lines · Frank Rowand
Quoted 4 lines · Brendan Higgins
Split out a couple of test cases that these features in
base.cfrom theunittest.cmonolith. The intention is that we will eventually split out all test cases and group them together based on what portion of device tree they test.I still object to this patch. I do not want this code scattered into additional files.
Sure, no problem. I will remove this from future revisions.
RFC v4 17/17 of: unittest: split up some super large test cases
In reply to Brendan HigginsSplit up the super large test cases of_unittest_find_node_by_name and of_unittest_dynamic into properly sized and defined test cases.
--- drivers/of/base-test.c | 297 ++++++++++++++++++++++++++++++++++------- 1 file changed, 249 insertions(+), 48 deletions(-)
drivers/of/base-test.c +249 -48
@@ -8,10 +8,10 @@#include "test-common.h" -static void of_unittest_find_node_by_name (struct kunit *test) +static void of_test_find_node_by_name_basic (struct kunit *test) {struct device_node *np; -const char *options, *name; +const char *name; np =of_find_node_by_path ("/testcase-data" ); name =kasprintf (GFP_KERNEL ,"%pOF" , np);
@@ -20,11 +20,21 @@ static void of_unittest_find_node_by_name(struct kunit *test) "find /testcase-data failed\n");of_node_put (np);kfree (name); +} +static void of_test_find_node_by_name_trailing_slash (struct kunit *test) +{/* Test if trailing '/' works */ KUNIT_EXPECT_EQ_MSG (test,of_find_node_by_path ("/testcase-data/" ),NULL ,"trailing '/' on /testcase-data/ should fail\n" ); +} + +static void of_test_find_node_by_name_multiple_components (struct kunit *test) +{ +struct device_node *np; +const char *name; + np =of_find_node_by_path ("/testcase-data/phandle-tests/consumer-a" );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); name =kasprintf (GFP_KERNEL ,"%pOF" , np);
@@ -33,6 +43,12 @@ static void of_unittest_find_node_by_name(struct kunit *test) "find /testcase-data/phandle-tests/consumer-a failed\n");of_node_put (np);kfree (name); +} + +static void of_test_find_node_by_name_with_alias (struct kunit *test) +{ +struct device_node *np; +const char *name; np =of_find_node_by_path ("testcase-alias" );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np);
@@ -41,10 +57,23 @@ static void of_unittest_find_node_by_name(struct kunit *test) "find testcase-alias failed\n");of_node_put (np);kfree (name); +} +static void of_test_find_node_by_name_with_alias_and_slash (struct kunit *test) +{/* Test if trailing '/' works on aliases */ KUNIT_EXPECT_EQ_MSG (test,of_find_node_by_path ("testcase-alias/" ),NULL , -"trailing '/' on testcase-alias/ should fail\n" ); +"trailing '/' on testcase-alias/ should fail\n" ); +} + +/* +* TODO([email protected]): This looks like a duplicate of +* of_test_find_node_by_name_multiple_components +*/ +static void of_test_find_node_by_name_multiple_components_2 (struct kunit *test) +{ +struct device_node *np; +const char *name; np =of_find_node_by_path ("testcase-alias/phandle-tests/consumer-a" );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np);
@@ -54,17 +83,33 @@ static void of_unittest_find_node_by_name(struct kunit *test) "find testcase-alias/phandle-tests/consumer-a failed\n");of_node_put (np);kfree (name); +} + +static void of_test_find_node_by_name_missing_path (struct kunit *test) +{ +struct device_node *np;KUNIT_EXPECT_EQ_MSG ( test, np =of_find_node_by_path ("/testcase-data/missing-path" ),NULL ,"non-existent path returned node %pOF\n" , np);of_node_put (np); +} + +static void of_test_find_node_by_name_missing_alias (struct kunit *test) +{ +struct device_node *np;KUNIT_EXPECT_EQ_MSG ( test, np =of_find_node_by_path ("missing-alias" ),NULL ,"non-existent alias returned node %pOF\n" , np);of_node_put (np); +} + +static void of_test_find_node_by_name_missing_alias_with_relative_path ( +struct kunit *test) +{ +struct device_node *np; KUNIT_EXPECT_EQ_MSG( test,
@@ -72,12 +117,24 @@ static void of_unittest_find_node_by_name(struct kunit *test)"non-existent alias with relative path returned node %pOF\n" , np);of_node_put (np); +} + +static void of_test_find_node_by_name_with_option (struct kunit *test) +{ +struct device_node *np; +const char *options; np =of_find_node_opts_by_path ("/testcase-data:testoption" , &options);KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np);KUNIT_EXPECT_STREQ_MSG (test,"testoption" , options,"option path test failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_with_option_and_slash (struct kunit *test) +{ +struct device_node *np; +const char *options; np =of_find_node_opts_by_path ("/testcase-data:test/option" , &options);KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np);
@@ -90,11 +147,22 @@ static void of_unittest_find_node_by_name(struct kunit *test)KUNIT_EXPECT_STREQ_MSG (test,"test/option" , options,"option path test, subcase #2 failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_with_null_option (struct kunit *test) +{ +struct device_node *np; np =of_find_node_opts_by_path ("/testcase-data:testoption" ,NULL );KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG (test, np,"NULL option path test failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_with_option_alias (struct kunit *test) +{ +struct device_node *np; +const char *options; np =of_find_node_opts_by_path ("testcase-alias:testaliasoption" , &options);
@@ -102,6 +170,13 @@ static void of_unittest_find_node_by_name(struct kunit *test)KUNIT_EXPECT_STREQ_MSG (test,"testaliasoption" , options,"option alias path test failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_with_option_alias_and_slash ( +struct kunit *test) +{ +struct device_node *np; +const char *options; np =of_find_node_opts_by_path ("testcase-alias:test/alias/option" , &options);
@@ -109,11 +184,22 @@ static void of_unittest_find_node_by_name(struct kunit *test)KUNIT_EXPECT_STREQ_MSG (test,"test/alias/option" , options,"option alias path test, subcase #1 failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_with_null_option_alias (struct kunit *test) +{ +struct device_node *np; np =of_find_node_opts_by_path ("testcase-alias:testaliasoption" ,NULL );KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG ( test, np,"NULL option alias path test failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_option_clearing (struct kunit *test) +{ +struct device_node *np; +const char *options; options ="testoption" ; np =of_find_node_opts_by_path ("testcase-alias" , &options);
@@ -121,6 +207,12 @@ static void of_unittest_find_node_by_name(struct kunit *test)KUNIT_EXPECT_EQ_MSG (test, options,NULL ,"option clearing test failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_option_clearing_root (struct kunit *test) +{ +struct device_node *np; +const char *options; options ="testoption" ; np =of_find_node_opts_by_path ("/" , &options);
@@ -130,65 +222,147 @@ static void of_unittest_find_node_by_name(struct kunit *test)of_node_put (np); } -static void of_unittest_dynamic (struct kunit *test) +static int of_test_find_node_by_name_init (struct kunit *test) { +/* adding data for unittest */ +KUNIT_ASSERT_EQ (test,0 ,unittest_data_add ()); + +if (!of_aliases) + of_aliases =of_find_node_by_path ("/aliases" ); + +KUNIT_ASSERT_NOT_ERR_OR_NULL (test,of_find_node_by_path ( +"/testcase-data/phandle-tests/consumer-a" )); + +return 0 ; +} + +static struct kunit_case of_test_find_node_by_name_cases[] = { +KUNIT_CASE (of_test_find_node_by_name_basic), +KUNIT_CASE (of_test_find_node_by_name_trailing_slash), +KUNIT_CASE (of_test_find_node_by_name_multiple_components), +KUNIT_CASE (of_test_find_node_by_name_with_alias), +KUNIT_CASE (of_test_find_node_by_name_with_alias_and_slash), +KUNIT_CASE (of_test_find_node_by_name_multiple_components_2), +KUNIT_CASE (of_test_find_node_by_name_missing_path), +KUNIT_CASE (of_test_find_node_by_name_missing_alias), +KUNIT_CASE (of_test_find_node_by_name_missing_alias_with_relative_path), +KUNIT_CASE (of_test_find_node_by_name_with_option), +KUNIT_CASE (of_test_find_node_by_name_with_option_and_slash), +KUNIT_CASE (of_test_find_node_by_name_with_null_option), +KUNIT_CASE (of_test_find_node_by_name_with_option_alias), +KUNIT_CASE (of_test_find_node_by_name_with_option_alias_and_slash), +KUNIT_CASE (of_test_find_node_by_name_with_null_option_alias), +KUNIT_CASE (of_test_find_node_by_name_option_clearing), +KUNIT_CASE (of_test_find_node_by_name_option_clearing_root), + {}, +}; + +static struct kunit_module of_test_find_node_by_name_module = { + .name ="of-test-find-node-by-name" , + .init = of_test_find_node_by_name_init, + .test_cases = of_test_find_node_by_name_cases, +}; +module_test (of_test_find_node_by_name_module); + +struct of_test_dynamic_context {struct device_node *np; -struct property *prop; +struct property *prop0; +struct property *prop1; +}; - np =of_find_node_by_path ("/testcase-data" ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +static void of_test_dynamic_basic (struct kunit *test) +{ +struct of_test_dynamic_context *ctx = test->priv; +struct device_node *np = ctx->np; +struct property *prop0 = ctx->prop0; -/* Array of 4 properties for the purpose of testing */ - prop =kcalloc (4 ,sizeof (*prop),GFP_KERNEL ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, prop); +/* Add a new property - should pass*/ + prop0->name ="new-property" ; + prop0->value ="new-property-data" ; + prop0->length =strlen (prop0->value) +1 ; +KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop0),0 , +"Adding a new property failed\n" ); + +/* Test that we can remove a property */ +KUNIT_EXPECT_EQ (test,of_remove_property (np, prop0),0 ); +} + +static void of_test_dynamic_add_existing_property (struct kunit *test) +{ +struct of_test_dynamic_context *ctx = test->priv; +struct device_node *np = ctx->np; +struct property *prop0 = ctx->prop0, *prop1 = ctx->prop1;/* Add a new property - should pass*/ - prop->name ="new-property" ; - prop->value ="new-property-data" ; - prop->length =strlen (prop->value) +1 ; -KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop),0 , + prop0->name ="new-property" ; + prop0->value ="new-property-data" ; + prop0->length =strlen (prop0->value) +1 ; +KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop0),0 ,"Adding a new property failed\n" );/* Try to add an existing property - should fail */ - prop++; - prop->name ="new-property" ; - prop->value ="new-property-data-should-fail" ; - prop->length =strlen (prop->value) +1 ; -KUNIT_EXPECT_NE_MSG (test,of_add_property (np, prop),0 , + prop1->name ="new-property" ; + prop1->value ="new-property-data-should-fail" ; + prop1->length =strlen (prop1->value) +1 ; +KUNIT_EXPECT_NE_MSG (test,of_add_property (np, prop1),0 ,"Adding an existing property should have failed\n" ); +} + +static void of_test_dynamic_modify_existing_property (struct kunit *test) +{ +struct of_test_dynamic_context *ctx = test->priv; +struct device_node *np = ctx->np; +struct property *prop0 = ctx->prop0, *prop1 = ctx->prop1; + +/* Add a new property - should pass*/ + prop0->name ="new-property" ; + prop0->value ="new-property-data" ; + prop0->length =strlen (prop0->value) +1 ; +KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop0),0 , +"Adding a new property failed\n" );/* Try to modify an existing property - should pass */ - prop->value ="modify-property-data-should-pass" ; - prop->length =strlen (prop->value) +1 ; -KUNIT_EXPECT_EQ_MSG ( - test,of_update_property (np, prop),0 , -"Updating an existing property should have passed\n" ); + prop1->name ="new-property" ; + prop1->value ="modify-property-data-should-pass" ; + prop1->length =strlen (prop1->value) +1 ; +KUNIT_EXPECT_EQ_MSG (test,of_update_property (np, prop1),0 , +"Updating an existing property should have passed\n" ); +} + +static void of_test_dynamic_modify_non_existent_property (struct kunit *test) +{ +struct of_test_dynamic_context *ctx = test->priv; +struct device_node *np = ctx->np; +struct property *prop0 = ctx->prop0;/* Try to modify non-existent property - should pass*/ - prop++; - prop->name ="modify-property" ; - prop->value ="modify-missing-property-data-should-pass" ; - prop->length =strlen (prop->value) +1 ; -KUNIT_EXPECT_EQ_MSG (test,of_update_property (np, prop),0 , + prop0->name ="modify-property" ; + prop0->value ="modify-missing-property-data-should-pass" ; + prop0->length =strlen (prop0->value) +1 ; +KUNIT_EXPECT_EQ_MSG (test,of_update_property (np, prop0),0 ,"Updating a missing property should have passed\n" ); +} -/* Remove property - should pass */ -KUNIT_EXPECT_EQ_MSG (test,of_remove_property (np, prop),0 , -"Removing a property should have passed\n" ); +static void of_test_dynamic_large_property (struct kunit *test) +{ +struct of_test_dynamic_context *ctx = test->priv; +struct device_node *np = ctx->np; +struct property *prop0 = ctx->prop0;/* Adding very large property - should pass */ - prop++; - prop->name ="large-property-PAGE_SIZEx8" ; - prop->length =PAGE_SIZE *8 ; - prop->value =kzalloc (prop->length,GFP_KERNEL ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, prop->value); -KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop),0 , + prop0->name ="large-property-PAGE_SIZEx8" ; + prop0->length =PAGE_SIZE *8 ; + prop0->value =kunit_kzalloc (test, prop0->length,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, prop0->value); + +KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop0),0 ,"Adding a large property should have passed\n" ); } -static int of_test_init (struct kunit *test) +static int of_test_dynamic_init (struct kunit *test) { -/* adding data for unittest */ +struct of_test_dynamic_context *ctx; +KUNIT_ASSERT_EQ (test,0 ,unittest_data_add ());if (!of_aliases)
@@ -197,18 +371,45 @@ static int of_test_init(struct kunit *test)KUNIT_ASSERT_NOT_ERR_OR_NULL (test,of_find_node_by_path ("/testcase-data/phandle-tests/consumer-a" )); + ctx =kunit_kzalloc (test,sizeof (*ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx); + test->priv = ctx; + + ctx->np =of_find_node_by_path ("/testcase-data" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx->np); + + ctx->prop0 =kunit_kzalloc (test,sizeof (*ctx->prop0),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx->prop0); + + ctx->prop1 =kunit_kzalloc (test,sizeof (*ctx->prop1),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx->prop1); +return 0 ; } -static struct kunit_case of_test_cases[] = { -KUNIT_CASE (of_unittest_find_node_by_name), -KUNIT_CASE (of_unittest_dynamic), +static void of_test_dynamic_exit (struct kunit *test) +{ +struct of_test_dynamic_context *ctx = test->priv; +struct device_node *np = ctx->np; + +of_remove_property (np, ctx->prop0); +of_remove_property (np, ctx->prop1); +of_node_put (np); +} + +static struct kunit_case of_test_dynamic_cases[] = { +KUNIT_CASE (of_test_dynamic_basic), +KUNIT_CASE (of_test_dynamic_add_existing_property), +KUNIT_CASE (of_test_dynamic_modify_existing_property), +KUNIT_CASE (of_test_dynamic_modify_non_existent_property), +KUNIT_CASE (of_test_dynamic_large_property), {}, }; -static struct kunit_module of_test_module = { - .name ="of-base-test" , - .init = of_test_init, - .test_cases = of_test_cases, +static struct kunit_module of_test_dynamic_module = { + .name ="of-dynamic-test" , + .init = of_test_dynamic_init, + .exit = of_test_dynamic_exit, + .test_cases = of_test_dynamic_cases, }; -module_test (of_test_module); +module_test (of_test_dynamic_module);
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
Re: [RFC v4 17/17] of: unittest: split up some super large test cases
Quoted 2 lines · Brendan Higgins
Split up the super large test cases
of_unittest_find_node_by_nameandof_unittest_dynamicinto properly sized and defined test cases.
I also still object to this patch.
-Frank
Quoted 455 lines
Signed-off-by Brendan Higgins <[email protected]>--- drivers/of/base-test.c | 297 ++++++++++++++++++++++++++++++++++------- 1 file changed, 249 insertions(+), 48 deletions(-)drivers/of/base-test.c +249
-48@@ -8,10 +8,10 @@#include "test-common.h" -static void of_unittest_find_node_by_name (struct kunit *test) +static void of_test_find_node_by_name_basic (struct kunit *test) {struct device_node *np; -const char *options, *name; +const char *name; np =of_find_node_by_path ("/testcase-data" ); name =kasprintf (GFP_KERNEL ,"%pOF" , np);@@ -20,11 +20,21 @@ static void of_unittest_find_node_by_name(struct kunit *test) "find /testcase-data failed\n");of_node_put (np);kfree (name); +} +static void of_test_find_node_by_name_trailing_slash (struct kunit *test) +{/* Test if trailing '/' works */ KUNIT_EXPECT_EQ_MSG (test,of_find_node_by_path ("/testcase-data/" ),NULL ,"trailing '/' on /testcase-data/ should fail\n" ); +} + +static void of_test_find_node_by_name_multiple_components (struct kunit *test) +{ +struct device_node *np; +const char *name; + np =of_find_node_by_path ("/testcase-data/phandle-tests/consumer-a" );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); name =kasprintf (GFP_KERNEL ,"%pOF" , np);@@ -33,6 +43,12 @@ static void of_unittest_find_node_by_name(struct kunit *test) "find /testcase-data/phandle-tests/consumer-a failed\n");of_node_put (np);kfree (name); +} + +static void of_test_find_node_by_name_with_alias (struct kunit *test) +{ +struct device_node *np; +const char *name; np =of_find_node_by_path ("testcase-alias" );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np);@@ -41,10 +57,23 @@ static void of_unittest_find_node_by_name(struct kunit *test) "find testcase-alias failed\n");of_node_put (np);kfree (name); +} +static void of_test_find_node_by_name_with_alias_and_slash (struct kunit *test) +{/* Test if trailing '/' works on aliases */ KUNIT_EXPECT_EQ_MSG (test,of_find_node_by_path ("testcase-alias/" ),NULL , -"trailing '/' on testcase-alias/ should fail\n" ); +"trailing '/' on testcase-alias/ should fail\n" ); +} + +/* +* TODO([email protected]): This looks like a duplicate of +* of_test_find_node_by_name_multiple_components +*/ +static void of_test_find_node_by_name_multiple_components_2 (struct kunit *test) +{ +struct device_node *np; +const char *name; np =of_find_node_by_path ("testcase-alias/phandle-tests/consumer-a" );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np);@@ -54,17 +83,33 @@ static void of_unittest_find_node_by_name(struct kunit *test) "find testcase-alias/phandle-tests/consumer-a failed\n");of_node_put (np);kfree (name); +} + +static void of_test_find_node_by_name_missing_path (struct kunit *test) +{ +struct device_node *np;KUNIT_EXPECT_EQ_MSG ( test, np =of_find_node_by_path ("/testcase-data/missing-path" ),NULL ,"non-existent path returned node %pOF\n" , np);of_node_put (np); +} + +static void of_test_find_node_by_name_missing_alias (struct kunit *test) +{ +struct device_node *np;KUNIT_EXPECT_EQ_MSG ( test, np =of_find_node_by_path ("missing-alias" ),NULL ,"non-existent alias returned node %pOF\n" , np);of_node_put (np); +} + +static void of_test_find_node_by_name_missing_alias_with_relative_path ( +struct kunit *test) +{ +struct device_node *np; KUNIT_EXPECT_EQ_MSG( test,@@ -72,12 +117,24 @@ static void of_unittest_find_node_by_name(struct kunit *test)"non-existent alias with relative path returned node %pOF\n" , np);of_node_put (np); +} + +static void of_test_find_node_by_name_with_option (struct kunit *test) +{ +struct device_node *np; +const char *options; np =of_find_node_opts_by_path ("/testcase-data:testoption" , &options);KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np);KUNIT_EXPECT_STREQ_MSG (test,"testoption" , options,"option path test failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_with_option_and_slash (struct kunit *test) +{ +struct device_node *np; +const char *options; np =of_find_node_opts_by_path ("/testcase-data:test/option" , &options);KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np);@@ -90,11 +147,22 @@ static void of_unittest_find_node_by_name(struct kunit *test)KUNIT_EXPECT_STREQ_MSG (test,"test/option" , options,"option path test, subcase #2 failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_with_null_option (struct kunit *test) +{ +struct device_node *np; np =of_find_node_opts_by_path ("/testcase-data:testoption" ,NULL );KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG (test, np,"NULL option path test failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_with_option_alias (struct kunit *test) +{ +struct device_node *np; +const char *options; np =of_find_node_opts_by_path ("testcase-alias:testaliasoption" , &options);@@ -102,6 +170,13 @@ static void of_unittest_find_node_by_name(struct kunit *test)KUNIT_EXPECT_STREQ_MSG (test,"testaliasoption" , options,"option alias path test failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_with_option_alias_and_slash ( +struct kunit *test) +{ +struct device_node *np; +const char *options; np =of_find_node_opts_by_path ("testcase-alias:test/alias/option" , &options);@@ -109,11 +184,22 @@ static void of_unittest_find_node_by_name(struct kunit *test)KUNIT_EXPECT_STREQ_MSG (test,"test/alias/option" , options,"option alias path test, subcase #1 failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_with_null_option_alias (struct kunit *test) +{ +struct device_node *np; np =of_find_node_opts_by_path ("testcase-alias:testaliasoption" ,NULL );KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG ( test, np,"NULL option alias path test failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_option_clearing (struct kunit *test) +{ +struct device_node *np; +const char *options; options ="testoption" ; np =of_find_node_opts_by_path ("testcase-alias" , &options);@@ -121,6 +207,12 @@ static void of_unittest_find_node_by_name(struct kunit *test)KUNIT_EXPECT_EQ_MSG (test, options,NULL ,"option clearing test failed\n" );of_node_put (np); +} + +static void of_test_find_node_by_name_option_clearing_root (struct kunit *test) +{ +struct device_node *np; +const char *options; options ="testoption" ; np =of_find_node_opts_by_path ("/" , &options);@@ -130,65 +222,147 @@ static void of_unittest_find_node_by_name(struct kunit *test)of_node_put (np); } -static void of_unittest_dynamic (struct kunit *test) +static int of_test_find_node_by_name_init (struct kunit *test) { +/* adding data for unittest */ +KUNIT_ASSERT_EQ (test,0 ,unittest_data_add ()); + +if (!of_aliases) + of_aliases =of_find_node_by_path ("/aliases" ); + +KUNIT_ASSERT_NOT_ERR_OR_NULL (test,of_find_node_by_path ( +"/testcase-data/phandle-tests/consumer-a" )); + +return 0 ; +} + +static struct kunit_case of_test_find_node_by_name_cases[] = { +KUNIT_CASE (of_test_find_node_by_name_basic), +KUNIT_CASE (of_test_find_node_by_name_trailing_slash), +KUNIT_CASE (of_test_find_node_by_name_multiple_components), +KUNIT_CASE (of_test_find_node_by_name_with_alias), +KUNIT_CASE (of_test_find_node_by_name_with_alias_and_slash), +KUNIT_CASE (of_test_find_node_by_name_multiple_components_2), +KUNIT_CASE (of_test_find_node_by_name_missing_path), +KUNIT_CASE (of_test_find_node_by_name_missing_alias), +KUNIT_CASE (of_test_find_node_by_name_missing_alias_with_relative_path), +KUNIT_CASE (of_test_find_node_by_name_with_option), +KUNIT_CASE (of_test_find_node_by_name_with_option_and_slash), +KUNIT_CASE (of_test_find_node_by_name_with_null_option), +KUNIT_CASE (of_test_find_node_by_name_with_option_alias), +KUNIT_CASE (of_test_find_node_by_name_with_option_alias_and_slash), +KUNIT_CASE (of_test_find_node_by_name_with_null_option_alias), +KUNIT_CASE (of_test_find_node_by_name_option_clearing), +KUNIT_CASE (of_test_find_node_by_name_option_clearing_root), + {}, +}; + +static struct kunit_module of_test_find_node_by_name_module = { + .name ="of-test-find-node-by-name" , + .init = of_test_find_node_by_name_init, + .test_cases = of_test_find_node_by_name_cases, +}; +module_test (of_test_find_node_by_name_module); + +struct of_test_dynamic_context {struct device_node *np; -struct property *prop; +struct property *prop0; +struct property *prop1; +}; - np =of_find_node_by_path ("/testcase-data" ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, np); +static void of_test_dynamic_basic (struct kunit *test) +{ +struct of_test_dynamic_context *ctx = test->priv; +struct device_node *np = ctx->np; +struct property *prop0 = ctx->prop0; -/* Array of 4 properties for the purpose of testing */ - prop =kcalloc (4 ,sizeof (*prop),GFP_KERNEL ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, prop); +/* Add a new property - should pass*/ + prop0->name ="new-property" ; + prop0->value ="new-property-data" ; + prop0->length =strlen (prop0->value) +1 ; +KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop0),0 , +"Adding a new property failed\n" ); + +/* Test that we can remove a property */ +KUNIT_EXPECT_EQ (test,of_remove_property (np, prop0),0 ); +} + +static void of_test_dynamic_add_existing_property (struct kunit *test) +{ +struct of_test_dynamic_context *ctx = test->priv; +struct device_node *np = ctx->np; +struct property *prop0 = ctx->prop0, *prop1 = ctx->prop1;/* Add a new property - should pass*/ - prop->name ="new-property" ; - prop->value ="new-property-data" ; - prop->length =strlen (prop->value) +1 ; -KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop),0 , + prop0->name ="new-property" ; + prop0->value ="new-property-data" ; + prop0->length =strlen (prop0->value) +1 ; +KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop0),0 ,"Adding a new property failed\n" );/* Try to add an existing property - should fail */ - prop++; - prop->name ="new-property" ; - prop->value ="new-property-data-should-fail" ; - prop->length =strlen (prop->value) +1 ; -KUNIT_EXPECT_NE_MSG (test,of_add_property (np, prop),0 , + prop1->name ="new-property" ; + prop1->value ="new-property-data-should-fail" ; + prop1->length =strlen (prop1->value) +1 ; +KUNIT_EXPECT_NE_MSG (test,of_add_property (np, prop1),0 ,"Adding an existing property should have failed\n" ); +} + +static void of_test_dynamic_modify_existing_property (struct kunit *test) +{ +struct of_test_dynamic_context *ctx = test->priv; +struct device_node *np = ctx->np; +struct property *prop0 = ctx->prop0, *prop1 = ctx->prop1; + +/* Add a new property - should pass*/ + prop0->name ="new-property" ; + prop0->value ="new-property-data" ; + prop0->length =strlen (prop0->value) +1 ; +KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop0),0 , +"Adding a new property failed\n" );/* Try to modify an existing property - should pass */ - prop->value ="modify-property-data-should-pass" ; - prop->length =strlen (prop->value) +1 ; -KUNIT_EXPECT_EQ_MSG ( - test,of_update_property (np, prop),0 , -"Updating an existing property should have passed\n" ); + prop1->name ="new-property" ; + prop1->value ="modify-property-data-should-pass" ; + prop1->length =strlen (prop1->value) +1 ; +KUNIT_EXPECT_EQ_MSG (test,of_update_property (np, prop1),0 , +"Updating an existing property should have passed\n" ); +} + +static void of_test_dynamic_modify_non_existent_property (struct kunit *test) +{ +struct of_test_dynamic_context *ctx = test->priv; +struct device_node *np = ctx->np; +struct property *prop0 = ctx->prop0;/* Try to modify non-existent property - should pass*/ - prop++; - prop->name ="modify-property" ; - prop->value ="modify-missing-property-data-should-pass" ; - prop->length =strlen (prop->value) +1 ; -KUNIT_EXPECT_EQ_MSG (test,of_update_property (np, prop),0 , + prop0->name ="modify-property" ; + prop0->value ="modify-missing-property-data-should-pass" ; + prop0->length =strlen (prop0->value) +1 ; +KUNIT_EXPECT_EQ_MSG (test,of_update_property (np, prop0),0 ,"Updating a missing property should have passed\n" ); +} -/* Remove property - should pass */ -KUNIT_EXPECT_EQ_MSG (test,of_remove_property (np, prop),0 , -"Removing a property should have passed\n" ); +static void of_test_dynamic_large_property (struct kunit *test) +{ +struct of_test_dynamic_context *ctx = test->priv; +struct device_node *np = ctx->np; +struct property *prop0 = ctx->prop0;/* Adding very large property - should pass */ - prop++; - prop->name ="large-property-PAGE_SIZEx8" ; - prop->length =PAGE_SIZE *8 ; - prop->value =kzalloc (prop->length,GFP_KERNEL ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, prop->value); -KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop),0 , + prop0->name ="large-property-PAGE_SIZEx8" ; + prop0->length =PAGE_SIZE *8 ; + prop0->value =kunit_kzalloc (test, prop0->length,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, prop0->value); + +KUNIT_EXPECT_EQ_MSG (test,of_add_property (np, prop0),0 ,"Adding a large property should have passed\n" ); } -static int of_test_init (struct kunit *test) +static int of_test_dynamic_init (struct kunit *test) { -/* adding data for unittest */ +struct of_test_dynamic_context *ctx; +KUNIT_ASSERT_EQ (test,0 ,unittest_data_add ());if (!of_aliases)@@ -197,18 +371,45 @@ static int of_test_init(struct kunit *test)KUNIT_ASSERT_NOT_ERR_OR_NULL (test,of_find_node_by_path ("/testcase-data/phandle-tests/consumer-a" )); + ctx =kunit_kzalloc (test,sizeof (*ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx); + test->priv = ctx; + + ctx->np =of_find_node_by_path ("/testcase-data" ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx->np); + + ctx->prop0 =kunit_kzalloc (test,sizeof (*ctx->prop0),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx->prop0); + + ctx->prop1 =kunit_kzalloc (test,sizeof (*ctx->prop1),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx->prop1); +return 0 ; } -static struct kunit_case of_test_cases[] = { -KUNIT_CASE (of_unittest_find_node_by_name), -KUNIT_CASE (of_unittest_dynamic), +static void of_test_dynamic_exit (struct kunit *test) +{ +struct of_test_dynamic_context *ctx = test->priv; +struct device_node *np = ctx->np; + +of_remove_property (np, ctx->prop0); +of_remove_property (np, ctx->prop1); +of_node_put (np); +} + +static struct kunit_case of_test_dynamic_cases[] = { +KUNIT_CASE (of_test_dynamic_basic), +KUNIT_CASE (of_test_dynamic_add_existing_property), +KUNIT_CASE (of_test_dynamic_modify_existing_property), +KUNIT_CASE (of_test_dynamic_modify_non_existent_property), +KUNIT_CASE (of_test_dynamic_large_property), {}, }; -static struct kunit_module of_test_module = { - .name ="of-base-test" , - .init = of_test_init, - .test_cases = of_test_cases, +static struct kunit_module of_test_dynamic_module = { + .name ="of-dynamic-test" , + .init = of_test_dynamic_init, + .exit = of_test_dynamic_exit, + .test_cases = of_test_dynamic_cases, }; -module_test (of_test_module); +module_test (of_test_dynamic_module);
Re: [RFC v4 17/17] of: unittest: split up some super large test cases
Quoted 6 lines · Frank Rowand
Quoted 2 lines · Brendan Higgins
Split up the super large test cases
of_unittest_find_node_by_nameandof_unittest_dynamicinto properly sized and defined test cases.I also still object to this patch.
I figured. Will drop.
Quoted 50 lines · Brendan Higgins
This patch set proposes KUnit, a lightweight unit testing and mocking framework for the Linux kernel.
Unlike Autotest and kselftest, KUnit is a true unit testing framework; it does not require installing the kernel on a test machine or in a VM and does not require tests to be written in userspace running on a host kernel. Additionally, KUnit is fast: From invocation to completion KUnit can run several dozen tests in under a second. Currently, the entire KUnit test suite for KUnit runs in under a second from the initial invocation (build time excluded).
KUnit is heavily inspired by JUnit, Python's unittest.mock, and Googletest/Googlemock for C++. KUnit provides facilities for defining unit test cases, grouping related test cases into test suites, providing common infrastructure for running tests, mocking, spying, and much more.
## What's so special about unit testing?
A unit test is supposed to test a single unit of code in isolation, hence the name. There should be no dependencies outside the control of the test; this means no external dependencies, which makes tests orders of magnitudes faster. Likewise, since there are no external dependencies, there are no hoops to jump through to run the tests. Additionally, this makes unit tests deterministic: a failing unit test always indicates a problem. Finally, because unit tests necessarily have finer granularity, they are able to test all code paths easily solving the classic problem of difficulty in exercising error handling code.
## Is KUnit trying to replace other testing frameworks for the kernel?
No. Most existing tests for the Linux kernel are end-to-end tests, which have their place. A well tested system has lots of unit tests, a reasonable number of integration tests, and some end-to-end tests. KUnit is just trying to address the unit test space which is currently not being addressed.
## More information on KUnit
There is a bunch of documentation near the end of this patch set that describes how to use KUnit and best practices for writing unit tests. For convenience I am hosting the compiled docs here:
https://google.github.io/kunit-docs/third_party/kernel/docs/
Additionally for convenience, I have applied these patches to a branch:
https://kunit.googlesource.com/linux/+/kunit/rfc/5.0-rc5/v4
The repo may be cloned with:
git clone https://kunit.googlesource.com/linux
This patchset is on the kunit/rfc/5.0-rc5/v4 branch.## Changes Since Last Version
Quoted 11 lines
- Got KUnit working on (hypothetically) all architectures (tested on x86), as per Rob's (and other's) request
- Punting all KUnit features/patches depending on UML for now.
- Broke out UML specific support intoarch/um/*as per "[RFC v3 01/19]
kunit: test: add KUnit test runner core", as requested by Luis.
- Added support tokunit_toolto allow it to build kernels in external directories, as suggested by Kieran.
- Added a UML defconfig, and a config fragment for KUnit as suggested by Kieran and Luis.
- Cleaned up, and reformatted a bunch of stuff.
I have not read through the patches in any detail. I have read some of the code to try to understand the patches to the devicetree unit tests. So that may limit how valid my comments below are.
I found the code difficult to read in places where it should have been much simpler to read. Structuring the code in a pseudo object oriented style meant that everywhere in a code path that I encountered a dynamic function call, I had to go find where that dynamic function call was initialized (and being the cautious person that I am, verify that no where else was the value of that dynamic function call). With primitive vi and tags, that search would have instead just been a simple key press (or at worst a few keys) if hard coded function calls were done instead of dynamic function calls. In the code paths that I looked at, I did not see any case of a dynamic function being anything other than the value it was originally initialized as. There may be such cases, I did not read the entire patch set. There may also be cases envisioned in the architects mind of how this flexibility may be of future value. Dunno.
-Frank
On Mon, Feb 18, 2019 at 12:02 PM Frank Rowand <[email protected]> wrote:
<snip>
Quoted 3 lines
I have not read through the patches in any detail. I have read some of the code to try to understand the patches to the devicetree unit tests. So that may limit how valid my comments below are.
No problem.
Quoted 15 lines
I found the code difficult to read in places where it should have been much simpler to read. Structuring the code in a pseudo object oriented style meant that everywhere in a code path that I encountered a dynamic function call, I had to go find where that dynamic function call was initialized (and being the cautious person that I am, verify that no where else was the value of that dynamic function call). With primitive vi and tags, that search would have instead just been a simple key press (or at worst a few keys) if hard coded function calls were done instead of dynamic function calls. In the code paths that I looked at, I did not see any case of a dynamic function being anything other than the value it was originally initialized as. There may be such cases, I did not read the entire patch set. There may also be cases envisioned in the architects mind of how this flexibility may be of future value. Dunno.
Yeah, a lot of it is intended to make architecture specific implementations and some other future work easier. Some of it is also for testing purposes. Admittedly some is for neither reason, but given the heavy usage elsewhere, I figured there was no harm since it was all private internal usage anyway.
Quoted 24 lines · Brendan Higgins
On Mon, Feb 18, 2019 at 12:02 PM Frank Rowand <[email protected]> wrote:
<snip>Quoted 3 lines
I have not read through the patches in any detail. I have read some of the code to try to understand the patches to the devicetree unit tests. So that may limit how valid my comments below are.
No problem.
Quoted 15 lines
I found the code difficult to read in places where it should have been much simpler to read. Structuring the code in a pseudo object oriented style meant that everywhere in a code path that I encountered a dynamic function call, I had to go find where that dynamic function call was initialized (and being the cautious person that I am, verify that no where else was the value of that dynamic function call). With primitive vi and tags, that search would have instead just been a simple key press (or at worst a few keys) if hard coded function calls were done instead of dynamic function calls. In the code paths that I looked at, I did not see any case of a dynamic function being anything other than the value it was originally initialized as. There may be such cases, I did not read the entire patch set. There may also be cases envisioned in the architects mind of how this flexibility may be of future value. Dunno.
Quoted 6 lines
Yeah, a lot of it is intended to make architecture specific implementations and some other future work easier. Some of it is also for testing purposes. Admittedly some is for neither reason, but given the heavy usage elsewhere, I figured there was no harm since it was all private internal usage anyway.
Increasing the cost for me (and all the other potential code readers) to read the code is harm.
Quoted 32 lines · Frank Rowand
Quoted 24 lines · Brendan Higgins
On Mon, Feb 18, 2019 at 12:02 PM Frank Rowand <[email protected]> wrote:
<snip>Quoted 3 lines
I have not read through the patches in any detail. I have read some of the code to try to understand the patches to the devicetree unit tests. So that may limit how valid my comments below are.
No problem.
Quoted 15 lines
I found the code difficult to read in places where it should have been much simpler to read. Structuring the code in a pseudo object oriented style meant that everywhere in a code path that I encountered a dynamic function call, I had to go find where that dynamic function call was initialized (and being the cautious person that I am, verify that no where else was the value of that dynamic function call). With primitive vi and tags, that search would have instead just been a simple key press (or at worst a few keys) if hard coded function calls were done instead of dynamic function calls. In the code paths that I looked at, I did not see any case of a dynamic function being anything other than the value it was originally initialized as. There may be such cases, I did not read the entire patch set. There may also be cases envisioned in the architects mind of how this flexibility may be of future value. Dunno.
Quoted 6 lines
Yeah, a lot of it is intended to make architecture specific implementations and some other future work easier. Some of it is also for testing purposes. Admittedly some is for neither reason, but given the heavy usage elsewhere, I figured there was no harm since it was all private internal usage anyway.
Quoted 2 lines
Increasing the cost for me (and all the other potential code readers) to read the code is harm.
Dynamic function calls aren't necessary for arch-specific implementations either. See for example arch_kexec_image_load() in kernel/kexec_file.c, which uses a weak symbol that is overriden by arch-specific code. Not everybody likes weak symbols, so another alternative (which admitedly not everybody likes either) is to use a macro with the name of the arch-specific function, as used by arch_kexec_post_alloc_pages() in <linux/kexec.h> for instance.
--
Thiago Jung Bauermann
IBM Linux Technology Center
Quoted 39 lines · Thiago Jung Bauermann
Quoted 32 lines · Frank Rowand
Quoted 24 lines · Brendan Higgins
On Mon, Feb 18, 2019 at 12:02 PM Frank Rowand <[email protected]> wrote:
<snip>Quoted 3 lines
I have not read through the patches in any detail. I have read some of the code to try to understand the patches to the devicetree unit tests. So that may limit how valid my comments below are.
No problem.
Quoted 15 lines
I found the code difficult to read in places where it should have been much simpler to read. Structuring the code in a pseudo object oriented style meant that everywhere in a code path that I encountered a dynamic function call, I had to go find where that dynamic function call was initialized (and being the cautious person that I am, verify that no where else was the value of that dynamic function call). With primitive vi and tags, that search would have instead just been a simple key press (or at worst a few keys) if hard coded function calls were done instead of dynamic function calls. In the code paths that I looked at, I did not see any case of a dynamic function being anything other than the value it was originally initialized as. There may be such cases, I did not read the entire patch set. There may also be cases envisioned in the architects mind of how this flexibility may be of future value. Dunno.
Quoted 6 lines
Yeah, a lot of it is intended to make architecture specific implementations and some other future work easier. Some of it is also for testing purposes. Admittedly some is for neither reason, but given the heavy usage elsewhere, I figured there was no harm since it was all private internal usage anyway.
Quoted 2 lines
Increasing the cost for me (and all the other potential code readers) to read the code is harm.
Quoted 7 lines
Dynamic function calls aren't necessary for arch-specific implementations either. See for example
arch_kexec_image_load()inkernel/kexec_file.c, which uses a weak symbol that is overriden by arch-specific code. Not everybody likes weak symbols, so another alternative (which admitedly not everybody likes either) is to use a macro with the name of the arch-specific function, as used byarch_kexec_post_alloc_pages()in <linux/kexec.h> for instance.
I personally have a strong preference for dynamic function calls over weak symbols or macros, but I can change it if it really makes anyone's eyes bleed.
Quoted 33 lines · Frank Rowand
Quoted 24 lines · Brendan Higgins
On Mon, Feb 18, 2019 at 12:02 PM Frank Rowand <[email protected]> wrote:
<snip>Quoted 3 lines
I have not read through the patches in any detail. I have read some of the code to try to understand the patches to the devicetree unit tests. So that may limit how valid my comments below are.
No problem.
Quoted 15 lines
I found the code difficult to read in places where it should have been much simpler to read. Structuring the code in a pseudo object oriented style meant that everywhere in a code path that I encountered a dynamic function call, I had to go find where that dynamic function call was initialized (and being the cautious person that I am, verify that no where else was the value of that dynamic function call). With primitive vi and tags, that search would have instead just been a simple key press (or at worst a few keys) if hard coded function calls were done instead of dynamic function calls. In the code paths that I looked at, I did not see any case of a dynamic function being anything other than the value it was originally initialized as. There may be such cases, I did not read the entire patch set. There may also be cases envisioned in the architects mind of how this flexibility may be of future value. Dunno.
Quoted 6 lines
Yeah, a lot of it is intended to make architecture specific implementations and some other future work easier. Some of it is also for testing purposes. Admittedly some is for neither reason, but given the heavy usage elsewhere, I figured there was no harm since it was all private internal usage anyway.
Quoted 2 lines
Increasing the cost for me (and all the other potential code readers) to read the code is harm.
You are right. I like the object oriented C style; I didn't think it hurt readability.
In any case, I will go through and replace instances where I am not using it for one of the above reasons.
Quoted 4 lines · Brendan Higgins
This patch set proposes KUnit, a lightweight unit testing and mocking framework for the Linux kernel.
<snip>
Quoted 25 lines
## More information on KUnit
There is a bunch of documentation near the end of this patch set that describes how to use KUnit and best practices for writing unit tests. For convenience I am hosting the compiled docs here:
https://google.github.io/kunit-docs/third_party/kernel/docs/
Additionally for convenience, I have applied these patches to a branch:
https://kunit.googlesource.com/linux/+/kunit/rfc/5.0-rc5/v4
The repo may be cloned with:
git clone https://kunit.googlesource.com/linux
This patchset is on the kunit/rfc/5.0-rc5/v4 branch.## Changes Since Last Version
- Got KUnit working on (hypothetically) all architectures (tested on x86), as per Rob's (and other's) request
- Punting all KUnit features/patches depending on UML for now.
- Broke out UML specific support intoarch/um/*as per "[RFC v3 01/19]
kunit: test: add KUnit test runner core", as requested by Luis.
- Added support tokunit_toolto allow it to build kernels in external directories, as suggested by Kieran.
- Added a UML defconfig, and a config fragment for KUnit as suggested by Kieran and Luis.
- Cleaned up, and reformatted a bunch of stuff.
Quoted 3 lines
--
2.21.0.rc0.258.g878e2cd30e-goog
Someone suggested I should send the next revision out as "PATCH" instead of "RFC" since there seems to be general consensus about everything at a high level, with a couple exceptions.
At this time I am planning on sending the next revision out as "[PATCH v1 00/NN] kunit: introduce KUnit, the Linux kernel unit testing framework". Initially I wasn't sure if the next revision should be "[PATCH v1 ...]" or "[PATCH v5 ...]". Please let me know if you have a strong objection to the former.
In the next revision, I will be dropping the last two of three patches for the DT unit tests as there doesn't seem to be enough features currently available to justify the heavy refactoring I did; however, I will still include the patch that just converts everything over to KUnit without restructuring the test cases:
https://lkml.org/lkml/2019/2/14/1133
I should have the next revision out in a week or so.
_______________________________________________
dri-devel mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 48 lines · Brendan Higgins
Quoted 4 lines · Brendan Higgins
This patch set proposes KUnit, a lightweight unit testing and mocking framework for the Linux kernel.
<snip>
Quoted 25 lines
## More information on KUnit
There is a bunch of documentation near the end of this patch set that describes how to use KUnit and best practices for writing unit tests. For convenience I am hosting the compiled docs here:
https://google.github.io/kunit-docs/third_party/kernel/docs/
Additionally for convenience, I have applied these patches to a branch:
https://kunit.googlesource.com/linux/+/kunit/rfc/5.0-rc5/v4
The repo may be cloned with:
git clone https://kunit.googlesource.com/linux
This patchset is on the kunit/rfc/5.0-rc5/v4 branch.## Changes Since Last Version
- Got KUnit working on (hypothetically) all architectures (tested on x86), as per Rob's (and other's) request
- Punting all KUnit features/patches depending on UML for now.
- Broke out UML specific support intoarch/um/*as per "[RFC v3 01/19]
kunit: test: add KUnit test runner core", as requested by Luis.
- Added support tokunit_toolto allow it to build kernels in external directories, as suggested by Kieran.
- Added a UML defconfig, and a config fragment for KUnit as suggested by Kieran and Luis.
- Cleaned up, and reformatted a bunch of stuff.Quoted 3 lines
--
2.21.0.rc0.258.g878e2cd30e-googSomeone suggested I should send the next revision out as "PATCH" instead of "RFC" since there seems to be general consensus about everything at a high level, with a couple exceptions.
At this time I am planning on sending the next revision out as "[PATCH v1 00/NN] kunit: introduce KUnit, the Linux kernel unit testing framework". Initially I wasn't sure if the next revision should be "[PATCH v1 ...]" or "[PATCH v5 ...]". Please let me know if you have a strong objection to the former.
Quoted 3 lines
In the next revision, I will be dropping the last two of three patches for the DT unit tests as there doesn't seem to be enough features currently available to justify the heavy refactoring I did; however, I
Thank you.
Quoted 3 lines
will still include the patch that just converts everything over to KUnit without restructuring the test cases:
https://lkml.org/lkml/2019/2/14/1133
The link doesn't work for me (don't worry about that), so I'm assuming this is:
[RFC v4 15/17] of: unittest: migrate tests to run on KUnit
The conversation on that patch ended after:
Quoted 5 lines
Quoted 1 line
After adding patch 15, there are a lot of "unittest internal error" messages.
Yeah, I meant to ask you about that. I thought it was due to a change you made, but after further examination, just now, I found it was my fault. Sorry for not mentioning that anywhere. I will fix it in v5.
It is not worth my time to look at patch 15 when it is that broken. So I have not done any review of it.
So no, I think you are still in the RFC stage unless you drop patch 15.
Quoted 3 lines
I should have the next revision out in a week or so.
Quoted 3 lines · Frank Rowand
Quoted 1 line · Brendan Higgins
On Thu, Feb 14, 2019 at 1:38 PM Brendan Higgins
< snip >
Quoted 17 lines
Quoted 10 lines
Someone suggested I should send the next revision out as "PATCH" instead of "RFC" since there seems to be general consensus about everything at a high level, with a couple exceptions.
At this time I am planning on sending the next revision out as "[PATCH v1 00/NN] kunit: introduce KUnit, the Linux kernel unit testing framework". Initially I wasn't sure if the next revision should be "[PATCH v1 ...]" or "[PATCH v5 ...]". Please let me know if you have a strong objection to the former.
Quoted 3 lines
In the next revision, I will be dropping the last two of three patches for the DT unit tests as there doesn't seem to be enough features currently available to justify the heavy refactoring I did; however, I
Thank you.
Quoted 8 lines
Quoted 3 lines
will still include the patch that just converts everything over to KUnit without restructuring the test cases:
https://lkml.org/lkml/2019/2/14/1133The link doesn't work for me (don't worry about that), so I'm assuming this is:
[RFC v4 15/17] of: unittest: migrate tests to run on KUnit
That's correct.
Quoted 11 lines
The conversation on that patch ended after:
Quoted 5 lines
Quoted 1 line
After adding patch 15, there are a lot of "unittest internal error" messages.
Yeah, I meant to ask you about that. I thought it was due to a change you made, but after further examination, just now, I found it was my fault. Sorry for not mentioning that anywhere. I will fix it in v5.
It is not worth my time to look at patch 15 when it is that broken. So I have not done any review of it.
Right, I didn't expect you to, we were still discussing things on RFC v3 at the time. I think I got you comments on v3 in a very short time frame around sending out v4; hence why your comments were not addressed.
Quoted 2 lines
So no, I think you are still in the RFC stage unless you drop patch 15.
Noted. I might split that out into a separate RFC then.
Quoted 5 lines
Quoted 4 lines
I should have the next revision out in a week or so.
Cheers!
Hi,
Quoted 2 lines · Brendan Higgins
This patch set proposes KUnit, a lightweight unit testing and mocking framework for the Linux kernel.
I haven't followed the entire conversation but I saw the KUnit write-up on LWN and ended up, as an exercise, giving it a try.
I really like the idea of having a fast unit testing infrastructure in the kernel. Occasionally, I write userspace tests for tricky functions that I essentially write by copying the code over to a throw away C file and exercise them as I need. I think it would be great to be able to keep these tests around in a way that they can be run by anyone who wants to touch the code.
I was just dealing with some functions that required some mocked up tests so I thought I'd give KUnit a try. I found writing the code very easy and the infrastructure I was testing was quite simple to mock out the hardware.
However, I got a bit hung up by one issue: I was writing unit tests for code in the NTB tree which itself depends on CONFIG_PCI which cannot be enabled in UML (for what should be obvious reasons). I managed to work around this because, as luck would have it, all the functions I cared about testing were actually static inline functions in headers. So I placed my test code in the kunit folder (so it would compile) and hacked around a couple a of functions I didn't care about that would not be compiled.
In the end I got it to work acceptably, but I get the impression that KUnit will not be usable for wide swaths of kernel code that can't be compiled in UML. Has there been any discussion or ideas on how to work around this so it can be more generally useful? Or will this feature be restricted roughly to non-drivers and functions in headers that don't have #ifdefs around them?
If you're interested in seeing the unit tests I ended up writing you can find the commits here[1].
Thanks,
Logan
[1] https://github.com/sbates130272/linux-p2pmem/ ntb_kunit
Hi Logan,
Quoted 30 lines · Logan Gunthorpe
Hi,
Quoted 2 lines · Brendan Higgins
This patch set proposes KUnit, a lightweight unit testing and mocking framework for the Linux kernel.
I haven't followed the entire conversation but I saw the KUnit write-up on LWN and ended up, as an exercise, giving it a try.
I really like the idea of having a fast unit testing infrastructure in the kernel. Occasionally, I write userspace tests for tricky functions that I essentially write by copying the code over to a throw away C file and exercise them as I need. I think it would be great to be able to keep these tests around in a way that they can be run by anyone who wants to touch the code.
I was just dealing with some functions that required some mocked up tests so I thought I'd give KUnit a try. I found writing the code very easy and the infrastructure I was testing was quite simple to mock out the hardware.
However, I got a bit hung up by one issue: I was writing unit tests for code in the NTB tree which itself depends on
CONFIG_PCIwhich cannot be enabled in UML (for what should be obvious reasons). I managed to work around this because, as luck would have it, all the functions I cared about testing were actually static inline functions in headers. So I placed my test code in the kunit folder (so it would compile) and hacked around a couple a of functions I didn't care about that would not be compiled.
Quoted 6 lines
In the end I got it to work acceptably, but I get the impression that KUnit will not be usable for wide swaths of kernel code that can't be compiled in UML. Has there been any discussion or ideas on how to work around this so it can be more generally useful? Or will this feature be restricted roughly to non-drivers and functions in headers that don't have #ifdefs around them?
Testing drivers, hardware and firmware within production kernels was the use case that inspired KTF (Kernel Test Framework). Currently KTF is available as a standalone git repository. That's been the most efficient form for us so far, as we typically want tests to be developed once but deployed on many different kernel versions simultaneously, as part of continuous integration.
But we're also working towards a suitable proposal for how it can be smoothly integrated into the kernel, but while still keeping the benefits and tools to allow cross-kernel development of tests. As part of this, I have a master student who has been looking at converting some of the existing kernel test suites to KTF, and we have more examples coming from our internal usage, as we get more mileage and more users. See for instance this recent blog entry testing skbuff as an example, on the Oracle kernel development blog:
https://blogs.oracle.com/linux/writing-kernel-tests-with-the-new-kernel-test-framework-ktf
Other relevant links:
Git repo: https://github.com/oracle/ktf
Formatted docs: http://heim.ifi.uio.no/~knuto/ktf/
LWN mention from my presentation at LPC'17: https://lwn.net/Articles/735034/
Earlier Oracle blog post: https://blogs.oracle.com/linux/oracles-new-kernel-test-framework-for-linux-v2
OSS'18 presentation slides: https://events.linuxfoundation.org/wp-content/uploads/2017/12/Test-Driven-Kernel-Development-Knut-Omang-Oracle.pdf
Quoted 2 lines
If you're interested in seeing the unit tests I ended up writing you can find the commits here[1].
It would certainly be interesting to see how the use cases you struggled with would work with KTF ;-)
Thanks,
Knut
Quoted 6 lines
Thanks,
Logan
[1] https://github.com/sbates130272/linux-p2pmem/
ntb_kunit
Quoted 5 lines · Knut Omang
Testing drivers, hardware and firmware within production kernels was the use case that inspired KTF (Kernel Test Framework). Currently KTF is available as a standalone git repository. That's been the most efficient form for us so far, as we typically want tests to be developed once but deployed on many different kernel versions simultaneously, as part of continuous integration.
Interesting. It seems like it's really in direct competition with KUnit. I didn't really go into it in too much detail but these are my thoughts:
Quoted 1 line
From a developer perspective I think KTF not being in the kernel tree is
a huge negative. I want minimal effort to include my tests in a patch series and minimal effort for other developers to be able to use them. Needing to submit these tests to another project or use another project to run them is too much friction.
Also I think the goal of having tests that run on any kernel version is a pipe dream. You'd absolutely need a way to encode which kernel versions a test is expected to pass on because the tests might not make sense until a feature is finished in upstream. And this makes it even harder to develop these tests because, when we write them, we might not even know which kernel version the feature will be added to. Similarly, if a feature is removed or substantially changed, someone will have to do a patch to disable the test for subsequent kernel versions and create a new test for changed features. So, IMO, tests absolutely have to be part of the kernel tree so they can be changed with the respective features they test.
Kunit's ability to run without having to build and run the entire kernel
is also a huge plus. (Assuming there's a way to get around the build
dependency issues). Because of this, it can be very quick to run these tests which makes development a *lot* easier seeing you don't have to reboot a machine every time you want to test a fix.
Logan
Quoted 11 lines · Logan Gunthorpe
Quoted 5 lines · Knut Omang
Testing drivers, hardware and firmware within production kernels was the use case that inspired KTF (Kernel Test Framework). Currently KTF is available as a standalone git repository. That's been the most efficient form for us so far, as we typically want tests to be developed once but deployed on many different kernel versions simultaneously, as part of continuous integration.
Interesting. It seems like it's really in direct competition with KUnit.
I won't speak for Knut, but I don't think we are in competition. I see KTF as a novel way to do a kind of white box end-to-end testing for the Linux kernel, which is a valuable thing, especially in some circumstances. I could see KTF having a lot of value for someone who wants to maintain out of tree drivers, in particular.
Nevertheless, I don't really see KTF as a real unit testing framework for a number of different reasons; you pointed out some below, but I think the main one being that it requires booting a real kernel on actual hardware; I imagine it could be made to work on a VM, but that isn't really the point; it fundamentally depends on having part of the test, or at least driving the test from userspace on top of the kernel under test. Knut, myself, and others, had a previous discussion to this effect here: https://lkml.org/lkml/2018/11/24/170
Quoted 20 lines
I didn't really go into it in too much detail but these are my thoughts:
From a developer perspective I think KTF not being in the kernel tree is a huge negative. I want minimal effort to include my tests in a patch series and minimal effort for other developers to be able to use them. Needing to submit these tests to another project or use another project to run them is too much friction.
Also I think the goal of having tests that run on any kernel version is a pipe dream. You'd absolutely need a way to encode which kernel versions a test is expected to pass on because the tests might not make sense until a feature is finished in upstream. And this makes it even harder to develop these tests because, when we write them, we might not even know which kernel version the feature will be added to. Similarly, if a feature is removed or substantially changed, someone will have to do a patch to disable the test for subsequent kernel versions and create a new test for changed features. So, IMO, tests absolutely have to be part of the kernel tree so they can be changed with the respective features they test.
Quoted 5 lines
Kunit's ability to run without having to build and run the entire kernel
is also a huge plus. (Assuming there's a way to get around the build
dependency issues). Because of this, it can be very quick to run these tests which makes development a *lot* easier seeing you don't have to reboot a machine every time you want to test a fix.
I will reply to your comments directly on your original email. I don't want to hijack this thread, in case we want to discuss the topic of KUnit vs. KTF further.
Quoted 14 lines · Brendan Higgins
Quoted 13 lines · Logan Gunthorpe
Quoted 9 lines · Knut Omang
Testing drivers, hardware and firmware within production kernels was the use
case that inspired KTF (Kernel Test Framework). Currently KTF is available as a
standalone git repository. That's been the most efficient form for us so far,
as we typically want tests to be developed once but deployed on many different
kernel versions simultaneously, as part of continuous integration.
Quoted 3 lines
Quoted 1 line
Interesting. It seems like it's really in direct competition with KUnit.
I won't speak for Knut, but I don't think we are in competition.
I would rather say we have some overlap in functionality :)
My understanding is that we have a common goal of providing better infrastructure for testing, but have approached this whole problem complex from somewhat different perspectives.
Quoted 5 lines
I see
KTF as a novel way to do a kind of white box end-to-end testing for the Linux kernel, which is a valuable thing, especially in some circumstances. I could see KTF having a lot of value for someone who wants to maintain out of tree drivers, in particular.
The best argument here is really good examples.
I'm not sure the distinction between "black box" and "white box" testing is
useful here, there's always underlying assumptions behind specific,
deterministic tests. Writing a test is a very good way to get to
understand a piece of code. Just look at the flow of the example in
https://blogs.oracle.com/linux/writing-kernel-tests-with-the-new-kernel-test-framework-ktf, clearly unit tests, but the knowledge gathering is an important part and motivation!
Quoted 4 lines
Nevertheless, I don't really see KTF as a real unit testing framework for a number of different reasons; you pointed out some below, but I think the main one being that it requires booting a real kernel on actual hardware;
That depends on what you want to test. If you need hardware (or simulated or emulated hardware) for the test, of course you would need to have that hardware, but if, lets say, you just wanted to run tests like the skbuff example tests (see link above) you wouldn't need anything more than what you need to run KUnit tests.
Quoted 13 lines
I imagine it could be made to work on a VM, but that
isn't really the point; it fundamentally depends on having part of the test, or at least driving the test from userspace on top of the kernel under test. Knut, myself, and others, had a previous discussion to this effect here: https://lkml.org/lkml/2018/11/24/170Quoted 7 lines
I didn't really go into it in too much detail but these are my thoughts:
From a developer perspective I think KTF not being in the kernel tree is a huge negative. I want minimal effort to include my tests in a patch series and minimal effort for other developers to be able to use them. Needing to submit these tests to another project or use another project to run them is too much friction.
As said, I recognize the need to upstream KTF, and we are working to do that, that's why I bother to write this :)
Quoted 2 lines
Quoted 2 lines
Also I think the goal of having tests that run on any kernel version is a pipe dream.
I have fulfilled that dream, so I know it is possible (Inifinband driver, kernels from 2.6.39 to 4.8.x or so..) I know a lot of projects would benefit from support for such workflows, but that's not really the point here - we want to achieve both goals!
Quoted 10 lines
Quoted 10 lines
You'd absolutely need a way to encode which kernel
versions a test is expected to pass on because the tests might not make sense until a feature is finished in upstream. And this makes it even harder to develop these tests because, when we write them, we might not even know which kernel version the feature will be added to. Similarly, if a feature is removed or substantially changed, someone will have to do a patch to disable the test for subsequent kernel versions and create a new test for changed features. So, IMO, tests absolutely have to be part of the kernel tree so they can be changed with the respective features they test.
Of course a feature that is not part of a kernel cannot easily pass for that kernel. And yes, testing for kernel version might be necessary in some cases, and even to write a section of extra code to handle differences, still that's worth the benefit.
And that's also a use case: "Can I use kernel v.X.Y.Z if I need feature w?" Lets assume we had a set of tests covering a particular feature, and someone needed that feature, then they could just run the latest set of tests for that feature on an older kernel to determine if they had enough support for what they needed. If necessary, they could then backport the feature, and run the tests to verify that they actually implemented it correctly.
On example I recall of this from the Infiniband driver times was the need to have a predictable way to efficiently use huge scatterlists across kernels. We relied upon scatterlist chaining in a particular way, and the API descriptions did not really specify to a detailed enough level how the guaranteed semantics were supposed to be.
I wrote a few simple KTF tests that tested the driver code for the semantics we expected, and ran them against older and newer kernels and used them to make sure we would have a driver that worked across a few subtle changes to scatterlists and their use.
Quoted 2 lines
Quoted 2 lines
Kunit's ability to run without having to build and run the entire kernel
is also a huge plus.
IMHO the UML kernel is still a kernel running inside a user land program, and so is a QEMU/KVM VM, which is my favourite KTF test environment.
Also with UML it is more difficult/less useful to deploy user space tools such as valgrind, which IMHO would be my main reason for getting kernel code out of the kernel. I recognize that there's a need for
doing just that (e.g. compiling complicated data structures entirely in user space with mocked interfaces) but I think it would be much more useful to be able to do that without the additional complexity of UML (or QEMU).
Quoted 4 lines
Quoted 4 lines
(Assuming there's a way to get around the build
dependency issues). Because of this, it can be very quick to run these tests which makes development a *lot* easier seeing you don't have to reboot a machine every time you want to test a fix.
If your target component under test can be built as a kernel module, or set of modules, with KTF your workflow would not involve booting at all (unless you happened to crash the system with one of your tests, that is :) )
You would just unload your module under test and the test module, recompile the two and insmod again. My work current work cycle on this is just a few seconds.
Quoted 3 lines
I will reply to your comments directly on your original email. I don't want to hijack this thread, in case we want to discuss the topic of KUnit vs. KTF further.
Good idea!
We can at least agree upon that such an important matter as this can be worthy a good, detailed discussion! :)
Thanks!
Knut
Quoted 10 lines · Knut Omang
Quoted 4 lines
Nevertheless, I don't really see KTF as a real unit testing framework for a number of different reasons; you pointed out some below, but I think the main one being that it requires booting a real kernel on actual hardware;
That depends on what you want to test. If you need hardware (or simulated or emulated hardware) for the test, of course you would need to have that hardware, but if, lets say, you just wanted to run tests like the skbuff example tests (see link above) you wouldn't need anything more than what you need to run KUnit tests.
I'm starting to get the same impression: KTF isn't unit testing. When we are saying "unit tests" we are specifying exactly what we want to test:
small sections of code in isolation. So by definition you should not need hardware for this.
Quoted 4 lines
I have fulfilled that dream, so I know it is possible (Inifinband driver, kernels from 2.6.39 to 4.8.x or so..) I know a lot of projects would benefit from support for such workflows, but that's not really the point here - we want to achieve both goals!
This is what makes me think we are not talking about testing the same things. We are not talking about end to end testing of entire drivers but smaller sections of code. A unit test is far more granular and despite an infinband driver existing for 2.6.39 through 4.8, the internal implementation could be drastically different. But unit tests would be testing internal details which could be very different version to version and has to evolve with the implementation.
Quoted 6 lines
If your target component under test can be built as a kernel module, or set of modules, with KTF your workflow would not involve booting at all (unless you happened to crash the system with one of your tests, that is :) )
You would just unload your module under test and the test module, recompile the two and insmod again. My work current work cycle on this is just a few seconds.
Yes, I'm sure we've all done that many a time but it's really beside the point. Kunit offers a much nicer method for running a lot of unit tests on existing code.
Logan
Quoted 15 lines · Logan Gunthorpe
Quoted 5 lines · Knut Omang
Quoted 4 lines
Nevertheless, I don't really see KTF as a real unit testing framework for a number of different reasons; you pointed out some below, but I think the main one being that it requires booting a real kernel on actual hardware;
Quoted 7 lines
That depends on what you want to test. If you need hardware (or simulated or emulated hardware) for the test, of course you would need to have that hardware,
but if, lets say, you just wanted to run tests like the skbuff example tests (see link above) you wouldn't need anything more than what you need to run KUnit
tests.
Quoted 4 lines
I'm starting to get the same impression: KTF isn't unit testing. When we are saying "unit tests" we are specifying exactly what we want to test:
small sections of code in isolation. So by definition you should not need hardware for this.
In my world hardware is just that: a piece of code. It can be in many forms, but it is still code to be tested, and code that can be changed (but sometimes at a slightly higher cost than a recompile ;-)
But that's not the point here: KTF can be used for your narrower definition of unit tests, and it can be used for small, precise tests, for particular bugs for instance, that you would not characterize as a unit test, still it serves the same purpose, and I believe in a pragmatic approach to this. We want to maximize the value of our time. I believe there's a sweet point wrt return on investment on the scale from purist unit testing to just writing code and test with existing applications. We're targeting that ;-)
Quoted 9 lines
Quoted 5 lines
I have fulfilled that dream, so I know it is possible (Inifinband driver, kernels from 2.6.39 to 4.8.x or so..) I know a lot of projects would benefit from support for such workflows, but that's not really the point here - we want
to achieve both goals!This is what makes me think we are not talking about testing the same things. We are not talking about end to end testing of entire drivers but smaller sections of code.
No! I am talking about testing units within a driver, or within any kernel component. I am sure you agree that what constitutes a unit depend on what level of abstraction you are looking at.
Quoted 15 lines
A unit test is far more granular and
despite an infinband driver existing for 2.6.39 through 4.8, the internal implementation could be drastically different. But unit tests would be testing internal details which could be very different version to version and has to evolve with the implementation.Quoted 8 lines
If your target component under test can be built as a kernel module, or set of
modules, with KTF your workflow would not involve booting at all (unless you happened to crash the system with one of your tests, that is :) ) You would just unload your module under test and the test module, recompile the
two and insmod again. My work current work cycle on this is just a few seconds.
Quoted 3 lines
Yes, I'm sure we've all done that many a time but it's really beside the point. Kunit offers a much nicer method for running a lot of unit tests on existing code.
Again, use cases and examples are the key here,..
Knut
Quoted 9 lines · Logan Gunthorpe
Hi,
Quoted 2 lines · Brendan Higgins
This patch set proposes KUnit, a lightweight unit testing and mocking framework for the Linux kernel.
I haven't followed the entire conversation but I saw the KUnit write-up on LWN and ended up, as an exercise, giving it a try.
Awesome! Thanks for taking the time to try it out and to give me feedback!
Quoted 13 lines
I really like the idea of having a fast unit testing infrastructure in the kernel. Occasionally, I write userspace tests for tricky functions that I essentially write by copying the code over to a throw away C file and exercise them as I need. I think it would be great to be able to keep these tests around in a way that they can be run by anyone who wants to touch the code.
I was just dealing with some functions that required some mocked up tests so I thought I'd give KUnit a try. I found writing the code very easy and the infrastructure I was testing was quite simple to mock out the hardware.
Quoted 8 lines
However, I got a bit hung up by one issue: I was writing unit tests for code in the NTB tree which itself depends on
CONFIG_PCIwhich cannot be enabled in UML (for what should be obvious reasons). I managed to work around this because, as luck would have it, all the functions I cared about testing were actually static inline functions in headers. So I placed my test code in the kunit folder (so it would compile) and hacked around a couple a of functions I didn't care about that would not be compiled.
A couple of points, as for needing CONFIG_PCI; my plan to deal with that type of thing has been that we would add support for a KUnit/UML version that is just for KUnit. It would mock out the necessary bits to provide a fake hardware implementation for anything that might depend on it. I wrote a prototype for mocking/faking MMIO that I presented to the list here[1]; it is not part of the current patchset because we decided it would be best to focus on getting an MVP in, but I plan on bringing it back up at some point. Anyway, what do you generally think of this approach?
Quoted 2 lines
In the end I got it to work acceptably, but I get the impression that
Awesome, I looked at the code you posted and it doesn't look like you have had too many troubles. One thing that stood out to me, why did you need to put it in the kunit/ dir?
Quoted 2 lines
KUnit will not be usable for wide swaths of kernel code that can't be compiled in UML. Has there been any discussion or ideas on how to work
For the most part, I was planning on relying on mocking or faking the hardware out as you have done. I have found this worked pretty well in other instances. I know there are some edge cases for which even this won't work like code in arch/; for that you *can* compile KUnit for non-UML, but that is not really unit testing any more, but I included it that as more of a last ditch effort, debugging aid, or to allow a user to write integration tests (also very much because other people asked me to do it ;-) ). So the main plan here is mocking and faking.
There has been some additional discussion here[2] (see the replies); this thread is more about the future of trying to pull out kernel dependencies which I discussed further with Luis (off list) here[3] (in reality the discussion has been spread across a number of different threads, but I think those are places you could at least get context to jump in and talk about mocking and faking hardware and Linux kernel resources).
In short, I think the idea is we are using UML as scaffolding and we will gradually try to make it more independent by either reducing dependencies on kernel resources (by providing high quality fakes) or by somehow providing a better view of the dependencies so that you can specify more directly what piece of code you need for your test. This part is obviously very long term, but I think that's what we need to do if we want to do really high quality unit testing, regardless of whether we use KUnit or not. In anycase, right now, we are just working on the basic tools to write *a* unit test for the kernel.
Quoted 3 lines
around this so it can be more generally useful? Or will this feature be restricted roughly to non-drivers and functions in headers that don't have #ifdefs around them?
I hope not.
The #ifdef thing is something that I don't have a good answer for at this time. Luis and I talked about it, but haven't come up with any good ideas, sorry.
As for drivers, I found that testing drivers is quite doable. You can see an example of a test I wrote for a i2c bus driver here[4]. I know the i2c subsystem is pretty simple, but the general principle should apply elsewhere.
Quoted 9 lines
If you're interested in seeing the unit tests I ended up writing you can find the commits here[1].
Thanks,
Logan
[1] https://github.com/sbates130272/linux-p2pmem/
ntb_kunit
I am looking forward to see what you think!
Cheers
[1] https://lkml.org/lkml/2018/10/17/122
[2] https://lkml.org/lkml/2018/11/29/93
[3] https://groups.google.com/forum/#!topic/kunit-dev/EQ1x0SzrUus
[4] https://kunit.googlesource.com/linux/+/e10484ad2f9fc7926412ec84739fe105981b4771/drivers/i2c/busses/i2c-aspeed-test.c
Quoted 9 lines · Brendan Higgins
A couple of points, as for needing
CONFIG_PCI; my plan to deal with that type of thing has been that we would add support for a KUnit/UML version that is just for KUnit. It would mock out the necessary bits to provide a fake hardware implementation for anything that might depend on it. I wrote a prototype for mocking/faking MMIO that I presented to the list here[1]; it is not part of the current patchset because we decided it would be best to focus on getting an MVP in, but I plan on bringing it back up at some point. Anyway, what do you generally think of this approach?
Yes, I was wondering if that might be possible. I think that's a great approach but it will unfortunately take a lot of work before larger swaths of the kernel are testable in Kunit with UML. Having more common mocked infrastructure will be great by-product of it though.
Quoted 3 lines
Awesome, I looked at the code you posted and it doesn't look like you have had too many troubles. One thing that stood out to me, why did you need to put it in the kunit/ dir?
Yeah, writing the code was super easy. Only after, did I realized I couldn't get it to easily build.
Putting it in the kunit directory was necessary because nothing in the NTB tree builds unless CONFIG_NTB is set (see drivers/Makefile) and CONFIG_NTB depends on CONFIG_PCI. I didn't experiment to see how hard it would be to set CONFIG_NTB without CONFIG_PCI; I assumed it would be tricky.
Quoted 1 line
I am looking forward to see what you think!
Generally, I'm impressed and want to see this work in upstream as soon as possible so I can start to make use of it!
Logan
Quoted 14 lines · Logan Gunthorpe
Quoted 9 lines · Brendan Higgins
A couple of points, as for needing
CONFIG_PCI; my plan to deal with that type of thing has been that we would add support for a KUnit/UML version that is just for KUnit. It would mock out the necessary bits to provide a fake hardware implementation for anything that might depend on it. I wrote a prototype for mocking/faking MMIO that I presented to the list here[1]; it is not part of the current patchset because we decided it would be best to focus on getting an MVP in, but I plan on bringing it back up at some point. Anyway, what do you generally think of this approach?
Quoted 4 lines
Yes, I was wondering if that might be possible. I think that's a great approach but it will unfortunately take a lot of work before larger swaths of the kernel are testable in Kunit with UML. Having more common mocked infrastructure will be great by-product of it though.
Yeah, it's unfortunate that the best way to do something often takes so much longer.
Quoted 7 lines
Quoted 4 lines
Awesome, I looked at the code you posted and it doesn't look like you have had too many troubles. One thing that stood out to me, why did you need to put it in the kunit/ dir?
Yeah, writing the code was super easy. Only after, did I realized I couldn't get it to easily build.
Yeah, we really need to fix that; unfortunately, broadly addressing that problem is really hard and will most likely take a long time.
Quoted 10 lines
Putting it in the kunit directory was necessary because nothing in the NTB tree builds unless
CONFIG_NTBis set (seedrivers/Makefile) andCONFIG_NTBdepends onCONFIG_PCI. I didn't experiment to see how hard it would be to setCONFIG_NTBwithoutCONFIG_PCI; I assumed it would be tricky.Quoted 1 line
I am looking forward to see what you think!
Generally, I'm impressed and want to see this work in upstream as soon as possible so I can start to make use of it!
Great to hear! I was trying to get the next revision out this week, but addressing some of the comments is taking a little longer than expected. I should have something together fairly soon though (hopefully next week). Good news is that next revision will be non-RFC; most of the feedback has settled down and I think we are ready to start figuring out how to merge it. Fingers crossed :-)
Cheers
Quoted 45 lines · Brendan Higgins
Quoted 14 lines · Logan Gunthorpe
Quoted 9 lines · Brendan Higgins
A couple of points, as for needing
CONFIG_PCI; my plan to deal with that type of thing has been that we would add support for a KUnit/UML version that is just for KUnit. It would mock out the necessary bits to provide a fake hardware implementation for anything that might depend on it. I wrote a prototype for mocking/faking MMIO that I presented to the list here[1]; it is not part of the current patchset because we decided it would be best to focus on getting an MVP in, but I plan on bringing it back up at some point. Anyway, what do you generally think of this approach?Quoted 4 lines
Yes, I was wondering if that might be possible. I think that's a great approach but it will unfortunately take a lot of work before larger swaths of the kernel are testable in Kunit with UML. Having more common mocked infrastructure will be great by-product of it though.
Yeah, it's unfortunate that the best way to do something often takes so much longer.
Quoted 7 lines
Quoted 4 lines
Awesome, I looked at the code you posted and it doesn't look like you have had too many troubles. One thing that stood out to me, why did you need to put it in the kunit/ dir?
Yeah, writing the code was super easy. Only after, did I realized I couldn't get it to easily build.
Yeah, we really need to fix that; unfortunately, broadly addressing that problem is really hard and will most likely take a long time.
Quoted 10 lines
Putting it in the kunit directory was necessary because nothing in the NTB tree builds unless
CONFIG_NTBis set (seedrivers/Makefile) andCONFIG_NTBdepends onCONFIG_PCI. I didn't experiment to see how hard it would be to setCONFIG_NTBwithoutCONFIG_PCI; I assumed it would be tricky.Quoted 1 line
I am looking forward to see what you think!
Generally, I'm impressed and want to see this work in upstream as soon as possible so I can start to make use of it!
Quoted 8 lines
Great to hear! I was trying to get the next revision out this week, but addressing some of the comments is taking a little longer than expected. I should have something together fairly soon though (hopefully next week). Good news is that next revision will be non-RFC; most of the feedback has settled down and I think we are ready to start figuring out how to merge it. Fingers crossed :-)
Cheers
I'll be out of the office next week and will not be able to review. Please hold off on any devicetree related files until after I review.
Thanks,
Frank
Quoted 54 lines · Frank Rowand
Quoted 52 lines · Brendan Higgins
Quoted 14 lines · Logan Gunthorpe
Quoted 9 lines · Brendan Higgins
A couple of points, as for needing
CONFIG_PCI; my plan to deal with that type of thing has been that we would add support for a KUnit/UML version that is just for KUnit. It would mock out the necessary bits to provide a fake hardware implementation for anything that might depend on it. I wrote a prototype for mocking/faking MMIO that I presented to the list here[1]; it is not part of the current patchset because we decided it would be best to focus on getting an MVP in, but I plan on bringing it back up at some point. Anyway, what do you generally think of this approach?Quoted 4 lines
Yes, I was wondering if that might be possible. I think that's a great approach but it will unfortunately take a lot of work before larger swaths of the kernel are testable in Kunit with UML. Having more common mocked infrastructure will be great by-product of it though.
Yeah, it's unfortunate that the best way to do something often takes so much longer.
Quoted 7 lines
Quoted 4 lines
Awesome, I looked at the code you posted and it doesn't look like you have had too many troubles. One thing that stood out to me, why did you need to put it in the kunit/ dir?
Yeah, writing the code was super easy. Only after, did I realized I couldn't get it to easily build.
Yeah, we really need to fix that; unfortunately, broadly addressing that problem is really hard and will most likely take a long time.
Quoted 10 lines
Putting it in the kunit directory was necessary because nothing in the NTB tree builds unless
CONFIG_NTBis set (seedrivers/Makefile) andCONFIG_NTBdepends onCONFIG_PCI. I didn't experiment to see how hard it would be to setCONFIG_NTBwithoutCONFIG_PCI; I assumed it would be tricky.Quoted 1 line
I am looking forward to see what you think!
Generally, I'm impressed and want to see this work in upstream as soon as possible so I can start to make use of it!
Great to hear! I was trying to get the next revision out this week, but addressing some of the comments is taking a little longer than expected. I should have something together fairly soon though (hopefully next week). Good news is that next revision will be non-RFC; most of the feedback has settled down and I think we are ready to start figuring out how to merge it. Fingers crossed :-)
Quoted 4 lines
Quoted 1 line
Cheers
I'll be out of the office next week and will not be able to review. Please hold off on any devicetree related files until after I review.
Will do.