summaryrefslogtreecommitdiffstats
path: root/tests/monitor/run-tests.sh
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2017-07-19 15:05:29 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2017-07-19 19:24:39 +0200
commitabd40b25d2ed413994e19699425964115ef49aa5 (patch)
treef0296a9f79cac884b60a1f2e6667b00386eed2b1 /tests/monitor/run-tests.sh
parent504439a02da3ca284e17b9755f3734e45a68cc44 (diff)
tests: Add basic monitor testing framework
This implements testing of 'nft monitor' output correctness and adds a number of testcases for named sets. Signed-off-by: Phil Sutter <phil@nwl.cc> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'tests/monitor/run-tests.sh')
-rwxr-xr-xtests/monitor/run-tests.sh78
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/monitor/run-tests.sh b/tests/monitor/run-tests.sh
new file mode 100755
index 00000000..7447adf1
--- /dev/null
+++ b/tests/monitor/run-tests.sh
@@ -0,0 +1,78 @@
+#!/bin/bash
+
+cd $(dirname $0)
+
+testdir=$(mktemp -d)
+if [ ! -d $testdir ]; then
+ echo "Failed to create test directory" >&2
+ exit 0
+fi
+trap "rm -rf $testdir" EXIT
+
+nft=../../src/nft
+command_file=$(mktemp -p $testdir)
+output_file=$(mktemp -p $testdir)
+
+cmd_append() {
+ echo "$*" >>$command_file
+}
+output_append() {
+ echo "$*" >>$output_file
+}
+run_test() {
+ monitor_output=$(mktemp -p $testdir)
+ $nft monitor >$monitor_output &
+ monitor_pid=$!
+
+ sleep 0.5
+
+ $nft -f $command_file || {
+ echo "nft command failed!"
+ kill $monitor_pid
+ wait >/dev/null 2>&1
+ exit 1
+ }
+ sleep 0.5
+ kill $monitor_pid
+ wait >/dev/null 2>&1
+ if ! diff -Z -q $monitor_output $output_file >/dev/null 2>&1; then
+ echo "monitor output differs!"
+ diff -Z -u $output_file $monitor_output
+ exit 1
+ fi
+ rm $command_file
+ rm $output_file
+ touch $command_file
+ touch $output_file
+}
+
+for testcase in testcases/*.t; do
+ echo "running tests from file $(basename $testcase)"
+ # files are like this:
+ #
+ # I add table ip t
+ # O add table ip t
+ # I add chain ip t c
+ # O add chain ip t c
+
+ $nft flush ruleset
+
+ input_complete=false
+ while read dir line; do
+ case $dir in
+ I)
+ $input_complete && run_test
+ input_complete=false
+ cmd_append "$line"
+ ;;
+ O)
+ input_complete=true
+ output_append "$line"
+ ;;
+ '#'|'')
+ # ignore comments and empty lines
+ ;;
+ esac
+ done <$testcase
+ $input_complete && run_test
+done