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

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