From patchwork Fri Nov 15 10:16:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 11245961 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 681381393 for ; Fri, 15 Nov 2019 10:18:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3FF292077B for ; Fri, 15 Nov 2019 10:18:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="JCacvAHo" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727364AbfKOKR6 (ORCPT ); Fri, 15 Nov 2019 05:17:58 -0500 Received: from userp2120.oracle.com ([156.151.31.85]:54784 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727202AbfKOKR5 (ORCPT ); Fri, 15 Nov 2019 05:17:57 -0500 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xAFAEP70021065; Fri, 15 Nov 2019 10:16:35 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2019-08-05; bh=da7iXdjbFIbuGZbasA5yd1nTh/1IQS1oVdExPz6PWJs=; b=JCacvAHonTpbbcZe/ABZkI5jbfAti28DtxC00XA9S78xKbuXokx8WDyZKAttGVll9cCl 5u7C7DPandnmTOlkABKNKNGcgnPI8cgcJN+XZC01ly2pvmkQCKEEvdkLKkicqqeYzGq6 wYapmLtn7kKNZ42QdrqCDv1xyuTx5prjSBA/kKXhogY39IG7s8dOdIIa3uXGgTuRcqJp r2TJ+V+k5ncO6EVv72wyOmn5q+JxYrq8T9mQ9/LTn9o5oFqAfObUzWk4mhm2V3x6bA3A cIanDRI56pY5fPYxnfLs8KnWrSer+HMTiWdSzhwaCyvmXvZOaPQCgmjM8dkkrC/OLYHW 9w== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2120.oracle.com with ESMTP id 2w9gxpje1u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2019 10:16:34 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xAFADEKU169388; Fri, 15 Nov 2019 10:16:34 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserp3020.oracle.com with ESMTP id 2w9h0k4d40-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2019 10:16:33 +0000 Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id xAFAGThO001589; Fri, 15 Nov 2019 10:16:29 GMT Received: from dhcp-10-175-208-51.vpn.oracle.com (/10.175.208.51) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 15 Nov 2019 02:16:28 -0800 From: Alan Maguire To: brendanhiggins@google.com, skhan@linuxfoundation.org, linux-kselftest@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, keescook@chromium.org, yzaikin@google.com, akpm@linux-foundation.org, yamada.masahiro@socionext.com, catalin.marinas@arm.com, joe.lawrence@redhat.com, penguin-kernel@i-love.sakura.ne.jp, schowdary@nvidia.com, urezki@gmail.com, andriy.shevchenko@linux.intel.com, corbet@lwn.net, tytso@mit.edu, adilger.kernel@dilger.ca, mcgrof@kernel.org, changbin.du@intel.com, linux-ext4@vger.kernel.org, linux-doc@vger.kernel.org, sboyd@kernel.org, Alan Maguire , Knut Omang Subject: [PATCH v4 linux-kselftest-test 1/6] kunit: move string-stream.h to lib/kunit Date: Fri, 15 Nov 2019 10:16:07 +0000 Message-Id: <1573812972-10529-2-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1573812972-10529-1-git-send-email-alan.maguire@oracle.com> References: <1573812972-10529-1-git-send-email-alan.maguire@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9441 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=3 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1911150096 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9441 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1911150096 Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org string-stream interfaces are not intended for external use; move them from include/kunit to lib/kunit accordingly. Signed-off-by: Alan Maguire Signed-off-by: Knut Omang Reviewed-by: Brendan Higgins Tested-by: Brendan Higgins --- include/kunit/assert.h | 3 ++- include/kunit/string-stream.h | 51 ------------------------------------------ lib/kunit/assert.c | 2 ++ lib/kunit/string-stream-test.c | 3 ++- lib/kunit/string-stream.c | 3 ++- lib/kunit/string-stream.h | 51 ++++++++++++++++++++++++++++++++++++++++++ lib/kunit/test.c | 2 ++ 7 files changed, 61 insertions(+), 54 deletions(-) delete mode 100644 include/kunit/string-stream.h create mode 100644 lib/kunit/string-stream.h diff --git a/include/kunit/assert.h b/include/kunit/assert.h index db6a0fc..ad889b5 100644 --- a/include/kunit/assert.h +++ b/include/kunit/assert.h @@ -9,10 +9,11 @@ #ifndef _KUNIT_ASSERT_H #define _KUNIT_ASSERT_H -#include #include +#include struct kunit; +struct string_stream; /** * enum kunit_assert_type - Type of expectation/assertion. diff --git a/include/kunit/string-stream.h b/include/kunit/string-stream.h deleted file mode 100644 index fe98a00..0000000 --- a/include/kunit/string-stream.h +++ /dev/null @@ -1,51 +0,0 @@ -/* 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 - */ - -#ifndef _KUNIT_STRING_STREAM_H -#define _KUNIT_STRING_STREAM_H - -#include -#include -#include - -struct string_stream_fragment { - struct kunit *test; - 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 __printf(2, 3) 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); - -bool string_stream_is_empty(struct string_stream *stream); - -int string_stream_destroy(struct string_stream *stream); - -#endif /* _KUNIT_STRING_STREAM_H */ diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c index 86013d4..9aca71c 100644 --- a/lib/kunit/assert.c +++ b/lib/kunit/assert.c @@ -7,6 +7,8 @@ */ #include +#include "string-stream.h" + void kunit_base_assert_format(const struct kunit_assert *assert, struct string_stream *stream) { diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 76cc05e..6c70dc8 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -6,10 +6,11 @@ * Author: Brendan Higgins */ -#include #include #include +#include "string-stream.h" + static void string_stream_test_empty_on_creation(struct kunit *test) { struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL); diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index e6d17aa..3503920 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -6,11 +6,12 @@ * Author: Brendan Higgins */ -#include #include #include #include +#include "string-stream.h" + struct string_stream_fragment_alloc_context { struct kunit *test; int len; diff --git a/lib/kunit/string-stream.h b/lib/kunit/string-stream.h new file mode 100644 index 0000000..fe98a00 --- /dev/null +++ b/lib/kunit/string-stream.h @@ -0,0 +1,51 @@ +/* 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 + */ + +#ifndef _KUNIT_STRING_STREAM_H +#define _KUNIT_STRING_STREAM_H + +#include +#include +#include + +struct string_stream_fragment { + struct kunit *test; + 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 __printf(2, 3) 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); + +bool string_stream_is_empty(struct string_stream *stream); + +int string_stream_destroy(struct string_stream *stream); + +#endif /* _KUNIT_STRING_STREAM_H */ diff --git a/lib/kunit/test.c b/lib/kunit/test.c index c83c0fa..36ebf47 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -11,6 +11,8 @@ #include #include +#include "string-stream.h" + static void kunit_set_failure(struct kunit *test) { WRITE_ONCE(test->success, false); From patchwork Fri Nov 15 10:16:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 11245969 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 244871393 for ; Fri, 15 Nov 2019 10:18:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EF2ED2077B for ; Fri, 15 Nov 2019 10:18:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="P1ueIpeh" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727347AbfKOKR4 (ORCPT ); Fri, 15 Nov 2019 05:17:56 -0500 Received: from userp2120.oracle.com ([156.151.31.85]:54770 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727137AbfKOKR4 (ORCPT ); Fri, 15 Nov 2019 05:17:56 -0500 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xAFAERfJ021074; Fri, 15 Nov 2019 10:16:36 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2019-08-05; bh=GEUb/C//sxYHWwJHoIqtobWLRbr5DWDL/Y95mcw7Fwo=; b=P1ueIpeh15zw3ZM4/jxR26XteBfIbiBKxcjSHoal6HvAxkxasRvVATDNV+VpyLTnL9ec w0rNIxEzblpngOovKSv1rBjhT9LUPbg6MBsGfY3LB8y82bEHTgm2VJH1eXEZm1MWBD6d 5QYMgKHAgVHfj75nvK/FGTw9pgh0nTcMwOw4Q5rIpqIeFl4k2V12I0whp6YSAmtep3a8 Wmu8skTTvdAMiw61YzF/4LY3XtznpcUwIpcc6F/lIIncJMfcPCMWF6pgyPCLM6lGmYyJ pBgbeVrglALi18yEcx5HqRu0zWlmetu5YSxamnzkeJUI9D1aO+if3ae04pW0GaFkudAz DQ== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2120.oracle.com with ESMTP id 2w9gxpje1w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2019 10:16:35 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xAFADF2H169520; Fri, 15 Nov 2019 10:16:35 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserp3020.oracle.com with ESMTP id 2w9h0k4d8h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2019 10:16:35 +0000 Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id xAFAGWmT016310; Fri, 15 Nov 2019 10:16:32 GMT Received: from dhcp-10-175-208-51.vpn.oracle.com (/10.175.208.51) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 15 Nov 2019 02:16:32 -0800 From: Alan Maguire To: brendanhiggins@google.com, skhan@linuxfoundation.org, linux-kselftest@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, keescook@chromium.org, yzaikin@google.com, akpm@linux-foundation.org, yamada.masahiro@socionext.com, catalin.marinas@arm.com, joe.lawrence@redhat.com, penguin-kernel@i-love.sakura.ne.jp, schowdary@nvidia.com, urezki@gmail.com, andriy.shevchenko@linux.intel.com, corbet@lwn.net, tytso@mit.edu, adilger.kernel@dilger.ca, mcgrof@kernel.org, changbin.du@intel.com, linux-ext4@vger.kernel.org, linux-doc@vger.kernel.org, sboyd@kernel.org, Alan Maguire Subject: [PATCH v4 linux-kselftest-test 2/6] kunit: hide unexported try-catch interface in try-catch-impl.h Date: Fri, 15 Nov 2019 10:16:08 +0000 Message-Id: <1573812972-10529-3-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1573812972-10529-1-git-send-email-alan.maguire@oracle.com> References: <1573812972-10529-1-git-send-email-alan.maguire@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9441 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=3 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1911150096 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9441 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1911150096 Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Define function as static inline in try-catch-impl.h to allow it to be used in kunit itself and tests. Also remove unused kunit_generic_try_catch Signed-off-by: Alan Maguire Reviewed-by: Brendan Higgins Tested-by: Brendan Higgins --- include/kunit/try-catch.h | 10 ---------- lib/kunit/test-test.c | 2 ++ lib/kunit/test.c | 2 +- lib/kunit/try-catch-impl.h | 28 ++++++++++++++++++++++++++++ lib/kunit/try-catch.c | 13 ++----------- 5 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 lib/kunit/try-catch-impl.h diff --git a/include/kunit/try-catch.h b/include/kunit/try-catch.h index 404f336..c507dd4 100644 --- a/include/kunit/try-catch.h +++ b/include/kunit/try-catch.h @@ -53,11 +53,6 @@ struct kunit_try_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); @@ -67,9 +62,4 @@ 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/lib/kunit/test-test.c b/lib/kunit/test-test.c index 5ebe059..5a6cc04 100644 --- a/lib/kunit/test-test.c +++ b/lib/kunit/test-test.c @@ -7,6 +7,8 @@ */ #include +#include "try-catch-impl.h" + struct kunit_try_catch_test_context { struct kunit_try_catch *try_catch; bool function_called; diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 36ebf47..58a6227 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -7,11 +7,11 @@ */ #include -#include #include #include #include "string-stream.h" +#include "try-catch-impl.h" static void kunit_set_failure(struct kunit *test) { diff --git a/lib/kunit/try-catch-impl.h b/lib/kunit/try-catch-impl.h new file mode 100644 index 0000000..e308d5c --- /dev/null +++ b/lib/kunit/try-catch-impl.h @@ -0,0 +1,28 @@ +/* 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 + */ + +#ifndef _KUNIT_TRY_CATCH_IMPL_H +#define _KUNIT_TRY_CATCH_IMPL_H + +#include +#include + +struct kunit; + +static inline 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) +{ + try_catch->test = test; + try_catch->try = try; + try_catch->catch = catch; +} + +#endif /* _KUNIT_TRY_CATCH_IMPL_H */ diff --git a/lib/kunit/try-catch.c b/lib/kunit/try-catch.c index 55686839..4a66d16 100644 --- a/lib/kunit/try-catch.c +++ b/lib/kunit/try-catch.c @@ -8,12 +8,13 @@ */ #include -#include #include #include #include #include +#include "try-catch-impl.h" + void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch) { try_catch->try_result = -EFAULT; @@ -106,13 +107,3 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) try_catch->catch(try_catch->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) -{ - try_catch->test = test; - try_catch->try = try; - try_catch->catch = catch; -} From patchwork Fri Nov 15 10:16:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 11245959 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D9E8317E0 for ; Fri, 15 Nov 2019 10:17:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 951DB2075E for ; Fri, 15 Nov 2019 10:17:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="P7215GLz" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727450AbfKOKRG (ORCPT ); Fri, 15 Nov 2019 05:17:06 -0500 Received: from aserp2120.oracle.com ([141.146.126.78]:40746 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727036AbfKOKRG (ORCPT ); Fri, 15 Nov 2019 05:17:06 -0500 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xAFAESnF039115; Fri, 15 Nov 2019 10:16:40 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2019-08-05; bh=p1o1TXYgkhuSmy+/5e1DV+WNci6/qvJJ5VYZZq8fLaE=; b=P7215GLznALutqWm+ndtHPGDPTFEJWSLFdKCwmgOCU9CRbe5JD5LesQ/LmrIV18uHvxT DHWvTpiF1ztW/drIfiuthhfpKq/BWEdSlioBkho8KEnuJivTm39+SaEafFm0f9EalOfM 5PyhqkLJncjUZfvSXXRemQB5dU7bn+mHMoAC7LP3S5Emvc+CexaNWlSTEEDV4h3jJaeZ zmaaBlLgd4tOy13ix4F1AEH+wltoYTdU5WekjadCwQaFwiRf3FGHduF6b++nxFsvx3zc ibCMCEaIGx9Xp85wNLQZf/HXLqkv/tD6PdqMhC4ErTmifP2SuMpojRU6YpxT/uyD065a lg== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by aserp2120.oracle.com with ESMTP id 2w9gxpjd2t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2019 10:16:39 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xAFADMnu109733; Fri, 15 Nov 2019 10:16:39 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserp3030.oracle.com with ESMTP id 2w9h14fgh1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2019 10:16:39 +0000 Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id xAFAGaWW016332; Fri, 15 Nov 2019 10:16:37 GMT Received: from dhcp-10-175-208-51.vpn.oracle.com (/10.175.208.51) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 15 Nov 2019 02:16:36 -0800 From: Alan Maguire To: brendanhiggins@google.com, skhan@linuxfoundation.org, linux-kselftest@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, keescook@chromium.org, yzaikin@google.com, akpm@linux-foundation.org, yamada.masahiro@socionext.com, catalin.marinas@arm.com, joe.lawrence@redhat.com, penguin-kernel@i-love.sakura.ne.jp, schowdary@nvidia.com, urezki@gmail.com, andriy.shevchenko@linux.intel.com, corbet@lwn.net, tytso@mit.edu, adilger.kernel@dilger.ca, mcgrof@kernel.org, changbin.du@intel.com, linux-ext4@vger.kernel.org, linux-doc@vger.kernel.org, sboyd@kernel.org, Alan Maguire , Knut Omang Subject: [PATCH v4 linux-kselftest-test 3/6] kunit: allow kunit tests to be loaded as a module Date: Fri, 15 Nov 2019 10:16:09 +0000 Message-Id: <1573812972-10529-4-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1573812972-10529-1-git-send-email-alan.maguire@oracle.com> References: <1573812972-10529-1-git-send-email-alan.maguire@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9441 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=11 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1911150096 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9441 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=11 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1911150096 Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org As tests are added to kunit, it will become less feasible to execute all built tests together. By supporting modular tests we provide a simple way to do selective execution on a running system; specifying CONFIG_KUNIT=y CONFIG_KUNIT_EXAMPLE_TEST=m ...means we can simply "insmod example-test.ko" to run the tests. To achieve this we need to do the following: o export the required symbols in kunit o string-stream tests utilize non-exported symbols so for now we skip building them when CONFIG_KUNIT_TEST=m. o support a new way of declaring test suites. Because a module cannot do multiple late_initcall()s, we provide a kunit_test_suites() macro to declare multiple suites within the same module at once. o some test module names would have been too general ("test-test" and "example-test" for kunit tests, "inode-test" for ext4 tests); rename these as appropriate ("kunit-test", "kunit-example-test" and "ext4-inode-test" respectively). Signed-off-by: Alan Maguire Signed-off-by: Knut Omang --- fs/ext4/Kconfig | 2 +- fs/ext4/Makefile | 5 + fs/ext4/inode-test.c | 4 +- include/kunit/test.h | 35 +++-- kernel/sysctl-test.c | 4 +- lib/Kconfig.debug | 4 +- lib/kunit/Kconfig | 4 +- lib/kunit/Makefile | 10 +- lib/kunit/assert.c | 8 + lib/kunit/example-test.c | 88 ----------- lib/kunit/kunit-example-test.c | 90 +++++++++++ lib/kunit/kunit-test.c | 334 +++++++++++++++++++++++++++++++++++++++++ lib/kunit/string-stream-test.c | 2 +- lib/kunit/test-test.c | 333 ---------------------------------------- lib/kunit/test.c | 8 + lib/kunit/try-catch.c | 2 + lib/list-test.c | 4 +- 17 files changed, 494 insertions(+), 443 deletions(-) delete mode 100644 lib/kunit/example-test.c create mode 100644 lib/kunit/kunit-example-test.c create mode 100644 lib/kunit/kunit-test.c delete mode 100644 lib/kunit/test-test.c diff --git a/fs/ext4/Kconfig b/fs/ext4/Kconfig index ef42ab0..435510f 100644 --- a/fs/ext4/Kconfig +++ b/fs/ext4/Kconfig @@ -108,7 +108,7 @@ config EXT4_DEBUG echo 1 > /sys/module/ext4/parameters/mballoc_debug config EXT4_KUNIT_TESTS - bool "KUnit tests for ext4" + tristate "KUnit tests for ext4" select EXT4_FS depends on KUNIT help diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile index 840b91d..1e72ef6 100644 --- a/fs/ext4/Makefile +++ b/fs/ext4/Makefile @@ -13,5 +13,10 @@ ext4-y := balloc.o bitmap.o block_validity.o dir.o ext4_jbd2.o extents.o \ ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o +ifeq ($(CONFIG_EXT4_KUNIT_TESTS),y) ext4-$(CONFIG_EXT4_KUNIT_TESTS) += inode-test.o +else +obj-$(CONFIG_EXT4_KUNIT_TESTS) += ext4-inode-test.o +ext4-inode-test-objs += inode-test.o +endif ext4-$(CONFIG_FS_VERITY) += verity.o diff --git a/fs/ext4/inode-test.c b/fs/ext4/inode-test.c index 92a9da1..5a59978 100644 --- a/fs/ext4/inode-test.c +++ b/fs/ext4/inode-test.c @@ -269,4 +269,6 @@ static void inode_test_xtimestamp_decoding(struct kunit *test) .test_cases = ext4_inode_test_cases, }; -kunit_test_suite(ext4_inode_test_suite); +kunit_test_suites(&ext4_inode_test_suite); + +MODULE_LICENSE("GPL"); diff --git a/include/kunit/test.h b/include/kunit/test.h index dba4830..4e21a36 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -197,31 +198,45 @@ struct kunit { int kunit_run_tests(struct kunit_suite *suite); /** - * kunit_test_suite() - used to register a &struct kunit_suite with KUnit. + * kunit_test_suites() - used to register one or more &struct kunit_suite + * with KUnit. * - * @suite: a statically allocated &struct kunit_suite. + * @suites: a statically allocated list of &struct kunit_suite. * - * Registers @suite with the test framework. See &struct kunit_suite for + * Registers @suites with the test framework. See &struct kunit_suite for * more information. * - * NOTE: Currently KUnit tests are all run as late_initcalls; this means + * When builtin, 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. * + * An alternative is to build the tests as a module. Because modules + * do not support multiple late_initcall()s, we need to initialize an + * array of suites for a module. + * * TODO(brendanhiggins@google.com): 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. */ -#define kunit_test_suite(suite) \ - static int kunit_suite_init##suite(void) \ - { \ - return kunit_run_tests(&suite); \ - } \ - late_initcall(kunit_suite_init##suite) +#define kunit_test_suites(...) \ + static struct kunit_suite *suites[] = { __VA_ARGS__, NULL}; \ + static int kunit_test_suites_init(void) \ + { \ + unsigned int i; \ + for (i = 0; suites[i] != NULL; i++) \ + kunit_run_tests(suites[i]); \ + return 0; \ + } \ + late_initcall(kunit_test_suites_init); \ + static void __exit kunit_test_suites_exit(void) \ + { \ + return; \ + } \ + module_exit(kunit_test_suites_exit) /* * Like kunit_alloc_resource() below, but returns the struct kunit_resource diff --git a/kernel/sysctl-test.c b/kernel/sysctl-test.c index 2a63241..98d3e4e 100644 --- a/kernel/sysctl-test.c +++ b/kernel/sysctl-test.c @@ -389,4 +389,6 @@ static void sysctl_test_api_dointvec_write_single_greater_int_max( .test_cases = sysctl_test_cases, }; -kunit_test_suite(sysctl_test_suite); +kunit_test_suites(&sysctl_test_suite); + +MODULE_LICENSE("GPL"); diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 6c1be61..4b25bef 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1951,7 +1951,7 @@ config TEST_SYSCTL If unsure, say N. config SYSCTL_KUNIT_TEST - bool "KUnit test for sysctl" + tristate "KUnit test for sysctl" depends on KUNIT help This builds the proc sysctl unit test, which runs on boot. @@ -1962,7 +1962,7 @@ config SYSCTL_KUNIT_TEST If unsure, say N. config LIST_KUNIT_TEST - bool "KUnit Test for Kernel Linked-list structures" + tristate "KUnit Test for Kernel Linked-list structures" depends on KUNIT help This builds the linked list KUnit test suite. diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig index af37016..9ebd5e6 100644 --- a/lib/kunit/Kconfig +++ b/lib/kunit/Kconfig @@ -15,7 +15,7 @@ menuconfig KUNIT if KUNIT config KUNIT_TEST - bool "KUnit test for KUnit" + tristate "KUnit test for 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 @@ -24,7 +24,7 @@ config KUNIT_TEST expected. config KUNIT_EXAMPLE_TEST - bool "Example test for KUnit" + tristate "Example test for 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 diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 769d940..bc6e5e54 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -3,7 +3,11 @@ obj-$(CONFIG_KUNIT) += test.o \ assert.o \ try-catch.o -obj-$(CONFIG_KUNIT_TEST) += test-test.o \ - string-stream-test.o +obj-$(CONFIG_KUNIT_TEST) += kunit-test.o -obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += example-test.o +# string-stream-test compiles built-in only. +ifeq ($(CONFIG_KUNIT_TEST),y) +obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o +endif + +obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += kunit-example-test.o diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c index 9aca71c..b24bebc 100644 --- a/lib/kunit/assert.c +++ b/lib/kunit/assert.c @@ -26,6 +26,7 @@ void kunit_base_assert_format(const struct kunit_assert *assert, string_stream_add(stream, "%s FAILED at %s:%d\n", expect_or_assert, assert->file, assert->line); } +EXPORT_SYMBOL_GPL(kunit_base_assert_format); void kunit_assert_print_msg(const struct kunit_assert *assert, struct string_stream *stream) @@ -33,6 +34,7 @@ void kunit_assert_print_msg(const struct kunit_assert *assert, if (assert->message.fmt) string_stream_add(stream, "\n%pV", &assert->message); } +EXPORT_SYMBOL_GPL(kunit_assert_print_msg); void kunit_fail_assert_format(const struct kunit_assert *assert, struct string_stream *stream) @@ -40,6 +42,7 @@ void kunit_fail_assert_format(const struct kunit_assert *assert, kunit_base_assert_format(assert, stream); string_stream_add(stream, "%pV", &assert->message); } +EXPORT_SYMBOL_GPL(kunit_fail_assert_format); void kunit_unary_assert_format(const struct kunit_assert *assert, struct string_stream *stream) @@ -58,6 +61,7 @@ void kunit_unary_assert_format(const struct kunit_assert *assert, unary_assert->condition); kunit_assert_print_msg(assert, stream); } +EXPORT_SYMBOL_GPL(kunit_unary_assert_format); void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert, struct string_stream *stream) @@ -78,6 +82,7 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert, } kunit_assert_print_msg(assert, stream); } +EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format); void kunit_binary_assert_format(const struct kunit_assert *assert, struct string_stream *stream) @@ -99,6 +104,7 @@ void kunit_binary_assert_format(const struct kunit_assert *assert, binary_assert->right_value); kunit_assert_print_msg(assert, stream); } +EXPORT_SYMBOL_GPL(kunit_binary_assert_format); void kunit_binary_ptr_assert_format(const struct kunit_assert *assert, struct string_stream *stream) @@ -120,6 +126,7 @@ void kunit_binary_ptr_assert_format(const struct kunit_assert *assert, binary_assert->right_value); kunit_assert_print_msg(assert, stream); } +EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format); void kunit_binary_str_assert_format(const struct kunit_assert *assert, struct string_stream *stream) @@ -141,3 +148,4 @@ void kunit_binary_str_assert_format(const struct kunit_assert *assert, binary_assert->right_value); kunit_assert_print_msg(assert, stream); } +EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format); diff --git a/lib/kunit/example-test.c b/lib/kunit/example-test.c deleted file mode 100644 index f64a829..0000000 --- a/lib/kunit/example-test.c +++ /dev/null @@ -1,88 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* - * Example KUnit test to show how to use KUnit. - * - * Copyright (C) 2019, Google LLC. - * Author: Brendan Higgins - */ - -#include - -/* - * This is the most fundamental element of KUnit, the test case. A test case - * makes a set EXPECTATIONs and ASSERTIONs about the behavior of some code; if - * any expectations or assertions are not met, the test fails; otherwise, the - * test passes. - * - * In KUnit, a test case is just a function with the signature - * `void (*)(struct kunit *)`. `struct kunit` is a context object that stores - * information about the current test. - */ -static void example_simple_test(struct kunit *test) -{ - /* - * This is an EXPECTATION; it is how KUnit tests things. When you want - * to test a piece of code, you set some expectations about what the - * code should do. KUnit then runs the test and verifies that the code's - * behavior matched what was expected. - */ - KUNIT_EXPECT_EQ(test, 1 + 1, 2); -} - -/* - * This is run once before each test case, see the comment on - * example_test_suite for more information. - */ -static int example_test_init(struct kunit *test) -{ - kunit_info(test, "initializing\n"); - - return 0; -} - -/* - * Here we make a list of all the test cases we want to add to the test suite - * below. - */ -static struct kunit_case example_test_cases[] = { - /* - * This is a helper to create a test case object from a test case - * function; its exact function is not important to understand how to - * use KUnit, just know that this is how you associate test cases with a - * test suite. - */ - KUNIT_CASE(example_simple_test), - {} -}; - -/* - * This defines a suite or grouping of tests. - * - * Test cases are defined as belonging to the suite by adding them to - * `kunit_cases`. - * - * Often it is desirable to run some function which will set up things which - * will be used by every test; this is accomplished with an `init` function - * which runs before each test case is invoked. Similarly, an `exit` function - * may be specified which runs after every test case and can be used to for - * cleanup. For clarity, running tests in a test suite would behave as follows: - * - * suite.init(test); - * suite.test_case[0](test); - * suite.exit(test); - * suite.init(test); - * suite.test_case[1](test); - * suite.exit(test); - * ...; - */ -static struct kunit_suite example_test_suite = { - .name = "example", - .init = example_test_init, - .test_cases = example_test_cases, -}; - -/* - * This registers the above test suite telling KUnit that this is a suite of - * tests that need to be run. - */ -kunit_test_suite(example_test_suite); diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c new file mode 100644 index 0000000..ff930e0 --- /dev/null +++ b/lib/kunit/kunit-example-test.c @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Example KUnit test to show how to use KUnit. + * + * Copyright (C) 2019, Google LLC. + * Author: Brendan Higgins + */ + +#include + +/* + * This is the most fundamental element of KUnit, the test case. A test case + * makes a set EXPECTATIONs and ASSERTIONs about the behavior of some code; if + * any expectations or assertions are not met, the test fails; otherwise, the + * test passes. + * + * In KUnit, a test case is just a function with the signature + * `void (*)(struct kunit *)`. `struct kunit` is a context object that stores + * information about the current test. + */ +static void example_simple_test(struct kunit *test) +{ + /* + * This is an EXPECTATION; it is how KUnit tests things. When you want + * to test a piece of code, you set some expectations about what the + * code should do. KUnit then runs the test and verifies that the code's + * behavior matched what was expected. + */ + KUNIT_EXPECT_EQ(test, 1 + 1, 2); +} + +/* + * This is run once before each test case, see the comment on + * example_test_suite for more information. + */ +static int example_test_init(struct kunit *test) +{ + kunit_info(test, "initializing\n"); + + return 0; +} + +/* + * Here we make a list of all the test cases we want to add to the test suite + * below. + */ +static struct kunit_case example_test_cases[] = { + /* + * This is a helper to create a test case object from a test case + * function; its exact function is not important to understand how to + * use KUnit, just know that this is how you associate test cases with a + * test suite. + */ + KUNIT_CASE(example_simple_test), + {} +}; + +/* + * This defines a suite or grouping of tests. + * + * Test cases are defined as belonging to the suite by adding them to + * `kunit_cases`. + * + * Often it is desirable to run some function which will set up things which + * will be used by every test; this is accomplished with an `init` function + * which runs before each test case is invoked. Similarly, an `exit` function + * may be specified which runs after every test case and can be used to for + * cleanup. For clarity, running tests in a test suite would behave as follows: + * + * suite.init(test); + * suite.test_case[0](test); + * suite.exit(test); + * suite.init(test); + * suite.test_case[1](test); + * suite.exit(test); + * ...; + */ +static struct kunit_suite example_test_suite = { + .name = "example", + .init = example_test_init, + .test_cases = example_test_cases, +}; + +/* + * This registers the above test suite telling KUnit that this is a suite of + * tests that need to be run. + */ +kunit_test_suites(&example_test_suite); + +MODULE_LICENSE("GPL"); diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c new file mode 100644 index 0000000..1f5942e --- /dev/null +++ b/lib/kunit/kunit-test.c @@ -0,0 +1,334 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for core test infrastructure. + * + * Copyright (C) 2019, Google LLC. + * Author: Brendan Higgins + */ +#include + +#include "try-catch-impl.h" + +struct kunit_try_catch_test_context { + struct kunit_try_catch *try_catch; + bool function_called; +}; + +static void kunit_test_successful_try(void *data) +{ + struct kunit *test = data; + struct kunit_try_catch_test_context *ctx = test->priv; + + ctx->function_called = true; +} + +static void kunit_test_no_catch(void *data) +{ + struct kunit *test = data; + + KUNIT_FAIL(test, "Catch should not be called\n"); +} + +static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test) +{ + struct kunit_try_catch_test_context *ctx = test->priv; + struct kunit_try_catch *try_catch = ctx->try_catch; + + kunit_try_catch_init(try_catch, + test, + kunit_test_successful_try, + kunit_test_no_catch); + kunit_try_catch_run(try_catch, test); + + KUNIT_EXPECT_TRUE(test, ctx->function_called); +} + +static void kunit_test_unsuccessful_try(void *data) +{ + struct kunit *test = data; + struct kunit_try_catch_test_context *ctx = test->priv; + struct kunit_try_catch *try_catch = ctx->try_catch; + + kunit_try_catch_throw(try_catch); + KUNIT_FAIL(test, "This line should never be reached\n"); +} + +static void kunit_test_catch(void *data) +{ + struct kunit *test = data; + struct kunit_try_catch_test_context *ctx = test->priv; + + ctx->function_called = true; +} + +static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test) +{ + struct kunit_try_catch_test_context *ctx = test->priv; + struct kunit_try_catch *try_catch = ctx->try_catch; + + kunit_try_catch_init(try_catch, + test, + kunit_test_unsuccessful_try, + kunit_test_catch); + kunit_try_catch_run(try_catch, test); + + KUNIT_EXPECT_TRUE(test, ctx->function_called); +} + +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; +} + +static struct kunit_case kunit_try_catch_test_cases[] = { + KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch), + KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch), + {} +}; + +static struct kunit_suite kunit_try_catch_test_suite = { + .name = "kunit-try-catch-test", + .init = kunit_try_catch_test_init, + .test_cases = kunit_try_catch_test_cases, +}; + +/* + * 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_destroy_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_ASSERT_FALSE(test, + kunit_resource_destroy(&ctx->test, + kunit_resource_instance_match, + res->free, + res->allocation)); + + 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(brendanhiggins@google.com): 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); + + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); + + 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_destroy_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_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite); + +MODULE_LICENSE("GPL"); diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 6c70dc8..110f3a9 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -50,4 +50,4 @@ static void string_stream_test_get_string(struct kunit *test) .name = "string-stream-test", .test_cases = string_stream_test_cases }; -kunit_test_suite(string_stream_test_suite); +kunit_test_suites(&string_stream_test_suite); diff --git a/lib/kunit/test-test.c b/lib/kunit/test-test.c deleted file mode 100644 index 5a6cc04..0000000 --- a/lib/kunit/test-test.c +++ /dev/null @@ -1,333 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* - * KUnit test for core test infrastructure. - * - * Copyright (C) 2019, Google LLC. - * Author: Brendan Higgins - */ -#include - -#include "try-catch-impl.h" - -struct kunit_try_catch_test_context { - struct kunit_try_catch *try_catch; - bool function_called; -}; - -static void kunit_test_successful_try(void *data) -{ - struct kunit *test = data; - struct kunit_try_catch_test_context *ctx = test->priv; - - ctx->function_called = true; -} - -static void kunit_test_no_catch(void *data) -{ - struct kunit *test = data; - - KUNIT_FAIL(test, "Catch should not be called\n"); -} - -static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test) -{ - struct kunit_try_catch_test_context *ctx = test->priv; - struct kunit_try_catch *try_catch = ctx->try_catch; - - kunit_try_catch_init(try_catch, - test, - kunit_test_successful_try, - kunit_test_no_catch); - kunit_try_catch_run(try_catch, test); - - KUNIT_EXPECT_TRUE(test, ctx->function_called); -} - -static void kunit_test_unsuccessful_try(void *data) -{ - struct kunit *test = data; - struct kunit_try_catch_test_context *ctx = test->priv; - struct kunit_try_catch *try_catch = ctx->try_catch; - - kunit_try_catch_throw(try_catch); - KUNIT_FAIL(test, "This line should never be reached\n"); -} - -static void kunit_test_catch(void *data) -{ - struct kunit *test = data; - struct kunit_try_catch_test_context *ctx = test->priv; - - ctx->function_called = true; -} - -static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test) -{ - struct kunit_try_catch_test_context *ctx = test->priv; - struct kunit_try_catch *try_catch = ctx->try_catch; - - kunit_try_catch_init(try_catch, - test, - kunit_test_unsuccessful_try, - kunit_test_catch); - kunit_try_catch_run(try_catch, test); - - KUNIT_EXPECT_TRUE(test, ctx->function_called); -} - -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; -} - -static struct kunit_case kunit_try_catch_test_cases[] = { - KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch), - KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch), - {} -}; - -static struct kunit_suite kunit_try_catch_test_suite = { - .name = "kunit-try-catch-test", - .init = kunit_try_catch_test_init, - .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_destroy_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_ASSERT_FALSE(test, - kunit_resource_destroy(&ctx->test, - kunit_resource_instance_match, - res->free, - res->allocation)); - - 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(brendanhiggins@google.com): 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); - - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); - - 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_destroy_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); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 58a6227..87b5cf1 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -173,6 +173,7 @@ void kunit_do_assertion(struct kunit *test, if (assert->type == KUNIT_ASSERTION) kunit_abort(test); } +EXPORT_SYMBOL_GPL(kunit_do_assertion); void kunit_init_test(struct kunit *test, const char *name) { @@ -181,6 +182,7 @@ void kunit_init_test(struct kunit *test, const char *name) test->name = name; test->success = true; } +EXPORT_SYMBOL_GPL(kunit_init_test); /* * Initializes and runs test case. Does not clean up or do post validations. @@ -319,6 +321,7 @@ int kunit_run_tests(struct kunit_suite *suite) return 0; } +EXPORT_SYMBOL_GPL(kunit_run_tests); struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test, kunit_resource_init_t init, @@ -344,6 +347,7 @@ struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test, return res; } +EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource); static void kunit_resource_free(struct kunit *test, struct kunit_resource *res) { @@ -402,6 +406,7 @@ int kunit_resource_destroy(struct kunit *test, kunit_resource_free(test, resource); return 0; } +EXPORT_SYMBOL_GPL(kunit_resource_destroy); struct kunit_kmalloc_params { size_t size; @@ -437,6 +442,7 @@ void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) gfp, ¶ms); } +EXPORT_SYMBOL_GPL(kunit_kmalloc); void kunit_kfree(struct kunit *test, const void *ptr) { @@ -449,6 +455,7 @@ void kunit_kfree(struct kunit *test, const void *ptr) WARN_ON(rc); } +EXPORT_SYMBOL_GPL(kunit_kfree); void kunit_cleanup(struct kunit *test) { @@ -478,3 +485,4 @@ void kunit_cleanup(struct kunit *test) kunit_resource_free(test, resource); } } +EXPORT_SYMBOL_GPL(kunit_cleanup); diff --git a/lib/kunit/try-catch.c b/lib/kunit/try-catch.c index 4a66d16..0247a28 100644 --- a/lib/kunit/try-catch.c +++ b/lib/kunit/try-catch.c @@ -20,6 +20,7 @@ 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); } +EXPORT_SYMBOL_GPL(kunit_try_catch_throw); static int kunit_generic_run_threadfn_adapter(void *data) { @@ -107,3 +108,4 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) try_catch->catch(try_catch->context); } +EXPORT_SYMBOL_GPL(kunit_try_catch_run); diff --git a/lib/list-test.c b/lib/list-test.c index 363c600..4c31a91 100644 --- a/lib/list-test.c +++ b/lib/list-test.c @@ -743,4 +743,6 @@ static void list_test_list_for_each_entry_reverse(struct kunit *test) .test_cases = list_test_cases, }; -kunit_test_suite(list_test_module); +kunit_test_suites(&list_test_module); + +MODULE_LICENSE("GPL"); From patchwork Fri Nov 15 10:16:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 11245965 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DBF6B159A for ; Fri, 15 Nov 2019 10:18:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BC3F7207FA for ; Fri, 15 Nov 2019 10:18:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="iDXjXEt1" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727591AbfKOKSF (ORCPT ); Fri, 15 Nov 2019 05:18:05 -0500 Received: from userp2130.oracle.com ([156.151.31.86]:47718 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727134AbfKOKSE (ORCPT ); Fri, 15 Nov 2019 05:18:04 -0500 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xAFAEITt047434; Fri, 15 Nov 2019 10:16:42 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2019-08-05; bh=P+HiVSl1jyeAKRlbuhH0mupiRsIP0/UxZq92oAfDDa0=; b=iDXjXEt1yuowJ4YIPk8xoU6oq4qYiEeje0rFnIEyBHg+KTaZb66llIpBGdJy7pc2MgHK r5OZp2TUZ2yi0cKrAtEYbBaSA8jcEtR/Opcfgla9om84hvTZG0jagJoEJEqWE3o+Zbjj tS7VoTgh5xdaAuDH/ubM78mlyt90N+1tcHMKim9WmsMbIKNKENXUKfIb48++SWIo2uJ/ w4zLXpJgqcFa3B8Ui5rOR8UEwg+SiitObUXpwJb8Ws1fDzpzAbRui7pm5d0BQmFDXrcT 1ggG8Cc9OK53K3/dwEDjYd1GuZ+X7HXCtyDiyXctKtiGWevZhicEJfxvfb1MTPhMOboZ Jg== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by userp2130.oracle.com with ESMTP id 2w9gxpje6a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2019 10:16:42 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xAFADDL3007920; Fri, 15 Nov 2019 10:16:42 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userp3030.oracle.com with ESMTP id 2w9h148ff4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2019 10:16:42 +0000 Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id xAFAGe8p016353; Fri, 15 Nov 2019 10:16:40 GMT Received: from dhcp-10-175-208-51.vpn.oracle.com (/10.175.208.51) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 15 Nov 2019 02:16:40 -0800 From: Alan Maguire To: brendanhiggins@google.com, skhan@linuxfoundation.org, linux-kselftest@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, keescook@chromium.org, yzaikin@google.com, akpm@linux-foundation.org, yamada.masahiro@socionext.com, catalin.marinas@arm.com, joe.lawrence@redhat.com, penguin-kernel@i-love.sakura.ne.jp, schowdary@nvidia.com, urezki@gmail.com, andriy.shevchenko@linux.intel.com, corbet@lwn.net, tytso@mit.edu, adilger.kernel@dilger.ca, mcgrof@kernel.org, changbin.du@intel.com, linux-ext4@vger.kernel.org, linux-doc@vger.kernel.org, sboyd@kernel.org, Alan Maguire , Knut Omang Subject: [PATCH v4 linux-kselftest-test 4/6] kunit: remove timeout dependence on sysctl_hung_task_timeout_seconds Date: Fri, 15 Nov 2019 10:16:10 +0000 Message-Id: <1573812972-10529-5-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1573812972-10529-1-git-send-email-alan.maguire@oracle.com> References: <1573812972-10529-1-git-send-email-alan.maguire@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9441 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1911150096 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9441 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1911150096 Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org In discussion of how to handle timeouts, it was noted that if sysctl_hung_task_timeout_seconds is exceeded for a kunit test, the test task will be killed and an oops generated. This should suffice as a means of debugging such timeout issues for now. Hence remove use of sysctl_hung_task_timeout_secs, which has the added benefit of avoiding the need to export that symbol from the core kernel. Signed-off-by: Alan Maguire Signed-off-by: Knut Omang Reviewed-by: Stephen Boyd --- lib/kunit/try-catch.c | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/lib/kunit/try-catch.c b/lib/kunit/try-catch.c index 0247a28..0dd434e 100644 --- a/lib/kunit/try-catch.c +++ b/lib/kunit/try-catch.c @@ -11,7 +11,6 @@ #include #include #include -#include #include "try-catch-impl.h" @@ -33,8 +32,6 @@ static int kunit_generic_run_threadfn_adapter(void *data) static unsigned long kunit_test_timeout(void) { - unsigned long timeout_msecs; - /* * TODO(brendanhiggins@google.com): We should probably have some type of * variable timeout here. The only question is what that timeout value @@ -51,22 +48,11 @@ static unsigned long kunit_test_timeout(void) * * For more background on this topic, see: * https://mike-bland.com/2011/11/01/small-medium-large.html + * + * If tests timeout due to exceeding sysctl_hung_task_timeout_secs, + * the task will be killed and an oops generated. */ - if (sysctl_hung_task_timeout_secs) { - /* - * If sysctl_hung_task is active, just set the timeout to some - * value less than that. - * - * In regards to the above TODO, if we decide on variable - * timeouts, this logic will likely need to change. - */ - timeout_msecs = (sysctl_hung_task_timeout_secs - 1) * - MSEC_PER_SEC; - } else { - timeout_msecs = 300 * MSEC_PER_SEC; /* 5 min */ - } - - return timeout_msecs; + return 300 * MSEC_PER_SEC; /* 5 min */ } void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) From patchwork Fri Nov 15 10:16:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 11245975 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 09F6A1393 for ; Fri, 15 Nov 2019 10:19:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DD51A2075E for ; Fri, 15 Nov 2019 10:19:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="nHuYle8X" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727364AbfKOKT3 (ORCPT ); Fri, 15 Nov 2019 05:19:29 -0500 Received: from userp2130.oracle.com ([156.151.31.86]:49056 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725829AbfKOKT3 (ORCPT ); Fri, 15 Nov 2019 05:19:29 -0500 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xAFAEITu047434; Fri, 15 Nov 2019 10:16:48 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2019-08-05; bh=aUmmUofXeDq4ex4OiGNZ0rOva3H7BkAtzGPYlS55bLk=; b=nHuYle8Xty0AFSNWkPzS01umuJvgDfPs7wnJsKEkmb3wEmAFZA9m6ADpCi3xqoLsGtGd Z3KnGAiwTZMsrWUq/ifEwPFsT0E3obZCRX9/N3WhhOAIuRtzQ5vY93ewLEo9ZTkmE5Wd NNLKVjfN3sx2Et9XcM1iaqY5JWvKEToHIyJKQuA4a6y41COXAtwq4FF+A++Sv4ah0sbf HOsNmQHNnNTbXM82dvkXlDuAQ2tlz7f3bqBUUH6pfVL+qMpuchnyZd50ClZt/dh31VK4 oxZMn3cgvHjQ9WB1wgbUElqNpPmYoYqCjrzVdViSDriL7uxELP/adwQHlnVPos5kki9N uw== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by userp2130.oracle.com with ESMTP id 2w9gxpje6f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2019 10:16:48 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xAFADDrC007930; Fri, 15 Nov 2019 10:16:47 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by userp3030.oracle.com with ESMTP id 2w9h148fhj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2019 10:16:47 +0000 Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id xAFAGi6e007575; Fri, 15 Nov 2019 10:16:44 GMT Received: from dhcp-10-175-208-51.vpn.oracle.com (/10.175.208.51) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 15 Nov 2019 02:16:44 -0800 From: Alan Maguire To: brendanhiggins@google.com, skhan@linuxfoundation.org, linux-kselftest@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, keescook@chromium.org, yzaikin@google.com, akpm@linux-foundation.org, yamada.masahiro@socionext.com, catalin.marinas@arm.com, joe.lawrence@redhat.com, penguin-kernel@i-love.sakura.ne.jp, schowdary@nvidia.com, urezki@gmail.com, andriy.shevchenko@linux.intel.com, corbet@lwn.net, tytso@mit.edu, adilger.kernel@dilger.ca, mcgrof@kernel.org, changbin.du@intel.com, linux-ext4@vger.kernel.org, linux-doc@vger.kernel.org, sboyd@kernel.org, Alan Maguire , Knut Omang Subject: [PATCH v4 linux-kselftest-test 5/6] kunit: allow kunit to be loaded as a module Date: Fri, 15 Nov 2019 10:16:11 +0000 Message-Id: <1573812972-10529-6-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1573812972-10529-1-git-send-email-alan.maguire@oracle.com> References: <1573812972-10529-1-git-send-email-alan.maguire@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9441 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=3 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1911150096 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9441 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1911150096 Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Making kunit itself buildable as a module allows for "always-on" kunit configuration; specifying CONFIG_KUNIT=m means the module is built but only used when loaded. Kunit test modules will load kunit.ko as an implicit dependency, so simply running "modprobe my-kunit-tests" will load the tests along with the kunit module and run them. Signed-off-by: Alan Maguire Signed-off-by: Knut Omang --- lib/kunit/Kconfig | 2 +- lib/kunit/Makefile | 4 +++- lib/kunit/test.c | 13 +++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig index 9ebd5e6..065aa16 100644 --- a/lib/kunit/Kconfig +++ b/lib/kunit/Kconfig @@ -3,7 +3,7 @@ # menuconfig KUNIT - bool "KUnit - Enable support for unit tests" + tristate "KUnit - Enable support for unit tests" help Enables support for kernel unit tests (KUnit), a lightweight unit testing and mocking framework for the Linux kernel. These tests are diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index bc6e5e54..fab5564 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -1,4 +1,6 @@ -obj-$(CONFIG_KUNIT) += test.o \ +obj-$(CONFIG_KUNIT) += kunit.o + +kunit-objs += test.o \ string-stream.o \ assert.o \ try-catch.o diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 87b5cf1..41ef71a 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -486,3 +486,16 @@ void kunit_cleanup(struct kunit *test) } } EXPORT_SYMBOL_GPL(kunit_cleanup); + +static int kunit_init(void) +{ + return 0; +} +late_initcall(kunit_init); + +static void __exit kunit_exit(void) +{ +} +module_exit(kunit_exit); + +MODULE_LICENSE("GPL"); From patchwork Fri Nov 15 10:16:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 11245957 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8A36E159A for ; Fri, 15 Nov 2019 10:17:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5B1C820740 for ; Fri, 15 Nov 2019 10:17:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="QmXceClB" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727521AbfKOKRO (ORCPT ); Fri, 15 Nov 2019 05:17:14 -0500 Received: from userp2120.oracle.com ([156.151.31.85]:53936 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727036AbfKOKRN (ORCPT ); Fri, 15 Nov 2019 05:17:13 -0500 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xAFAELeI021035; Fri, 15 Nov 2019 10:16:50 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2019-08-05; bh=kEBzW5bFpEsigVBsTTXhd4LHdOdlS9x/rndpgUook+0=; b=QmXceClB8p34iys5VDuKVySkAzZ4nh9dFKS4BNrvUbfMN+yZoNVZN1iKt1naAftTGmYu jHsxmKfERo5ya7YAeJdViHurp4hZkbUAj/IQDfw5hfaYAd/bjOJelRpVNCzih9pVH1TV HOZPCDXCpZDwrewquKXdkU2eC8LX5HgWReEARQ29ZX6kXJR2uyRhHiIPHCfn9L7egDEE DVoejjxyLFLd4dwDVvz8+LR5atlQLwEFKvdocAoqlCgYcJB/v5yWjdvzgOze0pX4szs9 ymxaqO/tpKr/emc5HaxhwwFC1QioLZMmw3h87eIDCcFyC9FbN4fFgICksIk1dYWHXPxw +w== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by userp2120.oracle.com with ESMTP id 2w9gxpje2p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2019 10:16:50 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xAFADEql008030; Fri, 15 Nov 2019 10:16:49 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by userp3030.oracle.com with ESMTP id 2w9h148fjm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2019 10:16:49 +0000 Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id xAFAGm0d007595; Fri, 15 Nov 2019 10:16:48 GMT Received: from dhcp-10-175-208-51.vpn.oracle.com (/10.175.208.51) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 15 Nov 2019 02:16:48 -0800 From: Alan Maguire To: brendanhiggins@google.com, skhan@linuxfoundation.org, linux-kselftest@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, keescook@chromium.org, yzaikin@google.com, akpm@linux-foundation.org, yamada.masahiro@socionext.com, catalin.marinas@arm.com, joe.lawrence@redhat.com, penguin-kernel@i-love.sakura.ne.jp, schowdary@nvidia.com, urezki@gmail.com, andriy.shevchenko@linux.intel.com, corbet@lwn.net, tytso@mit.edu, adilger.kernel@dilger.ca, mcgrof@kernel.org, changbin.du@intel.com, linux-ext4@vger.kernel.org, linux-doc@vger.kernel.org, sboyd@kernel.org, Alan Maguire , Knut Omang Subject: [PATCH v4 linux-kselftest-test 6/6] kunit: update documentation to describe module-based build Date: Fri, 15 Nov 2019 10:16:12 +0000 Message-Id: <1573812972-10529-7-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1573812972-10529-1-git-send-email-alan.maguire@oracle.com> References: <1573812972-10529-1-git-send-email-alan.maguire@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9441 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=4 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1911150096 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9441 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=4 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1911140001 definitions=main-1911150096 Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Documentation should describe how to build kunit and tests as modules. Signed-off-by: Alan Maguire Signed-off-by: Knut Omang Reviewed-by: Stephen Boyd --- Documentation/dev-tools/kunit/faq.rst | 3 ++- Documentation/dev-tools/kunit/index.rst | 3 +++ Documentation/dev-tools/kunit/usage.rst | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Documentation/dev-tools/kunit/faq.rst b/Documentation/dev-tools/kunit/faq.rst index bf20951..ea55b24 100644 --- a/Documentation/dev-tools/kunit/faq.rst +++ b/Documentation/dev-tools/kunit/faq.rst @@ -29,7 +29,8 @@ Yes, well, mostly. For the most part, the KUnit core framework (what you use to write the tests) can compile to any architecture; it compiles like just another part of the -kernel and runs when the kernel boots. However, there is some infrastructure, +kernel and runs when the kernel boots, or when built as a module, when the +module is loaded. However, there is some infrastructure, like the KUnit Wrapper (``tools/testing/kunit/kunit.py``) that does not support other architectures. diff --git a/Documentation/dev-tools/kunit/index.rst b/Documentation/dev-tools/kunit/index.rst index 26ffb46..7ddc385 100644 --- a/Documentation/dev-tools/kunit/index.rst +++ b/Documentation/dev-tools/kunit/index.rst @@ -48,6 +48,9 @@ to a standalone program that can be run like any other program directly inside of a host operating system; to be clear, it does not require any virtualization support; it is just a regular program. +Alternatively, kunit and kunit tests can be built as modules and tests will +run when the test module is loaded. + KUnit is fast. Excluding build time, from invocation to completion KUnit can run several dozen tests in only 10 to 20 seconds; this might not sound like a big deal to some people, but having such fast and easy to run tests fundamentally diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst index c6e6963..82f9213 100644 --- a/Documentation/dev-tools/kunit/usage.rst +++ b/Documentation/dev-tools/kunit/usage.rst @@ -539,6 +539,22 @@ Interspersed in the kernel logs you might see the following: Congratulations, you just ran a KUnit test on the x86 architecture! +In a similar manner, kunit and kunit tests can also be built as modules, +so if you wanted to run tests in this way you might add the following config +options to your ``.config``: + +.. code-block:: none + + CONFIG_KUNIT=m + CONFIG_KUNIT_EXAMPLE_TEST=m + +Once the kernel is built and installed, a simple + +.. code-block:: bash + modprobe example-test + +...will run the tests. + Writing new tests for other architectures -----------------------------------------