From patchwork Tue May 14 05:42:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Higgins X-Patchwork-Id: 1099238 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="lyyXpiZg"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="rBSMRYv6"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45367d6dLnz9sNf for ; Tue, 14 May 2019 15:44:13 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version :Message-Id:In-Reply-To:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ZxxQfR3uIe8fpRIFzsPSC9FrhNycS0fVVBYor2FYXFs=; b=lyyXpiZgyAgYAt GilP/ap0qyDCRAWQSKUKer3x8vO0NqJgUDyKmV5toPJg9jTGcfZSfugvEET6WED1y7XCuXeU7LtMs P30/Wpq4c5M7X/fG5j/Xzf3n/yrQI+dAUI57puHaoPgRhfEs1KDKTp1R+XaSw9Yh15boE3c/UdFpg Q3Uaesi//HL/w1+j8/QonPQcLduoYUajXU5kALf++hYKzbLgklRtAmMK5piDvQ1xH/1xrbgaOOtUP qOE3Ys8B9x06cODM1DuicjHSoq5vMSxKRbj4aef356ESporJToezhflM1tctektLRo5pU1USEjkiz 1qaVExSg+uJUUd0Aqe4g==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hQQEP-0004rx-TX; Tue, 14 May 2019 05:44:05 +0000 Received: from mail-pg1-x549.google.com ([2607:f8b0:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hQQEM-0004qi-UP for linux-um@lists.infradead.org; Tue, 14 May 2019 05:44:04 +0000 Received: by mail-pg1-x549.google.com with SMTP id h14so10734883pgn.23 for ; Mon, 13 May 2019 22:44:02 -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=J7jNxXx2/Tojw9LkQBvkznuAV27E11EoaIcDTsM30ik=; b=rBSMRYv6rcgf9//g8xbBS7nj7wXeNN0kUp4Ny4gMt/205NXZuBUEfAGZGUqQ/IIRLJ T9q5gjFm+3UV+T+Ba55A5XSzmNTjVsyCWu7g0rjQIhYWvGtM/lKSV0MBPT7+Uu4sj3Ns cdhhmUPyS9LpYUrAU9TSmLXVwMsy8Ru0sHQaCkmJQcniNEVVGKyzGisJ5rNE+SySd0Ho 5bEf+Nlf5wVndA4XF35fHsn5SKA+4SR6UH8eJFEQyxBNdjWzK2fW50vOUx5PIkp8y7FR x0rv8s/8/ssOFMyHsqoRkTT4/0V1oZ3eddqPhKnu4A9BAU4StCJclt3AgjNPIBj/4Hmj SzLg== 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=J7jNxXx2/Tojw9LkQBvkznuAV27E11EoaIcDTsM30ik=; b=exFJA1RqTezo7/Gt5a45EbxfIT5BhkyH9SkBncAL7CA33JcrNkbJdGbsPkze/+SIZZ 2eVu0rd3rHqSwdXLhRF1HMwR/uZZeQZN9wyGoailnxgR6exdcScRdqhV+mFBCoapGWTr wPlrisKZHG8pCCb2Ua2mOa17h2f0lbUEkQS11CkPpfwnnK6ThpTcJClZgPxuYQQIQfGZ D8C/6HvB65PolsKMVhs6zYTcmRYuHUWa3Ii2kNxl2p+hFso6KU8rFJg8CiF1kBKm/OVd KvqGYafB4UBC3C0rOJyR6MWqLnc1EuTfmrr0MBJVwkGQFRn8ihnC2JzBVr14wuChcKlF NsNg== X-Gm-Message-State: APjAAAXLHYf6Al/psIN6VXWk8RdZ8NtwC6GFV1bM7e0W9+DqbZelzW+3 jXaGPJ1827ZGuc70WSNZhzBMQDWVXJJI3u9c0g5GSA== X-Google-Smtp-Source: APXvYqwDgWK6D5zh2H9zOeZMl7FTqGXfZom8j6k/qPQTCzWEIs4iUL6yGm+xbdZd25stfXMxSVVYpf0Cp9+yxVzww2vLNg== X-Received: by 2002:a63:1c4:: with SMTP id 187mr8764328pgb.317.1557812641863; Mon, 13 May 2019 22:44:01 -0700 (PDT) Date: Mon, 13 May 2019 22:42:37 -0700 In-Reply-To: <20190514054251.186196-1-brendanhiggins@google.com> Message-Id: <20190514054251.186196-5-brendanhiggins@google.com> Mime-Version: 1.0 References: <20190514054251.186196-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.21.0.1020.gf2820cf01a-goog Subject: [PATCH v3 04/18] kunit: test: add kunit_stream a std::stream like logger From: Brendan Higgins To: frowand.list@gmail.com, gregkh@linuxfoundation.org, keescook@google.com, kieran.bingham@ideasonboard.com, mcgrof@kernel.org, robh@kernel.org, sboyd@kernel.org, shuah@kernel.org, tytso@mit.edu, yamada.masahiro@socionext.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190513_224402_979211_B432396E X-CRM114-Status: GOOD ( 16.72 ) X-Spam-Score: -7.7 (-------) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-7.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:549 listed in] [list.dnswl.org] -7.5 USER_IN_DEF_DKIM_WL From: address is in the default DKIM white-list -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.0 DKIMWL_WL_MED DKIMwl.org - Medium sender X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pmladek@suse.com, linux-doc@vger.kernel.org, amir73il@gmail.com, Brendan Higgins , dri-devel@lists.freedesktop.org, Alexander.Levin@microsoft.com, linux-kselftest@vger.kernel.org, linux-nvdimm@lists.01.org, khilman@baylibre.com, knut.omang@oracle.com, wfg@linux.intel.com, joel@jms.id.au, rientjes@google.com, jdike@addtoit.com, dan.carpenter@oracle.com, devicetree@vger.kernel.org, linux-kbuild@vger.kernel.org, Tim.Bird@sony.com, linux-um@lists.infradead.org, rostedt@goodmis.org, julia.lawall@lip6.fr, dan.j.williams@intel.com, kunit-dev@googlegroups.com, richard@nod.at, rdunlap@infradead.org, linux-kernel@vger.kernel.org, daniel@ffwll.ch, mpe@ellerman.id.au, linux-fsdevel@vger.kernel.org, logang@deltatee.com Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org A lot of the expectation and assertion infrastructure prints out fairly complicated test failure messages, so add a C++ style log library for for logging test results. Signed-off-by: Brendan Higgins Reviewed-by: Greg Kroah-Hartman Reviewed-by: Logan Gunthorpe --- Changes Since Last Revision: - Greg and Frank requested that KUnit prints out results in TAP format; the work to support this mostly happened in PATCH 01/18. - `kunit_stream_get_level` was made static as suggested by kbuild bot. --- include/kunit/kunit-stream.h | 85 ++++++++++++++++++++ include/kunit/test.h | 3 + kunit/Makefile | 3 +- kunit/kunit-stream.c | 152 +++++++++++++++++++++++++++++++++++ kunit/test.c | 7 ++ 5 files changed, 249 insertions(+), 1 deletion(-) create mode 100644 include/kunit/kunit-stream.h create mode 100644 kunit/kunit-stream.c diff --git a/include/kunit/kunit-stream.h b/include/kunit/kunit-stream.h new file mode 100644 index 0000000000000..d457a54fe0100 --- /dev/null +++ b/include/kunit/kunit-stream.h @@ -0,0 +1,85 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * C++ stream style string formatter and printer used in KUnit for outputting + * KUnit messages. + * + * Copyright (C) 2019, Google LLC. + * Author: Brendan Higgins + */ + +#ifndef _KUNIT_KUNIT_STREAM_H +#define _KUNIT_KUNIT_STREAM_H + +#include +#include + +struct kunit; + +/** + * struct kunit_stream - a std::stream style string builder. + * + * A std::stream style string builder. Allows messages to be built up and + * printed all at once. + */ +struct kunit_stream { + /* private: internal use only. */ + struct kunit *test; + spinlock_t lock; /* Guards level. */ + const char *level; + struct string_stream *internal_stream; +}; + +/** + * kunit_new_stream() - constructs a new &struct kunit_stream. + * @test: The test context object. + * + * Constructs a new test managed &struct kunit_stream. + */ +struct kunit_stream *kunit_new_stream(struct kunit *test); + +/** + * kunit_stream_set_level(): sets the level that string should be printed at. + * @this: the stream being operated on. + * @level: the print level the stream is set to output to. + * + * Sets the print level at which the stream outputs. + */ +void kunit_stream_set_level(struct kunit_stream *this, const char *level); + +/** + * kunit_stream_add(): adds the formatted input to the internal buffer. + * @this: the stream being operated on. + * @fmt: printf style format string to append to stream. + * + * Appends the formatted string, @fmt, to the internal buffer. + */ +void __printf(2, 3) kunit_stream_add(struct kunit_stream *this, + const char *fmt, ...); + +/** + * kunit_stream_append(): appends the contents of @other to @this. + * @this: the stream to which @other is appended. + * @other: the stream whose contents are appended to @this. + * + * Appends the contents of @other to @this. + */ +void kunit_stream_append(struct kunit_stream *this, struct kunit_stream *other); + +/** + * kunit_stream_commit(): prints out the internal buffer to the user. + * @this: the stream being operated on. + * + * Outputs the contents of the internal buffer as a kunit_printk formatted + * output. + */ +void kunit_stream_commit(struct kunit_stream *this); + +/** + * kunit_stream_clear(): clears the internal buffer. + * @this: the stream being operated on. + * + * Clears the contents of the internal buffer. + */ +void kunit_stream_clear(struct kunit_stream *this); + +#endif /* _KUNIT_KUNIT_STREAM_H */ diff --git a/include/kunit/test.h b/include/kunit/test.h index 5e86d88cd5305..4bc839884a83c 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -11,6 +11,7 @@ #include #include +#include struct kunit_resource; @@ -172,6 +173,8 @@ struct kunit { void kunit_init_test(struct kunit *test, const char *name); +void kunit_fail(struct kunit *test, struct kunit_stream *stream); + int kunit_run_tests(struct kunit_module *module); /** diff --git a/kunit/Makefile b/kunit/Makefile index 275b565a0e81f..6ddc622ee6b1c 100644 --- a/kunit/Makefile +++ b/kunit/Makefile @@ -1,2 +1,3 @@ obj-$(CONFIG_KUNIT) += test.o \ - string-stream.o + string-stream.o \ + kunit-stream.o diff --git a/kunit/kunit-stream.c b/kunit/kunit-stream.c new file mode 100644 index 0000000000000..1884f1b550888 --- /dev/null +++ b/kunit/kunit-stream.c @@ -0,0 +1,152 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * C++ stream style string formatter and printer used in KUnit for outputting + * KUnit messages. + * + * Copyright (C) 2019, Google LLC. + * Author: Brendan Higgins + */ + +#include +#include +#include + +static const char *kunit_stream_get_level(struct kunit_stream *this) +{ + unsigned long flags; + const char *level; + + spin_lock_irqsave(&this->lock, flags); + level = this->level; + spin_unlock_irqrestore(&this->lock, flags); + + return level; +} + +void kunit_stream_set_level(struct kunit_stream *this, const char *level) +{ + unsigned long flags; + + spin_lock_irqsave(&this->lock, flags); + this->level = level; + spin_unlock_irqrestore(&this->lock, flags); +} + +void kunit_stream_add(struct kunit_stream *this, const char *fmt, ...) +{ + va_list args; + struct string_stream *stream = this->internal_stream; + + va_start(args, fmt); + + if (string_stream_vadd(stream, fmt, args) < 0) + kunit_err(this->test, "Failed to allocate fragment: %s\n", fmt); + + va_end(args); +} + +void kunit_stream_append(struct kunit_stream *this, + struct kunit_stream *other) +{ + struct string_stream *other_stream = other->internal_stream; + const char *other_content; + + other_content = string_stream_get_string(other_stream); + + if (!other_content) { + kunit_err(this->test, + "Failed to get string from second argument for appending.\n"); + return; + } + + kunit_stream_add(this, other_content); +} + +void kunit_stream_clear(struct kunit_stream *this) +{ + string_stream_clear(this->internal_stream); +} + +void kunit_stream_commit(struct kunit_stream *this) +{ + struct string_stream *stream = this->internal_stream; + struct string_stream_fragment *fragment; + const char *level; + char *buf; + + level = kunit_stream_get_level(this); + if (!level) { + kunit_err(this->test, + "Stream was committed without a specified log level.\n"); + level = KERN_ERR; + kunit_stream_set_level(this, level); + } + + buf = string_stream_get_string(stream); + if (!buf) { + kunit_err(this->test, + "Could not allocate buffer, dumping stream:\n"); + list_for_each_entry(fragment, &stream->fragments, node) { + kunit_err(this->test, fragment->fragment); + } + kunit_err(this->test, "\n"); + goto cleanup; + } + + kunit_printk(level, this->test, buf); + kfree(buf); + +cleanup: + kunit_stream_clear(this); +} + +static int kunit_stream_init(struct kunit_resource *res, void *context) +{ + struct kunit *test = context; + struct kunit_stream *stream; + + stream = kzalloc(sizeof(*stream), GFP_KERNEL); + if (!stream) + return -ENOMEM; + + res->allocation = stream; + stream->test = test; + spin_lock_init(&stream->lock); + stream->internal_stream = new_string_stream(); + + if (!stream->internal_stream) { + kfree(stream); + return -ENOMEM; + } + + return 0; +} + +static void kunit_stream_free(struct kunit_resource *res) +{ + struct kunit_stream *stream = res->allocation; + + if (!string_stream_is_empty(stream->internal_stream)) { + kunit_err(stream->test, + "End of test case reached with uncommitted stream entries.\n"); + kunit_stream_commit(stream); + } + + destroy_string_stream(stream->internal_stream); + kfree(stream); +} + +struct kunit_stream *kunit_new_stream(struct kunit *test) +{ + struct kunit_resource *res; + + res = kunit_alloc_resource(test, + kunit_stream_init, + kunit_stream_free, + test); + + if (res) + return res->allocation; + else + return NULL; +} diff --git a/kunit/test.c b/kunit/test.c index a15e6f8c41582..0bb6f53a7f87e 100644 --- a/kunit/test.c +++ b/kunit/test.c @@ -138,6 +138,13 @@ static void kunit_print_test_case_ok_not_ok(struct kunit_case *test_case, test_case->name); } +void kunit_fail(struct kunit *test, struct kunit_stream *stream) +{ + kunit_set_success(test, false); + kunit_stream_set_level(stream, KERN_ERR); + kunit_stream_commit(stream); +} + void kunit_init_test(struct kunit *test, const char *name) { spin_lock_init(&test->lock);