diff options
Diffstat (limited to 't/Monitoring-Plugin-02.t')
-rw-r--r-- | t/Monitoring-Plugin-02.t | 160 |
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 | |||
3 | use strict; | ||
4 | use Test::More tests => 103; | ||
5 | |||
6 | BEGIN { use_ok("Monitoring::Plugin") } | ||
7 | require Monitoring::Plugin::Functions; | ||
8 | Monitoring::Plugin::Functions::_fake_exit(1); | ||
9 | |||
10 | # Hardcoded checks of constants | ||
11 | my %ERRORS = %Monitoring::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 = Monitoring::Plugin->new( shortname => $plugin ); | ||
20 | is($np->shortname, $plugin, "shortname() is $plugin"); | ||
21 | |||
22 | # Test plugin_exit( CONSTANT, $msg ), plugin_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->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 | ||
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->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 | ||
70 | my @ugly2 = ( | ||
71 | [ '' ], | ||
72 | [ undef ], | ||
73 | [ UNKNOWN ], | ||
74 | ); | ||
75 | for (@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 ) | ||
85 | my @msg = ( | ||
86 | [ 'die you dog' ], | ||
87 | [ '' ], | ||
88 | [ undef ], | ||
89 | ); | ||
90 | for (@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 | ); | ||
110 | for (@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 | ||
152 | SKIP: { | ||
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 | } | ||