]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
tests/shell: add feature probing via "features/*.nft" files
authorThomas Haller <thaller@redhat.com>
Fri, 15 Sep 2023 15:32:35 +0000 (17:32 +0200)
committerFlorian Westphal <fw@strlen.de>
Sat, 16 Sep 2023 12:43:37 +0000 (14:43 +0200)
Running selftests on older kernels makes some of them fail very early
because some tests use features that are not available on older kernels,
e.g. -stable releases.

Known examples:
- inner header matching
- anonymous chains
- elem delete from packet path

Also, some test cases might fail because a feature isn't compiled in,
such as netdev chains.

This adds a feature-probing mechanism to shell tests.

Simply drop a 'nft -f' compatible file with a .nft suffix into
"tests/shell/features". "run-tests.sh" will load it via `nft --check`
and will export

  NFT_TEST_HAVE_${feature}=y|n

Here ${feature} is the basename of the .nft file without file extension.
It must be all lower-case.

This extends the existing NFT_TEST_HAVE_json= feature detection.
Similarly, NFT_TEST_REQUIRES(NFT_TEST_HAVE_*) tags work to easily skip a
test.

The test script that cannot fully work without the feature should either
skip the test entirely (NFT_TEST_REQUIRES(NFT_TEST_HAVE_*)), or run a
reduced/modified test. If a modified test was run and passes, it is
still a good idea to mark the overall result as skipped (exit 77)
instead of claiming success to the modified test. We want to know when
not the full test was running, while we want to test as much as we can.

This patch is based on Florian's feature probing patch.

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

index c5d6307d067e2d7fef209c5b05750f1673893d8a..01a312d0ee2cc18f0e6c5276f3dfc928a21c75e7 100755 (executable)
@@ -222,6 +222,23 @@ NFT_TEST_BASEDIR="$(dirname "$0")"
 export NFT_TEST_BASEDIR
 
 _HAVE_OPTS=( json )
+_HAVE_OPTS_NFT=()
+shopt -s nullglob
+F=( "$NFT_TEST_BASEDIR/features/"*.nft )
+shopt -u nullglob
+for file in "${F[@]}"; do
+       feat="${file##*/}"
+       feat="${feat%.nft}"
+       re="^[a-z_0-9]+$"
+       if [[ "$feat" =~ $re ]] && ! array_contains "$feat" "${_HAVE_OPTS[@]}" ; then
+               _HAVE_OPTS_NFT+=( "$feat" )
+       else
+               msg_warn "Ignore feature file \"$file\""
+       fi
+done
+_HAVE_OPTS+=( "${_HAVE_OPTS_NFT[@]}" )
+_HAVE_OPTS=( $(printf '%s\n' "${_HAVE_OPTS[@]}" | LANG=C sort) )
+
 for KEY in $(compgen -v | grep '^NFT_TEST_HAVE_' | sort) ; do
        if ! array_contains "${KEY#NFT_TEST_HAVE_}" "${_HAVE_OPTS[@]}" ; then
                unset "$KEY"
@@ -477,6 +494,17 @@ else
 fi
 export NFT_TEST_HAVE_json
 
+for feat in "${_HAVE_OPTS_NFT[@]}" ; do
+       var="NFT_TEST_HAVE_$feat"
+       if [ -z "${!var+x}" ] ; then
+               val='y'
+               $NFT_TEST_UNSHARE_CMD "$NFT_REAL" --check -f "$NFT_TEST_BASEDIR/features/$feat.nft" &>/dev/null || val='n'
+       else
+               val="$(bool_n "${!var}")"
+       fi
+       eval "export $var=$val"
+done
+
 if [ "$NFT_TEST_JOBS" -eq 0 ] ; then
        MODPROBE="$(which modprobe)"
        if [ ! -x "$MODPROBE" ] ; then