linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: shuah <shuah@kernel.org>
To: Brendan Higgins <brendanhiggins@google.com>,
	frowand.list@gmail.com, gregkh@linuxfoundation.org,
	jpoimboe@redhat.com, keescook@google.com,
	kieran.bingham@ideasonboard.com, mcgrof@kernel.org,
	peterz@infradead.org, robh@kernel.org, sboyd@kernel.org,
	tytso@mit.edu, yamada.masahiro@socionext.com
Cc: devicetree@vger.kernel.org, dri-devel@lists.freedesktop.org,
	kunit-dev@googlegroups.com, linux-doc@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, linux-kbuild@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-nvdimm@lists.01.org, linux-um@lists.infradead.org,
	Alexander.Levin@microsoft.com, Tim.Bird@sony.com,
	amir73il@gmail.com, dan.carpenter@oracle.com, daniel@ffwll.ch,
	jdike@addtoit.com, joel@jms.id.au, julia.lawall@lip6.fr,
	khilman@baylibre.com, knut.omang@oracle.com, logang@deltatee.com,
	mpe@ellerman.id.au, pmladek@suse.com, rdunlap@infradead.org,
	richard@nod.at, rientjes@google.com, rostedt@goodmis.org,
	wfg@linux.intel.com, shuah <shuah@kernel.org>
Subject: Re: [PATCH v14 09/18] kunit: test: add support for test abort
Date: Fri, 23 Aug 2019 09:36:34 -0600	[thread overview]
Message-ID: <ae6722ce-80ac-5840-5c4b-6f6726e4239d@kernel.org> (raw)
In-Reply-To: <20190820232046.50175-10-brendanhiggins@google.com>

Hi Brendan,

