From patchwork Thu Apr 4 22:06:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Higgins X-Patchwork-Id: 1077768 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="rOX0WkY6"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="ItUauAP5"; 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 44Zxw012mMz9sRX for ; Fri, 5 Apr 2019 09:10:24 +1100 (AEDT) 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=HDI1z9Y9aBFp3a8PAdLb2hFYdKMIfyz/ctFpvHoZ45g=; b=rOX0WkY6gY5KDo OTTNVcujKLSCfzligNsMViaD6OOWaUcufPvR1APg0TtvxVeR5ZBK/LD94mtlYcsoYKz9CkJb7OHC8 feCl170fO9FZU26DtYztk7T9UW0pFwjJjCSkqBoiL1zh8maiFqSwbB0/dFl7e5OyeKCCsyY7Oejko fucMq5Ji0oK4fD3nzv530YCyqit6tooUhd5UHEarzlfFB4RwTo4sTcjaSnV1Sg/GL2VgjM+9IZVvY QxZcL2dCKSC9/DgiLvDp3Tgaio7zBMUwi4T/uhslqKOFRWC5J4ebjzQTa6fW0cU30+xQvFJrjKR0p A760D6g6ruAvZbs7647Q==; 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 1hCAYq-0008Qr-5S; Thu, 04 Apr 2019 22:10:16 +0000 Received: from mail-pl1-x649.google.com ([2607:f8b0:4864:20::649]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hCAYm-0008O6-MF for linux-um@lists.infradead.org; Thu, 04 Apr 2019 22:10:14 +0000 Received: by mail-pl1-x649.google.com with SMTP id m37so2640495plg.22 for ; Thu, 04 Apr 2019 15:10: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=BpmVc/meH/mLCfCvPJn0rY1dGhQCFLBweArh7ihnh1c=; b=ItUauAP50gtoPItqLCryDWw8tQGqyVi3H8AdDlO1y89Dd5yohyEZ4TzGTqGoCsifZk /L2Q8Aml7z3Lk/bar6x5gqzKWKmlntPUdzDLJz5GZXYp1OlChhXAEeekTIIBYq9bZsov l8LXHkz9YV2rf9p2Di5v/LrxeR8zebhSfuJRcF4n9KiRxZZt/C7N1KDcUoOuR8tbq1Ad d6dsTh80xiYO32voKBWrYDt7WqcVIgpQ5+jIoLVEq9E/gjarHQym2e3M200kTl/mdA9X GDhI5J3pdCTOcTLzVNFIR9izU1vp84EyehE5wFFGVJ1sQ2BIvq6VlMjkGcDK/LQVZwJf dPNg== 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=BpmVc/meH/mLCfCvPJn0rY1dGhQCFLBweArh7ihnh1c=; b=BYNFxJYYOdcCTeV+gxgkvnj81lErAn/8iO/EYMTRqqLhpDsQawWwhyPB98450Npdng 8Bzf12yK2IxvKusdKtmbvtah2Jv8azMA3VyKkChFX51iQKBqjs+Dh9FmmQIXKvnNopzy kCFt3Fvvio/jiK7iwuMlC2dOfkAmVMDZdGWlJklTWaui3TCWsyN/koS8/2d0++Eog1Bx +pueEh6XCLeZY2UotiWJ9vgIyaRu/uFVV8GtlsTh5swffAcXVaqAG0GZb6eTSTcPwozL XtRvWAvOwX0OtUKel3YiteyLtUYHwuK1y+gDz7pmypFkG46LD/kiTCIU8MCdEqkIg8Bt 7y4Q== X-Gm-Message-State: APjAAAVV51B1KXk03P8bigEU22E16y7t+tq7EK4Qw5ORyNBRsApcB0c7 yh1IRl91pZCKSvL/NsSd/KwZcNdV9DwXZzGGCUpAqA== X-Google-Smtp-Source: APXvYqw0wnHnP3QfFTtnkvRULuIQlmTgTSvU7oXn40HOrPxHO1CKjz+JK955tzZSMgzAMWwCcLQTVr0GG7k87+1dtrIsVw== X-Received: by 2002:a63:ff65:: with SMTP id s37mr178946pgk.143.1554415811110; Thu, 04 Apr 2019 15:10:11 -0700 (PDT) Date: Thu, 4 Apr 2019 15:06:39 -0700 In-Reply-To: <20190404220652.19765-1-brendanhiggins@google.com> Message-Id: <20190404220652.19765-5-brendanhiggins@google.com> Mime-Version: 1.0 References: <20190404220652.19765-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.21.0.392.gf8f6787159e-goog Subject: [PATCH v1 04/17] kunit: test: add kunit_stream a std::stream like logger From: Brendan Higgins To: corbet@lwn.net, frowand.list@gmail.com, keescook@google.com, kieran.bingham@ideasonboard.com, mcgrof@kernel.org, robh@kernel.org, shuah@kernel.org, yamada.masahiro@socionext.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190404_151012_727420_45CF6424 X-CRM114-Status: GOOD ( 17.23 ) 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:649 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -7.5 USER_IN_DEF_DKIM_WL From: address is in the default DKIM white-list 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -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, 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, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, daniel@ffwll.ch, mpe@ellerman.id.au, linux-fsdevel@vger.kernel.org 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 --- include/kunit/kunit-stream.h | 85 ++++++++++++++++++++ include/kunit/test.h | 2 + kunit/Makefile | 3 +- kunit/kunit-stream.c | 149 +++++++++++++++++++++++++++++++++++ kunit/test.c | 8 ++ 5 files changed, 246 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 819edd8db4e81..4668e8a635954 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -11,6 +11,7 @@ #include #include +#include struct kunit_resource; @@ -171,6 +172,7 @@ struct kunit { void (*vprintk)(const struct kunit *test, const char *level, struct va_format *vaf); + void (*fail)(struct kunit *test, struct kunit_stream *stream); }; int kunit_init_test(struct kunit *test, const char *name); 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..93c14eec03844 --- /dev/null +++ b/kunit/kunit-stream.c @@ -0,0 +1,149 @@ +// 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 + +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) + 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 541f9adb1608c..f7575b127e2df 100644 --- a/kunit/test.c +++ b/kunit/test.c @@ -63,12 +63,20 @@ static void kunit_vprintk(const struct kunit *test, "kunit %s: %pV", test->name, vaf); } +static 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); +} + int kunit_init_test(struct kunit *test, const char *name) { spin_lock_init(&test->lock); INIT_LIST_HEAD(&test->resources); test->name = name; test->vprintk = kunit_vprintk; + test->fail = kunit_fail; return 0; }