From patchwork Fri Apr 21 04:02:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 13219454 X-Patchwork-Delegate: brendanhiggins@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C69A4C77B75 for ; Fri, 21 Apr 2023 04:02:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232447AbjDUEC2 (ORCPT ); Fri, 21 Apr 2023 00:02:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230200AbjDUEC0 (ORCPT ); Fri, 21 Apr 2023 00:02:26 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9005840C7 for ; Thu, 20 Apr 2023 21:02:24 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-54fc35ab48fso4405127b3.2 for ; Thu, 20 Apr 2023 21:02:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682049744; x=1684641744; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=o9d5prSVTa/w/iAx/0mRPU+MJNkirOzNMYWiYZtwtOw=; b=5qqiOD9JYyFpBBk6u/jB7iUcDv4aCe4LpP2tTwyJvFdy/RRC2Ydet8dhwQA02BIe6+ xQFkxKU3Cb7FJKJPCKd7M/r72X3ddDsIyOeEQXjdVoHY5VbFoGq4pGTE8yTpsbgceLov Pb0YepRKcbUXq+QEl8ChusLCfRnbrHad+x0ennLc3phDdhOfazZhUZSWgwWap8OHNEXh Bm6pOZxhhSi3Jsrinee2ZSrkfrQeqz10Kg02iwIXt6Rdi0hF/nS0IDYJJxsPEGpzoTPS kcb4z+5nPSD7NiQCNTXfFB8h0bi6JB4Q2cLFuJZLRXSYedGs54zZVNNBo85EkTMh61vZ 70CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682049744; x=1684641744; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=o9d5prSVTa/w/iAx/0mRPU+MJNkirOzNMYWiYZtwtOw=; b=G/GOfFumQeLZpqy/J8Impe0ELK3qvNKukqvFmqp96J2PwY25Y5zSqXopjluJZRi8p5 o/mhcJk/KHYO75UT7uq7GnI8EoqY6uYWhVaEKS5jePssawT+3g06KB5/9aeCVEZN4PlI mO1Vnv5vD3F0XnG7MrukmCaUFTzrxcVQ6G76VjSAKjdlJG3ZUAvgjMTQzeLq9KuRsglW zIynXsTn0SOFIqpLzz3e913ySRYPrGY24x4ox8BUXjSdZQ/dM6hFXb7W+thIhuGvmkVh NWEsnPTJ4wiZ2It/ObwF9iLh/ahqp53du5r0IOzrNdn73r/BfnTIBYYvKxN9EkWO+oip kpGA== X-Gm-Message-State: AAQBX9czSRxa9GoKNBgE1I+kJf4O+AiqRtPBIYsGJHyyVfc4I7tZ8L3B WXVfIbCmH8JlbeqUtadw9aXqQyGOAqBgOQ== X-Google-Smtp-Source: AKy350auvqRIr2cxFc6OvBQ6nlmzF6QWoEWHmkIgQLzzTfkffTVCH4iJjocmiVjzpmkXW9Wc0DKBTyzP7KbROQ== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:450c:0:b0:545:883a:544d with SMTP id s12-20020a81450c000000b00545883a544dmr578383ywa.9.1682049743866; Thu, 20 Apr 2023 21:02:23 -0700 (PDT) Date: Fri, 21 Apr 2023 12:02:15 +0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230421040218.2156548-1-davidgow@google.com> Subject: [PATCH v3 1/4] kunit: Always run cleanup from a test kthread From: David Gow To: Benjamin Berg , Brendan Higgins , Shuah Khan , Rae Moar , Daniel Latypov Cc: David Gow , maxime@cerno.tech, Stephen Boyd , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Sadiya Kazi Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org KUnit tests run in a kthread, with the current->kunit_test pointer set to the test's context. This allows the kunit_get_current_test() and kunit_fail_current_test() macros to work. Normally, this pointer is still valid during test shutdown (i.e., the suite->exit function, and any resource cleanup). However, if the test has exited early (e.g., due to a failed assertion), the cleanup is done in the parent KUnit thread, which does not have an active context. Instead, in the event test terminates early, run the test exit and cleanup from a new 'cleanup' kthread, which sets current->kunit_test, and better isolates the rest of KUnit from issues which arise in test cleanup. If a test cleanup function itself aborts (e.g., due to an assertion failing), there will be no further attempts to clean up: an error will be logged and the test failed. For example: # example_simple_test: test aborted during cleanup. continuing without cleaning up This should also make it easier to get access to the KUnit context, particularly from within resource cleanup functions, which may, for example, need access to data in test->priv. Signed-off-by: David Gow Reviewed-by: Benjamin Berg Reviewed-by: Maxime Ripard Tested-by: Maxime Ripard --- This is an updated version of / replacement of "kunit: Set the current KUnit context when cleaning up", which instead creates a new kthread for cleanup tasks if the original test kthread is aborted. This protects us from failed assertions during cleanup, if the test exited early. Changes since v2: https://lore.kernel.org/linux-kselftest/20230419085426.1671703-1-davidgow@google.com/ - Always run cleanup in its own kthread - Therefore, never attempt to re-run it if it exits - Thanks, Benjamin. Changes since v1: https://lore.kernel.org/linux-kselftest/20230415091401.681395-1-davidgow@google.com/ - Move cleanup execution to another kthread - (Thanks, Benjamin, for pointing out the assertion issues) --- lib/kunit/test.c | 55 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/lib/kunit/test.c b/lib/kunit/test.c index e2910b261112..2025e51941e6 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -419,10 +419,50 @@ static void kunit_try_run_case(void *data) * thread will resume control and handle any necessary clean up. */ kunit_run_case_internal(test, suite, test_case); - /* This line may never be reached. */ +} + +static void kunit_try_run_case_cleanup(void *data) +{ + struct kunit_try_catch_context *ctx = data; + struct kunit *test = ctx->test; + struct kunit_suite *suite = ctx->suite; + + current->kunit_test = test; + kunit_run_case_cleanup(test, suite); } +static void kunit_catch_run_case_cleanup(void *data) +{ + struct kunit_try_catch_context *ctx = data; + struct kunit *test = ctx->test; + int try_exit_code = kunit_try_catch_get_result(&test->try_catch); + + /* It is always a failure if cleanup aborts. */ + kunit_set_failure(test); + + if (try_exit_code) { + /* + * Test case could not finish, we have no idea what state it is + * in, so don't do clean up. + */ + if (try_exit_code == -ETIMEDOUT) { + kunit_err(test, "test case cleanup timed out\n"); + /* + * Unknown internal error occurred preventing test case from + * running, so there is nothing to clean up. + */ + } else { + kunit_err(test, "internal error occurred during test case cleanup: %d\n", + try_exit_code); + } + return; + } + + kunit_err(test, "test aborted during cleanup. continuing without cleaning up\n"); +} + + static void kunit_catch_run_case(void *data) { struct kunit_try_catch_context *ctx = data; @@ -448,12 +488,6 @@ static void kunit_catch_run_case(void *data) } return; } - - /* - * Test case was run, but aborted. It is the test case's business as to - * whether it failed or not, we just need to clean up. - */ - kunit_run_case_cleanup(test, suite); } /* @@ -478,6 +512,13 @@ static void kunit_run_case_catch_errors(struct kunit_suite *suite, context.test_case = test_case; kunit_try_catch_run(try_catch, &context); + /* Now run the cleanup */ + kunit_try_catch_init(try_catch, + test, + kunit_try_run_case_cleanup, + kunit_catch_run_case_cleanup); + kunit_try_catch_run(try_catch, &context); + /* Propagate the parameter result to the test case. */ if (test->status == KUNIT_FAILURE) test_case->status = KUNIT_FAILURE; From patchwork Fri Apr 21 04:02:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 13219455 X-Patchwork-Delegate: brendanhiggins@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C6EFC77B75 for ; Fri, 21 Apr 2023 04:02:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233753AbjDUECi (ORCPT ); Fri, 21 Apr 2023 00:02:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229660AbjDUECa (ORCPT ); Fri, 21 Apr 2023 00:02:30 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1165D40C7 for ; Thu, 20 Apr 2023 21:02:29 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-54f8a3f6b03so18096637b3.2 for ; Thu, 20 Apr 2023 21:02:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682049748; x=1684641748; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6axHHeceCfT18Iy9/ahE+yGfyWAXleNOBhmrrlTMIXA=; b=Zd0eS4RY8TdnluDs8kHriFgx0zcAaGgzda+ecPw1Et8sMbW84233s0kcsmUCTayTKt 39DD/0vEMLFYIbCGcHuT4jt4ze798iBDxMd2S3fLLChUbSRIgy+mpyNwRghxERfG4P99 IjWMCqHQNiVOWq/PD+UCZrXGEggqdXohmrqDWxe4Ieih3oRBRMUdqqxqmnoUtrLx+n/1 BouKZBgAliqQHTUnk3uYUrDnuaNLcr8tePVKUT+ioVQet42AlbT/j5rgQs08w9IN0enO Er1Lb6UWBKGses/tebvlTvtE3HimYo+gq6GNVxhgXro6kYyHyDbh6t6FbT/jNJslfK/a ICfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682049748; x=1684641748; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6axHHeceCfT18Iy9/ahE+yGfyWAXleNOBhmrrlTMIXA=; b=BaPmZMd5fs7PwHmUBWM3MAM8iZ9KoSWBrQfngX0qd1j6NclF/SgcHAtAtqZlVCo1Zt qHFGKpHFSswboqZ7dMO80lgBS9/AkvGFzh8t30gEejne0RAPDvR5qMNoDaGZoUQP6AWH ckTx1YPVhm8DboI1DqF2Mqmlz/r3qfwIzrrd6yoPsPk6kPhPASdUhHaM9hOYq2ptfUm1 XyxbHH7jR0TDjB17OlZXvd2N64h6d1iQsMiLwZga20j0/LxsDMneBVNtKy3JKpwoB5Sl 9jOeCspkXWg+doGV1dT0JI6JIuDJAJLcg7zad0UFmoSWPeBLza+IN2C4WzY2Odc33pVQ t0YA== X-Gm-Message-State: AAQBX9cq6GDpJ/eNipDv5BV5uOREHNubkTKS1tvr/R0U9e1e40bT2TxC vdsZ/PNhVVGxjlmusjkFp+9DCosmaUFXpg== X-Google-Smtp-Source: AKy350ajLMrF+3r3keuCLx0Vo4kxs32IK/Or+s0nVILEcr/GOQ1BqLQokRn267/2EXkLouihy9RC8iKSvytUiQ== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:b61e:0:b0:54f:17b6:f30a with SMTP id u30-20020a81b61e000000b0054f17b6f30amr593605ywh.4.1682049748345; Thu, 20 Apr 2023 21:02:28 -0700 (PDT) Date: Fri, 21 Apr 2023 12:02:16 +0800 In-Reply-To: <20230421040218.2156548-1-davidgow@google.com> Mime-Version: 1.0 References: <20230421040218.2156548-1-davidgow@google.com> X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230421040218.2156548-2-davidgow@google.com> Subject: [PATCH v3 2/4] Documentation: kunit: Note that assertions should not be used in cleanup From: David Gow To: Benjamin Berg , Brendan Higgins , Shuah Khan , Rae Moar , Daniel Latypov Cc: David Gow , maxime@cerno.tech, Stephen Boyd , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Sadiya Kazi Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org As assertions abort the test cleanup process, they should be avoided from within a suite's exit function, or from within resource 'free' functions. Unlike with initialisation or main test execution, no further cleanup will be performed after a failed assertion, potentially causing a leak of resources. Signed-off-by: David Gow Reviewed-by: Sadiya Kazi --- No changes since v2: https://lore.kernel.org/linux-kselftest/20230419085426.1671703-2-davidgow@google.com/ This patch was introduced in v2. --- Documentation/dev-tools/kunit/usage.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst index 9faf2b4153fc..9f720f1317d3 100644 --- a/Documentation/dev-tools/kunit/usage.rst +++ b/Documentation/dev-tools/kunit/usage.rst @@ -121,6 +121,12 @@ there's an allocation error. ``return`` so they only work from the test function. In KUnit, we stop the current kthread on failure, so you can call them from anywhere. +.. note:: + Warning: There is an exception to the above rule. You shouldn't use assertions + in the suite's exit() function, or in the free function for a resource. These + run when a test is shutting down, and an assertion here prevents further + cleanup code from running, potentially leading to a memory leak. + Customizing error messages -------------------------- From patchwork Fri Apr 21 04:02:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 13219456 X-Patchwork-Delegate: brendanhiggins@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9D317C7618E for ; Fri, 21 Apr 2023 04:02:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229660AbjDUECl (ORCPT ); Fri, 21 Apr 2023 00:02:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233575AbjDUECh (ORCPT ); Fri, 21 Apr 2023 00:02:37 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A6A465B8 for ; Thu, 20 Apr 2023 21:02:34 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-b8f34ed571bso1933464276.1 for ; Thu, 20 Apr 2023 21:02:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682049753; x=1684641753; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ZEBiF7ByWZHT8hk8CTAs7S8P0dMCig1qp9ER8e9wTTI=; b=zaKGvpPPkSpchoZHU6U0I24jBX2qIKZqPmCqLKQwxNsTvgcZmojAMcmLO77HZwoA2Q 5PUv8FWzIKAwO240MbvzUMu1QzqMgdq3d5WqERH/Hdv8lsL3xy/hdKDD63Pm3IzQqwrI gsqPoRnR/DC0HzKjOoaX2jp9LOfsnJerUmo1ZVU+dsM8uAMc8gqqZyUUTPMgBnW/t69x qcb4IqlcuqLlfka5CuirZs99gUDidt8kf3a0DXbNZHAJsStpaVx9/w9FVVBQhONmB82C GpyMdm0ZR+qTmteIe5XzfNe3lffxuOTR2xK/Y3B6tQ1v9dMVZEciYyM+grJaVeMepiOD qNkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682049753; x=1684641753; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZEBiF7ByWZHT8hk8CTAs7S8P0dMCig1qp9ER8e9wTTI=; b=HjGviQPLPvEqWeqCwM/8BaECFb9ZYHz6pA7Vwesxnc1yO9MOXmxrMNl+NJ3Wicq6II QAR/8/eOoefNDBHdVqzGB8Qt1PVy9O7ydSQZkiC7yHc5uTYBFkBZy56NdbaktNA1x1YM vwvodz0x/sIAJIVrdJgRkqeolBYUJ58Gxw1KQ0UoYu9PGZb3ezxUpE8EM7yLdPVdWPcj hYUNymrz8aojo0WGoHAAdFP1O04kxzLfCNcKk/3de0HrkU5kt9QkXnW/EkS45acYn1sg ni8SvHx3wNIbZjjpeNNGk+24UaDw/0QLUXfi5cLr6DpwP/PCM4JlEwMiSII56Yw/L0U5 ap0Q== X-Gm-Message-State: AAQBX9e+SNgiNHIZruZwR3/LpCZaU2dfIrik3yP1UT9HQtTuV7Kyo7PQ FZQulPY5mk5VKcyshHnzL6ufSByr384O0A== X-Google-Smtp-Source: AKy350Z4qe08DAOgSQCJ/aJkhSWjti/yq3AmrcunrXa5Dr5OO3XBAOZOvW7bm47CFcUVlqiZuF1VXH8w6XShUQ== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:ca12:0:b0:b96:347d:46e7 with SMTP id a18-20020a25ca12000000b00b96347d46e7mr905278ybg.1.1682049753474; Thu, 20 Apr 2023 21:02:33 -0700 (PDT) Date: Fri, 21 Apr 2023 12:02:17 +0800 In-Reply-To: <20230421040218.2156548-1-davidgow@google.com> Mime-Version: 1.0 References: <20230421040218.2156548-1-davidgow@google.com> X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230421040218.2156548-3-davidgow@google.com> Subject: [PATCH v3 3/4] Documentation: kunit: Warn that exit functions run even if init fails From: David Gow To: Benjamin Berg , Brendan Higgins , Shuah Khan , Rae Moar , Daniel Latypov Cc: David Gow , maxime@cerno.tech, Stephen Boyd , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Sadiya Kazi Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org KUnit's exit functions will run even if the corresponding init function fails. It's easy, when writing an exit function, to assume the init function succeeded, and (for example) access uninitialised memory or dereference NULL pointers. Note that this case exists and should be handled in the documentation. Suggested-by: Benjamin Berg Link: https://lore.kernel.org/linux-kselftest/a39af0400abedb2e9b31d84c37551cecc3eed0e1.camel@sipsolutions.net/ Signed-off-by: David Gow Reviewed-by: Sadiya Kazi --- No changes since v2: https://lore.kernel.org/linux-kselftest/20230419085426.1671703-3-davidgow@google.com/ This patch was introduced in v2. --- Documentation/dev-tools/kunit/usage.rst | 12 ++++++++++-- include/kunit/test.h | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst index 9f720f1317d3..f6d6c9a9ff54 100644 --- a/Documentation/dev-tools/kunit/usage.rst +++ b/Documentation/dev-tools/kunit/usage.rst @@ -166,7 +166,12 @@ many similar tests. In order to reduce duplication in these closely related tests, most unit testing frameworks (including KUnit) provide the concept of a *test suite*. A test suite is a collection of test cases for a unit of code with optional setup and teardown functions that run before/after the whole -suite and/or every test case. For example: +suite and/or every test case. + +.. note:: + A test case will only run if it is associated with a test suite. + +For example: .. code-block:: c @@ -196,7 +201,10 @@ after everything else. ``kunit_test_suite(example_test_suite)`` registers the test suite with the KUnit test framework. .. note:: - A test case will only run if it is associated with a test suite. + The ``exit`` and ``suite_exit`` functions will run even if ``init`` or + ``suite_init`` fail. Make sure that they can handle any inconsistent + state which may result from ``init`` or ``suite_init`` encoutering errors + or exiting early. ``kunit_test_suite(...)`` is a macro which tells the linker to put the specified test suite in a special linker section so that it can be run by KUnit diff --git a/include/kunit/test.h b/include/kunit/test.h index 57b309c6ca27..3028a1a3fcad 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -168,6 +168,9 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) * test case, similar to the notion of a *test fixture* or a *test class* * in other unit testing frameworks like JUnit or Googletest. * + * Note that @exit and @suite_exit will run even if @init or @suite_init + * fail: make sure they can handle any inconsistent state which may result. + * * Every &struct kunit_case must be associated with a kunit_suite for KUnit * to run it. */ From patchwork Fri Apr 21 04:02:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 13219457 X-Patchwork-Delegate: brendanhiggins@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 917DAC77B75 for ; Fri, 21 Apr 2023 04:03:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233905AbjDUEC7 (ORCPT ); Fri, 21 Apr 2023 00:02:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233817AbjDUECk (ORCPT ); Fri, 21 Apr 2023 00:02:40 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB58B5251 for ; Thu, 20 Apr 2023 21:02:38 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-b92309d84c1so3685875276.1 for ; Thu, 20 Apr 2023 21:02:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682049758; x=1684641758; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=EHcf3TtAxqYUDjiNAmiyf7rcYOiQRzr3HgI4M/rAcaU=; b=vhOy+/6XGUNImozjW6uXE3awKrpBeiSgUUKudpv9mylXFGdVs6orwkvboBIKJ+gUSI fYZxsGRNlFm42JgTEFcUFLD1/1BFY8psdjXBoP38wBHcHbK+wyMHxCGzN+w61d7gytkU vu9cmM7aNMUhG0WLCVGBj+xxPFj9ebAu54bdHCIxwewn0vAO1Cqhp29CDwt7vEBNgg9Q jrC7EJhVkPl+hArDZNM17DQB7/02YtPjoh48F2mhv8wHzWaUCan39/Vnac6KexCLRV0V hXowYu8GYiC8PFpOmN/hmhw7rl1DR50qb8sDWApZ6QcE+iUGe+OnLapi388bUTfWh++g r0YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682049758; x=1684641758; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EHcf3TtAxqYUDjiNAmiyf7rcYOiQRzr3HgI4M/rAcaU=; b=Ibi3Y3tfS4EGBWQlcwqcHBoO91yuj1WMs4fXU2ms93WDULBF0Tbmj2vLf0jSO8AO7g 1pWrN6J1zOIkVnbXkDt1cXAkaWYqEVYFiaUIcDoZ62Pz/aAEoxG/2eSEo8Plqm3hKb8u vjLA4ZnyxOj8vz3vF5SyZG04TlOekJ8wWm0yw7mvjUb2ub7VUBE7i1pddakvjuuQ089i oQA96LCboKl4CQp6QjNG2WOD6esv+iO1bV5pFX0wx0FX1Mqtt28FCattI5ZrYecwoTz/ LNJYHbT1O4Yc8S6CUBt53dQwdDgSant0TrylcTG7dZxsu2BTviAXOj0Yel3qHiATM0JG Kk7Q== X-Gm-Message-State: AAQBX9f6Kf976AI8r8+pV0bN4aBnGzmrvPalmlqAMfy45QmdtNO1k9ZG ZDUKSPZTjc5lJhLa8TLLTaj8XrAgSKy4iA== X-Google-Smtp-Source: AKy350Y54kiwT82AuAuLomhBMXGh6hDiOxSmVnuqZHCRGMCYguaPXyqmvOXzegJ/z1hHhEwjXIqd/8eSj0tc5g== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:690c:2b88:b0:54c:15ad:11e4 with SMTP id en8-20020a05690c2b8800b0054c15ad11e4mr897255ywb.0.1682049758080; Thu, 20 Apr 2023 21:02:38 -0700 (PDT) Date: Fri, 21 Apr 2023 12:02:18 +0800 In-Reply-To: <20230421040218.2156548-1-davidgow@google.com> Mime-Version: 1.0 References: <20230421040218.2156548-1-davidgow@google.com> X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230421040218.2156548-4-davidgow@google.com> Subject: [PATCH v3 4/4] kunit: example: Provide example exit functions From: David Gow To: Benjamin Berg , Brendan Higgins , Shuah Khan , Rae Moar , Daniel Latypov Cc: David Gow , maxime@cerno.tech, Stephen Boyd , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Sadiya Kazi Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Add an example .exit and .suite_exit function to the KUnit example suite. Given exit functions are a bit more subtle than init functions (due to running in a different kthread, and running even after tests or test init functions fail), providing an easy place to experiment with them is useful. Signed-off-by: David Gow Reviewed-by: Rae Moar --- This patch was introduced in v3. --- lib/kunit/kunit-example-test.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index cd8b7e51d02b..24315c882b31 100644 --- a/lib/kunit/kunit-example-test.c +++ b/lib/kunit/kunit-example-test.c @@ -41,6 +41,16 @@ static int example_test_init(struct kunit *test) return 0; } +/* + * This is run once after each test case, see the comment on + * example_test_suite for more information. + */ +static void example_test_exit(struct kunit *test) +{ + kunit_info(test, "cleaning up\n"); +} + + /* * This is run once before all test cases in the suite. * See the comment on example_test_suite for more information. @@ -52,6 +62,16 @@ static int example_test_init_suite(struct kunit_suite *suite) return 0; } +/* + * This is run once after all test cases in the suite. + * See the comment on example_test_suite for more information. + */ +static void example_test_exit_suite(struct kunit_suite *suite) +{ + kunit_info(suite, "exiting suite\n"); +} + + /* * This test should always be skipped. */ @@ -211,7 +231,9 @@ static struct kunit_case example_test_cases[] = { static struct kunit_suite example_test_suite = { .name = "example", .init = example_test_init, + .exit = example_test_exit, .suite_init = example_test_init_suite, + .suite_exit = example_test_exit_suite, .test_cases = example_test_cases, };