diff options
Diffstat (limited to 't/Monitoring-Plugin-Threshold.t')
| -rw-r--r-- | t/Monitoring-Plugin-Threshold.t | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/t/Monitoring-Plugin-Threshold.t b/t/Monitoring-Plugin-Threshold.t new file mode 100644 index 0000000..8849464 --- /dev/null +++ b/t/Monitoring-Plugin-Threshold.t | |||
| @@ -0,0 +1,221 @@ | |||
| 1 | |||
| 2 | use strict; | ||
| 3 | use Test::More tests => 93; | ||
| 4 | BEGIN { | ||
| 5 | use_ok('Monitoring::Plugin::Threshold'); | ||
| 6 | use_ok('Monitoring::Plugin::Functions', ':all' ); | ||
| 7 | # Silence warnings unless TEST_VERBOSE is set | ||
| 8 | $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} }; | ||
| 9 | } | ||
| 10 | |||
| 11 | diag "\nusing Monitoring::Plugin::Threshold revision ". $Monitoring::Plugin::Threshold::VERSION . "\n" | ||
| 12 | if $ENV{TEST_VERBOSE}; | ||
| 13 | |||
| 14 | Monitoring::Plugin::Functions::_fake_exit(1); | ||
| 15 | |||
| 16 | my $t; | ||
| 17 | |||
| 18 | $t = Monitoring::Plugin::Threshold->set_thresholds(warning => undef, critical => undef); | ||
| 19 | ok( defined $t, "two undefs" ); | ||
| 20 | ok( ! $t->warning->is_set, "warning not set" ); | ||
| 21 | ok( ! $t->critical->is_set, "critical not set" ); | ||
| 22 | |||
| 23 | $t = Monitoring::Plugin::Threshold->set_thresholds(warning => "", critical => ""); | ||
| 24 | ok( defined $t, "two empty strings" ); | ||
| 25 | ok( ! $t->warning->is_set, "warning not set" ); | ||
| 26 | ok( ! $t->critical->is_set, "critical not set" ); | ||
| 27 | |||
| 28 | diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; | ||
| 29 | my $t = Monitoring::Plugin::Threshold->set_thresholds(critical => "80"); | ||
| 30 | ok( defined $t, "Threshold ('', '80') set"); | ||
| 31 | ok( ! $t->warning->is_set, "Warning not set"); | ||
| 32 | cmp_ok( $t->critical->start, '==', 0, "Critical strat set correctly"); | ||
| 33 | cmp_ok( $t->critical->end, '==', 80, "Critical end set correctly"); | ||
| 34 | ok ! $t->critical->end_infinity, "not forever"; | ||
| 35 | |||
| 36 | my $expected = { qw( | ||
| 37 | -1 CRITICAL | ||
| 38 | 4 OK | ||
| 39 | 79.999999 OK | ||
| 40 | 80 OK | ||
| 41 | 80.1 CRITICAL | ||
| 42 | 102321 CRITICAL | ||
| 43 | ) }; | ||
| 44 | |||
| 45 | sub test_expected_statuses { | ||
| 46 | my $t = shift; | ||
| 47 | my $expected = shift; | ||
| 48 | my $debug = shift; | ||
| 49 | |||
| 50 | foreach (sort {$a<=>$b} keys %$expected) { | ||
| 51 | is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}"; | ||
| 52 | if ($debug) { | ||
| 53 | diag "val = $_; critical check = ".$t->critical->check_range($_). | ||
| 54 | "; warning check = ".$t->warning->check_range($_); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | use Data::Dumper; | ||
| 58 | diag "thresh dump: ". Dumper $t if $debug; | ||
| 59 | } | ||
| 60 | test_expected_statuses( $t, $expected ); | ||
| 61 | |||
| 62 | # GMC: this test seems bogus to me - either we've died, in which case internal | ||
| 63 | # state is undefined (and untestable!), or we should be returning a non-fatal error | ||
| 64 | if (0) { | ||
| 65 | diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; | ||
| 66 | eval { $t = Monitoring::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") }; | ||
| 67 | ok( defined $t, "Threshold ('5:33', '') set"); | ||
| 68 | cmp_ok( $t->warning->start, '==', 5, "Warning start set"); | ||
| 69 | cmp_ok( $t->warning->end, '==', 33, "Warning end set"); | ||
| 70 | ok( ! $t->critical->is_set, "Critical not set"); | ||
| 71 | } | ||
| 72 | |||
| 73 | # GC: same as previous test, except critical is undef instead of '' | ||
| 74 | diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; | ||
| 75 | $t = Monitoring::Plugin::Threshold->set_thresholds(warning => "5:33", critical => undef); | ||
| 76 | ok( defined $t, "Threshold ('5:33', '') set"); | ||
| 77 | cmp_ok( $t->warning->start, '==', 5, "Warning start set"); | ||
| 78 | cmp_ok( $t->warning->end, '==', 33, "Warning end set"); | ||
| 79 | ok( ! $t->critical->is_set, "Critical not set"); | ||
| 80 | |||
| 81 | $expected = { qw( | ||
| 82 | -1 WARNING | ||
| 83 | 4 WARNING | ||
| 84 | 4.999999 WARNING | ||
| 85 | 5 OK | ||
| 86 | 14.21 OK | ||
| 87 | 33 OK | ||
| 88 | 33.01 WARNING | ||
| 89 | 10231 WARNING | ||
| 90 | ) }; | ||
| 91 | test_expected_statuses( $t, $expected ); | ||
| 92 | |||
| 93 | diag "threshold: warn if more than 30; critical if > 60" if $ENV{TEST_VERBOSE}; | ||
| 94 | $t = Monitoring::Plugin::Threshold->set_thresholds(warning => "~:30", critical => "~:60"); | ||
| 95 | ok( defined $t, "Threshold ('~:30', '~:60') set"); | ||
| 96 | cmp_ok( $t->warning->end, '==', 30, "Warning end set"); | ||
| 97 | cmp_ok( $t->critical->end, '==',60, "Critical end set"); | ||
| 98 | ok $t->critical->start_infinity, "Critical starts at negative infinity"; | ||
| 99 | |||
| 100 | $expected = { qw( | ||
| 101 | -1 OK | ||
| 102 | 4 OK | ||
| 103 | 29.999999 OK | ||
| 104 | 30 OK | ||
| 105 | 30.1 WARNING | ||
| 106 | 50.90 WARNING | ||
| 107 | 59.9 WARNING | ||
| 108 | 60 WARNING | ||
| 109 | 60.00001 CRITICAL | ||
| 110 | 10231 CRITICAL | ||
| 111 | ) }; | ||
| 112 | test_expected_statuses( $t, $expected ); | ||
| 113 | |||
| 114 | # "I'm going to die homeless, penniless, and 30 pounds overweight." | ||
| 115 | # "...and that's...okay." | ||
| 116 | |||
| 117 | # TODO: figure out why this doesn't work and fix the test. | ||
| 118 | goto SKIP_DEATH; | ||
| 119 | diag "threshold: test pure crap for arguments - default to OK." if $ENV{TEST_VERBOSE}; | ||
| 120 | diag "you should see one invalid range definition warning and an UNKNOWN line here:\n"; | ||
| 121 | Monitoring::Plugin::Functions->print_on_die(1); | ||
| 122 | Monitoring::Plugin::Functions->exit_on_die(1); | ||
| 123 | |||
| 124 | dies_ok( sub { | ||
| 125 | $t = Monitoring::Plugin::Threshold->set_thresholds( | ||
| 126 | warning => "total", | ||
| 127 | critical => "rubbish" | ||
| 128 | ) | ||
| 129 | }, "bad thresholds cause death" | ||
| 130 | ); | ||
| 131 | Monitoring::Plugin::Functions->print_on_die(0); | ||
| 132 | Monitoring::Plugin::Functions->exit_on_die(0); | ||
| 133 | SKIP_DEATH: | ||
| 134 | |||
| 135 | |||
| 136 | diag "threshold: critical if > 25 " if $ENV{TEST_VERBOSE}; | ||
| 137 | $t = Monitoring::Plugin::Threshold->set_thresholds( critical => "~:25" ); | ||
| 138 | ok( defined $t, "Threshold ('', '~:25') set (".$t->critical.")" ); | ||
| 139 | ok( ! $t->warning->is_set, "Warning not set"); | ||
| 140 | cmp_ok( $t->critical->end, '==',25, "Critical end set"); | ||
| 141 | ok $t->critical->start_infinity, "Critical starts at negative infinity"; | ||
| 142 | |||
| 143 | $expected = { qw( | ||
| 144 | -1 OK | ||
| 145 | 4 OK | ||
| 146 | 10 OK | ||
| 147 | 14.21 OK | ||
| 148 | 25 OK | ||
| 149 | 25.01 CRITICAL | ||
| 150 | 31001 CRITICAL | ||
| 151 | ) }; | ||
| 152 | test_expected_statuses( $t, $expected); | ||
| 153 | |||
| 154 | diag "threshold: warn if OUTSIDE {10..25} , critical if > 25 " if $ENV{TEST_VERBOSE}; | ||
| 155 | $t = Monitoring::Plugin::Threshold->set_thresholds(warning => "10:25", critical => "~:25"); | ||
| 156 | ok( defined $t, "Threshold ('10:25', '~:25') set"); | ||
| 157 | cmp_ok( $t->warning->start, '==', 10, "Warning start set"); | ||
| 158 | cmp_ok( $t->warning->end, '==', 25, "Warning end set"); | ||
| 159 | cmp_ok( $t->critical->end, '==', 25, "Critical end set"); | ||
| 160 | |||
| 161 | $expected = { qw( | ||
| 162 | -1 WARNING | ||
| 163 | 4 WARNING | ||
| 164 | 9.999999 WARNING | ||
| 165 | 10 OK | ||
| 166 | 14.21 OK | ||
| 167 | 25 OK | ||
| 168 | 25.01 CRITICAL | ||
| 169 | 31001 CRITICAL | ||
| 170 | ) }; | ||
| 171 | test_expected_statuses( $t, $expected ); | ||
| 172 | |||
| 173 | |||
| 174 | diag "warn if INSIDE {10..25} , critical if < 10 " if $ENV{TEST_VERBOSE}; | ||
| 175 | $t = Monitoring::Plugin::Threshold->set_thresholds(warning => "\@10:25", critical => "10:"); | ||
| 176 | $expected = { qw( | ||
| 177 | -1 CRITICAL | ||
| 178 | 4 CRITICAL | ||
| 179 | 9.999999 CRITICAL | ||
| 180 | 10 WARNING | ||
| 181 | 14.21 WARNING | ||
| 182 | 25 WARNING | ||
| 183 | 25.01 OK | ||
| 184 | 31001 OK | ||
| 185 | ) }; | ||
| 186 | test_expected_statuses( $t, $expected ); | ||
| 187 | |||
| 188 | |||
| 189 | # GMC: as of 0.16, set_thresholds can also be called as a mutator | ||
| 190 | diag "threshold mutator: warn if more than 30; critical if > 60" | ||
| 191 | if $ENV{TEST_VERBOSE}; | ||
| 192 | my $t1 = $t; | ||
| 193 | $t->set_thresholds(warning => "0:45", critical => "0:90"); | ||
| 194 | is($t1, $t, "same threshold object after \$t->set_thresholds"); | ||
| 195 | ok( defined $t, "Threshold ('0:45', '0:90') set"); | ||
| 196 | is( $t->warning->start, 0, "Warning start ok"); | ||
| 197 | is( $t->warning->end, 45, "Warning end ok"); | ||
| 198 | is( $t->critical->start, 0, "Critical start ok"); | ||
| 199 | is( $t->critical->end, 90, "Critical end ok"); | ||
| 200 | |||
| 201 | |||
| 202 | # Also as of 0.16, accepts N::P::Range objects as arguments | ||
| 203 | my $warning = Monitoring::Plugin::Range->parse_range_string("50"); | ||
| 204 | my $critical = Monitoring::Plugin::Range->parse_range_string("70:90"); | ||
| 205 | $t = Monitoring::Plugin::Threshold->set_thresholds(warning => $warning, critical => $critical); | ||
| 206 | ok( defined $t, "Threshold from ranges ('50', '70:90') set"); | ||
| 207 | is( $t->warning->start, 0, "Warning start ok"); | ||
| 208 | is( $t->warning->end, 50, "Warning end ok"); | ||
| 209 | is( $t->critical->start, 70, "Critical start ok"); | ||
| 210 | is( $t->critical->end, 90, "Critical end ok"); | ||
| 211 | |||
| 212 | $critical = Monitoring::Plugin::Range->parse_range_string("90:"); | ||
| 213 | $t->set_thresholds(warning => "~:20", critical => $critical); | ||
| 214 | ok( defined $t, "Threshold from string + range ('~:20', '90:') set"); | ||
| 215 | ok( $t->warning->start_infinity, "Warning start ok (infinity)"); | ||
| 216 | is( $t->warning->end, 20, "Warning end ok"); | ||
| 217 | is( $t->critical->start, 90, "Critical start ok"); | ||
| 218 | ok( $t->critical->end_infinity, "Critical end ok (infinity)"); | ||
| 219 | |||
| 220 | |||
| 221 | ok 1, "sweet, made it to the end."; | ||
