From patchwork Wed Jul 10 07:14:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Higgins X-Patchwork-Id: 1130207 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="mOQkAm5X"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="IxWYHTc7"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45k9TM4VyNz9s8m for ; Wed, 10 Jul 2019 17:16:07 +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=qzerKCbYmfA5BzT9qYZ/J4qeKoxztYtw3R1P7bfNJR4=; b=mOQkAm5XrcPuIA xDk59i9OsEn53dmiR1hr327behxU6dtOD0FMp7xv1RnDHI6/rXrHVtcOPLDj8muUD3eX1QYlhXb4B VYje+NxOX2wQR0LWwwVW8kMGJRGkXfzZUMs1K5uRoZKR4+MjrQrZjJVMO/dfsFv5qMoLvqlqCvEGS GoIgV4ILmRbBaSBDhxwJeuyg3Rz+3TXLBnLHkZneK1b427BfL4khj4E7S5Uq1WZvzGYpqxherDgrI gYPI/qbsX1BNbp//Isi3s645Q7Csr9X4DHCp+jRjqCk1xvMnlyPwLiiVFy2JnPfmRiB4XHZHS4HOv Sne215qEOlv6KPUh/Hiw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hl6pb-0001Ci-RD; Wed, 10 Jul 2019 07:15:59 +0000 Received: from mail-pg1-x54a.google.com ([2607:f8b0:4864:20::54a]) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1hl6pY-0001B8-M2 for linux-um@lists.infradead.org; Wed, 10 Jul 2019 07:15:58 +0000 Received: by mail-pg1-x54a.google.com with SMTP id d187so920165pga.7 for ; Wed, 10 Jul 2019 00:15:55 -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=pTGx5akQJDhaSZySqpQ1nhnlCVlScw5xa33k0FhGOt8=; b=IxWYHTc7US3jJEJlBSIfA0aESuzH+kXjaX3iXZsQmZUkvs/a6GPHCgR5DAWTzvMmYP c+TMI1KeA4zZ7iaEMPsAAFRIht6hOk5jCHv/BWFH4eKSV8xr6hm0bP5aPUD9F8rGUWA7 8HKl5pdYded1evW3M53CpAJZ/fjaP7cGED1BfcgZb6+mCP+iKfhmmVipH+M3EKYJdD4S MeWYpYqnLXBR7H43JuPbe9JSYcZHocoNDQj4uH+Ds0oTb9srypQqpZ08fII6SfpypVOF XErDAUVYJvMHbWC2A0KfVaSx+HvSEsxHKbyLa022CZWAjHEt+6cKDc4ELjcKxPkh6eIc oiqA== 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=pTGx5akQJDhaSZySqpQ1nhnlCVlScw5xa33k0FhGOt8=; b=TsncsVriIKh4BBxE22VyqI3ca2ujodYQNvUgM+wcQSbSW7mls2dwzyBlzyBnLgAeXh aNJaFiKFmwSG++YbdzPym6c1Jn5tcJzsD04VNJdC66cR5QAT2hJwLx4DxGffgRiTtA3t 2YWxRWR4nmNceuArAPfFtmxxi1s/0JcfZGwMo7AuYU8yV5gqe0KkOgrv+kwsu4ewEaZB CrEDuiKVZs75y1DoI/KJd/ESChKNo4sskV2W5J9ZHzEkeUqgn3K/bi9sbQVs6AC9ACLa S3nC751p/rBt2EHaWlS4cT9Wyw4XTXXEibbHJ232gkwWUG/o7b2B+KFgvezo4NPAqY1j afiQ== X-Gm-Message-State: APjAAAUagkXdWHZdeCq00IcXw/hzGg+jiHYsoZaw9PnmBEwQNAEtsL3e lO6Nd206mslzznC3QUvBH9lbhUPaVj+Pb9mDOcqsbg== X-Google-Smtp-Source: APXvYqx8cA2dNLeA0C6Vho2SScOzHfHVox80Qha+6CSk6cStqQFHHqPA+g4yJ++kIRVzrA9LuPRlGLPzJ0rGnGICxSqX5Q== X-Received: by 2002:a63:f150:: with SMTP id o16mr35695224pgk.105.1562742954282; Wed, 10 Jul 2019 00:15:54 -0700 (PDT) Date: Wed, 10 Jul 2019 00:14:54 -0700 In-Reply-To: <20190710071508.173491-1-brendanhiggins@google.com> Message-Id: <20190710071508.173491-5-brendanhiggins@google.com> Mime-Version: 1.0 References: <20190710071508.173491-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCH v8 04/18] kunit: test: add kunit_stream a std::stream like logger From: Brendan Higgins To: frowand.list@gmail.com, gregkh@linuxfoundation.org, jpoimboe@redhat.com, keescook@google.com, kieran.bingham@ideasonboard.com, mcgrof@kernel.org, peterz@infradead.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-20190710_001556_729261_7F02377C X-CRM114-Status: GOOD ( 16.85 ) X-Spam-Score: -6.4 (------) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-6.4 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:54a listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -7.5 USER_IN_DEF_DKIM_WL From: address is in the default DKIM white-list -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 1.3 PDS_NO_HELO_DNS High profile HELO but no A record -0.0 DKIMWL_WL_MED DKIMwl.org - Medium sender X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.29 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, 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 --- include/kunit/kunit-stream.h | 81 +++++++++++++++++++++++ include/kunit/test.h | 3 + kunit/Makefile | 3 +- kunit/kunit-stream.c | 123 +++++++++++++++++++++++++++++++++++ kunit/test.c | 6 ++ 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..a7b53eabf6be4 --- /dev/null +++ b/include/kunit/kunit-stream.h @@ -0,0 +1,81 @@ +/* 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; + const char *level; + struct string_stream *internal_stream; +}; + +/** + * alloc_kunit_stream() - constructs a new &struct kunit_stream. + * @test: The test context object. + * @level: The log level at which to print out the message. + * + * Constructs a new test managed &struct kunit_stream. + */ +struct kunit_stream *alloc_kunit_stream(struct kunit *test, const char *level); + +/** + * kunit_stream_add(): adds the formatted input to the internal buffer. + * @kstream: 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 *kstream, + const char *fmt, ...); + +/** + * kunit_stream_append(): appends the contents of @other to @kstream. + * @kstream: the stream to which @other is appended. + * @other: the stream whose contents are appended to @kstream. + * + * Appends the contents of @other to @kstream. + */ +void kunit_stream_append(struct kunit_stream *kstream, + struct kunit_stream *other); + +/** + * kunit_stream_commit(): prints out the internal buffer to the user. + * @kstream: the stream being operated on. + * + * Outputs the contents of the internal buffer as a kunit_printk formatted + * output. KUNIT_STREAM ONLY OUTPUTS ITS BUFFER TO THE USER IF COMMIT IS + * CALLED!!! The reason for this is that it allows us to construct a message + * before we know whether we want to print it out; this can be extremely handy + * if there is information you might need for a failure message that is easiest + * to collect in the steps leading up to the actual check. + */ +void kunit_stream_commit(struct kunit_stream *kstream); + +/** + * kunit_stream_clear(): clears the internal buffer. + * @kstream: the stream being operated on. + * + * Clears the contents of the internal buffer. + */ +void kunit_stream_clear(struct kunit_stream *kstream); + +#endif /* _KUNIT_KUNIT_STREAM_H */ diff --git a/include/kunit/test.h b/include/kunit/test.h index bdf41d31c343c..bc7dbdcf8abab 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -11,6 +11,7 @@ #include #include +#include struct kunit_resource; @@ -184,6 +185,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_suite *suite); /** 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..8bea1f22eafb5 --- /dev/null +++ b/kunit/kunit-stream.c @@ -0,0 +1,123 @@ +// 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 + +void kunit_stream_add(struct kunit_stream *kstream, const char *fmt, ...) +{ + va_list args; + struct string_stream *stream = kstream->internal_stream; + + va_start(args, fmt); + + if (string_stream_vadd(stream, fmt, args) < 0) + kunit_err(kstream->test, + "Failed to allocate fragment: %s\n", + fmt); + + va_end(args); +} + +void kunit_stream_append(struct kunit_stream *kstream, + 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(kstream->test, + "Failed to get string from second argument for appending\n"); + return; + } + + kunit_stream_add(kstream, other_content); +} + +void kunit_stream_clear(struct kunit_stream *kstream) +{ + string_stream_clear(kstream->internal_stream); +} + +void kunit_stream_commit(struct kunit_stream *kstream) +{ + struct string_stream *stream = kstream->internal_stream; + struct string_stream_fragment *fragment; + struct kunit *test = kstream->test; + char *buf; + + buf = string_stream_get_string(stream); + if (!buf) { + kunit_err(test, + "Could not allocate buffer, dumping stream:\n"); + list_for_each_entry(fragment, &stream->fragments, node) { + kunit_err(test, fragment->fragment); + } + kunit_err(test, "\n"); + goto cleanup; + } + + kunit_printk(kstream->level, test, buf); + kfree(buf); + +cleanup: + kunit_stream_clear(kstream); +} + +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; + stream->internal_stream = alloc_string_stream(test); + + 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); + } +} + +struct kunit_stream *alloc_kunit_stream(struct kunit *test, const char *level) +{ + struct kunit_stream *kstream; + struct kunit_resource *res; + + res = kunit_alloc_resource(test, + kunit_stream_init, + kunit_stream_free, + test); + + if (!res) + return NULL; + + kstream = res->allocation; + kstream->level = level; + + return kstream; +} diff --git a/kunit/test.c b/kunit/test.c index f165c9d8e10b0..29edf34a89a37 100644 --- a/kunit/test.c +++ b/kunit/test.c @@ -120,6 +120,12 @@ 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_failure(test); + kunit_stream_commit(stream); +} + void kunit_init_test(struct kunit *test, const char *name) { mutex_init(&test->lock);