[Nagiosplug-checkins] Nagios-Plugin/t Nagios-Plugin-01.t, NONE, 1.1 Nagios-Plugin-02.t, NONE, 1.1 Nagios-Plugin-03.t, NONE, 1.1 Nagios-Plugin.t, 1.5, NONE

Gavin Carr gonzai at users.sourceforge.net
Tue Sep 26 06:11:41 CEST 2006


Update of /cvsroot/nagiosplug/Nagios-Plugin/t
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv12327/t

Added Files:
	Nagios-Plugin-01.t Nagios-Plugin-02.t Nagios-Plugin-03.t 
Removed Files:
	Nagios-Plugin.t 
Log Message:
Add additional Nagios::Plugin tests.

--- NEW FILE: Nagios-Plugin-01.t ---

use strict;
use Test::More tests => 12;

BEGIN { use_ok('Nagios::Plugin') };

use Nagios::Plugin::Functions;
Nagios::Plugin::Functions::_fake_exit(1);

diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n"
  if $ENV{TEST_VERBOSE};

my $p = Nagios::Plugin->new;
isa_ok( $p, "Nagios::Plugin");

$p->shortname("PAGESIZE");
is($p->shortname, "PAGESIZE", "shortname set correctly");

$p = Nagios::Plugin->new;
ok(! defined $p->shortname, "shortname should be unset on new");

$p = Nagios::Plugin->new( shortname => "SIZE" );
is($p->shortname, "SIZE", "shortname set correctly on new");

diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE};
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 $expected = {qw(
		   -1    WARNING
		   1     WARNING
		   20    OK
		   25    OK
		   26    CRITICAL
		   30    CRITICAL
		   )};

foreach (sort {$a<=>$b} keys %$expected) {
    like  $p->die( return_code => $t->get_status($_), message => "page size at http://... was ${_}kB" ),
	 qr/$expected->{$_}/,
	"Output okay. $_ = $expected->{$_}" ;
}


--- Nagios-Plugin.t DELETED ---

--- NEW FILE: Nagios-Plugin-03.t ---
# $np->check_messages tests

use strict;
use Test::More tests => 61;

BEGIN { 
    use_ok("Nagios::Plugin");
    use_ok("Nagios::Plugin::Functions", ":all");
}
Nagios::Plugin::Functions::_fake_exit(1);

my $plugin = 'NP_CHECK_MESSAGES_03';
my $np = Nagios::Plugin->new( shortname => $plugin );
is($np->shortname, $plugin, "shortname() is $plugin"); 

my ($code, $message);

# -------------------------------------------------------------------------
# Check codes
my @codes = (
    [ [ qw(Critical) ],   [ qw(Warning) ],        CRITICAL ],
    [ [],                 [ qw(Warning) ],        WARNING  ],
    [ [],                 [],                     OK       ],
);
my $i = 0;
for (@codes) {
    $i++;
    $code = $np->check_messages( critical => $_->[0], warning  => $_->[1] );
    is($code, $_->[2], "Code test $i returned $STATUS_TEXT{$_->[2]}");
}

# -------------------------------------------------------------------------
# Check messages
my %arrays = (
    critical    => [ qw(A B C) ],
    warning     => [ qw(D E F) ],
    ok          => [ qw(G H I) ],
);
my %messages = map { $_ => join(' ', @{$arrays{$_}}) } keys %arrays;

# critical, warning
($code, $message) = $np->check_messages(
    critical => $arrays{critical}, warning => $arrays{warning},
);
is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}");
is($message, $messages{critical}, "(critical, warning) message is $message");

# critical, warning, ok
($code, $message) = $np->check_messages(
    critical => $arrays{critical}, warning => $arrays{warning},
    ok => $arrays{ok},
);
is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}");
is($message, $messages{critical}, "(critical, warning, ok) message is $message");

# critical, warning, $ok
($code, $message) = $np->check_messages(
    critical => $arrays{critical}, warning => $arrays{warning},
    ok => 'G H I',
);
is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}");
is($message, $messages{critical}, "(critical, warning, \$ok) message is $message");

# warning
($code, $message) = $np->check_messages(
    critical => [],     warning => $arrays{warning},
);
is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}");
is($message, $messages{warning}, "(warning) message is $message");

# warning, ok
($code, $message) = $np->check_messages(
    critical => [],     warning => $arrays{warning}, ok => $arrays{ok},
);
is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}");
is($message, $messages{warning}, "(warning, ok) message is $message");

