diff options
Diffstat (limited to 't/Nagios-Plugin-02.t')
| -rw-r--r-- | t/Nagios-Plugin-02.t | 161 |
1 files changed, 0 insertions, 161 deletions
diff --git a/t/Nagios-Plugin-02.t b/t/Nagios-Plugin-02.t deleted file mode 100644 index f64fb6f..0000000 --- a/t/Nagios-Plugin-02.t +++ /dev/null | |||
| @@ -1,161 +0,0 @@ | |||
| 1 | # Nagios::Plugin test set 2, testing NP::Functions wrapping | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Test::More tests => 103; | ||
| 5 | |||
| 6 | BEGIN { use_ok("Nagios::Plugin") } | ||
| 7 | require Nagios::Plugin::Functions; | ||
| 8 | Nagios::Plugin::Functions::_fake_exit(1); | ||
| 9 | |||
| 10 | # Hardcoded checks of constants | ||
| 11 | my %ERRORS = %Nagios::Plugin::Functions::ERRORS; | ||
| 12 | is(OK, $ERRORS{OK}, "OK => $ERRORS{OK}"); | ||
| 13 | is(WARNING, $ERRORS{WARNING}, "WARNING => $ERRORS{WARNING}"); | ||
| 14 | is(CRITICAL, $ERRORS{CRITICAL}, "CRITICAL => $ERRORS{CRITICAL}"); | ||
| 15 | is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}"); | ||
| 16 | is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}"); | ||
| 17 | |||
| 18 | my $plugin = 'TEST_PLUGIN'; | ||
| 19 | my $np = Nagios::Plugin->new( shortname => $plugin ); | ||
| 20 | is($np->shortname, $plugin, "shortname() is $plugin"); | ||
| 21 | |||
| 22 | # Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg ) | ||
| 23 | my $r; | ||
| 24 | my @ok = ( | ||
| 25 | [ OK, "OK", 'test the first', ], | ||
| 26 | [ WARNING, "WARNING", 'test the second', ], | ||
| 27 | [ CRITICAL, "CRITICAL", 'test the third', ], | ||
| 28 | [ UNKNOWN, "UNKNOWN", 'test the fourth', ], | ||
| 29 | [ DEPENDENT, "DEPENDENT", 'test the fifth', ], | ||
| 30 | ); | ||
| 31 | for (@ok) { | ||
| 32 | # CONSTANT | ||
| 33 | $r = $np->nagios_exit($_->[0], $_->[2]); | ||
| 34 | is($r->return_code, $_->[0], | ||
| 35 | sprintf('nagios_exit(%s, $msg) returned %s', $_->[1], $_->[0])); | ||
| 36 | like($r->message, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/, | ||
| 37 | sprintf('nagios_exit(%s, $msg) output matched "%s"', $_->[1], | ||
| 38 | $plugin . ' ' . $_->[1] . '.*' . $_->[2])); | ||
| 39 | |||
| 40 | # $string | ||
| 41 | $r = $np->nagios_exit($_->[1], $_->[2]); | ||
| 42 | is($r->return_code, $_->[0], | ||
| 43 | sprintf('nagios_exit("%s", $msg) returned %s', $_->[1], $_->[0])); | ||
| 44 | like($r->message, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/, | ||
| 45 | sprintf('nagios_exit("%s", $msg) output matched "%s"', $_->[1], | ||
| 46 | $plugin . ' ' . $_->[1] . '.*' . $_->[2])); | ||
| 47 | like($r, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/, | ||
| 48 | sprintf('nagios_exit("%s", $msg) stringified matched "%s"', $_->[1], | ||
| 49 | $plugin . ' ' . $_->[1] . '.*' . $_->[2])); | ||
| 50 | } | ||
| 51 | |||
| 52 | # nagios_exit code corner cases | ||
| 53 | my @ugly1 = ( | ||
| 54 | [ -1, 'testing code -1' ], | ||
| 55 | [ 7, 'testing code 7' ], | ||
| 56 | [ undef, 'testing code undef' ], | ||
| 57 | [ '', qq(testing code '') ], | ||
| 58 | [ 'string', qq(testing code 'string') ], | ||
| 59 | ); | ||
| 60 | for (@ugly1) { | ||
| 61 | $r = $np->nagios_exit($_->[0], $_->[1]); | ||
| 62 | my $display = defined $_->[0] ? "'$_->[0]'" : 'undef'; | ||
| 63 | is($r->return_code, UNKNOWN, "nagios_exit($display, \$msg) returned ". UNKNOWN); | ||
| 64 | like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/, | ||
| 65 | sprintf('nagios_exit(%s, $msg) output matched "%s"', | ||
| 66 | $display, 'UNKNOWN.*' . $_->[1])); | ||
| 67 | } | ||
| 68 | |||
| 69 | # nagios_exit message corner cases | ||
| 70 | my @ugly2 = ( | ||
| 71 | [ '' ], | ||
| 72 | [ undef ], | ||
| 73 | [ UNKNOWN ], | ||
| 74 | ); | ||
| 75 | for (@ugly2) { | ||
| 76 | $r = $np->nagios_exit(CRITICAL, $_->[0]); | ||
| 77 | my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; | ||
| 78 | my $display2 = defined $_->[0] ? $_->[0] : ''; | ||
| 79 | like($r->message, qr/CRITICAL\b.*\b$display2$/, | ||
| 80 | sprintf('nagios_exit(%s, $msg) output matched "%s"', | ||
| 81 | $display1, "CRITICAL.*$display2")); | ||
| 82 | } | ||
| 83 | |||
| 84 | # Test nagios_die( $msg ) | ||
| 85 | my @msg = ( | ||
| 86 | [ 'die you dog' ], | ||
| 87 | [ '' ], | ||
| 88 | [ undef ], | ||
| 89 | ); | ||
| 90 | for (@msg) { | ||
| 91 | $r = $np->nagios_die($_->[0]); | ||
| 92 | my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; | ||
| 93 | my $display2 = defined $_->[0] ? $_->[0] : ''; | ||
| 94 | is($r->return_code, UNKNOWN, | ||
| 95 | sprintf('nagios_die(%s) returned UNKNOWN', $display1)); | ||
| 96 | like($r->message, qr/UNKNOWN\b.*\b$display2$/, | ||
| 97 | sprintf('nagios_die(%s) output matched "%s"', $display1, | ||
| 98 | "UNKNOWN.*$display2")); | ||
| 99 | } | ||
| 100 | |||
| 101 | # Test nagios_die( CONSTANT, $msg ), nagios_die( $msg, CONSTANT ), | ||
| 102 | # nagios_die( $string, $msg ), and nagios_die( $msg, $string ) | ||
| 103 | @ok = ( | ||
| 104 | [ OK, "OK", 'test the first', ], | ||
| 105 | [ WARNING, "WARNING", 'test the second', ], | ||
| 106 | [ CRITICAL, "CRITICAL", 'test the third', ], | ||
| 107 | [ UNKNOWN, "UNKNOWN", 'test the fourth', ], | ||
| 108 | [ DEPENDENT, "DEPENDENT", 'test the fifth', ], | ||
| 109 | ); | ||
| 110 | for (@ok) { | ||
| 111 | # CONSTANT, $msg | ||
| 112 | $r = $np->nagios_die($_->[0], $_->[2]); | ||
| 113 | is($r->return_code, $_->[0], | ||
| 114 | sprintf('nagios_die(%s, $msg) returned %s', $_->[1], $_->[0])); | ||
| 115 | like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, | ||
| 116 | sprintf('nagios_die(%s, $msg) output matched "%s"', | ||
| 117 | $_->[1], $_->[1] . '.*' . $_->[2])); | ||
| 118 | |||
| 119 | # $msg, CONSTANT | ||
| 120 | $r = $np->nagios_die($_->[2], $_->[0]); | ||
| 121 | is($r->return_code, $_->[0], | ||
| 122 | sprintf('nagios_die($msg, %s) returned %s', $_->[1], $_->[0])); | ||
| 123 | like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, | ||
| 124 | sprintf('nagios_die($msg, %s) output matched "%s"', | ||
| 125 | $_->[1], $_->[1] . '.*' . $_->[2])); | ||
| 126 | |||
| 127 | # $string, $msg | ||
| 128 | $r = $np->nagios_die($_->[1], $_->[2]); | ||
| 129 | is($r->return_code, $_->[0], | ||
| 130 | sprintf('nagios_die("%s", $msg) returned %s', $_->[1], $_->[0])); | ||
| 131 | like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, | ||
| 132 | sprintf('nagios_die("%s", $msg) output matched "%s"', $_->[1], | ||
| 133 | $_->[1] . '.*' . $_->[2])); | ||
| 134 | like($r, qr/$_->[1]\b.*\b$_->[2]$/, | ||
| 135 | sprintf('nagios_die("%s", $msg) stringified matched "%s"', $_->[1], | ||
| 136 | $_->[1] . '.*' . $_->[2])); | ||
| 137 | |||
| 138 | # $string, $msg | ||
| 139 | $r = $np->nagios_die($_->[2], $_->[1]); | ||
| 140 | is($r->return_code, $_->[0], | ||
| 141 | sprintf('nagios_die($msg, "%s") returned %s', $_->[1], $_->[0])); | ||
| 142 | like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, | ||
| 143 | sprintf('nagios_die($msg, "%s") output matched "%s"', $_->[1], | ||
| 144 | $_->[1] . '.*' . $_->[2])); | ||
| 145 | like($r, qr/$_->[1]\b.*\b$_->[2]$/, | ||
| 146 | sprintf('nagios_die($msg, "%s") stringified matched "%s"', $_->[1], | ||
| 147 | $_->[1] . '.*' . $_->[2])); | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | # shortname testing | ||
| 152 | SKIP: { | ||
| 153 | skip "requires File::Basename", 2 unless eval { require File::Basename }; | ||
| 154 | $np = Nagios::Plugin->new( version => "1"); | ||
| 155 | $plugin = uc File::Basename::basename($0); | ||
| 156 | $plugin =~ s/\..*$//; | ||
| 157 | is($np->shortname, $plugin, "shortname() is '$plugin'"); | ||
| 158 | $r = $np->nagios_exit(OK, "foobar"); | ||
| 159 | like($r->message, qr/^$plugin OK/, "message begins with '$plugin OK'"); | ||
| 160 | } | ||
| 161 | |||
