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