Blame SOURCES/0006-tests-monitor-Test-JSON-output-as-well.patch

34625f
From 24b0395e8d26ed9f7eb1716249a8e07f9f84571d Mon Sep 17 00:00:00 2001
34625f
From: Phil Sutter <phil@nwl.cc>
34625f
Date: Thu, 11 Oct 2018 17:49:01 +0200
34625f
Subject: [PATCH] tests: monitor: Test JSON output as well
34625f
34625f
Enhance monitor test suite to test check JSON output as well. Note that
34625f
for now there is no support for --echo output testing with JSON.
34625f
34625f
Signed-off-by: Phil Sutter <phil@nwl.cc>
34625f
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
34625f
(cherry picked from commit 16694bdd4c01ae001b60fbc18d45200fffc84de5)
34625f
34625f
Conflicts:
34625f
	tests/monitor/testcases/object.t
34625f
-> Missing ct timeout support
34625f
34625f
Signed-off-by: Phil Sutter <psutter@redhat.com>
34625f
---
34625f
 tests/monitor/README                   | 27 +++++++++++-----
34625f
 tests/monitor/run-tests.sh             | 45 ++++++++++++++++++++++++--
34625f
 tests/monitor/testcases/object.t       | 11 +++++++
34625f
 tests/monitor/testcases/set-maps.t     |  4 +++
34625f
 tests/monitor/testcases/set-mixed.t    |  7 ++++
34625f
 tests/monitor/testcases/set-multiple.t |  5 +++
34625f
 tests/monitor/testcases/set-simple.t   | 19 +++++++++++
34625f
 tests/monitor/testcases/simple.t       |  8 +++++
34625f
 8 files changed, 115 insertions(+), 11 deletions(-)
34625f
34625f
diff --git a/tests/monitor/README b/tests/monitor/README
34625f
index 9c5e37f5c75c9..39096a7fae078 100644
34625f
--- a/tests/monitor/README
34625f
+++ b/tests/monitor/README
34625f
@@ -15,13 +15,14 @@ to be established manually, i.e. in order to test monitor output when adding a
34625f
 chain, the table containing it has to be created first. In between each
34625f
 testcase, rule set is flushed completely.
34625f
 
34625f
-Input and output lines are prefixed by 'I' and 'O', respectively. The prefix has
34625f
-to be separated from the rest of the line by whitespace. Consecutive input lines
34625f
-are passed to 'nft' together, hence lead to a single transaction.
34625f
+Input lines are prefixed by 'I'. Multiple consecutive input lines are passed to
34625f
+'nft' together, hence lead to a single transaction.
34625f
 
34625f
-Since in most cases output should be equal to input, there is a shortcut: If a
34625f
-line consists of 'O -' only, the test script uses all previous input lines as
34625f
-expected output directly.
34625f
+There are two types of output lines: Those for standard syntax, prefixed by 'O'
34625f
+and those for JSON output, prefixed by 'J'. For standard syntax output lines,
34625f
+there is a shortcut: If a line consists of 'O -' only, the test script uses all
34625f
+previous input lines as expected output directly. Of course this is not
34625f
+available for JSON output lines.
34625f
 
34625f
 Empty lines and those starting with '#' are ignored.
34625f
 
34625f
@@ -29,8 +30,8 @@ Test Script Semantics
34625f
 ---------------------
34625f
 
34625f
 The script iterates over all test case files, reading them line by line. It
34625f
-assumes that sections of 'I' lines alternate with sections of 'O' lines. After
34625f
-stripping the prefix, each line is appended to a temporary file. There are
34625f
+assumes that sections of 'I' lines alternate with sections of 'O'/'J' lines.
34625f
+After stripping the prefix, each line is appended to a temporary file. There are
34625f
 separate files for input and output lines.
34625f
 
