All of lore.kernel.org
 help / color / mirror / Atom feed
* [nft PATCH 1/3] tests: json_echo: Print errors to stderr
@ 2021-08-11 22:53 Phil Sutter
  2021-08-11 22:53 ` [nft PATCH 2/3] tests: monitor: " Phil Sutter
  2021-08-11 22:53 ` [nft PATCH 3/3] tests: monitor: Continue on error Phil Sutter
  0 siblings, 2 replies; 3+ messages in thread
From: Phil Sutter @ 2021-08-11 22:53 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel

Apart from the obvious, this fixes exit_dump() which tried to dump the
wrong variable ('out' instead of 'obj') and missed that json.dumps()
doesn't print but just returns a string. Make it call exit_err() to
share some code, which changes the prefix from 'FAIL' to 'Error' as a
side-effect.

While being at it, fix for a syntax warning with newer Python in
unrelated code.

Fixes: bb32d8db9a125 ("JSON: Add support for echo option")
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 tests/json_echo/run-test.py | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/tests/json_echo/run-test.py b/tests/json_echo/run-test.py
index 36a377ac95eec..a6bdfc61afd7b 100755
--- a/tests/json_echo/run-test.py
+++ b/tests/json_echo/run-test.py
@@ -95,25 +95,25 @@ add_quota = { "add": {
 # helper functions
 
 def exit_err(msg):
-    print("Error: %s" %msg)
+    print("Error: %s" %msg, file=sys.stderr)
     sys.exit(1)
 
 def exit_dump(e, obj):
-    print("FAIL: {}".format(e))
-    print("Output was:")
-    json.dumps(out, sort_keys = True, indent = 4, separators = (',', ': '))
-    sys.exit(1)
+    msg = "{}\n".format(e)
+    msg += "Output was:\n"
+    msg += json.dumps(obj, sort_keys = True, indent = 4, separators = (',', ': '))
+    exit_err(msg)
 
 def do_flush():
     rc, out, err = nftables.json_cmd({ "nftables": [flush_ruleset] })
-    if not rc is 0:
+    if rc != 0:
         exit_err("flush ruleset failed: {}".format(err))
 
 def do_command(cmd):
     if not type(cmd) is list:
         cmd = [cmd]
     rc, out, err = nftables.json_cmd({ "nftables": cmd })
-    if not rc is 0:
+    if rc != 0:
         exit_err("command failed: {}".format(err))
     return out
 
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [nft PATCH 2/3] tests: monitor: Print errors to stderr
  2021-08-11 22:53 [nft PATCH 1/3] tests: json_echo: Print errors to stderr Phil Sutter
@ 2021-08-11 22:53 ` Phil Sutter
  2021-08-11 22:53 ` [nft PATCH 3/3] tests: monitor: Continue on error Phil Sutter
  1 sibling, 0 replies; 3+ messages in thread
From: Phil Sutter @ 2021-08-11 22:53 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel

While being at it, introduce die() to error and exit. But don't use it
everywhere to prepare for continuing on errors.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 tests/monitor/run-tests.sh | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/tests/monitor/run-tests.sh b/tests/monitor/run-tests.sh
index 1fe613c7bc301..c30c328ca6e1e 100755
--- a/tests/monitor/run-tests.sh
+++ b/tests/monitor/run-tests.sh
@@ -9,15 +9,22 @@ mydiff() {
 	diff -w -I '^# ' "$@"
 }
 
-if [ "$(id -u)" != "0" ] ; then
-	echo "this requires root!"
+err() {
+	echo "$*" >&2
+}
+
+die() {
+	err "$*"
 	exit 1
+}
+
+if [ "$(id -u)" != "0" ] ; then
+	die "this requires root!"
 fi
 
 testdir=$(mktemp -d)
 if [ ! -d $testdir ]; then
-	echo "Failed to create test directory" >&2
-	exit 1
+	die "Failed to create test directory"
 fi
 trap 'rm -rf $testdir; $nft flush ruleset' EXIT
 
@@ -67,7 +74,7 @@ monitor_run_test() {
 		cat $command_file
 	}
 	$nft -f $command_file || {
-		echo "nft command failed!"
+		err "nft command failed!"
 		kill $monitor_pid
 		wait >/dev/null 2>&1
 		exit 1
@@ -77,8 +84,8 @@ monitor_run_test() {
 	wait >/dev/null 2>&1
 	$test_json && json_output_filter $monitor_output
 	if ! mydiff -q $monitor_output $output_file >/dev/null 2>&1; then
-		echo "monitor output differs!"
-		mydiff -u $output_file $monitor_output
+		err "monitor output differs!"
+		mydiff -u $output_file $monitor_output >&2
 		exit 1
 	fi
 	rm $command_file
@@ -94,12 +101,12 @@ echo_run_test() {
 		cat $command_file
 	}
 	$nft -nn -e -f $command_file >$echo_output || {
-		echo "nft command failed!"
+		err "nft command failed!"
 		exit 1
 	}
 	if ! mydiff -q $echo_output $output_file >/dev/null 2>&1; then
-		echo "echo output differs!"
-		mydiff -u $output_file $echo_output
+		err "echo output differs!"
+		mydiff -u $output_file $echo_output >&2
 		exit 1
 	fi
 	rm $command_file
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [nft PATCH 3/3] tests: monitor: Continue on error
  2021-08-11 22:53 [nft PATCH 1/3] tests: json_echo: Print errors to stderr Phil Sutter
  2021-08-11 22:53 ` [nft PATCH 2/3] tests: monitor: " Phil Sutter
@ 2021-08-11 22:53 ` Phil Sutter
  1 sibling, 0 replies; 3+ messages in thread
From: Phil Sutter @ 2021-08-11 22:53 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel

Just make sure return code reflects the overall result.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 tests/monitor/run-tests.sh | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/tests/monitor/run-tests.sh b/tests/monitor/run-tests.sh
index c30c328ca6e1e..ff00450b19c23 100755
--- a/tests/monitor/run-tests.sh
+++ b/tests/monitor/run-tests.sh
@@ -63,6 +63,7 @@ monitor_run_test() {
 	monitor_output=$(mktemp -p $testdir)
 	monitor_args=""
 	$test_json && monitor_args="vm json"
+	local rc=0
 
 	$nft -nn monitor $monitor_args >$monitor_output &
 	monitor_pid=$!
@@ -75,44 +76,48 @@ monitor_run_test() {
 	}
 	$nft -f $command_file || {
 		err "nft command failed!"
-		kill $monitor_pid
-		wait >/dev/null 2>&1
-		exit 1
+		rc=1
 	}
 	sleep 0.5
 	kill $monitor_pid
 	wait >/dev/null 2>&1
 	$test_json && json_output_filter $monitor_output
-	if ! mydiff -q $monitor_output $output_file >/dev/null 2>&1; then
+	mydiff -q $monitor_output $output_file >/dev/null 2>&1
+	if [[ $rc == 0 && $? != 0 ]]; then
 		err "monitor output differs!"
 		mydiff -u $output_file $monitor_output >&2
-		exit 1
+		rc=1
 	fi
 	rm $command_file
 	rm $output_file
 	touch $command_file
 	touch $output_file
+	return $rc
 }
 
 echo_run_test() {
 	echo_output=$(mktemp -p $testdir)
+	local rc=0
+
 	$debug && {
 		echo "command file:"
 		cat $command_file
 	}
 	$nft -nn -e -f $command_file >$echo_output || {
 		err "nft command failed!"
-		exit 1
+		rc=1
 	}
-	if ! mydiff -q $echo_output $output_file >/dev/null 2>&1; then
+	mydiff -q $echo_output $output_file >/dev/null 2>&1
+	if [[ $rc == 0 && $? != 0 ]]; then
 		err "echo output differs!"
 		mydiff -u $output_file $echo_output >&2
-		exit 1
+		rc=1
 	fi
 	rm $command_file
 	rm $output_file
 	touch $command_file
 	touch $output_file
+	return $rc
 }
 
 testcases=""
@@ -150,6 +155,7 @@ else
 	variants="monitor echo"
 fi
 
+rc=0
 for variant in $variants; do
 	run_test=${variant}_run_test
 	output_append=${variant}_output_append
@@ -169,7 +175,10 @@ for variant in $variants; do
 		while read dir line; do
 			case $dir in
 			I)
-				$input_complete && $run_test
+				$input_complete && {
+					$run_test
+					let "rc += $?"
+				}
 				input_complete=false
 				cmd_append "$line"
 				;;
@@ -186,6 +195,10 @@ for variant in $variants; do
 				;;
 			esac
 		done <$testcase
-		$input_complete && $run_test
+		$input_complete && {
+			$run_test
+			let "rc += $?"
+		}
 	done
 done
+exit $rc
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-08-11 22:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-11 22:53 [nft PATCH 1/3] tests: json_echo: Print errors to stderr Phil Sutter
2021-08-11 22:53 ` [nft PATCH 2/3] tests: monitor: " Phil Sutter
2021-08-11 22:53 ` [nft PATCH 3/3] tests: monitor: Continue on error Phil Sutter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.