]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
tests/shell: rework command line parsing in "run-tests.sh"
authorThomas Haller <thaller@redhat.com>
Wed, 6 Sep 2023 11:52:04 +0000 (13:52 +0200)
committerFlorian Westphal <fw@strlen.de>
Thu, 7 Sep 2023 17:35:20 +0000 (19:35 +0200)
Parse the arguments in a loop, so that their order does not matter.
Also, soon more command line arguments will be added, and this way of
parsing seems more maintainable and flexible.

Currently this is still after the is-root check and after unshare. That
will be addressed later.

Signed-off-by: Thomas Haller <thaller@redhat.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
tests/shell/run-tests.sh

index b66ef4fa4d1f31ffed7926e15ffc5906fd899af1..a15764935ec6a0805cd95d3e169a02ad7c3780dd 100755 (executable)
@@ -1,10 +1,5 @@
 #!/bin/bash
 
-# Configuration
-TESTDIR="./$(dirname $0)/testcases"
-SRC_NFT="$(dirname $0)/../../src/nft"
-DIFF=$(which diff)
-
 msg_error() {
        echo "E: $1 ..." >&2
        exit 1
@@ -18,6 +13,32 @@ msg_info() {
        echo "I: $1"
 }
 
+usage() {
+       echo " $0 [OPTIONS]"
+       echo
+       echo "OPTIONS:"
+       echo " -h|--help : Print usage."
+       echo " -v        : Sets VERBOSE=y."
+       echo " -g        : Sets DUMPGEN=y."
+       echo " -V        : Sets VALGRIND=y."
+       echo " -K        : Sets KMEMLEAK=y."
+       echo
+       echo "ENVIRONMENT VARIABLES:"
+       echo " NFT=<CMD>    : Path to nft executable. Will be called as \`\$NFT [...]\` so"
+       echo "                it can be a command with parameters. Note that in this mode quoting"
+       echo "                does not work, so the usage is limited and the command cannot contain"
+       echo "                spaces."
+       echo " VERBOSE=*|y  : Enable verbose output."
+       echo " DUMPGEN=*|y  : Regenerate dump files."
+       echo " VALGRIND=*|y : Run \$NFT in valgrind."
+       echo " KMEMLEAK=*|y : Check for kernel memleaks."
+}
+
+# Configuration
+TESTDIR="./$(dirname $0)/testcases"
+SRC_NFT="$(dirname $0)/../../src/nft"
+DIFF=$(which diff)
+
 if [ "$(id -u)" != "0" ] ; then
        msg_error "this requires root!"
 fi
@@ -31,6 +52,48 @@ if [ "${1}" != "run" ]; then
 fi
 shift
 
+VERBOSE="$VERBOSE"
+DUMPGEN="$DUMPGEN"
+VALGRIND="$VALGRIND"
+KMEMLEAK="$KMEMLEAK"
+
+TESTS=()
+
+while [ $# -gt 0 ] ; do
+       A="$1"
+       shift
+       case "$A" in
+               -v)
+                       VERBOSE=y
+                       ;;
+               -g)
+                       DUMPGEN=y
+                       ;;
+               -V)
+                       VALGRIND=y
+                       ;;
+               -K)
+                       KMEMLEAK=y
+                       ;;
+               -h|--help)
+                       usage
+                       exit 0
+                       ;;
+               --)
+                       TESTS+=( "$@" )
+                       shift $#
+                       ;;
+               *)
+                       # Any unrecognized option is treated as a test name, and also
+                       # enable verbose tests.
+                       TESTS+=( "$A" )
+                       VERBOSE=y
+                       ;;
+       esac
+done
+
+SINGLE="${TESTS[*]}"
+
 [ -z "$NFT" ] && NFT=$SRC_NFT
 ${NFT} > /dev/null 2>&1
 ret=$?
@@ -59,31 +122,6 @@ if [ ! -x "$DIFF" ] ; then
        DIFF=true
 fi
 
-if [ "$1" == "-v" ] ; then
-       VERBOSE=y
-       shift
-fi
-
-if [ "$1" == "-g" ] ; then
-       DUMPGEN=y
-       shift
-fi
-
-if [ "$1" == "-V" ] ; then
-       VALGRIND=y
-       shift
-fi
-
-if [ "$1" == "-K" ]; then
-       KMEMLEAK=y
-       shift
-fi
-
-for arg in "$@"; do
-       SINGLE+=" $arg"
-       VERBOSE=y
-done
-
 kernel_cleanup() {
        $NFT flush ruleset
        $MODPROBE -raq \