diff options
| author | Gavin Carr <gonzai@users.sourceforge.net> | 2007-03-16 11:25:15 +0000 |
|---|---|---|
| committer | Gavin Carr <gonzai@users.sourceforge.net> | 2007-03-16 11:25:15 +0000 |
| commit | c6cbf050974c8f6642fa1d7bde309710b66cbfa0 (patch) | |
| tree | 0eb0ea62814b0623fb619c2af3287a585de48c2c /t | |
| parent | e5109f99c9657a1a8e9fb32b19254a417e2ccd65 (diff) | |
| download | monitoring-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.t | 13 | ||||
| -rw-r--r-- | t/Nagios-Plugin-Threshold.t | 61 |
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 @@ | |||
| 5 | use strict; | 5 | use strict; |
| 6 | #use Test::More 'no_plan'; | 6 | #use Test::More 'no_plan'; |
| 7 | use Test::More tests=>26; | 7 | use Test::More tests=>26; |
| 8 | use Test::Exception; | ||
| 9 | 8 | ||
| 10 | BEGIN { use_ok('Nagios::Plugin') }; | 9 | BEGIN { use_ok('Nagios::Plugin') }; |
| 11 | use Nagios::Plugin::Functions; | 10 | use Nagios::Plugin::Functions; |
| 12 | Nagios::Plugin::Functions::_fake_exit(1); | 11 | Nagios::Plugin::Functions::_fake_exit(1); |
| 13 | 12 | ||
| 14 | 13 | ||
| 15 | lives_ok sub { my $broke = Nagios::Plugin->new(); }, "constructor DOESN'T die without usage"; | 14 | eval { Nagios::Plugin->new(); }; |
| 15 | ok(! $@, "constructor DOESN'T die without usage"); | ||
| 16 | 16 | ||
| 17 | my $p = Nagios::Plugin->new(); | 17 | my $p = Nagios::Plugin->new(); |
| 18 | dies_ok sub { $p->add_arg('warning', 'warning') }, "add_arg() dies if you haven't instantiated with usage"; | 18 | eval { $p->add_arg('warning', 'warning') }; |
| 19 | dies_ok sub { $p->getopts }, "getopts() dies if you haven't instantiated with usage"; | 19 | ok($@, "add_arg() dies if you haven't instantiated with usage"); |
| 20 | eval { $p->getopts }; | ||
| 21 | ok($@, "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 | ||
| 44 | dies_ok sub { $p->check_threshold() }, "check_threshold dies if called with no args"; | 46 | eval { $p->check_threshold() }; |
| 47 | ok($@, "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 | ||
| 2 | use strict; | 2 | use strict; |
| 3 | use Test::More tests => 71; | 3 | use Test::More tests => 87; |
| 4 | #use Test::Exception; # broken for now so we don't need this. | ||
| 5 | BEGIN { | 4 | BEGIN { |
| 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 | } |
| 49 | test_expected_statuses( $t, $expected ); | 48 | test_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 | ||
| 52 | if (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 '' | ||
| 51 | diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; | 62 | diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; |
| 52 | eval { $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") }; | 63 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => undef); |
| 53 | ok( defined $t, "Threshold ('5:33', '') set"); | 64 | ok( defined $t, "Threshold ('5:33', '') set"); |
| 54 | cmp_ok( $t->warning->start, '==', 5, "Warning start set"); | 65 | cmp_ok( $t->warning->start, '==', 5, "Warning start set"); |
| 55 | cmp_ok( $t->warning->end, '==', 33, "Warning end set"); | 66 | cmp_ok( $t->warning->end, '==', 33, "Warning end set"); |
| @@ -88,7 +99,6 @@ $expected = { qw( | |||
| 88 | ) }; | 99 | ) }; |
| 89 | test_expected_statuses( $t, $expected ); | 100 | test_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 | ) }; |
| 164 | test_expected_statuses( $t, $expected ); | 174 | test_expected_statuses( $t, $expected ); |
| 165 | 175 | ||
| 176 | |||
| 177 | # GMC: as of 0.16, set_thresholds can also be called as a mutator | ||
| 178 | diag "threshold mutator: warn if more than 30; critical if > 60" | ||
| 179 | if $ENV{TEST_VERBOSE}; | ||
| 180 | my $t1 = $t; | ||
| 181 | $t->set_thresholds(warning => "0:45", critical => "0:90"); | ||
| 182 | is($t1, $t, "same threshold object after \$t->set_thresholds"); | ||
| 183 | ok( defined $t, "Threshold ('0:45', '0:90') set"); | ||
| 184 | is( $t->warning->start, 0, "Warning start ok"); | ||
| 185 | is( $t->warning->end, 45, "Warning end ok"); | ||
| 186 | is( $t->critical->start, 0, "Critical start ok"); | ||
| 187 | is( $t->critical->end, 90, "Critical end ok"); | ||
| 188 | |||
| 189 | |||
| 190 | # Also as of 0.16, accepts N::P::Range objects as arguments | ||
| 191 | my $warning = Nagios::Plugin::Range->parse_range_string("50"); | ||
| 192 | my $critical = Nagios::Plugin::Range->parse_range_string("70:90"); | ||
| 193 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => $warning, critical => $critical); | ||
| 194 | ok( defined $t, "Threshold from ranges ('50', '70:90') set"); | ||
| 195 | is( $t->warning->start, 0, "Warning start ok"); | ||
| 196 | is( $t->warning->end, 50, "Warning end ok"); | ||
| 197 | is( $t->critical->start, 70, "Critical start ok"); | ||
| 198 | is( $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); | ||
| 202 | ok( defined $t, "Threshold from string + range ('~:20', '90:') set"); | ||
| 203 | ok( $t->warning->start_infinity, "Warning start ok (infinity)"); | ||
| 204 | is( $t->warning->end, 20, "Warning end ok"); | ||
| 205 | is( $t->critical->start, 90, "Critical start ok"); | ||
| 206 | ok( $t->critical->end_infinity, "Critical end ok (infinity)"); | ||
| 207 | |||
| 208 | |||
| 166 | ok 1, "sweet, made it to the end."; | 209 | ok 1, "sweet, made it to the end."; |
