From c6cbf050974c8f6642fa1d7bde309710b66cbfa0 Mon Sep 17 00:00:00 2001 From: Gavin Carr Date: Fri, 16 Mar 2007 11:25:15 +0000 Subject: 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 --- t/Nagios-Plugin-04.t | 13 ++++++---- t/Nagios-Plugin-Threshold.t | 61 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 60 insertions(+), 14 deletions(-) (limited to 't') 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 @@ use strict; #use Test::More 'no_plan'; use Test::More tests=>26; -use Test::Exception; BEGIN { use_ok('Nagios::Plugin') }; use Nagios::Plugin::Functions; Nagios::Plugin::Functions::_fake_exit(1); -lives_ok sub { my $broke = Nagios::Plugin->new(); }, "constructor DOESN'T die without usage"; +eval { Nagios::Plugin->new(); }; +ok(! $@, "constructor DOESN'T die without usage"); my $p = Nagios::Plugin->new(); -dies_ok sub { $p->add_arg('warning', 'warning') }, "add_arg() dies if you haven't instantiated with usage"; -dies_ok sub { $p->getopts }, "getopts() dies if you haven't instantiated with usage"; +eval { $p->add_arg('warning', 'warning') }; +ok($@, "add_arg() dies if you haven't instantiated with usage"); +eval { $p->getopts }; +ok($@, "getopts() dies if you haven't instantiated with usage"); $p = Nagios::Plugin->new( usage => "dummy usage statement" ); @@ -41,7 +43,8 @@ can_ok $p, 'threshold'; #isa_ok $p->threshold, 'Nagios::Plugin::Threshold', "threshold object is defined"; -dies_ok sub { $p->check_threshold() }, "check_threshold dies if called with no args"; +eval { $p->check_threshold() }; +ok($@, "check_threshold dies if called with no args"); # 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 @@ use strict; -use Test::More tests => 71; -#use Test::Exception; # broken for now so we don't need this. +use Test::More tests => 87; BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Functions', ':all' ); @@ -37,19 +36,31 @@ sub test_expected_statuses { my $debug = shift; foreach (sort {$a<=>$b} keys %$expected) { - is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}"; - if ($debug) { - diag "val = $_; critical check = ".$t->critical->check_range($_). - "; warning check = ".$t->warning->check_range($_); - } + is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}"; + if ($debug) { + diag "val = $_; critical check = ".$t->critical->check_range($_). + "; warning check = ".$t->warning->check_range($_); + } } use Data::Dumper; diag "thresh dump: ". Dumper $t if $debug; } test_expected_statuses( $t, $expected ); +# GMC: this test seems bogus to me - either we've died, in which case internal +# state is undefined (and untestable!), or we should be returning a non-fatal error +if (0) { + diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; + eval { $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") }; + ok( defined $t, "Threshold ('5:33', '') set"); + cmp_ok( $t->warning->start, '==', 5, "Warning start set"); + cmp_ok( $t->warning->end, '==', 33, "Warning end set"); + ok( ! $t->critical->is_set, "Critical not set"); +} + +# GC: same as previous test, except critical is undef instead of '' diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; -eval { $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") }; +$t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => undef); ok( defined $t, "Threshold ('5:33', '') set"); cmp_ok( $t->warning->start, '==', 5, "Warning start set"); cmp_ok( $t->warning->end, '==', 33, "Warning end set"); @@ -88,7 +99,6 @@ $expected = { qw( ) }; test_expected_statuses( $t, $expected ); - # "I'm going to die homeless, penniless, and 30 pounds overweight." # "...and that's...okay." @@ -163,4 +173,37 @@ $expected = { qw( ) }; test_expected_statuses( $t, $expected ); + +# GMC: as of 0.16, set_thresholds can also be called as a mutator +diag "threshold mutator: warn if more than 30; critical if > 60" + if $ENV{TEST_VERBOSE}; +my $t1 = $t; +$t->set_thresholds(warning => "0:45", critical => "0:90"); +is($t1, $t, "same threshold object after \$t->set_thresholds"); +ok( defined $t, "Threshold ('0:45', '0:90') set"); +is( $t->warning->start, 0, "Warning start ok"); +is( $t->warning->end, 45, "Warning end ok"); +is( $t->critical->start, 0, "Critical start ok"); +is( $t->critical->end, 90, "Critical end ok"); + + +# Also as of 0.16, accepts N::P::Range objects as arguments +my $warning = Nagios::Plugin::Range->parse_range_string("50"); +my $critical = Nagios::Plugin::Range->parse_range_string("70:90"); +$t = Nagios::Plugin::Threshold->set_thresholds(warning => $warning, critical => $critical); +ok( defined $t, "Threshold from ranges ('50', '70:90') set"); +is( $t->warning->start, 0, "Warning start ok"); +is( $t->warning->end, 50, "Warning end ok"); +is( $t->critical->start, 70, "Critical start ok"); +is( $t->critical->end, 90, "Critical end ok"); + +$critical = Nagios::Plugin::Range->parse_range_string("90:"); +$t->set_thresholds(warning => "~:20", critical => $critical); +ok( defined $t, "Threshold from string + range ('~:20', '90:') set"); +ok( $t->warning->start_infinity, "Warning start ok (infinity)"); +is( $t->warning->end, 20, "Warning end ok"); +is( $t->critical->start, 90, "Critical start ok"); +ok( $t->critical->end_infinity, "Critical end ok (infinity)"); + + ok 1, "sweet, made it to the end."; -- cgit v1.2.3-74-g34f1