Skip to contentSkip to conversation

kunit: introduce KUnit, the Linux kernel unit testing framework

PATCH Superseded Brendan Higgins 2 participants 60 messages Aug 12, 2019 – Aug 13, 2019

Subsystems (11)
Documentation VFS Kbuild Kselftest +7 more subsystems

v12 of 18 Superseded Latest is v18 →

lore
Share & tools
lore
#0 lore raw

v12 cover kunit: introduce KUnit, the Linux kernel unit testing framework

## TL;DR

This revision removes dependence on kunit_stream in favor of kunit_assert, as suggested by Stephen Boyd. kunit_assert provides a more structured interface for constructing messages and allows most required data to be stored on the stack for most expectations until it is determined that a failure message must be produced.

As a part of introducing kunit_assert, expectations (KUNIT_EXPECT_*) and assertions (KUNIT_ASSERT_*) have been substantially refactored. Nevertheless, behavior should be the same.

As this revision, adds a new patch, it, [PATCH v12 04/18], needs to be reviewed. All other patches have appropriate reviews and acks.

I also rebased the patchset on v5.3-rc3.

## Background

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 (however, KUnit still allows you to run tests on test machines or in VMs if you want[1]) 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 about 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[2].

Additionally for convenience, I have applied these patches to a branch[3]. The repo may be cloned with:
git clone https://kunit.googlesource.com/linux
This patchset is on the kunit/rfc/v5.3/v12 branch.

## Changes Since Last Version

- Dropped patch "[PATCH v11 04/18] kunit: test: add kunit_stream a std::stream like logger" and replaced it with "[PATCH v12 04/18]
kunit: test: add assertion printing library", which provides a totally new mechanism for constructing expectation/assertion failure messages.
- Substantially refactored expectations and assertions definitions in [PATCH 05/18] and [PATCH 11/18] respectively.
- Rebased patchset on v5.3-rc3.
- Fixed a minor documentation bug.

[1] https://google.github.io/kunit-docs/third_party/kernel/docs/usage.html#kunit-on-non-uml-architectures
[2] https://google.github.io/kunit-docs/third_party/kernel/docs/
[3] https://kunit.googlesource.com/linux/+/kunit/rfc/v5.3/v12

Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
#1 lore raw

PATCH v12 1/18 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 | 179 ++++++++++++++++++++++++++++++++++++++++
 kunit/Kconfig        |  17 ++++
 kunit/Makefile       |   1 +
 kunit/test.c         | 191 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 388 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 +179 -0
new file mode 100644
index 0000000000000..e0b34acb9ee4e
@@ -0,0 +1,179 @@
+/* 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>
+
+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_suite and will be run after the
+ * suite's init function and followed by the suite'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;
+
+	/* 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_suite - 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_suite 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_suite for KUnit to
+ * run it.
+ */
+struct kunit_suite {
+	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_suite).
+ *
+ * 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! */
+	/*
+	 * success starts as true, and may only be set to false during a test
+	 * case; thus, it is safe to update this across multiple threads using
+	 * WRITE_ONCE; however, as a consequence, it may only be read after the
+	 * test case finishes once all threads associated with the test case
+	 * have terminated.
+	 */
+	bool success; /* Read only after test_case finishes! */
+};
+
+void kunit_init_test(struct kunit *test, const char *name);
+
+int kunit_run_tests(struct kunit_suite *suite);
+
+/**
+ * kunit_test_suite() - used to register a &struct kunit_suite with KUnit.
+ * @suite: a statically allocated &struct kunit_suite.
+ *
+ * Registers @suite with the test framework. See &struct kunit_suite for more
+ * information.
+ *
+ * NOTE: Currently KUnit tests are all run as late_initcalls; this means that
+ * they cannot test anything where tests must run at a different init phase. One
+ * significant restriction resulting from this is that KUnit cannot reliably
+ * test anything that is initialize in the late_init phase; another is that
+ * KUnit is useless to test things that need to be run in an earlier init phase.
+ */
+#define kunit_test_suite(suite)						       \
+		/*
+		 * TODO([email protected]): Don't run all KUnit tests as
+		 * late_initcalls.  I have some future work planned to dispatch
+		 * all KUnit tests from the same place, and at the very least to
+		 * do so after everything else is definitely initialized.
+		 */							       \
+		static int kunit_suite_init##suite(void)		       \
+		{							       \
+			return kunit_run_tests(&suite);			       \
+		}							       \
+		late_initcall(kunit_suite_init##suite)
+
+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 suite 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.
+ *
+ * Prints a warning level message.
+ */
+#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.
+ *
+ * Prints an error level message.
+ */
+#define kunit_err(test, fmt, ...) \
+		kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
+
+#endif /* _KUNIT_TEST_H */
kunit/Kconfig new file +17 -0
new file mode 100644
index 0000000000000..330ae83527c23
@@ -0,0 +1,17 @@
+#
+# 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 when using UML. Can also be used on most other
+	  architectures. For more information, please see
+	  Documentation/dev-tools/kunit/.
+
+endmenu
kunit/Makefile new file +1 -0
new file mode 100644
index 0000000000000..5efdc4dea2c08
@@ -0,0 +1 @@
+obj-$(CONFIG_KUNIT) +=			test.o
kunit/test.c new file +191 -0

191 lines not on this page · raw message

Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
#2 lore raw

PATCH v12 2/18 kunit: test: add test resource management API

In reply to Brendan Higgins

Create 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 | 143 +++++++++++++++++++++++++++++++++++++++++++
 kunit/test.c         |  92 ++++++++++++++++++++++++++++
 2 files changed, 235 insertions(+)
include/kunit/test.h +143 -0
index e0b34acb9ee4e..d0bf112910caf 100644
@@ -10,6 +10,70 @@
 #define _KUNIT_TEST_H
 
 #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, &params);
+ *		if (res)
+ *			return res->allocation;
+ *
+ *		return NULL;
+ *	}
+ */
+struct kunit_resource {
+	void *allocation;
+	kunit_resource_free_t free;
+
+	/* private: internal use only. */
+	struct list_head node;
+};
 
 struct kunit;
 
@@ -109,6 +173,13 @@ struct kunit {
 	 * have terminated.
 	 */
 	bool success; /* Read only after test_case finishes! */
+	spinlock_t lock; /* Gaurds all mutable test state. */
+	/*
+	 * Because resources is a list that may be updated multiple times (with
+	 * new resources) from any thread associated with a test case, we must
+	 * protect it with some type of lock.
+	 */
+	struct list_head resources; /* Protected by lock. */
 };
 
 void kunit_init_test(struct kunit *test, const char *name);
@@ -141,6 +212,78 @@ int kunit_run_tests(struct kunit_suite *suite);
 		}							       \
 		late_initcall(kunit_suite_init##suite)
 
+/*
+ * Like kunit_alloc_resource() below, but returns the struct kunit_resource
+ * object that contains the allocation. This is mostly for testing purposes.
+ */
+struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
+						    kunit_resource_init_t init,
+						    kunit_resource_free_t free,
+						    gfp_t internal_gfp,
+						    void *context);
+
+/**
+ * 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.
+ * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
+ * @context: for the user to pass in arbitrary data to the init function.
+ *
+ * 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.
+ *
+ * NOTE: KUnit needs to allocate memory for each kunit_resource object. You must
+ * specify an @internal_gfp that is compatible with the use context of your
+ * resource.
+ */
+static inline void *kunit_alloc_resource(struct kunit *test,
+					 kunit_resource_init_t init,
+					 kunit_resource_free_t free,
+					 gfp_t internal_gfp,
+					 void *context)
+{
+	struct kunit_resource *res;
+
+	res = kunit_alloc_and_get_resource(test, init, free, internal_gfp,
+					   context);
+
+	if (res)
+		return res->allocation;
+
+	return NULL;
+}
+
+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 +92 -0

110 lines not on this page · raw message

Signature · 6 lines
2.23.0.rc1.153.gdeed80330f-goog

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v12 2/18
#3 lore raw

Re: [PATCH v12 02/18] kunit: test: add test resource management API

Quoting Brendan Higgins (2019-08-12 11:24:05)

Quoted 11 lines

Create 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.

---

#4 lore raw

PATCH v12 3/18 kunit: test: add string_stream a std::stream like string builder

In reply to Brendan Higgins

A 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. string_stream is really just a string builder, nothing more.

---
 include/kunit/string-stream.h |  49 +++++++++++
 kunit/Makefile                |   3 +-
 kunit/string-stream.c         | 152 ++++++++++++++++++++++++++++++++++
 3 files changed, 203 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 +49 -0
new file mode 100644
index 0000000000000..4fa107e38deb5
@@ -0,0 +1,49 @@
+/* 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 <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 kunit *test;
+	gfp_t gfp;
+};
+
+struct kunit;
+
+struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp);
+
+int string_stream_add(struct string_stream *stream, const char *fmt, ...);
+
+int string_stream_vadd(struct string_stream *stream,
+		       const char *fmt,
+		       va_list args);
+
+char *string_stream_get_string(struct string_stream *stream);
+
+int string_stream_append(struct string_stream *stream,
+			 struct string_stream *other);
+
+void string_stream_clear(struct string_stream *stream);
+
+bool string_stream_is_empty(struct string_stream *stream);
+
+#endif /* _KUNIT_STRING_STREAM_H */
kunit/Makefile +2 -1
index 5efdc4dea2c08..275b565a0e81f 100644
@@ -1 +1,2 @@
-obj-$(CONFIG_KUNIT) +=			test.o
+obj-$(CONFIG_KUNIT) +=			test.o \
+					string-stream.o
kunit/string-stream.c new file +152 -0

152 lines not on this page · raw message

Signature · 6 lines
2.23.0.rc1.153.gdeed80330f-goog

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#5 lore raw

Re: [PATCH v12 03/18] kunit: test: add string_stream a std::stream like string builder

Quoting Brendan Higgins (2019-08-12 11:24:06)

kunit/string-stream.c line 70

Quoted 10 lines
kunit/string-stream.c new file +10 -0
+void string_stream_clear(struct string_stream *stream)
+{
+       struct string_stream_fragment *frag_container, *frag_container_safe;
+
+       spin_lock(&stream->lock);
+       list_for_each_entry_safe(frag_container,
+                                frag_container_safe,
+                                &stream->fragments,
+                                node) {
+               list_del(&frag_container->node);

Shouldn't we free the allocation here? Otherwise, if some test is going to add, add, clear, add, it's going to leak until the test is over?

kunit/string-stream.c line 75

Quoted 5 lines
kunit/string-stream.c new file +5 -0
+       }
+       stream->length = 0;
+       spin_unlock(&stream->lock);
+}
+

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#6 lore raw

Re: [PATCH v12 03/18] kunit: test: add string_stream a std::stream like string builder

Quoted 14 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 11:24:06)

kunit/string-stream.c line 70

Quoted 10 lines
kunit/string-stream.c new file +10 -0
+void string_stream_clear(struct string_stream *stream)
+{
+       struct string_stream_fragment *frag_container, *frag_container_safe;
+
+       spin_lock(&stream->lock);
+       list_for_each_entry_safe(frag_container,
+                                frag_container_safe,
+                                &stream->fragments,
+                                node) {
+               list_del(&frag_container->node);

Shouldn't we free the allocation here? Otherwise, if some test is going to add, add, clear, add, it's going to leak until the test is over?

So basically this means I should add a kunit_kfree and kunit_resource_destroy (respective equivalents to devm_kfree, and devres_destroy) and use kunit_kfree here?

Quoted 5 lines
Quoted 5 lines
kunit/string-stream.c new file +5 -0
+       }
+       stream->length = 0;
+       spin_unlock(&stream->lock);
+}
+
#7 lore raw

Re: [PATCH v12 03/18] kunit: test: add string_stream a std::stream like string builder

Quoting Brendan Higgins (2019-08-12 16:33:36)

Quoted 16 lines · Stephen Boyd
Quoted 14 lines

Quoting Brendan Higgins (2019-08-12 11:24:06)

kunit/string-stream.c line 70

Quoted 10 lines
kunit/string-stream.c new file +10 -0
+void string_stream_clear(struct string_stream *stream)
+{
+       struct string_stream_fragment *frag_container, *frag_container_safe;
+
+       spin_lock(&stream->lock);
+       list_for_each_entry_safe(frag_container,
+                                frag_container_safe,
+                                &stream->fragments,
+                                node) {
+               list_del(&frag_container->node);

Shouldn't we free the allocation here? Otherwise, if some test is going to add, add, clear, add, it's going to leak until the test is over?

Quoted 4 lines

So basically this means I should add a kunit_kfree and kunit_resource_destroy (respective equivalents to devm_kfree, and devres_destroy) and use kunit_kfree here?

Yes, or drop the API entirely? Does anything need this functionality?

#8 lore raw

Re: [PATCH v12 03/18] kunit: test: add string_stream a std::stream like string builder

Quoted 18 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 16:33:36)

Quoted 16 lines · Stephen Boyd
Quoted 14 lines

Quoting Brendan Higgins (2019-08-12 11:24:06)

kunit/string-stream.c line 70

Quoted 10 lines
kunit/string-stream.c new file +10 -0
+void string_stream_clear(struct string_stream *stream)
+{
+       struct string_stream_fragment *frag_container, *frag_container_safe;
+
+       spin_lock(&stream->lock);
+       list_for_each_entry_safe(frag_container,
+                                frag_container_safe,
+                                &stream->fragments,
+                                node) {
+               list_del(&frag_container->node);

Shouldn't we free the allocation here? Otherwise, if some test is going to add, add, clear, add, it's going to leak until the test is over?

Quoted 6 lines
Quoted 4 lines

So basically this means I should add a kunit_kfree and kunit_resource_destroy (respective equivalents to devm_kfree, and devres_destroy) and use kunit_kfree here?

Yes, or drop the API entirely? Does anything need this functionality?

Drop the kunit_resource API? I would strongly prefer not to. string_stream uses it; the expectation stuff uses it via string stream; some of the tests in this patchset allocate memory as part of the test setup that uses it. The intention is that we would provide a kunit_res_* version of many (hopefully eventually most) common resources required by tests and it would be used in the same way that the devm_* stuff is.

Nevertheless, I am fine adding the kunit_resource_destroy, etc. I just wanted to make sure I understood what you were asking.

#9 lore raw

Re: [PATCH v12 03/18] kunit: test: add string_stream a std::stream like string builder

Quoting Brendan Higgins (2019-08-12 17:41:05)

Quoted 9 lines · Stephen Boyd
Quoted 6 lines
Quoted 4 lines

kunit_resource_destroy (respective equivalents to devm_kfree, and devres_destroy) and use kunit_kfree here?

Yes, or drop the API entirely? Does anything need this functionality?

Drop the kunit_resource API? I would strongly prefer not to.

No. I mean this API, string_stream_clear(). Does anything use it?

#10 lore raw

Re: [PATCH v12 03/18] kunit: test: add string_stream a std::stream like string builder

Quoted 8 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 17:41:05)

Quoted 6 lines · Stephen Boyd
Quoted 5 lines
Quoted 4 lines

kunit_resource_destroy (respective equivalents to devm_kfree, and devres_destroy) and use kunit_kfree here?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Yes, or drop the API entirely? Does anything need this functionality?

Drop the kunit_resource API? I would strongly prefer not to.

No. I mean this API, string_stream_clear(). Does anything use it?

Oh, right. No.

However, now that I added the kunit_resource_destroy, I thought it might be good to free the string_stream after I use it in each call to kunit_assert->format(...) in which case I will be using this logic.

So I think the right thing to do is to expose string_stream_destroy so kunit_do_assert can clean up when it's done, and then demote string_stream_clear to static. Sound good?

#11 lore raw

Re: [PATCH v12 03/18] kunit: test: add string_stream a std::stream like string builder

Quoting Brendan Higgins (2019-08-12 22:02:59)

Quoted 17 lines · Stephen Boyd
Quoted 8 lines

Quoting Brendan Higgins (2019-08-12 17:41:05)

Quoted 6 lines · Stephen Boyd
Quoted 5 lines
Quoted 4 lines

kunit_resource_destroy (respective equivalents to devm_kfree, and devres_destroy) and use kunit_kfree here?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Yes, or drop the API entirely? Does anything need this functionality?

Drop the kunit_resource API? I would strongly prefer not to.

No. I mean this API, string_stream_clear(). Does anything use it?

Oh, right. No.

Quoted 7 lines

However, now that I added the kunit_resource_destroy, I thought it might be good to free the string_stream after I use it in each call to kunit_assert->format(...) in which case I will be using this logic.

So I think the right thing to do is to expose string_stream_destroy so kunit_do_assert can clean up when it's done, and then demote string_stream_clear to static. Sound good?

Ok, sure. I don't really see how clearing it explicitly when the assertion prints vs. never allocating it to begin with is really any different. Maybe I've missed something though.

#12 lore raw

Re: [PATCH v12 03/18] kunit: test: add string_stream a std::stream like string builder

Quoted 23 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 22:02:59)

Quoted 21 lines · Stephen Boyd
Quoted 8 lines

Quoting Brendan Higgins (2019-08-12 17:41:05)

Quoted 6 lines · Stephen Boyd
Quoted 5 lines
Quoted 4 lines

kunit_resource_destroy (respective equivalents to devm_kfree, and devres_destroy) and use kunit_kfree here?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Yes, or drop the API entirely? Does anything need this functionality?

Drop the kunit_resource API? I would strongly prefer not to.

No. I mean this API, string_stream_clear(). Does anything use it?

Oh, right. No.

However, now that I added the kunit_resource_destroy, I thought it might be good to free the string_stream after I use it in each call to kunit_assert->format(...) in which case I will be using this logic.

Quoted 7 lines
Quoted 3 lines

So I think the right thing to do is to expose string_stream_destroy so kunit_do_assert can clean up when it's done, and then demote string_stream_clear to static. Sound good?

Ok, sure. I don't really see how clearing it explicitly when the assertion prints vs. never allocating it to begin with is really any different. Maybe I've missed something though.

It's for the case that we *do* print something out. Once we are doing printing, we don't want the fragments anymore.

#13 lore raw

Re: [PATCH v12 03/18] kunit: test: add string_stream a std::stream like string builder

Quoted 29 lines · Brendan Higgins
Quoted 27 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 22:02:59)

Quoted 24 lines · Stephen Boyd
Quoted 8 lines

Quoting Brendan Higgins (2019-08-12 17:41:05)

Quoted 6 lines · Stephen Boyd
Quoted 5 lines
Quoted 4 lines

kunit_resource_destroy (respective equivalents to devm_kfree, and devres_destroy) and use kunit_kfree here?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Yes, or drop the API entirely? Does anything need this functionality?

Drop the kunit_resource API? I would strongly prefer not to.

No. I mean this API, string_stream_clear(). Does anything use it?

Oh, right. No.

However, now that I added the kunit_resource_destroy, I thought it might be good to free the string_stream after I use it in each call to kunit_assert->format(...) in which case I will be using this logic.

So I think the right thing to do is to expose string_stream_destroy so kunit_do_assert can clean up when it's done, and then demote string_stream_clear to static. Sound good?

Quoted 6 lines
Quoted 3 lines

Ok, sure. I don't really see how clearing it explicitly when the assertion prints vs. never allocating it to begin with is really any different. Maybe I've missed something though.

It's for the case that we *do* print something out. Once we are doing printing, we don't want the fragments anymore.

Oops, sorry fat fingered: s/doing/done

#14 lore raw

Re: [PATCH v12 03/18] kunit: test: add string_stream a std::stream like string builder

Quoting Brendan Higgins (2019-08-13 02:12:54)

