tests: shell: drop metainfo from the json dump comparison
PATCH 1/1 tests: shell: drop metainfo from the json dump comparison
The comparison reads nothing from the metainfo object, but 431 dumps carry one, so bumping json_schema_version would mean regenerating all of them.
Add a helper that drops the object and apply it to both sides of the dump diff. Nothing else changes: json-pretty.sh stays as it is, so what nft emits, what nft -j --check is fed and what DUMPGEN writes are untouched.
Nothing then asserts that nft still emits metainfo, so add a test. It also pins that the jq and python3 branches agree and that both reject a malformed ruleset, which a host with jq never exercises.
Suggested-by Phil Sutter <[email protected]>
Signed-off-by Avinash Duduskar <[email protected]>
--- tests/shell/helpers/json-strip-metainfo.sh | 39 +++++++++++ tests/shell/helpers/test-wrapper.sh | 24 ++++++- tests/shell/testcases/json/0009metainfo_0 | 69 +++++++++++++++++++ .../json/dumps/0009metainfo_0.nodump | 0 4 files changed, 129 insertions(+), 3 deletions(-) create mode 100755 tests/shell/helpers/json-strip-metainfo.sh create mode 100755 tests/shell/testcases/json/0009metainfo_0 create mode 100644 tests/shell/testcases/json/dumps/0009metainfo_0.nodump
tests/shell/helpers/json-strip-metainfo.sh new file +39 -0
@@ -0,0 +1,39 @@ +#!/bin/bash -e + +exec_strip() { + # `jq` and the python3 fallback must produce the same output. Both + # reject a non-array "nftables" rather than reshaping it. They are not + # equivalent on every malformed input, only on what the test pins, and + # only nft's own output reaches this. + + if command -v jq &>/dev/null ; then + exec jq ' + if (.nftables | type) != "array" then + error("nftables is not an array") + else + .nftables |= map(select(has("metainfo") | not)) + end' + fi + + # Fallback to python3. + exec python3 -c ' +import json +import sys + +parsed = json.load(sys.stdin) +if not isinstance(parsed.get("nftables"), list): + sys.exit("nftables is not an array") +parsed["nftables"] = [x for x in parsed["nftables"] if "metainfo" not in x] +print(json.dumps(parsed, indent=2)) +' +} + +[ "$#" -le 1 ] || { echo "At most one argument supported" ; exit 1 ; } + +if [ "$#" -eq 1 ] ; then + # One argument passed. This must be a JSON file. + [ -f "$1" ] || { echo "File \"$1\" does not exist" ; exit 1 ; } + exec_strip < "$1" +fi + +exec_strip
tests/shell/helpers/test-wrapper.sh +21 -3
@@ -54,6 +54,12 @@ json_pretty() { "$NFT_TEST_BASEDIR/helpers/json-pretty.sh" "$@" 2>&1 || : } +json_strip_metainfo() { + # No `|| :` here. A failure must fail the dump check, not silently + # leave a truncated file for the diff to compare. + "$NFT_TEST_BASEDIR/helpers/json-strip-metainfo.sh" "$@" +} + TEST="$1" TESTBASE="$(basename "$TEST")" TESTDIR="$(dirname "$TEST")"
@@ -228,11 +234,23 @@ if [ "$rc_test" -ne 77 -a "$dump_written" != y ] ; then diff_check_setcount "$DUMPFILE" "$NFT_TEST_TESTTMPDIR/ruleset-after" fi if [ "$NFT_TEST_HAVE_json" != n -a -f "$JDUMPFILE" ] ; then - if ! $DIFF -u "$JDUMPFILE" "$NFT_TEST_TESTTMPDIR/ruleset-after.json-pretty" &> "$NFT_TEST_TESTTMPDIR/ruleset-diff.json" ; then - show_file "$NFT_TEST_TESTTMPDIR/ruleset-diff.json" "Failed \`$DIFF -u \"$JDUMPFILE\" \"$NFT_TEST_TESTTMPDIR/ruleset-after.json-pretty\"\`" >> "$NFT_TEST_TESTTMPDIR/rc-failed-dump" + # Drop metainfo from both sides: the comparison ignores it, so + # a json_schema_version bump needs no dump regeneration. + rc_strip=0 + json_strip_metainfo "$JDUMPFILE" \ + > "$NFT_TEST_TESTTMPDIR/dump-expected.json-stripped" \ + 2> "$NFT_TEST_TESTTMPDIR/strip-err" || rc_strip=1 + json_strip_metainfo "$NFT_TEST_TESTTMPDIR/ruleset-after.json-pretty" \ + > "$NFT_TEST_TESTTMPDIR/ruleset-after.json-stripped" \ + 2>> "$NFT_TEST_TESTTMPDIR/strip-err" || rc_strip=1 + if [ "$rc_strip" -ne 0 ] ; then + show_file "$NFT_TEST_TESTTMPDIR/strip-err" "Command \`json-strip-metainfo.sh\` failed" >> "$NFT_TEST_TESTTMPDIR/rc-failed-dump" + rc_dump=1 + elif ! $DIFF -u "$NFT_TEST_TESTTMPDIR/dump-expected.json-stripped" "$NFT_TEST_TESTTMPDIR/ruleset-after.json-stripped" &> "$NFT_TEST_TESTTMPDIR/ruleset-diff.json" ; then + show_file "$NFT_TEST_TESTTMPDIR/ruleset-diff.json" "Failed \`$DIFF -u \"$NFT_TEST_TESTTMPDIR/dump-expected.json-stripped\" \"$NFT_TEST_TESTTMPDIR/ruleset-after.json-stripped\"\` (metainfo stripped from \"$JDUMPFILE\" and the live dump)" >> "$NFT_TEST_TESTTMPDIR/rc-failed-dump" rc_dump=1 else - rm -f "$NFT_TEST_TESTTMPDIR/ruleset-diff.json" + rm -f "$NFT_TEST_TESTTMPDIR/ruleset-diff.json" "$NFT_TEST_TESTTMPDIR/strip-err" fi fi fi
tests/shell/testcases/json/0009metainfo_0 new file +69 -0
69 lines not on this page · raw message
tests/shell/testcases/json/dumps/0009metainfo_0.nodump new file +0 -0
base-commit: fcef13a358a0755a0bd980910d47989b064a756d
Signature · 1 line
2.55.0