From patchwork Thu Feb 14 21:37:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: kernel test robot via dri-devel X-Patchwork-Id: 10816605 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 5F6C317D5 for ; Sat, 16 Feb 2019 20:13:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4E24F2AB54 for ; Sat, 16 Feb 2019 20:13:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3F9012AB6F; Sat, 16 Feb 2019 20:13:42 +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=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CEA3B2AB54 for ; Sat, 16 Feb 2019 20:13:41 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 321206E4E9; Sat, 16 Feb 2019 20:11:39 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-vs1-xe49.google.com (mail-vs1-xe49.google.com [IPv6:2607:f8b0:4864:20::e49]) by gabe.freedesktop.org (Postfix) with ESMTPS id E28A36E402 for ; Thu, 14 Feb 2019 21:38:56 +0000 (UTC) Received: by mail-vs1-xe49.google.com with SMTP id g26so1808936vsp.20 for ; Thu, 14 Feb 2019 13:38:56 -0800 (PST) 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=lFB3ckD9wxM7bLXj4FIGXk/8wLU/9JJW8w6AYqQ9grk=; b=jk75oesP5NizwNwQ3KjABwNhKnBlCZCofa34o6W0CJw5jlsXc9z9/URVLKZDimCgt5 HAqsBiz2Bn0Pa817UlsupTSHHdokuIOthuQYpkfecjoxkcSV/hbp34ObdsZAJkrW81Gh qzpltQ0gB9BbGZPd07zdQV79RtBo9Pqz/zTQc3MQAD9eHfLDZ3Ey1LEVFxr2cguIcy6z pAb/wOLEOmQG6PDKm4XwR/hOVQc64c3J3nUZuaIvWHCYlcoqUrUjFKgWBWHsRjaAeDil DJ882FEq3qoujO42t2QWPr8lZqIJ9ah9sRt5V27Lf8DKYsfeSuCRFP3+BLmas/5BxkOM 93+Q== X-Gm-Message-State: AHQUAua23PPrrnw+LkA2L1BF9g1QOG8smJ90EKQVRcVM6KIIqGT4lsMM hBKW+o+PPT9aW3omjci/oDMJShnS1F8mQYOMZ/nc0g== X-Google-Smtp-Source: AHgI3IZSFaDNk0CEH4L9GvdAED7vCeS8gXXQOioZtPpSgMCEqNw/swX6iIzcGwBJejpW5Xow4Gqbz47ePA5Svka1+dm8fw== X-Received: by 2002:ac5:c203:: with SMTP id m3mr3934061vkk.19.1550180335945; Thu, 14 Feb 2019 13:38:55 -0800 (PST) Date: Thu, 14 Feb 2019 13:37:22 -0800 In-Reply-To: <20190214213729.21702-1-brendanhiggins@google.com> Message-Id: <20190214213729.21702-11-brendanhiggins@google.com> Mime-Version: 1.0 References: <20190214213729.21702-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.21.0.rc0.258.g878e2cd30e-goog Subject: [RFC v4 10/17] kunit: test: add test managed resource tests To: keescook@google.com, mcgrof@kernel.org, shuah@kernel.org, robh@kernel.org, kieran.bingham@ideasonboard.com, frowand.list@gmail.com X-Mailman-Original-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=lFB3ckD9wxM7bLXj4FIGXk/8wLU/9JJW8w6AYqQ9grk=; b=wR5UZ8MnBfmgt0mrk9OI2zfDUhZY2FSpJFlc0H37jwgc96pBztb7fGKWTkoOFM28Ga QdMIu5N3UkjMmm7021TzRYk/cV1BnPbW4MtjUKPwt51uaE+fMNPFoimjbpRGCDHRC4qG rF/9HGEJTgXVmjERaYbSkHQNurtVfrADHvm+4f2etjUc19ab4p6e7BsAxhXpCzh2DVbh 1qIqHUMYZmAhWwLj0wStuzKrlp2f5ArmCo8Pw4WHmaV0ubR/6cHsjshHUAlvy2ad4Zc7 fT6+TNOikOpDCk2iZY9GB/1MB0KgGDOgjPxVqJZHudZCO4TukRT9wX0iqh4to7EcadBk jKQg== X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Brendan Higgins via dri-devel From: kernel test robot via dri-devel Reply-To: Brendan Higgins Cc: brakmo@fb.com, pmladek@suse.com, amir73il@gmail.com, Brendan Higgins , dri-devel@lists.freedesktop.org, Alexander.Levin@microsoft.com, linux-kselftest@vger.kernel.org, linux-nvdimm@lists.01.org, richard@nod.at, knut.omang@oracle.com, wfg@linux.intel.com, joel@jms.id.au, jdike@addtoit.com, dan.carpenter@oracle.com, devicetree@vger.kernel.org, Tim.Bird@sony.com, linux-um@lists.infradead.org, rostedt@goodmis.org, julia.lawall@lip6.fr, dan.j.williams@intel.com, kunit-dev@googlegroups.com, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, Avinash Kondareddy , mpe@ellerman.id.au, joe@perches.com, khilman@baylibre.com Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Tests how tests interact with test managed resources in their lifetime. Signed-off-by: Avinash Kondareddy Signed-off-by: Brendan Higgins --- kunit/test-test.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/kunit/test-test.c b/kunit/test-test.c index 0b4ad6690310d..bb34431398526 100644 --- a/kunit/test-test.c +++ b/kunit/test-test.c @@ -127,3 +127,124 @@ 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_EQ(test, &ctx->is_resource_initialized, res->allocation); + KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources)); + KUNIT_EXPECT_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)); +} + +#define KUNIT_RESOURCE_NUM 5 +static void kunit_resource_test_cleanup_resources(struct kunit *test) +{ + int i; + struct kunit_test_resource_context *ctx = test->priv; + struct kunit_resource *resources[KUNIT_RESOURCE_NUM]; + + for (i = 0; i < KUNIT_RESOURCE_NUM; 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);