Quoted 14 lines
Quoted 12 lines · Brendan Higgins
Quoted 10 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 22:02:59)

Quoted 7 lines

However, now that I added the kunit_resource_destroy, I thought it might be good to free the string_stream after I use it in each call to kunit_assert->format(...) in which case I will be using this logic.

So I think the right thing to do is to expose string_stream_destroy so kunit_do_assert can clean up when it's done, and then demote string_stream_clear to static. Sound good?

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Ok, sure. I don't really see how clearing it explicitly when the assertion prints vs. never allocating it to begin with is really any different. Maybe I've missed something though.

It's for the case that we *do* print something out. Once we are doing printing, we don't want the fragments anymore.

Oops, sorry fat fingered: s/doing/done

Yes, but when we print something out we've run into some sort of problem and then the test is over. So freeing the memory when it fails vs. when the test is over seems like a minor difference. Or is it also used to print other informational messages while the test is running?

I'm not particularly worried here, just trying to see if less code is possible.

#15 lore raw

PATCH v12 4/18 kunit: test: add assertion printing library

In reply to Brendan Higgins

Add struct kunit_assert and friends which provide a structured way to capture data from an expectation or an assertion (introduced later in the series) so that it may be printed out in the event of a failure.

---
 include/kunit/assert.h | 183 +++++++++++++++++++++++++++++++++++++++++
 kunit/Makefile         |   3 +-
 kunit/assert.c         | 141 +++++++++++++++++++++++++++++++
 3 files changed, 326 insertions(+), 1 deletion(-)
 create mode 100644 include/kunit/assert.h
 create mode 100644 kunit/assert.c
include/kunit/assert.h new file +183 -0
new file mode 100644
index 0000000000000..55f1b88b0cb4d
@@ -0,0 +1,183 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Assertion and expectation serialization API.
+ *
+ * Copyright (C) 2019, Google LLC.
+ * Author: Brendan Higgins <[email protected]>
+ */
+
+#ifndef _KUNIT_ASSERT_H
+#define _KUNIT_ASSERT_H
+
+#include <linux/err.h>
+#include <kunit/string-stream.h>
+
+struct kunit;
+
+enum kunit_assert_type {
+	KUNIT_ASSERTION,
+	KUNIT_EXPECTATION,
+};
+
+struct kunit_assert {
+	struct kunit *test;
+	enum kunit_assert_type type;
+	int line;
+	const char *file;
+	struct va_format message;
+	void (*format)(const struct kunit_assert *assert,
+		       struct string_stream *stream);
+};
+
+#define KUNIT_INIT_VA_FMT_NULL { .fmt = NULL, .va = NULL }
+
+#define KUNIT_INIT_ASSERT_STRUCT(kunit, assert_type, fmt) {		       \
+	.test = kunit,							       \
+	.type = assert_type,						       \
+	.file = __FILE__,						       \
+	.line = __LINE__,						       \
+	.message = KUNIT_INIT_VA_FMT_NULL,				       \
+	.format = fmt							       \
+}
+
+void kunit_base_assert_format(const struct kunit_assert *assert,
+			      struct string_stream *stream);
+
+void kunit_assert_print_msg(const struct kunit_assert *assert,
+			    struct string_stream *stream);
+
+struct kunit_fail_assert {
+	struct kunit_assert assert;
+};
+
+void kunit_fail_assert_format(const struct kunit_assert *assert,
+			      struct string_stream *stream);
+
+#define KUNIT_INIT_FAIL_ASSERT_STRUCT(test, type) {			       \
+		.assert = KUNIT_INIT_ASSERT_STRUCT(test,		       \
+						   type,		       \
+						   kunit_fail_assert_format)   \
+}
+
+struct kunit_unary_assert {
+	struct kunit_assert assert;
+	const char *condition;
+	bool expected_true;
+};
+
+void kunit_unary_assert_format(const struct kunit_assert *assert,
+			       struct string_stream *stream);
+
+#define KUNIT_INIT_UNARY_ASSERT_STRUCT(test, type, cond, expect_true) {	       \
+	.assert = KUNIT_INIT_ASSERT_STRUCT(test,			       \
+					   type,			       \
+					   kunit_unary_assert_format),	       \
+	.condition = cond,						       \
+	.expected_true = expect_true					       \
+}
+
+struct kunit_ptr_not_err_assert {
+	struct kunit_assert assert;
+	const char *text;
+	const void *value;
+};
+
+void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
+				     struct string_stream *stream);
+
+#define KUNIT_INIT_PTR_NOT_ERR_STRUCT(test, type, txt, val) {		       \
+	.assert = KUNIT_INIT_ASSERT_STRUCT(test,			       \
+					   type,			       \
+					   kunit_ptr_not_err_assert_format),   \
+	.text = txt,							       \
+	.value = val							       \
+}
+
+struct kunit_binary_assert {
+	struct kunit_assert assert;
+	const char *operation;
+	const char *left_text;
+	long long left_value;
+	const char *right_text;
+	long long right_value;
+};
+
+void kunit_binary_assert_format(const struct kunit_assert *assert,
+				struct string_stream *stream);
+
+#define KUNIT_INIT_BINARY_ASSERT_STRUCT(test,				       \
+					type,				       \
+					op_str,				       \
+					left_str,			       \
+					left_val,			       \
+					right_str,			       \
+					right_val) {			       \
+	.assert = KUNIT_INIT_ASSERT_STRUCT(test,			       \
+					   type,			       \
+					   kunit_binary_assert_format),	       \
+	.operation = op_str,						       \
+	.left_text = left_str,						       \
+	.left_value = left_val,						       \
+	.right_text = right_str,					       \
+	.right_value = right_val					       \
+}
+
+struct kunit_binary_ptr_assert {
+	struct kunit_assert assert;
+	const char *operation;
+	const char *left_text;
+	const void *left_value;
+	const char *right_text;
+	const void *right_value;
+};
+
+void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
+				    struct string_stream *stream);
+
+#define KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT(test,			       \
+					    type,			       \
+					    op_str,			       \
+					    left_str,			       \
+					    left_val,			       \
+					    right_str,			       \
+					    right_val) {		       \
+	.assert = KUNIT_INIT_ASSERT_STRUCT(test,			       \
+					   type,			       \
+					   kunit_binary_ptr_assert_format),    \
+	.operation = op_str,						       \
+	.left_text = left_str,						       \
+	.left_value = left_val,						       \
+	.right_text = right_str,					       \
+	.right_value = right_val					       \
+}
+
+struct kunit_binary_str_assert {
+	struct kunit_assert assert;
+	const char *operation;
+	const char *left_text;
+	const char *left_value;
+	const char *right_text;
+	const char *right_value;
+};
+
+void kunit_binary_str_assert_format(const struct kunit_assert *assert,
+				    struct string_stream *stream);
+
+#define KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(test,			       \
+					    type,			       \
+					    op_str,			       \
+					    left_str,			       \
+					    left_val,			       \
+					    right_str,			       \
+					    right_val) {		       \
+	.assert = KUNIT_INIT_ASSERT_STRUCT(test,			       \
+					   type,			       \
+					   kunit_binary_str_assert_format),    \
+	.operation = op_str,						       \
+	.left_text = left_str,						       \
+	.left_value = left_val,						       \
+	.right_text = right_str,					       \
+	.right_value = right_val					       \
+}
+
+#endif /*  _KUNIT_ASSERT_H */
kunit/Makefile +2 -1
index 275b565a0e81f..6dcbe309036b8 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_KUNIT) +=			test.o \
-					string-stream.o
+					string-stream.o \
+					assert.o
kunit/assert.c new file +141 -0

141 lines not on this page · raw message

Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
Reviewed-by on v12 4/18
#16 lore raw

Re: [PATCH v12 04/18] kunit: test: add assertion printing library

Quoting Brendan Higgins (2019-08-12 11:24:07)

Quoted 6 lines

Add struct kunit_assert and friends which provide a structured way to capture data from an expectation or an assertion (introduced later in the series) so that it may be printed out in the event of a failure.

---

Just some minor nits below

Quoted 6 lines
include/kunit/assert.h new file +0 -0
new file mode 100644
index 0000000000000..55f1b88b0cb4d
@@ -0,0 +1,183 @@

[...]

include/kunit/assert.h line 59

Quoted 13 lines
include/kunit/assert.h new file +13 -0
+                           struct string_stream *stream);
+
+struct kunit_fail_assert {
+       struct kunit_assert assert;
+};
+
+void kunit_fail_assert_format(const struct kunit_assert *assert,
+                             struct string_stream *stream);
+
+#define KUNIT_INIT_FAIL_ASSERT_STRUCT(test, type) {                           \
+               .assert = KUNIT_INIT_ASSERT_STRUCT(test,                       \
+                                                  type,                       \
+                                                  kunit_fail_assert_format)   \

This one got indented one too many times?

Quoted 11 lines
include/kunit/assert.h new file +11 -0
+}
+
+struct kunit_unary_assert {
+       struct kunit_assert assert;
+       const char *condition;
+       bool expected_true;
+};
+
+void kunit_unary_assert_format(const struct kunit_assert *assert,
+                              struct string_stream *stream);
+

[...]

Quoted 6 lines
include/kunit/assert.h new file +6 -0
+#define KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(test,                             \
+                                           type,                              \
+                                           op_str,                            \
+                                           left_str,                          \
+                                           left_val,                          \
+                                           right_str,                         \

include/kunit/assert.h line 181

Quoted 10 lines
include/kunit/assert.h new file +10 -0
+                                           right_val) {                       \
+       .assert = KUNIT_INIT_ASSERT_STRUCT(test,                               \
+                                          type,                               \
+                                          kunit_binary_str_assert_format),    \
+       .operation = op_str,                                                   \
+       .left_text = left_str,                                                 \
+       .left_value = left_val,                                                \
+       .right_text = right_str,                                               \
+       .right_value = right_val                                               \
+}

It would be nice to have kernel doc on these macros so we know how to use them.

#17 lore raw

Re: [PATCH v12 04/18] kunit: test: add assertion printing library

Quoted 20 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 11:24:07)

Quoted 6 lines

Add struct kunit_assert and friends which provide a structured way to capture data from an expectation or an assertion (introduced later in the series) so that it may be printed out in the event of a failure.

---

Just some minor nits below

Quoted 6 lines
include/kunit/assert.h new file +0 -0
new file mode 100644
index 0000000000000..55f1b88b0cb4d
@@ -0,0 +1,183 @@

[...]

Quoted 15 lines

include/kunit/assert.h line 59

Quoted 13 lines
include/kunit/assert.h new file +13 -0
+                           struct string_stream *stream);
+
+struct kunit_fail_assert {
+       struct kunit_assert assert;
+};
+
+void kunit_fail_assert_format(const struct kunit_assert *assert,
+                             struct string_stream *stream);
+
+#define KUNIT_INIT_FAIL_ASSERT_STRUCT(test, type) {                           \
+               .assert = KUNIT_INIT_ASSERT_STRUCT(test,                       \
+                                                  type,                       \
+                                                  kunit_fail_assert_format)   \

This one got indented one too many times?

Not unless I have been using the wrong formatting for multiline macros. You can see this commit applied here:
https://kunit.googlesource.com/linux/+/870964da2990920030990dd1ffb647ef408e52df/include/kunit/assert.h#59

I have test, type, and kunit_fail_assert_format all column aligned (it just doesn't render nicely in the patch format).

Quoted 23 lines
Quoted 11 lines
include/kunit/assert.h new file +11 -0
+}
+
+struct kunit_unary_assert {
+       struct kunit_assert assert;
+       const char *condition;
+       bool expected_true;
+};
+
+void kunit_unary_assert_format(const struct kunit_assert *assert,
+                              struct string_stream *stream);
+

[...]

Quoted 11 lines
include/kunit/assert.h new file +11 -0
+#define KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(test,                             \
+                                           type,                              \
+                                           op_str,                            \
+                                           left_str,                          \
+                                           left_val,                          \
+                                           right_str,                         \
+                                           right_val) {                       \
+       .assert = KUNIT_INIT_ASSERT_STRUCT(test,                               \
+                                          type,                               \
+                                          kunit_binary_str_assert_format),    \
+       .operation = op_str,                                                   \
Quoted 8 lines

include/kunit/assert.h line 123

Quoted 5 lines
include/kunit/assert.h new file +5 -0
+       .left_text = left_str,                                                 \
+       .left_value = left_val,                                                \
+       .right_text = right_str,                                               \
+       .right_value = right_val                                               \
+}

It would be nice to have kernel doc on these macros so we know how to use them.

Sounds good. Will fix.

#18 lore raw

Re: [PATCH v12 04/18] kunit: test: add assertion printing library

Quoted 38 lines · Brendan Higgins
Quoted 20 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 11:24:07)

Quoted 6 lines

Add struct kunit_assert and friends which provide a structured way to capture data from an expectation or an assertion (introduced later in the series) so that it may be printed out in the event of a failure.

---

Just some minor nits below

Quoted 6 lines
include/kunit/assert.h new file +0 -0
new file mode 100644
index 0000000000000..55f1b88b0cb4d
@@ -0,0 +1,183 @@

[...]

Quoted 15 lines

include/kunit/assert.h line 59

Quoted 13 lines
include/kunit/assert.h new file +13 -0
+                           struct string_stream *stream);
+
+struct kunit_fail_assert {
+       struct kunit_assert assert;
+};
+
+void kunit_fail_assert_format(const struct kunit_assert *assert,
+                             struct string_stream *stream);
+
+#define KUNIT_INIT_FAIL_ASSERT_STRUCT(test, type) {                           \
+               .assert = KUNIT_INIT_ASSERT_STRUCT(test,                       \
+                                                  type,                       \
+                                                  kunit_fail_assert_format)   \

This one got indented one too many times?

Quoted 6 lines

Not unless I have been using the wrong formatting for multiline macros. You can see this commit applied here:
https://kunit.googlesource.com/linux/+/870964da2990920030990dd1ffb647ef408e52df/include/kunit/assert.h#59

I have test, type, and kunit_fail_assert_format all column aligned (it just doesn't render nicely in the patch format).

Disregard that last comment. I just looked at the line immediately above your comment and thought it looked correct. Sorry about that (you were pointing out that the .assert line looked wrong, correct?).

Quoted 33 lines
Quoted 23 lines
Quoted 11 lines
include/kunit/assert.h new file +11 -0
+}
+
+struct kunit_unary_assert {
+       struct kunit_assert assert;
+       const char *condition;
+       bool expected_true;
+};
+
+void kunit_unary_assert_format(const struct kunit_assert *assert,
+                              struct string_stream *stream);
+

[...]

Quoted 11 lines
include/kunit/assert.h new file +11 -0
+#define KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(test,                             \
+                                           type,                              \
+                                           op_str,                            \
+                                           left_str,                          \
+                                           left_val,                          \
+                                           right_str,                         \
+                                           right_val) {                       \
+       .assert = KUNIT_INIT_ASSERT_STRUCT(test,                               \
+                                          type,                               \
+                                          kunit_binary_str_assert_format),    \
+       .operation = op_str,                                                   \
Quoted 8 lines

include/kunit/assert.h line 123

Quoted 5 lines
include/kunit/assert.h new file +5 -0
+       .left_text = left_str,                                                 \
+       .left_value = left_val,                                                \
+       .right_text = right_str,                                               \
+       .right_value = right_val                                               \
+}

It would be nice to have kernel doc on these macros so we know how to use them.

Sounds good. Will fix.

#19 lore raw

Re: [PATCH v12 04/18] kunit: test: add assertion printing library

Quoting Brendan Higgins (2019-08-12 21:27:05)

Quoted 17 lines
Quoted 15 lines · Brendan Higgins
Quoted 8 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 11:24:07)

include/kunit/assert.h line 59

Quoted 4 lines
include/kunit/assert.h new file +4 -0
+#define KUNIT_INIT_FAIL_ASSERT_STRUCT(test, type) {                           \
+               .assert = KUNIT_INIT_ASSERT_STRUCT(test,                       \
+                                                  type,                       \
+                                                  kunit_fail_assert_format)   \

This one got indented one too many times?

Not unless I have been using the wrong formatting for multiline macros. You can see this commit applied here:
https://kunit.googlesource.com/linux/+/870964da2990920030990dd1ffb647ef408e52df/include/kunit/assert.h#59

Quoted 6 lines
Quoted 2 lines

I have test, type, and kunit_fail_assert_format all column aligned (it just doesn't render nicely in the patch format).

Disregard that last comment. I just looked at the line immediately above your comment and thought it looked correct. Sorry about that (you were pointing out that the .assert line looked wrong, correct?).

Yes. .assert is double tabbed?

#20 lore raw

Re: [PATCH v12 04/18] kunit: test: add assertion printing library

Quoted 19 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 21:27:05)

Quoted 17 lines
Quoted 15 lines · Brendan Higgins
Quoted 8 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 11:24:07)

include/kunit/assert.h line 59

Quoted 4 lines
include/kunit/assert.h new file +4 -0
+#define KUNIT_INIT_FAIL_ASSERT_STRUCT(test, type) {                           \
+               .assert = KUNIT_INIT_ASSERT_STRUCT(test,                       \
+                                                  type,                       \
+                                                  kunit_fail_assert_format)   \

This one got indented one too many times?