34625f
 If a set of input and output lines is complete (i.e. upon encountering either a
34625f
@@ -46,3 +47,13 @@ Note: Running 'nft monitor' in background is prone to race conditions. Hence
34625f
 an artificial delay is introduced before calling 'nft -f' to allow for 'nft
34625f
 monitor' to complete initialization and another one before comparing the output
34625f
 to allow for 'nft monitor' to process the netlink events.
34625f
+
34625f
+By default, only standard syntax is being tested for, i.e. 'J'-prefixed lines
34625f
+are simply ignored. If JSON testing was requested (by passing '-j' flag to the
34625f
+test script), 'O'-prefixed lines in turn are ignored.
34625f
+
34625f
+There is one caveat with regards to JSON output: Since it always contains handle
34625f
+properties (if the given object possesses such) which is supposed to be
34625f
+arbitrary, there is a filter script which normalizes all handle values in
34625f
+monitor output to zero before comparison. Therefore expected output must have
34625f
+all handle properties present but with a value of zero.
34625f
diff --git a/tests/monitor/run-tests.sh b/tests/monitor/run-tests.sh
34625f
index 1adabda193949..f4089887b69aa 100755
34625f
--- a/tests/monitor/run-tests.sh
34625f
+++ b/tests/monitor/run-tests.sh
34625f
@@ -3,6 +3,7 @@
34625f
 cd $(dirname $0)
34625f
 nft=../../src/nft
34625f
 debug=false
34625f
+test_json=false
34625f
 
34625f
 mydiff() {
34625f
 	diff -w -I '^# ' "$@"
34625f
@@ -47,9 +48,16 @@ echo_output_append() {
34625f
 	}
34625f
 	[[ "$*" =~ ^add|replace|insert ]] && echo "$*" >>$output_file
34625f
 }
34625f
+json_output_filter() { # (filename)
34625f
+	# unify handle values
34625f
+	sed -i -e 's/\("handle":\) [0-9][0-9]*/\1 0/g' "$1"
34625f
+}
34625f
 monitor_run_test() {
34625f
 	monitor_output=$(mktemp -p $testdir)
34625f
-	$nft -nn monitor >$monitor_output &
34625f
+	monitor_args=""
34625f
+	$test_json && monitor_args="vm json"
34625f
+
34625f
+	$nft -nn monitor $monitor_args >$monitor_output &
34625f
 	monitor_pid=$!
34625f
 
34625f
 	sleep 0.5
34625f
@@ -67,6 +75,7 @@ monitor_run_test() {
34625f
 	sleep 0.5
34625f
 	kill $monitor_pid
34625f
 	wait >/dev/null 2>&1
34625f
+	$test_json && json_output_filter $monitor_output
34625f
 	if ! mydiff -q $monitor_output $output_file >/dev/null 2>&1; then
34625f
 		echo "monitor output differs!"
34625f
 		mydiff -u $output_file $monitor_output
34625f
@@ -99,7 +108,33 @@ echo_run_test() {
34625f
 	touch $output_file
34625f
 }
34625f
 
34625f
-for variant in monitor echo; do
34625f
+while [ -n "$1" ]; do
34625f
+	case "$1" in
34625f
+	-d|--debug)
34625f
+		debug=true
34625f
+		shift
34625f
+		;;
34625f
+	-j|--json)
34625f
+		test_json=true
34625f
+		shift
34625f
+		;;
34625f
+	*)
34625f
+		echo "unknown option '$1'"
34625f
+		;&
34625f
+	-h|--help)
34625f
+		echo "Usage: $(basename $0) [-j|--json] [-d|--debug]"
34625f
+		exit 1
34625f
+		;;
34625f
+	esac
34625f
+done
34625f
+
34625f
+if $test_json; then
34625f
+	variants="monitor"
34625f
+else
34625f
+	variants="monitor echo"
34625f
+fi
34625f
+
34625f
+for variant in $variants; do
34625f
 	run_test=${variant}_run_test
34625f
 	output_append=${variant}_output_append
34625f
 
34625f
@@ -124,7 +159,11 @@ for variant in monitor echo; do
34625f
 				;;
34625f
 			O)
34625f
 				input_complete=true
34625f
-				$output_append "$line"
34625f
+				$test_json || $output_append "$line"
34625f
+				;;
34625f
+			J)
34625f
+				input_complete=true
34625f
+				$test_json && $output_append "$line"
34625f
 				;;
34625f
 			'#'|'')
34625f
 				# ignore comments and empty lines
