]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
test: shell: Introduce $NFT_TEST_LIBRARY_FILE, helper/lib.sh
authorYi Chen <yiche@redhat.com>
Sun, 22 Jun 2025 12:55:52 +0000 (20:55 +0800)
committerFlorian Westphal <fw@strlen.de>
Sun, 22 Jun 2025 13:26:49 +0000 (15:26 +0200)
Consolidate frequently used functions in helper/lib.sh
switch nat_ftp and flowtables to use it.

Signed-off-by: Yi Chen <yiche@redhat.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
tests/shell/helpers/lib.sh [new file with mode: 0755]
tests/shell/helpers/test-wrapper.sh
tests/shell/testcases/packetpath/flowtables
tests/shell/testcases/packetpath/nat_ftp

diff --git a/tests/shell/helpers/lib.sh b/tests/shell/helpers/lib.sh
new file mode 100755 (executable)
index 0000000..d2d2098
--- /dev/null
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+# contains utility functions commonly used in tests.
+
+assert_pass()
+{
+       local ret=$?
+       if [ $ret != 0 ]; then
+               echo "FAIL: ${@}"
+               if type -t assert_failout; then
+                       assert_failout
+               fi
+               exit 1
+       else
+               echo "PASS: ${@}"
+       fi
+}
+assert_fail()
+{
+       local ret=$?
+       if [ $ret == 0 ]; then
+               echo "FAIL: ${@}"
+               if type -t assert_failout; then
+                       assert_failout
+               fi
+               exit 1
+       else
+               echo "PASS: ${@}"
+       fi
+}
index 4a7e8b7b9e7d73a1c64369fb0235861e4fbb7d9d..6ec4e0301ca4ad4835f99d4adfa931af7398a18f 100755 (executable)
@@ -36,6 +36,7 @@ TESTDIR="$(dirname "$TEST")"
 START_TIME="$(cut -d ' ' -f1 /proc/uptime)"
 
 export TMPDIR="$NFT_TEST_TESTTMPDIR"
+export NFT_TEST_LIBRARY_FILE="$NFT_TEST_BASEDIR/helpers/lib.sh"
 
 CLEANUP_UMOUNT_VAR_RUN=n
 
index ab11431f93a5062f3f7ad17faf83dee4cccf00f7..f3580a5fb9f0bcd65bc5dcdc4417ff480bcf7f32 100755 (executable)
@@ -3,6 +3,8 @@
 # NFT_TEST_REQUIRES(NFT_TEST_HAVE_socat)
 # NFT_TEST_SKIP(NFT_TEST_SKIP_slow)
 
+. $NFT_TEST_LIBRARY_FILE
+
 rnd=$(mktemp -u XXXXXXXX)
 R="flowtable-router-$rnd"
 C="flowtable-client-$rnd"
@@ -17,29 +19,10 @@ cleanup()
 }
 trap cleanup EXIT
 
-assert_pass()
-{
-       local ret=$?
-       if [ $ret != 0 ]
-       then
-               echo "FAIL: ${@}"
-               ip netns exec $R cat /proc/net/nf_conntrack
-               exit 1
-       else
-               echo "PASS: ${@}"
-       fi
-}
-assert_fail()
+# Call back when assertion fails.
+assert_failout()
 {
-       local ret=$?
-       if [ $ret == 0 ]
-       then
-               echo "FAIL: ${@}"
-               ip netns exec $R cat /proc/net/nf_conntrack
-               exit 1
-       else
-               echo "PASS: ${@}"
-       fi
+       ip netns exec $R cat /proc/net/nf_conntrack
 }
 
 ip netns add $R
index 1eb4714a6e5c816b04b7e37d12d6dfecc4af4f89..2a80c43f6fc53abbb25703e2c904a5e7afdc8cfe 100755 (executable)
@@ -4,6 +4,8 @@
 # NFT_TEST_REQUIRES(NFT_TEST_HAVE_curl)
 # NFT_TEST_REQUIRES(NFT_TEST_HAVE_vsftpd)
 
+. $NFT_TEST_LIBRARY_FILE
+
 cleanup()
 {
        for i in $R $C $S;do
@@ -14,22 +16,14 @@ cleanup()
 }
 trap cleanup EXIT
 
-assert_pass()
+assert_failout()
 {
-       local ret=$?
-       if [ $ret != 0 ]
-       then
-               echo "FAIL: ${@}"
-               ip netns exec $R $NFT list ruleset
-               tcpdump -nnr ${PCAP}
-               test -r /proc/net/nf_conntrack && ip netns exec $R cat /proc/net/nf_conntrack
-               ip netns exec $R conntrack -S
-               ip netns exec $R conntrack -L
-               ip netns exec $S ss -nitepal
-               exit 1
-       else
-               echo "PASS: ${@}"
-       fi
+       ip netns exec $R $NFT list ruleset
+       tcpdump -nnr ${PCAP}
+       test -r /proc/net/nf_conntrack && ip netns exec $R cat /proc/net/nf_conntrack
+       ip netns exec $R conntrack -S
+       ip netns exec $R conntrack -L
+       ip netns exec $S ss -nitepal
 }
 
 rnd=$(mktemp -u XXXXXXXX)