Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp2258685ybi; Mon, 17 Jun 2019 01:29:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqwa9G0nrJRWgPK9p4P6cUsqLJphmJvetjOvvp/y1CkQFE+vNDtttwk5zkujAQ+coKn22t1A X-Received: by 2002:a63:6981:: with SMTP id e123mr23911657pgc.136.1560760197798; Mon, 17 Jun 2019 01:29:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560760197; cv=none; d=google.com; s=arc-20160816; b=Swt7eGcxnyUFlyG82QhSccZm7Jsr0+G7Pc+QttzJmNty3KEDf3G4g4Via/YYk0qvTd 3Qw/KUFSmm+ql82uYW2BacToDsyupUZitVyTM+3hUBStr1+kq3Xtj84cHksQBo7VZP4E 12RQrEZV9RfGGYkQzXNklD5wBQzcNt9aMp3VXx9bVUQiZD+X33A9h1k5RHnW5M9O9LF7 jDLfHCKW1aUlQ3b4wmxzeBfsKE9CYKbkhUEZSk0xOkDU7z4BJqkrspQwp1DDoy0aqNbm CCfBo2NMRAah3ZJ/65Q+prTN9JMZG9XLLqmzEs76ZGpzZ8TlSGI5sIsWslk82E20hmTH 9FZQ== 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=q4u3K2o+PGWHKQMYW6GJulzbQhMLZBbExmthsuEmVYA=; b=EidbtLfLJvLmcIWWxLLf41rYuZ0U8Lz1X7SEXFnsuEuTOcQjoaEr2YcuLvSvBS7/Pj fYyplu72uNsm5CILnM/DeLSv+BTi95T0esxgePFBTZOD4StGVyCgbj22apA0kHnu78Mv Uwgs3IXR5clwgTozafiZvYJZG1qAP1cXGIUq7D9W4SYdXz6z9oYKKx5NYhukICU8DLra xfzZGm6JePsLIbfhyI7iGBj3vKz3uooiI4UnUAWyH2hcAhIMfhjnMHC7PEftP5gAsL81 LJTZFmVR71fzbpjWN1pJZoNT+dlwVF5jvE5hVS0maoX5oosrUBM8fDKLqc0lAb/hTnGw DEOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=KONCrR9X; 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 w5si9128704pjt.86.2019.06.17.01.29.42; Mon, 17 Jun 2019 01:29:57 -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=KONCrR9X; 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 S1727939AbfFQI1f (ORCPT + 99 others); Mon, 17 Jun 2019 04:27:35 -0400 Received: from mail-ot1-f74.google.com ([209.85.210.74]:48447 "EHLO mail-ot1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727620AbfFQI1d (ORCPT ); Mon, 17 Jun 2019 04:27:33 -0400 Received: by mail-ot1-f74.google.com with SMTP id b4so4582292otf.15 for ; Mon, 17 Jun 2019 01:27:33 -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=q4u3K2o+PGWHKQMYW6GJulzbQhMLZBbExmthsuEmVYA=; b=KONCrR9XKO6a/42sXEG+qp4qQkmC/vZY5/QUb1W8vaG/3JrKerLSMXIUtx9hhIgBA+ 6Ydueb1aDkkRClPaW4rcMGl4guf7yUgqbFKApT2oc0aPrWdvu4+hk/qZMvEjbrAXxHZA +BOlwn2CmUv0A0UJDIBcJ7UN2vpUjW9oUtdN0gAZTRJcqwmrT4Rzj/VIj1fEr/vFRdTC O4jWhtj4KF6wiqA7V3WjMbDqhn58sL3SAXtUpzZRJZ/u16slCYhi65NL34uOgKTFdtbv 83j5QptrG4ALHgxwsqoie4UlVJRO5mcGODYjTisAn8cJbajr27RyN4JZoEuCObcr7fX/ AJPQ== 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=q4u3K2o+PGWHKQMYW6GJulzbQhMLZBbExmthsuEmVYA=; b=oQ7z3qJfVntOI9vMMGHgae6WWkXwcSShCl3iOlhv4PLM+UTOSRvxNYoIutfN4HRgRr Sp+td935iE7YPWVQ++/a3VrQ1hvr8JmkPcXwukp6jPPAtWWEBSAO8okQs4jV8fVEfP0l I+MdSzFLmsdyLavETThH7clwAB4TTdXY15ic0bnPaaSFfW4CCuMD13CZzcV9CN3EWMmW cHe3MqhcaxX6ZdM8dqYsnI9tgHiapcjm1IY2de34dbskpjrxox1AcYsMi0FFe7SAz9oZ NaoE3m9j9xQPsKDa56xV6dJWLGNPH8+Q5RxTIb5flsE8FzdvLQXtkv+7AjhGXKJEAXn0 YQAA== X-Gm-Message-State: APjAAAU4afAdyh141f2eIKGU99QR9+dtQfT9JOBiTWN2Bh1PMnDgThWx URQ8V91gbs1pYOtzHi8xvZIt5obGnTE6yA0zdL5APg== X-Received: by 2002:a05:6830:15d7:: with SMTP id j23mr7972436otr.176.1560760052933; Mon, 17 Jun 2019 01:27:32 -0700 (PDT) Date: Mon, 17 Jun 2019 01:26:07 -0700 In-Reply-To: <20190617082613.109131-1-brendanhiggins@google.com> Message-Id: <20190617082613.109131-13-brendanhiggins@google.com> Mime-Version: 1.0 References: <20190617082613.109131-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCH v5 12/18] kunit: test: add tests for KUnit managed resources From: Brendan Higgins To: frowand.list@gmail.com, gregkh@linuxfoundation.org, jpoimboe@redhat.com, keescook@google.com, kieran.bingham@ideasonboard.com, mcgrof@kernel.org, peterz@infradead.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, 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 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 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 --- kunit/test-test.c | 219 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 219 insertions(+) diff --git a/kunit/test-test.c b/kunit/test-test.c index 43d1bedcf4592..60ef508cda1c6 100644 --- a/kunit/test-test.c +++ b/kunit/test-test.c @@ -101,3 +101,222 @@ 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; + int allocate_order[2]; + int free_order[2]; +}; + +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, + (bool *) 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_FALSE(test, 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 void kunit_resource_test_mark_order(int order_array[], + size_t order_size, + int key) +{ + int i; + + for (i = 0; i < order_size && order_array[i]; i++) + ; + + order_array[i] = key; +} + +#define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key) \ + kunit_resource_test_mark_order(ctx->order_field, \ + ARRAY_SIZE(ctx->order_field), \ + key) + +static int fake_resource_2_init(struct kunit_resource *res, void *context) +{ + struct kunit_test_resource_context *ctx = context; + + KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2); + + res->allocation = ctx; + + return 0; +} + +static void fake_resource_2_free(struct kunit_resource *res) +{ + struct kunit_test_resource_context *ctx = res->allocation; + + KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2); +} + +static int fake_resource_1_init(struct kunit_resource *res, void *context) +{ + struct kunit_test_resource_context *ctx = context; + + kunit_alloc_resource(&ctx->test, + fake_resource_2_init, + fake_resource_2_free, + ctx); + + KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1); + + res->allocation = ctx; + + return 0; +} + +static void fake_resource_1_free(struct kunit_resource *res) +{ + struct kunit_test_resource_context *ctx = res->allocation; + + KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1); +} + +/* + * TODO(brendanhiggins@google.com): replace the arrays that keep track of the + * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro + * to assert allocation and freeing order when the feature becomes available. + */ +static void kunit_resource_test_proper_free_ordering(struct kunit *test) +{ + struct kunit_test_resource_context *ctx = test->priv; + + /* fake_resource_1 allocates a fake_resource_2 in its init. */ + kunit_alloc_resource(&ctx->test, + fake_resource_1_init, + fake_resource_1_free, + ctx); + + /* + * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER + * before returning to fake_resource_1_init, it should be the first to + * put its key in the allocate_order array. + */ + KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2); + KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1); + + kunit_cleanup(&ctx->test); + + /* + * Because fake_resource_2 finishes allocation before fake_resource_1, + * fake_resource_1 should be freed first since it could depend on + * fake_resource_2. + */ + KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1); + KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2); +} + +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), + KUNIT_CASE(kunit_resource_test_proper_free_ordering), + {} +}; + +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.22.0.410.gd8fdbe21b5-goog