monitor: Some fixes and improvements
Phil Sutter
reviewercover monitor: Some fixes and improvements
The following series happened while fixing monitor tests and contains some changes to monitor code along the way.
Phil Sutter (6):
tests/monitor: Ignore newgen messages in output
monitor: Fix printing of set declarations
tests/monitor: Simplify testcases
monitor: Print space after semicolon
tests/monitor: Clear ruleset after testing
tests/monitor: Add a small README
src/rule.c | 8 +++--- tests/monitor/README | 48 ++++++++++++++++++++++++++++++++++ tests/monitor/run-tests.sh | 16 +++++++++--- tests/monitor/testcases/set-maps.t | 7 +++-- tests/monitor/testcases/set-mixed.t | 10 +++---- tests/monitor/testcases/set-multiple.t | 7 ++--- tests/monitor/testcases/set-simple.t | 21 ++++++--------- 7 files changed, 80 insertions(+), 37 deletions(-) create mode 100644 tests/monitor/README
Signature · 1 line
2.13.1
Phil Sutter
reviewerPATCH 1/6 tests/monitor: Ignore newgen messages in output
Predicting the new ID value is not feasible and neither is implementing support for regular expressions when matching monitor output, so simply ignore them.
Also use diff option '-w' instead of '-Z' to ignore all whitespace, not just at EOL.
--- tests/monitor/run-tests.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
tests/monitor/run-tests.sh +5 -2
@@ -10,6 +10,9 @@ fi trap "rm -rf $testdir" EXIT nft=../../src/nft +mydiff() { + diff -w -I '^# ' "$@" +} command_file=$(mktemp -p $testdir) output_file=$(mktemp -p $testdir)
@@ -35,9 +38,9 @@ run_test() { sleep 0.5 kill $monitor_pid wait >/dev/null 2>&1 - if ! diff -Z -q $monitor_output $output_file >/dev/null 2>&1; then + if ! mydiff -q $monitor_output $output_file >/dev/null 2>&1; then echo "monitor output differs!" - diff -Z -u $output_file $monitor_output + mydiff -u $output_file $monitor_output exit 1 fi rm $command_file
Signature · 1 line
2.13.1
Re: [nft PATCH 1/6] tests/monitor: Ignore newgen messages in output
Quoted 6 lines · Phil Sutter
Predicting the new ID value is not feasible and neither is implementing support for regular expressions when matching monitor output, so simply ignore them.
Also use diff option '-w' instead of '-Z' to ignore all whitespace, not just at EOL.
Applied, thanks Phil.
Phil Sutter
reviewerPATCH 2/6 monitor: Fix printing of set declarations
In reply to Phil SutterThe optional attributes 'flags', 'gc-interval' and 'timeout' have to be delimited by stmt_separator (either newline or semicolon), not 'nl' which is set to whitespace by set_print_plain().
--- src/rule.c | 6 +++--- tests/monitor/testcases/set-maps.t | 2 +- tests/monitor/testcases/set-mixed.t | 2 +- tests/monitor/testcases/set-multiple.t | 4 ++-- tests/monitor/testcases/set-simple.t | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-)
src/rule.c +3 -3
@@ -335,18 +335,18 @@ static void set_print_declaration(const struct set *set,printf ("%stimeout" , delim); delim ="," ; } -printf ("%s" , opts->nl); +printf ("%s" , opts->stmt_separator); }if (set->timeout) {printf ("%s%stimeout " , opts->tab, opts->tab);time_print (set->timeout /1000 ); -printf ("%s" , opts->nl); +printf ("%s" , opts->stmt_separator); }if (set->gc_int) {printf ("%s%sgc-interval " , opts->tab, opts->tab);time_print (set->gc_int /1000 ); -printf ("%s" , opts->nl); +printf ("%s" , opts->stmt_separator); } }
tests/monitor/testcases/set-maps.t +1 -1
@@ -2,7 +2,7 @@ I add table ip t O add table ip t I add map ip t portip { type inet_service: ipv4_addr; flags interval; } -O add map ip t portip { type inet_service : ipv4_addr;flags interval } +O add map ip t portip { type inet_service : ipv4_addr;flags interval; } I add element ip t portip { 80-100: 10.0.0.1 } O add element ip t portip { 80-100 : 10.0.0.1 }
tests/monitor/testcases/set-mixed.t +1 -1
@@ -2,7 +2,7 @@ I add table ip t O add table ip t I add set ip t portrange { type inet_service; flags interval; } -O add set ip t portrange { type inet_service;flags interval } +O add set ip t portrange { type inet_service;flags interval; } I add set ip t ports { type inet_service; } O add set ip t ports { type inet_service;}
tests/monitor/testcases/set-multiple.t +2 -2
@@ -2,9 +2,9 @@ I add table ip t O add table ip t I add set ip t portrange { type inet_service; flags interval; } -O add set ip t portrange { type inet_service;flags interval } +O add set ip t portrange { type inet_service;flags interval; } I add set ip t portrange2 { type inet_service; flags interval; } -O add set ip t portrange2 { type inet_service;flags interval } +O add set ip t portrange2 { type inet_service;flags interval; } # make sure concurrent adds work I add element ip t portrange { 1024-65535 }
tests/monitor/testcases/set-simple.t +1 -1
@@ -2,7 +2,7 @@ I add table ip t O add table ip t I add set ip t portrange { type inet_service; flags interval; } -O add set ip t portrange { type inet_service;flags interval } +O add set ip t portrange { type inet_service;flags interval; } # adding some ranges I add element ip t portrange { 1-10 }
Signature · 1 line
2.13.1
Pablo Neira Ayuso
maintainerRe: [nft PATCH 2/6] monitor: Fix printing of set declarations
tests/monitor/testcases/set-maps.t line 5
Quoted 10 lines · Phil Sutter
tests/monitor/testcases/set-maps.t +1
-1@@ -2,7 +2,7 @@ I add table ip t O add table ip t I add map ip t portip { type inet_service: ipv4_addr; flags interval; } -O add map ip t portip { type inet_service : ipv4_addr;flags interval } +O add map ip t portip { type inet_service : ipv4_addr;flags interval; }
So the proposal is to remove the whitespace? I think it's more readable the way it is already.
Unless I'm overlooking anything, please, leave it as it is. Thanks.
Phil Sutter
reviewerRe: [nft PATCH 2/6] monitor: Fix printing of set declarations
Quoted 14 lines · Pablo Neira Ayuso
tests/monitor/testcases/set-maps.t line 5
Quoted 10 lines · Phil Sutter
tests/monitor/testcases/set-maps.t +1
-1@@ -2,7 +2,7 @@ I add table ip t O add table ip t I add map ip t portip { type inet_service: ipv4_addr; flags interval; } -O add map ip t portip { type inet_service : ipv4_addr;flags interval } +O add map ip t portip { type inet_service : ipv4_addr;flags interval; }So the proposal is to remove the whitespace? I think it's more readable the way it is already.
No, it is about the missing semicolon after 'flags interval' (and after 'timeout' and 'gc-interval' options if present). The output of 'nft monitor' is not syntactically correct without this.
You are correct in that it removes the whitespace before the closing brace, but patch 4 "fixes" that by making 'stmt_separator' consist of semicolon + whitespace.
Is it clear now?
Thanks, Phil
Pablo Neira Ayuso
maintainerRe: [nft PATCH 2/6] monitor: Fix printing of set declarations
Quoted 20 lines · Phil Sutter
Quoted 14 lines · Pablo Neira Ayuso
tests/monitor/testcases/set-maps.t line 5
Quoted 10 lines · Phil Sutter
tests/monitor/testcases/set-maps.t +1
-1@@ -2,7 +2,7 @@ I add table ip t O add table ip t I add map ip t portip { type inet_service: ipv4_addr; flags interval; } -O add map ip t portip { type inet_service : ipv4_addr;flags interval } +O add map ip t portip { type inet_service : ipv4_addr;flags interval; }So the proposal is to remove the whitespace? I think it's more readable the way it is already.
No, it is about the missing semicolon after 'flags interval' (and after 'timeout' and 'gc-interval' options if present). The output of 'nft monitor' is not syntactically correct without this.
Quoted 5 lines
You are correct in that it removes the whitespace before the closing brace, but patch 4 "fixes" that by making '
stmt_separator' consist of semicolon + whitespace.Is it clear now?
Thanks for explaining, yes this clarifies. Please, add examples next time to your commit messages.
BTW, no need to update .t files after patch 4/6 to add the space after semicolon?
Thanks!
Phil Sutter
reviewerRe: [nft PATCH 2/6] monitor: Fix printing of set declarations
Quoted 21 lines · Pablo Neira Ayuso
Quoted 20 lines · Phil Sutter
Quoted 14 lines · Pablo Neira Ayuso
tests/monitor/testcases/set-maps.t line 5
Quoted 10 lines · Phil Sutter
tests/monitor/testcases/set-maps.t +1
-1@@ -2,7 +2,7 @@ I add table ip t O add table ip t I add map ip t portip { type inet_service: ipv4_addr; flags interval; } -O add map ip t portip { type inet_service : ipv4_addr;flags interval } +O add map ip t portip { type inet_service : ipv4_addr;flags interval; }So the proposal is to remove the whitespace? I think it's more readable the way it is already.
No, it is about the missing semicolon after 'flags interval' (and after 'timeout' and 'gc-interval' options if present). The output of 'nft monitor' is not syntactically correct without this.
Quoted 8 lines
Quoted 5 lines
You are correct in that it removes the whitespace before the closing brace, but patch 4 "fixes" that by making '
stmt_separator' consist of semicolon + whitespace.Is it clear now?
Thanks for explaining, yes this clarifies. Please, add examples next time to your commit messages.
Yes, will do. And in that case I already did, please see v2 containing the remaining patches (starting with message ID [email protected]).
Quoted 2 lines
BTW, no need to update .t files after patch 4/6 to add the space after semicolon?
No, because I changed diff options along with the fix for NEWGEN message to ignore all whitespace, not just at EOL.
Thanks, Phil
Phil Sutter
reviewerPATCH 3/6 tests/monitor: Simplify testcases
In reply to Phil SutterBy introducing 'O -' indicating that output should be identical as input, testcases can be simplified quite a bit.
--- tests/monitor/run-tests.sh | 4 ++++ tests/monitor/testcases/set-maps.t | 7 +++---- tests/monitor/testcases/set-mixed.t | 10 +++------- tests/monitor/testcases/set-multiple.t | 7 ++----- tests/monitor/testcases/set-simple.t | 21 ++++++++------------- 5 files changed, 20 insertions(+), 29 deletions(-)
tests/monitor/run-tests.sh +4 -0
@@ -20,6 +20,10 @@ cmd_append() { echo "$*" >>$command_file } output_append() { + [[ "$*" == '-' ]] && { + cat $command_file >>$output_file + return + } echo "$*" >>$output_file } run_test() {
tests/monitor/testcases/set-maps.t +3 -4
@@ -1,11 +1,10 @@ # first the setup I add table ip t -O add table ip t I add map ip t portip { type inet_service: ipv4_addr; flags interval; } -O add map ip t portip { type inet_service : ipv4_addr;flags interval; } +O - I add element ip t portip { 80-100: 10.0.0.1 } -O add element ip t portip { 80-100 : 10.0.0.1 } +O - I add element ip t portip { 1024-65535: 10.0.0.1 } -O add element ip t portip { 1024-65535 : 10.0.0.1 } +O -
tests/monitor/testcases/set-mixed.t +3 -7
@@ -1,19 +1,15 @@ # first the setup I add table ip t -O add table ip t I add set ip t portrange { type inet_service; flags interval; } -O add set ip t portrange { type inet_service;flags interval; } I add set ip t ports { type inet_service; } -O add set ip t ports { type inet_service;} +O - # make sure concurrent adds work I add element ip t portrange { 1024-65535 } I add element ip t ports { 10 } -O add element ip t portrange { 1024-65535 } -O add element ip t ports { 10 } +O - # delete items again I delete element ip t portrange { 1024-65535 } I delete element ip t ports { 10 } -O delete element ip t portrange { 1024-65535 } -O delete element ip t ports { 10 } +O -
tests/monitor/testcases/set-multiple.t +2 -5
@@ -1,13 +1,10 @@ # first the setup I add table ip t -O add table ip t I add set ip t portrange { type inet_service; flags interval; } -O add set ip t portrange { type inet_service;flags interval; } I add set ip t portrange2 { type inet_service; flags interval; } -O add set ip t portrange2 { type inet_service;flags interval; } +O - # make sure concurrent adds work I add element ip t portrange { 1024-65535 } I add element ip t portrange2 { 10-20 } -O add element ip t portrange { 1024-65535 } -O add element ip t portrange2 { 10-20 } +O -
tests/monitor/testcases/set-simple.t +8 -13
@@ -1,14 +1,13 @@ # first the setup I add table ip t -O add table ip t I add set ip t portrange { type inet_service; flags interval; } -O add set ip t portrange { type inet_service;flags interval; } +O - # adding some ranges I add element ip t portrange { 1-10 } -O add element ip t portrange { 1-10 } +O - I add element ip t portrange { 1024-65535 } -O add element ip t portrange { 1024-65535 } +O - I add element ip t portrange { 20-30, 40-50 } O add element ip t portrange { 20-30 } O add element ip t portrange { 40-50 }
@@ -22,26 +21,22 @@ O delete element ip t portrange { 1-10 } # make sure lower scope boundary works I add element ip t portrange { 0-10 } -O add element ip t portrange { 0-10 } +O - # make sure half open before other element works I add element ip t portrange { 1024-65535 } I add element ip t portrange { 100-200 } -O add element ip t portrange { 1024-65535 } -O add element ip t portrange { 100-200 } +O - # make sure deletion of elements works I delete element ip t portrange { 0-10 } -O delete element ip t portrange { 0-10 } +O - I delete element ip t portrange { 100-200 } I delete element ip t portrange { 1024-65535 } -O delete element ip t portrange { 100-200 } -O delete element ip t portrange { 1024-65535 } +O - # make sure mixed add/delete works I add element ip t portrange { 10-20 } I add element ip t portrange { 1024-65535 } I delete element ip t portrange { 10-20 } -O add element ip t portrange { 10-20 } -O add element ip t portrange { 1024-65535 } -O delete element ip t portrange { 10-20 } +O -
Signature · 1 line
2.13.1
Pablo Neira Ayuso
maintainerRe: [nft PATCH 3/6] tests/monitor: Simplify testcases
Quoted 2 lines · Phil Sutter
By introducing 'O -' indicating that output should be identical as input, testcases can be simplified quite a bit.
I like this update.
Pablo Neira Ayuso
maintainerRe: [nft PATCH 3/6] tests/monitor: Simplify testcases
Quoted 5 lines · Pablo Neira Ayuso
Quoted 2 lines · Phil Sutter
By introducing 'O -' indicating that output should be identical as input, testcases can be simplified quite a bit.
I like this update.
But I can't take it, git am complains. Please, rebase and resubmit, thanks!
Phil Sutter
reviewerPATCH 4/6 monitor: Print space after semicolon
In reply to Phil SutterThis makes output a bit more readable.
--- src/rule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/rule.c +1 -1
@@ -381,7 +381,7 @@ void set_print_plain(const struct set *s, struct output_ctx *octx) .nl = " ", .table = s->handle.table, .family = family2str(s->handle.family), - .stmt_separator = ";", + .stmt_separator = "; ", };do_set_print (s, &opts, octx);
Signature · 1 line
2.13.1
Pablo Neira Ayuso
maintainerRe: [nft PATCH 4/6] monitor: Print space after semicolon
Quoted 1 line · Phil Sutter
This makes output a bit more readable.
Could you print an example in the patch description of what is being more readable after this? Thanks!
[...]
Quoted 16 lines
src/rule.c +1
-1@@ -381,7 +381,7 @@ void set_print_plain(const struct set *s, struct output_ctx *octx) .nl = " ", .table = s->handle.table, .family = family2str(s->handle.family), - .stmt_separator = ";", + .stmt_separator = "; ", };do_set_print (s, &opts, octx);Signature · 1 line
2.13.1
Phil Sutter
reviewerPATCH 5/6 tests/monitor: Clear ruleset after testing
In reply to Phil Sutter--- tests/monitor/run-tests.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
tests/monitor/run-tests.sh +6 -5
@@ -2,17 +2,18 @@ cd $(dirname $0) +nft=../../src/nft +mydiff() { + diff -w -I '^# ' "$@" +} + testdir=$(mktemp -d) if [ ! -d $testdir ]; then echo "Failed to create test directory" >&2 exit 0 fi -trap "rm -rf $testdir" EXIT +trap "rm -rf $testdir; $nft flush ruleset" EXIT -nft=../../src/nft -mydiff() { - diff -w -I '^# ' "$@" -} command_file=$(mktemp -p $testdir) output_file=$(mktemp -p $testdir)
Signature · 1 line
2.13.1
Pablo Neira Ayuso
maintainerRe: [nft PATCH 5/6] tests/monitor: Clear ruleset after testing
5/6 and 6/6 also LGTM.
Phil Sutter
reviewerPATCH 6/6 tests/monitor: Add a small README
In reply to Phil Sutter--- tests/monitor/README | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/monitor/README
tests/monitor/README new file +48 -0
@@ -0,0 +1,48 @@ +Simple NFT MONITOR Testsuite +============================ + +The purpose of this suite of tests is to assert correct 'nft monitor' output for +known input. The suite consists of the single shell script 'run-tests.sh' which +performs the tests and a number of test definition files in 'testcases/'. The +latter have to be suffixed '.t' in order to be recognized as such. + +Test Case Syntax +---------------- + +Each testcase defines a number of commands to pass on to 'nft' binary and an +associated 'nft monitor' output definition. Prerequisites for each command have +to be established manually, i.e. in order to test monitor output when adding a +chain, the table containing it has to be created first. In between each +testcase, rule set is flushed completely. + +Input and output lines are prefixed by 'I' and 'O', respectively. The prefix has +to be separated from the rest of the line by whitespace. Consecutive input lines +are passed to 'nft' together, hence lead to a single transaction. + +Since in most cases output should be equal to input, there is a shortcut: If a +line consists of 'O -' only, the test script uses all previous input lines as +expected output directly. + +Empty lines and those starting with '#' are ignored. + +Test Script Semantics +--------------------- + +The script iterates over all test case files, reading them line by line. It +assumes that sections of 'I' lines alternate with sections of 'O' lines. After +stripping the prefix, each line is appended to a temporary file. There are +separate files for input and output lines. + +If a set of input and output lines is complete (i.e. upon encountering either a +new input line or end of file), a testrun is performed: 'nft monitor' is run in +background, redirecting the output into a third file. The input file is passed +to 'nft -f'. Finally 'nft monitor' is killed and it's output compared to the +output file created earlier. If the files differ, a unified diff is printed and +test execution aborts. + +After each testrun, input and output files are cleared. + +Note: Running 'nft monitor' in background is prone to race conditions. Hence +an artificial delay is introduced before calling 'nft -f' to allow for 'nft +monitor' to complete initialization and another one before comparing the output +to allow for 'nft monitor' to process the netlink events.
Signature · 1 line
2.13.1
Pablo Neira Ayuso
maintainerRe: [nft PATCH 6/6] tests/monitor: Add a small README
This applies cleanly, so I just pushed it out.
I'm taking as much as it applies cleanly via git am.
Thanks!