Not unless I have been using the wrong formatting for multiline macros. You can see this commit applied here:
https://kunit.googlesource.com/linux/+/870964da2990920030990dd1ffb647ef408e52df/include/kunit/assert.h#59

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I have test, type, and kunit_fail_assert_format all column aligned (it just doesn't render nicely in the patch format).

Disregard that last comment. I just looked at the line immediately above your comment and thought it looked correct. Sorry about that (you were pointing out that the .assert line looked wrong, correct?).

Yes. .assert is double tabbed?

Yes it is. Sorry about the confusion. Will fix.

#21 lore raw

PATCH v12 5/18 kunit: test: add the concept of expectations

In reply to Brendan Higgins

Add support for expectations, which allow properties to be specified and then verified in tests.

---
 include/kunit/test.h | 843 +++++++++++++++++++++++++++++++++++++++++++
 kunit/test.c         |  58 +++
 2 files changed, 901 insertions(+)
include/kunit/test.h +843 -0
index d0bf112910caf..2625bcfeb19ac 100644
@@ -9,8 +9,10 @@
 #ifndef _KUNIT_TEST_H
 #define _KUNIT_TEST_H
 
+#include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/slab.h>
+#include <kunit/assert.h>
 
 struct kunit_resource;
 
@@ -319,4 +321,845 @@ void __printf(3, 4) kunit_printk(const char *level,
 #define kunit_err(test, fmt, ...) \
 		kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
 
+/*
+ * Generates a compile-time warning in case of comparing incompatible types.
+ */
+#define __kunit_typecheck(lhs, rhs) \
+	((void) __typecheck(lhs, rhs))
+
+/**
+ * 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)
+
+void kunit_do_assertion(struct kunit *test,
+			struct kunit_assert *assert,
+			bool pass,
+			const char *fmt, ...);
+
+#define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do {  \
+	struct assert_class __assertion = INITIALIZER;			       \
+	kunit_do_assertion(test,					       \
+			   &__assertion.assert,				       \
+			   pass,					       \
+			   fmt,						       \
+			   ##__VA_ARGS__);				       \
+} while (0)
+
+
+#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...)		       \
+		KUNIT_ASSERTION(test,					       \
+				false,					       \
+				kunit_fail_assert,			       \
+				KUNIT_INIT_FAIL_ASSERT_STRUCT(test,	       \
+							      assert_type),    \
+				fmt,					       \
+				##__VA_ARGS__)
+
+/**
+ * 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_FAIL_ASSERTION(test,				       \
+				     KUNIT_EXPECTATION,			       \
+				     fmt,				       \
+				     ##__VA_ARGS__)
+
+#define KUNIT_UNARY_ASSERTION(test,					       \
+			      assert_type,				       \
+			      condition,				       \
+			      expected_true,				       \
+			      fmt,					       \
+			      ...)					       \
+		KUNIT_ASSERTION(test,					       \
+				!!(condition) == !!expected_true,	       \
+				kunit_unary_assert,			       \
+				KUNIT_INIT_UNARY_ASSERT_STRUCT(test,	       \
+							       assert_type,    \
+							       #condition,     \
+							       expected_true), \
+				fmt,					       \
+				##__VA_ARGS__)
+
+#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)       \
+		KUNIT_UNARY_ASSERTION(test,				       \
+				      assert_type,			       \
+				      condition,			       \
+				      true,				       \
+				      fmt,				       \
+				      ##__VA_ARGS__)
+
+#define KUNIT_TRUE_ASSERTION(test, assert_type, condition) \
+		KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, NULL)
+
+#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)      \
+		KUNIT_UNARY_ASSERTION(test,				       \
+				      assert_type,			       \
+				      condition,			       \
+				      false,				       \
+				      fmt,				       \
+				      ##__VA_ARGS__)
+
+#define KUNIT_FALSE_ASSERTION(test, assert_type, condition) \
+		KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, NULL)
+
+#define KUNIT_BINARY_CLASS \
+		kunit_binary_assert, KUNIT_INIT_BINARY_ASSERT_STRUCT
+#define KUNIT_BINARY_PTR_CLASS \
+		kunit_binary_ptr_assert, KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT
+
+/*
+ * A factory macro for defining the assertions andexpectations 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_BASE_BINARY_ASSERTION(test,				       \
+				    assert_class,			       \
+				    ASSERT_CLASS_INIT,			       \
+				    assert_type,			       \
+				    left,				       \
+				    op,					       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+do {									       \
+	typeof(left) __left = (left);					       \
+	typeof(right) __right = (right);				       \
+	__kunit_typecheck(__left, __right);				       \
+									       \
+	KUNIT_ASSERTION(test,						       \
+			__left op __right,				       \
+			assert_class,					       \
+			ASSERT_CLASS_INIT(test,				       \
+					  assert_type,			       \
+					  #op,				       \
+					  #left,			       \
+					  __left,			       \
+					  #right,			       \
+					  __right),			       \
+			fmt,						       \
+			##__VA_ARGS__);					       \
+} while (0)
+
+#define KUNIT_BINARY_ASSERTION(test, assert_type, left, op, right, fmt, ...)   \
+		KUNIT_BASE_BINARY_ASSERTION(test,			       \
+					    kunit_binary_assert,	       \
+					    KUNIT_INIT_BINARY_ASSERT_STRUCT,   \
+					    assert_type,		       \
+					    left,			       \
+					    op,				       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BASE_EQ_MSG_ASSERTION(test,				       \
+				    ASSERT_CLASS,			       \
+				    assert_type,			       \
+				    left,				       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+		KUNIT_BASE_BINARY_ASSERTION(test,			       \
+					    ASSERT_CLASS,		       \
+					    assert_type,		       \
+					    left, ==, right,		       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BASE_NE_MSG_ASSERTION(test,				       \
+				    ASSERT_CLASS,			       \
+				    assert_type,			       \
+				    left,				       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+		KUNIT_BASE_BINARY_ASSERTION(test,			       \
+					    ASSERT_CLASS,		       \
+					    assert_type,		       \
+					    left, !=, right,		       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BASE_LT_MSG_ASSERTION(test,				       \
+				    ASSERT_CLASS,			       \
+				    assert_type,			       \
+				    left,				       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+		KUNIT_BASE_BINARY_ASSERTION(test,			       \
+					    ASSERT_CLASS,		       \
+					    assert_type,		       \
+					    left, <, right,		       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BASE_LE_MSG_ASSERTION(test,				       \
+				    ASSERT_CLASS,			       \
+				    assert_type,			       \
+				    left,				       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+		KUNIT_BASE_BINARY_ASSERTION(test,			       \
+					    ASSERT_CLASS,		       \
+					    assert_type,		       \
+					    left, <=, right,		       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BASE_GT_MSG_ASSERTION(test,				       \
+				    ASSERT_CLASS,			       \
+				    assert_type,			       \
+				    left,				       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+		KUNIT_BASE_BINARY_ASSERTION(test,			       \
+					    ASSERT_CLASS,		       \
+					    assert_type,		       \
+					    left, >, right,		       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BASE_GE_MSG_ASSERTION(test,				       \
+				    ASSERT_CLASS,			       \
+				    assert_type,			       \
+				    left,				       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+		KUNIT_BASE_BINARY_ASSERTION(test,			       \
+					    ASSERT_CLASS,		       \
+					    assert_type,		       \
+					    left, >=, right,		       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
+		KUNIT_BASE_EQ_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_CLASS,		       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_EQ_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_EQ_MSG_ASSERTION(test,			       \
+					      assert_type,		       \
+					      left,			       \
+					      right,			       \
+					      NULL)
+
+#define KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BASE_EQ_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_PTR_CLASS,	       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_PTR_EQ_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
+		KUNIT_BASE_NE_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_CLASS,		       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_NE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_NE_MSG_ASSERTION(test,			       \
+					      assert_type,		       \
+					      left,			       \
+					      right,			       \
+					      NULL)
+
+#define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BASE_NE_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_PTR_CLASS,	       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_PTR_NE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_LT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
+		KUNIT_BASE_LT_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_CLASS,		       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_LT_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_LT_MSG_ASSERTION(test,			       \
+					      assert_type,		       \
+					      left,			       \
+					      right,			       \
+					      NULL)
+
+#define KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BASE_LT_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_PTR_CLASS,	       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_PTR_LT_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_LE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
+		KUNIT_BASE_LE_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_CLASS,		       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_LE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_LE_MSG_ASSERTION(test,			       \
+					      assert_type,		       \
+					      left,			       \
+					      right,			       \
+					      NULL)
+
+#define KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BASE_LE_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_PTR_CLASS,	       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_PTR_LE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_GT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
+		KUNIT_BASE_GT_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_CLASS,		       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_GT_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_GT_MSG_ASSERTION(test,			       \
+					      assert_type,		       \
+					      left,			       \
+					      right,			       \
+					      NULL)
+
+#define KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BASE_GT_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_PTR_CLASS,	       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_PTR_GT_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_GE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
+		KUNIT_BASE_GE_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_CLASS,		       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_GE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_GE_MSG_ASSERTION(test,			       \
+					      assert_type,		       \
+					      left,			       \
+					      right,			       \
+					      NULL)
+
+#define KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BASE_GE_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_PTR_CLASS,	       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_PTR_GE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_STR_ASSERTION(test,				       \
+				   assert_type,				       \
+				   left,				       \
+				   op,					       \
+				   right,				       \
+				   fmt,					       \
+				   ...)					       \
+do {									       \
+	typeof(left) __left = (left);					       \
+	typeof(right) __right = (right);				       \
+									       \
+	KUNIT_ASSERTION(test,						       \
+			strcmp(__left, __right) op 0,			       \
+			kunit_binary_str_assert,			       \
+			KUNIT_INIT_BINARY_ASSERT_STRUCT(test,		       \
+							assert_type,	       \
+							#op,		       \
+							#left,		       \
+							__left,		       \
+							#right,		       \
+							__right),	       \
+			fmt,						       \
+			##__VA_ARGS__);					       \
+} while (0)
+
+#define KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BINARY_STR_ASSERTION(test,			       \
+					   assert_type,			       \
+					   left, ==, right,		       \
+					   fmt,				       \
+					   ##__VA_ARGS__)
+
+#define KUNIT_BINARY_STR_EQ_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_STR_NE_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BINARY_STR_ASSERTION(test,			       \
+					   assert_type,			       \
+					   left, !=, right,		       \
+					   fmt,				       \
+					   ##__VA_ARGS__)
+
+#define KUNIT_BINARY_STR_NE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_STR_NE_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
+						assert_type,		       \
+						ptr,			       \
+						fmt,			       \
+						...)			       \
+do {									       \
+	typeof(ptr) __ptr = (ptr);					       \
+									       \
+	KUNIT_ASSERTION(test,						       \
+			!IS_ERR_OR_NULL(__ptr),				       \
+			kunit_ptr_not_err_assert,			       \
+			KUNIT_INIT_PTR_NOT_ERR_STRUCT(test,		       \
+						      assert_type,	       \
+						      #ptr,		       \
+						      __ptr),		       \
+			fmt,						       \
+			##__VA_ARGS__);					       \
+} while (0)
+
+#define KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, assert_type, ptr)	       \
+		KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,		       \
+							assert_type,	       \
+							ptr,		       \
+							NULL)
+
+/**
+ * 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_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)
+
+#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...)		       \
+		KUNIT_TRUE_MSG_ASSERTION(test,				       \
+					 KUNIT_EXPECTATION,		       \
+					 condition,			       \
+					 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_FALSE_ASSERTION(test, KUNIT_EXPECTATION, condition)
+
+#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...)		       \
+		KUNIT_FALSE_MSG_ASSERTION(test,				       \
+					  KUNIT_EXPECTATION,		       \
+					  condition,			       \
+					  fmt,				       \
+					  ##__VA_ARGS__)
+
+/**
+ * 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_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
+
+#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_EQ_MSG_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right,			       \
+					      fmt,			       \
+					      ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a pointer.
+ * @right: an arbitrary expression that evaluates to a pointer.
+ *
+ * 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_PTR_EQ(test, left, right)				       \
+		KUNIT_BINARY_PTR_EQ_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right)
+
+#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test,			       \
+						  KUNIT_EXPECTATION,	       \
+						  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_BINARY_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
+
+#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_NE_MSG_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right,			       \
+					      fmt,			       \
+					      ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a pointer.
+ * @right: an arbitrary expression that evaluates to a pointer.
+ *
+ * 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_PTR_NE(test, left, right)				       \
+		KUNIT_BINARY_PTR_NE_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right)
+
+#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test,			       \
+						  KUNIT_EXPECTATION,	       \
+						  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_BINARY_LT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
+
+#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_LT_MSG_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      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_BINARY_LE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
+
+#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_LE_MSG_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      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_BINARY_GT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
+
+#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_GT_MSG_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      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_BINARY_GE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
+
+#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_GE_MSG_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      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)				       \
+		KUNIT_BINARY_STR_EQ_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right)
+
+#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test,			       \
+						  KUNIT_EXPECTATION,	       \
+						  left,			       \
+						  right,		       \
+						  fmt,			       \
+						  ##__VA_ARGS__)
+
+/**
+ * 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)				       \
+		KUNIT_BINARY_STR_NE_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right)
+
+#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_STR_NE_MSG_ASSERTION(test,			       \
+						  KUNIT_EXPECTATION,	       \
+						  left,			       \
+						  right,		       \
+						  fmt,			       \
+						  ##__VA_ARGS__)
+
+/**
+ * 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)				       \
+		KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test,		       \
+						    KUNIT_EXPECTATION,	       \
+						    ptr)
+
+#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
+		KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,		       \
+							KUNIT_EXPECTATION,     \
+							ptr,		       \
+							fmt,		       \
+							##__VA_ARGS__)
+
 #endif /* _KUNIT_TEST_H */
kunit/test.c +58 -0

64 lines not on this page · raw message

Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
Reviewed-by on v12 5/18
#22 lore raw

Re: [PATCH v12 05/18] kunit: test: add the concept of expectations

Quoting Brendan Higgins (2019-08-12 11:24:08)

Quoted 6 lines

Add support for expectations, which allow properties to be specified and then verified in tests.

Just some minor nits again.

include/kunit/test.h line 15

Quoted 12 lines
include/kunit/test.h +2 -0
index d0bf112910caf..2625bcfeb19ac 100644
@@ -9,8 +9,10 @@
 #ifndef _KUNIT_TEST_H
 #define _KUNIT_TEST_H
 
+#include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/slab.h>
+#include <kunit/assert.h>

Can you alphabet sort these?

include/kunit/test.h void __printf(3, 4) kunit_printk(const char *level, · line 328

Quoted 12 lines
include/kunit/test.h +5 -0
 struct kunit_resource;
 
@@ -319,4 +321,845 @@ void __printf(3, 4) kunit_printk(const char *level,
 #define kunit_err(test, fmt, ...) \
                kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
 
+/*
+ * Generates a compile-time warning in case of comparing incompatible types.
+ */
+#define __kunit_typecheck(lhs, rhs) \
+       ((void) __typecheck(lhs, rhs))

Is there a reason why this can't be inlined and the __kunit_typecheck() macro can't be removed?

Quoted 4 lines
include/kunit/test.h +4 -0
+
+/**
+ * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
+ * @test: The test context object.

[...]

include/kunit/test.h void __printf(3, 4) kunit_printk(const char *level, · line 883

Quoted 10 lines

+ * @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*.
+ */

include/kunit/test.h +2 -0
+#define KUNIT_EXPECT_TRUE(test, condition) \
+               KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)

A lot of these macros seem double indented.

include/kunit/test.h void __printf(3, 4) kunit_printk(const char *level, · line 891

Quoted 8 lines
include/kunit/test.h +8 -0
+
+#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...)                      \
+               KUNIT_TRUE_MSG_ASSERTION(test,                                 \
+                                        KUNIT_EXPECTATION,                    \
+                                        condition,                            \
+                                        fmt,                                  \
+                                        ##__VA_ARGS__)
+

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#23 lore raw

Re: [PATCH v12 05/18] kunit: test: add the concept of expectations

Quoted 20 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 11:24:08)

Quoted 6 lines

Add support for expectations, which allow properties to be specified and then verified in tests.

Just some minor nits again.

Quoted 8 lines
include/kunit/test.h +0 -0
index d0bf112910caf..2625bcfeb19ac 100644
@@ -9,8 +9,10 @@
 #ifndef _KUNIT_TEST_H
 #define _KUNIT_TEST_H
 
Quoted 6 lines

include/kunit/test.h line 15

Quoted 4 lines
include/kunit/test.h +2 -0
+#include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/slab.h>
+#include <kunit/assert.h>

Can you alphabet sort these?

Sure. Will fix.

Quoted 7 lines
Quoted 7 lines
include/kunit/test.h +0 -0
 struct kunit_resource;
 
@@ -319,4 +321,845 @@ void __printf(3, 4) kunit_printk(const char *level,
 #define kunit_err(test, fmt, ...) \
                kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
 
Quoted 8 lines

include/kunit/test.h void __printf(3, 4) kunit_printk(const char *level, · line 328

Quoted 5 lines
include/kunit/test.h +5 -0
+/*
+ * Generates a compile-time warning in case of comparing incompatible types.
+ */
+#define __kunit_typecheck(lhs, rhs) \
+       ((void) __typecheck(lhs, rhs))

Is there a reason why this can't be inlined and the __kunit_typecheck() macro can't be removed?

No real reason anymore. I used it in multiple places before and we weren't sure if we wanted to stick with the warnings that __typecheck produces long term, but now that it is only used in one place, I guess that doesn't make sense anymore. Will fix.

Quoted 16 lines
Quoted 4 lines
include/kunit/test.h +4 -0
+
+/**
+ * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
+ * @test: The test context object.

[...]

include/kunit/test.h void __printf(3, 4) kunit_printk(const char *level, · line 883

Quoted 10 lines

+ * @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*.
+ */

include/kunit/test.h +2 -0
+#define KUNIT_EXPECT_TRUE(test, condition) \
+               KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)
Quoted 1 line

A lot of these macros seem double indented.

In a case you pointed out in the preceding patch, I was just keeping the arguments column aligned.

In this case I am just indenting two tabs for a line continuation. I thought I found other instances in the kernel that did this early on (and that's also what the Linux kernel vim plugin wanted me to do). After a couple of spot checks, it seems like one tab for this kind of line continuation seems more common. I personally don't feel strongly about any particular version. I just want to know now what the correct indentation is for macros before I go through and change them all.

I think there are three cases:

#define macro0(param0, param1) \
		a_really_long_macro(...)

In this first case, I use two tabs for the first indent, I think you are telling me this should be one tab.

#define macro1(param0, param1) {					       \
	statement_in_a_block0;						       \
	statement_in_a_block1;						       \
	...								       \
}

In this case, every line is in a block and is indented as it would be in a function body. I think you are okay with this, and now that I am thinking about it, what I think you are proposing for macro0 will make these two cases more consistent.

#define macro2(param0,							       \
	       param1,							       \
	       param2,							       \
	       param3,							       \
	       ...,							       \
	       paramn) ...						       \

In this last case, the body would be indented as in macro0, or macro1, but the parameters passed into the macro are column aligned, consistent with one of the acceptable ways of formatting function parameters that don't fit on a single line.

In all cases, I put 1 space in between the closing parameter paren and the line continuation \, if only one \ is needed. Otherwise, I align all the \s to the 80th column. Is this okay, or would you prefer that I align them all to the 80th column, or something else?

Quoted 8 lines
Quoted 8 lines
include/kunit/test.h +8 -0
+
+#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...)                      \
+               KUNIT_TRUE_MSG_ASSERTION(test,                                 \
+                                        KUNIT_EXPECTATION,                    \
+                                        condition,                            \
+                                        fmt,                                  \
+                                        ##__VA_ARGS__)
+
#24 lore raw

Re: [PATCH v12 05/18] kunit: test: add the concept of expectations

Quoting Brendan Higgins (2019-08-12 17:33:52)

Quoted 19 lines · Stephen Boyd
Quoted 6 lines

Quoting Brendan Higgins (2019-08-12 11:24:08)

include/kunit/test.h void __printf(3, 4) kunit_printk(const char *level, · line 883

Quoted 3 lines

+ */

include/kunit/test.h +2 -0
+#define KUNIT_EXPECT_TRUE(test, condition) \
+               KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)

A lot of these macros seem double indented.

In a case you pointed out in the preceding patch, I was just keeping the arguments column aligned.

In this case I am just indenting two tabs for a line continuation. I thought I found other instances in the kernel that did this early on (and that's also what the Linux kernel vim plugin wanted me to do). After a couple of spot checks, it seems like one tab for this kind of line continuation seems more common. I personally don't feel strongly about any particular version. I just want to know now what the correct indentation is for macros before I go through and change them all.

Quoted 7 lines

I think there are three cases:

#define macro0(param0, param1) \
                a_really_long_macro(...)

In this first case, I use two tabs for the first indent, I think you are telling me this should be one tab.

Yes. Should be one.

Quoted 24 lines
#define macro1(param0, param1) {                                               \
        statement_in_a_block0;                                                 \
        statement_in_a_block1;                                                 \
        ...                                                                    \

}

In this case, every line is in a block and is indented as it would be in a function body. I think you are okay with this, and now that I am thinking about it, what I think you are proposing for macro0 will make these two cases more consistent.

#define macro2(param0,                                                         \
               param1,                                                         \
               param2,                                                         \
               param3,                                                         \
               ...,                                                            \
               paramn) ...                                                     \

In this last case, the body would be indented as in macro0, or macro1, but the parameters passed into the macro are column aligned, consistent with one of the acceptable ways of formatting function parameters that don't fit on a single line.

Quoted 5 lines

In all cases, I put 1 space in between the closing parameter paren and the line continuation \, if only one \ is needed. Otherwise, I align all the \s to the 80th column. Is this okay, or would you prefer that I align them all to the 80th column, or something else?

This all sounds fine and I'm not nitpicking this style. Just the double tabs making lines longer than required.

#25 lore raw

Re: [PATCH v12 05/18] kunit: test: add the concept of expectations

Quoted 55 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 17:33:52)

Quoted 19 lines · Stephen Boyd
Quoted 6 lines

Quoting Brendan Higgins (2019-08-12 11:24:08)

include/kunit/test.h void __printf(3, 4) kunit_printk(const char *level, · line 883

Quoted 3 lines

+ */

include/kunit/test.h +2 -0
+#define KUNIT_EXPECT_TRUE(test, condition) \
+               KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)

A lot of these macros seem double indented.

In a case you pointed out in the preceding patch, I was just keeping the arguments column aligned.

In this case I am just indenting two tabs for a line continuation. I thought I found other instances in the kernel that did this early on (and that's also what the Linux kernel vim plugin wanted me to do). After a couple of spot checks, it seems like one tab for this kind of line continuation seems more common. I personally don't feel strongly about any particular version. I just want to know now what the correct indentation is for macros before I go through and change them all.

Quoted 7 lines

I think there are three cases:

#define macro0(param0, param1) \
                a_really_long_macro(...)

In this first case, I use two tabs for the first indent, I think you are telling me this should be one tab.

Yes. Should be one.

Quoted 24 lines
#define macro1(param0, param1) {                                               \
        statement_in_a_block0;                                                 \
        statement_in_a_block1;                                                 \
        ...                                                                    \

}

In this case, every line is in a block and is indented as it would be in a function body. I think you are okay with this, and now that I am thinking about it, what I think you are proposing for macro0 will make these two cases more consistent.

#define macro2(param0,                                                         \
               param1,                                                         \
               param2,                                                         \
               param3,                                                         \
               ...,                                                            \
               paramn) ...                                                     \

In this last case, the body would be indented as in macro0, or macro1, but the parameters passed into the macro are column aligned, consistent with one of the acceptable ways of formatting function parameters that don't fit on a single line.

Quoted 8 lines
Quoted 5 lines

In all cases, I put 1 space in between the closing parameter paren and the line continuation \, if only one \ is needed. Otherwise, I align all the \s to the 80th column. Is this okay, or would you prefer that I align them all to the 80th column, or something else?

This all sounds fine and I'm not nitpicking this style. Just the double tabs making lines longer than required.

Sounds good. Will do.

#26 lore raw

PATCH v12 6/18 kbuild: enable building KUnit

In reply to Brendan Higgins

KUnit is a new unit testing framework for the kernel and when used is built into the kernel as a part of it. Add KUnit to the root Kconfig and Makefile to allow it to be actually built.

---
 Kconfig  | 2 ++
 Makefile | 2 ++
 2 files changed, 4 insertions(+)
Kconfig +2 -0
index e10b3ee084d4d..47886dbd6c2a6 100644
@@ -32,3 +32,5 @@ source "lib/Kconfig"
 source "lib/Kconfig.debug"
 
 source "Documentation/Kconfig"
+
+source "kunit/Kconfig"
Makefile +2 -0
index 23cdf1f413646..3795d0a5d0376 100644
@@ -1005,6 +1005,8 @@ PHONY += prepare0
 ifeq ($(KBUILD_EXTMOD),)
 core-y		+= kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/
 
+core-$(CONFIG_KUNIT) += kunit/
+
 vmlinux-dirs	:= $(patsubst %/,%,$(filter %/, $(init-y) $(init-m) \
 		     $(core-y) $(core-m) $(drivers-y) $(drivers-m) \
 		     $(net-y) $(net-m) $(libs-y) $(libs-m) $(virt-y)))
Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
#27 lore raw

PATCH v12 7/18 kunit: test: add initial tests

In reply to Brendan Higgins

Add a test for string stream along with a simpler example.

---
 kunit/Kconfig              | 21 +++++++++
 kunit/Makefile             |  4 ++
 kunit/example-test.c       | 88 ++++++++++++++++++++++++++++++++++++++
 kunit/string-stream-test.c | 74 ++++++++++++++++++++++++++++++++
 4 files changed, 187 insertions(+)
 create mode 100644 kunit/example-test.c
 create mode 100644 kunit/string-stream-test.c
kunit/Kconfig +21 -0
index 330ae83527c23..8541ef95b65ad 100644
@@ -14,4 +14,25 @@ config KUNIT
 	  architectures. For more information, please see
 	  Documentation/dev-tools/kunit/.
 
+config KUNIT_TEST
+	bool "KUnit test for KUnit"
+	depends on KUNIT
+	help
+	  Enables the unit tests for the KUnit test framework. These tests test
+	  the KUnit test framework itself; the tests are both written using
+	  KUnit and test KUnit. This option should only be enabled for testing
+	  purposes by developers interested in testing that KUnit works as
+	  expected.
+
+config KUNIT_EXAMPLE_TEST
+	bool "Example test for KUnit"
+	depends on KUNIT
+	help
+	  Enables an example unit test that illustrates some of the basic
+	  features of KUnit. This test only exists to help new users understand
+	  what KUnit is and how it is used. Please refer to the example test
+	  itself, kunit/example-test.c, for more information. This option is
+	  intended for curious hackers who would like to understand how to use
+	  KUnit for kernel development.
+
 endmenu
kunit/Makefile +4 -0
index 6dcbe309036b8..4e46450bcb3a8 100644
@@ -1,3 +1,7 @@
 obj-$(CONFIG_KUNIT) +=			test.o \
 					string-stream.o \
 					assert.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 +74 -0

74 lines not on this page · raw message

Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
Reviewed-by on v12 7/18
#28 lore raw

Re: [PATCH v12 07/18] kunit: test: add initial tests

Quoting Brendan Higgins (2019-08-12 11:24:10)

Quoted 6 lines

Add a test for string stream along with a simpler example.

---

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#29 lore raw

PATCH v12 8/18 objtool: add kunit_try_catch_throw to the noreturn list

In reply to Brendan Higgins

Fix the following warning seen on GCC 7.3:
kunit/test-test.o: warning: objtool: kunit_test_unsuccessful_try() falls through to next function kunit_test_catch()

kunit_try_catch_throw is a function added in the following patch in this series; it allows KUnit, a unit testing framework for the kernel, to bail out of a broken test. As a consequence, it is a new __noreturn function that objtool thinks is broken (as seen above). So fix this warning by adding kunit_try_catch_throw to objtool's noreturn list.

---
 tools/objtool/check.c | 1 +
 1 file changed, 1 insertion(+)
tools/objtool/check.c +1 -0
index 176f2f0840609..0c8e17f946cda 100644
@@ -145,6 +145,7 @@ static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
 		"usercopy_abort",
 		"machine_real_restart",
 		"rewind_stack_do_exit",
+		"kunit_try_catch_throw",
 	};
 
 	if (!func)
Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
#30 lore raw

PATCH v12 9/18 kunit: test: add support for test abort

In reply to Brendan Higgins

Add support for aborting/bailing out of test cases, which is needed for implementing assertions.

An assertion is like an expectation, but bails out of the test case early if the assertion is not met. 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.

---
 include/kunit/test.h      |  14 +++
 include/kunit/try-catch.h |  69 +++++++++++++++
 kunit/Makefile            |   3 +-
 kunit/test.c              | 179 ++++++++++++++++++++++++++++++++++----
 kunit/try-catch.c         |  95 ++++++++++++++++++++
 5 files changed, 344 insertions(+), 16 deletions(-)
 create mode 100644 include/kunit/try-catch.h
 create mode 100644 kunit/try-catch.c
include/kunit/test.h +14 -0
index 2625bcfeb19ac..93381f841e09f 100644
@@ -13,6 +13,7 @@
 #include <linux/types.h>
 #include <linux/slab.h>
 #include <kunit/assert.h>
+#include <kunit/try-catch.h>
 
 struct kunit_resource;
 
@@ -167,6 +168,7 @@ struct kunit {
 
 	/* private: internal use only. */
 	const char *name; /* Read only after initialization! */
+	struct kunit_try_catch try_catch;
 	/*
 	 * success starts as true, and may only be set to false during a test
 	 * case; thus, it is safe to update this across multiple threads using
@@ -176,6 +178,11 @@ struct kunit {
 	 */
 	bool success; /* Read only after test_case finishes! */
 	spinlock_t lock; /* Gaurds all mutable test state. */
+	/*
+	 * death_test may be both set and unset from multiple threads in a test
+	 * case.
+	 */
+	bool death_test; /* Protected by lock. */
 	/*
 	 * Because resources is a list that may be updated multiple times (with
 	 * new resources) from any thread associated with a test case, we must
@@ -184,6 +191,13 @@ struct kunit {
 	struct list_head resources; /* Protected by lock. */
 };
 
+static inline void kunit_set_death_test(struct kunit *test, bool death_test)
+{
+	spin_lock(&test->lock);
+	test->death_test = death_test;
+	spin_unlock(&test->lock);
+}
+
 void kunit_init_test(struct kunit *test, const char *name);
 
 int kunit_run_tests(struct kunit_suite *suite);
include/kunit/try-catch.h new file +69 -0

69 lines not on this page · raw message

kunit/Makefile +2 -1
index 4e46450bcb3a8..c9176c9c578c6 100644
@@ -1,6 +1,7 @@
 obj-$(CONFIG_KUNIT) +=			test.o \
 					string-stream.o \
-					assert.o
+					assert.o \
+					try-catch.o
 
 obj-$(CONFIG_KUNIT_TEST) +=		string-stream-test.o
 
kunit/test.c +164 -15

232 lines not on this page · raw message

kunit/try-catch.c new file +95 -0

95 lines not on this page · raw message

Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
#31 lore raw

Re: [PATCH v12 09/18] kunit: test: add support for test abort

Quoting Brendan Higgins (2019-08-12 11:24:12)

Quoted 33 lines
include/kunit/test.h +7 -0
index 2625bcfeb19ac..93381f841e09f 100644
@@ -13,6 +13,7 @@
 #include <linux/types.h>
 #include <linux/slab.h>
 #include <kunit/assert.h>
+#include <kunit/try-catch.h>
 
 struct kunit_resource;
 
@@ -167,6 +168,7 @@ struct kunit {
 
        /* private: internal use only. */
        const char *name; /* Read only after initialization! */
+       struct kunit_try_catch try_catch;
        /*
         * success starts as true, and may only be set to false during a test
         * case; thus, it is safe to update this across multiple threads using
@@ -176,6 +178,11 @@ struct kunit {
         */
        bool success; /* Read only after test_case finishes! */
        spinlock_t lock; /* Gaurds all mutable test state. */
+       /*
+        * death_test may be both set and unset from multiple threads in a test
+        * case.
+        */
+       bool death_test; /* Protected by lock. */
        /*
         * Because resources is a list that may be updated multiple times (with
         * new resources) from any thread associated with a test case, we must

include/kunit/test.h struct kunit { · line 199

Quoted 9 lines
include/kunit/test.h +6 -0
@@ -184,6 +191,13 @@ struct kunit {
        struct list_head resources; /* Protected by lock. */
 };
 
+static inline void kunit_set_death_test(struct kunit *test, bool death_test)
+{
+       spin_lock(&test->lock);
+       test->death_test = death_test;
+       spin_unlock(&test->lock);
+}

These getters and setters are using spinlocks again. It doesn't make any sense. It probably needs a rework like was done for the other bool member, success.

Quoted 17 lines
include/kunit/test.h +1 -0
+
 void kunit_init_test(struct kunit *test, const char *name);
 
 int kunit_run_tests(struct kunit_suite *suite);
include/kunit/try-catch.h new file +7 -0
new file mode 100644
index 0000000000000..8a414a9af0b64
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * An API to allow a function, that may fail, to be executed, and recover in a
+ * controlled manner.
+ *
+ * Copyright (C) 2019, Google LLC.
+ * Author: Brendan Higgins <[email protected]>

include/kunit/try-catch.h line 17

Quoted 10 lines
include/kunit/try-catch.h new file +10 -0
+ */
+
+#ifndef _KUNIT_TRY_CATCH_H
+#define _KUNIT_TRY_CATCH_H
+
+#include <linux/types.h>
+
+typedef void (*kunit_try_catch_func_t)(void *);
+
+struct kunit;

Forward declare struct completion?

Quoted 19 lines
include/kunit/try-catch.h new file +19 -0
+
+/*
+ * struct kunit_try_catch - provides a generic way to run code which might fail.
+ * @context: used to pass user data to the try and catch functions.
+ *
+ * kunit_try_catch provides a generic, architecture independent way to execute
+ * an arbitrary function of type kunit_try_catch_func_t which may bail out by
+ * calling kunit_try_catch_throw(). If kunit_try_catch_throw() is called, @try
+ * is stopped at the site of invocation and @catch is catch is called.
+ *
+ * struct kunit_try_catch provides a generic interface for the functionality
+ * needed to implement kunit->abort() which in turn is needed for implementing
+ * assertions. Assertions allow stating a precondition for a test simplifying
+ * how test cases are written and presented.
+ *
+ * Assertions are like expectations, except they abort (call
+ * kunit_try_catch_throw()) when the specified condition is not met. This is
+ * useful when you look at a test case as a logical statement about some piece
+ * of code, where assertions are the premises for the test case, and the

include/kunit/try-catch.h line 46

Quoted 10 lines
include/kunit/try-catch.h new file +10 -0
+ * conclusion is a set of predicates, rather expectations, that must all be
+ * true. If your premises are violated, it does not makes sense to continue.
+ */
+struct kunit_try_catch {
+       /* private: internal use only. */
+       struct kunit *test;
+       struct completion *try_completion;
+       int try_result;
+       kunit_try_catch_func_t try;
+       kunit_try_catch_func_t catch;

Can these other variables be documented in the kernel doc? And should context be marked as 'public'?

Quoted 9 lines
include/kunit/try-catch.h new file +9 -0
+       void *context;
+};
+
+void kunit_try_catch_init(struct kunit_try_catch *try_catch,
+                         struct kunit *test,
+                         kunit_try_catch_func_t try,
+                         kunit_try_catch_func_t catch);
+
+void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context);

