From patchwork Wed Nov 28 19:36:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Higgins X-Patchwork-Id: 1004794 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="q7fCCYJn"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="Ob6JecNp"; 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 434rY22P9Bz9s3l for ; Thu, 29 Nov 2018 06:38:14 +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=hQJzPsOrvu/72YTNh9H+IXc+mP0SGdtP/88II4PlJ5U=; b=q7fCCYJno6PxvC xlR/qdSgwGtrH4fxNAbp8kwi7BAQWOX/cRKtgRGI9iI9lV/xTpCoAVnPZfKO6O26CvtbrLbrwR5k5 1n3kolVu05+lkTl+zlElClkrmx7Q77nzAlvmS8Cas4Ma9sI7lZa+Biy5yLFtCfoYtz1vsKZjLHBhp YvrOw9NW0J/C5o/C6cRQLQl10J2WKdUTOGr3JMP5Ud51ZrGp4PeTOs5AmCz2L9v9ZLYzJgAJ+GnF5 zCrVNleo3LS0HxsVmjYp2alutNZ9BG9g6sON4nJKhgH81X4sXsSWn827SlvfbJgCdLIjkhcSo6GlT ZdOJJ2zschqNplgHECUg==; 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 1gS5ey-0003cK-1G; Wed, 28 Nov 2018 19:38:08 +0000 Received: from mail-ot1-x34a.google.com ([2607:f8b0:4864:20::34a]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gS5eu-0003WT-RT for linux-um@lists.infradead.org; Wed, 28 Nov 2018 19:38:06 +0000 Received: by mail-ot1-x34a.google.com with SMTP id 62so12558052otr.14 for ; Wed, 28 Nov 2018 11:37:54 -0800 (PST) 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=kYyv/RTHzJjGNg9RbFN3NZsq45WeoGrJwFQsspAHU+g=; b=Ob6JecNpave3zhvIzALx2WdTG2Xf/wGlI/aOCikKVn7jzvpI2ltvh23barswRa5c14 3tqzjK0a9Vpy6Zbhov5vaM+si1RPqBkT/9MgrerDKynQXSQzUxuznSJSHNPUNyHZT48H 59hgN9IqUhaCcN9V18TPDmKbCQ5vEQqLm6tNYBGrgtT9a1KCVXvHLEazCzr/yUXaJttV cl7gzvJIN/tCmINTta3CLCiYXRylhaU6LSgdi+Lfvphl7XAdvF6sS19eKGA0Z2iVRRwz VOgxRJFZc/EFRhs7HF2PqZRVu9yzsTNA6j8cbGWZz5o4izuKO5PcRWTp5xKeBioj4jzC KBkQ== 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=kYyv/RTHzJjGNg9RbFN3NZsq45WeoGrJwFQsspAHU+g=; b=fvrWg7OYnl4qdSiaHHv4/YYprXkrRjjXKda/u3hDa6ClDnNRcDzvI0XkEx6HXFL2DX ARfOWPDwDVMPIDrw9ttrt8Yt9PjI1cXJAvSiu1aeCnLghWEjZ3r2YSgT2qgth8GsI8pc eRuqZtxFFbHGBlLpuKpUEpR7GqsLE4bU/+4Ers2HdOlu2jSADvkD4MMyUCB4umfl1wEs F0y2Tn81hrrJy+z9EwFVkyMgEpqRkQigo68WN2gJ5tUGig7yEScKRqvbhWIfoPqWNpOy yd9VwISnIa6IriQk+RpPcvMgdrvBr+QJEn2SGaoI/ZM8OW/kVh0FOtpA2hhsmg/8SPq5 T/NA== X-Gm-Message-State: AA+aEWa8N53emuPxXdnHVIcSitdTLlASDO0meN9RyMydTmXxPQIUOsjj nfpPNDRoyYMLs1zhN1CF26U9YGYa0z29J46JlSXEig== X-Google-Smtp-Source: AFSGD/VmjDGXZv/Qf9wHBL015DQW6wWAWy/xIyPqNqjwxASmV1d+01uM7VvOaizuli4IdMjUSTMMhsj0NyLNeOs4RvhTsw== X-Received: by 2002:aca:c255:: with SMTP id s82-v6mr4424965oif.50.1543433873527; Wed, 28 Nov 2018 11:37:53 -0800 (PST) Date: Wed, 28 Nov 2018 11:36:21 -0800 In-Reply-To: <20181128193636.254378-1-brendanhiggins@google.com> Message-Id: <20181128193636.254378-5-brendanhiggins@google.com> Mime-Version: 1.0 References: <20181128193636.254378-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.20.0.rc0.387.gc7a69e6b6c-goog Subject: [RFC v3 04/19] kunit: test: add test_stream a std::stream like logger From: Brendan Higgins To: gregkh@linuxfoundation.org, keescook@google.com, mcgrof@kernel.org, shuah@kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181128_113805_031883_40945268 X-CRM114-Status: GOOD ( 15.78 ) 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 ---- ---------------------- -------------------------------------------------- -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_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.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.0 DKIMWL_WL_MED DKIMwl.org - Whitelisted 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: brakmo@fb.com, jdike@addtoit.com, dri-devel@lists.freedesktop.org, linux-kselftest@vger.kernel.org, frowand.list@gmail.com, robh@kernel.org, linux-nvdimm@lists.01.org, richard@nod.at, knut.omang@oracle.com, kieran.bingham@ideasonboard.com, joel@jms.id.au, khilman@baylibre.com, Brendan Higgins , 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, linux-kernel@vger.kernel.org, daniel@ffwll.ch, mpe@ellerman.id.au, joe@perches.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 --- include/kunit/kunit-stream.h | 50 ++++++++++++ include/kunit/test.h | 2 + kunit/Makefile | 3 +- kunit/kunit-stream.c | 153 +++++++++++++++++++++++++++++++++++ kunit/test.c | 8 ++ 5 files changed, 215 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..3b3119450be3f --- /dev/null +++ b/include/kunit/kunit-stream.h @@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * C++ stream style string formatter and printer used in KUnit for outputting + * KUnit messages. + * + * Copyright (C) 2018, 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. + * @set_level: sets the level that this string should be printed at. + * @add: adds the formatted input to the internal buffer. + * @append: adds the contents of other to this. + * @commit: prints out the internal buffer to the user. + * @clear: clears the internal buffer. + * + * A std::stream style string builder. Allows messages to be built up and + * printed all at once. + */ +struct kunit_stream { + void (*set_level)(struct kunit_stream *this, const char *level); + void (*add)(struct kunit_stream *this, const char *fmt, ...); + void (*append)(struct kunit_stream *this, struct kunit_stream *other); + void (*commit)(struct kunit_stream *this); + void (*clear)(struct kunit_stream *this); + /* 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); + +#endif /* _KUNIT_KUNIT_STREAM_H */ diff --git a/include/kunit/test.h b/include/kunit/test.h index 583840e24ffda..ea424095e4fb4 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..70f5182245e0b --- /dev/null +++ b/kunit/kunit-stream.c @@ -0,0 +1,153 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * C++ stream style string formatter and printer used in KUnit for outputting + * KUnit messages. + * + * Copyright (C) 2018, 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; +} + +static 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); +} + +static 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 (stream->vadd(stream, fmt, args) < 0) + kunit_err(this->test, "Failed to allocate fragment: %s", fmt); + + va_end(args); +} + +static 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 = other_stream->get_string(other_stream); + + if (!other_content) { + kunit_err(this->test, + "Failed to get string from second argument for appending."); + return; + } + + this->add(this, other_content); +} + +static void kunit_stream_clear(struct kunit_stream *this) +{ + this->internal_stream->clear(this->internal_stream); +} + +static 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."); + level = KERN_ERR; + this->set_level(this, level); + } + + buf = stream->get_string(stream); + if (!buf) { + kunit_err(this->test, + "Could not allocate buffer, dumping stream:"); + list_for_each_entry(fragment, &stream->fragments, node) { + kunit_err(this->test, fragment->fragment); + } + goto cleanup; + } + + kunit_printk(level, this->test, buf); + kfree(buf); + +cleanup: + this->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; + + stream->set_level = kunit_stream_set_level; + stream->add = kunit_stream_add; + stream->append = kunit_stream_append; + stream->commit = kunit_stream_commit; + stream->clear = kunit_stream_clear; + + return 0; +} + +static void kunit_stream_free(struct kunit_resource *res) +{ + struct kunit_stream *stream = res->allocation; + + if (!stream->internal_stream->is_empty(stream->internal_stream)) { + kunit_err(stream->test, + "End of test case reached with uncommitted stream entries."); + 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 fb1a786e4c94f..abeb939dc7fa2 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); + stream->set_level(stream, KERN_ERR); + 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; }