From patchwork Tue Oct 16 23:50:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Higgins X-Patchwork-Id: 10644371 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 DCD0C17D4 for ; Tue, 16 Oct 2018 23:54:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CE61629124 for ; Tue, 16 Oct 2018 23:54:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C26562A27D; Tue, 16 Oct 2018 23:54:13 +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=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5FFE129124 for ; Tue, 16 Oct 2018 23:54:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727633AbeJQHrB (ORCPT ); Wed, 17 Oct 2018 03:47:01 -0400 Received: from mail-qt1-f202.google.com ([209.85.160.202]:56876 "EHLO mail-qt1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727506AbeJQHrA (ORCPT ); Wed, 17 Oct 2018 03:47:00 -0400 Received: by mail-qt1-f202.google.com with SMTP id k14-v6so12666653qta.23 for ; Tue, 16 Oct 2018 16:54:11 -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=d9RWd340hsvL8f+86Hq8ZYiuwlmFywi6LURyY+fbx2M=; b=Yx0djIaN7qGsKbKVWcYTEqjXtuCt5mJzBG+llgMyRaDxHH9Nk2+9u04slBVXvfc3wS WWJHLZ4Cy0qql1s9KGSUtvIF+PX0aE7H32+1XHTQwloMetji6MM1GrYxeoWsHDNEyHme 26sRpezs791PHz4VTxdpWVRBV4zsZkpnI+T4tVqgNpfWT3JVKRx9VCbofy90SGD86rNZ CVw0KoKkl9sSfkCpgdn4xhVAM4qWEBIHZA6zZhx9yOE/i8xtzOj5GcVO+gq7diSY3OG3 3aJXOMLC0d6eKbLtdgSbwRXBJ4WRK0n7XsIsJ8o1UnMToMe3XRTQjSJFXuGQOvz9UjCu IUHg== 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=d9RWd340hsvL8f+86Hq8ZYiuwlmFywi6LURyY+fbx2M=; b=ekMpqbiZcSBRHRo+xjMVebj1chhljsMNWKEORDVqYoQjMJyxHS6dk5jnqrSG3K8ffH /a6JRj7LWM0NOtxtm4Lu3E667/s8PjG0SIBtONYKKdn+5z/WS+Cmo24UYuvjd2hNGifb ha0eakxyZDuG4TdWly7LIySoR1RXZrRMMqe2VbPFNoq5LWN+TUgZ2f2aFfH70mwObidK qfvqapjMHo0uuWPi9sU8hucHyw5PImvgCt+eqrZ8diWJnA2N8mfUIsAYgYcXIAl9lCzK tpPYzxq+3fWPDmTZxB2EdfVAsc3HXfcjxKmGWXo2DX/9gSfALrYa8ExrXAR9djC1K7R/ nMQA== X-Gm-Message-State: ABuFfogJG7pRWq9zJuZvQgvWQaORAodqfowQ0fdFtFwalD6W9Xng4X+E Sc30KUHjXzyxzTdXHPtISfr0PqDzAqAXdD+q5GUDdw== X-Google-Smtp-Source: ACcGV60dfjopFdXI57rHANuj2fTuNIbfr0UGtI22ZYmJqsrCuYMIb093FrDiq5/oUqMydOfiaVQ+Y58DAxIqLJV1QYBy9g== X-Received: by 2002:ac8:8d4:: with SMTP id y20-v6mr20143675qth.9.1539734050869; Tue, 16 Oct 2018 16:54:10 -0700 (PDT) Date: Tue, 16 Oct 2018 16:50:59 -0700 In-Reply-To: <20181016235120.138227-1-brendanhiggins@google.com> Message-Id: <20181016235120.138227-11-brendanhiggins@google.com> Mime-Version: 1.0 References: <20181016235120.138227-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.19.1.331.ge82ca0e54c-goog Subject: [RFC v1 10/31] kunit: test: added concept of initcalls From: Brendan Higgins To: gregkh@linuxfoundation.org, keescook@google.com, mcgrof@kernel.org, shuah@kernel.org Cc: 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, Brendan Higgins Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Added a way to add plugins that require a test module to be loaded during initialization. Signed-off-by: Brendan Higgins --- include/kunit/test.h | 19 +++++++++++++++++++ kunit/test.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/include/kunit/test.h b/include/kunit/test.h index 49a9d6e43992c..58dbe2aee423f 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -152,6 +152,12 @@ struct test_module { struct test_case *test_cases; }; +struct test_initcall { + struct list_head node; + int (*init)(struct test_initcall *this, struct test *test); + void (*exit)(struct test_initcall *this); +}; + /** * struct test - represents a running instance of a test. * @priv: for user to store arbitrary data. Commonly used to pass data created @@ -183,6 +189,19 @@ int test_init_test(struct test *test, const char *name); int test_run_tests(struct test_module *module); +void test_install_initcall(struct test_initcall *initcall); + +#define test_pure_initcall(fn) postcore_initcall(fn) + +#define test_register_initcall(initcall) \ + static int register_test_initcall_##initcall(void) \ + { \ + test_install_initcall(&initcall); \ + \ + return 0; \ + } \ + test_pure_initcall(register_test_initcall_##initcall) + /** * module_test() - used to register a &struct test_module with KUnit. * @module: a statically allocated &struct test_module. diff --git a/kunit/test.c b/kunit/test.c index f89cfaaf5eb79..9737465fb0568 100644 --- a/kunit/test.c +++ b/kunit/test.c @@ -53,6 +53,19 @@ static void test_set_death_test(struct test *test, bool death_test) spin_unlock_irqrestore(&test->lock, flags); } +struct test_global_context { + struct list_head initcalls; +}; + +static struct test_global_context test_global_context = { + .initcalls = LIST_HEAD_INIT(test_global_context.initcalls), +}; + +void test_install_initcall(struct test_initcall *initcall) +{ + list_add_tail(&initcall->node, &test_global_context.initcalls); +} + static int test_vprintk_emit(const struct test *test, int level, const char *fmt, @@ -130,8 +143,18 @@ static void test_run_case_internal(struct test *test, struct test_module *module, struct test_case *test_case) { + struct test_initcall *initcall; int ret; + list_for_each_entry(initcall, &test_global_context.initcalls, node) { + ret = initcall->init(initcall, test); + if (ret) { + test_err(test, "failed to initialize: %d", ret); + test->success = false; + return; + } + } + if (module->init) { ret = module->init(test); if (ret) { @@ -146,6 +169,12 @@ static void test_run_case_internal(struct test *test, static void test_case_internal_cleanup(struct test *test) { + struct test_initcall *initcall; + + list_for_each_entry(initcall, &test_global_context.initcalls, node) { + initcall->exit(initcall); + } + test_cleanup(test); }