include/kunit/try-catch.h line 65

Quoted 10 lines
include/kunit/try-catch.h new file +10 -0
+
+void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch);
+
+static inline int kunit_try_catch_get_result(struct kunit_try_catch *try_catch)
+{
+       return try_catch->try_result;
+}
+
+/*
+ * Exposed for testing only.

Ugh that's sad. I hope we don't expose more functions just for testing in other cases.

Quoted 42 lines
include/kunit/try-catch.h new file +4 -0
+ */
+void kunit_generic_try_catch_init(struct kunit_try_catch *try_catch);
+
+#endif /* _KUNIT_TRY_CATCH_H */
kunit/test.c +16 -0
index e5080a2c6b29c..995cb53fe4ee9 100644
@@ -7,13 +7,26 @@
  */
 
 #include <linux/kernel.h>
+#include <linux/sched/debug.h>
 #include <kunit/test.h>
+#include <kunit/try-catch.h>
 
 static void kunit_set_failure(struct kunit *test)
 {
        WRITE_ONCE(test->success, false);
 }
 
+static bool kunit_get_death_test(struct kunit *test)
+{
+       bool death_test;
+
+       spin_lock(&test->lock);
+       death_test = test->death_test;
+       spin_unlock(&test->lock);
+
+       return death_test;
+}
+
 static int kunit_vprintk_emit(int level, const char *fmt, va_list args)
 {
        return vprintk_emit(0, level, NULL, 0, fmt, args);
@@ -158,6 +171,21 @@ static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
        kunit_print_string_stream(test, stream);
 }
 
+void __noreturn kunit_abort(struct kunit *test)
+{
+       kunit_set_death_test(test, true);

kunit/test.c static void kunit_fail(struct kunit *test, struct kunit_assert *assert) · line 186

Quoted 10 lines
kunit/test.c +10 -0
+
+       kunit_try_catch_throw(&test->try_catch);
+
+       /*
+        * Throw could not abort from test.
+        *
+        * XXX: we should never reach this line! As kunit_try_catch_throw is
+        * marked __noreturn.
+        */
+       WARN_ONCE(true, "Throw could not abort from test!\n");

Should this just be a BUG_ON? It's supposedly impossible.

Quoted 72 lines
kunit/test.c +30 -13
+}
+
 void kunit_do_assertion(struct kunit *test,
                        struct kunit_assert *assert,
                        bool pass,
@@ -176,6 +204,9 @@ void kunit_do_assertion(struct kunit *test,
        kunit_fail(test, assert);
 
        va_end(args);
+
+       if (assert->type == KUNIT_ASSERTION)
+               kunit_abort(test);
 }
 
 void kunit_init_test(struct kunit *test, const char *name)
@@ -184,36 +215,154 @@ void kunit_init_test(struct kunit *test, const char *name)
        INIT_LIST_HEAD(&test->resources);
        test->name = name;
        test->success = true;
+       test->death_test = false;
 }
 
 /*
- * Performs all logic to run a test case.
+ * Initializes and runs test case. Does not clean up or do post validations.
  */
-static void kunit_run_case(struct kunit_suite *suite,
-                          struct kunit_case *test_case)
+static void kunit_run_case_internal(struct kunit *test,
+                                   struct kunit_suite *suite,
+                                   struct kunit_case *test_case)
 {
-       struct kunit test;
-
-       kunit_init_test(&test, test_case->name);
-
        if (suite->init) {
                int ret;
 
-               ret = suite->init(&test);
+               ret = suite->init(test);
                if (ret) {
-                       kunit_err(&test, "failed to initialize: %d\n", ret);
-                       kunit_set_failure(&test);
-                       test_case->success = test.success;
+                       kunit_err(test, "failed to initialize: %d\n", ret);
+                       kunit_set_failure(test);
                        return;
                }
        }
 
-       test_case->run_case(&test);
+       test_case->run_case(test);
+}
+
+static void kunit_case_internal_cleanup(struct kunit *test)
+{
+       kunit_cleanup(test);
+}
 
