linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stephen Boyd <sboyd@kernel.org>
To: Brendan Higgins <brendanhiggins@google.com>
Cc: Frank Rowand <frowand.list@gmail.com>,
	Greg KH <gregkh@linuxfoundation.org>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Kees Cook <keescook@google.com>,
	Kieran Bingham <kieran.bingham@ideasonboard.com>,
	Luis Chamberlain <mcgrof@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Rob Herring <robh@kernel.org>, shuah <shuah@kernel.org>,
	Theodore Ts'o <tytso@mit.edu>,
	Masahiro Yamada <yamada.masahiro@socionext.com>,
	devicetree <devicetree@vger.kernel.org>,
	dri-devel <dri-devel@lists.freedesktop.org>,
	kunit-dev@googlegroups.com,
	"open list:DOCUMENTATION" <linux-doc@vger.kernel.org>,
	linux-fsdevel@vger.kernel.org,
	linux-kbuild <linux-kbuild@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	"open list:KERNEL SELFTEST FRAMEWORK" 
	<linux-kselftest@vger.kernel.org>,
	linux-nvdimm <linux-nvdimm@lists.01.org>,
	linux-um@lists.infradead.org,
	Sasha Levin <Alexander.Levin@microsoft.com>,
	"Bird, Timothy" <Tim.Bird@sony.com>,
	Amir Goldstein <amir73il@gmail.com>,
	Dan Carpenter <dan.carpenter@oracle.com>,
	Daniel Vetter <daniel@ffwll.ch>, Jeff Dike <jdike@addtoit.com>,
	Joel Stanley <joel@jms.id.au>,
	Julia Lawall <julia.lawall@lip6.fr>,
	Kevin Hilman <khilman@baylibre.com>,
	Knut Omang <knut.omang@oracle.com>,
	Logan Gunthorpe <logang@deltatee.com>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Petr Mladek <pmladek@suse.com>,
	Randy Dunlap <rdunlap@infradead.org>,
	Richard Weinberger <richard@nod.at>,
	David Rientjes <rientjes@google.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	wfg@linux.intel.com
Subject: Re: [PATCH v9 04/18] kunit: test: add kunit_stream a std::stream like logger
Date: Thu, 18 Jul 2019 10:50:23 -0700	[thread overview]
Message-ID: <20190718175024.C3EC421019@mail.kernel.org> (raw)
In-Reply-To: <CAFd5g453vXeSUCZenCk_CzJ-8a1ym9RaPo0NVF=FujF9ac-5Ag@mail.gmail.com>

Quoting Brendan Higgins (2019-07-16 11:52:01)
> On Tue, Jul 16, 2019 at 10:50 AM Stephen Boyd <sboyd@kernel.org> wrote:
> >
> 
> > The only hypothetical case where this can't be done is a complicated
> > assertion or expectation that does more than one check and can't be
> > written as a function that dumps out what went wrong. Is this a real
> > problem? Maybe such an assertion should just open code that logic so we
> > don't have to build up a string for all the other simple cases.
> 
> I have some expectations in follow up patchsets for which I created a
> set of composable matchers for matching structures and function calls
> that by their nature cannot be written as a single function. The
> matcher thing is a bit speculative, I know, but for any kind of
> function call matching, you need to store a record of functions you
> are expecting to have called and then each one needs to have a set of
> expectations defined by the user; I don't think there is a way to do
> that that doesn't involve having multiple separate functions each
> having some information useful to constructing the message.
> 
> I know the code in question isn't in this patchset; the function
> matching code was in one of the earlier versions of the RFC, but I
> dropped it to make this patchset smaller and more manageable. So I get
> it if you would like me to drop it and add it back in when I try to
> get the function and structure matching stuff in, but I would really
> prefer to keep it as is if you don't care too much.

Do you have a link to those earlier patches?

