Re: [PATCH v1 11/17] kunit: test: add test managed resource tests

From: Masayoshi Mizuma
Date: Wed Apr 24 2019 - 15:00:27 EST


On Thu, Apr 04, 2019 at 03:06:46PM -0700, Brendan Higgins wrote:
> From: Avinash Kondareddy <akndr41@xxxxxxxxx>
>
> Tests how tests interact with test managed resources in their lifetime.
>
> Signed-off-by: Avinash Kondareddy <akndr41@xxxxxxxxx>
> Signed-off-by: Brendan Higgins <brendanhiggins@xxxxxxxxxx>
> ---
> 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_NOT_ERR_OR_NULL(test, res);

Thanks!
Masa