+/*
+ * Performs post validations and cleanup after a test case was run.
+ * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
+ */
+static void kunit_run_case_cleanup(struct kunit *test,
+                                  struct kunit_suite *suite)
+{
        if (suite->exit)
-               suite->exit(&test);
+               suite->exit(test);
+
+       kunit_case_internal_cleanup(test);

kunit/test.c void kunit_init_test(struct kunit *test, const char *name) · line 267

Quoted 10 lines
kunit/test.c +10 -0
+}
+
+/*
+ * Handles an unexpected crash in a test case.
+ */
+static void kunit_handle_test_crash(struct kunit *test,
+                                  struct kunit_suite *suite,
+                                  struct kunit_case *test_case)
+{
+       kunit_err(test, "kunit test case crashed!");

Does this need a newline?

Quoted 45 lines
kunit/test.c +45 -0
+       /*
+        * TODO([email protected]): This prints the stack trace up
+        * through this frame, not up to the frame that caused the crash.
+        */
+       show_stack(NULL, NULL);
+
+       kunit_case_internal_cleanup(test);
+}
+
+struct kunit_try_catch_context {
+       struct kunit *test;
+       struct kunit_suite *suite;
+       struct kunit_case *test_case;
+};
+
+static void kunit_try_run_case(void *data)
+{
+       struct kunit_try_catch_context *ctx = data;
+       struct kunit *test = ctx->test;
+       struct kunit_suite *suite = ctx->suite;
+       struct kunit_case *test_case = ctx->test_case;
+
+       /*
+        * kunit_run_case_internal may encounter a fatal error; if it does,
+        * abort will be called, this thread will exit, and finally the parent
+        * thread will resume control and handle any necessary clean up.
+        */
+       kunit_run_case_internal(test, suite, test_case);
+       /* This line may never be reached. */
+       kunit_run_case_cleanup(test, suite);
+}
+
+static void kunit_catch_run_case(void *data)
+{
+       struct kunit_try_catch_context *ctx = data;
+       struct kunit *test = ctx->test;
+       struct kunit_suite *suite = ctx->suite;
+       struct kunit_case *test_case = ctx->test_case;
+       int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
+
+       if (try_exit_code) {
+               kunit_set_failure(test);
+               /*
+                * Test case could not finish, we have no idea what state it is
+                * in, so don't do clean up.

kunit/test.c void kunit_init_test(struct kunit *test, const char *name) · line 322

Quoted 10 lines
kunit/test.c +10 -0
+                */
+               if (try_exit_code == -ETIMEDOUT)
+                       kunit_err(test, "test case timed out\n");
+               /*
+                * Unknown internal error occurred preventing test case from
+                * running, so there is nothing to clean up.
+                */
+               else
+                       kunit_err(test, "internal error occurred preventing test case from running: %d\n",
+                                 try_exit_code);

Nitpick: I would add braces here because you make the if statement into multi-line arms for each case.

Quoted 8 lines
kunit/test.c +8 -0
+               return;
+       }
+
+       if (kunit_get_death_test(test)) {
+               /*
+                * EXPECTED DEATH: kunit_run_case_internal encountered
+                * anticipated fatal error. Everything should be in a safe
+                * state.

kunit/test.c void kunit_init_test(struct kunit *test, const char *name) · line 340

Quoted 10 lines
kunit/test.c +10 -0
+                */
+               kunit_run_case_cleanup(test, suite);
+       } else {
+               /*
+                * UNEXPECTED DEATH: kunit_run_case_internal encountered an
+                * unanticipated fatal error. We have no idea what the state of
+                * the test case is in.
+                */
+               kunit_handle_test_crash(test, suite, test_case);
+               kunit_set_failure(test);

Like was done here.

kunit/test.c void kunit_init_test(struct kunit *test, const char *name) · line 346

Quoted 6 lines
kunit/test.c +6 -0
+       }
+}
+
+/*
+ * Performs all logic to run a test case. It also catches most errors that
+ * occurs in a test case and reports them as failures.

s/occurs/occur/

Quoted 2 lines
kunit/test.c +2 -0
+ */
+static void kunit_run_case_catch_errors(struct kunit_suite *suite,

[...]

Quoted 63 lines
kunit/try-catch.c new file +57 -0
new file mode 100644
index 0000000000000..de580f074387b
@@ -0,0 +1,95 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * An API to allow a function, that may fail, to be executed, and recover in a
+ * controlled manner.
+ *
+ * Copyright (C) 2019, Google LLC.
+ * Author: Brendan Higgins <[email protected]>
+ */
+
+#include <kunit/try-catch.h>
+#include <kunit/test.h>
+#include <linux/completion.h>
+#include <linux/kthread.h>
+
+void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch)
+{
+       try_catch->try_result = -EFAULT;
+       complete_and_exit(try_catch->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->try_completion, 0);
+}
+
+void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
+{
+       DECLARE_COMPLETION_ONSTACK(try_completion);
+       struct kunit *test = try_catch->test;
+       struct task_struct *task_struct;
+       int exit_code, status;
+
+       try_catch->context = context;
+       try_catch->try_completion = &try_completion;
+       try_catch->try_result = 0;
+       task_struct = kthread_run(kunit_generic_run_threadfn_adapter,
+                                 try_catch,
+                                 "kunit_try_catch_thread");
+       if (IS_ERR(task_struct)) {
+               try_catch->catch(try_catch->context);
+               return;
+       }
+
+       /*
+        * TODO([email protected]): We should probably have some type of
+        * variable 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
+        * default timeout value kind of like what Bazel does:
+        * https://docs.bazel.build/versions/master/be/common-definitions.html#test.size

kunit/try-catch.c line 67

Quoted 10 lines
kunit/try-catch.c new file +10 -0
+        * 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
+        */
+       status = wait_for_completion_timeout(&try_completion,
+                                            300 * MSEC_PER_SEC); /* 5 min */
+       if (status < 0) {

wait_for_completion_timeout() doesn't return a negative value on timeout. It returns 0. Please rename 'status' to 'time_remaining' and test with if (!time_remaining) instead or some other suitably named variable name indicating that the return value is the time remaining before the timeout.

May also want to clamp this to the hung task timeout value, which is typically less than 5 minutes. Otherwise, the hung task detector may find the problem first before this timeout happens.

kunit/try-catch.c line 70

Quoted 3 lines
kunit/try-catch.c new file +3 -0
+               kunit_err(test, "try timed out\n");
+               try_catch->try_result = -ETIMEDOUT;
+       }

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#32 lore raw

Re: [PATCH v12 09/18] kunit: test: add support for test abort

Quoted 38 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 11:24:12)

Quoted 36 lines
include/kunit/test.h +7 -0
index 2625bcfeb19ac..93381f841e09f 100644
@@ -13,6 +13,7 @@
 #include <linux/types.h>
 #include <linux/slab.h>
 #include <kunit/assert.h>
+#include <kunit/try-catch.h>
 
 struct kunit_resource;
 
@@ -167,6 +168,7 @@ struct kunit {
 
        /* private: internal use only. */
        const char *name; /* Read only after initialization! */
+       struct kunit_try_catch try_catch;
        /*
         * success starts as true, and may only be set to false during a test
         * case; thus, it is safe to update this across multiple threads using
@@ -176,6 +178,11 @@ struct kunit {
         */
        bool success; /* Read only after test_case finishes! */
        spinlock_t lock; /* Gaurds all mutable test state. */
+       /*
+        * death_test may be both set and unset from multiple threads in a test
+        * case.
+        */
+       bool death_test; /* Protected by lock. */
        /*
         * Because resources is a list that may be updated multiple times (with
         * new resources) from any thread associated with a test case, we must
@@ -184,6 +191,13 @@ struct kunit {
        struct list_head resources; /* Protected by lock. */
 };
 
Quoted 10 lines

include/kunit/test.h struct kunit { · line 199

Quoted 6 lines
include/kunit/test.h +6 -0
+static inline void kunit_set_death_test(struct kunit *test, bool death_test)
+{
+       spin_lock(&test->lock);
+       test->death_test = death_test;
+       spin_unlock(&test->lock);
+}

These getters and setters are using spinlocks again. It doesn't make any sense. It probably needs a rework like was done for the other bool member, success.

No, this is intentional. death_test can transition from false to true and then back to false within the same test. Maybe that deserves a comment?

Quoted 21 lines
Quoted 21 lines
include/kunit/test.h +1 -0
+
 void kunit_init_test(struct kunit *test, const char *name);
 
 int kunit_run_tests(struct kunit_suite *suite);
include/kunit/try-catch.h new file +11 -0
new file mode 100644
index 0000000000000..8a414a9af0b64
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * An API to allow a function, that may fail, to be executed, and recover in a
+ * controlled manner.
+ *
+ * Copyright (C) 2019, Google LLC.
+ * Author: Brendan Higgins <[email protected]>
+ */
+
+#ifndef _KUNIT_TRY_CATCH_H
+#define _KUNIT_TRY_CATCH_H
Quoted 8 lines

include/kunit/try-catch.h line 17

Quoted 6 lines
include/kunit/try-catch.h new file +6 -0
+
+#include <linux/types.h>
+
+typedef void (*kunit_try_catch_func_t)(void *);
+
+struct kunit;

Forward declare struct completion?

Sure. Will do.

Quoted 24 lines
Quoted 24 lines
include/kunit/try-catch.h new file +24 -0
+
+/*
+ * struct kunit_try_catch - provides a generic way to run code which might fail.
+ * @context: used to pass user data to the try and catch functions.
+ *
+ * kunit_try_catch provides a generic, architecture independent way to execute
+ * an arbitrary function of type kunit_try_catch_func_t which may bail out by
+ * calling kunit_try_catch_throw(). If kunit_try_catch_throw() is called, @try
+ * is stopped at the site of invocation and @catch is catch is called.
+ *
+ * struct kunit_try_catch provides a generic interface for the functionality
+ * needed to implement kunit->abort() which in turn is needed for implementing
+ * assertions. Assertions allow stating a precondition for a test simplifying
+ * how test cases are written and presented.
+ *
+ * Assertions are like expectations, except they abort (call
+ * kunit_try_catch_throw()) when the specified condition is not met. This is
+ * useful when you look at a test case as a logical statement about some piece
+ * of code, where assertions are the premises for the test case, and the
+ * conclusion is a set of predicates, rather expectations, that must all be
+ * true. If your premises are violated, it does not makes sense to continue.
+ */
+struct kunit_try_catch {
+       /* private: internal use only. */
Quoted 8 lines

include/kunit/try-catch.h line 46

Quoted 5 lines
include/kunit/try-catch.h new file +5 -0
+       struct kunit *test;
+       struct completion *try_completion;
+       int try_result;
+       kunit_try_catch_func_t try;
+       kunit_try_catch_func_t catch;

Can these other variables be documented in the kernel doc? And should context be marked as 'public'?

Sure, I can document them.

But I don't think context should be public; it should only be accessed by kunit_try_catch_* functions. context should only be populated by *_init, and will be passed into *try and *catch when they are called internally.

Quoted 14 lines
Quoted 14 lines
include/kunit/try-catch.h new file +14 -0
+       void *context;
+};
+
+void kunit_try_catch_init(struct kunit_try_catch *try_catch,
+                         struct kunit *test,
+                         kunit_try_catch_func_t try,
+                         kunit_try_catch_func_t catch);
+
+void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context);
+
+void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch);
+
+static inline int kunit_try_catch_get_result(struct kunit_try_catch *try_catch)
+{
Quoted 8 lines

include/kunit/try-catch.h line 65

Quoted 5 lines
include/kunit/try-catch.h new file +5 -0
+       return try_catch->try_result;
+}
+
+/*
+ * Exposed for testing only.

Ugh that's sad. I hope we don't expose more functions just for testing in other cases.

I don't think I am in any other cases in this patchset. I agree that it is generally bad to expose a private function for testing purposes, but I didn't see a better way here.

Quoted 39 lines
Quoted 39 lines
include/kunit/try-catch.h new file +4 -0
+ */
+void kunit_generic_try_catch_init(struct kunit_try_catch *try_catch);
+
+#endif /* _KUNIT_TRY_CATCH_H */
kunit/test.c +13 -0
index e5080a2c6b29c..995cb53fe4ee9 100644
@@ -7,13 +7,26 @@
  */
 
 #include <linux/kernel.h>
+#include <linux/sched/debug.h>
 #include <kunit/test.h>
+#include <kunit/try-catch.h>
 
 static void kunit_set_failure(struct kunit *test)
 {
        WRITE_ONCE(test->success, false);
 }
 
+static bool kunit_get_death_test(struct kunit *test)
+{
+       bool death_test;
+
+       spin_lock(&test->lock);
+       death_test = test->death_test;
+       spin_unlock(&test->lock);
+
+       return death_test;
+}
+
 static int kunit_vprintk_emit(int level, const char *fmt, va_list args)
 {
        return vprintk_emit(0, level, NULL, 0, fmt, args);
@@ -158,6 +171,21 @@ static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
        kunit_print_string_stream(test, stream);
 }
 
Quoted 15 lines

kunit/test.c static void kunit_fail(struct kunit *test, struct kunit_assert *assert) · line 186

Quoted 13 lines
kunit/test.c +13 -0
+void __noreturn kunit_abort(struct kunit *test)
+{
+       kunit_set_death_test(test, true);
+
+       kunit_try_catch_throw(&test->try_catch);
+
+       /*
+        * Throw could not abort from test.
+        *
+        * XXX: we should never reach this line! As kunit_try_catch_throw is
+        * marked __noreturn.
+        */
+       WARN_ONCE(true, "Throw could not abort from test!\n");

Should this just be a BUG_ON? It's supposedly impossible.

It should be impossible; it will only reach this line if there is a bug in kunit_try_catch_throw. The reason I didn't use BUG_ON was because I previously got yelled at for having BUG_ON in this code path.

Nevertheless, I think BUG_ON is more correct, so if you will stand by it, then that's what I will do.

Quoted 76 lines
Quoted 76 lines
kunit/test.c +34 -13
+}
+
 void kunit_do_assertion(struct kunit *test,
                        struct kunit_assert *assert,
                        bool pass,
@@ -176,6 +204,9 @@ void kunit_do_assertion(struct kunit *test,
        kunit_fail(test, assert);
 
        va_end(args);
+
+       if (assert->type == KUNIT_ASSERTION)
+               kunit_abort(test);
 }
 
 void kunit_init_test(struct kunit *test, const char *name)
@@ -184,36 +215,154 @@ void kunit_init_test(struct kunit *test, const char *name)
        INIT_LIST_HEAD(&test->resources);
        test->name = name;
        test->success = true;
+       test->death_test = false;
 }
 
 /*
- * Performs all logic to run a test case.
+ * Initializes and runs test case. Does not clean up or do post validations.
  */
-static void kunit_run_case(struct kunit_suite *suite,
-                          struct kunit_case *test_case)
+static void kunit_run_case_internal(struct kunit *test,
+                                   struct kunit_suite *suite,
+                                   struct kunit_case *test_case)
 {
-       struct kunit test;
-
-       kunit_init_test(&test, test_case->name);
-
        if (suite->init) {
                int ret;
 
-               ret = suite->init(&test);
+               ret = suite->init(test);
                if (ret) {
-                       kunit_err(&test, "failed to initialize: %d\n", ret);
-                       kunit_set_failure(&test);
-                       test_case->success = test.success;
+                       kunit_err(test, "failed to initialize: %d\n", ret);
+                       kunit_set_failure(test);
                        return;
                }
        }
 
-       test_case->run_case(&test);
+       test_case->run_case(test);
+}
+
+static void kunit_case_internal_cleanup(struct kunit *test)
+{
+       kunit_cleanup(test);
+}
 
