From: Yi Chen Date: Sun, 22 Jun 2025 12:55:52 +0000 (+0800) Subject: test: shell: Introduce $NFT_TEST_LIBRARY_FILE, helper/lib.sh X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f21d7d4bf853960f5b1bb4d5ac280b25682fa088;p=thirdparty%2Fnftables.git test: shell: Introduce $NFT_TEST_LIBRARY_FILE, helper/lib.sh Consolidate frequently used functions in helper/lib.sh switch nat_ftp and flowtables to use it. Signed-off-by: Yi Chen Signed-off-by: Florian Westphal --- diff --git a/tests/shell/helpers/lib.sh b/tests/shell/helpers/lib.sh new file mode 100755 index 00000000..d2d20984 --- /dev/null +++ b/tests/shell/helpers/lib.sh @@ -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 +} diff --git a/tests/shell/helpers/test-wrapper.sh b/tests/shell/helpers/test-wrapper.sh index 4a7e8b7b..6ec4e030 100755 --- a/tests/shell/helpers/test-wrapper.sh +++ b/tests/shell/helpers/test-wrapper.sh @@ -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 diff --git a/tests/shell/testcases/packetpath/flowtables b/tests/shell/testcases/packetpath/flowtables index ab11431f..f3580a5f 100755 --- a/tests/shell/testcases/packetpath/flowtables +++ b/tests/shell/testcases/packetpath/flowtables @@ -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 diff --git a/tests/shell/testcases/packetpath/nat_ftp b/tests/shell/testcases/packetpath/nat_ftp index 1eb4714a..2a80c43f 100755 --- a/tests/shell/testcases/packetpath/nat_ftp +++ b/tests/shell/testcases/packetpath/nat_ftp @@ -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)