# ok
($code, $message) = $np->check_messages(
    critical => [],     warning => [],  ok => $arrays{ok},
);
is($code, OK, "(ok) code is $STATUS_TEXT{$code}");
is($message, $messages{ok}, "(ok) message is $message");

# $ok
($code, $message) = $np->check_messages(
    critical => [],     warning => [],  ok => 'G H I',
);
is($code, OK, "(\$ok) code is $STATUS_TEXT{$code}");
is($message, $messages{ok}, "(\$ok) message is $message");

# -------------------------------------------------------------------------
# explicit join
my $join = '+';
($code, $message) = $np->check_messages(
    critical => $arrays{critical}, warning => $arrays{warning},
    join => $join,
);
is($message, join($join, @{$arrays{critical}}), "joined '$join' (critical, warning) message is $message");
$join = '';
($code, $message) = $np->check_messages(
    critical => [],     warning => $arrays{warning},
    join => $join,
);
is($message, join($join, @{$arrays{warning}}), "joined '$join' (warning) message is $message");
$join = undef;
($code, $message) = $np->check_messages(
    critical => [],     warning => [],          ok => $arrays{ok},
    join => $join,
);
is($message, join(' ', @{$arrays{ok}}), "joined undef (ok) message is $message");

# -------------------------------------------------------------------------
# join_all messages
my $join_all = ' :: ';
my $msg_all_cwo = join($join_all, map { join(' ', @{$arrays{$_}}) } 
    qw(critical warning ok));
my $msg_all_cw = join($join_all, map { join(' ', @{$arrays{$_}}) } 
    qw(critical warning));
my $msg_all_wo = join($join_all, map { join(' ', @{$arrays{$_}}) } 
    qw(warning ok));

# critical, warning, ok
($code, $message) = $np->check_messages(
    critical => $arrays{critical}, warning => $arrays{warning}, ok => $arrays{ok},
    join_all => $join_all,
);
is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}");
is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, ok) message is $message");

# critical, warning, $ok
($code, $message) = $np->check_messages(
    critical => $arrays{critical}, warning => $arrays{warning}, ok => 'G H I',
    join_all => $join_all,
);
is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}");
is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, \$ok) message is $message");

# critical, warning
($code, $message) = $np->check_messages(
    critical => $arrays{critical}, warning => $arrays{warning},
    join_all => $join_all,
);
is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}");
is($message, $msg_all_cw, "join_all '$join_all' (critical, warning) message is $message");

# warning, ok
($code, $message) = $np->check_messages(
    critical => [],     warning => $arrays{warning}, ok => $arrays{ok},
    join_all => $join_all,
);
is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}");
is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, ok) message is $message");

# warning, $ok
($code, $message) = $np->check_messages(
    critical => [],     warning => $arrays{warning}, ok => 'G H I',
    join_all => $join_all,
);
is($code, WARNING, "(warning, \$ok) code is $STATUS_TEXT{$code}");
is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, \$ok) message is $message");

# warning
($code, $message) = $np->check_messages(
    critical => [],     warning => $arrays{warning},
    join_all => $join_all,
);
is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}");
is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $message");

# -------------------------------------------------------------------------
# add_messages

# Constant codes
$np = Nagios::Plugin->new;
$np->add_message( CRITICAL, "A B C" );
$np->add_message( WARNING, "D E F" );
($code, $message) = $np->check_messages();
is($code, CRITICAL, "(CRITICAL, WARNING) code is $STATUS_TEXT{$code}");
is($message, $messages{critical}, "(CRITICAL, WARNING) message is $message");

$np = Nagios::Plugin->new;
$np->add_message( CRITICAL, "A B C" );
($code, $message) = $np->check_messages();
is($code, CRITICAL, "(CRITICAL) code is $STATUS_TEXT{$code}");
is($message, $messages{critical}, "(CRITICAL) message is $message");

$np = Nagios::Plugin->new;
$np->add_message( WARNING, "D E F" );
($code, $message) = $np->check_messages();
is($code, WARNING, "(WARNING) code is $STATUS_TEXT{$code}");
is($message, $messages{warning}, "(WARNING) message is $message");

$np = Nagios::Plugin->new;
$np->add_message( WARNING, "D E F" );
$np->add_message( OK, "G H I" );
($code, $message) = $np->check_messages();
is($code, WARNING, "(WARNING, OK) code is $STATUS_TEXT{$code}");
is($message, $messages{warning}, "(WARNING, OK) message is $message");

$np = Nagios::Plugin->new;
$np->add_message( OK, "G H I" );
($code, $message) = $np->check_messages();
is($code, OK, "(OK) code is $STATUS_TEXT{$code}");
is($message, $messages{ok}, "(OK) message is $message");