+/*
+ * Performs post validations and cleanup after a test case was run.
+ * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
+ */
+static void kunit_run_case_cleanup(struct kunit *test,
+                                  struct kunit_suite *suite)
+{
        if (suite->exit)
-               suite->exit(&test);
+               suite->exit(test);
+
+       kunit_case_internal_cleanup(test);
+}
+
+/*
+ * Handles an unexpected crash in a test case.
Quoted 8 lines

kunit/test.c void kunit_init_test(struct kunit *test, const char *name) · line 267

Quoted 6 lines
kunit/test.c +6 -0
+ */
+static void kunit_handle_test_crash(struct kunit *test,
+                                  struct kunit_suite *suite,
+                                  struct kunit_case *test_case)
+{
+       kunit_err(test, "kunit test case crashed!");

Does this need a newline?

Yep, nice catch. I thought I grepped for all the instance a while ago, but I apparently missed this one.

Quoted 50 lines
Quoted 50 lines
kunit/test.c +50 -0
+       /*
+        * TODO([email protected]): This prints the stack trace up
+        * through this frame, not up to the frame that caused the crash.
+        */
+       show_stack(NULL, NULL);
+
+       kunit_case_internal_cleanup(test);
+}
+
+struct kunit_try_catch_context {
+       struct kunit *test;
+       struct kunit_suite *suite;
+       struct kunit_case *test_case;
+};
+
+static void kunit_try_run_case(void *data)
+{
+       struct kunit_try_catch_context *ctx = data;
+       struct kunit *test = ctx->test;
+       struct kunit_suite *suite = ctx->suite;
+       struct kunit_case *test_case = ctx->test_case;
+
+       /*
+        * kunit_run_case_internal may encounter a fatal error; if it does,
+        * abort will be called, this thread will exit, and finally the parent
+        * thread will resume control and handle any necessary clean up.
+        */
+       kunit_run_case_internal(test, suite, test_case);
+       /* This line may never be reached. */
+       kunit_run_case_cleanup(test, suite);
+}
+
+static void kunit_catch_run_case(void *data)
+{
+       struct kunit_try_catch_context *ctx = data;
+       struct kunit *test = ctx->test;
+       struct kunit_suite *suite = ctx->suite;
+       struct kunit_case *test_case = ctx->test_case;
+       int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
+
+       if (try_exit_code) {
+               kunit_set_failure(test);
+               /*
+                * Test case could not finish, we have no idea what state it is
+                * in, so don't do clean up.
+                */
+               if (try_exit_code == -ETIMEDOUT)
+                       kunit_err(test, "test case timed out\n");
+               /*
+                * Unknown internal error occurred preventing test case from
Quoted 8 lines

kunit/test.c void kunit_init_test(struct kunit *test, const char *name) · line 322

Quoted 5 lines
kunit/test.c +5 -0
+                * running, so there is nothing to clean up.
+                */
+               else
+                       kunit_err(test, "internal error occurred preventing test case from running: %d\n",
+                                 try_exit_code);

Nitpick: I would add braces here because you make the if statement into multi-line arms for each case.

Will do. I think it looks better with braces anyway.

Quoted 19 lines
Quoted 8 lines
kunit/test.c +8 -0
+               return;
+       }
+
+       if (kunit_get_death_test(test)) {
+               /*
+                * EXPECTED DEATH: kunit_run_case_internal encountered
+                * anticipated fatal error. Everything should be in a safe
+                * state.

kunit/test.c void kunit_init_test(struct kunit *test, const char *name) · line 340

Quoted 10 lines
kunit/test.c +10 -0
+                */
+               kunit_run_case_cleanup(test, suite);
+       } else {
+               /*
+                * UNEXPECTED DEATH: kunit_run_case_internal encountered an
+                * unanticipated fatal error. We have no idea what the state of
+                * the test case is in.
+                */
+               kunit_handle_test_crash(test, suite, test_case);
+               kunit_set_failure(test);
Quoted 1 line

Like was done here.

Sorry, like what?

Quoted 8 lines

kunit/test.c void kunit_init_test(struct kunit *test, const char *name) · line 346

Quoted 6 lines
kunit/test.c +6 -0
+       }
+}
+
+/*
+ * Performs all logic to run a test case. It also catches most errors that
+ * occurs in a test case and reports them as failures.

s/occurs/occur/

Damn, I should go over all these with spell check. Will fix, thanks!

Quoted 74 lines
Quoted 2 lines
kunit/test.c +2 -0
+ */
+static void kunit_run_case_catch_errors(struct kunit_suite *suite,

[...]

Quoted 71 lines
kunit/try-catch.c new file +65 -0
new file mode 100644
index 0000000000000..de580f074387b
@@ -0,0 +1,95 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * An API to allow a function, that may fail, to be executed, and recover in a
+ * controlled manner.
+ *
+ * Copyright (C) 2019, Google LLC.
+ * Author: Brendan Higgins <[email protected]>
+ */
+
+#include <kunit/try-catch.h>
+#include <kunit/test.h>
+#include <linux/completion.h>
+#include <linux/kthread.h>
+
+void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch)
+{
+       try_catch->try_result = -EFAULT;
+       complete_and_exit(try_catch->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->try_completion, 0);
+}
+
+void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
+{
+       DECLARE_COMPLETION_ONSTACK(try_completion);
+       struct kunit *test = try_catch->test;
+       struct task_struct *task_struct;
+       int exit_code, status;
+
+       try_catch->context = context;
+       try_catch->try_completion = &try_completion;
+       try_catch->try_result = 0;
+       task_struct = kthread_run(kunit_generic_run_threadfn_adapter,
+                                 try_catch,
+                                 "kunit_try_catch_thread");
+       if (IS_ERR(task_struct)) {
+               try_catch->catch(try_catch->context);
+               return;
+       }
+
+       /*
+        * TODO([email protected]): We should probably have some type of
+        * variable 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
+        * 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
+        */
+       status = wait_for_completion_timeout(&try_completion,
Quoted 8 lines

kunit/try-catch.c line 67

Quoted 2 lines
kunit/try-catch.c new file +2 -0
+                                            300 * MSEC_PER_SEC); /* 5 min */
+       if (status < 0) {

wait_for_completion_timeout() doesn't return a negative value on timeout. It returns 0. Please rename 'status' to 'time_remaining' and test with if (!time_remaining) instead or some other suitably named variable name indicating that the return value is the time remaining before the timeout.

Crap, I knew that. Sorry, I wasn't thinking.

Quoted 3 lines

May also want to clamp this to the hung task timeout value, which is typically less than 5 minutes. Otherwise, the hung task detector may find the problem first before this timeout happens.

Makes sense. Will fix.

Quoted 3 lines
Quoted 3 lines
kunit/try-catch.c new file +3 -0
+               kunit_err(test, "try timed out\n");
+               try_catch->try_result = -ETIMEDOUT;
+       }
#33 lore raw

Re: [PATCH v12 09/18] kunit: test: add support for test abort

Quoting Brendan Higgins (2019-08-12 21:57:55)

Quoted 34 lines · Stephen Boyd
Quoted 22 lines

Quoting Brendan Higgins (2019-08-12 11:24:12)

Quoted 20 lines
include/kunit/test.h +5 -0
index 2625bcfeb19ac..93381f841e09f 100644
@@ -176,6 +178,11 @@ struct kunit {
         */
        bool success; /* Read only after test_case finishes! */
        spinlock_t lock; /* Gaurds all mutable test state. */
+       /*
+        * death_test may be both set and unset from multiple threads in a test
+        * case.
+        */
+       bool death_test; /* Protected by lock. */
        /*
         * Because resources is a list that may be updated multiple times (with
         * new resources) from any thread associated with a test case, we must
@@ -184,6 +191,13 @@ struct kunit {
        struct list_head resources; /* Protected by lock. */
 };
 
Quoted 10 lines

include/kunit/test.h struct kunit { · line 199

Quoted 6 lines
include/kunit/test.h +6 -0
+static inline void kunit_set_death_test(struct kunit *test, bool death_test)
+{
+       spin_lock(&test->lock);
+       test->death_test = death_test;
+       spin_unlock(&test->lock);
+}

These getters and setters are using spinlocks again. It doesn't make any sense. It probably needs a rework like was done for the other bool member, success.

Quoted 3 lines

No, this is intentional. death_test can transition from false to true and then back to false within the same test. Maybe that deserves a comment?

Yes. How does it transition from true to false again?

Either way, having a spinlock around a read/write API doesn't make sense because it just makes sure that two writes don't overlap, but otherwise does nothing to keep things synchronized. For example a set to true after a set to false when the two calls to set true or false aren't synchronized means they can happen in any order. So I don't see how it needs a spinlock. The lock needs to be one level higher.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
include/kunit/test.h +1 -0
+
 void kunit_init_test(struct kunit *test, const char *name);
 
 int kunit_run_tests(struct kunit_suite *suite);
include/kunit/try-catch.h new file +0 -0
new file mode 100644
index 0000000000000..8a414a9af0b64

[...]

Quoted 33 lines
Quoted 24 lines
Quoted 24 lines
include/kunit/try-catch.h new file +24 -0
+
+/*
+ * struct kunit_try_catch - provides a generic way to run code which might fail.
+ * @context: used to pass user data to the try and catch functions.
+ *
+ * kunit_try_catch provides a generic, architecture independent way to execute
+ * an arbitrary function of type kunit_try_catch_func_t which may bail out by
+ * calling kunit_try_catch_throw(). If kunit_try_catch_throw() is called, @try
+ * is stopped at the site of invocation and @catch is catch is called.
+ *
+ * struct kunit_try_catch provides a generic interface for the functionality
+ * needed to implement kunit->abort() which in turn is needed for implementing
+ * assertions. Assertions allow stating a precondition for a test simplifying
+ * how test cases are written and presented.
+ *
+ * Assertions are like expectations, except they abort (call
+ * kunit_try_catch_throw()) when the specified condition is not met. This is
+ * useful when you look at a test case as a logical statement about some piece
+ * of code, where assertions are the premises for the test case, and the
+ * conclusion is a set of predicates, rather expectations, that must all be
+ * true. If your premises are violated, it does not makes sense to continue.
+ */
+struct kunit_try_catch {
+       /* private: internal use only. */
Quoted 8 lines

include/kunit/try-catch.h line 46

Quoted 5 lines
include/kunit/try-catch.h new file +5 -0
+       struct kunit *test;
+       struct completion *try_completion;
+       int try_result;
+       kunit_try_catch_func_t try;
+       kunit_try_catch_func_t catch;

Can these other variables be documented in the kernel doc? And should context be marked as 'public'?

Quoted 6 lines

Sure, I can document them.

But I don't think context should be public; it should only be accessed by kunit_try_catch_* functions. context should only be populated by *_init, and will be passed into *try and *catch when they are called internally.

Ok. Then I guess just document them all but keep them all marked as private.

Quoted 29 lines
Quoted 13 lines
Quoted 13 lines

+ */

include/kunit/try-catch.h new file +3 -0
+void kunit_generic_try_catch_init(struct kunit_try_catch *try_catch);
+
+#endif /* _KUNIT_TRY_CATCH_H */
kunit/test.c +0 -0
index e5080a2c6b29c..995cb53fe4ee9 100644
@@ -158,6 +171,21 @@ static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
        kunit_print_string_stream(test, stream);
 }
 
Quoted 15 lines

kunit/test.c static void kunit_fail(struct kunit *test, struct kunit_assert *assert) · line 186

Quoted 13 lines
kunit/test.c +13 -0
+void __noreturn kunit_abort(struct kunit *test)
+{
+       kunit_set_death_test(test, true);
+
+       kunit_try_catch_throw(&test->try_catch);
+
+       /*
+        * Throw could not abort from test.
+        *
+        * XXX: we should never reach this line! As kunit_try_catch_throw is
+        * marked __noreturn.
+        */
+       WARN_ONCE(true, "Throw could not abort from test!\n");

Should this just be a BUG_ON? It's supposedly impossible.

Quoted 7 lines

It should be impossible; it will only reach this line if there is a bug in kunit_try_catch_throw. The reason I didn't use BUG_ON was because I previously got yelled at for having BUG_ON in this code path.

Nevertheless, I think BUG_ON is more correct, so if you will stand by it, then that's what I will do.

Yeah BUG_ON is appropriate here and self documenting so please use it.

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
kunit/test.c +18 -0
+               return;
+       }
+
+       if (kunit_get_death_test(test)) {
+               /*
+                * EXPECTED DEATH: kunit_run_case_internal encountered
+                * anticipated fatal error. Everything should be in a safe
+                * state.
+                */
+               kunit_run_case_cleanup(test, suite);
+       } else {
+               /*
+                * UNEXPECTED DEATH: kunit_run_case_internal encountered an
+                * unanticipated fatal error. We have no idea what the state of
+                * the test case is in.
+                */
+               kunit_handle_test_crash(test, suite, test_case);
+               kunit_set_failure(test);
Quoted 3 lines
Quoted 1 line

Like was done here.

Sorry, like what?

Just saying this has braces for the if-else.

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#34 lore raw

Re: [PATCH v12 09/18] kunit: test: add support for test abort

Quoted 36 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 21:57:55)

Quoted 34 lines · Stephen Boyd
Quoted 32 lines

Quoting Brendan Higgins (2019-08-12 11:24:12)

Quoted 17 lines
include/kunit/test.h +5 -0
index 2625bcfeb19ac..93381f841e09f 100644
@@ -176,6 +178,11 @@ struct kunit {
         */
        bool success; /* Read only after test_case finishes! */
        spinlock_t lock; /* Gaurds all mutable test state. */
+       /*
+        * death_test may be both set and unset from multiple threads in a test
+        * case.
+        */
+       bool death_test; /* Protected by lock. */
        /*
         * Because resources is a list that may be updated multiple times (with
         * new resources) from any thread associated with a test case, we must

include/kunit/test.h struct kunit { · line 199

Quoted 9 lines
include/kunit/test.h +6 -0
@@ -184,6 +191,13 @@ struct kunit {
        struct list_head resources; /* Protected by lock. */
 };
 
+static inline void kunit_set_death_test(struct kunit *test, bool death_test)
+{
+       spin_lock(&test->lock);
+       test->death_test = death_test;
+       spin_unlock(&test->lock);
+}

These getters and setters are using spinlocks again. It doesn't make any sense. It probably needs a rework like was done for the other bool member, success.

Quoted 5 lines
Quoted 3 lines

No, this is intentional. death_test can transition from false to true and then back to false within the same test. Maybe that deserves a comment?

Yes. How does it transition from true to false again?

The purpose is to tell try_catch that it was expected for the test to bail out. Given the default implementation there is no way for this to happen aside from abort() being called, but in some implementations it is possible to implement a fault catcher which allows a test suite to recover from an unexpected failure.

Maybe it would be best to drop this until I add one of those alternative implementations.

Quoted 6 lines

Either way, having a spinlock around a read/write API doesn't make sense because it just makes sure that two writes don't overlap, but otherwise does nothing to keep things synchronized. For example a set to true after a set to false when the two calls to set true or false aren't synchronized means they can happen in any order. So I don't see how it needs a spinlock. The lock needs to be one level higher.

There shouldn't be any cases where one thread is trying to set it while another is trying to unset it. The thing I am worried about here is making sure all the cores see the write, and making sure no reads or writes get reordered before it. So I guess I just want a fence. So I take it I should probably have is a WRITE_ONCE here and READ_ONCE in the getter?

Quoted 46 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
include/kunit/test.h +1 -0
+
 void kunit_init_test(struct kunit *test, const char *name);
 
 int kunit_run_tests(struct kunit_suite *suite);
include/kunit/try-catch.h new file +0 -0
new file mode 100644
index 0000000000000..8a414a9af0b64

[...]

Quoted 35 lines
Quoted 24 lines
Quoted 24 lines
include/kunit/try-catch.h new file +24 -0
+
+/*
+ * struct kunit_try_catch - provides a generic way to run code which might fail.
+ * @context: used to pass user data to the try and catch functions.
+ *
+ * kunit_try_catch provides a generic, architecture independent way to execute
+ * an arbitrary function of type kunit_try_catch_func_t which may bail out by
+ * calling kunit_try_catch_throw(). If kunit_try_catch_throw() is called, @try
+ * is stopped at the site of invocation and @catch is catch is called.
+ *
+ * struct kunit_try_catch provides a generic interface for the functionality
+ * needed to implement kunit->abort() which in turn is needed for implementing
+ * assertions. Assertions allow stating a precondition for a test simplifying
+ * how test cases are written and presented.
+ *
+ * Assertions are like expectations, except they abort (call
+ * kunit_try_catch_throw()) when the specified condition is not met. This is
+ * useful when you look at a test case as a logical statement about some piece
+ * of code, where assertions are the premises for the test case, and the
+ * conclusion is a set of predicates, rather expectations, that must all be
+ * true. If your premises are violated, it does not makes sense to continue.
+ */
+struct kunit_try_catch {
+       /* private: internal use only. */
Quoted 8 lines

include/kunit/try-catch.h line 46

Quoted 5 lines
include/kunit/try-catch.h new file +5 -0
+       struct kunit *test;
+       struct completion *try_completion;
+       int try_result;
+       kunit_try_catch_func_t try;
+       kunit_try_catch_func_t catch;

Can these other variables be documented in the kernel doc? And should context be marked as 'public'?

Sure, I can document them.

Quoted 7 lines
Quoted 4 lines

But I don't think context should be public; it should only be accessed by kunit_try_catch_* functions. context should only be populated by *_init, and will be passed into *try and *catch when they are called internally.

Ok. Then I guess just document them all but keep them all marked as private.

Will do.

Quoted 34 lines
Quoted 34 lines
Quoted 13 lines
Quoted 13 lines

+ */

include/kunit/try-catch.h new file +3 -0
+void kunit_generic_try_catch_init(struct kunit_try_catch *try_catch);
+
+#endif /* _KUNIT_TRY_CATCH_H */
kunit/test.c +0 -0
index e5080a2c6b29c..995cb53fe4ee9 100644
@@ -158,6 +171,21 @@ static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
        kunit_print_string_stream(test, stream);
 }
 
Quoted 15 lines

kunit/test.c static void kunit_fail(struct kunit *test, struct kunit_assert *assert) · line 186

Quoted 13 lines
kunit/test.c +13 -0
+void __noreturn kunit_abort(struct kunit *test)
+{
+       kunit_set_death_test(test, true);
+
+       kunit_try_catch_throw(&test->try_catch);
+
+       /*
+        * Throw could not abort from test.
+        *
+        * XXX: we should never reach this line! As kunit_try_catch_throw is
+        * marked __noreturn.
+        */
+       WARN_ONCE(true, "Throw could not abort from test!\n");

Should this just be a BUG_ON? It's supposedly impossible.

It should be impossible; it will only reach this line if there is a bug in kunit_try_catch_throw. The reason I didn't use BUG_ON was because I previously got yelled at for having BUG_ON in this code path.

Quoted 4 lines
Quoted 2 lines

Nevertheless, I think BUG_ON is more correct, so if you will stand by it, then that's what I will do.

Yeah BUG_ON is appropriate here and self documenting so please use it.

Cool, will do.

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
kunit/test.c +18 -0
+               return;
+       }
+
+       if (kunit_get_death_test(test)) {
+               /*
+                * EXPECTED DEATH: kunit_run_case_internal encountered
+                * anticipated fatal error. Everything should be in a safe
+                * state.
+                */
+               kunit_run_case_cleanup(test, suite);
+       } else {
+               /*
+                * UNEXPECTED DEATH: kunit_run_case_internal encountered an
+                * unanticipated fatal error. We have no idea what the state of
+                * the test case is in.
+                */
+               kunit_handle_test_crash(test, suite, test_case);
+               kunit_set_failure(test);
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Like was done here.

Sorry, like what?

Just saying this has braces for the if-else.

Ah, gotcha.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#35 lore raw

Re: [PATCH v12 09/18] kunit: test: add support for test abort

Quoting Brendan Higgins (2019-08-13 00:52:03)

Quoted 31 lines · Stephen Boyd
Quoted 24 lines

Quoting Brendan Higgins (2019-08-12 21:57:55)

Quoted 22 lines · Stephen Boyd
Quoted 20 lines

Quoting Brendan Higgins (2019-08-12 11:24:12)

Quoted 5 lines
include/kunit/test.h +0 -0
index 2625bcfeb19ac..93381f841e09f 100644

include/kunit/test.h struct kunit { · line 199

Quoted 9 lines
include/kunit/test.h +6 -0
@@ -184,6 +191,13 @@ struct kunit {
        struct list_head resources; /* Protected by lock. */
 };
 
+static inline void kunit_set_death_test(struct kunit *test, bool death_test)
+{
+       spin_lock(&test->lock);
+       test->death_test = death_test;
+       spin_unlock(&test->lock);
+}

These getters and setters are using spinlocks again. It doesn't make any sense. It probably needs a rework like was done for the other bool member, success.

Quoted 5 lines
Quoted 3 lines

No, this is intentional. death_test can transition from false to true and then back to false within the same test. Maybe that deserves a comment?

Yes. How does it transition from true to false again?

Quoted 8 lines

The purpose is to tell try_catch that it was expected for the test to bail out. Given the default implementation there is no way for this to happen aside from abort() being called, but in some implementations it is possible to implement a fault catcher which allows a test suite to recover from an unexpected failure.

Maybe it would be best to drop this until I add one of those alternative implementations.

Ok.

Quoted 8 lines
Quoted 7 lines

Either way, having a spinlock around a read/write API doesn't make sense because it just makes sure that two writes don't overlap, but otherwise does nothing to keep things synchronized. For example a set to true after a set to false when the two calls to set true or false aren't synchronized means they can happen in any order. So I don't see how it needs a spinlock. The lock needs to be one level higher.

Quoted 7 lines

There shouldn't be any cases where one thread is trying to set it while another is trying to unset it. The thing I am worried about here is making sure all the cores see the write, and making sure no reads or writes get reordered before it. So I guess I just want a fence. So I take it I should probably have is a WRITE_ONCE here and READ_ONCE in the getter?

Are the gets and sets in program order? If so, WRITE_ONCE and READ_ONCE aren't required. Otherwise, if it's possible for one thread to write it and another to read it but the threads are ordered with some other barrier like a completion or lock, then again the macros aren't needed. It would be good to read memory-barriers.txt to understand when to use the read/write macros.

#36 lore raw

PATCH v12 10/18 kunit: test: add tests for kunit test abort

In reply to Brendan Higgins

Add KUnit tests for the KUnit test abort mechanism (see preceding commit). Add tests both for general try catch mechanism as well as non-architecture specific mechanism.

---
 kunit/Makefile    |   3 +-
 kunit/test-test.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 103 insertions(+), 1 deletion(-)
 create mode 100644 kunit/test-test.c
kunit/Makefile +2 -1
index c9176c9c578c6..769d9402b5d3a 100644
@@ -3,6 +3,7 @@ obj-$(CONFIG_KUNIT) +=			test.o \
 					assert.o \
 					try-catch.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 +101 -0

101 lines not on this page · raw message

Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
#37 lore raw

Re: [PATCH v12 10/18] kunit: test: add tests for kunit test abort

Quoting Brendan Higgins (2019-08-12 11:24:13)

kunit/test-test.c line 80

Quoted 6 lines
kunit/test-test.c new file +6 -0
+
+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);

Can this fail? Should return -ENOMEM in that case?

Quoted 5 lines
kunit/test-test.c new file +5 -0
+       test->priv = ctx;
+
+       ctx->try_catch = kunit_kmalloc(test,
+                                      sizeof(*ctx->try_catch),
+                                      GFP_KERNEL);
#38 lore raw

Re: [PATCH v12 10/18] kunit: test: add tests for kunit test abort

Quoted 10 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 11:24:13)

kunit/test-test.c line 80

Quoted 6 lines
kunit/test-test.c new file +6 -0
+
+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);

Can this fail? Should return -ENOMEM in that case?

Yes, I should do that.

Quoted 5 lines
Quoted 5 lines
kunit/test-test.c new file +5 -0
+       test->priv = ctx;
+
+       ctx->try_catch = kunit_kmalloc(test,
+                                      sizeof(*ctx->try_catch),
+                                      GFP_KERNEL);
Reviewed-by
#39 lore raw

Re: [PATCH v12 10/18] kunit: test: add tests for kunit test abort

Quoting Brendan Higgins (2019-08-12 22:06:04)

Quoted 12 lines · Stephen Boyd
Quoted 10 lines

Quoting Brendan Higgins (2019-08-12 11:24:13)

kunit/test-test.c line 80

Quoted 6 lines
kunit/test-test.c new file +6 -0
+
+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);

Can this fail? Should return -ENOMEM in that case?

Quoted 1 line

Yes, I should do that.

Looks like it's asserted to not be an error. If it's pushed into the API then there's nothing to do here, and you can have my reviewed-by on this patch.

#40 lore raw

Re: [PATCH v12 10/18] kunit: test: add tests for kunit test abort

Quoted 14 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 22:06:04)

Quoted 12 lines · Stephen Boyd
Quoted 10 lines

Quoting Brendan Higgins (2019-08-12 11:24:13)

kunit/test-test.c line 80

Quoted 6 lines
kunit/test-test.c new file +6 -0
+
+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);

Can this fail? Should return -ENOMEM in that case?

Quoted 7 lines
Quoted 1 line

Yes, I should do that.

Looks like it's asserted to not be an error. If it's pushed into the API then there's nothing to do here, and you can have my reviewed-by on this patch.

