new file mode 100755
@@ -0,0 +1,53 @@
+#!/bin/bash
+
+cd $(dirname $0)
+nft=../../src/nft
+debug=false
+
+debug_echo() {
+ $debug || return
+
+ echo "$@"
+}
+
+undo_file=$(mktemp)
+
+undo() {
+ [[ -f $undo_file ]] || return
+ tac $undo_file | while read line; do
+ debug_echo "undo: '$nft delete $line'"
+ $nft delete $line || {
+ echo "Warning: undo failed for line '$line'"
+ }
+ done
+ rm $undo_file
+}
+
+trap undo EXIT
+
+for testcase in testcases/*.t; do
+ echo "running tests from file $(basename $testcase)"
+ # files are like this:
+ #
+ # <input command>;<output regexp>
+
+ while read line; do
+ [[ -z "$line" || "$line" == "#"* ]] && continue
+
+ # XXX: this only works if there is no semicolon in output
+ input="${line%;*}"
+ output="${line##*;}"
+
+ debug_echo "calling '$nft --echo $input'"
+ cmd_out=$($nft --echo $input)
+ debug_echo "got output '$cmd_out'"
+ [[ $cmd_out == $output ]] || {
+ echo "Warning: Output differs:"
+ echo "# nft --echo $input"
+ echo "- $output"
+ echo "+ $cmd_out"
+ }
+ echo "$cmd_out" >> $undo_file
+ done <$testcase
+ undo
+done
new file mode 100644
@@ -0,0 +1,8 @@
+add table ip t;table ip t
+add chain ip t c;chain ip t c
+add rule ip t c accept;rule ip t c handle *
+
+add set ip t ipset {type ipv4_addr; };set ip t ipset
+add element ip t ipset { 192.168.0.1 };element ip t ipset { 192.168.0.1 }
+
+add counter ip t cnt;counter ip t cnt
The fancy thing about this is that it uses the actual echo output to undo the changes to the rule set. Signed-off-by: Phil Sutter <phil@nwl.cc> --- tests/echo/run-tests.sh | 53 +++++++++++++++++++++++++++++++++++++++++++ tests/echo/testcases/simple.t | 8 +++++++ 2 files changed, 61 insertions(+) create mode 100755 tests/echo/run-tests.sh create mode 100644 tests/echo/testcases/simple.t