From patchwork Tue May 14 05:42:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Higgins X-Patchwork-Id: 10942147 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A6E2517EE for ; Tue, 14 May 2019 05:46:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 96C21285DD for ; Tue, 14 May 2019 05:46:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8A607285E2; Tue, 14 May 2019 05:46:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F0BCF285E1 for ; Tue, 14 May 2019 05:46:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727007AbfENFqY (ORCPT ); Tue, 14 May 2019 01:46:24 -0400 Received: from mail-pg1-f202.google.com ([209.85.215.202]:40167 "EHLO mail-pg1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727058AbfENFqX (ORCPT ); Tue, 14 May 2019 01:46:23 -0400 Received: by mail-pg1-f202.google.com with SMTP id e69so10767882pgc.7 for ; Mon, 13 May 2019 22:46: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=tNDeMg/5pXNKXoffed92pmn5mKLyE8fJ51qEs51jHbo=; b=UlwF03hfMD573CXSZKTM89cl+dGNsQlrAAcNlGkkrX75BIzdQg4aArRMoF9Bmy2Qlw PXrgTSUkgnVN+bnxnLajVm05ycPVxONHbvvbTvDUTENsiu0aJklvsyiVP9j+iBMawv1g FRe5k2u2rDc9MYmWej/lEeO0JpJ/fvi2ZCnyZ1nymlGEMFagxsc78jUduo/GgXdowJnf 66WgavGICXmbJjT7ztAy1SZxxz5kK12lWrJp9UqiqXt4ZT/xIZ+fi1NJB6D9ojukOtuR tkyYIx2134H/mLbxf1sCJ8MTSPGyosGhq0HrbxI/4wkmO4LkcQgWlCRtmPYogqh4QOhf B1kw== 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=tNDeMg/5pXNKXoffed92pmn5mKLyE8fJ51qEs51jHbo=; b=Bewggs9E8kEVlLpoMxI3/TedGarXBngsFR6ec8CLX2psq1KYGi3Y/EWB5f0qDgh5Jf 2ypDjdgxaTMFEVDjf9V1COLvKu+887Nmv4xPlcM+lybOsgiEKUhf66/LYJYD8ooXofwj 5c54XdDnSs94J7tjMWwTeunFFGcz54jl6CbsmYSWh0EtnoG3LhBkZ6D0NHiU5q8J9Bxr Q8w36vDDFRJWzIHKFqN42w0sNnniLga0Yg9+Fdkp1CIBXi9Ep3s+RYLDxQYwHKnAAoJQ al85vgIF3aFnCMh+F3AqhuvqRnNXmzpuedFANCaNfXtkIA6hmYr76s9h7xX41UpF4YtU uJeA== X-Gm-Message-State: APjAAAW4+Dgf0rI1xmkUYFeYWwhHF5Igg1CFfGlEU3Cxc0hDHaTN48C4 PqC+YafqJPOnnP8Qwg4W0SzWAWajGsq7xZS3+4MIZg== X-Google-Smtp-Source: APXvYqyrOv4qazCxPTd56gxM/i8w2fE956x/NWO2xJ602032eHv+/2M1Zv2X7CH3OxIYsrqnxjIClL4SIX+qLSgOSaKb8A== X-Received: by 2002:a63:1d14:: with SMTP id d20mr21863890pgd.281.1557812782521; Mon, 13 May 2019 22:46:22 -0700 (PDT) Date: Mon, 13 May 2019 22:42:46 -0700 In-Reply-To: <20190514054251.186196-1-brendanhiggins@google.com> Message-Id: <20190514054251.186196-13-brendanhiggins@google.com> Mime-Version: 1.0 References: <20190514054251.186196-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.21.0.1020.gf2820cf01a-goog Subject: [PATCH v3 12/18] kunit: test: add tests for KUnit managed resources From: Brendan Higgins To: frowand.list@gmail.com, gregkh@linuxfoundation.org, keescook@google.com, kieran.bingham@ideasonboard.com, mcgrof@kernel.org, robh@kernel.org, sboyd@kernel.org, shuah@kernel.org, tytso@mit.edu, yamada.masahiro@socionext.com Cc: devicetree@vger.kernel.org, dri-devel@lists.freedesktop.org, kunit-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-nvdimm@lists.01.org, linux-um@lists.infradead.org, Alexander.Levin@microsoft.com, Tim.Bird@sony.com, amir73il@gmail.com, dan.carpenter@oracle.com, dan.j.williams@intel.com, daniel@ffwll.ch, jdike@addtoit.com, joel@jms.id.au, julia.lawall@lip6.fr, khilman@baylibre.com, knut.omang@oracle.com, logang@deltatee.com, mpe@ellerman.id.au, pmladek@suse.com, rdunlap@infradead.org, richard@nod.at, rientjes@google.com, rostedt@goodmis.org, wfg@linux.intel.com, Avinash Kondareddy , Brendan Higgins Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Avinash Kondareddy Add unit tests for KUnit managed resources. KUnit managed resources (struct kunit_resource) are resources that are automatically cleaned up at the end of a KUnit test, similar to the concept of devm_* managed resources. Signed-off-by: Avinash Kondareddy Signed-off-by: Brendan Higgins Reviewed-by: Greg Kroah-Hartman Reviewed-by: Logan Gunthorpe --- Changes Since Last Revision: - Added more details to change log as per Shuah's comments. - Made use of new EXPECT macro for pointer comparison to warning reported by kbuild bot. --- kunit/test-test.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/kunit/test-test.c b/kunit/test-test.c index 7afc9bb52f7be..ee926f798b266 100644 --- a/kunit/test-test.c +++ b/kunit/test-test.c @@ -101,3 +101,126 @@ static struct kunit_module kunit_try_catch_test_module = { .test_cases = kunit_try_catch_test_cases, }; module_test(kunit_try_catch_test_module); + +/* + * Context for testing test managed resources + * is_resource_initialized is used to test arbitrary resources + */ +struct kunit_test_resource_context { + struct kunit test; + bool is_resource_initialized; +}; + +static int fake_resource_init(struct kunit_resource *res, void *context) +{ + struct kunit_test_resource_context *ctx = context; + + res->allocation = &ctx->is_resource_initialized; + ctx->is_resource_initialized = true; + return 0; +} + +static void fake_resource_free(struct kunit_resource *res) +{ + bool *is_resource_initialized = res->allocation; + + *is_resource_initialized = false; +} + +static void kunit_resource_test_init_resources(struct kunit *test) +{ + struct kunit_test_resource_context *ctx = test->priv; + + kunit_init_test(&ctx->test, "testing_test_init_test"); + + KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); +} + +static void kunit_resource_test_alloc_resource(struct kunit *test) +{ + struct kunit_test_resource_context *ctx = test->priv; + struct kunit_resource *res; + kunit_resource_free_t free = fake_resource_free; + + res = kunit_alloc_resource(&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res); + KUNIT_EXPECT_PTR_EQ(test, + &ctx->is_resource_initialized, res->allocation); + KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources)); + KUNIT_EXPECT_PTR_EQ(test, free, res->free); +} + +static void kunit_resource_test_free_resource(struct kunit *test) +{ + struct kunit_test_resource_context *ctx = test->priv; + struct kunit_resource *res = kunit_alloc_resource(&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + + kunit_free_resource(&ctx->test, res); + + KUNIT_EXPECT_EQ(test, false, ctx->is_resource_initialized); + KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); +} + +static void kunit_resource_test_cleanup_resources(struct kunit *test) +{ + int i; + struct kunit_test_resource_context *ctx = test->priv; + struct kunit_resource *resources[5]; + + for (i = 0; i < ARRAY_SIZE(resources); i++) { + resources[i] = kunit_alloc_resource(&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + } + + kunit_cleanup(&ctx->test); + + KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); +} + +static int kunit_resource_test_init(struct kunit *test) +{ + struct kunit_test_resource_context *ctx = + kzalloc(sizeof(*ctx), GFP_KERNEL); + + if (!ctx) + return -ENOMEM; + + test->priv = ctx; + + kunit_init_test(&ctx->test, "test_test_context"); + + return 0; +} + +static void kunit_resource_test_exit(struct kunit *test) +{ + struct kunit_test_resource_context *ctx = test->priv; + + kunit_cleanup(&ctx->test); + kfree(ctx); +} + +static struct kunit_case kunit_resource_test_cases[] = { + KUNIT_CASE(kunit_resource_test_init_resources), + KUNIT_CASE(kunit_resource_test_alloc_resource), + KUNIT_CASE(kunit_resource_test_free_resource), + KUNIT_CASE(kunit_resource_test_cleanup_resources), + {}, +}; + +static struct kunit_module kunit_resource_test_module = { + .name = "kunit-resource-test", + .init = kunit_resource_test_init, + .exit = kunit_resource_test_exit, + .test_cases = kunit_resource_test_cases, +}; +module_test(kunit_resource_test_module);