Cool, thanks!

#41 lore raw

PATCH v12 11/18 kunit: test: add the concept of assertions

In reply to Brendan Higgins

Add support for assertions which are like expectations except the test terminates if the assertion is not satisfied.

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.

---
 include/kunit/test.h       | 297 ++++++++++++++++++++++++++++++++++++-
 kunit/string-stream-test.c |  12 +-
 kunit/test-test.c          |   2 +
 3 files changed, 302 insertions(+), 9 deletions(-)
include/kunit/test.h +294 -3
index 93381f841e09f..dcebaca456a1f 100644
@@ -86,9 +86,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_suite and will be run after the
- * suite's init function and followed by the suite'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_suite and will be run after the suite's init function and
+ * followed by the suite'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
@@ -1176,4 +1177,294 @@ do {									       \
 							fmt,		       \
 							##__VA_ARGS__)
 
+#define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
+		KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, 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_TRUE_ASSERTION(test, KUNIT_ASSERTION, condition)
+
+#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...)		       \
+		KUNIT_TRUE_MSG_ASSERTION(test,				       \
+					 KUNIT_ASSERTION,		       \
+					 condition,			       \
+					 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_FALSE_ASSERTION(test, KUNIT_ASSERTION, condition)
+
+#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...)		       \
+		KUNIT_FALSE_MSG_ASSERTION(test,				       \
+					  KUNIT_ASSERTION,		       \
+					  condition,			       \
+					  fmt,				       \
+					  ##__VA_ARGS__)
+
+/**
+ * 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_BINARY_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
+
+#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_EQ_MSG_ASSERTION(test,			       \
+					      KUNIT_ASSERTION,		       \
+					      left,			       \
+					      right,			       \
+					      fmt,			       \
+					      ##__VA_ARGS__)
+
+/**
+ * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a pointer.
+ * @right: an arbitrary expression that evaluates to a pointer.
+ *
+ * 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_PTR_EQ(test, left, right) \
+		KUNIT_BINARY_PTR_EQ_ASSERTION(test,			       \
+					      KUNIT_ASSERTION,		       \
+					      left,			       \
+					      right)
+
+#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test,			       \
+						  KUNIT_ASSERTION,	       \
+						  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_BINARY_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
+
+#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_NE_MSG_ASSERTION(test,			       \
+					      KUNIT_ASSERTION,		       \
+					      left,			       \
+					      right,			       \
+					      fmt,			       \
+					      ##__VA_ARGS__)
+
+/**
+ * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
+ * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a pointer.
+ * @right: an arbitrary expression that evaluates to a pointer.
+ *
+ * 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_PTR_NE(test, left, right) \
+		KUNIT_BINARY_PTR_NE_ASSERTION(test,			       \
+					      KUNIT_ASSERTION,		       \
+					      left,			       \
+					      right)
+
+#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test,			       \
+						  KUNIT_ASSERTION,	       \
+						  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_BINARY_LT_ASSERTION(test, KUNIT_ASSERTION, left, right)
+
+#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_LT_MSG_ASSERTION(test,			       \
+					      KUNIT_ASSERTION,		       \
+					      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_BINARY_LE_ASSERTION(test, KUNIT_ASSERTION, left, right)
+
+#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_LE_MSG_ASSERTION(test,			       \
+					      KUNIT_ASSERTION,		       \
+					      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_BINARY_GT_ASSERTION(test, KUNIT_ASSERTION, left, right)
+
+#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_GT_MSG_ASSERTION(test,			       \
+					      KUNIT_ASSERTION,		       \
+					      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_BINARY_GE_ASSERTION(test, KUNIT_ASSERTION, left, right)
+
+#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_GE_MSG_ASSERTION(test,			       \
+					      KUNIT_ASSERTION,		       \
+					      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)				       \
+		KUNIT_BINARY_STR_EQ_ASSERTION(test,			       \
+					      KUNIT_ASSERTION,		       \
+					      left,			       \
+					      right)
+
+#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test,			       \
+						  KUNIT_ASSERTION,	       \
+						  left,			       \
+						  right,		       \
+						  fmt,			       \
+						  ##__VA_ARGS__)
+
+/**
+ * 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)				       \
+		KUNIT_BINARY_STR_NE_ASSERTION(test,			       \
+					      KUNIT_ASSERTION,		       \
+					      left,			       \
+					      right)
+
+#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_STR_NE_MSG_ASSERTION(test,			       \
+						  KUNIT_ASSERTION,	       \
+						  left,			       \
+						  right,		       \
+						  fmt,			       \
+						  ##__VA_ARGS__)
+
+/**
+ * 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)				       \
+		KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test,		       \
+						    KUNIT_ASSERTION,	       \
+						    ptr)
+
+#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
+		KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,		       \
+							KUNIT_ASSERTION,       \
+							ptr,		       \
+							fmt,		       \
+							##__VA_ARGS__)
+
 #endif /* _KUNIT_TEST_H */
kunit/string-stream-test.c +6 -6
index 9cf08f9dadf36..fd928ab7d65a8 100644
@@ -35,7 +35,7 @@ static void string_stream_test_get_string(struct kunit *test)
 	string_stream_add(stream, " %s", "bar");
 
 	output = string_stream_get_string(stream);
-	KUNIT_EXPECT_STREQ(test, output, "Foo bar");
+	KUNIT_ASSERT_STREQ(test, output, "Foo bar");
 }
 
 static void string_stream_test_add_and_clear(struct kunit *test)
@@ -48,15 +48,15 @@ static void string_stream_test_add_and_clear(struct kunit *test)
 		string_stream_add(stream, "A");
 
 	output = string_stream_get_string(stream);
-	KUNIT_EXPECT_STREQ(test, output, "AAAAAAAAAA");
-	KUNIT_EXPECT_EQ(test, stream->length, (size_t)10);
-	KUNIT_EXPECT_FALSE(test, string_stream_is_empty(stream));
+	KUNIT_ASSERT_STREQ(test, output, "AAAAAAAAAA");
+	KUNIT_ASSERT_EQ(test, stream->length, (size_t)10);
+	KUNIT_ASSERT_FALSE(test, string_stream_is_empty(stream));
 
 	string_stream_clear(stream);
 
 	output = string_stream_get_string(stream);
-	KUNIT_EXPECT_STREQ(test, output, "");
-	KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream));
+	KUNIT_ASSERT_STREQ(test, output, "");
+	KUNIT_ASSERT_TRUE(test, string_stream_is_empty(stream));
 }
 
 static struct kunit_case string_stream_test_cases[] = {
kunit/test-test.c +2 -0
index 88f4cdf03db2a..058f3fb37458a 100644
@@ -78,11 +78,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);
 
 	return 0;
 }
Signature · 6 lines
2.23.0.rc1.153.gdeed80330f-goog

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v12 11/18
#42 lore raw

Re: [PATCH v12 11/18] kunit: test: add the concept of assertions

Quoting Brendan Higgins (2019-08-12 11:24:14)

Quoted 13 lines

Add support for assertions which are like expectations except the test terminates if the assertion is not satisfied.

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 3 lines
Quoted 7 lines

+ * 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.
+ */

include/kunit/test.h +2 -0
+#define KUNIT_ASSERT_STRNEQ(test, left, right)                                \
+               KUNIT_BINARY_STR_NE_ASSERTION(test,                            \

include/kunit/test.h do { \ · line 1445

Quoted 10 lines
include/kunit/test.h +10 -0
+                                             KUNIT_ASSERTION,                 \
+                                             left,                            \
+                                             right)
+
+#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...)                  \
+               KUNIT_BINARY_STR_NE_MSG_ASSERTION(test,                        \
+                                                 KUNIT_ASSERTION,             \
+                                                 left,                        \
+                                                 right,                       \
+                                                 fmt,                         \

Same question about tabbing too.

kunit/test-test.c static int kunit_try_catch_test_init(struct kunit *test) · line 81

Quoted 9 lines
kunit/test-test.c +1 -0
index 88f4cdf03db2a..058f3fb37458a 100644
@@ -78,11 +78,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);

Ah ok. Question still stands if kunit_kzalloc() should just have the assertion on failure.

kunit/test-test.c static int kunit_try_catch_test_init(struct kunit *test) · line 88

Quoted 7 lines
kunit/test-test.c +1 -0
        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);
 

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#43 lore raw

Re: [PATCH v12 11/18] kunit: test: add the concept of assertions

Quoted 32 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 11:24:14)

Quoted 13 lines

Add support for assertions which are like expectations except the test terminates if the assertion is not satisfied.

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 3 lines
Quoted 11 lines

+ * 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.
+ */

include/kunit/test.h +6 -0
+#define KUNIT_ASSERT_STRNEQ(test, left, right)                                \
+               KUNIT_BINARY_STR_NE_ASSERTION(test,                            \
+                                             KUNIT_ASSERTION,                 \
+                                             left,                            \
+                                             right)
+
Quoted 8 lines

include/kunit/test.h do { \ · line 1445

Quoted 6 lines
include/kunit/test.h +6 -0
+#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...)                  \
+               KUNIT_BINARY_STR_NE_MSG_ASSERTION(test,                        \
+                                                 KUNIT_ASSERTION,             \
+                                                 left,                        \
+                                                 right,                       \
+                                                 fmt,                         \

Same question about tabbing too.

Yep. WIll fix.

Quoted 7 lines
Quoted 7 lines
kunit/test-test.c +0 -0
index 88f4cdf03db2a..058f3fb37458a 100644
@@ -78,11 +78,13 @@ static int kunit_try_catch_test_init(struct kunit *test)
        struct kunit_try_catch_test_context *ctx;
 
Quoted 5 lines

kunit/test-test.c static int kunit_try_catch_test_init(struct kunit *test) · line 81

Quoted 2 lines
kunit/test-test.c +1 -0
        ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);

Ah ok. Question still stands if kunit_kzalloc() should just have the assertion on failure.

Right. In the previous patch KUNIT_ASSERT_* doesn't exist yet, so I can't use it. And rather than fall back to return -ENOMEM like I should have, I evidently forgot to do that.

Quoted 7 lines
Quoted 7 lines
kunit/test-test.c +1 -0
        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);
 
#44 lore raw

PATCH v12 12/18 kunit: test: add tests for KUnit managed resources

In reply to Brendan Higgins

From: Avinash Kondareddy <[email protected]>

Add unit tests for KUnit managed resources. KUnit managed resources (struct kunit_resource) are resources that are automatically cleaned up at the end of a KUnit test, similar to the concept of devm_* managed resources.

---
 kunit/test-test.c | 225 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 225 insertions(+)
kunit/test-test.c +225 -0
index 058f3fb37458a..725f1486376fa 100644
@@ -101,3 +101,228 @@ static struct kunit_suite kunit_try_catch_test_suite = {
 	.test_cases = kunit_try_catch_test_cases,
 };
 kunit_test_suite(kunit_try_catch_test_suite);
+
+/*
+ * 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;
+	int allocate_order[2];
+	int free_order[2];
+};
+
+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_and_get_resource(&ctx->test,
+					   fake_resource_init,
+					   fake_resource_free,
+					   GFP_KERNEL,
+					   ctx);
+
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
+	KUNIT_EXPECT_PTR_EQ(test,
+			    &ctx->is_resource_initialized,
+			    (bool *) res->allocation);
+	KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
+	KUNIT_EXPECT_PTR_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_and_get_resource(
+			&ctx->test,
+			fake_resource_init,
+			fake_resource_free,
+			GFP_KERNEL,
+			ctx);
+
+	kunit_free_resource(&ctx->test, res);
+
+	KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
+	KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
+}
+
+static void kunit_resource_test_cleanup_resources(struct kunit *test)
+{
+	int i;
+	struct kunit_test_resource_context *ctx = test->priv;
+	struct kunit_resource *resources[5];
+
+	for (i = 0; i < ARRAY_SIZE(resources); i++) {
+		resources[i] = kunit_alloc_and_get_resource(&ctx->test,
+							    fake_resource_init,
+							    fake_resource_free,
+							    GFP_KERNEL,
+							    ctx);
+	}
+
+	kunit_cleanup(&ctx->test);
+
+	KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
+}
+
+static void kunit_resource_test_mark_order(int order_array[],
+					   size_t order_size,
+					   int key)
+{
+	int i;
+
+	for (i = 0; i < order_size && order_array[i]; i++)
+		;
+
+	order_array[i] = key;
+}
+
+#define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key)		       \
+		kunit_resource_test_mark_order(ctx->order_field,	       \
+					       ARRAY_SIZE(ctx->order_field),   \
+					       key)
+
+static int fake_resource_2_init(struct kunit_resource *res, void *context)
+{
+	struct kunit_test_resource_context *ctx = context;
+
+	KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2);
+
+	res->allocation = ctx;
+
+	return 0;
+}
+
+static void fake_resource_2_free(struct kunit_resource *res)
+{
+	struct kunit_test_resource_context *ctx = res->allocation;
+
+	KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2);
+}
+
+static int fake_resource_1_init(struct kunit_resource *res, void *context)
+{
+	struct kunit_test_resource_context *ctx = context;
+
+	kunit_alloc_and_get_resource(&ctx->test,
+				     fake_resource_2_init,
+				     fake_resource_2_free,
+				     GFP_KERNEL,
+				     ctx);
+
+	KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
+
+	res->allocation = ctx;
+
+	return 0;
+}
+
+static void fake_resource_1_free(struct kunit_resource *res)
+{
+	struct kunit_test_resource_context *ctx = res->allocation;
+
+	KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1);
+}
+
+/*
+ * TODO([email protected]): replace the arrays that keep track of the
+ * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
+ * to assert allocation and freeing order when the feature becomes available.
+ */
+static void kunit_resource_test_proper_free_ordering(struct kunit *test)
+{
+	struct kunit_test_resource_context *ctx = test->priv;
+
+	/* fake_resource_1 allocates a fake_resource_2 in its init. */
+	kunit_alloc_and_get_resource(&ctx->test,
+				     fake_resource_1_init,
+				     fake_resource_1_free,
+				     GFP_KERNEL,
+				     ctx);
+
+	/*
+	 * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
+	 * before returning to fake_resource_1_init, it should be the first to
+	 * put its key in the allocate_order array.
+	 */
+	KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
+	KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
+
+	kunit_cleanup(&ctx->test);
+
+	/*
+	 * Because fake_resource_2 finishes allocation before fake_resource_1,
+	 * fake_resource_1 should be freed first since it could depend on
+	 * fake_resource_2.
+	 */
+	KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
+	KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
+}
+
+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),
+	KUNIT_CASE(kunit_resource_test_proper_free_ordering),
+	{}
+};
+
+static struct kunit_suite kunit_resource_test_suite = {
+	.name = "kunit-resource-test",
+	.init = kunit_resource_test_init,
+	.exit = kunit_resource_test_exit,
+	.test_cases = kunit_resource_test_cases,
+};
+kunit_test_suite(kunit_resource_test_suite);
Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
#45 lore raw

Re: [PATCH v12 12/18] kunit: test: add tests for KUnit managed resources

Quoting Brendan Higgins (2019-08-12 11:24:15)

