mirror of
https://github.com/monitoring-plugins/monitoring-plugins.git
synced 2026-04-04 16:46:32 -04:00
Merge pull request #2076 from RincewindsHat/feature/json_parsing_in_tests
Implement JSON output parsing for tests
This commit is contained in:
commit
03b09f7ce0
2 changed files with 22 additions and 21 deletions
|
|
@ -15,6 +15,10 @@ use warnings;
|
|||
use Cwd;
|
||||
use File::Basename;
|
||||
|
||||
use JSON;
|
||||
|
||||
use Try::Tiny;
|
||||
|
||||
use IO::File;
|
||||
use Data::Dumper;
|
||||
|
||||
|
|
@ -617,6 +621,10 @@ sub testCmd {
|
|||
chomp $output;
|
||||
$object->output($output);
|
||||
|
||||
try {
|
||||
$object->{'mp_test_result'} = decode_json($output);
|
||||
};
|
||||
|
||||
alarm(0);
|
||||
|
||||
my ($pkg, $file, $line) = caller(0);
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Reference in a new issue