34625f
diff --git a/tests/monitor/testcases/object.t b/tests/monitor/testcases/object.t
34625f
index 7b88409775796..e9a6d56ac546c 100644
34625f
--- a/tests/monitor/testcases/object.t
34625f
+++ b/tests/monitor/testcases/object.t
34625f
@@ -1,27 +1,38 @@
34625f
 # first the setup
34625f
 I add table ip t
34625f
 O -
34625f
+J {"add": {"table": {"family": "ip", "name": "t", "handle": 0}}}
34625f
 
34625f
 I add counter ip t c
34625f
 O add counter ip t c { packets 0 bytes 0 }
34625f
+J {"add": {"counter": {"family": "ip", "name": "c", "table": "t", "handle": 0, "packets": 0, "bytes": 0}}}
34625f
 
34625f
 I delete counter ip t c
34625f
 O -
34625f
+J {"delete": {"counter": {"family": "ip", "name": "c", "table": "t", "handle": 0, "packets": 0, "bytes": 0}}}
34625f
 
34625f
+# FIXME: input/output shouldn't be asynchronous here
34625f
 I add quota ip t q 25 mbytes
34625f
 O add quota ip t q { 25 mbytes }
34625f
+J {"add": {"quota": {"family": "ip", "name": "q", "table": "t", "handle": 0, "bytes": 26214400, "used": 0, "inv": false}}}
34625f
 
34625f
 I delete quota ip t q
34625f
 O -
34625f
+J {"delete": {"quota": {"family": "ip", "name": "q", "table": "t", "handle": 0, "bytes": 26214400, "used": 0, "inv": false}}}
34625f
 
34625f
+# FIXME: input/output shouldn't be asynchronous here
34625f
 I add limit ip t l rate 1/second
34625f
 O add limit ip t l { rate 1/second }
34625f
+J {"add": {"limit": {"family": "ip", "name": "l", "table": "t", "handle": 0, "rate": 1, "per": "second", "burst": 5}}}
34625f
 
34625f
 I delete limit ip t l
34625f
 O -
34625f
+J {"delete": {"limit": {"family": "ip", "name": "l", "table": "t", "handle": 0, "rate": 1, "per": "second", "burst": 5}}}
34625f
 
34625f
 I add ct helper ip t cth { type "sip" protocol tcp; l3proto ip; }
34625f
 O -
34625f
+J {"add": {"ct helper": {"family": "ip", "name": "cth", "table": "t", "handle": 0, "type": "sip", "protocol": "tcp", "l3proto": "ip"}}}
34625f
 
34625f
 I delete ct helper ip t cth
34625f
 O -
34625f
+J {"delete": {"ct helper": {"family": "ip", "name": "cth", "table": "t", "handle": 0, "type": "sip", "protocol": "tcp", "l3proto": "ip"}}}
34625f
diff --git a/tests/monitor/testcases/set-maps.t b/tests/monitor/testcases/set-maps.t
34625f
index 3d86720ec8136..acda480d86dbb 100644
34625f
--- a/tests/monitor/testcases/set-maps.t
34625f
+++ b/tests/monitor/testcases/set-maps.t
34625f
@@ -2,9 +2,13 @@
34625f
 I add table ip t
34625f
 I add map ip t portip { type inet_service: ipv4_addr; flags interval; }
34625f
 O -
34625f
+J {"add": {"table": {"family": "ip", "name": "t", "handle": 0}}}
34625f
+J {"add": {"map": {"family": "ip", "name": "portip", "table": "t", "type": "inet_service", "handle": 0, "map": "ipv4_addr", "flags": ["interval"]}}}
34625f
 
34625f
 I add element ip t portip { 80-100: 10.0.0.1 }
34625f
 O -
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portip", "elem": {"set": [[{"range": [80, 100]}, "10.0.0.1"]]}}}}
34625f
 
34625f
 I add element ip t portip { 1024-65535: 10.0.0.1 }
34625f
 O -
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portip", "elem": {"set": [[{"range": [1024, 65535]}, "10.0.0.1"]]}}}}
34625f
diff --git a/tests/monitor/testcases/set-mixed.t b/tests/monitor/testcases/set-mixed.t
34625f
index 9c1c5323f2e4e..08c20116de92f 100644
34625f
--- a/tests/monitor/testcases/set-mixed.t
34625f
+++ b/tests/monitor/testcases/set-mixed.t
34625f
@@ -3,13 +3,20 @@ I add table ip t
34625f
 I add set ip t portrange { type inet_service; flags interval; }
