Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1644087yba; Thu, 4 Apr 2019 15:12:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqzFgjCxdgNSeaLFrmH398VZ6hMO8k7mSDRzyf982VUTGZTDKc9QjP7inIYwGEisQM6DXdwY X-Received: by 2002:a62:14c3:: with SMTP id 186mr8656777pfu.21.1554415961338; Thu, 04 Apr 2019 15:12:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554415961; cv=none; d=google.com; s=arc-20160816; b=ARlyylMYkiiOISScefcmrB0UreIB0Tn5SsTicUlzfTFxSasUJ1EIOdB460zGFBm4gr u8qogAo/FSalDwKxI9UU1JhyTM1wU/hPUkNlLJFJW2GKUX6oqmq/gst2wiMO5xdltkKR LvpMbTQM0mtx2IgfCX05+SMGasVgJW1Wwb/BQAQOJqobp5m4/CEqR1YFKxm3QJJkEOiJ cy08dI34KRgdIqLmX5xFi3ebrCO/KVpV4oCI9SN2GrTXB4LUOG0coJdNsmRta2IHUW2H DjzlXVBZzlaZdRSCytU49rKnhWl9x1xZtkWS3v2WrWpm8V7xenl6lhvRhZPULgVhw5/A 34kA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=eSQTnJlsVyvVkHe102MVefzKzQnpLodW/Uu/p7ocufw=; b=gmssdYpmfHp2/dJJlv/CFhVJWEIlw1SAAACOXi2aZAxV8+7AqSdp54m0/en/a1v85V YKBPNUXBdicLajucUuW3f7CdSO7mLZBnXGb+MccMW/neec7h8CEHMbyc4Vm9KTIHQyrm 2FqqJ6bgJHzHHRJ9i+9cCoH2m137XbL3bFaBtiSy+83f96X+TobOjvuSPEJFX0QuH1IN XrwmYJXs6BBMUBlhR8SYRRcCH+vU99RzBjne1K6TpatrlAZdK7aeRiCr9yc17Ko1pBXz mji1HJUeA47yoA8es0e6eIOkVrFbmgwoqNWqeusmvFkodK+XJa/ATENEgTvMJajjjF3s q29w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=OOcaDPIB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n1si17162522plp.26.2019.04.04.15.12.26; Thu, 04 Apr 2019 15:12:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=OOcaDPIB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1731174AbfDDWLR (ORCPT + 99 others); Thu, 4 Apr 2019 18:11:17 -0400 Received: from mail-qt1-f201.google.com ([209.85.160.201]:52039 "EHLO mail-qt1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730889AbfDDWKa (ORCPT ); Thu, 4 Apr 2019 18:10:30 -0400 Received: by mail-qt1-f201.google.com with SMTP id l26so3620448qtk.18 for ; Thu, 04 Apr 2019 15:10:29 -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=eSQTnJlsVyvVkHe102MVefzKzQnpLodW/Uu/p7ocufw=; b=OOcaDPIBFjNNiGZw7QY6fYXsb0W4NQAQjsAcAWcfEzOlt0OwYQIc5J4fefUyP7oYKi wQaEczWo/9IvNvEwTixlEez5VbeP3VsrYKzrRLWwSd2r0j/jVJ6zqRWIDpR0too8sWxq 78HAHmstJpjcRQU9/pBkMRMAjKilUpNGArEbHfPolO9bjFnpQTuU0fqv8GFAmWApoGSN mf+HldJP+Ktxq9DKw9MIB2LsSsDyX7Gu7v2OoLDwPZNJO1BFh/JfgobZyoXiWShFVfVQ LGLlmn3CdWhJgHWUMrcyqFo+Ei9bAF+4ZavRrcQCI6swKSx3Gv8segziCWQvJpPsqfWW 2dXQ== 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=eSQTnJlsVyvVkHe102MVefzKzQnpLodW/Uu/p7ocufw=; b=gStJ4QrkpOwdo69gsh2TCCSM66cEg5+kIHu1WwHj6fz97q5GSdVQpgw5ojqBmFomi2 Sbb1mb1VsiceN6IjMMFSv+v7K/jShrOTaTP072Z+SOlKwjTdPyQP2a6KcG708Zmk92L3 CV1bKoSL7e42eOstDjI4D1/lYTncNe4MelUBE8BVmM7getQhISG95Qquawss8Bc29DUs wV8ZjL6S83oCP4dEsf6qs01cXKWw4meZRAqC5W79t9GSmou2BuMQyUUjpnqStdUlVaj1 bs6twlFFXviuV3btgfMyLQtQiXzzrFgxPR8skDpSqwgEoOs3pdfohmYQtPgGUQWHUNpg ti3g== X-Gm-Message-State: APjAAAWctGnC8hwuziKAIyf1Uz5SIGrQlvz6FDz87VeZ0HOi7dUt2UFE Mk18rGnqKXds+xCzjCwIgAU+JEfJpfaez4zkjbOy9A== X-Received: by 2002:ae9:e849:: with SMTP id a70mr1215066qkg.34.1554415829026; Thu, 04 Apr 2019 15:10:29 -0700 (PDT) Date: Thu, 4 Apr 2019 15:06:46 -0700 In-Reply-To: <20190404220652.19765-1-brendanhiggins@google.com> Message-Id: <20190404220652.19765-12-brendanhiggins@google.com> Mime-Version: 1.0 References: <20190404220652.19765-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.21.0.392.gf8f6787159e-goog Subject: [PATCH v1 11/17] kunit: test: add test managed resource tests From: Brendan Higgins To: corbet@lwn.net, frowand.list@gmail.com, keescook@google.com, kieran.bingham@ideasonboard.com, mcgrof@kernel.org, robh@kernel.org, shuah@kernel.org, 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, gregkh@linuxfoundation.org, jdike@addtoit.com, joel@jms.id.au, julia.lawall@lip6.fr, khilman@baylibre.com, knut.omang@oracle.com, mpe@ellerman.id.au, pmladek@suse.com, richard@nod.at, rostedt@goodmis.org, wfg@linux.intel.com, Avinash Kondareddy , Brendan Higgins Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Avinash Kondareddy 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 | 122 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/kunit/test-test.c b/kunit/test-test.c index 4bd7a34d0a6cb..54add8ca418a0 100644 --- a/kunit/test-test.c +++ b/kunit/test-test.c @@ -135,3 +135,125 @@ 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)); +} + +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); -- 2.21.0.392.gf8f6787159e-goog