netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: netfilter-devel@vger.kernel.org
Subject: [nft PATCH v3 4/4] tests: Add a simple test suite for --echo option
Date: Fri, 28 Jul 2017 13:55:48 +0200	[thread overview]
Message-ID: <20170728115548.5171-5-phil@nwl.cc> (raw)
In-Reply-To: <20170728115548.5171-1-phil@nwl.cc>

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

diff --git a/tests/echo/run-tests.sh b/tests/echo/run-tests.sh
new file mode 100755
index 0000000000000..c10eb47b0d1bb
--- /dev/null
+++ b/tests/echo/run-tests.sh
@@ -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
diff --git a/tests/echo/testcases/simple.t b/tests/echo/testcases/simple.t
new file mode 100644
index 0000000000000..bf8857dc5e858
--- /dev/null
+++ b/tests/echo/testcases/simple.t
@@ -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
-- 
2.13.1


      parent reply	other threads:[~2017-07-28 11:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-28 11:55 [nft PATCH v3 0/4] Implement --echo option Phil Sutter
2017-07-28 11:55 ` [nft PATCH v3 1/4] mnl: Consolidate mnl_batch_talk() parameters Phil Sutter
2017-08-02 12:36   ` Pablo Neira Ayuso
2017-07-28 11:55 ` [nft PATCH v3 2/4] netlink: Pass nlmsg flags from rule.c Phil Sutter
2017-08-02 12:36   ` Pablo Neira Ayuso
2017-08-02 12:58     ` Pablo Neira Ayuso
2017-08-02 13:38       ` Phil Sutter
2017-07-28 11:55 ` [nft PATCH v3 3/4] Implement --echo option Phil Sutter
2017-08-02 12:37   ` Pablo Neira Ayuso
2017-08-02 13:18     ` Pablo Neira Ayuso
2017-08-02 13:45       ` Phil Sutter
2017-07-28 11:55 ` Phil Sutter [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170728115548.5171-5-phil@nwl.cc \
    --to=phil@nwl.cc \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).