tests: prettify JSON in test output and add helper
PATCH 1/1 tests: prettify JSON in test output and add helper
- 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.
--- 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
tests/shell/helpers/json-diff-pretty.sh new file +17 -0
@@ -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 || :
tests/shell/helpers/json-pretty.sh new file +17 -0
@@ -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)) +'
tests/shell/helpers/test-wrapper.sh +4 -0
@@ -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
Signature · 1 line
2.42.0
Pablo Neira Ayuso
maintainerHi,
Quoted 11 lines · Thomas Haller
- add helper script "
json-pretty.sh" for prettify/format JSON. It uses eitherjqor apythonfallback. 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.
One silly question: Does the prettify hightlights the difference?
tests/py clearly shows what is the difference in the JSON diff that quickly helps you identify what is missing.
Thanks!
Quoted 73 lines
Signed-off-by Thomas Haller <[email protected]>--- 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.shtests/shell/helpers/json-diff-pretty.sh new file +17
-0@@ -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 || :tests/shell/helpers/json-pretty.sh new file +17
-0@@ -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)) +'tests/shell/helpers/test-wrapper.sh +4
-0@@ -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 elseSignature · 1 line
2.42.0
Quoted 11 lines · Pablo Neira Ayuso
Hi,
Quoted 8 lines · Thomas Haller
- add helper script "
json-pretty.sh" for prettify/format JSON. It uses eitherjqor apythonfallback. 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.
Quoted 8 lines
Quoted 6 lines
- 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.One silly question: Does the prettify hightlights the difference?
Yes. That that is the idea.
Quoted 3 lines
tests/py clearly shows what is the difference in the JSON diff that quickly helps you identify what is missing.
As always, you will find some result files in /tmp/nft- test.latest.$USER/, which I usually read with
$ grep --color=always ^ -aR /tmp/nft-test.latest.*/ | less -R
there will be a new file there, named "ruleset-diff.json.pretty".
It contains the content of "./tests/shell/helpers/json-diff-pretty.sh" output.
For example:
$ cp tests/shell/testcases/bitwise/dumps/0040mark_binop_2.json-nft tests/shell/testcases/bitwise/dumps/0040mark_binop_3.json-nft $ ./tests/shell/run-tests.sh tests/shell/testcases/bitwise/0040mark_binop_3
leaves a file
/tmp/nft-test.latest.*/test-tests-shell-testcases-bitwise-0040mark_binop_3.1/ruleset-diff.json.pretty
with the following content:
Cmd: "./tests/shell/helpers/json-diff-pretty.sh" "tests/shell/testcases/bitwise/dumps/0040mark_binop_3.json-nft" "/tmp/nft-test.20231122-135045.550.28uKHu/test-tests-shell-testcases-bitwise-0040mark_binop_3.1/ruleset-after.json"
tmp/nft-test.20231122-135045.550.28uKHu/test-tests-shell-testcases-bitwise-0040mark_binop_3.1/ruleset-after.json renamed from shell/testcases/bitwise/dumps/0040mark_binop_3.json-nft +1 -1
@@ -1 +1 @@ -{"nftables": [{"metainfo": {"version": "VERSION", "release_name": "RELEASE_NAME", "json_schema_version": 1}}, {"table": {"family": "ip", "name": "t", "handle": 0}}, {"chain": {"family": "ip", "table": "t", "name": "c", "handle": 0, "type": "filter", "hook": "output", "prio": 0, "policy": "accept"}}, {"rule": {"family": "ip", "table": "t", "chain": "c", "handle": 0, "expr": [{"mangle": {"key": {"ct": {"key": "mark"}}, "value": {"|": [{"<<": [{"payload": {"protocol": "ip", "field": "dscp"}}, 2]}, 16]}}}]}}]} +{"nftables": [{"metainfo": {"version": "VERSION", "release_name": "RELEASE_NAME", "json_schema_version": 1}}, {"table": {"family": "ip", "name": "t", "handle": 0}}, {"chain": {"family": "ip", "table": "t", "name": "c", "handle": 0, "type": "filter", "hook": "input", "prio": 0, "policy": "accept"}}, {"rule": {"family": "ip", "table": "t", "chain": "c", "handle": 0, "expr": [{"mangle": {"key": {"meta": {"key": "mark"}}, "value": {"|": [{"<<": [{"payload": {"protocol": "ip", "field": "dscp"}}, 2]}, 16]}}}]}}]}
dev/fd/62 renamed from dev/fd/63 +2 -2
@@ -21,7 +21,7 @@ "name": "c", "handle": 0, "type": "filter", - "hook": "output", + "hook": "input", "prio": 0, "policy": "accept" }
@@ -36,7 +36,7 @@ { "mangle": { "key": { - "ct": { + "meta": { "key": "mark" } },
Pablo Neira Ayuso
maintainerOn Wed, Nov 22, 2023 at 01:55:57PM +0100, Thomas Haller wrote:
[...]
Quoted 13 lines
As always, you will find some result files in
/tmp/nft-test.latest.$USER/, which I usually read with$ grep --color=always ^ -aR /tmp/nft-test.latest.*/ | less -Rthere will be a new file there, named "ruleset-diff.json.pretty".
It contains the content of "
./tests/shell/helpers/json-diff-pretty.sh" output.For example:
Quoted 7 lines
$ cp tests/shell/testcases/bitwise/dumps/0040mark_binop_2.json-nft tests/shell/testcases/bitwise/dumps/0040mark_binop_3.json-nft $ ./tests/shell/run-tests.sh tests/shell/testcases/bitwise/0040mark_binop_3leaves a file
/tmp/nft-test.latest.*/test-tests-shell-testcases-bitwise-0040mark_binop_3.1/ruleset-diff.json.pretty
Thanks for explaining, this is now applied.
And please, add copy and paste this info to the README file (unless I am overlooking anything, it is not yet there).
Quoted 1 line · Pablo Neira Ayuso
Thanks for explaining, this is now applied.
Thank you.
Quoted 3 lines
And please, add copy and paste this info to the README file (unless I am overlooking anything, it is not yet there).
I hear you. It will still happen!!
Thomas