From patchwork Wed May 10 05:38:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 680777 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 6789CC77B7C for ; Wed, 10 May 2023 05:38:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229482AbjEJFik (ORCPT ); Wed, 10 May 2023 01:38:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229500AbjEJFij (ORCPT ); Wed, 10 May 2023 01:38:39 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C1A3273D for ; Tue, 9 May 2023 22:38:38 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-643a9203dc2so3509269b3a.1 for ; Tue, 09 May 2023 22:38:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683697118; x=1686289118; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=sgUiCgU3PluO8I1muuqpEf3Ia4SIHEPSkMyiMMz9or4=; b=JKhkMANCdve8/KBZ4tWsLrb35Ucn/9dbb9jOBv/cRu6xQ/8H3lm0z/6DMAVTLAIzaw wkNm5xjm+/pjNc1SnszT3K4+5jNChom8yxYBZwbbIixJvKEh+sgePhKMCaTzfHtUeUaT gvod4z/BVz2J2+2WuGUi+SU7x1j0FYs7nO0M6QiVJH68DVPP5I9TGulaAFihJjXjnYnM oq9j5k/J4FDucGgQU4ZJQRRxegTkCqM/DJ1hK+Yl2Sbt6MSyxfoZjQ0TD/okUUhNfqx/ rH6mAiZWWIXzFtImVi9wK8jQkVNqZ6dqYr3l9sVgJNGaqmXd2QXSh/EYgzNqiWHDloGY yrBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683697118; x=1686289118; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=sgUiCgU3PluO8I1muuqpEf3Ia4SIHEPSkMyiMMz9or4=; b=ObJGBNm4o9Erau2Jxn+rZNakSjyCcwf7hkhT7OyIM3UWm5tBSFI6MergPHsqON22Z5 Lx8SVToP/IJAVYuD15YDbV4PQCQzWhZbRGGj5nmV6MAGJjCbtGGbxKw/NdfqKbptQ+YU nc6uBG3jDKcvUhMWiZYa0FRVcB4x114QgEoDiMiHAEewDrjCy+0EHycunbMrGYOgB05D XLoHjt8fkNsz6sdWDhpXUJJxJLQCKJMQrLA5HFsi3VjD3FShLnWSE1OlJDYn+8hdKByQ 6nZmpRyHwwk+vzgOPHdp/a9sWEaABzhdqIisjCGquY87KABoqnE/Rz8sO7mVm5g5CR+S /51g== X-Gm-Message-State: AC+VfDx+eQsJDWVWpsuhSesSXqciqUYQpIa2v1BcsmEyfU55q457ygR/ izEaniBy96DH2zAKdJHEZ/6MhdW9UNtIOA== X-Google-Smtp-Source: ACHHUZ7vHZ8F8rpi4vKX8dfvMN6thxrKRmRi02Tukl1XOtNa2jsns36iWDO8yi0vzcNoxWchQEjeYLQNwfQc0w== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:6a00:d5a:b0:643:536b:e764 with SMTP id n26-20020a056a000d5a00b00643536be764mr4660893pfv.4.1683697118051; Tue, 09 May 2023 22:38:38 -0700 (PDT) Date: Wed, 10 May 2023 13:38:29 +0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8-goog Message-ID: <20230510053832.438529-1-davidgow@google.com> Subject: [PATCH v4 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 , Benjamin Berg 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. Reviewed-by: Benjamin Berg Reviewed-by: Maxime Ripard Tested-by: Maxime Ripard Signed-off-by: David Gow --- This is an updated version of / replacement for "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 v3: https://lore.kernel.org/all/20230421040218.2156548-1-davidgow@google.com/ - Get rid of a unused 'suite' variable (kernel test robot) - Add Benjamin and Maxime's Reviewed-by tags. 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 | 56 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/lib/kunit/test.c b/lib/kunit/test.c index e2910b261112..f5e4ceffd282 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -419,15 +419,54 @@ 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; struct kunit *test = ctx->test; - struct kunit_suite *suite = ctx->suite; int try_exit_code = kunit_try_catch_get_result(&test->try_catch); if (try_exit_code) { @@ -448,12 +487,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 +511,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 Wed May 10 05:38:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 681510 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 60F98C77B7C for ; Wed, 10 May 2023 05:38:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235925AbjEJFiu (ORCPT ); Wed, 10 May 2023 01:38:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235798AbjEJFiq (ORCPT ); Wed, 10 May 2023 01:38:46 -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 9BD40273E for ; Tue, 9 May 2023 22:38:43 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-b9a7c58ec19so8255499276.2 for ; Tue, 09 May 2023 22:38:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683697123; x=1686289123; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=D21RSkoXuPIazsZogkwKEp2pOsmA6u55RxvLm4Pqyu0=; b=lPjEr4Y91YjA1DNPtgQa83pK7PbcyEl0pyuhZLNr+D/cRXObIpRBPlUEnuSxJD0dat cc9uF5IP+1FSE0JWzIbCDVH5NaX+zg1uIVxcUJ/OmfnkEBJ9ysUbl6tbjvwDi7BY/1HH le11hW04bhEyKovFflUg+lE3nLrPr4onNp7uVYyuvjtqtsZEICQ4i1FAk+Mv2wh5EAqo TxHhUuRFI/GRs+iIpWx/0jB5sbtHiiCZjO1575V3AN+lIxzVgz4qBoFa3qGGH+K9CrXq 1TQp+VfVULgwLO4EPsVnPAasLSHyQ8EcYiA8UTj/qM8M3bV+4xCuc0CeWygjj9EyT/UT Cmrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683697123; x=1686289123; 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=D21RSkoXuPIazsZogkwKEp2pOsmA6u55RxvLm4Pqyu0=; b=BthHHOafhOxY/hnsSNMIQJlDY62ZdPfmYlGo3RLbFWkYcd7n1iIpi4i1OKGeh30Qin qKKauSERauqdMJQZrSTvadEYfyY8Th3kxUTWRbc1ppNmxZt6LnIRNOWiQj9Ga1vmeurf 6Ca2FQLaFm87rB1YxX+pLwR5Fq+7zOstwfHWene9zXBXHIDOnK/ZwALhtvMWjZOcx4tj omb8p5iyuubWm/uXnoMIXqdKkZAjmQe5kGH2AQiKSciiJRkS6G/hRDSgICpJCUsR12SU k0Cf0I+qamkfEXtBIJv9p0U1iG4NoRSDePBT9Qewx7QTLITuSTPbun6cGer5rNcAzwDW V2uA== X-Gm-Message-State: AC+VfDwh040OFlTW80JVclHraOAoLtX1kRFTNvWC+gQ883KMEhuJ6lKG bH9KkXXpAb4/cyGiL4YmU8BMjCI7ETDscA== X-Google-Smtp-Source: ACHHUZ5prhcsa2uKfRyYa70NkTmue+tLexFwGpYvoi5fvKb9TeMGfaJMHMNK6fm4cHpohQ01ySzPxykDD0BHfg== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:d6d0:0:b0:b8f:35c1:e63e with SMTP id n199-20020a25d6d0000000b00b8f35c1e63emr10524538ybg.6.1683697122908; Tue, 09 May 2023 22:38:42 -0700 (PDT) Date: Wed, 10 May 2023 13:38:30 +0800 In-Reply-To: <20230510053832.438529-1-davidgow@google.com> Mime-Version: 1.0 References: <20230510053832.438529-1-davidgow@google.com> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8-goog Message-ID: <20230510053832.438529-2-davidgow@google.com> Subject: [PATCH v4 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. Reviewed-by: Sadiya Kazi Signed-off-by: David Gow --- Changes since v3: https://lore.kernel.org/linux-kselftest/20230421040218.2156548-2-davidgow@google.com/ - Add Sadiya's Reviewed-by tag. 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 Wed May 10 05:38:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 680776 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 4A48FC7EE24 for ; Wed, 10 May 2023 05:38:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235798AbjEJFiw (ORCPT ); Wed, 10 May 2023 01:38:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235913AbjEJFit (ORCPT ); Wed, 10 May 2023 01:38:49 -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 152182D4C for ; Tue, 9 May 2023 22:38:48 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-b9a776a5eb2so15399865276.0 for ; Tue, 09 May 2023 22:38:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683697127; x=1686289127; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Tbe7DEKzQKrcYiGl9gwDjN/HIv29nyKqeYl67bnl2t4=; b=Batqp3PBLuZq0MPoOLnpmGf8eMXnRJVw+ue5JhqhX/o6ZwSSRmvX46Z18pKW1fSnVT XQht0bAdfLIFZw8Hx4Fuun2NsahncYeYTqhpD682BVs2fhfoWEX3gHn2urK3BQ6UtqTq 4TGJ6cEVC4M4lRN7j8OJeMGaeQzXedvUkKxBxeyzM2ToJEnITg1mfP+jaFpPq7RnuVqo D54HXfiPXWQPqJEOcwOOTyTPq+zBacAXSP6Xa1H5hvzht/x1gfNG81/OEx3iDgHVwElE rOCw0Y6I/kDp7CeZc0pOcoeFTPA3MajrXOl04uOeVvkOC2bjcLO7gSpks1eJbCm23ebo nHpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683697127; x=1686289127; 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=Tbe7DEKzQKrcYiGl9gwDjN/HIv29nyKqeYl67bnl2t4=; b=k6eIEMeVazuiVU6ZpSeBrWvexGQAtwof1hlv9/lMPQsbbgkQnpcRm+dYVkLxqJu+cn OGpP1wWVPI9OjqfpV2gFJ/gq8PZ+btb+OtzQSlgysmyJoCNCOzoEpabPmZtP47SBYTJL uroYZgFP7NSG+BD7H9HCdVgKUbHM2qqgqdxBzlm+lmnR/cQGBQbeB7O5ELoucH4cY4Ev IcnDYeUmgNdAz3EiN90bB1xw+gAk0xACY/BNwPNenmCVisYqi9a5xG56xyoTSGuBKouu /dCHmZqc1Hp+3AwU8aeqZOU193E0Mv7P3hyKH0GrGqXbRO9AxYLa7RuKbx9eJzSCGrmS 727Q== X-Gm-Message-State: AC+VfDwGZ6xoj47QPKKeu/bxcliEEQPOS9/Nxo0C+aZqDHMqbcSx2pjH Clggfz+/a4j7cuFNjj/0dSYOpmXBo7T9Mw== X-Google-Smtp-Source: ACHHUZ4pNuWHJlqRy36nSA4eoVUW4Wj/ws86Z3o7XvuctcNiiBD8+Xw3sqaiKFRGnD56tbXBoApPrswps0casw== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:d8cd:0:b0:b9a:703d:e650 with SMTP id p196-20020a25d8cd000000b00b9a703de650mr7478834ybg.7.1683697127356; Tue, 09 May 2023 22:38:47 -0700 (PDT) Date: Wed, 10 May 2023 13:38:31 +0800 In-Reply-To: <20230510053832.438529-1-davidgow@google.com> Mime-Version: 1.0 References: <20230510053832.438529-1-davidgow@google.com> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8-goog Message-ID: <20230510053832.438529-3-davidgow@google.com> Subject: [PATCH v4 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/ Reviewed-by: Sadiya Kazi Signed-off-by: David Gow --- Changes since v3: https://lore.kernel.org/linux-kselftest/20230421040218.2156548-3-davidgow@google.com/ - Fix a spelling error (thanks, Sadiya) - Add Reviewed-by tag. 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..46957d1cbcbb 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`` encountering 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 Wed May 10 05:38:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 681509 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 1A3F7C77B7C for ; Wed, 10 May 2023 05:39:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235902AbjEJFi7 (ORCPT ); Wed, 10 May 2023 01:38:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235908AbjEJFi5 (ORCPT ); Wed, 10 May 2023 01:38:57 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD5A830F9 for ; Tue, 9 May 2023 22:38:52 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-559e281c5dfso75247017b3.3 for ; Tue, 09 May 2023 22:38:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683697132; x=1686289132; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Mfo42Q0Y+93PbUz1Yll6pAE36cAitDEFg/Zj034KFHY=; b=rmd4TKqnOZTd1yPShThAvmZvlcVmX5R+MgeqNKnU3hjsvhOeXoQWw53L4vlBu24w2O uJeQvXfNnt98wUYtlcyRgAKitZINh5JyjVOuwF2GxPSJXqIIV43P6XuSWOqjS5pmNHR6 VWUzt2NU3BYqGgVPQoBsH9Fk3x8n7j9XYXs5WxGPAabxedPFurLRhNz++N8msmsf+MO5 9isTNnVN+Qp9/1ik9kKp3mLdQlDkvGApwVj8S645AWqwdb2rgbtLQBp1C5Bv27LIO3IT zG2Dvkum7WvbQ5CKzfSr4xngncd/YTFZzlcD7oU6Cp1VOBzOSnXoEL7Olo8pNbumiaym Bv0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683697132; x=1686289132; 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=Mfo42Q0Y+93PbUz1Yll6pAE36cAitDEFg/Zj034KFHY=; b=X7sHRcOkuEe/7N24FMALZor/psy7cY7mWyjcP+SEUxQJb6EkL+6IB7I4TirO4EZekp D6hRe877lLRSvoznT+wzdX7r61lJLKIwoMIA70DXNutiV71v6rXVkTGC8nFpmDIuJ02L FNsLaapnysD7kY0yNVSW8p150J4GkFQ5Jx+BMlh3PTS9IQ2TotW9Vpxdx6f9GhAaRNSf blUWpYmblC5jFwjCWsfR44wQMbZg7AqNL+yPcYxbbKMNQu0x0HQz2BKeSetIWr2xE7xw jrGIh+FBYePBbir2o3hgJ97ewsaO4xsRqtpk91swxbQ/LDL4HKb83bRsuNbrj3uLuv35 boDQ== X-Gm-Message-State: AC+VfDwa3U8I6NRVZwZFj58pZvhrYZi5tUr7QE2LdBmHsQ4i1DBtKpax X/Ej+6y4+CLnI3I4HZgR/Y22VumCNnViyA== X-Google-Smtp-Source: ACHHUZ4e/K50fW2t0YQaNJVoN4iOC0HO0V4/JFVNf7CQLXQsKDEhj8MTD0BwKOn6kf9Vf6zoEUsUiv90+jKT8g== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:c903:0:b0:556:cacc:1f1d with SMTP id o3-20020a81c903000000b00556cacc1f1dmr10154166ywi.8.1683697132036; Tue, 09 May 2023 22:38:52 -0700 (PDT) Date: Wed, 10 May 2023 13:38:32 +0800 In-Reply-To: <20230510053832.438529-1-davidgow@google.com> Mime-Version: 1.0 References: <20230510053832.438529-1-davidgow@google.com> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8-goog Message-ID: <20230510053832.438529-4-davidgow@google.com> Subject: [PATCH v4 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. Reviewed-by: Rae Moar Signed-off-by: David Gow --- Changes since v3: https://lore.kernel.org/linux-kselftest/20230421040218.2156548-4-davidgow@google.com/ - Rebased on top of latest kselftest/kunit branch. - Added Reviewed-by tag. 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, };