diff mbox series

[nft,8/9] tests: monitor: Support running external commands

Message ID 20241002193853.13818-9-phil@nwl.cc
State New
Headers show
Series Support wildcard netdev hooks and events | expand

Commit Message

Phil Sutter Oct. 2, 2024, 7:38 p.m. UTC
Accept '@'-prefixed lines mixed in between 'I'-prefixed ones and apply
nft input and run commands in specified ordering.

To keep things simple, ignore such test cases when in echo mode for now.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 tests/monitor/run-tests.sh | 61 ++++++++++++++++++++++++++++++++++----
 1 file changed, 56 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/tests/monitor/run-tests.sh b/tests/monitor/run-tests.sh
index 214512d269e8d..36fc800c10520 100755
--- a/tests/monitor/run-tests.sh
+++ b/tests/monitor/run-tests.sh
@@ -59,6 +59,44 @@  json_output_filter() { # (filename)
 	# unify handle values
 	sed -i -e 's/\("handle":\) [0-9][0-9]*/\1 0/g' "$1"
 }
+apply_commands_from_file() { # (command_file)
+	grep -q '^# run: ' $1 || {
+		$nft -f - <$1 || {
+			err "nft command failed!"
+			return 1
+		}
+		return 0
+	}
+	local nft_cmd=""
+	local sep=""
+	local line=""
+	while read line; do
+		[[ $line =~ ^#\ run: ]] || {
+			nft_cmd+="$sep$line"
+			sep="; "
+			continue
+		}
+		[[ -n $nft_cmd ]] && {
+			$nft -f - <<<"$nft_cmd" || {
+				err "nft command failed!"
+				return 1
+			}
+		}
+		nft_cmd=""
+		sep=""
+		${line#\# run: } || {
+			err "custom command failed!"
+			return 1
+		}
+	done <$1
+	[[ -n $nft_cmd ]] && {
+		$nft -f - <<<"$nft_cmd" || {
+			err "nft command failed!"
+			return 1
+		}
+	}
+	return 0
+}
 monitor_run_test() {
 	monitor_output=$(mktemp -p $testdir)
 	monitor_args=""
@@ -74,10 +112,7 @@  monitor_run_test() {
 		echo "command file:"
 		cat $command_file
 	}
-	$nft -f - <$command_file || {
-		err "nft command failed!"
-		rc=1
-	}
+	apply_commands_from_file $command_file || rc=1
 	sleep 0.5
 	kill $monitor_pid
 	wait >/dev/null 2>&1
@@ -103,6 +138,17 @@  echo_run_test() {
 		echo "command file:"
 		cat $command_file
 	}
+	grep -q '^# run: ' $command_file && {
+		$debug && {
+			echo "skipping unsuitable test case in command file:"
+			cat $command_file
+		}
+		rm $command_file
+		rm $output_file
+		touch $command_file
+		touch $output_file
+		return 0
+	}
 	$nft -nn -e -f - <$command_file >$echo_output || {
 		err "nft command failed!"
 		rc=1
@@ -182,18 +228,23 @@  for variant in $variants; do
 		# O add table ip t
 		# I add chain ip t c
 		# O add chain ip t c
+		# I <nft input1>
+		# @ <command to run after nft input1>
+		# I <nft input2>
+		# O ...
 
 		$nft flush ruleset
 
 		input_complete=false
 		while read dir line; do
 			case $dir in
-			I)
+			I|@)
 				$input_complete && {
 					$run_test
 					let "rc += $?"
 				}
 				input_complete=false
+				[[ $dir == '@' ]] && line="# run: $line"
 				cmd_append "$line"
 				;;
 			O)