]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
tests: monitor: Test JSON echo mode as well
authorPhil Sutter <phil@nwl.cc>
Wed, 3 Sep 2025 13:30:31 +0000 (15:30 +0200)
committerPhil Sutter <phil@nwl.cc>
Thu, 4 Sep 2025 15:07:18 +0000 (17:07 +0200)
Reuse the expected JSON monitor output for --echo testing as it is
supposed to be "identical" - apart from formatting differences. To match
lines of commands (monitor output) against a single line of JSON object
(echo output), join the former's lines and drop the surrounding object
in the latter since this seems to be the simplest way.

Signed-off-by: Phil Sutter <phil@nwl.cc>
tests/monitor/run-tests.sh

index 4cbdee587f47c40f10bf31f8b3b1cf8d3cd214df..38c20adb1dc616a1188fb85206d1e4f433e6b178 100755 (executable)
@@ -52,7 +52,7 @@ echo_output_append() {
                grep '^\(add\|replace\|insert\)' $command_file >>$output_file
                return
        }
-       [[ "$*" =~ ^(add|replace|insert) ]] && echo "$*" >>$output_file
+       [[ "$*" =~ ^(\{\")?(add|replace|insert) ]] && echo "$*" >>$output_file
 }
 json_output_filter() { # (filename)
        # unify handle values
@@ -96,16 +96,32 @@ monitor_run_test() {
 
 echo_run_test() {
        echo_output=$(mktemp -p $testdir)
+       echo_args="-nn -e"
+       $test_json && echo_args+=" -j"
        local rc=0
 
        $debug && {
                echo "command file:"
                cat $command_file
        }
-       $nft -nn -e -f - <$command_file >$echo_output || {
+       $nft $echo_args -f - <$command_file >$echo_output || {
                err "nft command failed!"
                rc=1
        }
+       if $test_json; then
+               # Extract commands from the surrounding JSON object
+               sed -i -e 's/^{"nftables": \[//' -e 's/\]}$//' $echo_output
+               json_output_filter $echo_output
+
+               # Replace newlines by ", " in output file
+               readarray -t output_file_lines <$output_file
+               sep=""
+               for ((i = 0; i < ${#output_file_lines[*]}; i++)); do
+                       printf "${sep}${output_file_lines[$i]}"
+                       sep=", "
+               done >$output_file
+               [ $i -gt 0 ] && echo "" >>$output_file
+       fi
        mydiff -q $echo_output $output_file >/dev/null 2>&1
        if [[ $rc == 0 && $? != 0 ]]; then
                err "echo output differs!"
@@ -159,12 +175,7 @@ while [ -n "$1" ]; do
        esac
 done
 
-if $test_json; then
-       variants="monitor"
-else
-       variants="monitor echo"
-fi
-
+variants="monitor echo"
 rc=0
 for variant in $variants; do
        run_test=${variant}_run_test