summaryrefslogtreecommitdiffstats
path: root/t/Monitoring-Plugin-Functions-01.t
diff options
context:
space:
mode:
Diffstat (limited to 't/Monitoring-Plugin-Functions-01.t')
-rw-r--r--t/Monitoring-Plugin-Functions-01.t161
1 files changed, 161 insertions, 0 deletions
diff --git a/t/Monitoring-Plugin-Functions-01.t b/t/Monitoring-Plugin-Functions-01.t
new file mode 100644
index 0000000..084ad28
--- /dev/null
+++ b/t/Monitoring-Plugin-Functions-01.t
@@ -0,0 +1,161 @@
1
2use strict;
3use Test::More tests => 113;
4
5BEGIN { use_ok("Monitoring::Plugin::Functions", ":all"); }
6Monitoring::Plugin::Functions::_fake_exit(1);
7
8my $this_version=$Monitoring::Plugin::Functions::VERSION;
9foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) {
10 my $mod = "Monitoring::Plugin$m";
11 use_ok($mod);
12 # Lots of hackery below. Easier to say $mod->VERSION, but this is probably a recent perl thing
13 my $v = "$mod"."::VERSION";
14 my $a = eval "\$$v";
15 is($a, $this_version, "Version number for $mod the same as Functions: $this_version");
16}
17
18# check get_shortname
19is(get_shortname, "MONITORING-PLUGIN-FUNCTIONS-01", "get_shortname ok");
20
21# Hardcoded checks of constants
22ok(%ERRORS, '%ERRORS defined');
23is(OK, $ERRORS{OK}, "OK => $ERRORS{OK}");
24is(WARNING, $ERRORS{WARNING}, "WARNING => $ERRORS{WARNING}");
25is(CRITICAL, $ERRORS{CRITICAL}, "CRITICAL => $ERRORS{CRITICAL}");
26is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}");
27is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}");
28
29# Test plugin_exit( CONSTANT, $msg ), plugin_exit( $string, $msg )
30my $r;
31my @ok = (
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', ],
37);
38for (@ok) {
39 # CONSTANT
40 $r = plugin_exit($_->[0], $_->[2]);
41 is($r->return_code, $_->[0],
42 sprintf('plugin_exit(%s, $msg) returned %s', $_->[1], $_->[0]));
43 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
44 sprintf('plugin_exit(%s, $msg) output matched "%s"',
45 $_->[1], $_->[1] . '.*' . $_->[2]));
46
47 # $string
48 $r = plugin_exit($_->[1], $_->[2]);
49 is($r->return_code, $_->[0],
50 sprintf('plugin_exit("%s", $msg) returned %s', $_->[1], $_->[0]));
51 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
52 sprintf('plugin_exit("%s", $msg) output matched "%s"', $_->[1],
53 $_->[1] . '.*' . $_->[2]));
54 like($r, qr/$_->[1]\b.*\b$_->[2]$/,
55 sprintf('plugin_exit("%s", $msg) stringified matched "%s"', $_->[1],
56 $_->[1] . '.*' . $_->[2]));
57}
58
59# plugin_exit code corner cases
60my @ugly1 = (
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') ],
66);
67for (@ugly1) {
68 $r = plugin_exit($_->[0], $_->[1]);
69 my $display = defined $_->[0] ? "'$_->[0]'" : 'undef';
70 is($r->return_code, UNKNOWN, "plugin_exit($display, \$msg) returned ". UNKNOWN);
71 like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/,
72 sprintf('plugin_exit(%s, $msg) output matched "%s"',
73 $display, 'UNKNOWN.*' . $_->[1]));
74}
75
76# plugin_exit message corner cases
77my @ugly2 = (
78 [ '' ],
79 [ undef ],
80 [ UNKNOWN ],
81);
82for (@ugly2) {
83 $r = plugin_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('plugin_exit(%s, $msg) output matched "%s"',
88 $display1, "CRITICAL.*$display2"));
89}
90
91# Test plugin_die( $msg )
92my @msg = (
93 [ 'die you dog' ],
94 [ '' ],
95 [ undef ],
96);
97for (@msg) {
98 $r = plugin_die($_->[0]);
99 my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef";
100 my $display2 = defined $_->[0] ? $_->[0] : '';
101 is($r->return_code, UNKNOWN,
102 sprintf('plugin_die(%s) returned UNKNOWN', $display1));
103 like($r->message, qr/UNKNOWN\b.*\b$display2$/,
104 sprintf('plugin_die(%s) output matched "%s"', $display1,
105 "UNKNOWN.*$display2"));
106}
107
108# Test plugin_die( CONSTANT, $msg ), plugin_die( $msg, CONSTANT ),
109# plugin_die( $string, $msg ), and plugin_die( $msg, $string )
110@ok = (
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', ],
116);
117for (@ok) {
118 # CONSTANT, $msg
119 $r = plugin_die($_->[0], $_->[2]);
120 is($r->return_code, $_->[0],
121 sprintf('plugin_die(%s, $msg) returned %s', $_->[1], $_->[0]));
122 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
123 sprintf('plugin_die(%s, $msg) output matched "%s"',
124 $_->[1], $_->[1] . '.*' . $_->[2]));
125
126 # $msg, CONSTANT
127 $r = plugin_die($_->[2], $_->[0]);
128 is($r->return_code, $_->[0],
129 sprintf('plugin_die($msg, %s) returned %s', $_->[1], $_->[0]));
130 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
131 sprintf('plugin_die($msg, %s) output matched "%s"',
132 $_->[1], $_->[1] . '.*' . $_->[2]));
133
134 # $string, $msg
135 $r = plugin_die($_->[1], $_->[2]);
136 is($r->return_code, $_->[0],
137 sprintf('plugin_die("%s", $msg) returned %s', $_->[1], $_->[0]));
138 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
139 sprintf('plugin_die("%s", $msg) output matched "%s"', $_->[1],
140 $_->[1] . '.*' . $_->[2]));
141 like($r, qr/$_->[1]\b.*\b$_->[2]$/,
142 sprintf('plugin_die("%s", $msg) stringified matched "%s"', $_->[1],
143 $_->[1] . '.*' . $_->[2]));
144
145 # $string, $msg
146 $r = plugin_die($_->[2], $_->[1]);
147 is($r->return_code, $_->[0],
148 sprintf('plugin_die($msg, "%s") returned %s', $_->[1], $_->[0]));
149 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
150 sprintf('plugin_die($msg, "%s") output matched "%s"', $_->[1],
151 $_->[1] . '.*' . $_->[2]));
152 like($r, qr/$_->[1]\b.*\b$_->[2]$/,
153 sprintf('plugin_die($msg, "%s") stringified matched "%s"', $_->[1],
154 $_->[1] . '.*' . $_->[2]));
155}
156
157# Check that _use_die set to 1 will catch exceptions correctly
158Monitoring::Plugin::Functions::_fake_exit(0);
159Monitoring::Plugin::Functions::_use_die(1);
160eval { plugin_die("Using die") };
161is( $@, "MONITORING-PLUGIN-FUNCTIONS-01 UNKNOWN - Using die\n", "Caught exception");