linux-um.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Brendan Higgins <brendanhiggins@google.com>
To: gregkh@linuxfoundation.org, keescook@google.com,
	mcgrof@kernel.org, shuah@kernel.org
Cc: brakmo@fb.com, richard@nod.at, mpe@ellerman.id.au,
	Tim.Bird@sony.com, linux-um@lists.infradead.org,
	linux-kernel@vger.kernel.org, rostedt@goodmis.org,
	julia.lawall@lip6.fr, joel@jms.id.au,
	linux-kselftest@vger.kernel.org, khilman@baylibre.com,
	joe@perches.com, jdike@addtoit.com,
	Brendan Higgins <brendanhiggins@google.com>,
	kunit-dev@googlegroups.com
Subject: [RFC v1 04/31] kunit: test: added test_stream a std::stream like logger
Date: Tue, 16 Oct 2018 16:50:53 -0700	[thread overview]
Message-ID: <20181016235120.138227-5-brendanhiggins@google.com> (raw)
In-Reply-To: <20181016235120.138227-1-brendanhiggins@google.com>

Adds a C++ style log library for for logging test results.

Signed-off-by: Brendan Higgins <brendanhiggins@google.com>
---
 include/kunit/test-stream.h |  49 ++++++++++++
 include/kunit/test.h        |   2 +
 kunit/Makefile              |   2 +-
 kunit/test-stream.c         | 153 ++++++++++++++++++++++++++++++++++++
 kunit/test.c                |   8 ++
 5 files changed, 213 insertions(+), 1 deletion(-)
 create mode 100644 include/kunit/test-stream.h
 create mode 100644 kunit/test-stream.c

diff --git a/include/kunit/test-stream.h b/include/kunit/test-stream.h
new file mode 100644
index 0000000000000..c5dfd95ef21bb
--- /dev/null
+++ b/include/kunit/test-stream.h
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * C++ stream style string formatter and printer used in KUnit for outputting
+ * KUnit messages.
+ *
+ * Copyright (C) 2018, Google LLC.
+ * Author: Brendan Higgins <brendanhiggins@google.com>
+ */
+
+#ifndef _KUNIT_TEST_STREAM_H
+#define _KUNIT_TEST_STREAM_H
+
+#include <linux/types.h>
+#include <kunit/string-stream.h>
+
+struct test;
+
+/**
+ * struct test_stream - a std::stream style string builder.
+ * @set_level: sets the level that this string should be printed at.
+ * @add: adds the formatted input to the internal buffer.
+ * @commit: prints out the internal buffer to the user.
+ * @clear: clears the internal buffer.
+ *
+ * A std::stream style string builder. Allows messages to be built up and
+ * printed all at once.
+ */
+struct test_stream {
+	void (*set_level)(struct test_stream *this, const char *level);
+	void (*add)(struct test_stream *this, const char *fmt, ...);
+	void (*append)(struct test_stream *this, struct test_stream *other);
+	void (*commit)(struct test_stream *this);
+	void (*clear)(struct test_stream *this);
+	/* private: internal use only. */
+	struct test *test;
+	spinlock_t lock; /* Guards level. */
+	const char *level;
+	struct string_stream *internal_stream;
+};
+
+/**
+ * test_new_stream() - constructs a new &struct test_stream.
+ * @test: The test context object.
+ *
+ * Constructs a new test managed &struct test_stream.
+ */
+struct test_stream *test_new_stream(struct test *test);
+
+#endif /* _KUNIT_TEST_STREAM_H */
diff --git a/include/kunit/test.h b/include/kunit/test.h
index 1c116a20063da..68320fa2452de 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -11,6 +11,7 @@
 
 #include <linux/types.h>
 #include <linux/slab.h>
+#include <kunit/test-stream.h>
 
 struct test_resource;
 
@@ -171,6 +172,7 @@ struct test {
 	void (*vprintk)(const struct test *test,
 			const char *level,
 			struct va_format *vaf);
+	void (*fail)(struct test *test, struct test_stream *stream);
 };
 
 int test_init_test(struct test *test, const char *name);
