From 5ad1c0e821a5aaafe47dbe79d5f69820fde798bd Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 7 Mar 2025 09:09:45 +0100 Subject: Implement JSON output parsing for tests --- NPTest.pm | 4 ++++ plugins/t/check_swap.t | 35 ++++++++++++++--------------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/NPTest.pm b/NPTest.pm index 9b25ac3e..47773acc 100644 --- a/NPTest.pm +++ b/NPTest.pm @@ -15,6 +15,8 @@ use warnings; use Cwd; use File::Basename; +use JSON; + use IO::File; use Data::Dumper; @@ -617,6 +619,8 @@ sub testCmd { chomp $output; $object->output($output); + $object->{'mp_test_result'} = decode_json($output); + alarm(0); my ($pkg, $file, $line) = caller(0); diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t index 7e61b766..68946f6d 100644 --- a/plugins/t/check_swap.t +++ b/plugins/t/check_swap.t @@ -17,42 +17,35 @@ my $message = '/^[0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; $result = NPTest->testCmd( "./check_swap $outputFormat" ); # Always OK cmp_ok( $result->return_code, "==", 0, "Always OK" ); -$output = decode_json($result->output); -is($output->{'state'}, "OK", "State was correct"); -like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); +is($result->{'mp_test_result'}->{'state'}, "OK", "State was correct"); +like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); $result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576 $outputFormat" ); # 1 MB free cmp_ok( $result->return_code, "==", 0, "Always OK" ); -$output = decode_json($result->output); -is($output->{'state'}, "OK", "State was correct"); -like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); +is($result->{'mp_test_result'}->{'state'}, "OK", "State was correct"); +like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); $result = NPTest->testCmd( "./check_swap -w 1% -c 1% $outputFormat" ); # 1% free cmp_ok( $result->return_code, "==", 0, "Always OK" ); -$output = decode_json($result->output); -is($output->{'state'}, "OK", "State was correct"); -like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); +is($result->{'mp_test_result'}->{'state'}, "OK", "State was correct"); +like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); $result = NPTest->testCmd( "./check_swap -w 100% -c 100% $outputFormat" ); # 100% (always critical) cmp_ok( $result->return_code, "==", 0, "Always OK" ); -$output = decode_json($result->output); -is($output->{'state'}, "CRITICAL", "State was correct"); -like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); +is($result->{'mp_test_result'}->{'state'}, "CRITICAL", "State was correct"); +like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); $result = NPTest->testCmd( "./check_swap -w 100% -c 1% $outputFormat" ); # 100% (always warn) cmp_ok( $result->return_code, "==", 0, "Always OK" ); -$output = decode_json($result->output); -is($output->{'state'}, "WARNING", "State was correct"); -like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); +is($result->{'mp_test_result'}->{'state'}, "WARNING", "State was correct"); +like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); $result = NPTest->testCmd( "./check_swap -w 100% $outputFormat" ); # 100% (single threshold, always warn) cmp_ok( $result->return_code, "==", 0, "Always OK" ); -$output = decode_json($result->output); -is($output->{'state'}, "WARNING", "State was correct"); -like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); +is($result->{'mp_test_result'}->{'state'}, "WARNING", "State was correct"); +like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); $result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical) cmp_ok( $result->return_code, "==", 0, "Always OK" ); -$output = decode_json($result->output); -is($output->{'state'}, "CRITICAL", "State was correct"); -like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); +is($result->{'mp_test_result'}->{'state'}, "CRITICAL", "State was correct"); +like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); -- cgit v1.2.3-74-g34f1 From 18dedf3463534a9f6c8137628c482298dbb0459b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 7 Mar 2025 17:28:36 +0100 Subject: Do not die in tests if JSON Parsing fails --- NPTest.pm | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/NPTest.pm b/NPTest.pm index 47773acc..f9f18b5f 100644 --- a/NPTest.pm +++ b/NPTest.pm @@ -17,6 +17,8 @@ use File::Basename; use JSON; +use feature 'try'; + use IO::File; use Data::Dumper; @@ -619,7 +621,9 @@ sub testCmd { chomp $output; $object->output($output); - $object->{'mp_test_result'} = decode_json($output); + try { + $object->{'mp_test_result'} = decode_json($output); + } alarm(0); -- cgit v1.2.3-74-g34f1 From f2275431783a5aaed25c35b5deec37c244872a7a Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 7 Mar 2025 17:48:27 +0100 Subject: Use Try::Tiny instead of experimental feature --- NPTest.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NPTest.pm b/NPTest.pm index f9f18b5f..987d3b71 100644 --- a/NPTest.pm +++ b/NPTest.pm @@ -17,7 +17,7 @@ use File::Basename; use JSON; -use feature 'try'; +use Try::Tiny; use IO::File; use Data::Dumper; @@ -623,7 +623,7 @@ sub testCmd { try { $object->{'mp_test_result'} = decode_json($output); - } + }; alarm(0); -- cgit v1.2.3-74-g34f1