]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
tests: monitor: Excercise all syntaxes and variants by default
authorPhil Sutter <phil@nwl.cc>
Wed, 3 Sep 2025 15:41:23 +0000 (17:41 +0200)
committerPhil Sutter <phil@nwl.cc>
Thu, 11 Sep 2025 16:11:53 +0000 (18:11 +0200)
Introduce -s/--standard flag to restrict execution to standard syntax
and let users select a specific variant by means of -e/--echo and
-m/--monitor flags. Run all four possible combinations by default.

To keep indenting sane, introduce run_testcase() executing tests in a
single test case for a given syntax and variant.

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

index b09b72ae034cb0e2b2dec41920ecdea1eaf1867f..32b1b86e0cc6e9f363bc64b7b9bd62984ca5c696 100755 (executable)
@@ -154,14 +154,28 @@ if $netns; then
 fi
 
 testcases=""
+variants=""
+syntaxes=""
 while [ -n "$1" ]; do
        case "$1" in
        -d|--debug)
                debug=true
                shift
                ;;
+       -s|--standard)
+               syntaxes+=" standard"
+               shift
+               ;;
        -j|--json)
-               test_json=true
+               syntaxes+=" json"
+               shift
+               ;;
+       -e|--echo)
+               variants+=" echo"
+               shift
+               ;;
+       -m|--monitor)
+               variants+=" monitor"
                shift
                ;;
        --no-netns)
@@ -179,64 +193,74 @@ while [ -n "$1" ]; do
                echo "unknown option '$1'"
                ;&
        -h|--help)
-               echo "Usage: $(basename $0) [-j|--json] [-d|--debug] [testcase ...]"
+               echo "Usage: $(basename $0) [(-e|--echo)|(-m|--monitor)] [(-j|--json)|(-s|--standard)] [-d|--debug] [testcase ...]"
                exit 1
                ;;
        esac
 done
 
-variants="monitor echo"
-rc=0
-for variant in $variants; do
-       run_test=${variant}_run_test
-       output_append=${variant}_output_append
-
-       for testcase in ${testcases:-testcases/*.t}; do
-               filename=$(basename $testcase)
-               echo "$variant: running tests from file $filename"
-               rc_start=$rc
-
-               # files are like this:
-               #
-               # I add table ip t
-               # O add table ip t
-               # I add chain ip t c
-               # O add chain ip t c
-
-               $nft flush ruleset
-
-               input_complete=false
-               while read dir line; do
-                       case $dir in
-                       I)
-                               $input_complete && {
-                                       $run_test
-                                       let "rc += $?"
-                               }
-                               input_complete=false
-                               cmd_append "$line"
-                               ;;
-                       O)
-                               input_complete=true
-                               $test_json || $output_append "$line"
-                               ;;
-                       J)
-                               input_complete=true
-                               $test_json && $output_append "$line"
-                               ;;
-                       '#'|'')
-                               # ignore comments and empty lines
-                               ;;
-                       esac
-               done <$testcase
-               $input_complete && {
-                       $run_test
-                       let "rc += $?"
-               }
-
-               let "rc_diff = rc - rc_start"
-               [[ $rc_diff -ne 0 ]] && \
-                       echo "$variant: $rc_diff tests from file $filename failed"
+# run the single test in $1
+# expect $variant and $test_json to be set appropriately
+run_testcase() {
+       testcase="$1"
+       filename=$(basename $testcase)
+       rc=0
+       $test_json && printf "json-"
+       echo "$variant: running tests from file $filename"
+
+       # files are like this:
+       #
+       # I add table ip t
+       # O add table ip t
+       # I add chain ip t c
+       # O add chain ip t c
+
+       $nft flush ruleset
+
+       input_complete=false
+       while read dir line; do
+               case $dir in
+               I)
+                       $input_complete && {
+                               ${variant}_run_test
+                               $run_test
+                               let "rc += $?"
+                       }
+                       input_complete=false
+                       cmd_append "$line"
+                       ;;
+               O)
+                       input_complete=true
+                       $test_json || ${variant}_output_append "$line"
+                       ;;
+               J)
+                       input_complete=true
+                       $test_json && ${variant}_output_append "$line"
+                       ;;
+               '#'|'')
+                       # ignore comments and empty lines
+                       ;;
+               esac
+       done <$testcase
+       $input_complete && {
+               ${variant}_run_test
+               let "rc += $?"
+       }
+
+       [[ $rc -ne 0 ]] && \
+               echo "$variant: $rc tests from file $filename failed"
+       return $rc
+}
+
+total_rc=0
+for syntax in ${syntaxes:-standard json}; do
+       [ $syntax == json ] && test_json=true || test_json=false
+       for variant in ${variants:-echo monitor}; do
+               for testcase in ${testcases:-testcases/*.t}; do
+                       run_testcase "$testcase"
+                       let "total_rc += $?"
+               done
        done
 done
-exit $rc
+
+exit $total_rc