diff --git a/kunit/Makefile b/kunit/Makefile
index 7fc613a9b383b..5b4562ea7f322 100644
--- a/kunit/Makefile
+++ b/kunit/Makefile
@@ -1 +1 @@
-obj-$(CONFIG_KUNIT)		+= test.o string-stream.o
+obj-$(CONFIG_KUNIT)		+= test.o string-stream.o test-stream.o
diff --git a/kunit/test-stream.c b/kunit/test-stream.c
new file mode 100644
index 0000000000000..392966864a708
--- /dev/null
+++ b/kunit/test-stream.c
@@ -0,0 +1,153 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * C++ stream style string formatter and printer used in KUnit for outputting
+ * KUnit messages.
+ *
+ * Copyright (C) 2018, Google LLC.
+ * Author: Brendan Higgins <brendanhiggins@google.com>
+ */
+
+#include <kunit/test.h>
+#include <kunit/test-stream.h>
+#include <kunit/string-stream.h>
+
+static const char *test_stream_get_level(struct test_stream *this)
+{
+	unsigned long flags;
+	const char *level;
+
+	spin_lock_irqsave(&this->lock, flags);
+	level = this->level;
+	spin_unlock_irqrestore(&this->lock, flags);
+
+	return level;
+}
+
+static void test_stream_set_level(struct test_stream *this, const char *level)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&this->lock, flags);
+	this->level = level;
+	spin_unlock_irqrestore(&this->lock, flags);
+}
+
+static void test_stream_add(struct test_stream *this, const char *fmt, ...)
+{
+	va_list args;
+	struct string_stream *stream = this->internal_stream;
+
+	va_start(args, fmt);
+	if (stream->vadd(stream, fmt, args) < 0)
+		test_err(this->test, "Failed to allocate fragment: %s", fmt);
+
+	va_end(args);
+}
+
+static void test_stream_append(struct test_stream *this,
+			       struct test_stream *other)
+{
+	struct string_stream *other_stream = other->internal_stream;
+	const char *other_content;
+
+	other_content = other_stream->get_string(other_stream);
+
+	if (!other_content) {
+		test_err(this->test,
+			 "Failed to get string from second argument for appending.");
+		return;
+	}
+
+	this->add(this, other_content);
+}
+
+static void test_stream_clear(struct test_stream *this)
+{
+	this->internal_stream->clear(this->internal_stream);
+}
+
+static void test_stream_commit(struct test_stream *this)
+{
+	struct string_stream *stream = this->internal_stream;
+	struct string_stream_fragment *fragment;
+	const char *level;
+	char *buf;
+
+	level = test_stream_get_level(this);
+	if (!level) {
+		test_err(this->test,
+			 "Stream was committed without a specified log level.");
+		level = KERN_ERR;
+		this->set_level(this, level);
+	}
+
+	buf = stream->get_string(stream);
+	if (!buf) {
+		test_err(this->test,
+			 "Could not allocate buffer, dumping stream:");
+		list_for_each_entry(fragment, &stream->fragments, node) {
+			test_err(this->test, fragment->fragment);
+		}
+		goto cleanup;
+	}
+
+	test_printk(level, this->test, buf);
+	kfree(buf);
+
+cleanup:
+	this->clear(this);
+}
+
+static int test_stream_init(struct test_resource *res, void *context)
+{
+	struct test *test = context;
+	struct test_stream *stream;
+
+	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
+	if (!stream)
+		return -ENOMEM;
+	res->allocation = stream;
+	stream->test = test;
+	spin_lock_init(&stream->lock);
+	stream->internal_stream = new_string_stream();
+
+	if (!stream->internal_stream)
+		return -ENOMEM;
+
+	stream->set_level = test_stream_set_level;
+	stream->add = test_stream_add;
+	stream->append = test_stream_append;
+	stream->commit = test_stream_commit;
+	stream->clear = test_stream_clear;
+
+	return 0;
+}
+
+static void test_stream_free(struct test_resource *res)
+{
+	struct test_stream *stream = res->allocation;
+
+	if (!stream->internal_stream->is_empty(stream->internal_stream)) {
+		test_err(stream->test,
+			 "End of test case reached with uncommitted stream entries.");
+		stream->commit(stream);
+	}
+
+	destroy_string_stream(stream->internal_stream);
+	kfree(stream);
+}
+
+struct test_stream *test_new_stream(struct test *test)
+{
+	struct test_resource *res;
+
+	res = test_alloc_resource(test,
+				  test_stream_init,
+				  test_stream_free,
+				  test);
+
+	if (res)
+		return res->allocation;
+	else
+		return NULL;
+}
diff --git a/kunit/test.c b/kunit/test.c
index fd0a51245215e..f798183533c8d 100644
--- a/kunit/test.c
+++ b/kunit/test.c
@@ -63,12 +63,20 @@ static void test_vprintk(const struct test *test,
 			 "kunit %s: %pV", test->name, vaf);
 }
 
+static void test_fail(struct test *test, struct test_stream *stream)
+{
+	test_set_success(test, false);
+	stream->set_level(stream, KERN_ERR);
+	stream->commit(stream);
+}
+
 int test_init_test(struct test *test, const char *name)
 {
 	spin_lock_init(&test->lock);
 	INIT_LIST_HEAD(&test->resources);
 	test->name = name;
 	test->vprintk = test_vprintk;
+	test->fail = test_fail;
 
 	return 0;
 }
-- 
2.19.1.331.ge82ca0e54c-goog