# String codes
$np = Nagios::Plugin->new;
$np->add_message( critical => "A B C" );
$np->add_message( warning => "D E F" );
($code, $message) = $np->check_messages();
is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}");
is($message, $messages{critical}, "(critical, warning) message is $message");

$np = Nagios::Plugin->new;
$np->add_message( critical => "A B C" );
($code, $message) = $np->check_messages();
is($code, CRITICAL, "(critical) code is $STATUS_TEXT{$code}");
is($message, $messages{critical}, "(critical) message is $message");

$np = Nagios::Plugin->new;
$np->add_message( warning => "D E F" );
($code, $message) = $np->check_messages();
is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}");
is($message, $messages{warning}, "(warning) message is $message");

$np = Nagios::Plugin->new;
$np->add_message( warning => "D E F" );
$np->add_message( ok => "G H I" );
($code, $message) = $np->check_messages();
is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}");
is($message, $messages{warning}, "(warning, ok) message is $message");

$np = Nagios::Plugin->new;
$np->add_message( ok => "G H I" );
($code, $message) = $np->check_messages();
is($code, OK, "(ok) code is $STATUS_TEXT{$code}");
is($message, $messages{ok}, "(ok) message is $message");


# No add_message
$np = Nagios::Plugin->new;
($code, $message) = $np->check_messages();
is($code, OK, "() code is $STATUS_TEXT{$code}");
is($message, '', "() message is ''");


# -------------------------------------------------------------------------
# Error conditions

# add_message errors
$np = Nagios::Plugin->new;
ok(! defined eval { $np->add_message( foobar => 'hi mum' ) }, 
    'add_message dies on invalid code');
ok(! defined eval { $np->add_message( OKAY => 'hi mum' ) }, 
    'add_message dies on invalid code');
# UNKNOWN and DEPENDENT error codes
ok(! defined eval { $np->add_message( unknown => 'hi mum' ) }, 
    'add_message dies on UNKNOWN code');
ok(! defined eval { $np->add_message( dependent => 'hi mum' ) }, 
    'add_message dies on DEPENDENT code');


--- NEW FILE: Nagios-Plugin-02.t ---

use strict;
use Test::More tests => 101;

BEGIN { use_ok("Nagios::Plugin") }
require Nagios::Plugin::Functions;
Nagios::Plugin::Functions::_fake_exit(1);

# Hardcoded checks of constants
my %ERRORS = %Nagios::Plugin::Functions::ERRORS;
is(OK,          $ERRORS{OK},            "OK        => $ERRORS{OK}");
is(WARNING,     $ERRORS{WARNING},       "WARNING   => $ERRORS{WARNING}");
is(CRITICAL,    $ERRORS{CRITICAL},      "CRITICAL  => $ERRORS{CRITICAL}");
is(UNKNOWN,     $ERRORS{UNKNOWN},       "UNKNOWN   => $ERRORS{UNKNOWN}");
is(DEPENDENT,   $ERRORS{DEPENDENT},     "DEPENDENT => $ERRORS{DEPENDENT}");

my $plugin = 'TEST_PLUGIN';
my $np = Nagios::Plugin->new( shortname => $plugin );
is($np->shortname, $plugin, "shortname() is $plugin"); 

# Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg )
my $r;
my @ok = (
    [ OK,        "OK",           'test the first',  ],
    [ WARNING,   "WARNING",      'test the second', ],
    [ CRITICAL,  "CRITICAL",     'test the third',  ],
    [ UNKNOWN,   "UNKNOWN",      'test the fourth', ],
    [ DEPENDENT, "DEPENDENT",    'test the fifth',  ],
);
for (@ok) {
    # CONSTANT
    $r = $np->nagios_exit($_->[0], $_->[2]);
    is($r->return_code, $_->[0], 
        sprintf('nagios_exit(%s, $msg) returned %s', $_->[1], $_->[0]));
    like($r->message, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/, 
        sprintf('nagios_exit(%s, $msg) output matched "%s"', $_->[1], 
            $plugin . ' ' . $_->[1] . '.*' . $_->[2]));

    # $string
    $r = $np->nagios_exit($_->[1], $_->[2]);
    is($r->return_code, $_->[0], 
        sprintf('nagios_exit("%s", $msg) returned %s', $_->[1], $_->[0]));
    like($r->message, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/, 
        sprintf('nagios_exit("%s", $msg) output matched "%s"', $_->[1], 
            $plugin . ' ' . $_->[1] . '.*' . $_->[2]));
    like($r, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/, 
        sprintf('nagios_exit("%s", $msg) stringified matched "%s"', $_->[1], 
            $plugin . ' ' . $_->[1] . '.*' . $_->[2]));
}