> 
> > It seems far simpler to get rid of the string stream API and just have a
> > struct for this.
> >
> >         struct kunit_fail_msg {
> >                 const char *line;
> >                 const char *file;
> >                 const char *func;
> >                 const char *msg;
> >         };
> >
> > Then you can have the assertion macros create this on the stack (with
> > another macro?).
> >
> >         #define DEFINE_KUNIT_FAIL_MSG(name, _msg) \
> >                 struct kunit_fail_msg name = { \
> >                         .line =  __LINE__, \
> >                         .file = __FILE__, \
> >                         .func = __func__, \
> >                         .msg = _msg, \
> >                 }
> >
> > I don't want to derail this whole series on this topic, but it seems
> > like a bunch of code is there to construct this same set of information
> > over and over again into a buffer a little bit at a time and then throw
> > it away when nothing fails just because we may want to support the case
> > where we have some unstructured data to inform the user about.
> 
> Yeah, that's fair. I think there are a number of improvements to be
> made with how the expectations are defined other than that, but I was
> hoping I could do that after this patchset is merged. I just figured
> with the kinds of things I would like to do, it would lead to a whole
> new round of discussion.
> 
> In either case, I think I would still like to use the `struct
> kunit_stream` as part of the interface to share the failure message
> with the test case runner code in test.c, at least eventually, so that
> I only have to have one way to receive data from expectations, but I
> think I can do that and still do what you suggest by just constructing
> the kunit_stream at the end of expectations where it is feasible.
> 
> All in all I agree with what you are saying, but I would rather do it
> as a follow up possibly once we have some more code on the table. I
> could just see this opening up a whole new can of worms where we
> debate about exactly how expectations and assertions work for another
> several months, only to rip it all out shortly there after. I know
> that's how these things go, but that's my preference.
> 
> I can do what you suggest if you feel strongly about it, but I would
> prefer to hold off until later. It's your call.
> 

The crux of my complaint is that the string stream API is too loosely
defined to be usable. It allows tests to build up a string of
unstructured information, but with certain calling constraints so we
have to tread carefully. If there was more structure to the data that's
being recorded then the test case runner could operate on the data
without having to do string/stream operations, allocations, etc. This
would make the assertion logic much more concrete and specific to kunit,
instead of this small kunit wrapper that's been placed on top of string
stream.

TL;DR: If we can get rid of the string stream API I'd view that as an
improvement because building arbitrary strings in the kernel is complex,
error prone and has calling context concerns.

Is the intention that other code besides unit tests will use this string
stream API to build up strings? Any targets in mind? This would be a
good way to get the API merged upstream given that its 2019 and we
haven't had such an API in the kernel so far.

An "object oriented" (strong quotes!) approach where kunit_fail_msg is
the innermost struct in some assertion specific structure might work
nicely and allow the test runner to call a generic 'format' function to
print out the message based on the type of assertion/expectation it is.
It probably would mean less code size too because the strings that are
common will be in the common printing function instead of created twice,
in the macros/code and then copied to the heap for the string stream.

	struct kunit_assert {
		const char *line;
		const char *file;
		const char *func;
		void (*format)(struct kunit_assert *assert);
	};

	struct kunit_comparison_assert {
		enum operator operator;
		const char *left;
		const char *right;
		struct kunit_assert assert;
	};

	struct kunit_bool_assert {
		const char *truth;
		const char *statement;
		struct kunit_assert assert;
	};

	void kunit_format_comparison(struct kunit_assert *assert)
	{
		struct kunit_comparison_assert *comp = container_of(assert, ...)

		kunit_printk(...)
	}

Maybe other people have opinions here on if you should do it now or
later. Future coding is not a great argument because it's hard to
predict the future. On the other hand, this patchset is in good shape to
merge and I'd like to use it to write unit tests for code I maintain so
I don't want to see this stall out. Sorry if I'm opening the can of
worms you're talking about.


  reply	other threads:[~2019-07-18 17:50 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-12  8:17 [PATCH v9 00/18] kunit: introduce KUnit, the Linux kernel unit testing framework Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 01/18] kunit: test: add KUnit test runner core Brendan Higgins
