From patchwork Wed Nov 28 19:36:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Higgins X-Patchwork-Id: 10703235 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 B080715A8 for ; Wed, 28 Nov 2018 19:38:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A33942B214 for ; Wed, 28 Nov 2018 19:38:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 973692C35F; Wed, 28 Nov 2018 19:38:11 +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=-2.7 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (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 70D132B182 for ; Wed, 28 Nov 2018 19:38:10 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 53B3A21196224; Wed, 28 Nov 2018 11:38:10 -0800 (PST) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2607:f8b0:4864:20::54a; helo=mail-pg1-x54a.google.com; envelope-from=3oo7-ww4kdf88obka7kefddfkpdlldib.9ljifkru-ksafjjifpqp.xy.lod@flex--brendanhiggins.bounces.google.com; receiver=linux-nvdimm@lists.01.org Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id E716821196224 for ; Wed, 28 Nov 2018 11:38:08 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id 143so12762370pgc.3 for ; Wed, 28 Nov 2018 11:38:08 -0800 (PST) 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=4IKZcfrlGwiEH28lOjBx5LUer+sMqwxtctpWBn7VdAA=; b=UCoLXEex5P9gF0WFmCQLfGm+EaXZnIgPgEFS3D1b4XYYWAOr+oa/EhGwwpLKUwMoHv tuJbY1Jo3rvL8QmWbs7VoXQHUKjYsLzaBbfgWlUF+9QyDx525KsLaNCtov3bGDxjIhMq aM9iwePUKMTX37lxS70ZAWsaefCmazhkVU1LrnhwSaPzk58SYuodbP5WiEVAf4ddjWGQ vg3Lc4t7W5ZhhY1HXZ3yidiCg+ml4Pij4s8TMqdveqJjh4UqSzIhEcso6TdsYOZbQN5H NW1ao59aOBDsIVqCi+2gphq5l6I2W8ahTtcVviP7cBirF0n/1VG8Pw3Xk2vaa5oYScKA /1Zw== 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=4IKZcfrlGwiEH28lOjBx5LUer+sMqwxtctpWBn7VdAA=; b=J7yvo5fSl+mnei0qGRLbaEGCtzEb2GjWpnGlZGCT3ozBRPhh4dmpKkx4XLdCz6d2JD spDem7sSk8+GP2VpVnTHbGP1SxAOv/UK/03Q9AO09hf/cK0kT+NB36snWt/82NtZYPOk n0xpaAMnlpoRT5hQmX15mOYmmw39zrV1fOqe/LnI3MvfHQgnQ/V2ChqYztBRFdw6RW74 QYq4tnBHuaCEkMisSHpFV5Dx+1Ehkp/0wLuIpz5Ji6yAo6qwE4izmKHFoK5g/W2Dtk27 dQoLNjQqIqv25Jfzu04RUuZcedgiHq2odnTnQs/OZLFM8830C1TfocuT2e6VH8QUeKBA pDHg== X-Gm-Message-State: AA+aEWb4n5Wp+7JG0ceb2tJ5+4vlPUXosob70v7jbv0JktUyhx9ht/cZ riHE5ulRfm+CZVlRKzdXMcIrDKLdiLr0c+wRCWwfxg== X-Google-Smtp-Source: AFSGD/WnggAFh0owRTchvcPojBQP+KX0t2TGac4DD2dNnzBrZvZn2nu84oJBGeXQPeslzFI8Ot+IGoVssdKwirNseddX6g== X-Received: by 2002:aa7:81cc:: with SMTP id c12mr2953373pfn.69.1543433888545; Wed, 28 Nov 2018 11:38:08 -0800 (PST) Date: Wed, 28 Nov 2018 11:36:27 -0800 In-Reply-To: <20181128193636.254378-1-brendanhiggins@google.com> Message-Id: <20181128193636.254378-11-brendanhiggins@google.com> Mime-Version: 1.0 References: <20181128193636.254378-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.20.0.rc0.387.gc7a69e6b6c-goog Subject: [RFC v3 10/19] kunit: test: add test managed resource tests From: Brendan Higgins To: gregkh@linuxfoundation.org, keescook@google.com, mcgrof@kernel.org, shuah@kernel.org X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: brakmo@fb.com, jdike@addtoit.com, dri-devel@lists.freedesktop.org, linux-kselftest@vger.kernel.org, frowand.list@gmail.com, robh@kernel.org, linux-nvdimm@lists.01.org, richard@nod.at, knut.omang@oracle.com, kieran.bingham@ideasonboard.com, joel@jms.id.au, khilman@baylibre.com, Brendan Higgins , Tim.Bird@sony.com, linux-um@lists.infradead.org, rostedt@goodmis.org, julia.lawall@lip6.fr, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Avinash Kondareddy , daniel@ffwll.ch, mpe@ellerman.id.au, joe@perches.com Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" 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, 110 insertions(+), 11 deletions(-) diff --git a/kunit/test-test.c b/kunit/test-test.c index 88b3bcf9c4e00..36fd95c90a26a 100644 --- a/kunit/test-test.c +++ b/kunit/test-test.c @@ -7,31 +7,130 @@ */ #include -static void test_test_catches_segfault(struct kunit *test) +static void kunit_test_catches_segfault(struct kunit *test) { void (*invalid_func)(void) = (void (*)(void)) SIZE_MAX; KUNIT_ASSERT_SIGSEGV(test, invalid_func()); } -static int test_test_init(struct kunit *test) +/* + * Context for testing test managed resources + * is_resource_initialized is used to test arbitrary resources + */ +struct kunit_test_context { + struct kunit test; + bool is_resource_initialized; +}; + +static int fake_resource_init(struct kunit_resource *res, void *context) { + struct kunit_test_context *ctx = context; + + res->allocation = &ctx->is_resource_initialized; + ctx->is_resource_initialized = true; return 0; } -static void test_test_exit(struct kunit *test) +static void fake_resource_free(struct kunit_resource *res) +{ + bool *is_resource_initialized = res->allocation; + + *is_resource_initialized = false; +} + +static void kunit_test_init_resources(struct kunit *test) +{ + struct kunit_test_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_test_alloc_resource(struct kunit *test) +{ + struct kunit_test_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_test_free_resource(struct kunit *test) { + struct kunit_test_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_test_cleanup_resources(struct kunit *test) +{ + int i; + const int num_res = 5; + struct kunit_test_context *ctx = test->priv; + struct kunit_resource *resources[num_res]; + + for (i = 0; i < num_res; 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_test_init(struct kunit *test) +{ + struct kunit_test_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_test_exit(struct kunit *test) +{ + struct kunit_test_context *ctx = test->priv; + + kunit_cleanup(&ctx->test); + kfree(ctx); } -static struct kunit_case test_test_cases[] = { - KUNIT_CASE(test_test_catches_segfault), +static struct kunit_case kunit_test_cases[] = { + KUNIT_CASE(kunit_test_catches_segfault), + KUNIT_CASE(kunit_test_init_resources), + KUNIT_CASE(kunit_test_alloc_resource), + KUNIT_CASE(kunit_test_free_resource), + KUNIT_CASE(kunit_test_cleanup_resources), {}, }; -static struct kunit_module test_test_module = { - .name = "test-test", - .init = test_test_init, - .exit = test_test_exit, - .test_cases = test_test_cases, +static struct kunit_module kunit_test_module = { + .name = "kunit-test", + .init = kunit_test_init, + .exit = kunit_test_exit, + .test_cases = kunit_test_cases, }; -module_test(test_test_module); +module_test(kunit_test_module);