kunit/test-test.c static struct kunit_suite kunit_try_catch_test_suite = { · line 296

Quoted 8 lines
kunit/test-test.c +8 -0
+
+static int kunit_resource_test_init(struct kunit *test)
+{
+       struct kunit_test_resource_context *ctx =
+                       kzalloc(sizeof(*ctx), GFP_KERNEL);
+
+       if (!ctx)
+               return -ENOMEM;

Should this use the test assertion logic to make sure that it's not failing allocations during init? BTW, maybe kunit allocation APIs should fail the test if they fail to allocate in general. Unless we're unit testing failure to allocate problems.

Quoted 6 lines
kunit/test-test.c +6 -0
+
+       test->priv = ctx;
+
+       kunit_init_test(&ctx->test, "test_test_context");
+
+       return 0;
#46 lore raw

Re: [PATCH v12 12/18] kunit: test: add tests for KUnit managed resources

Quoted 13 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 11:24:15)

kunit/test-test.c static struct kunit_suite kunit_try_catch_test_suite = { · line 296

Quoted 8 lines
kunit/test-test.c +8 -0
+
+static int kunit_resource_test_init(struct kunit *test)
+{
+       struct kunit_test_resource_context *ctx =
+                       kzalloc(sizeof(*ctx), GFP_KERNEL);
+
+       if (!ctx)
+               return -ENOMEM;

Should this use the test assertion logic to make sure that it's not failing allocations during init?

Yep. Will fix.

Quoted 3 lines

BTW, maybe kunit allocation APIs should
fail the test if they fail to allocate in general. Unless we're unit testing failure to allocate problems.

Yeah, I thought about that. I wasn't sure how people would feel about it, and I thought it would be a pain to tease out all the issues arising from aborting in different contexts when someone might not expect it.

I am thinking later we can have kunit_kmalloc_or_abort variants? And then we can punt this issue to a later time?

Quoted 6 lines
Quoted 6 lines
kunit/test-test.c +6 -0
+
+       test->priv = ctx;
+
+       kunit_init_test(&ctx->test, "test_test_context");
+
+       return 0;
#47 lore raw

Re: [PATCH v12 12/18] kunit: test: add tests for KUnit managed resources

Quoting Brendan Higgins (2019-08-13 00:57:33)

Quoted 6 lines · Stephen Boyd
Quoted 4 lines

BTW, maybe kunit allocation APIs should
fail the test if they fail to allocate in general. Unless we're unit testing failure to allocate problems.

Quoted 8 lines

Yeah, I thought about that. I wasn't sure how people would feel about it, and I thought it would be a pain to tease out all the issues arising from aborting in different contexts when someone might not expect it.

I am thinking later we can have kunit_kmalloc_or_abort variants? And then we can punt this issue to a later time?

Sure. Sounds good.

#48 lore raw

PATCH v12 13/18 kunit: tool: add Python wrappers for running KUnit tests

In reply to Brendan Higgins

From: 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.

kunit_parser.py: parses raw logs returned out by kunit_kernel and displays them in a user friendly way.

test_data/*: samples of test data for testing kunit.py, kunit_config.py, etc.

---
 tools/testing/kunit/.gitignore                |   3 +
 tools/testing/kunit/kunit.py                  | 116 +++++++
 tools/testing/kunit/kunit_config.py           |  66 ++++
 tools/testing/kunit/kunit_kernel.py           | 148 +++++++++
 tools/testing/kunit/kunit_parser.py           | 310 ++++++++++++++++++
 tools/testing/kunit/kunit_tool_test.py        | 206 ++++++++++++
 .../test_is_test_passed-all_passed.log        |  32 ++
 .../test_data/test_is_test_passed-crash.log   |  69 ++++
 .../test_data/test_is_test_passed-failure.log |  36 ++
 .../test_is_test_passed-no_tests_run.log      |  75 +++++
 .../test_output_isolated_correctly.log        | 106 ++++++
 .../test_data/test_read_from_file.kconfig     |  17 +
 12 files changed, 1184 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
 create mode 100755 tools/testing/kunit/kunit_tool_test.py
 create mode 100644 tools/testing/kunit/test_data/test_is_test_passed-all_passed.log
 create mode 100644 tools/testing/kunit/test_data/test_is_test_passed-crash.log
 create mode 100644 tools/testing/kunit/test_data/test_is_test_passed-failure.log
 create mode 100644 tools/testing/kunit/test_data/test_is_test_passed-no_tests_run.log
 create mode 100644 tools/testing/kunit/test_data/test_output_isolated_correctly.log
 create mode 100644 tools/testing/kunit/test_data/test_read_from_file.kconfig
tools/testing/kunit/.gitignore new file +3 -0
new file mode 100644
index 0000000000000..c791ff59a37a9
@@ -0,0 +1,3 @@
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
\ No newline at end of file
tools/testing/kunit/kunit.py new file +116 -0

116 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 +310 -0

310 lines not on this page · raw message

tools/testing/kunit/kunit_tool_test.py new file +206 -0

206 lines not on this page · raw message

tools/testing/kunit/test_data/test_is_test_passed-all_passed.log new file +32 -0
new file mode 100644
index 0000000000000..62ebc0288355c
@@ -0,0 +1,32 @@
+TAP version 14
+	# Subtest: sysctl_test
+	1..8
+	# sysctl_test_dointvec_null_tbl_data: sysctl_test_dointvec_null_tbl_data passed
+	ok 1 - sysctl_test_dointvec_null_tbl_data
+	# sysctl_test_dointvec_table_maxlen_unset: sysctl_test_dointvec_table_maxlen_unset passed
+	ok 2 - sysctl_test_dointvec_table_maxlen_unset
+	# sysctl_test_dointvec_table_len_is_zero: sysctl_test_dointvec_table_len_is_zero passed
+	ok 3 - sysctl_test_dointvec_table_len_is_zero
+	# sysctl_test_dointvec_table_read_but_position_set: sysctl_test_dointvec_table_read_but_position_set passed
+	ok 4 - sysctl_test_dointvec_table_read_but_position_set
+	# sysctl_test_dointvec_happy_single_positive: sysctl_test_dointvec_happy_single_positive passed
+	ok 5 - sysctl_test_dointvec_happy_single_positive
+	# sysctl_test_dointvec_happy_single_negative: sysctl_test_dointvec_happy_single_negative passed
+	ok 6 - sysctl_test_dointvec_happy_single_negative
+	# sysctl_test_dointvec_single_less_int_min: sysctl_test_dointvec_single_less_int_min passed
+	ok 7 - sysctl_test_dointvec_single_less_int_min
+	# sysctl_test_dointvec_single_greater_int_max: sysctl_test_dointvec_single_greater_int_max passed
+	ok 8 - sysctl_test_dointvec_single_greater_int_max
+kunit sysctl_test: all tests passed
+ok 1 - sysctl_test
+	# Subtest: example
+	1..2
+init_suite
+	# example_simple_test: initializing
+	# example_simple_test: example_simple_test passed
+	ok 1 - example_simple_test
+	# example_mock_test: initializing
+	# example_mock_test: example_mock_test passed
+	ok 2 - example_mock_test
+kunit example: all tests passed
+ok 2 - example
tools/testing/kunit/test_data/test_is_test_passed-crash.log new file +69 -0

69 lines not on this page · raw message

tools/testing/kunit/test_data/test_is_test_passed-failure.log new file +36 -0
new file mode 100644
index 0000000000000..2ed870b6de72f
@@ -0,0 +1,36 @@
+TAP version 14
+	# Subtest: sysctl_test
+	1..8
+	# sysctl_test_dointvec_null_tbl_data: sysctl_test_dointvec_null_tbl_data passed
+	ok 1 - sysctl_test_dointvec_null_tbl_data
+	# sysctl_test_dointvec_table_maxlen_unset: sysctl_test_dointvec_table_maxlen_unset passed
+	ok 2 - sysctl_test_dointvec_table_maxlen_unset
+	# sysctl_test_dointvec_table_len_is_zero: sysctl_test_dointvec_table_len_is_zero passed
+	ok 3 - sysctl_test_dointvec_table_len_is_zero
+	# sysctl_test_dointvec_table_read_but_position_set: sysctl_test_dointvec_table_read_but_position_set passed
+	ok 4 - sysctl_test_dointvec_table_read_but_position_set
+	# sysctl_test_dointvec_happy_single_positive: sysctl_test_dointvec_happy_single_positive passed
+	ok 5 - sysctl_test_dointvec_happy_single_positive
+	# sysctl_test_dointvec_happy_single_negative: sysctl_test_dointvec_happy_single_negative passed
+	ok 6 - sysctl_test_dointvec_happy_single_negative
+	# sysctl_test_dointvec_single_less_int_min: sysctl_test_dointvec_single_less_int_min passed
+	ok 7 - sysctl_test_dointvec_single_less_int_min
+	# sysctl_test_dointvec_single_greater_int_max: sysctl_test_dointvec_single_greater_int_max passed
+	ok 8 - sysctl_test_dointvec_single_greater_int_max
+kunit sysctl_test: all tests passed
+ok 1 - sysctl_test
+	# Subtest: example
+	1..2
+init_suite
+	# example_simple_test: initializing
+	# example_simple_test: EXPECTATION FAILED at kunit/example-test.c:30
+	Expected 1 + 1 == 3, but
+		1 + 1 == 2
+		3 == 3
+	# example_simple_test: example_simple_test failed
+	not ok 1 - example_simple_test
+	# example_mock_test: initializing
+	# example_mock_test: example_mock_test passed
+	ok 2 - example_mock_test
+kunit example: one or more tests failed
+not ok 2 - example
tools/testing/kunit/test_data/test_is_test_passed-no_tests_run.log new file +75 -0

75 lines not on this page · raw message

tools/testing/kunit/test_data/test_output_isolated_correctly.log new file +106 -0

106 lines not on this page · raw message

tools/testing/kunit/test_data/test_read_from_file.kconfig new file +17 -0
new file mode 100644
index 0000000000000..d2a4928ac773b
@@ -0,0 +1,17 @@
+#
+# Automatically generated file; DO NOT EDIT.
+# User Mode Linux/x86 4.12.0-rc3 Kernel Configuration
+#
+CONFIG_UML=y
+CONFIG_MMU=y
+
+#
+# UML-specific options
+#
+
+#
+# Host processor type and features
+#
+# CONFIG_MK8 is not set
+CONFIG_TEST=y
+CONFIG_EXAMPLE_TEST=y
Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
Reviewed-by on v12 13/18
#49 lore raw

Re: [PATCH v12 13/18] kunit: tool: add Python wrappers for running KUnit tests

Quoting Brendan Higgins (2019-08-12 11:24:16)

Quoted 20 lines

From: 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.

kunit_parser.py: parses raw logs returned out by kunit_kernel and displays them in a user friendly way.

Quoted 8 lines

test_data/*: samples of test data for testing kunit.py, kunit_config.py, etc.

---

I look forward to the single binary solution, but until then, we have this.

#50 lore raw

PATCH v12 14/18 kunit: defconfig: add defconfigs for building KUnit tests

In reply to Brendan Higgins

Add 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.

---
 arch/um/configs/kunit_defconfig              |  8 ++++++
 tools/testing/kunit/configs/all_tests.config |  8 ++++++
 tools/testing/kunit/kunit.py                 | 28 +++++++++++++++++---
 tools/testing/kunit/kunit_kernel.py          |  3 ++-
 4 files changed, 42 insertions(+), 5 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
new file mode 100644
index 0000000000000..bfe49689038f1
@@ -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
new file mode 100644
index 0000000000000..bfe49689038f1
@@ -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 +24 -4
index da11bd62a4b82..0944dea5c3211 100755
@@ -11,6 +11,7 @@ import argparse
 import sys
 import os
 import time
+import shutil
 
 from collections import namedtuple
 from enum import Enum, auto
@@ -21,7 +22,7 @@ import kunit_parser
 
 KunitResult = namedtuple('KunitResult', ['status','result'])
 
-KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs', 'build_dir'])
+KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs', 'build_dir', 'defconfig'])
 
 class KunitStatus(Enum):
 	SUCCESS = auto()
@@ -29,8 +30,16 @@ class KunitStatus(Enum):
 	BUILD_FAILURE = auto()
 	TEST_FAILURE = auto()
 
+def create_default_kunitconfig():
+	if not os.path.exists(kunit_kernel.KUNITCONFIG_PATH):
+		shutil.copyfile('arch/um/configs/kunit_defconfig',
+				kunit_kernel.KUNITCONFIG_PATH)
+
 def run_tests(linux: kunit_kernel.LinuxSourceTree,
 	      request: KunitRequest) -> KunitResult:
+	if request.defconfig:
+		create_default_kunitconfig()
+
 	config_start = time.time()
 	success = linux.build_reconfig(request.build_dir)
 	config_end = time.time()
@@ -72,7 +81,7 @@ def run_tests(linux: kunit_kernel.LinuxSourceTree,
 	else:
 		return KunitResult(KunitStatus.SUCCESS, test_result)
 
-def main(argv, linux):
+def main(argv, linux=None):
 	parser = argparse.ArgumentParser(
 			description='Helps writing and running KUnit tests.')
 	subparser = parser.add_subparsers(dest='subcommand')
@@ -99,13 +108,24 @@ def main(argv, linux):
 				'directory.',
 				type=str, default=None, metavar='build_dir')
 
+	run_parser.add_argument('--defconfig',
+				help='Uses a default kunitconfig.',
+				action='store_true')
+
 	cli_args = parser.parse_args(argv)
 
 	if cli_args.subcommand == 'run':
+		if cli_args.defconfig:
+			create_default_kunitconfig()
+
+		if not linux:
+			linux = kunit_kernel.LinuxSourceTree()
+
 		request = KunitRequest(cli_args.raw_output,
 				       cli_args.timeout,
 				       cli_args.jobs,
-				       cli_args.build_dir)
+				       cli_args.build_dir,
+				       cli_args.defconfig)
 		result = run_tests(linux, request)
 		if result.status != KunitStatus.SUCCESS:
 			sys.exit(1)
@@ -113,4 +133,4 @@ def main(argv, linux):
 		parser.print_help()
 
 if __name__ == '__main__':
-	main(sys.argv[1:], kunit_kernel.LinuxSourceTree())
+	main(sys.argv[1:])
tools/testing/kunit/kunit_kernel.py +2 -1
index 07c0abf2f47df..bf38768353313 100644
@@ -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.23.0.rc1.153.gdeed80330f-goog
#51 lore raw

Re: [PATCH v12 14/18] kunit: defconfig: add defconfigs for building KUnit tests

Quoting Brendan Higgins (2019-08-12 11:24:17)

Quoted 20 lines
arch/um/configs/kunit_defconfig new file +8 -0
new file mode 100644
index 0000000000000..bfe49689038f1
@@ -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 +0 -0
new file mode 100644
index 0000000000000..bfe49689038f1

tools/testing/kunit/configs/all_tests.config line 5

Quoted 5 lines
tools/testing/kunit/configs/all_tests.config new file +5 -0
@@ -0,0 +1,8 @@
+CONFIG_OF=y
+CONFIG_OF_UNITTEST=y
+CONFIG_OF_OVERLAY=y
+CONFIG_I2C=y
+CONFIG_I2C_MUX=y

Are these above config options necessary? I don't think they're part of the patch series anymore so it looks odd to enable the OF unittests and i2c configs.

Quoted 3 lines
tools/testing/kunit/configs/all_tests.config new file +3 -0
+CONFIG_KUNIT=y
+CONFIG_KUNIT_TEST=y
+CONFIG_KUNIT_EXAMPLE_TEST=y
#52 lore raw

Re: [PATCH v12 14/18] kunit: defconfig: add defconfigs for building KUnit tests

Quoted 23 lines · Stephen Boyd

Quoting Brendan Higgins (2019-08-12 11:24:17)

Quoted 21 lines
arch/um/configs/kunit_defconfig new file +8 -0
new file mode 100644
index 0000000000000..bfe49689038f1
@@ -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 +1 -0
new file mode 100644
index 0000000000000..bfe49689038f1
@@ -0,0 +1,8 @@
+CONFIG_OF=y
Quoted 8 lines

tools/testing/kunit/configs/all_tests.config line 5

Quoted 4 lines
tools/testing/kunit/configs/all_tests.config new file +4 -0
+CONFIG_OF_UNITTEST=y
+CONFIG_OF_OVERLAY=y
+CONFIG_I2C=y
+CONFIG_I2C_MUX=y

Are these above config options necessary? I don't think they're part of the patch series anymore so it looks odd to enable the OF unittests and i2c configs.

Oh whoops, I forgot that we dropped the OF_UNITTEST from this patchset. Will fix.

Quoted 3 lines

tools/testing/kunit/configs/all_tests.config line 8

Quoted 3 lines
tools/testing/kunit/configs/all_tests.config new file +3 -0
+CONFIG_KUNIT=y
+CONFIG_KUNIT_TEST=y
+CONFIG_KUNIT_EXAMPLE_TEST=y

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#53 lore raw

PATCH v12 15/18 Documentation: kunit: add documentation for KUnit

In reply to Brendan Higgins

Add documentation for KUnit, the Linux kernel unit testing framework.
- Add intro and usage guide for KUnit
- Add API reference

---
 Documentation/dev-tools/index.rst           |   1 +
 Documentation/dev-tools/kunit/api/index.rst |  16 +
 Documentation/dev-tools/kunit/api/test.rst  |  14 +
 Documentation/dev-tools/kunit/faq.rst       |  62 +++
 Documentation/dev-tools/kunit/index.rst     |  79 +++
 Documentation/dev-tools/kunit/start.rst     | 180 ++++++
 Documentation/dev-tools/kunit/usage.rst     | 576 ++++++++++++++++++++
 7 files changed, 928 insertions(+)
 create mode 100644 Documentation/dev-tools/kunit/api/index.rst
 create mode 100644 Documentation/dev-tools/kunit/api/test.rst
 create mode 100644 Documentation/dev-tools/kunit/faq.rst
 create mode 100644 Documentation/dev-tools/kunit/index.rst
 create mode 100644 Documentation/dev-tools/kunit/start.rst
 create mode 100644 Documentation/dev-tools/kunit/usage.rst
Documentation/dev-tools/index.rst +1 -0
index b0522a4dd1073..09dee10d25928 100644
@@ -24,6 +24,7 @@ whole; patches welcome!
    gdb-kernel-debugging
    kgdb
    kselftest
+   kunit/index
 
 
 .. only::  subproject and html
Documentation/dev-tools/kunit/api/index.rst new file +16 -0
new file mode 100644
index 0000000000000..9b9bffe5d41a0
@@ -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 the
+following sections:
+
+================================= ==============================================
+:doc:`test`                       documents all of the standard testing API
+                                  excluding mocking or mocking related features.
+================================= ==============================================
Documentation/dev-tools/kunit/api/test.rst new file +14 -0
new file mode 100644
index 0000000000000..d0ce19b1e1185
@@ -0,0 +1,14 @@
+.. 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/dev-tools/kunit/faq.rst new file +62 -0

62 lines not on this page · raw message

Documentation/dev-tools/kunit/index.rst new file +79 -0

79 lines not on this page · raw message

Documentation/dev-tools/kunit/start.rst new file +180 -0

180 lines not on this page · raw message

Documentation/dev-tools/kunit/usage.rst new file +576 -0

576 lines not on this page · raw message

Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog
Reviewed-by on v12 15/18
#54 lore raw

Re: [PATCH v12 15/18] Documentation: kunit: add documentation for KUnit

Quoting Brendan Higgins (2019-08-12 11:24:18)

Quoted 10 lines

Add documentation for KUnit, the Linux kernel unit testing framework.
- Add intro and usage guide for KUnit
- Add API reference

---

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#55 lore raw

PATCH v12 16/18 MAINTAINERS: add entry for KUnit the unit testing framework

In reply to Brendan Higgins

Add myself as maintainer of KUnit, the Linux kernel's unit testing framework.

---
 MAINTAINERS | 11 +++++++++++
 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
index a2c343ee3b2ca..f0bd77e8a8a2f 100644
@@ -8799,6 +8799,17 @@ S:	Maintained
 F:	tools/testing/selftests/
 F:	Documentation/dev-tools/kselftest*
 
+KERNEL UNIT TESTING FRAMEWORK (KUnit)
+M:	Brendan Higgins <[email protected]>
+L:	[email protected]
+L:	kunit-dev-/[email protected]
+W:	https://google.github.io/kunit-docs/third_party/kernel/docs/
+S:	Maintained
+F:	Documentation/dev-tools/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.23.0.rc1.153.gdeed80330f-goog
#57 lore raw

PATCH v12 17/18 kernel/sysctl-test: Add null pointer test for sysctl.c:proc_dointvec()

In reply to Brendan Higgins

From: Iurii Zaikin <[email protected]>

KUnit tests for initialized data behavior of proc_dointvec that is explicitly checked in the code. Includes basic parsing tests including int min/max overflow.

---
 kernel/Makefile      |   2 +
 kernel/sysctl-test.c | 392 +++++++++++++++++++++++++++++++++++++++++++
 lib/Kconfig.debug    |  11 ++
 3 files changed, 405 insertions(+)
 create mode 100644 kernel/sysctl-test.c
kernel/Makefile +2 -0
index ef0d95a190b41..63e9ea6122c2c 100644
@@ -113,6 +113,8 @@ obj-$(CONFIG_TORTURE_TEST) += torture.o
 obj-$(CONFIG_HAS_IOMEM) += iomem.o
 obj-$(CONFIG_RSEQ) += rseq.o
 
+obj-$(CONFIG_SYSCTL_KUNIT_TEST) += sysctl-test.o
+
 obj-$(CONFIG_GCC_PLUGIN_STACKLEAK) += stackleak.o
 KASAN_SANITIZE_stackleak.o := n
 KCOV_INSTRUMENT_stackleak.o := n
kernel/sysctl-test.c new file +392 -0

392 lines not on this page · raw message

lib/Kconfig.debug +11 -0
index 5960e2980a8a0..a425741907b0d 100644
@@ -1965,6 +1965,17 @@ config TEST_SYSCTL
 
 	  If unsure, say N.
 
+config SYSCTL_KUNIT_TEST
+	bool "KUnit test for sysctl"
+	depends on KUNIT
+	help
+	  This builds the proc sysctl unit test, which runs on boot.
+	  Tests the API contract and implementation correctness of sysctl.
+	  For more information on KUnit and unit tests in general please refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
+
 config TEST_UDELAY
 	tristate "udelay test driver"
 	help
Signature · 6 lines
2.23.0.rc1.153.gdeed80330f-goog

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v12 17/18
#58 lore raw

Re: [PATCH v12 17/18] kernel/sysctl-test: Add null pointer test for sysctl.c:proc_dointvec()

Quoting Brendan Higgins (2019-08-12 11:24:20)

Quoted 6 lines

From: Iurii Zaikin <[email protected]>

KUnit tests for initialized data behavior of proc_dointvec that is explicitly checked in the code. Includes basic parsing tests including int min/max overflow.

Quoted 6 lines

---

#59 lore raw

PATCH v12 18/18 MAINTAINERS: add proc sysctl KUnit test to PROC SYSCTL section

In reply to Brendan Higgins

Add entry for the new proc sysctl KUnit test to the PROC SYSCTL section, and add Iurii as a maintainer.

---
 MAINTAINERS | 2 ++
 1 file changed, 2 insertions(+)
MAINTAINERS +2 -0
index f0bd77e8a8a2f..0cac78807137b 100644
@@ -12965,12 +12965,14 @@ F:	Documentation/filesystems/proc.txt
 PROC SYSCTL
 M:	Luis Chamberlain <[email protected]>
 M:	Kees Cook <[email protected]>
+M:	Iurii Zaikin <[email protected]>
 L:	[email protected]
 L:	[email protected]
 S:	Maintained
 F:	fs/proc/proc_sysctl.c
 F:	include/linux/sysctl.h
 F:	kernel/sysctl.c
+F:	kernel/sysctl-test.c
 F:	tools/testing/selftests/sysctl/
 
 PS3 NETWORK SUPPORT
Signature · 1 line
2.23.0.rc1.153.gdeed80330f-goog