From patchwork Tue Apr 26 18:19:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 566313 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 CAF91C433FE for ; Tue, 26 Apr 2022 18:19:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240536AbiDZSWv (ORCPT ); Tue, 26 Apr 2022 14:22:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229730AbiDZSWu (ORCPT ); Tue, 26 Apr 2022 14:22:50 -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 402A5197F91 for ; Tue, 26 Apr 2022 11:19:42 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id v17-20020a056902029100b006484d85132eso6658556ybh.14 for ; Tue, 26 Apr 2022 11:19:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=ISf/anLTGG8aDfdYm3HlRvwPzm+lbJ5gZK255uLU0N0=; b=Ur5xXORcPM2CZ439vOEwCulasH931zg9WytiwjntMXBrnbbzDY69LUuWucG4wHMCwE 4jr8lL1Bs3Ws8X3XRy8waN0e6K9qT52c02h+Rk2QaKhygzEYSgqzAOh+/CojekNAC+qD dFMSkG+mo3EoNlQ5vB5RGNUw0AdibLl8uXveQ+GKNfxn3tJ0qF6sn+ea+m0WSCIyEWxM ZkG/aK/tiH5824o7GHgYNVyJOhrvDWJ6mA+Z1Us5Nug/Hbcvri0g8eR0a0UQ0hiX8c8F AKeK0n19dC58zNBVaMyNpozByExipLG9wdOnf07v2TaC7wiTiErttyUvdbbz3jpuddED pJXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=ISf/anLTGG8aDfdYm3HlRvwPzm+lbJ5gZK255uLU0N0=; b=oeJEMyPFytMwtTnbL8YnEOVCMEhX45E382/1wgyJR6EDATIHh1nsvjZpa1E+PQR1SW FB5cLtyY/W7WiRTBNx/ie4JLBGktJHn4RP07kDHZK80zTNB+ywjp04hIEu3fH3VkBWZa L244DlVOVopRYfmTogXzbmjzJfucF09t1uWTsNSAw4bRvIO0cXp6oIFWTmO4dqzvOcAk W79cRl8wiq+r8nP7g8CKPDbnKm+0Yjp/XLB30XwoLOwYesolM/vAiH6auCOZHnTaggY8 U1JHk7NfAwlmdqQ/h5sQ0GtvX+IzwVJiAOQRUfMVY3m7DXEJeRi/WUygBUfpT2J4RJKh s9JA== X-Gm-Message-State: AOAM532bkqLazasfGUctP0W7MD5k4yfhp4+kDHIfUd+8spemLsF6NDwo uazccLgNXl5tWhYCNmGiAUnYzN53Q5UurA== X-Google-Smtp-Source: ABdhPJwPXVW3jrSsfOmBbLULPEFpZ1wlX5n1zj8BpLWvhRxzippBSvFduoToUHwDEkVuZv2mBhYmhuduAUThkA== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:b03d:8d64:4a06:2c5f]) (user=dlatypov job=sendgmr) by 2002:a25:2487:0:b0:645:7751:ba68 with SMTP id k129-20020a252487000000b006457751ba68mr22631565ybk.333.1650997181489; Tue, 26 Apr 2022 11:19:41 -0700 (PDT) Date: Tue, 26 Apr 2022 11:19:22 -0700 Message-Id: <20220426181925.3940286-1-dlatypov@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH 1/3] kunit: rename print_subtest_{start,end} for clarity (s/subtest/suite) From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org These names sound more general than they are. The _end() function increments a `static int kunit_suite_counter`, so it can only safely be called on suites, aka top-level subtests. It would need to have a separate counter for each level of subtest to be generic enough. So rename it to make it clear it's only appropriate for suites. Signed-off-by: Daniel Latypov Reviewed-by: David Gow --- lib/kunit/test.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) base-commit: 59729170afcd4900e08997a482467ffda8d88c7f diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 0f66c13d126e..64ee6a9d8003 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -134,7 +134,7 @@ size_t kunit_suite_num_test_cases(struct kunit_suite *suite) } EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases); -static void kunit_print_subtest_start(struct kunit_suite *suite) +static void kunit_print_suite_start(struct kunit_suite *suite) { kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s", suite->name); @@ -192,7 +192,7 @@ EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded); static size_t kunit_suite_counter = 1; -static void kunit_print_subtest_end(struct kunit_suite *suite) +static void kunit_print_suite_end(struct kunit_suite *suite) { kunit_print_ok_not_ok((void *)suite, false, kunit_suite_has_succeeded(suite), @@ -498,7 +498,7 @@ int kunit_run_tests(struct kunit_suite *suite) struct kunit_result_stats suite_stats = { 0 }; struct kunit_result_stats total_stats = { 0 }; - kunit_print_subtest_start(suite); + kunit_print_suite_start(suite); kunit_suite_for_each_test_case(suite, test_case) { struct kunit test = { .param_value = NULL, .param_index = 0 }; @@ -552,7 +552,7 @@ int kunit_run_tests(struct kunit_suite *suite) } kunit_print_suite_stats(suite, suite_stats, total_stats); - kunit_print_subtest_end(suite); + kunit_print_suite_end(suite); return 0; } From patchwork Tue Apr 26 18:19:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 566913 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 E66B2C4332F for ; Tue, 26 Apr 2022 18:19:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351107AbiDZSWx (ORCPT ); Tue, 26 Apr 2022 14:22:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351047AbiDZSWw (ORCPT ); Tue, 26 Apr 2022 14:22:52 -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 8A5A0197F91 for ; Tue, 26 Apr 2022 11:19:44 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2f4dee8688cso123459617b3.16 for ; Tue, 26 Apr 2022 11:19:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=1CXtLzSBvL82PZQ0YMp7opVTudfIiN/XhPTfSqGMA78=; b=BhwU0x6S09OF+VKAPRqv0YjM2Eh9AM2nIbZQJFTPuOriIqgw0vS1dh33NIN2DALean 2PeUKc7cspbxmJOPDKhoFfQVMKM9ULKowZsiMT+tf+JHOVROLBK18fLJTu8riwm/MP3Z 9Wn4fyCsT6Lk+4UQIBqjtfEeIguxBKjHOVW6bjg87UrvoOvfCg494WiqPg61VLb5sz3N l7X1MzQV7Sk1ZejqgDXTgcK5ZJMDBkz2P4eHfM6QTgpFNN7Z8+LmjVtoAiNfA7rj+dru ZYGqP5CxAyUoj+Ncnw3hRTnpSixxp3wV21h4oeaCpzcUEv2SKwLUI0UE8h+S6EGsnDlI PmEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=1CXtLzSBvL82PZQ0YMp7opVTudfIiN/XhPTfSqGMA78=; b=5NnmLLd0ARWl0s4GysXM450EmRsqYkYsx/UZpUQi6lfSbkT2Lx1UKPeuZ9MivRW7Tt vcEuFi5VGGt1232fr6sSJciL4gYdCfylc/6ovjYoaYGzsiwTNhNzS5m57hw4AHB2wuBY 1aiKhS4azG8eAOtJdU4aiJBiM9aph379p9z6T5szljoqvEbn0J18h81Pv/GTtOngVngf BaNEHIL3keL+Ztg7AkejzAUBDFvWBlxPb8YrUoOyZJDq1Jd6+HbkB3s2hg1ImxOxRfTv RmO5Pv/YXeZXUtL58JJAXHwxBQ5q4KFK1fFdHUvpNgLLEv/LLOwHjBTFEWZOONVub832 ndig== X-Gm-Message-State: AOAM5317aCXDKKs4UWBd84etPyuWOXs6GU16Nk5wvQwjILI1PaGAAqK8 HlKlc7hjp5pZwraZuSmVZCy9C7g95zahdA== X-Google-Smtp-Source: ABdhPJz5BpVmfi7Mn+azLxfv6jrw+X2+SLKzTFhGUrBCbdTwciRajV7s+Lkosi24FSduY+7LULfuUHBCbcEA5A== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:b03d:8d64:4a06:2c5f]) (user=dlatypov job=sendgmr) by 2002:a25:e606:0:b0:648:402e:598a with SMTP id d6-20020a25e606000000b00648402e598amr14246558ybh.170.1650997183848; Tue, 26 Apr 2022 11:19:43 -0700 (PDT) Date: Tue, 26 Apr 2022 11:19:23 -0700 In-Reply-To: <20220426181925.3940286-1-dlatypov@google.com> Message-Id: <20220426181925.3940286-2-dlatypov@google.com> Mime-Version: 1.0 References: <20220426181925.3940286-1-dlatypov@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH 2/3] kunit: add ability to specify suite-level init and exit functions From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org KUnit has support for setup/cleanup logic for each test case in a suite. But it lacks the ability to specify setup/cleanup for the entire suite itself. This can be used to do setup that is too expensive or cumbersome to do for each test. Or it can be used to do simpler things like log debug information after the suite completes. It's a fairly common feature, so the lack of it is noticeable. Some examples in other frameworks and languages: * https://docs.python.org/3/library/unittest.html#setupclass-and-teardownclass * https://google.github.io/googletest/reference/testing.html#Test::SetUpTestSuite Meta: This is very similar to this patch here: https://lore.kernel.org/linux-kselftest/20210805043503.20252-3-bvanassche@acm.org/ The changes from that patch: * pass in `struct kunit *` so users can do stuff like `kunit_info(suite, "debug message")` * makes sure the init failure is bubbled up as a failure * updates kunit-example-test.c to use a suite init * Updates kunit/usage.rst to mention the new support * some minor cosmetic things * use `suite_{init,exit}` instead of `{init/exit}_suite` * make suite init error message more consistent w/ test init * etc. Signed-off-by: Daniel Latypov Reviewed-by: David Gow --- Documentation/dev-tools/kunit/usage.rst | 19 +++++++++++-------- include/kunit/test.h | 4 ++++ lib/kunit/kunit-example-test.c | 14 ++++++++++++++ lib/kunit/test.c | 23 ++++++++++++++++++++--- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst index 1c83e7d60a8a..d62a04255c2e 100644 --- a/Documentation/dev-tools/kunit/usage.rst +++ b/Documentation/dev-tools/kunit/usage.rst @@ -125,8 +125,8 @@ We need many test cases covering all the unit's behaviors. It is common to have 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 a setup function that gets invoked before every test case and then a tear -down function that gets invoked after every test case completes. For example: +with optional setup and teardown functions that run before/after the whole +suite and/or every test case. For example: .. code-block:: c @@ -141,16 +141,19 @@ down function that gets invoked after every test case completes. For example: .name = "example", .init = example_test_init, .exit = example_test_exit, + .suite_init = example_suite_init, + .suite_exit = example_suite_exit, .test_cases = example_test_cases, }; kunit_test_suite(example_test_suite); -In the above example, the test suite ``example_test_suite`` would run the test -cases ``example_test_foo``, ``example_test_bar``, and ``example_test_baz``. Each -would have ``example_test_init`` called immediately before it and -``example_test_exit`` called immediately after it. -``kunit_test_suite(example_test_suite)`` registers the test suite with the -KUnit test framework. +In the above example, the test suite ``example_test_suite`` would first run +``example_suite_init``, then run the test cases ``example_test_foo``, +``example_test_bar``, and ``example_test_baz``. Each would have +``example_test_init`` called immediately before it and ``example_test_exit`` +called immediately after it. Finally, ``example_suite_exit`` would be called +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. diff --git a/include/kunit/test.h b/include/kunit/test.h index 97cd76461886..5d288f3d8f68 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -153,6 +153,8 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) * struct kunit_suite - describes a related collection of &struct kunit_case * * @name: the name of the test. Purely informational. + * @suite_init: called once per test suite before the test cases. + * @suite_exit: called once per test suite after all test cases. * @init: called before every test case. * @exit: called after every test case. * @test_cases: a null terminated array of test cases. @@ -167,6 +169,8 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) */ struct kunit_suite { const char name[256]; + int (*suite_init)(struct kunit_suite *suite); + void (*suite_exit)(struct kunit_suite *suite); int (*init)(struct kunit *test); void (*exit)(struct kunit *test); struct kunit_case *test_cases; diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index 91b1df7f59ed..f8fe582c9e36 100644 --- a/lib/kunit/kunit-example-test.c +++ b/lib/kunit/kunit-example-test.c @@ -40,6 +40,17 @@ static int example_test_init(struct kunit *test) return 0; } +/* + * This is run once before all test cases in the suite. + * See the comment on example_test_suite for more information. + */ +static int example_test_init_suite(struct kunit_suite *suite) +{ + kunit_info(suite, "initializing suite\n"); + + return 0; +} + /* * This test should always be skipped. */ @@ -142,17 +153,20 @@ static struct kunit_case example_test_cases[] = { * 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.suite_init(suite); * suite.init(test); * suite.test_case[0](test); * suite.exit(test); * suite.init(test); * suite.test_case[1](test); * suite.exit(test); + * suite.suite_exit(suite); * ...; */ static struct kunit_suite example_test_suite = { .name = "example", .init = example_test_init, + .suite_init = example_test_init_suite, .test_cases = example_test_cases, }; diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 64ee6a9d8003..b66e395c795a 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -192,10 +192,13 @@ EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded); static size_t kunit_suite_counter = 1; -static void kunit_print_suite_end(struct kunit_suite *suite) +static void kunit_print_suite_end(struct kunit_suite *suite, int init_err) { + enum kunit_status status = + init_err ? KUNIT_FAILURE : kunit_suite_has_succeeded(suite); + kunit_print_ok_not_ok((void *)suite, false, - kunit_suite_has_succeeded(suite), + status, kunit_suite_counter++, suite->name, suite->status_comment); @@ -497,6 +500,16 @@ int kunit_run_tests(struct kunit_suite *suite) struct kunit_case *test_case; struct kunit_result_stats suite_stats = { 0 }; struct kunit_result_stats total_stats = { 0 }; + int suite_init_err = 0; + + if (suite->suite_init) { + suite_init_err = suite->suite_init(suite); + if (suite_init_err != 0) { + kunit_err(suite, KUNIT_SUBTEST_INDENT + "# failed to initialize (%d)", suite_init_err); + goto suite_end; + } + } kunit_print_suite_start(suite); @@ -551,8 +564,12 @@ int kunit_run_tests(struct kunit_suite *suite) kunit_accumulate_stats(&total_stats, param_stats); } + if (suite->suite_exit) + suite->suite_exit(suite); + kunit_print_suite_stats(suite, suite_stats, total_stats); - kunit_print_suite_end(suite); +suite_end: + kunit_print_suite_end(suite, suite_init_err); return 0; } From patchwork Tue Apr 26 18:19:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 566312 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 472D1C433EF for ; Tue, 26 Apr 2022 18:19:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351129AbiDZSXB (ORCPT ); Tue, 26 Apr 2022 14:23:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351123AbiDZSWz (ORCPT ); Tue, 26 Apr 2022 14:22:55 -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 F3558197F91 for ; Tue, 26 Apr 2022 11:19:46 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2f7c5767f0fso84244967b3.4 for ; Tue, 26 Apr 2022 11:19:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=pmGguFEnVQZXUniO0qK+xxYCdVnApJ0Srb7572vJ7Dg=; b=pybsRBOiqiYAlzMXp0jHWN76469Ajig+vJfI3Nb9HvDU4KSuZlTuibVNq4m+bInuf5 txLVT4AZgjzSz9vjzUpguxII80F3NWSy7QmgQmXf2tU2YFxEbHMqkXdopTdqrpgFF4B8 xwyroLevaF1PIWB3Vuhst+GL3jimQFLupSWznScgRLW+xrtw/thXLT2AQbB3ydjAwJO6 nDELK2r3MqdMQJsACCsM+piK8NmEAMW7vVK0IAnVuS5jH49NMH4UfrG7exmMHFJTPGPQ ge1AciuENN8c+CtIKWruy3VmtqoPTv0C9aWq2+n9qX5W2oQ1iQFU6pXkqlWcnxqvWO3F n00A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=pmGguFEnVQZXUniO0qK+xxYCdVnApJ0Srb7572vJ7Dg=; b=pCVZ2He/ayVb52kvKhBTcIe5hx61TMREvm+a/TqdUVaGsT72IA2aVwUuOjZiLR8FmN 0QrFbEzbvmyWwcG4tcIV3+pnrvwgOBeI/yDOg7ohZBxo6k+BiTIJyS0KNRsUlO+W3Me7 cy/EbfmIFRk4/LC3awQ2hv+I1C3ElOgWzS+uw7vF+k7kaTI8ZtMZLFntwK7kYwjwdSmI xnWl7comza0Vxp1R500ijvmpBLaLaWzkEeCpYMmLExN+1Pod0TCK3pEUSU0WHEeC/4rm daiAf6v2EAtt3k2b+MdvbscOOUlrdHbPyF1eL/EZe9sEUaAp5ayJMDfwPUrO1B093CxC eV9g== X-Gm-Message-State: AOAM533f4G4OPZf4oYv0g5Vv5jWd9OPfgWxQWq1MMcKqkbYY+aPz5B8r OycB3dVoYZwBE0JnhVMEaWyu2voMGdjtLQ== X-Google-Smtp-Source: ABdhPJxhzH+TWPuyp7emwm0mw9vgu336o9A1/GlamB9krBA8VwSxwwkINWt6C1gmiXACaCSI1EXwnx475/Kcdg== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:b03d:8d64:4a06:2c5f]) (user=dlatypov job=sendgmr) by 2002:a5b:38d:0:b0:645:7b27:3b8b with SMTP id k13-20020a5b038d000000b006457b273b8bmr21950232ybp.146.1650997186276; Tue, 26 Apr 2022 11:19:46 -0700 (PDT) Date: Tue, 26 Apr 2022 11:19:24 -0700 In-Reply-To: <20220426181925.3940286-1-dlatypov@google.com> Message-Id: <20220426181925.3940286-3-dlatypov@google.com> Mime-Version: 1.0 References: <20220426181925.3940286-1-dlatypov@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH 3/3] kfence: test: use new suite_{init/exit} support, add .kunitconfig From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, Daniel Latypov , kasan-dev@googlegroups.com Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Currently, the kfence test suite could not run via "normal" means since KUnit didn't support per-suite setup/teardown. So it manually called internal kunit functions to run itself. This has some downsides, like missing TAP headers => can't use kunit.py to run or even parse the test results (w/o tweaks). Use the newly added support and convert it over, adding a .kunitconfig so it's even easier to run from kunit.py. People can now run the test via $ ./tools/testing/kunit/kunit.py run --kunitconfig=mm/kfence --arch=x86_64 ... [11:02:32] Testing complete. Passed: 23, Failed: 0, Crashed: 0, Skipped: 2, Errors: 0 [11:02:32] Elapsed time: 43.562s total, 0.003s configuring, 9.268s building, 34.281s running Cc: kasan-dev@googlegroups.com Signed-off-by: Daniel Latypov Tested-by: David Gow Reviewed-by: Marco Elver --- mm/kfence/.kunitconfig | 6 ++++++ mm/kfence/kfence_test.c | 31 +++++++++++++------------------ 2 files changed, 19 insertions(+), 18 deletions(-) create mode 100644 mm/kfence/.kunitconfig diff --git a/mm/kfence/.kunitconfig b/mm/kfence/.kunitconfig new file mode 100644 index 000000000000..f3d65e939bfa --- /dev/null +++ b/mm/kfence/.kunitconfig @@ -0,0 +1,6 @@ +CONFIG_KUNIT=y +CONFIG_KFENCE=y +CONFIG_KFENCE_KUNIT_TEST=y + +# Additional dependencies. +CONFIG_FTRACE=y diff --git a/mm/kfence/kfence_test.c b/mm/kfence/kfence_test.c index 1b50f70a4c0f..96206a4ee9ab 100644 --- a/mm/kfence/kfence_test.c +++ b/mm/kfence/kfence_test.c @@ -826,14 +826,6 @@ static void test_exit(struct kunit *test) test_cache_destroy(); } -static struct kunit_suite kfence_test_suite = { - .name = "kfence", - .test_cases = kfence_test_cases, - .init = test_init, - .exit = test_exit, -}; -static struct kunit_suite *kfence_test_suites[] = { &kfence_test_suite, NULL }; - static void register_tracepoints(struct tracepoint *tp, void *ignore) { check_trace_callback_type_console(probe_console); @@ -847,11 +839,7 @@ static void unregister_tracepoints(struct tracepoint *tp, void *ignore) tracepoint_probe_unregister(tp, probe_console, NULL); } -/* - * We only want to do tracepoints setup and teardown once, therefore we have to - * customize the init and exit functions and cannot rely on kunit_test_suite(). - */ -static int __init kfence_test_init(void) +static int kfence_suite_init(struct kunit_suite *suite) { /* * Because we want to be able to build the test as a module, we need to @@ -859,18 +847,25 @@ static int __init kfence_test_init(void) * won't work here. */ for_each_kernel_tracepoint(register_tracepoints, NULL); - return __kunit_test_suites_init(kfence_test_suites); + return 0; } -static void kfence_test_exit(void) +static void kfence_suite_exit(struct kunit_suite *suite) { - __kunit_test_suites_exit(kfence_test_suites); for_each_kernel_tracepoint(unregister_tracepoints, NULL); tracepoint_synchronize_unregister(); } -late_initcall_sync(kfence_test_init); -module_exit(kfence_test_exit); +static struct kunit_suite kfence_test_suite = { + .name = "kfence", + .test_cases = kfence_test_cases, + .init = test_init, + .exit = test_exit, + .suite_init = kfence_suite_init, + .suite_exit = kfence_suite_exit, +}; + +kunit_test_suites(&kfence_test_suite); MODULE_LICENSE("GPL v2"); MODULE_AUTHOR("Alexander Potapenko , Marco Elver ");