2019-07-15 20:10   ` Stephen Boyd
2019-07-15 21:25     ` Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 02/18] kunit: test: add test resource management API Brendan Higgins
2019-07-15 20:24   ` Stephen Boyd
2019-07-15 20:30     ` Brendan Higgins
2019-07-15 20:51       ` Stephen Boyd
2019-07-12  8:17 ` [PATCH v9 03/18] kunit: test: add string_stream a std::stream like string builder Brendan Higgins
2019-07-15 20:43   ` Stephen Boyd
2019-07-15 21:11     ` Brendan Higgins
2019-07-15 22:04       ` Stephen Boyd
2019-07-15 22:11         ` Brendan Higgins
2019-07-15 22:43           ` Brendan Higgins
2019-07-16 15:33             ` Stephen Boyd
2019-07-16 18:55               ` Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 04/18] kunit: test: add kunit_stream a std::stream like logger Brendan Higgins
2019-07-15 22:15   ` Stephen Boyd
2019-07-16  7:57     ` Brendan Higgins
2019-07-16  8:37       ` Brendan Higgins
2019-07-16 15:30         ` Stephen Boyd
2019-07-16 17:51           ` Brendan Higgins
2019-07-16 17:50         ` Stephen Boyd
2019-07-16 18:52           ` Brendan Higgins
2019-07-18 17:50             ` Stephen Boyd [this message]
2019-07-18 19:22               ` Brendan Higgins
2019-07-19  0:08                 ` Brendan Higgins
2019-07-22 18:10                   ` Brendan Higgins
2019-07-22 20:03                   ` Stephen Boyd
2019-07-22 22:30                     ` Brendan Higgins
2019-07-22 23:54                       ` Stephen Boyd
2019-07-23  0:32                         ` Brendan Higgins
2019-07-24  7:31                         ` Petr Mladek
2019-07-25 20:21                           ` Brendan Higgins
2019-07-26  8:31                             ` Petr Mladek
2019-08-01 18:55                               ` Brendan Higgins
2019-08-01 18:59                                 ` Brendan Higgins
2019-08-01 21:14                                   ` Stephen Boyd
2019-08-01 21:43                                     ` Brendan Higgins
2019-08-12 20:41                                       ` Brendan Higgins
2019-08-02  7:37                                 ` John Ogness
2019-08-12 21:12                                   ` Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 05/18] kunit: test: add the concept of expectations Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 06/18] kbuild: enable building KUnit Brendan Higgins
2019-07-15 20:49   ` Stephen Boyd
2019-07-12  8:17 ` [PATCH v9 07/18] kunit: test: add initial tests Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 08/18] objtool: add kunit_try_catch_throw to the noreturn list Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 09/18] kunit: test: add support for test abort Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 10/18] kunit: test: add tests for kunit " Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 11/18] kunit: test: add the concept of assertions Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 12/18] kunit: test: add tests for KUnit managed resources Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 13/18] kunit: tool: add Python wrappers for running KUnit tests Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 14/18] kunit: defconfig: add defconfigs for building " Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 15/18] Documentation: kunit: add documentation for KUnit Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 16/18] MAINTAINERS: add entry for KUnit the unit testing framework Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 17/18] kernel/sysctl-test: Add null pointer test for sysctl.c:proc_dointvec() Brendan Higgins
2019-07-12  8:17 ` [PATCH v9 18/18] MAINTAINERS: add proc sysctl KUnit test to PROC SYSCTL section 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=20190718175024.C3EC421019@mail.kernel.org \
    --to=sboyd@kernel.org \
    --cc=Alexander.Levin@microsoft.com \
    --cc=Tim.Bird@sony.com \
    --cc=amir73il@gmail.com \
    --cc=brendanhiggins@google.com \
    --cc=dan.carpenter@oracle.com \
    --cc=daniel@ffwll.ch \
    --cc=devicetree@vger.kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=frowand.list@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jdike@addtoit.com \
    --cc=joel@jms.id.au \
    --cc=jpoimboe@redhat.com \
    --cc=julia.lawall@lip6.fr \
    --cc=keescook@google.com \
    --cc=khilman@baylibre.com \
    --cc=kieran.bingham@ideasonboard.com \
    --cc=knut.omang@oracle.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-nvdimm@lists.01.org \
    --cc=linux-um@lists.infradead.org \
    --cc=logang@deltatee.com \
    --cc=mcgrof@kernel.org \
    --cc=mpe@ellerman.id.au \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=rdunlap@infradead.org \
    --cc=richard@nod.at \
    --cc=rientjes@google.com \
    --cc=robh@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=shuah@kernel.org \
    --cc=tytso@mit.edu \
    --cc=wfg@linux.intel.com \
    --cc=yamada.masahiro@socionext.com \
    /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).