34625f
 I add set ip t ports { type inet_service; }
34625f
 O -
34625f
+J {"add": {"table": {"family": "ip", "name": "t", "handle": 0}}}
34625f
+J {"add": {"set": {"family": "ip", "name": "portrange", "table": "t", "type": "inet_service", "handle": 0, "flags": ["interval"]}}}
34625f
+J {"add": {"set": {"family": "ip", "name": "ports", "table": "t", "type": "inet_service", "handle": 0}}}
34625f
 
34625f
 # make sure concurrent adds work
34625f
 I add element ip t portrange { 1024-65535 }
34625f
 I add element ip t ports { 10 }
34625f
 O -
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [1024, 65535]}]}}}}
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "ports", "elem": {"set": [10]}}}}
34625f
 
34625f
 # delete items again
34625f
 I delete element ip t portrange { 1024-65535 }
34625f
 I delete element ip t ports { 10 }
34625f
 O -
34625f
+J {"delete": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [1024, 65535]}]}}}}
34625f
+J {"delete": {"element": {"family": "ip", "table": "t", "name": "ports", "elem": {"set": [10]}}}}
34625f
diff --git a/tests/monitor/testcases/set-multiple.t b/tests/monitor/testcases/set-multiple.t
34625f
index ad91fac047fe8..bd7a6246a46b4 100644
34625f
--- a/tests/monitor/testcases/set-multiple.t
34625f
+++ b/tests/monitor/testcases/set-multiple.t
34625f
@@ -3,8 +3,13 @@ I add table ip t
34625f
 I add set ip t portrange { type inet_service; flags interval; }
34625f
 I add set ip t portrange2 { type inet_service; flags interval; }
34625f
 O -
34625f
+J {"add": {"table": {"family": "ip", "name": "t", "handle": 0}}}
34625f
+J {"add": {"set": {"family": "ip", "name": "portrange", "table": "t", "type": "inet_service", "handle": 0, "flags": ["interval"]}}}
34625f
+J {"add": {"set": {"family": "ip", "name": "portrange2", "table": "t", "type": "inet_service", "handle": 0, "flags": ["interval"]}}}
34625f
 
34625f
 # make sure concurrent adds work
34625f
 I add element ip t portrange { 1024-65535 }
34625f
 I add element ip t portrange2 { 10-20 }
34625f
 O -
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [1024, 65535]}]}}}}
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portrange2", "elem": {"set": [{"range": [10, 20]}]}}}}
34625f
diff --git a/tests/monitor/testcases/set-simple.t b/tests/monitor/testcases/set-simple.t
34625f
index ebff7cbda0c8b..8ca4f32463fd7 100644
34625f
--- a/tests/monitor/testcases/set-simple.t
34625f
+++ b/tests/monitor/testcases/set-simple.t
34625f
@@ -2,15 +2,21 @@
34625f
 I add table ip t
34625f
 I add set ip t portrange { type inet_service; flags interval; }
34625f
 O -
34625f
+J {"add": {"table": {"family": "ip", "name": "t", "handle": 0}}}
34625f
+J {"add": {"set": {"family": "ip", "name": "portrange", "table": "t", "type": "inet_service", "handle": 0, "flags": ["interval"]}}}
34625f
 
34625f
 # adding some ranges
34625f
 I add element ip t portrange { 1-10 }
34625f
 O -
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [1, 10]}]}}}}
34625f
 I add element ip t portrange { 1024-65535 }
34625f
 O -
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [1024, 65535]}]}}}}
34625f
 I add element ip t portrange { 20-30, 40-50 }
34625f
 O add element ip t portrange { 20-30 }
34625f
 O add element ip t portrange { 40-50 }
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [20, 30]}]}}}}
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [40, 50]}]}}}}
34625f
 
34625f
 # test flushing -> elements are removed in reverse
34625f
 I flush set ip t portrange
34625f
@@ -18,25 +24,38 @@ O delete element ip t portrange { 1024-65535 }
34625f
 O delete element ip t portrange { 40-50 }
34625f
 O delete element ip t portrange { 20-30 }
