diff options
Diffstat (limited to 'plugins/t/check_swap.t')
| -rw-r--r-- | plugins/t/check_swap.t | 72 |
1 files changed, 61 insertions, 11 deletions
diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t index eaa81083..93e481c3 100644 --- a/plugins/t/check_swap.t +++ b/plugins/t/check_swap.t | |||
| @@ -5,39 +5,89 @@ | |||
| 5 | # | 5 | # |
| 6 | 6 | ||
| 7 | use strict; | 7 | use strict; |
| 8 | use Test::More tests => 14; | 8 | use warnings; |
| 9 | use Test::More tests => 35; | ||
| 9 | use NPTest; | 10 | use NPTest; |
| 11 | use JSON; | ||
| 10 | 12 | ||
| 11 | my $successOutput = '/^SWAP 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\)/'; |
| 12 | my $failureOutput = '/^SWAP 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\)/'; |
| 13 | my $warnOutput = '/^SWAP WARNING - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; | 15 | my $warnOutput = '/^WARNING: .*- [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; |
| 14 | 16 | ||
| 17 | my $outputFormat = '--output-format one-line'; | ||
| 15 | my $result; | 18 | my $result; |
| 16 | 19 | ||
| 17 | $result = NPTest->testCmd( "./check_swap" ); # Always OK | 20 | $result = NPTest->testCmd( "./check_swap $outputFormat" ); # Always OK |
| 18 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | 21 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
| 19 | like( $result->output, $successOutput, "Right output" ); | 22 | like( $result->output, $successOutput, "Right output" ); |
| 20 | 23 | ||
| 21 | $result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576" ); # 1 MB free | 24 | $result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576 $outputFormat" ); # 1 MB free |
| 22 | cmp_ok( $result->return_code, "==", 0, "At least 1MB free" ); | 25 | cmp_ok( $result->return_code, "==", 0, "At least 1MB free" ); |
| 23 | like( $result->output, $successOutput, "Right output" ); | 26 | like( $result->output, $successOutput, "Right output" ); |
| 24 | 27 | ||
| 25 | $result = NPTest->testCmd( "./check_swap -w 1% -c 1%" ); # 1% free | 28 | $result = NPTest->testCmd( "./check_swap -w 1% -c 1% $outputFormat" ); # 1% free |
| 26 | cmp_ok( $result->return_code, "==", 0, 'At least 1% free' ); | 29 | cmp_ok( $result->return_code, "==", 0, 'At least 1% free' ); |
| 27 | like( $result->output, $successOutput, "Right output" ); | 30 | like( $result->output, $successOutput, "Right output" ); |
| 28 | 31 | ||
| 29 | $result = NPTest->testCmd( "./check_swap -w 100% -c 100%" ); # 100% (always critical) | 32 | $result = NPTest->testCmd( "./check_swap -w 100% -c 100% $outputFormat" ); # 100% (always critical) |
| 30 | cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); | 33 | cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); |
| 31 | like( $result->output, $failureOutput, "Right output" ); | 34 | like( $result->output, $failureOutput, "Right output" ); |
| 32 | 35 | ||
| 33 | $result = NPTest->testCmd( "./check_swap -w 100% -c 1%" ); # 100% (always warn) | 36 | $result = NPTest->testCmd( "./check_swap -w 100% -c 1% $outputFormat" ); # 100% (always warn) |
| 34 | cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); | 37 | cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); |
| 35 | like( $result->output, $warnOutput, "Right output" ); | 38 | like( $result->output, $warnOutput, "Right output" ); |
| 36 | 39 | ||
| 37 | $result = NPTest->testCmd( "./check_swap -w 100%" ); # 100% (single threshold, always warn) | 40 | $result = NPTest->testCmd( "./check_swap -w 100% $outputFormat" ); # 100% (single threshold, always warn) |
| 38 | cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); | 41 | cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); |
| 39 | like( $result->output, $warnOutput, "Right output" ); | 42 | like( $result->output, $warnOutput, "Right output" ); |
| 40 | 43 | ||
| 41 | $result = NPTest->testCmd( "./check_swap -c 100%" ); # 100% (single threshold, always critical) | 44 | $result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical) |
| 42 | 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' ); |
| 43 | 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"); | ||
