From patchwork Wed May 26 08:11:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 448513 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90C8FC2B9F7 for ; Wed, 26 May 2021 08:11:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 75D0F6143E for ; Wed, 26 May 2021 08:11:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233379AbhEZINC (ORCPT ); Wed, 26 May 2021 04:13:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233395AbhEZIMv (ORCPT ); Wed, 26 May 2021 04:12:51 -0400 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4AD23C061761 for ; Wed, 26 May 2021 01:11:19 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id z9-20020a05622a0609b02901f30a4fcf9bso197732qta.4 for ; Wed, 26 May 2021 01:11:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=EBgkMj153/2pgENUZ1hmDjjQdFq6A7iid7s+bEWY5sw=; b=NEiAiTiWdPRawa9D8Uu+hn/OpaxD+fFcgVsxfyRDS9ye2Za+OXJlX3lC8uXjmQnGm5 kfw5mLKa0ny8Q0QpLVVhaypR8nxxrSz4ND5IwGQM6RP1g33jweWbPly+1RcZ9AVMnW3m g07tUUOVx0U4fofVaRFgeVT3JY5qGwrj909Vcb9t7gA3srOo4jyLAAPX3WpHNkgTLPOG xeevOGXqEn2EvMRB+glCxrV8PamuWf3cgxlx4S9/sJ/ePtVnNmSCTBErouCckazLeGWk QuiXS+nBlEsCYgjGYmIdFXQAryt+g98VPlqand0u9k9UeFp525YG2z/cJNK6pyUvLv27 lNAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=EBgkMj153/2pgENUZ1hmDjjQdFq6A7iid7s+bEWY5sw=; b=tofdHNvQJTe0mI0yeZ1xSdJZWRhUaqUPb8Z30XThcqdTbXwM8Tpn2/FKiNVTrK+dGR gLZp97PyeZWNPrVrAp2+YZ7a3s2tFeJrPDp720C352t/TwlrMg7VmSD9CNy2zFWD2T/j TIyx/JUaB6wtrxiN3w4QLn/fbQVe61X92Avkn0y9zvgLZk89uSjFbch6RM47KojsssyZ DaU9OPWdb+b4767f//6imgQEQtzImbsFdM5odWzT+FEVAv84olZemwZ1hp0F5PlpIWT6 qxdY5AcdItW1w5eQtnrwNMlfif7mJgBZrNwv3Wgs3H1h27UomQO0GUBlceHYB3Yj4EjG p1Cg== X-Gm-Message-State: AOAM533Qi5jC/JQqOJwvOLffHtyhIIHQFKT9xuKEwKwOtf/FmlTdiJVe IJW3wu2SPPwHpoLrRA34/nXe9jwL5T87zg== X-Google-Smtp-Source: ABdhPJzOrnTuG3lq0uBfJAgLi02Aib2O7F3ZhpefOveqPzW5Fc+Br4bkL6Ci9cdvohtuiJBRuZ6T9n7kpUW68A== X-Received: from spirogrip.svl.corp.google.com ([2620:15c:2cb:201:90cb:eafc:a44d:da3d]) (user=davidgow job=sendgmr) by 2002:a0c:c492:: with SMTP id u18mr41058208qvi.40.1622016678371; Wed, 26 May 2021 01:11:18 -0700 (PDT) Date: Wed, 26 May 2021 01:11:10 -0700 Message-Id: <20210526081112.3652290-1-davidgow@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.1.818.g46aad6cb9e-goog Subject: [PATCH 1/3] kunit: Support skipped tests From: David Gow To: Brendan Higgins , Alan Maguire Cc: David Gow , Shuah Khan , Marco Elver , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The kunit_mark_skipped() macro marks the current test as "skipped", with the provided reason. The kunit_skip() macro will mark the test as skipped, and abort the test. The TAP specification supports this "SKIP directive" as a comment after the "ok" / "not ok" for a test. See the "Directives" section of the TAP spec for details: https://testanything.org/tap-specification.html#directives The 'success' field for KUnit tests is replaced with a kunit_status enum, which can be SUCCESS, FAILURE, or SKIPPED, combined with a 'status_comment' containing information on why a test was skipped. A new 'kunit_status' test suite is added to test this. Signed-off-by: David Gow Signed-off-by: Marco Elver Reported-by: kernel test robot --- This change depends on the assertion typechecking fix here: https://lore.kernel.org/linux-kselftest/20210513193204.816681-1-davidgow@google.com/ Only the first two patches in the series are required. This is the long-awaited follow-up to the skip tests RFC: https://lore.kernel.org/linux-kselftest/20200513042956.109987-1-davidgow@google.com/ There are quite a few changes since that version, principally: - A kunit_status enum is now used, with SKIPPED a distinct state - The kunit_mark_skipped() and kunit_skip() macros now take printf-style format strings. - There is now a kunit_status test suite providing basic tests of this functionality. - The kunit_tool changes have been split into a separate commit. - The example skipped tests have been expanded an moved to their own suite, which is not enabled by KUNIT_ALL_TESTS. - A number of other fixes and changes here and there. Cheers, -- David include/kunit/test.h | 68 ++++++++++++++++++++++++++++++++++++++---- lib/kunit/kunit-test.c | 42 +++++++++++++++++++++++++- lib/kunit/test.c | 51 ++++++++++++++++++------------- 3 files changed, 134 insertions(+), 27 deletions(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index b68c61348121..40b536da027e 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -105,6 +105,18 @@ struct kunit; #define KUNIT_SUBTEST_INDENT " " #define KUNIT_SUBSUBTEST_INDENT " " +/** + * enum kunit_status - Type of result for a test or test suite + * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped + * @KUNIT_FAILURE: Denotes the test has failed. + * @KUNIT_SKIPPED: Denotes the test has been skipped. + */ +enum kunit_status { + KUNIT_SUCCESS, + KUNIT_FAILURE, + KUNIT_SKIPPED, +}; + /** * struct kunit_case - represents an individual test case. * @@ -148,13 +160,20 @@ struct kunit_case { const void* (*generate_params)(const void *prev, char *desc); /* private: internal use only. */ - bool success; + enum kunit_status status; char *log; }; -static inline char *kunit_status_to_string(bool status) +static inline char *kunit_status_to_string(enum kunit_status status) { - return status ? "ok" : "not ok"; + switch (status) { + case KUNIT_SKIPPED: + case KUNIT_SUCCESS: + return "ok"; + case KUNIT_FAILURE: + return "not ok"; + } + return "invalid"; } /** @@ -212,6 +231,7 @@ struct kunit_suite { struct kunit_case *test_cases; /* private: internal use only */ + char status_comment[256]; struct dentry *debugfs; char *log; }; @@ -245,19 +265,21 @@ struct kunit { * be read after the test case finishes once all threads associated * with the test case have terminated. */ - bool success; /* Read only after test_case finishes! */ spinlock_t lock; /* Guards all mutable test state. */ + enum kunit_status status; /* Read only after test_case finishes! */ /* * Because resources is a list that may be updated multiple times (with * new resources) from any thread associated with a test case, we must * protect it with some type of lock. */ struct list_head resources; /* Protected by lock. */ + + char status_comment[256]; }; static inline void kunit_set_failure(struct kunit *test) { - WRITE_ONCE(test->success, false); + WRITE_ONCE(test->status, KUNIT_FAILURE); } void kunit_init_test(struct kunit *test, const char *name, char *log); @@ -348,7 +370,7 @@ static inline int kunit_run_all_tests(void) #define kunit_suite_for_each_test_case(suite, test_case) \ for (test_case = suite->test_cases; test_case->run_case; test_case++) -bool kunit_suite_has_succeeded(struct kunit_suite *suite); +enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite); /* * Like kunit_alloc_resource() below, but returns the struct kunit_resource @@ -612,6 +634,40 @@ void kunit_cleanup(struct kunit *test); void kunit_log_append(char *log, const char *fmt, ...); +/** + * kunit_mark_skipped() - Marks @test_or_suite as skipped + * + * @test_or_suite: The test context object. + * @fmt: A printk() style format string. + * + * Marks the test as skipped. @fmt is given output as the test status + * comment, typically the reason the test was skipped. + * + * Test execution continues after kunit_mark_skipped() is called. + */ +#define kunit_mark_skipped(test_or_suite, fmt, ...) \ + do { \ + WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \ + scnprintf((test_or_suite)->status_comment, 256, fmt, ##__VA_ARGS__); \ + } while (0) + +/** + * kunit_skip() - Marks @test_or_suite as skipped + * + * @test_or_suite: The test context object. + * @fmt: A printk() style format string. + * + * Skips the test. @fmt is given output as the test status + * comment, typically the reason the test was skipped. + * + * Test execution is halted after kunit_skip() is called. + */ +#define kunit_skip(test_or_suite, fmt, ...) \ + do { \ + kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\ + kunit_try_catch_throw(&((test_or_suite)->try_catch)); \ + } while (0) + /* * printk and log to per-test or per-suite log buffer. Logging only done * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used. diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index 69f902440a0e..d69efcbed624 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -437,7 +437,47 @@ static void kunit_log_test(struct kunit *test) #endif } +static void kunit_status_set_failure_test(struct kunit *test) +{ + struct kunit fake; + + kunit_init_test(&fake, "fake test", NULL); + + KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS); + kunit_set_failure(&fake); + KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE); +} + +static void kunit_status_mark_skipped_test(struct kunit *test) +{ + struct kunit fake; + + kunit_init_test(&fake, "fake test", NULL); + + /* Before: Should be SUCCESS with no comment. */ + KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS); + KUNIT_EXPECT_STREQ(test, fake.status_comment, ""); + + /* Mark the test as skipped. */ + kunit_mark_skipped(&fake, "Accepts format string: %s", "YES"); + + /* After: Should be SKIPPED with our comment. */ + KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED); + KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES"); +} + +static struct kunit_case kunit_status_test_cases[] = { + KUNIT_CASE(kunit_status_set_failure_test), + KUNIT_CASE(kunit_status_mark_skipped_test), + {} +}; + +static struct kunit_suite kunit_status_test_suite = { + .name = "kunit_status", + .test_cases = kunit_status_test_cases, +}; + kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, - &kunit_log_test_suite); + &kunit_log_test_suite, &kunit_status_test_suite); MODULE_LICENSE("GPL v2"); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 2f6cc0123232..0ee07705d2b0 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -98,12 +98,14 @@ static void kunit_print_subtest_start(struct kunit_suite *suite) static void kunit_print_ok_not_ok(void *test_or_suite, bool is_test, - bool is_ok, + enum kunit_status status, size_t test_number, - const char *description) + const char *description, + const char *directive) { struct kunit_suite *suite = is_test ? NULL : test_or_suite; struct kunit *test = is_test ? test_or_suite : NULL; + const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : ""; /* * We do not log the test suite results as doing so would @@ -114,25 +116,31 @@ static void kunit_print_ok_not_ok(void *test_or_suite, * representation. */ if (suite) - pr_info("%s %zd - %s\n", - kunit_status_to_string(is_ok), - test_number, description); + pr_info("%s %zd - %s%s%s\n", + kunit_status_to_string(status), + test_number, description, + directive_header, directive ? directive : ""); else - kunit_log(KERN_INFO, test, KUNIT_SUBTEST_INDENT "%s %zd - %s", - kunit_status_to_string(is_ok), - test_number, description); + kunit_log(KERN_INFO, test, + KUNIT_SUBTEST_INDENT "%s %zd - %s%s%s", + kunit_status_to_string(status), + test_number, description, + directive_header, directive ? directive : ""); } -bool kunit_suite_has_succeeded(struct kunit_suite *suite) +enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite) { const struct kunit_case *test_case; + enum kunit_status status = KUNIT_SKIPPED; kunit_suite_for_each_test_case(suite, test_case) { - if (!test_case->success) - return false; + if (test_case->status == KUNIT_FAILURE) + return KUNIT_FAILURE; + else if (test_case->status == KUNIT_SUCCESS) + status = KUNIT_SUCCESS; } - return true; + return status; } EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded); @@ -143,7 +151,8 @@ static void kunit_print_subtest_end(struct kunit_suite *suite) kunit_print_ok_not_ok((void *)suite, false, kunit_suite_has_succeeded(suite), kunit_suite_counter++, - suite->name); + suite->name, + suite->status_comment); } unsigned int kunit_test_case_num(struct kunit_suite *suite, @@ -252,7 +261,8 @@ void kunit_init_test(struct kunit *test, const char *name, char *log) test->log = log; if (test->log) test->log[0] = '\0'; - test->success = true; + test->status = KUNIT_SUCCESS; + test->status_comment[0] = '\0'; } EXPORT_SYMBOL_GPL(kunit_init_test); @@ -376,7 +386,8 @@ static void kunit_run_case_catch_errors(struct kunit_suite *suite, context.test_case = test_case; kunit_try_catch_run(try_catch, &context); - test_case->success = test->success; + test_case->status = test->status; + } int kunit_run_tests(struct kunit_suite *suite) @@ -388,7 +399,6 @@ int kunit_run_tests(struct kunit_suite *suite) kunit_suite_for_each_test_case(suite, test_case) { struct kunit test = { .param_value = NULL, .param_index = 0 }; - bool test_success = true; if (test_case->generate_params) { /* Get initial param. */ @@ -398,7 +408,6 @@ int kunit_run_tests(struct kunit_suite *suite) do { kunit_run_case_catch_errors(suite, test_case, &test); - test_success &= test_case->success; if (test_case->generate_params) { if (param_desc[0] == '\0') { @@ -410,7 +419,7 @@ int kunit_run_tests(struct kunit_suite *suite) KUNIT_SUBTEST_INDENT "# %s: %s %d - %s", test_case->name, - kunit_status_to_string(test.success), + kunit_status_to_string(test.status), test.param_index + 1, param_desc); /* Get next param. */ @@ -420,9 +429,10 @@ int kunit_run_tests(struct kunit_suite *suite) } } while (test.param_value); - kunit_print_ok_not_ok(&test, true, test_success, + kunit_print_ok_not_ok(&test, true, test_case->status, kunit_test_case_num(suite, test_case), - test_case->name); + test_case->name, + test.status_comment); } kunit_print_subtest_end(suite); @@ -434,6 +444,7 @@ EXPORT_SYMBOL_GPL(kunit_run_tests); static void kunit_init_suite(struct kunit_suite *suite) { kunit_debugfs_create_suite(suite); + suite->status_comment[0] = '\0'; } int __kunit_test_suites_init(struct kunit_suite * const * const suites) From patchwork Wed May 26 08:11:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 448512 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 891BEC2B9F7 for ; Wed, 26 May 2021 08:11:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6D410613CD for ; Wed, 26 May 2021 08:11:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233412AbhEZINN (ORCPT ); Wed, 26 May 2021 04:13:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233361AbhEZIM4 (ORCPT ); Wed, 26 May 2021 04:12:56 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5E01C061756 for ; Wed, 26 May 2021 01:11:23 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id c126-20020ae9ed840000b02903a69f28eea6so147690qkg.2 for ; Wed, 26 May 2021 01:11:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=TccYnAk92fbSMDbrufKidZyWvSdAcbuvQv0kTQ1sGd8=; b=VheGPqmZczRMJo0WtEivSffIheup6wkGynTDJekzs8b5Tt8q08spRx9OtZFPaBHs5q RswMXFjNfYUKwGlKaGcehJFMAoRn1g/Kp7FPSZU3WBPr7gqE7RNkG2umXsIQ0VOHKqFP ecCEpcltPV8uxwGa/eZpl3DCRQoH+zlrhD9vJeMyIQqvfxdZj6kjtEcQyxojLcmGj/14 UwaumwBFlCFja5Kbkpbet17r6Zzx5pD4xcobnhcx8wRSJEFHXWaiWEYUkYgjMJB9SD9G RXtdLvzV6Lb1m0w7FWu3c5oCC/YRdIfvdgURP4nzbPO8QhwrZOJjTJwzcVNk4sxEwi4h me0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=TccYnAk92fbSMDbrufKidZyWvSdAcbuvQv0kTQ1sGd8=; b=oVvKyhbobpCgSBglb802u2u0fGgJeJjJTheUuVJWPUXJmDNoLAcTyqDYFsDjfapiG/ d6YgGnLql+UVQJ/yOZLUZjouB0EwOUMHy6QoGxS1WUyNQued/iqym6HDa9hi6TR7xmLE WBRlJn1PzBVFzh0OKuA+9zs7THpg01t/toj2YJVJThVeasDzfxKdilfm4kSYR3Oykbx2 5+8R/pjPaJ96xUDnQYKxINf+4oy05ObFbeA6JYC5LpdbqmqQYmIK/CQRT+c9SBo3FQ/r Q7RCbBOAKCpEBCGXtynDchy0GcxWMLbCqD1JqcDQ90CcXfPlPyEBLj5JHIqPamqfSqFd TlcQ== X-Gm-Message-State: AOAM530srxX7Gmw5hU4dBtUroeXUbRxi71cM/BI/m5RmnthermLlTHWh AAayRROTWuSAU2VzWr/1K/AzHvDE00/Vzw== X-Google-Smtp-Source: ABdhPJzKkc7JGP0aLpTZCrFpaH9wmzrc4ve+tjeCGjrAE2LRaTwUEwBe+4IKUbl+Mq6J1pHmq1zCHAqpqGW1rA== X-Received: from spirogrip.svl.corp.google.com ([2620:15c:2cb:201:90cb:eafc:a44d:da3d]) (user=davidgow job=sendgmr) by 2002:a0c:edcf:: with SMTP id i15mr42224753qvr.10.1622016682848; Wed, 26 May 2021 01:11:22 -0700 (PDT) Date: Wed, 26 May 2021 01:11:12 -0700 In-Reply-To: <20210526081112.3652290-1-davidgow@google.com> Message-Id: <20210526081112.3652290-3-davidgow@google.com> Mime-Version: 1.0 References: <20210526081112.3652290-1-davidgow@google.com> X-Mailer: git-send-email 2.31.1.818.g46aad6cb9e-goog Subject: [PATCH 3/3] kunit: test: Add example_skip test suite which is always skipped From: David Gow To: Brendan Higgins , Alan Maguire Cc: David Gow , Shuah Khan , Marco Elver , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Add a new KUnit test suite which contains tests which are always skipped. This is used as an example for how to write tests which are skipped, and to demonstrate the difference between kunit_skip() and kunit_mark_skipped(). Because these tests do not pass (they're skipped), they are not enabled by default, or by the KUNIT_ALL_TESTS config option: they must be enabled explicitly by setting CONFIG_KUNIT_EXAMPLE_SKIP_TEST=y in either a .config or .kunitconfig file. Signed-off-by: David Gow --- lib/kunit/Kconfig | 15 +++++++++ lib/kunit/Makefile | 2 ++ lib/kunit/kunit-example-skip-test.c | 52 +++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 lib/kunit/kunit-example-skip-test.c diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig index 0b5dfb001bac..399fe5f789f7 100644 --- a/lib/kunit/Kconfig +++ b/lib/kunit/Kconfig @@ -45,6 +45,21 @@ config KUNIT_EXAMPLE_TEST is intended for curious hackers who would like to understand how to use KUnit for kernel development. +config KUNIT_EXAMPLE_SKIP_TEST + tristate "Skipped test example for KUnit" + default n + help + Enables an example unit test that is always skipped. + + This test only exists to help new users understand what KUnit is and + how it is used. Please refer to the example test itself, + lib/kunit/example-test.c, for more information. This option is + intended for curious hackers who would like to understand how to use + KUnit for kernel development. + + Because this test does not pass, it is not enabled by + CONFIG_KUNIT_ALL_TESTS + config KUNIT_ALL_TESTS tristate "All KUnit tests with satisfied dependencies" help diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index c49f4ffb6273..8a99ff2f83bd 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -18,3 +18,5 @@ obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o endif obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += kunit-example-test.o + +obj-$(CONFIG_KUNIT_EXAMPLE_SKIP_TEST) += kunit-example-skip-test.o diff --git a/lib/kunit/kunit-example-skip-test.c b/lib/kunit/kunit-example-skip-test.c new file mode 100644 index 000000000000..5395ee0be485 --- /dev/null +++ b/lib/kunit/kunit-example-skip-test.c @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Example KUnit test which is always skipped. + * + * Copyright (C) 2021, Google LLC. + * Author: David Gow + */ + +#include + +/* + * This test should always be skipped. + */ + +static void example_skip_test(struct kunit *test) +{ + /* This line should run */ + kunit_log(KERN_INFO, test, "You should not see a line below."); + + /* Skip (and abort) the test */ + kunit_skip(test, "this test should be skipped"); + + /* This line should not execute */ + kunit_log(KERN_INFO, test, "You should not see this line."); +} + +static void example_mark_skipped_test(struct kunit *test) +{ + /* This line should run */ + kunit_log(KERN_INFO, test, "You should see a line below."); + + /* Skip (but do not abort) the test */ + kunit_mark_skipped(test, "this test should be skipped"); + + /* This line should run */ + kunit_log(KERN_INFO, test, "You should see this line."); +} + +static struct kunit_case example_skip_test_cases[] = { + KUNIT_CASE(example_skip_test), + KUNIT_CASE(example_mark_skipped_test), + {} +}; + +static struct kunit_suite example_skip_test_suite = { + .name = "example_skip", + .test_cases = example_skip_test_cases, +}; + +kunit_test_suites(&example_skip_test_suite); + +MODULE_LICENSE("GPL v2");