# nagios_exit code corner cases
my @ugly1 = (
    [ -1, 'testing code -1' ],
    [ 7, 'testing code 7' ],
    [ undef, 'testing code undef' ],
    [ '', qq(testing code '') ],
    [ 'string', qq(testing code 'string') ],
);
for (@ugly1) {
    $r = $np->nagios_exit($_->[0], $_->[1]);
    my $display = defined $_->[0] ? "'$_->[0]'" : 'undef';
    is($r->return_code, UNKNOWN, "nagios_exit($display, \$msg) returned ". UNKNOWN);
    like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/, 
        sprintf('nagios_exit(%s, $msg) output matched "%s"',
            $display, 'UNKNOWN.*' . $_->[1]));
}

# nagios_exit message corner cases
my @ugly2 = (
    [ '' ],
    [ undef ],
    [ UNKNOWN ],
);
for (@ugly2) {
    $r = $np->nagios_exit(CRITICAL, $_->[0]);
    my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef";
    my $display2 = defined $_->[0] ? $_->[0] : '';
    like($r->message, qr/CRITICAL\b.*\b$display2$/, 
        sprintf('nagios_exit(%s, $msg) output matched "%s"',
            $display1, "CRITICAL.*$display2"));
}

# Test nagios_die( $msg )
my @msg = (
    [ 'die you dog' ],
    [ '' ],
    [ undef ],
);
for (@msg) {
    $r = $np->nagios_die($_->[0]);
    my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef";
    my $display2 = defined $_->[0] ? $_->[0] : '';
    is($r->return_code, UNKNOWN,
        sprintf('nagios_die(%s) returned UNKNOWN', $display1));
    like($r->message, qr/UNKNOWN\b.*\b$display2$/, 
        sprintf('nagios_die(%s) output matched "%s"', $display1,
            "UNKNOWN.*$display2"));
}

# Test nagios_die( CONSTANT, $msg ), nagios_die( $msg, CONSTANT ), 
#   nagios_die( $string, $msg ), and nagios_die( $msg, $string )
@ok = (
    [ OK,        "OK",           'test the first',  ],
    [ WARNING,   "WARNING",      'test the second', ],
    [ CRITICAL,  "CRITICAL",     'test the third',  ],
    [ UNKNOWN,   "UNKNOWN",      'test the fourth', ],
    [ DEPENDENT, "DEPENDENT",    'test the fifth',  ],
);
for (@ok) {
    # CONSTANT, $msg
    $r = $np->nagios_die($_->[0], $_->[2]);
    is($r->return_code, $_->[0], 
        sprintf('nagios_die(%s, $msg) returned %s', $_->[1], $_->[0]));
    like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
        sprintf('nagios_die(%s, $msg) output matched "%s"', 
            $_->[1], $_->[1] . '.*' . $_->[2]));

    # $msg, CONSTANT
    $r = $np->nagios_die($_->[2], $_->[0]);
    is($r->return_code, $_->[0], 
        sprintf('nagios_die($msg, %s) returned %s', $_->[1], $_->[0]));
    like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
        sprintf('nagios_die($msg, %s) output matched "%s"', 
            $_->[1], $_->[1] . '.*' . $_->[2]));

    # $string, $msg
    $r = $np->nagios_die($_->[1], $_->[2]);
    is($r->return_code, $_->[0], 
        sprintf('nagios_die("%s", $msg) returned %s', $_->[1], $_->[0]));
    like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
        sprintf('nagios_die("%s", $msg) output matched "%s"', $_->[1], 
            $_->[1] . '.*' . $_->[2]));
    like($r, qr/$_->[1]\b.*\b$_->[2]$/, 
        sprintf('nagios_die("%s", $msg) stringified matched "%s"', $_->[1], 
            $_->[1] . '.*' . $_->[2]));

    # $string, $msg
    $r = $np->nagios_die($_->[2], $_->[1]);
    is($r->return_code, $_->[0], 
        sprintf('nagios_die($msg, "%s") returned %s', $_->[1], $_->[0]));
    like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
        sprintf('nagios_die($msg, "%s") output matched "%s"', $_->[1], 
            $_->[1] . '.*' . $_->[2]));
    like($r, qr/$_->[1]\b.*\b$_->[2]$/, 
        sprintf('nagios_die($msg, "%s") stringified matched "%s"', $_->[1], 
            $_->[1] . '.*' . $_->[2]));
}






More information about the Commits mailing list