summaryrefslogtreecommitdiffstats
path: root/t/Nagios-Plugin-Threshold.t
diff options
context:
space:
mode:
Diffstat (limited to 't/Nagios-Plugin-Threshold.t')
-rw-r--r--t/Nagios-Plugin-Threshold.t61
1 files changed, 52 insertions, 9 deletions
diff --git a/t/Nagios-Plugin-Threshold.t b/t/Nagios-Plugin-Threshold.t
index ccb53eb..d3711bb 100644
--- a/t/Nagios-Plugin-Threshold.t
+++ b/t/Nagios-Plugin-Threshold.t
@@ -1,7 +1,6 @@
1 1
2use strict; 2use strict;
3use Test::More tests => 71; 3use Test::More tests => 87;
4#use Test::Exception; # broken for now so we don't need this.
5BEGIN { 4BEGIN {
6 use_ok('Nagios::Plugin::Threshold'); 5 use_ok('Nagios::Plugin::Threshold');
7 use_ok('Nagios::Plugin::Functions', ':all' ); 6 use_ok('Nagios::Plugin::Functions', ':all' );
@@ -37,19 +36,31 @@ sub test_expected_statuses {
37 my $debug = shift; 36 my $debug = shift;
38 37
39 foreach (sort {$a<=>$b} keys %$expected) { 38 foreach (sort {$a<=>$b} keys %$expected) {
40 is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}"; 39 is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}";
41 if ($debug) { 40 if ($debug) {
42 diag "val = $_; critical check = ".$t->critical->check_range($_). 41 diag "val = $_; critical check = ".$t->critical->check_range($_).
43 "; warning check = ".$t->warning->check_range($_); 42 "; warning check = ".$t->warning->check_range($_);
44 } 43 }
45 } 44 }
46 use Data::Dumper; 45 use Data::Dumper;
47 diag "thresh dump: ". Dumper $t if $debug; 46 diag "thresh dump: ". Dumper $t if $debug;
48} 47}
49test_expected_statuses( $t, $expected ); 48test_expected_statuses( $t, $expected );
50 49
50# GMC: this test seems bogus to me - either we've died, in which case internal
51# state is undefined (and untestable!), or we should be returning a non-fatal error
52if (0) {
53 diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE};
54 eval { $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") };
55 ok( defined $t, "Threshold ('5:33', '') set");
56 cmp_ok( $t->warning->start, '==', 5, "Warning start set");
57 cmp_ok( $t->warning->end, '==', 33, "Warning end set");
58 ok( ! $t->critical->is_set, "Critical not set");
59}
60
61# GC: same as previous test, except critical is undef instead of ''
51diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; 62diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE};
52eval { $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") }; 63$t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => undef);
53ok( defined $t, "Threshold ('5:33', '') set"); 64ok( defined $t, "Threshold ('5:33', '') set");
54cmp_ok( $t->warning->start, '==', 5, "Warning start set"); 65cmp_ok( $t->warning->start, '==', 5, "Warning start set");
55cmp_ok( $t->warning->end, '==', 33, "Warning end set"); 66cmp_ok( $t->warning->end, '==', 33, "Warning end set");
@@ -88,7 +99,6 @@ $expected = { qw(
88) }; 99) };
89test_expected_statuses( $t, $expected ); 100test_expected_statuses( $t, $expected );
90 101
91
92# "I'm going to die homeless, penniless, and 30 pounds overweight." 102# "I'm going to die homeless, penniless, and 30 pounds overweight."
93# "...and that's...okay." 103# "...and that's...okay."
94 104
@@ -163,4 +173,37 @@ $expected = { qw(
163) }; 173) };
164test_expected_statuses( $t, $expected ); 174test_expected_statuses( $t, $expected );
165 175
176
177# GMC: as of 0.16, set_thresholds can also be called as a mutator
178diag "threshold mutator: warn if more than 30; critical if > 60"
179 if $ENV{TEST_VERBOSE};
180my $t1 = $t;
181$t->set_thresholds(warning => "0:45", critical => "0:90");
182is($t1, $t, "same threshold object after \$t->set_thresholds");
183ok( defined $t, "Threshold ('0:45', '0:90') set");
184is( $t->warning->start, 0, "Warning start ok");
185is( $t->warning->end, 45, "Warning end ok");
186is( $t->critical->start, 0, "Critical start ok");
187is( $t->critical->end, 90, "Critical end ok");
188
189
190# Also as of 0.16, accepts N::P::Range objects as arguments
191my $warning = Nagios::Plugin::Range->parse_range_string("50");
192my $critical = Nagios::Plugin::Range->parse_range_string("70:90");
193$t = Nagios::Plugin::Threshold->set_thresholds(warning => $warning, critical => $critical);
194ok( defined $t, "Threshold from ranges ('50', '70:90') set");
195is( $t->warning->start, 0, "Warning start ok");
196is( $t->warning->end, 50, "Warning end ok");
197is( $t->critical->start, 70, "Critical start ok");
198is( $t->critical->end, 90, "Critical end ok");
199
200$critical = Nagios::Plugin::Range->parse_range_string("90:");
201$t->set_thresholds(warning => "~:20", critical => $critical);
202ok( defined $t, "Threshold from string + range ('~:20', '90:') set");
203ok( $t->warning->start_infinity, "Warning start ok (infinity)");
204is( $t->warning->end, 20, "Warning end ok");
205is( $t->critical->start, 90, "Critical start ok");
206ok( $t->critical->end_infinity, "Critical end ok (infinity)");
207
208
166ok 1, "sweet, made it to the end."; 209ok 1, "sweet, made it to the end.";