netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Haller <thaller@redhat.com>
To: NetFilter <netfilter-devel@vger.kernel.org>
Cc: Thomas Haller <thaller@redhat.com>
Subject: [PATCH nft 1/1] tests: prettify JSON in test output and add helper
Date: Wed, 22 Nov 2023 12:19:40 +0100	[thread overview]
Message-ID: <20231122111946.439474-1-thaller@redhat.com> (raw)

- add helper script "json-pretty.sh" for prettify/format JSON.
  It uses either `jq` or a `python` fallback. In my tests, they
  produce the same output, but the output is not guaranteed to be
  stable. This is mainly for informational purpose.

- add a "json-diff-pretty.sh" which prettifies two JSON inputs and
  shows a diff of them.

- in "test-wrapper.sh", after the check for a .json-nft dump fails, also
  call "json-diff-pretty.sh" and write the output to "ruleset-diff.json.pretty".
  This is beside "ruleset-diff.json", which contains the original diff.

Signed-off-by: Thomas Haller <thaller@redhat.com>
---
 tests/shell/helpers/json-diff-pretty.sh | 17 +++++++++++++++++
 tests/shell/helpers/json-pretty.sh      | 17 +++++++++++++++++
 tests/shell/helpers/test-wrapper.sh     |  4 ++++
 3 files changed, 38 insertions(+)
 create mode 100755 tests/shell/helpers/json-diff-pretty.sh
 create mode 100755 tests/shell/helpers/json-pretty.sh

diff --git a/tests/shell/helpers/json-diff-pretty.sh b/tests/shell/helpers/json-diff-pretty.sh
new file mode 100755
index 000000000000..bebb7e8ed006
--- /dev/null
+++ b/tests/shell/helpers/json-diff-pretty.sh
@@ -0,0 +1,17 @@
+#!/bin/bash -e
+
+BASEDIR="$(dirname "$0")"
+
+[ $# -eq 2 ] || (echo "$0: expects two JSON files as arguments" ; exit 1)
+
+FILE1="$1"
+FILE2="$2"
+
+pretty()
+{
+	"$BASEDIR/json-pretty.sh" < "$1" 2>&1 || :
+}
+
+echo "Cmd: \"$0\" \"$FILE1\" \"$FILE2\""
+diff -u "$FILE1" "$FILE2" 2>&1 || :
+diff -u <(pretty "$FILE1") <(pretty "$FILE2") 2>&1 || :
diff --git a/tests/shell/helpers/json-pretty.sh b/tests/shell/helpers/json-pretty.sh
new file mode 100755
index 000000000000..0d6972b81e2f
--- /dev/null
+++ b/tests/shell/helpers/json-pretty.sh
@@ -0,0 +1,17 @@
+#!/bin/bash -e
+
+# WARNING: the output is not guaranteed to be stable.
+
+if command -v jq &>/dev/null ; then
+	# If we have, use `jq`
+	exec jq
+fi
+
+# Fallback to python.
+exec python -c '
+import json
+import sys
+
+parsed = json.load(sys.stdin)
+print(json.dumps(parsed, indent=2))
+'
diff --git a/tests/shell/helpers/test-wrapper.sh b/tests/shell/helpers/test-wrapper.sh
index 9e8e60581890..4ffc48184dd7 100755
--- a/tests/shell/helpers/test-wrapper.sh
+++ b/tests/shell/helpers/test-wrapper.sh
@@ -202,6 +202,10 @@ if [ "$rc_test" -ne 77 -a "$dump_written" != y ] ; then
 	fi
 	if [ "$NFT_TEST_HAVE_json" != n -a -f "$JDUMPFILE" ] ; then
 		if ! $DIFF -u "$JDUMPFILE" "$NFT_TEST_TESTTMPDIR/ruleset-after.json" &> "$NFT_TEST_TESTTMPDIR/ruleset-diff.json" ; then
+			"$NFT_TEST_BASEDIR/helpers/json-diff-pretty.sh" \
+				"$JDUMPFILE" \
+				"$NFT_TEST_TESTTMPDIR/ruleset-after.json" \
+				2>&1 > "$NFT_TEST_TESTTMPDIR/ruleset-diff.json.pretty"
 			show_file "$NFT_TEST_TESTTMPDIR/ruleset-diff.json" "Failed \`$DIFF -u \"$JDUMPFILE\" \"$NFT_TEST_TESTTMPDIR/ruleset-after.json\"\`" >> "$NFT_TEST_TESTTMPDIR/rc-failed-dump"
 			rc_dump=1
 		else
-- 
2.42.0


             reply	other threads:[~2023-11-22 11:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-22 11:19 Thomas Haller [this message]
2023-11-22 12:33 ` [PATCH nft 1/1] tests: prettify JSON in test output and add helper Pablo Neira Ayuso
2023-11-22 12:55   ` Thomas Haller
2023-11-22 17:36     ` Pablo Neira Ayuso
2023-11-22 18:23       ` Thomas Haller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231122111946.439474-1-thaller@redhat.com \
    --to=thaller@redhat.com \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).