34625f
 O delete element ip t portrange { 1-10 }
34625f
+J {"delete": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [1024, 65535]}]}}}}
34625f
+J {"delete": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [40, 50]}]}}}}
34625f
+J {"delete": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [20, 30]}]}}}}
34625f
+J {"delete": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [1, 10]}]}}}}
34625f
 
34625f
 # make sure lower scope boundary works
34625f
 I add element ip t portrange { 0-10 }
34625f
 O -
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [0, 10]}]}}}}
34625f
 
34625f
 # make sure half open before other element works
34625f
 I add element ip t portrange { 1024-65535 }
34625f
 I add element ip t portrange { 100-200 }
34625f
 O -
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [1024, 65535]}]}}}}
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [100, 200]}]}}}}
34625f
 
34625f
 # make sure deletion of elements works
34625f
 I delete element ip t portrange { 0-10 }
34625f
 O -
34625f
+J {"delete": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [0, 10]}]}}}}
34625f
 I delete element ip t portrange { 100-200 }
34625f
 I delete element ip t portrange { 1024-65535 }
34625f
 O -
34625f
+J {"delete": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [100, 200]}]}}}}
34625f
+J {"delete": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [1024, 65535]}]}}}}
34625f
 
34625f
 # make sure mixed add/delete works
34625f
 I add element ip t portrange { 10-20 }
34625f
 I add element ip t portrange { 1024-65535 }
34625f
 I delete element ip t portrange { 10-20 }
34625f
 O -
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [10, 20]}]}}}}
34625f
+J {"add": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [1024, 65535]}]}}}}
34625f
+J {"delete": {"element": {"family": "ip", "table": "t", "name": "portrange", "elem": {"set": [{"range": [10, 20]}]}}}}
34625f
diff --git a/tests/monitor/testcases/simple.t b/tests/monitor/testcases/simple.t
34625f
index e4dc073e14b65..78d8f8b04bd36 100644
34625f
--- a/tests/monitor/testcases/simple.t
34625f
+++ b/tests/monitor/testcases/simple.t
34625f
@@ -2,19 +2,27 @@
34625f
 I add table ip t
34625f
 I add chain ip t c
34625f
 O -
34625f
+J {"add": {"table": {"family": "ip", "name": "t", "handle": 0}}}
34625f
+J {"add": {"chain": {"family": "ip", "table": "t", "name": "c", "handle": 0}}}
34625f
 
34625f
 I add rule ip t c accept
34625f
 O -
34625f
+J {"add": {"rule": {"family": "ip", "table": "t", "chain": "c", "handle": 0, "expr": [{"accept": null}]}}}
34625f
 
34625f
 I add rule ip t c tcp dport { 22, 80, 443 } accept
34625f
 O -
34625f
+J {"add": {"rule": {"family": "ip", "table": "t", "chain": "c", "handle": 0, "expr": [{"match": {"op": "==", "left": {"payload": {"protocol": "tcp", "field": "dport"}}, "right": {"set": [22, 80, 443]}}}, {"accept": null}]}}}
34625f
 
34625f
 I insert rule ip t c counter accept
34625f
 O add rule ip t c counter packets 0 bytes 0 accept
34625f
+J {"add": {"rule": {"family": "ip", "table": "t", "chain": "c", "handle": 0, "expr": [{"counter": {"packets": 0, "bytes": 0}}, {"accept": null}]}}}
34625f
 
34625f
 I replace rule ip t c handle 2 accept comment "foo bar"
34625f
 O delete rule ip t c handle 2
34625f
 O add rule ip t c accept comment "foo bar"
34625f
+J {"delete": {"rule": {"family": "ip", "table": "t", "chain": "c", "handle": 0, "expr": [{"accept": null}]}}}
34625f
+J {"add": {"rule": {"family": "ip", "table": "t", "chain": "c", "handle": 0, "comment": "foo bar", "expr": [{"accept": null}]}}}
34625f
 
34625f
 I add counter ip t cnt
34625f
 O add counter ip t cnt { packets 0 bytes 0 }
34625f
+J {"add": {"counter": {"family": "ip", "name": "cnt", "table": "t", "handle": 0, "packets": 0, "bytes": 0}}}
34625f
-- 
34625f
2.19.0
34625f