_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um


  parent reply	other threads:[~2018-10-16 23:54 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-16 23:50 [RFC v1 00/31] kunit: Introducing KUnit, the Linux kernel unit testing framework Brendan Higgins
2018-10-16 23:50 ` [RFC v1 01/31] kunit: test: added string_stream a std::stream like string builder Brendan Higgins
2018-10-16 23:50 ` [RFC v1 02/31] kunit: test: adds KUnit test runner core Brendan Higgins
2018-10-16 23:50 ` [RFC v1 03/31] kunit: test: added test resource management API Brendan Higgins
2018-10-16 23:50 ` Brendan Higgins [this message]
2018-10-16 23:50 ` [RFC v1 05/31] kunit: test: added the concept of expectations Brendan Higgins
2018-10-16 23:50 ` [RFC v1 06/31] arch: um: enabled running kunit from User Mode Linux Brendan Higgins
2018-10-17 15:29   ` Kieran Bingham
2018-10-17 17:43     ` Brendan Higgins
2018-10-17 17:52     ` Tim.Bird
2018-10-17 21:09       ` Brendan Higgins
2018-10-17 21:18         ` Tim.Bird
2018-10-17 22:45           ` Brendan Higgins
2018-10-16 23:50 ` [RFC v1 07/31] kunit: test: added initial tests Brendan Higgins
2018-10-16 23:50 ` [RFC v1 08/31] arch: um: added shim to trap to allow installing a fault catcher for tests Brendan Higgins
2018-10-16 23:50 ` [RFC v1 09/31] kunit: test: added the concept of assertions Brendan Higgins
2018-10-16 23:50 ` [RFC v1 10/31] kunit: test: added concept of initcalls Brendan Higgins
2018-10-16 23:51 ` [RFC v1 11/31] kunit: test: added concept of post conditions Brendan Higgins
2018-10-16 23:51 ` [RFC v1 12/31] checkpatch: added support for struct MOCK(foo) syntax Brendan Higgins
2018-10-16 23:59   ` Joe Perches
2018-10-17  0:03     ` Brendan Higgins
2018-10-16 23:51 ` [RFC v1 13/31] kunit: mock: added parameter list minipulation macros Brendan Higgins
2018-10-16 23:51 ` [RFC v1 14/31] kunit: mock: added internal mock infrastructure Brendan Higgins
2018-10-16 23:51 ` [RFC v1 15/31] kunit: mock: added basic matchers and actions Brendan Higgins
2018-10-16 23:51 ` [RFC v1 16/31] kunit: mock: added class mocking support Brendan Higgins
2018-10-16 23:51 ` [RFC v1 17/31] kunit: mock: added struct param matcher Brendan Higgins
2018-10-16 23:51 ` [RFC v1 18/31] kunit: mock: added parameter formatters Brendan Higgins
2018-10-16 23:51 ` [RFC v1 19/31] kunit: mock: implemented nice, strict and naggy mock distinctions Brendan Higgins
2018-10-16 23:51 ` [RFC v1 20/31] kunit: mock: add ability to mock functions with void context Brendan Higgins
2018-10-16 23:51 ` [RFC v1 21/31] kunit: mock: added support for arbitrary function mocking Brendan Higgins
2018-10-16 23:51 ` [RFC v1 22/31] kunit: mock: add the concept of spyable functions Brendan Higgins
2018-10-17 22:46   ` Rob Herring
2018-10-18  1:32     ` Brendan Higgins
2018-10-16 23:51 ` [RFC v1 23/31] kunit: mock: add parameter capturers Brendan Higgins
2018-10-16 23:51 ` [RFC v1 24/31] kunit: improved sigsegv stack trace printing Brendan Higgins
2018-10-16 23:51 ` [RFC v1 25/31] kunit: added concept of platform mocking Brendan Higgins
2018-10-16 23:51 ` [RFC v1 26/31] arch: um: added stubs for mock iomem for KUnit Brendan Higgins
2018-10-17 22:28   ` Rob Herring
2018-10-18  1:14     ` Brendan Higgins
2018-10-16 23:51 ` [RFC v1 27/31] Documentation: kunit: adds complete documentation " Brendan Higgins
2018-10-16 23:51 ` [RFC v1 28/31] kunit: added Python libraries for handing KUnit config and kernel Brendan Higgins
2018-10-16 23:51 ` [RFC v1 29/31] kunit: added KUnit wrapper script and simple output parser Brendan Higgins
2018-10-16 23:51 ` [RFC v1 30/31] kunit.py: improved output from python wrapper Brendan Higgins
2018-10-16 23:51 ` [RFC v1 31/31] MAINTAINERS: add entry for KUnit the unit testing framework Brendan Higgins
2018-10-17  9:08 ` [RFC v1 00/31] kunit: Introducing KUnit, the Linux kernel " Daniel Vetter
2018-10-17 17:49 ` Tim.Bird
2018-10-17 22:22   ` Brendan Higgins
2018-10-17 20:43 ` Rob Herring
2018-10-17 23:12 ` Randy Dunlap
2018-10-18  2:05   ` Brendan Higgins
2018-10-18  3:55 ` Dan Williams
2018-10-19  6:27   ` Brendan Higgins

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181016235120.138227-5-brendanhiggins@google.com \
    --to=brendanhiggins@google.com \
    --cc=Tim.Bird@sony.com \
    --cc=brakmo@fb.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jdike@addtoit.com \
    --cc=joe@perches.com \
    --cc=joel@jms.id.au \
    --cc=julia.lawall@lip6.fr \
    --cc=keescook@google.com \
    --cc=khilman@baylibre.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-um@lists.infradead.org \
    --cc=mcgrof@kernel.org \
    --cc=mpe@ellerman.id.au \
    --cc=richard@nod.at \
    --cc=rostedt@goodmis.org \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).