Skip to contentSkip to conversation

tests: json_echo: Print errors to stderr

PATCH New Phil Sutter 1 participant 3 messages Aug 11, 2021 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/3 tests: json_echo: Print errors to stderr

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")
---
 tests/json_echo/run-test.py | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
tests/json_echo/run-test.py +7 -7
index 36a377ac95eec..a6bdfc61afd7b 100755
@@ -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
 
Signature · 1 line
2.32.0
author reviewer
#1 lore raw

PATCH 2/3 tests: monitor: Print errors to stderr

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

---
 tests/monitor/run-tests.sh | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
tests/monitor/run-tests.sh +17 -10
index 1fe613c7bc301..c30c328ca6e1e 100755
@@ -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
Signature · 1 line
2.32.0
author reviewer
#2 lore raw

PATCH 3/3 tests: monitor: Continue on error

In reply to Phil Sutter

Just make sure return code reflects the overall result.

---
 tests/monitor/run-tests.sh | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
tests/monitor/run-tests.sh +23 -10
index c30c328ca6e1e..ff00450b19c23 100755
@@ -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
Signature · 1 line
2.32.0