From 2c6651034f76e2bccb549a867485f8fabbf07cb1 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Thu, 8 Jun 2006 12:27:44 +0000 Subject: Initial revision git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1419 f882894a-f735-0410-b71e-b25c423dba1c --- t/Nagios-Plugin-Performance.t | 63 ++++++++++++++++++++++++++++++++ t/Nagios-Plugin-Range.t | 84 +++++++++++++++++++++++++++++++++++++++++++ t/Nagios-Plugin-Threshold.t | 32 +++++++++++++++++ t/Nagios-Plugin.t | 32 +++++++++++++++++ 4 files changed, 211 insertions(+) create mode 100644 t/Nagios-Plugin-Performance.t create mode 100644 t/Nagios-Plugin-Range.t create mode 100644 t/Nagios-Plugin-Threshold.t create mode 100644 t/Nagios-Plugin.t (limited to 't') diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t new file mode 100644 index 0000000..2fe2326 --- /dev/null +++ b/t/Nagios-Plugin-Performance.t @@ -0,0 +1,63 @@ + +use strict; +use Test::More tests => 42; +BEGIN { use_ok('Nagios::Plugin::Performance') }; + +use Nagios::Plugin::Base; +Nagios::Plugin::Base->exit_on_die(0); + +my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); +cmp_ok( $p[0]->label, 'eq', "/", "label okay"); +cmp_ok( $p[0]->value, '==', 382, "value okay"); +cmp_ok( $p[0]->uom, 'eq', "MB", "uom okay"); +cmp_ok( $p[0]->threshold->warning->end, "==", 15264, "warn okay"); +cmp_ok( $p[0]->threshold->critical->end, "==", 15269, "crit okay"); +ok( ! defined $p[0]->min, "min okay"); +ok( ! defined $p[0]->max, "max okay"); + +cmp_ok( $p[1]->label, 'eq', "/var", "label okay"); +cmp_ok( $p[1]->value, '==', 218, "value okay"); +cmp_ok( $p[1]->uom, 'eq', "MB", "uom okay"); +cmp_ok( $p[1]->threshold->warning->end, "==", 9443, "warn okay"); +cmp_ok( $p[1]->threshold->critical->end, "==", 9448, "crit okay"); + +ok( ! defined Nagios::Plugin::Performance->parse_perfstring("rubbish"), "Errors correctly"); +ok( ! defined Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); + +@p = Nagios::Plugin::Performance->parse_perfstring( + "time=0.001229s;0.000000;0.000000;0.000000;10.000000"); +cmp_ok( $p[0]->label, "eq", "time", "label okay"); +cmp_ok( $p[0]->value, "==", 0.001229, "value okay"); +cmp_ok( $p[0]->uom, "eq", "s", "uom okay"); +cmp_ok( $p[0]->threshold->warning, "eq", "0", "warn okay"); +cmp_ok( $p[0]->threshold->critical, "eq", "0", "crit okay"); + +@p = Nagios::Plugin::Performance->parse_perfstring( + "load1=0.000;5.000;9.000;0; load5=0.000;5.000;9.000;0; load15=0.000;5.000;9.000;0;"); +cmp_ok( $p[0]->label, "eq", "load1", "label okay"); +cmp_ok( $p[0]->value, "eq", "0", "value okay with 0 as string"); +cmp_ok( $p[0]->uom, "eq", "", "uom empty"); +cmp_ok( $p[0]->threshold->warning, "eq", "5", "warn okay"); +cmp_ok( $p[0]->threshold->critical, "eq", "9", "crit okay"); +cmp_ok( $p[1]->label, "eq", "load5", "label okay"); +cmp_ok( $p[2]->label, "eq", "load15", "label okay"); + +@p = Nagios::Plugin::Performance->parse_perfstring( "users=4;20;50;0" ); +cmp_ok( $p[0]->label, "eq", "users", "label okay"); +cmp_ok( $p[0]->value, "==", 4, "value okay"); +cmp_ok( $p[0]->uom, "eq", "", "uom empty"); +cmp_ok( $p[0]->threshold->warning, 'eq', "20", "warn okay"); +cmp_ok( $p[0]->threshold->critical, 'eq', "50", "crit okay"); + +@p = Nagios::Plugin::Performance->parse_perfstring( + "time=0.215300s;5.000000;10.000000;0.000000 size=426B;;;0" ); +cmp_ok( $p[0]->label, "eq", "time", "label okay"); +cmp_ok( $p[0]->value, "eq", "0.2153", "value okay"); +cmp_ok( $p[0]->uom, "eq", "s", "uom okay"); +cmp_ok( $p[0]->threshold->warning, 'eq', "5", "warn okay"); +cmp_ok( $p[0]->threshold->critical, 'eq', "10", "crit okay"); +cmp_ok( $p[1]->label, "eq", "size", "label okay"); +cmp_ok( $p[1]->value, "==", 426, "value okay"); +cmp_ok( $p[1]->uom, "eq", "B", "uom okay"); + ok( ! defined $p[1]->threshold->warning, "warn okay"); + ok( ! defined $p[1]->threshold->critical, "crit okay"); diff --git a/t/Nagios-Plugin-Range.t b/t/Nagios-Plugin-Range.t new file mode 100644 index 0000000..13667de --- /dev/null +++ b/t/Nagios-Plugin-Range.t @@ -0,0 +1,84 @@ + +use strict; +use Test::More tests => 60; +BEGIN { use_ok('Nagios::Plugin::Range') }; + + +my $r = Nagios::Plugin::Range->parse_range_string("6"); +isa_ok( $r, "Nagios::Plugin::Range"); +ok( defined $r, "'6' is valid range"); +cmp_ok( $r->start, '==', 0, "Start correct"); +cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); +cmp_ok( $r->end, '==', 6, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r, 'eq', "6", "Stringification back to original"); + +$r = Nagios::Plugin::Range->parse_range_string("-7:23"); +ok( defined $r, "'-7:23' is valid range"); +cmp_ok( $r->start, '==', -7, "Start correct"); +cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); +cmp_ok( $r->end, '==', 23, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r, 'eq', "-7:23", "Stringification back to original"); + +$r = Nagios::Plugin::Range->parse_range_string(":5.75"); +ok( defined $r, "':5.75' is valid range"); +cmp_ok( $r->start, '==', 0, "Start correct"); +cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); +cmp_ok( $r->end, '==', 5.75, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r, 'eq', "5.75", "Stringification to simplification"); + +$r = Nagios::Plugin::Range->parse_range_string("~:-95.99"); +ok( defined $r, "'~:-95.99' is valid range"); +cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); +cmp_ok( $r->end, '==', -95.99, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r, 'eq', "~:-95.99", "Stringification back to original"); + +$r = Nagios::Plugin::Range->parse_range_string("123456789012345:"); +ok( defined $r, "'123456789012345:' is valid range"); +cmp_ok( $r->start, '==', 123456789012345, "Start correct"); +cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); +cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity"); +cmp_ok( $r, 'eq', "123456789012345:", "Stringification back to original"); + +$r = Nagios::Plugin::Range->parse_range_string("~:0"); +ok( defined $r, "'~:0' is valid range"); +cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); +cmp_ok( $r->end, '==', 0, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r->alert_on, '==', 0, "Will alert on outside of range"); +cmp_ok( $r, 'eq', "~:0", "Stringification back to original"); +ok( $r->check_range(0.5) == 1, "0.5 - alert"); +ok( $r->check_range(-10) == 0, "-10 - no alert"); +ok( $r->check_range(0) == 0, "0 - no alert"); + +$r = Nagios::Plugin::Range->parse_range_string('@0:657.8210567'); +ok( defined $r, '"@0:657.8210567" is a valid range'); +cmp_ok( $r->start, '==', 0, "Start correct"); +cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); +cmp_ok( $r->end, '==', 657.8210567, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r->alert_on, '==', 1, "Will alert on inside of range"); +cmp_ok( $r, 'eq', '@657.8210567', "Stringification to simplified version"); +ok( $r->check_range(32.88) == 1, "32.88 - alert"); +ok( $r->check_range(-2) == 0, "-2 - no alert"); +ok( $r->check_range(657.8210567) == 1, "657.8210567 - alert"); +ok( $r->check_range(0) == 1, "0 - alert"); + +$r = Nagios::Plugin::Range->parse_range_string('1:1'); +ok( defined $r, '"1:1" is a valid range'); +cmp_ok( $r->start, '==', 1, "Start correct"); +cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); +cmp_ok( $r->end, '==', 1, "End correct"); +cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); +cmp_ok( $r, 'eq', "1:1", "Stringification to simplified version"); +ok( $r->check_range(0.5) == 1, "0.5 - alert"); +ok( $r->check_range(1) == 0, "1 - no alert"); +ok( $r->check_range(5.2) == 1, "5.2 - alert"); + +$r = Nagios::Plugin::Range->parse_range_string('2:1'); +ok( ! defined $r, '"2:1" is rejected'); + +# TODO: Need more tests for invalid data diff --git a/t/Nagios-Plugin-Threshold.t b/t/Nagios-Plugin-Threshold.t new file mode 100644 index 0000000..764f7b0 --- /dev/null +++ b/t/Nagios-Plugin-Threshold.t @@ -0,0 +1,32 @@ + +use strict; +use Test::More tests => 18; +BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Base') }; + +Nagios::Plugin::Base->exit_on_die(0); +Nagios::Plugin::Base->print_on_die(0); + +my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); +ok( defined $t, "Threshold ('', '80') set"); +ok( ! defined $t->warning, "Warning not set"); +cmp_ok( $t->critical->end, '==', 80, "Critical set correctly"); + +$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( ! defined $t->critical, "Critical not set"); + +$t = Nagios::Plugin::Threshold->set_thresholds(warning => "30", critical => "60"); +ok( defined $t, "Threshold ('30', '60') set"); +cmp_ok( $t->warning->end, '==', 30, "Warning end set"); +cmp_ok( $t->critical->end, '==',60, "Critical end set"); +cmp_ok( $t->get_status(15.3), '==', $ERRORS{OK}, "15.3 - ok"); +cmp_ok( $t->get_status(30.0001), '==', $ERRORS{WARNING}, "30.0001 - warning"); +cmp_ok( $t->get_status(69), '==', $ERRORS{CRITICAL}, "69 - critical"); + +$t = Nagios::Plugin::Threshold->set_thresholds(warning => "total", critical => "rubbish"); +ok( defined $t, "Threshold object created although ..."); +ok( ! defined $t->warning, "Warning not set"); +ok( ! defined $t->critical, "Critical not set"); + diff --git a/t/Nagios-Plugin.t b/t/Nagios-Plugin.t new file mode 100644 index 0000000..38e792d --- /dev/null +++ b/t/Nagios-Plugin.t @@ -0,0 +1,32 @@ + +use strict; +use Test::More tests => 5; +BEGIN { use_ok('Nagios::Plugin') }; + +use Nagios::Plugin::Base; +Nagios::Plugin::Base->exit_on_die(0); +Nagios::Plugin::Base->print_on_die(0); + +my $p = Nagios::Plugin->new; +isa_ok( $p, "Nagios::Plugin"); + +$p->shortname("PAGESIZE"); + +my $t = $p->set_thresholds( warning => "10:25", critical => "25:" ); + +$p->add_perfdata( + label => "size", + value => 1, + uom => "kB", + threshold => $t, + ); + +cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;25:", "Perfdata correct"); + +my $o = $p->die( return_code => $t->get_status(1), message => "page size at http://... was 1kB" ); +cmp_ok( $o, "eq", 'PAGESIZE CRITICAL page size at http://... was 1kB | size=1kB;10:25;25:', "Output okay"); + +cmp_ok( $p->die( return_code => $t->get_status(30), message => "page size at http://... was 30kB" ), + "eq", 'PAGESIZE WARNING page size at http://... was 30kB | size=1kB;10:25;25:', "Output okay"); + + -- cgit v1.2.3-74-g34f1