diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-02-19 12:07:01 +0100 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-02-19 12:07:01 +0100 |
| commit | ec18b80cdbdc2c4c1e7eb587e251177e8cc7ca11 (patch) | |
| tree | 4ee0acfbdec6452879a3c01fcb50da194bdab887 | |
| parent | d27e0862a9e514149d81622a82622d31ab3bee9b (diff) | |
| download | monitoring-plugins-ec18b80cdbdc2c4c1e7eb587e251177e8cc7ca11.tar.gz | |
Add tests for check_swap with JSON output
| -rw-r--r-- | plugins/t/check_swap.t | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t index 6ef2323d..93e481c3 100644 --- a/plugins/t/check_swap.t +++ b/plugins/t/check_swap.t | |||
| @@ -6,8 +6,9 @@ | |||
| 6 | 6 | ||
| 7 | use strict; | 7 | use strict; |
| 8 | use warnings; | 8 | use warnings; |
| 9 | use Test::More tests => 14; | 9 | use Test::More tests => 35; |
| 10 | use NPTest; | 10 | use NPTest; |
| 11 | use JSON; | ||
| 11 | 12 | ||
| 12 | my $successOutput = '/^OK.* - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; | 13 | my $successOutput = '/^OK.* - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; |
| 13 | my $failureOutput = '/^CRITICAL: .*- [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; | 14 | my $failureOutput = '/^CRITICAL: .*- [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; |
| @@ -43,3 +44,50 @@ like( $result->output, $warnOutput, "Right output" ); | |||
| 43 | $result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical) | 44 | $result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical) |
| 44 | cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); | 45 | cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); |
| 45 | like( $result->output, $failureOutput, "Right output" ); | 46 | like( $result->output, $failureOutput, "Right output" ); |
| 47 | |||
| 48 | |||
| 49 | $outputFormat = '--output-format mp-test-json'; | ||
| 50 | my $output; | ||
| 51 | my $message = '/^[0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; | ||
| 52 | |||
| 53 | $result = NPTest->testCmd( "./check_swap $outputFormat" ); # Always OK | ||
| 54 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | ||
| 55 | $output = decode_json($result->output); | ||
| 56 | is($output->{'state'}, "OK", "State was correct"); | ||
| 57 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
| 58 | |||
| 59 | $result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576 $outputFormat" ); # 1 MB free | ||
| 60 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | ||
| 61 | $output = decode_json($result->output); | ||
| 62 | is($output->{'state'}, "OK", "State was correct"); | ||
| 63 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
| 64 | |||
| 65 | $result = NPTest->testCmd( "./check_swap -w 1% -c 1% $outputFormat" ); # 1% free | ||
| 66 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | ||
| 67 | $output = decode_json($result->output); | ||
| 68 | is($output->{'state'}, "OK", "State was correct"); | ||
| 69 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
| 70 | |||
| 71 | $result = NPTest->testCmd( "./check_swap -w 100% -c 100% $outputFormat" ); # 100% (always critical) | ||
| 72 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | ||
| 73 | $output = decode_json($result->output); | ||
| 74 | is($output->{'state'}, "CRITICAL", "State was correct"); | ||
| 75 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
| 76 | |||
| 77 | $result = NPTest->testCmd( "./check_swap -w 100% -c 1% $outputFormat" ); # 100% (always warn) | ||
| 78 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | ||
| 79 | $output = decode_json($result->output); | ||
| 80 | is($output->{'state'}, "WARNING", "State was correct"); | ||
| 81 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
| 82 | |||
| 83 | $result = NPTest->testCmd( "./check_swap -w 100% $outputFormat" ); # 100% (single threshold, always warn) | ||
| 84 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | ||
| 85 | $output = decode_json($result->output); | ||
| 86 | is($output->{'state'}, "WARNING", "State was correct"); | ||
| 87 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
| 88 | |||
| 89 | $result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical) | ||
| 90 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | ||
| 91 | $output = decode_json($result->output); | ||
| 92 | is($output->{'state'}, "CRITICAL", "State was correct"); | ||
| 93 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
