summaryrefslogtreecommitdiffstats
path: root/t
diff options
context:
space:
mode:
authorGavin Carr <gonzai@users.sourceforge.net>2007-03-16 11:25:15 (GMT)
committerGavin Carr <gonzai@users.sourceforge.net>2007-03-16 11:25:15 (GMT)
commitc6cbf050974c8f6642fa1d7bde309710b66cbfa0 (patch)
tree0eb0ea62814b0623fb619c2af3287a585de48c2c /t
parente5109f99c9657a1a8e9fb32b19254a417e2ccd65 (diff)
downloadmonitoring-plugin-perl-c6cbf050974c8f6642fa1d7bde309710b66cbfa0.tar.gz
Cleanups, mostly to N::P::Range/Threshold/Performance.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1641 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 't')
-rw-r--r--t/Nagios-Plugin-04.t13
-rw-r--r--t/Nagios-Plugin-Threshold.t61
2 files changed, 60 insertions, 14 deletions
diff --git a/t/Nagios-Plugin-04.t b/t/Nagios-Plugin-04.t
index a110b4c..6f31b56 100644
--- a/t/Nagios-Plugin-04.t
+++ b/t/Nagios-Plugin-04.t
@@ -5,18 +5,20 @@
5use strict; 5use strict;
6#use Test::More 'no_plan'; 6#use Test::More 'no_plan';
7use Test::More tests=>26; 7use Test::More tests=>26;
8use Test::Exception;
9 8
10BEGIN { use_ok('Nagios::Plugin') }; 9BEGIN { use_ok('Nagios::Plugin') };
11use Nagios::Plugin::Functions; 10use Nagios::Plugin::Functions;
12Nagios::Plugin::Functions::_fake_exit(1); 11Nagios::Plugin::Functions::_fake_exit(1);
13 12
14 13
15lives_ok sub { my $broke = Nagios::Plugin->new(); }, "constructor DOESN'T die without usage"; 14eval { Nagios::Plugin->new(); };
15ok(! $@, "constructor DOESN'T die without usage");
16 16
17my $p = Nagios::Plugin->new(); 17my $p = Nagios::Plugin->new();
18dies_ok sub { $p->add_arg('warning', 'warning') }, "add_arg() dies if you haven't instantiated with usage"; 18eval { $p->add_arg('warning', 'warning') };
19dies_ok sub { $p->getopts }, "getopts() dies if you haven't instantiated with usage"; 19ok($@, "add_arg() dies if you haven't instantiated with usage");
20eval { $p->getopts };
21ok($@, "getopts() dies if you haven't instantiated with usage");
20 22
21$p = Nagios::Plugin->new( usage => "dummy usage statement" ); 23$p = Nagios::Plugin->new( usage => "dummy usage statement" );
22 24
@@ -41,7 +43,8 @@ can_ok $p, 'threshold';
41#isa_ok $p->threshold, 'Nagios::Plugin::Threshold', "threshold object is defined"; 43#isa_ok $p->threshold, 'Nagios::Plugin::Threshold', "threshold object is defined";
42 44
43 45
44dies_ok sub { $p->check_threshold() }, "check_threshold dies if called with no args"; 46eval { $p->check_threshold() };
47ok($@, "check_threshold dies if called with no args");
45 48
46 49
47# thresholds set implicitly 50# thresholds set implicitly
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.";