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