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: 91883 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3385718vqo; Tue, 9 May 2023 22:48:08 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ74K6xVQzPpYPimHR5evgZ25bcPPW0FgAz/gkE8+sjbstS2ye8TL8IKhGR7wv1IvbWPxFnp X-Received: by 2002:a05:6a21:1517:b0:f2:bb3f:3b15 with SMTP id nq23-20020a056a21151700b000f2bb3f3b15mr16652068pzb.43.1683697688443; Tue, 09 May 2023 22:48:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683697688; cv=none; d=google.com; s=arc-20160816; b=Rvk/CiRxhxg+IQ5NL7dlhmk9lpAKHXBPayg3h0n3Mn77Loa57Obp9N4UN2gI23AM4G HVPhQgQ5wO9ymzle2UZY0K5x4jS++Lcu30Yqs1E/ZKuUnWIY4QfO4JCqc+qSz3aRurCd evCdOQPgFAEFWX8JrDVYcBrgtKplpMruVmHN5MJgUwFMNwJUs1wi62e1Qsn541Vz4YG5 3QTVKnjBK9qz142xCseHltSybwhbpS5rLpS/lDYqhJkdYkA76zv8KzsGKhrnX4VEEAA/ HQVzWjcVLG+WQtAxtbiFhXKYiCztr1cNYeXBRTnmccMpj+JP0DuoThHvU4WfFvewwGAC eocw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=sgUiCgU3PluO8I1muuqpEf3Ia4SIHEPSkMyiMMz9or4=; b=Bed0z0I9/V7oJ4UKiFF1ySVp6hJuDtSTMcD4zQpvZzurxRlGFBzWA+2zRjmkx51w/P QdkesNtbjLgmavPOyhM2OlRsquuz8OmiKh98zyYZX8OQxf3siCqSTE5F/E/qREuX1rZX 5j3uKISSlYT5HT3KNMh4OwCSR4MaAiovaErLLh3Qw2vCPVp2SfbmJ8okkVeMU6HYdZEc 5a3SkhkAA2CijWfMu1EzwXpDADfuwLlgZ0kIvRbFXFuEx/E9GPfD1EZBoy3gIkYvxQ0d N/tBjUoiGFWQ6TYIjKv3dLsz16+EVsnMe12dIn2U1HUa67xjwp2gc4WwPm+hY1VOld9z tBnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=JKhkMANC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z1-20020a63b901000000b0051b554df1besi3099573pge.840.2023.05.09.22.47.55; Tue, 09 May 2023 22:48:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=JKhkMANC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235768AbjEJFil (ORCPT + 99 others); Wed, 10 May 2023 01:38:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235642AbjEJFij (ORCPT ); Wed, 10 May 2023 01:38:39 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B6C4271B for ; Tue, 9 May 2023 22:38:38 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-6439bc1e3aaso3889528b3a.2 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=PNxRbXcNbdyHeYkMm+UBN7JBlIPWHcovTiajaeyKrQGJS/C4WRwVDnypBTkjH2KrzW G+mMu73b1cvOTX23SN75DBQ91QYV9n1lUrfFag1KMHUk411thTWfOyRN6u90Ei84Lh/q ndhsA4iQlJhS0/lZaqdpQL4hsWoDWlFrxlNHkgzWHZrWBqMYt7SeoLJVEF1Mr8oStxcg OWAZOUvdMiEYIJmgRNDVoaTz86RmIlqtyCIW7rPwFt+yliMrlyYUY+W+jzC43XsrAZhl MGbwyJKI7oh9I1GOUdAPvxLsJZl0uCjC90Z4D0yIqY1e5Om+qt7TbkJlgswk3sQr+KHy W6ng== X-Gm-Message-State: AC+VfDzYzyfCJfrJhkEuAbYEWP7u43rfv4B64GQlt8uAyfFUK5/KnRwJ s4Z23e/BNeZR1CvdnSfHvEHOXafgFVUjJQ== 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 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765484987079391423?= X-GMAIL-MSGID: =?utf-8?q?1765484987079391423?= 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: 91882 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3383649vqo; Tue, 9 May 2023 22:42:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6m0Mmn8Hq+eSPXCdJyIx5544KQ+raMsAV1JVIXvHGLUWqDxsuy10YVsbXveujz5n+VyamS X-Received: by 2002:a05:6a20:4326:b0:100:3258:d163 with SMTP id h38-20020a056a20432600b001003258d163mr13597303pzk.35.1683697362764; Tue, 09 May 2023 22:42:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683697362; cv=none; d=google.com; s=arc-20160816; b=a2+wwH7C2Ku7ERHl+vEMCUQNuc371XDeWrIbY+iexM6De0VCES/WvmtEa1vPcmLot9 jAjXFl/fxLND1ACgboOFlA1YVKe6ElP/xIN6NagY2nqzuoFMuprm6MjUx0J9gQ+scp9+ riNUCi7UjH+12lzySgPvnR7NmiP9A+zaIjeHwkcfNi3nYBLG13nNvFAHvlIefUooIgbU U+k0ExJmQbl9Ku550eWH0x97osq47X/SxnEZPU9J3rkLZ8OUL0t8x+cVwo3ziC+7mNKZ wYkW1cs/Mp9Io3niJ53Vk4PuuXrewz3ewyKbwvztQj9eyrZ1Kv/uKBUgJCyRDWLohv19 fCyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=D21RSkoXuPIazsZogkwKEp2pOsmA6u55RxvLm4Pqyu0=; b=CNVXsEw2gGIZzoLTyDUyT0crA7vM49EHMEqpu3cXQZ11BG/1iCH9OpsZmWbHgqOItv UPD0hBQczkZTF3cB0i/qwv4CDt2lSKo76rTPihwMCagc+s/CKSxAYkHVB+OluznI7iBV bldbXaE/TDhs4TAdfaTkoE8YNwFbKlJPVY5W2KrllDQ8mhc8EecD+BE3/Hu2/BYj1X19 DjPwKnAHrmFK2t290UWUCtUnXeBJcN33baEpKc11qCCdbSmCIVS7FnGcaSPLdxwiVmIo K/4PnsIKwQ1rP91mJZ2SqwFmP5VoHNMqHTHrGpR/TB+wZe1U0uENZz1YP0LWMSAm0mJe Drvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=lPjEr4Y9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m6-20020a17090aab0600b0024e2175c4a8si16564470pjq.132.2023.05.09.22.42.26; Tue, 09 May 2023 22:42:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=lPjEr4Y9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235642AbjEJFiv (ORCPT + 99 others); Wed, 10 May 2023 01:38:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235791AbjEJFip (ORCPT ); Wed, 10 May 2023 01:38:45 -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 9BCDB2720 for ; Tue, 9 May 2023 22:38:43 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-ba22ced2e95so5324781276.1 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=Z/Di7P9LuyKnsmV2X1MjTdVbDQjtjuRKR7j6hebyQ6hbYVMnIbyypVUlhdLsyqrUn1 aZr2tbDWZt00FI9aAT1NfnwXAKO0aZwSZfZVxrgwNqEC8E1FeybnfCvDpVwxwZPzzA2f oSRs58Fb8oTEc5/rhW/RxNsIEll+omvO3JkenxF8SValpHbaE5sDaZC6LrRKTTtZBy/h wgRuVS68GNj0tUxh82WC2ikuyueGQliETrpV86MESqMzUXaRh42n3AttqyZJGLfVNkn2 l9gBXma3Da67cIb8y4xeVAufo+tF6u7f6z4t3c0w97zvk1rkmBmj7tbKvUO/Kti1P+Q5 qZWg== X-Gm-Message-State: AC+VfDzAWfGtE1vV/pkICgORjbXm5FyGg9h4EYIP8MCj0iBTtOeiOMZb u2PTjmcqtXejdqczsEerk2EfFVjupBF56Q== 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 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765484645791041113?= X-GMAIL-MSGID: =?utf-8?q?1765484645791041113?= 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: 91885 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3386880vqo; Tue, 9 May 2023 22:51:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5RJ6ohDOmJeMofvMIY/b94KTndWxLeHvpOn6I3DEV33Rwe9GtgDTzAwILXowxgw8ZGl+W7 X-Received: by 2002:a05:6a00:1a56:b0:646:2e83:6b2e with SMTP id h22-20020a056a001a5600b006462e836b2emr10561811pfv.31.1683697882964; Tue, 09 May 2023 22:51:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683697882; cv=none; d=google.com; s=arc-20160816; b=TfCMtDPKt3UiOazjQ/QkVXOXMtnQtgr4lHax4znTMO4o8xDtw/h9BVYUMNMD6XBmEz qgtaJZ2jQAKGpZwtiNkp4YRSCaQWd5sMg7YoM8XAVzuJT64IbYAh5Ol6vISDMJxKzk+j B3JjHa1WQR+EPJZXlQcUdTX/vAv6xCCzmBIXF+tPQ8/y00Nvr1OKg6ud4gIKZHQNuoKs vY/Ml1KqfXLggP4epWnzHolN4zIQfvfj2mErQwxycnaClz35hF8SK+k8oFIRXbkEEAuG T6adr9qlAmulEdmHU3d0B+gOa0tYepwKbO9G5rWKuSGQNQ/TnQcRQxXBbuht8OHpp89x Sn6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=Tbe7DEKzQKrcYiGl9gwDjN/HIv29nyKqeYl67bnl2t4=; b=XAD5DfU08MX+2qxqs677yoGGPD/CVmCTEcJGvfKon7KnM+pW3c4x3E9yJKg8JGC2Ow uR+1cctazBz+pI/Ph8MwogG/qBmyFD4poEhIpObuc+PjAI89f/Hegtr896bIq1Ha8W93 mGNTEY0IUksImMKGWHh8jy0BZVRaXtDHnd8eY3u2ZR69LC+ypC8fLuAiK1l3gJSyFVQk tZ/Z/1GSCQu2rJle9G+0UBybl7Zt7zlIj5Ef3g+52avFFaFMcoKrr9SI1Rb44zNKSaST iJObAFZOk8sG2fvGwiPZAJcQLtDQ2j3O6QxJkiOSssOC84HWsFlfzzBCAwSbSko0/Ras sZ1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Batqp3PB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b27-20020a63931b000000b0051f15bd7215si3258843pge.342.2023.05.09.22.51.10; Tue, 09 May 2023 22:51:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Batqp3PB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235830AbjEJFix (ORCPT + 99 others); Wed, 10 May 2023 01:38:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235908AbjEJFit (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 151A82D46 for ; Tue, 9 May 2023 22:38:48 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-b9a829a3de0so15392616276.2 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=Xj5L/G6iy4ByPopg9NJmkxiW6Kyj4apb9Tl4khn5Eb6FgD8uijFjUMHY+iEvNGY+ma gQC3mKiWZxJW1sKnDcABPdDjfp8Yt4n4A4bcdgpB8WedynpD/zrBbLgaM/N4/jQK243T MSn0m8T+bma1C3dSUBt3fjlQeFvaOOhczqPSsAZpzNyXiVGWBTuMX0mQoINQ0otJw2kp +rF8qJ+fbyNApuf4ApIiqgzgeYKENOdqpDWKX2p7KoGVRbmJGoa2i7dgJvbHgaaPv5d+ qK/wgpiF4pb3QGDtkd2m0chGlF2xAPoUeqWVSGKXIA98/mK0XrddzgS4CpcyzkHz3e5j 1FVA== X-Gm-Message-State: AC+VfDyQQDy4RjIUpOYqkSktrvCSdFGw8pa80HlTlQA25SyMBhKL/mT/ xaA08NOyEuLwUIcd9xKVHoeNM+xuFUGkbA== 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 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765485191328412830?= X-GMAIL-MSGID: =?utf-8?q?1765485191328412830?= 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: 91884 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3385839vqo; Tue, 9 May 2023 22:48:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6KWuToV6ChPnF8covv2ik42Ui+UL/QHYQIIQO2VpUgDL6Jwp6tQwMUJRLPWAeDBqg7fNRl X-Received: by 2002:a05:6a00:14ce:b0:640:f313:efba with SMTP id w14-20020a056a0014ce00b00640f313efbamr22513608pfu.19.1683697703471; Tue, 09 May 2023 22:48:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683697703; cv=none; d=google.com; s=arc-20160816; b=ibnE/FUyl+FnW1pMmMjH966uqusQwWmdfXrm976fuE4z3BEiM/vKxSiu+pJY8vajQU OLvnqOJJ4ybipE+3Mr175eBiW5IdxZjC/MV5EJ+8WSqqFbKxBMjei+/TeSCAwv9ZsU0y FYW0eEzwwH1yF2AuNqNeS7H2ZovZZJArmcpfJNWhxOIxKesaFje2cnDp268RASpY5m2Y Ye2n6h2TeHSUMMBkUZCq8WTFZGAAWGOmM1xX+8dhIHh3CG6LSBVesrqul9qnpeQ9YoIO +ReN/+ib/02Jn6qCFMOKtqBmFq4THkqab3uluze9fE8OsQhWns2HMDb3qtdZauySCE2K T5DA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=Mfo42Q0Y+93PbUz1Yll6pAE36cAitDEFg/Zj034KFHY=; b=N4YgDpi8E6ms7BtwQy9yzypetvcEAkrmqCBXNsiAS/AFZw4ByiGV5+Tm5jc3qGaj83 W60f7U9lLwWHYHc+KPEnF9wyciu8KqHA/06dBNmPQU9QMQoI3m7iaEuzx/27O2pkR0+7 1i3PRx8gMiZ0IT7ug2SpomCNRUFCm848BXoKCLUu3ZFpRjhjaqixn0Y8xEfbeClN6mdF VfrTEbsbm2BDHC99+NgBYO4FG9Yd9AHT5+t7mf8sGeIh9GirNr9Ry0FewlwT4y2llIiX wnBQ+C7Wj+mZxHJrfV5KAph+o7Em0JttUPOTVqJ7wGUMdgepZZC8hrOKE8Rr5nkEJy7N Y+fA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=rmd4TKqn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b27-20020a63931b000000b0051f15bd7215si3258843pge.342.2023.05.09.22.48.10; Tue, 09 May 2023 22:48:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=rmd4TKqn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235962AbjEJFjB (ORCPT + 99 others); Wed, 10 May 2023 01:39:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235935AbjEJFi5 (ORCPT ); Wed, 10 May 2023 01:38:57 -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 CD3CA2D43 for ; Tue, 9 May 2023 22:38:52 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-559fb5bed89so75465127b3.0 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=BZPWmdI0/9cHGTC6YqTY19NxcPZ8G8AT5I5MtRGOy2P5aMb4NaKCDJ7Uuy+NwVVob0 vZk+l3iC6DAHNmn+XOQP9kA2Bjri8WQ5RnqdDOIFGX5gTlC4XBntP2qIrzSkGYUF0ELL u10j7YpgrNGb4/5TEroWBotc/dRCrbyuazl1ncTIC6iVQX1gS0YEqaI9S48YcEh1L1cd T5YeAdKmsAXdnmokXgaFS6Qye6dz0xYQ/d64saEpRplcYMdFI5WnV727AnBVG50KEMdn kFK+9KZNXsZ7T1C0yV051vHRzljDSNAWc1jJpMwUoPnPIKmd7WSbJJTZRx2xadTCtecz hLWA== X-Gm-Message-State: AC+VfDz8vbn7buldpjCShcTFMybHpUoQvZ8TC0YXLXpIyCTVnfK2/V1U Q3NtLm12oTU7ycTulky5Nqxfqs7edMwNfQ== 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 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765485003398573811?= X-GMAIL-MSGID: =?utf-8?q?1765485003398573811?= 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, };