Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp231950rwl; Thu, 30 Mar 2023 15:08:54 -0700 (PDT) X-Google-Smtp-Source: AKy350bmh6yePlf507ujPQ/tzYLH/JwnnwSjH+rTfzOnvy9Xr7KulGhOn8gRb+BrhVgZF1Oje6r0 X-Received: by 2002:a05:6a20:7da6:b0:d9:d1e6:82ac with SMTP id v38-20020a056a207da600b000d9d1e682acmr3665935pzj.5.1680214134442; Thu, 30 Mar 2023 15:08:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680214134; cv=none; d=google.com; s=arc-20160816; b=VFS+h6k/joEJk+tvJKZDJon39ZesS3qvNDRqQiMK9hoPQMie4JU0qHZVLOca9Ka2Mz 66lj3+RCb3QSFwWlPgMxQmBbN7ahjJC5M+xJuhQ43qfoUTz+iqUiFe0ASq1YoYhjsBEw uv+GNFlpdbZvy5UBEzjIwBem6eAy+UtK+25uzq7Fc82U1W5daT5ozHKDiXRSQ6BAPHBR JnZ8Z22O6rpVdUDAs4vwXFnKTv2b3gEaM9qz/Xm98WTP5qqf7HJ8PsLOxjX7zIub1Y5p 4cPQkGeTiYkADLVyYRRRCxMkL/mRArqe0LQhJ8THb0SwWk3+Y1Zpva5yyo8t/2u8upHB GzMg== 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=oTWHvZULnypCroDQByK+LhCtI2149pw2flJTMWCZ+Ug=; b=JgRmU+Gkci79q9sq2jwlt+P/61TmSy8WLFjTpIGGNnJleFBbe7DMK3/eq3lVi9zvFh DacjLtZO06cq+3C5ZZzJYTIJIpq7yXFTzkK+3utXCuhsE43AETIxSSJ5CNCGxC62L7t0 vil/uqZXM1tAQ1NWihdJg+U0IojSlS0ie/ithZXKpa+ZPtvKKQK9+3EzOxwX7Y1UmI17 fGvkmg0aO/7XskJFBmYCm+KHsaJcFwCXpQ24/SJYHImo7rY+zA5rHGv3aF9LvuRTyALu WQjMAoBrXWIYtmae/IkRAjeGR8iw/ARpwYPEPf/0sMJYho5CyGiW2mNcSU8y06L3UDuV 8eIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Xp2IdHLX; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j64-20020a638043000000b0051322a8d2aesi707974pgd.110.2023.03.30.15.08.40; Thu, 30 Mar 2023 15:08:54 -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=20210112 header.b=Xp2IdHLX; 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 S230342AbjC3WHk (ORCPT + 99 others); Thu, 30 Mar 2023 18:07:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230299AbjC3WGg (ORCPT ); Thu, 30 Mar 2023 18:06:36 -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 A3E1A10AA7 for ; Thu, 30 Mar 2023 15:05:09 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-545dd1a1e31so149684227b3.22 for ; Thu, 30 Mar 2023 15:05:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680213909; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=oTWHvZULnypCroDQByK+LhCtI2149pw2flJTMWCZ+Ug=; b=Xp2IdHLXHEWGS1lrBMpykudfcKCMvNqxtkJkrCjzBr205WfBba/8d0ab7sAaRGkMX8 OAaP08G/kgiUxMWSMhCmXAUISTC8qGmWEvBz8NsppE6PQ719BtHZYV7jb/gN0pJ+ZM3U siRv50S1kazPuk1E8GmYPBIXZ0nBRilF4QFv4zU2JN0C3A8b4ro1HkWi6xA4hcFGwJ7p myuLFi6aVb59Jk7WE53jbeEvkQy+13wjCknwWCdLgmUCPnegwnoz0+SuYwp+GRQ/NpNu rpzri8pI7IyZVvzfaIdh4Raf4pSceP476I8Dq1wiJPhzb60LiDy27VmjU+gcviD7cWBC uwaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680213909; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=oTWHvZULnypCroDQByK+LhCtI2149pw2flJTMWCZ+Ug=; b=eHXV5DMuceoSxi6Z9ve02XEXwkgkKfa/IzyPGrSQAj02DOV8AYCjQxIjJoJiYCTUwu GX9os2KjqbTcNqR+11QsODAx/RZPSqCA2cqlgeZWUg8m2HKFjE4sOwsMd/PbxXvRqPgu rkPc8QxbgRRHS68DmPIpNjr2Oh2hetbPdjsoVyX2Ji3s7HI719ghMEJebe89wKHPaea9 zxJXmkX5V1+j643CW8EwgVMwx7byyGi7Si6DHOJ/AiXOCbqE7veKJC+VgCwhtMXfHO/F PpcEiyQ5YTJ+KQzuOyALBsFKD5AY8PuJIePOML8s0ZdfEidkUq7eF2EzrA7h+aQd3qVL eVJQ== X-Gm-Message-State: AAQBX9cuCLiLd1urm+d184DAzh6Ka6CPbnQD3hNkEuxKUFmuwzAGgUvl Dz/LE2QYTenmQwuxeWXHRBfH8VeylQ== X-Received: from rmoar-specialist.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:45d3]) (user=rmoar job=sendgmr) by 2002:a25:e68e:0:b0:b6b:6a39:949c with SMTP id d136-20020a25e68e000000b00b6b6a39949cmr12186172ybh.6.1680213908990; Thu, 30 Mar 2023 15:05:08 -0700 (PDT) Date: Thu, 30 Mar 2023 22:05:06 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230330220506.1399796-1-rmoar@google.com> Subject: [PATCH v1] kunit: add tests for using current KUnit test field From: Rae Moar To: brendanhiggins@google.com, davidgow@google.com, dlatypov@google.com Cc: skhan@linuxfoundation.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Rae Moar Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.7 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,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 Create test suite called "kunit_current" to add test coverage for the use of current->kunit_test, which returns the current KUnit test. Add three test cases: - kunit_current_kunit_test_field to test the use of current->kunit_test. - kunit_current_get_current_test to test the method kunit_get_current_test(), which utilizes current->kunit_test. - kunit_current_fail_current_test to test the method kunit_fail_current_test(), which utilizes current->kunit_test. Signed-off-by: Rae Moar --- lib/kunit/kunit-test.c | 61 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index b63595d3e241..91984b92c916 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -6,6 +6,7 @@ * Author: Brendan Higgins */ #include +#include #include "try-catch-impl.h" @@ -532,7 +533,65 @@ static struct kunit_suite kunit_status_test_suite = { .test_cases = kunit_status_test_cases, }; +static void kunit_current_kunit_test_field(struct kunit *test) +{ + struct kunit *current_test; + + /* Check to ensure the result of current->kunit_test + * is equivalent to current test. + */ + current_test = current->kunit_test; + KUNIT_EXPECT_PTR_EQ(test, test, current_test); +} + +static void kunit_current_get_current_test(struct kunit *test) +{ + struct kunit *current_test1, *current_test2; + + /* Check to ensure the result of kunit_get_current_test() + * is equivalent to current test. + */ + current_test1 = kunit_get_current_test(); + KUNIT_EXPECT_PTR_EQ(test, test, current_test1); + + /* Check to ensure the result of kunit_get_current_test() + * is equivalent to current->kunit_test. + */ + current_test2 = current->kunit_test; + KUNIT_EXPECT_PTR_EQ(test, current_test1, current_test2); +} + +static void kunit_current_fail_current_test(struct kunit *test) +{ + struct kunit fake; + + /* Initialize fake test and set as current->kunit_test. */ + kunit_init_test(&fake, "fake test", NULL); + KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS); + current->kunit_test = &fake; + + /* Fail current test and expect status of fake test to be failed. */ + kunit_fail_current_test("This test is supposed to fail."); + KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE); + + /* Reset current->kunit_test to current test. */ + current->kunit_test = test; +} + +static struct kunit_case kunit_current_test_cases[] = { + KUNIT_CASE(kunit_current_kunit_test_field), + KUNIT_CASE(kunit_current_get_current_test), + KUNIT_CASE(kunit_current_fail_current_test), + {} +}; + +static struct kunit_suite kunit_current_test_suite = { + .name = "kunit_current", + .test_cases = kunit_current_test_cases, +}; + kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, - &kunit_log_test_suite, &kunit_status_test_suite); + &kunit_log_test_suite, &kunit_status_test_suite, + &kunit_current_test_suite); MODULE_LICENSE("GPL v2"); base-commit: 7232282dd47cce6a780c9414bd9baccf232c7686 -- 2.40.0.348.gf938b09366-goog