3 use Test::More tests => 113;
5 BEGIN { use_ok("Nagios::Monitoring::Plugin::Functions", ":all"); }
6 Nagios::Monitoring::Plugin::Functions::_fake_exit(1);
8 my $this_version=$Nagios::Monitoring::Plugin::Functions::VERSION;
9 foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) {
10 my $mod = "Nagios::Monitoring::Plugin$m";
12 # Lots of hackery below. Easier to say $mod->VERSION, but this is probably a recent perl thing
13 my $v = "$mod"."::VERSION";
15 is($a, $this_version, "Version number for $mod the same as Functions: $this_version");
19 is(get_shortname, "NAGIOS-MONITORING-PLUGIN-FUNCTIONS-01", "get_shortname ok");
21 # Hardcoded checks of constants
22 ok(%ERRORS, '%ERRORS defined');
23 is(OK, $ERRORS{OK}, "OK => $ERRORS{OK}");
24 is(WARNING, $ERRORS{WARNING}, "WARNING => $ERRORS{WARNING}");
25 is(CRITICAL, $ERRORS{CRITICAL}, "CRITICAL => $ERRORS{CRITICAL}");
26 is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}");
27 is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}");
29 # Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg )
32 [ OK, "OK", 'test the first', ],
33 [ WARNING, "WARNING", 'test the second', ],
34 [ CRITICAL, "CRITICAL", 'test the third', ],
35 [ UNKNOWN, "UNKNOWN", 'test the fourth', ],
36 [ DEPENDENT, "DEPENDENT", 'test the fifth', ],
40 $r = nagios_exit($_->[0], $_->[2]);
41 is($r->return_code, $_->[0],
42 sprintf('nagios_exit(%s, $msg) returned %s', $_->[1], $_->[0]));
43 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
44 sprintf('nagios_exit(%s, $msg) output matched "%s"',
45 $_->[1], $_->[1] . '.*' . $_->[2]));
48 $r = nagios_exit($_->[1], $_->[2]);
49 is($r->return_code, $_->[0],
50 sprintf('nagios_exit("%s", $msg) returned %s', $_->[1], $_->[0]));
51 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
52 sprintf('nagios_exit("%s", $msg) output matched "%s"', $_->[1],
53 $_->[1] . '.*' . $_->[2]));
54 like($r, qr/$_->[1]\b.*\b$_->[2]$/,
55 sprintf('nagios_exit("%s", $msg) stringified matched "%s"', $_->[1],
56 $_->[1] . '.*' . $_->[2]));
59 # nagios_exit code corner cases
61 [ -1, 'testing code -1' ],
62 [ 7, 'testing code 7' ],
63 [ undef, 'testing code undef' ],
64 [ '', qq(testing code '') ],
65 [ 'string', qq(testing code 'string') ],
68 $r = nagios_exit($_->[0], $_->[1]);
69 my $display = defined $_->[0] ? "'$_->[0]'" : 'undef';
70 is($r->return_code, UNKNOWN, "nagios_exit($display, \$msg) returned ". UNKNOWN);
71 like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/,
72 sprintf('nagios_exit(%s, $msg) output matched "%s"',
73 $display, 'UNKNOWN.*' . $_->[1]));
76 # nagios_exit message corner cases
83 $r = nagios_exit(CRITICAL, $_->[0]);
84 my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef";
85 my $display2 = defined $_->[0] ? $_->[0] : '';
86 like($r->message, qr/CRITICAL\b.*\b$display2$/,
87 sprintf('nagios_exit(%s, $msg) output matched "%s"',
88 $display1, "CRITICAL.*$display2"));
91 # Test nagios_die( $msg )
98 $r = nagios_die($_->[0]);
99 my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef";
100 my $display2 = defined $_->[0] ? $_->[0] : '';
101 is($r->return_code, UNKNOWN,
102 sprintf('nagios_die(%s) returned UNKNOWN', $display1));
103 like($r->message, qr/UNKNOWN\b.*\b$display2$/,
104 sprintf('nagios_die(%s) output matched "%s"', $display1,
105 "UNKNOWN.*$display2"));
108 # Test nagios_die( CONSTANT, $msg ), nagios_die( $msg, CONSTANT ),
109 # nagios_die( $string, $msg ), and nagios_die( $msg, $string )
111 [ OK, "OK", 'test the first', ],
112 [ WARNING, "WARNING", 'test the second', ],
113 [ CRITICAL, "CRITICAL", 'test the third', ],
114 [ UNKNOWN, "UNKNOWN", 'test the fourth', ],
115 [ DEPENDENT, "DEPENDENT", 'test the fifth', ],
119 $r = nagios_die($_->[0], $_->[2]);
120 is($r->return_code, $_->[0],
121 sprintf('nagios_die(%s, $msg) returned %s', $_->[1], $_->[0]));
122 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
123 sprintf('nagios_die(%s, $msg) output matched "%s"',
124 $_->[1], $_->[1] . '.*' . $_->[2]));
127 $r = nagios_die($_->[2], $_->[0]);
128 is($r->return_code, $_->[0],
129 sprintf('nagios_die($msg, %s) returned %s', $_->[1], $_->[0]));
130 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
131 sprintf('nagios_die($msg, %s) output matched "%s"',
132 $_->[1], $_->[1] . '.*' . $_->[2]));
135 $r = nagios_die($_->[1], $_->[2]);
136 is($r->return_code, $_->[0],
137 sprintf('nagios_die("%s", $msg) returned %s', $_->[1], $_->[0]));
138 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
139 sprintf('nagios_die("%s", $msg) output matched "%s"', $_->[1],
140 $_->[1] . '.*' . $_->[2]));
141 like($r, qr/$_->[1]\b.*\b$_->[2]$/,
142 sprintf('nagios_die("%s", $msg) stringified matched "%s"', $_->[1],
143 $_->[1] . '.*' . $_->[2]));
146 $r = nagios_die($_->[2], $_->[1]);
147 is($r->return_code, $_->[0],
148 sprintf('nagios_die($msg, "%s") returned %s', $_->[1], $_->[0]));
149 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
150 sprintf('nagios_die($msg, "%s") output matched "%s"', $_->[1],
151 $_->[1] . '.*' . $_->[2]));
152 like($r, qr/$_->[1]\b.*\b$_->[2]$/,
153 sprintf('nagios_die($msg, "%s") stringified matched "%s"', $_->[1],
154 $_->[1] . '.*' . $_->[2]));
157 # Check that _use_die set to 1 will catch exceptions correctly
158 Nagios::Monitoring::Plugin::Functions::_fake_exit(0);
159 Nagios::Monitoring::Plugin::Functions::_use_die(1);
160 eval { nagios_die("Using die") };
161 is( $@, "NAGIOS-MONITORING-PLUGIN-FUNCTIONS-01 UNKNOWN - Using die\n", "Caught exception");