Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp188721imj; Thu, 14 Feb 2019 18:17:03 -0800 (PST) X-Google-Smtp-Source: AHgI3IahEk5L/6HVJpsY1DFXusfqikGIVwExDRpdgnL0or73Pd4Jwo8Gn6jqBJEr+TkE43ToEZer X-Received: by 2002:a17:902:243:: with SMTP id 61mr7301565plc.249.1550197023500; Thu, 14 Feb 2019 18:17:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550197023; cv=none; d=google.com; s=arc-20160816; b=vdqHnAhvAa0Bxj5agm/BReCIBkmRFBKVSWDUZ9qWw6MAzWDs36uweL75qTgYv0CvVZ iKSjZpP+hlu0rN9xaM9z5gUwk//t7AnvVjZ3/m7amPKybSFNyBeFKBwXQHFzTCKDPzu6 LoXXfRYEgy4dmCnLmT4yA2IWaUgR8mF2ryYFjUTMoygMLeJItvTzO1MwMEvofYPvte24 VOZgjuni6Nx2LYe3znXHjWpcSzSRtEqChh7+P707xS2Yhdgh2pFb9p9juYraHQ8cCQhI OFWYEKvHmpnXfQXbLBdkr1P1PW29pcz2TfGyFXebhBh9HwlAFwhouw1SWg6RsS05uOLK zsfQ== 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=lFB3ckD9wxM7bLXj4FIGXk/8wLU/9JJW8w6AYqQ9grk=; b=yKtT3z6leW2ZcvxQRq2uulLtm8aOgH4V3Pi1HxVe+S7HCPiMggz852TdsND7WaRnYs FHYuwKlfUHl3nr0fHyEv2VHXXa5hHf5NtsWwxrUauuPFIfbb8jt//gGUsnig7l1AuLlm 4kXSdICwo4j5NO+5TYHm/cETwxLSriAGEo9u3eD9FbSz5mm7IT3TdJYp7KjPRZt4UkHP yWVoPP+Q12wCtswCBTOWcE9nAJ9xvnXqhdTVCfiDDwAWeEdnBKHgiWAm7YmjLuZJ5NOg 692SM0afwxAaCMP4272rf+FQ3ZdahjFXIr7BjgbM5MXdOusDbOS1lD/AMty+jhRstsl3 Qv6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=wR5UZ8Mn; 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 n24si3830646pgv.119.2019.02.14.18.16.47; Thu, 14 Feb 2019 18:17:03 -0800 (PST) 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=wR5UZ8Mn; 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 S2440353AbfBNVi6 (ORCPT + 99 others); Thu, 14 Feb 2019 16:38:58 -0500 Received: from mail-vk1-f201.google.com ([209.85.221.201]:56808 "EHLO mail-vk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2393180AbfBNVi5 (ORCPT ); Thu, 14 Feb 2019 16:38:57 -0500 Received: by mail-vk1-f201.google.com with SMTP id q125so834955vka.23 for ; Thu, 14 Feb 2019 13:38:56 -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=lFB3ckD9wxM7bLXj4FIGXk/8wLU/9JJW8w6AYqQ9grk=; b=wR5UZ8MnBfmgt0mrk9OI2zfDUhZY2FSpJFlc0H37jwgc96pBztb7fGKWTkoOFM28Ga QdMIu5N3UkjMmm7021TzRYk/cV1BnPbW4MtjUKPwt51uaE+fMNPFoimjbpRGCDHRC4qG rF/9HGEJTgXVmjERaYbSkHQNurtVfrADHvm+4f2etjUc19ab4p6e7BsAxhXpCzh2DVbh 1qIqHUMYZmAhWwLj0wStuzKrlp2f5ArmCo8Pw4WHmaV0ubR/6cHsjshHUAlvy2ad4Zc7 fT6+TNOikOpDCk2iZY9GB/1MB0KgGDOgjPxVqJZHudZCO4TukRT9wX0iqh4to7EcadBk jKQg== 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=Jm9f1DiA9eAbUa4uZrWT/AVB53Xqs0u4f1+VUe18pJ+P2cSiekvKNa6v5HiF0Pu/CS D0IpvnCIgUitYnJX2tNaGW3QHXBrY0V2bjWfKDT41yTt1924rUC2kTNloZ3/Y/2h5upl ilQn8e++41KRv+ZPqNUExMi997Rls++qBu9EKIxmedDagsWMAsJ5Eqa6d+TQvO07w5Z4 yNjZviCJrsbMzQAp898j0t/NwXtpkk956XIGQmWGPsueZkRpZ67kS3HnQifqFJgZKR6w hiPwowB40okr9lVwXk4xEdOgQjX7KA0oHYXipctEYqIb/Auj4HZZ4lI8vXfJ5s8d3Pv9 RUGQ== X-Gm-Message-State: AHQUAuZpu0HCosVa/8jS0AnHoP9XlugOrNCUQu2zMkScwN/0m0/7qfvG m/xrgSpJqWhGdKnnYZcojRkGWFDa+UnLwgBvG2obrw== 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 From: Brendan Higgins To: keescook@google.com, mcgrof@kernel.org, shuah@kernel.org, robh@kernel.org, kieran.bingham@ideasonboard.com, frowand.list@gmail.com Cc: gregkh@linuxfoundation.org, joel@jms.id.au, mpe@ellerman.id.au, joe@perches.com, brakmo@fb.com, rostedt@goodmis.org, Tim.Bird@sony.com, khilman@baylibre.com, julia.lawall@lip6.fr, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, jdike@addtoit.com, richard@nod.at, linux-um@lists.infradead.org, daniel@ffwll.ch, dri-devel@lists.freedesktop.org, dan.j.williams@intel.com, linux-nvdimm@lists.01.org, knut.omang@oracle.com, devicetree@vger.kernel.org, pmladek@suse.com, Alexander.Levin@microsoft.com, amir73il@gmail.com, dan.carpenter@oracle.com, wfg@linux.intel.com, Brendan Higgins , Avinash Kondareddy 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 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); -- 2.21.0.rc0.258.g878e2cd30e-goog