On 8/20/19 5:20 PM, Brendan Higgins wrote:
> 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.
> 
> Signed-off-by: Brendan Higgins <brendanhiggins@google.com>
> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Reviewed-by: Logan Gunthorpe <logang@deltatee.com>
> Reviewed-by: Stephen Boyd <sboyd@kernel.org>
> ---
>   include/kunit/test.h      |   2 +
>   include/kunit/try-catch.h |  75 +++++++++++++++++++++
>   kunit/Makefile            |   3 +-
>   kunit/test.c              | 137 +++++++++++++++++++++++++++++++++-----
>   kunit/try-catch.c         | 118 ++++++++++++++++++++++++++++++++
>   5 files changed, 319 insertions(+), 16 deletions(-)
>   create mode 100644 include/kunit/try-catch.h
>   create mode 100644 kunit/try-catch.c
> 
> diff --git a/include/kunit/test.h b/include/kunit/test.h
> index 6917b186b737a..390ce02f717b6 100644
> --- a/include/kunit/test.h
> +++ b/include/kunit/test.h
> @@ -10,6 +10,7 @@
>   #define _KUNIT_TEST_H
>   
>   #include <kunit/assert.h>
> +#include <kunit/try-catch.h>
>   #include <linux/kernel.h>
>   #include <linux/slab.h>
>   #include <linux/types.h>
> @@ -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
> diff --git a/include/kunit/try-catch.h b/include/kunit/try-catch.h
> new file mode 100644
> index 0000000000000..404f336cbdc85
> --- /dev/null
> +++ b/include/kunit/try-catch.h
> @@ -0,0 +1,75 @@
> +/* 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 <brendanhiggins@google.com>
> + */
> +
> +#ifndef _KUNIT_TRY_CATCH_H
> +#define _KUNIT_TRY_CATCH_H
> +
> +#include <linux/types.h>
> +
> +typedef void (*kunit_try_catch_func_t)(void *);
> +
> +struct completion;
> +struct kunit;
> +
> +/**
> + * struct kunit_try_catch - provides a generic way to run code which might fail.
> + * @test: The test case that is currently being executed.
> + * @try_completion: Completion that the control thread waits on while test runs.
> + * @try_result: Contains any errno obtained while running test case.
> + * @try: The function, the test case, to attempt to run.
> + * @catch: The function called if @try bails out.
> + * @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 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. */
> +	struct kunit *test;
> +	struct completion *try_completion;
> +	int try_result;
> +	kunit_try_catch_func_t try;
> +	kunit_try_catch_func_t catch;
> +	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)
> +{
> +	return try_catch->try_result;
> +}
> +
> +/*
> + * Exposed for testing only.
> + */
> +void kunit_generic_try_catch_init(struct kunit_try_catch *try_catch);
> +
> +#endif /* _KUNIT_TRY_CATCH_H */
> diff --git a/kunit/Makefile b/kunit/Makefile
> index 4e46450bcb3a8..c9176c9c578c6 100644
> --- a/kunit/Makefile
> +++ b/kunit/Makefile
> @@ -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
>   
> diff --git a/kunit/test.c b/kunit/test.c
> index 3cbceb34b3b36..ded9895143209 100644
> --- a/kunit/test.c
> +++ b/kunit/test.c
> @@ -7,7 +7,9 @@
>    */
>   
>   #include <kunit/test.h>
> +#include <kunit/try-catch.h>
>   #include <linux/kernel.h>
> +#include <linux/sched/debug.h>
>   
>   static void kunit_set_failure(struct kunit *test)
>   {
> @@ -162,6 +164,19 @@ static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
>   	WARN_ON(string_stream_destroy(stream));
>   }
>   
> +static void __noreturn kunit_abort(struct kunit *test)
> +{
> +	kunit_try_catch_throw(&test->try_catch); /* Does not return. */
> +
> +	/*
> +	 * Throw could not abort from test.
> +	 *
> +	 * XXX: we should never reach this line! As kunit_try_catch_throw is
> +	 * marked __noreturn.
> +	 */
> +	BUG();


I recall discussion on this. What's the point in keeping thie
BUG() around when it doesn't even reach? It can even be a
WARN_ON() in that case right?

thanks,
-- Shuah

  reply	other threads:[~2019-08-23 15:36 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-20 23:20 [PATCH v14 00/18] kunit: introduce KUnit, the Linux kernel unit testing framework Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 01/18] kunit: test: add KUnit test runner core Brendan Higgins
2019-08-23 15:33   ` shuah
2019-08-23 16:48     ` Brendan Higgins
2019-08-23 17:05       ` shuah
2019-08-23 17:27         ` Brendan Higgins
2019-08-23 17:34           ` shuah
2019-08-23 17:54             ` Brendan Higgins
2019-08-23 18:32               ` shuah
2019-08-23 18:56                 ` Brendan Higgins
2019-08-23 19:04                   ` shuah
2019-08-23 19:20                     ` Brendan Higgins
2019-08-23 19:43                       ` shuah
2019-08-20 23:20 ` [PATCH v14 02/18] kunit: test: add test resource management API Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 03/18] kunit: test: add string_stream a std::stream like string builder Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 04/18] kunit: test: add assertion printing library Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 05/18] kunit: test: add the concept of expectations Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 06/18] kbuild: enable building KUnit Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 07/18] kunit: test: add initial tests Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 08/18] objtool: add kunit_try_catch_throw to the noreturn list Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 09/18] kunit: test: add support for test abort Brendan Higgins
2019-08-23 15:36   ` shuah [this message]
2019-08-23 16:56     ` Brendan Higgins
2019-08-23 17:07       ` shuah
2019-08-23 17:29         ` Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 10/18] kunit: test: add tests for kunit " Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 11/18] kunit: test: add the concept of assertions Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 12/18] kunit: test: add tests for KUnit managed resources Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 13/18] kunit: tool: add Python wrappers for running KUnit tests Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 14/18] kunit: defconfig: add defconfigs for building " Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 15/18] Documentation: kunit: add documentation for KUnit Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 16/18] MAINTAINERS: add entry for KUnit the unit testing framework Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 17/18] kernel/sysctl-test: Add null pointer test for sysctl.c:proc_dointvec() Brendan Higgins
2019-08-20 23:20 ` [PATCH v14 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=ae6722ce-80ac-5840-5c4b-6f6726e4239d@kernel.org \
    --to=shuah@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=sboyd@kernel.org \
    --cc=tytso@mit.edu \
    --cc=wfg@linux.intel.com \
    --cc=yamada.masahiro@socionext.com \
    --subject='Re: [PATCH v14 09/18] kunit: test: add support for test abort' \
    /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

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