summaryrefslogtreecommitdiffstats
path: root/plugins/tests
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2017-04-14 18:04:51 (GMT)
committerSven Nierlein <sven@nierlein.de>2018-10-22 14:30:31 (GMT)
commitbbec77c7ecbc6ecaac06d2c8845f83a22546f273 (patch)
treeff7685bd37f2027876858c03d7b3fa081f239e29 /plugins/tests
parentf890d271fd2a39121bf1eede5b5fd4eb4cdf6dfd (diff)
downloadmonitoring-plugins-bbec77c7ecbc6ecaac06d2c8845f83a22546f273.tar.gz
made check_curl tests copies of check_http tests because they will differ slightly
Diffstat (limited to 'plugins/tests')
-rwxr-xr-x[l---------]plugins/tests/check_curl.t419
1 files changed, 418 insertions, 1 deletions
diff --git a/plugins/tests/check_curl.t b/plugins/tests/check_curl.t
index a54db96..dd56706 120000..100755
--- a/plugins/tests/check_curl.t
+++ b/plugins/tests/check_curl.t
@@ -1 +1,418 @@
1check_http.t \ No newline at end of file 1#! /usr/bin/perl -w -I ..
2#
3# Test check_http by having an actual HTTP server running
4#
5# To create the https server certificate:
6# openssl req -new -x509 -keyout server-key.pem -out server-cert.pem -days 3650 -nodes
7# Country Name (2 letter code) [AU]:UK
8# State or Province Name (full name) [Some-State]:Derbyshire
9# Locality Name (eg, city) []:Belper
10# Organization Name (eg, company) [Internet Widgits Pty Ltd]:Monitoring Plugins
11# Organizational Unit Name (eg, section) []:
12# Common Name (eg, YOUR name) []:Ton Voon
13# Email Address []:tonvoon@mac.com
14
15use strict;
16use Test::More;
17use NPTest;
18use FindBin qw($Bin);
19
20$ENV{'LC_TIME'} = "C";
21
22my $common_tests = 70;
23my $ssl_only_tests = 8;
24# Check that all dependent modules are available
25eval "use HTTP::Daemon 6.01;";
26plan skip_all => 'HTTP::Daemon >= 6.01 required' if $@;
27eval {
28 require HTTP::Status;
29 require HTTP::Response;
30};
31
32my $plugin = 'check_http';
33$plugin = 'check_curl' if $0 =~ m/check_curl/mx;
34
35if ($@) {
36 plan skip_all => "Missing required module for test: $@";
37} else {
38 if (-x "./$plugin") {
39 plan tests => $common_tests * 2 + $ssl_only_tests;
40 } else {
41 plan skip_all => "No $plugin compiled";
42 }
43}
44
45my $servers = { http => 0 }; # HTTP::Daemon should always be available
46eval { require HTTP::Daemon::SSL };
47if ($@) {
48 diag "Cannot load HTTP::Daemon::SSL: $@";
49} else {
50 $servers->{https} = 0;
51}
52
53# set a fixed version, so the header size doesn't vary
54$HTTP::Daemon::VERSION = "1.00";
55
56my $port_http = 50000 + int(rand(1000));
57my $port_https = $port_http + 1;
58my $port_https_expired = $port_http + 2;
59
60# This array keeps sockets around for implementing timeouts
61my @persist;
62
63# Start up all servers
64my @pids;
65my $pid = fork();
66if ($pid) {
67 # Parent
68 push @pids, $pid;
69 if (exists $servers->{https}) {
70 # Fork a normal HTTPS server
71 $pid = fork();
72 if ($pid) {
73 # Parent
74 push @pids, $pid;
75 # Fork an expired cert server
76 $pid = fork();
77 if ($pid) {
78 push @pids, $pid;
79 } else {
80 my $d = HTTP::Daemon::SSL->new(
81 LocalPort => $port_https_expired,
82 LocalAddr => "127.0.0.1",
83 SSL_cert_file => "$Bin/certs/expired-cert.pem",
84 SSL_key_file => "$Bin/certs/expired-key.pem",
85 ) || die;
86 print "Please contact https expired at: <URL:", $d->url, ">\n";
87 run_server( $d );
88 exit;
89 }
90 } else {
91 my $d = HTTP::Daemon::SSL->new(
92 LocalPort => $port_https,
93 LocalAddr => "127.0.0.1",
94 SSL_cert_file => "$Bin/certs/server-cert.pem",
95 SSL_key_file => "$Bin/certs/server-key.pem",
96 ) || die;
97 print "Please contact https at: <URL:", $d->url, ">\n";
98 run_server( $d );
99 exit;
100 }
101 }
102 # give our webservers some time to startup
103 sleep(1);
104} else {
105 # Child
106 #print "child\n";
107 my $d = HTTP::Daemon->new(
108 LocalPort => $port_http,
109 LocalAddr => "127.0.0.1",
110 ) || die;
111 print "Please contact http at: <URL:", $d->url, ">\n";
112 run_server( $d );
113 exit;
114}
115
116# Run the same server on http and https
117sub run_server {
118 my $d = shift;
119 MAINLOOP: while (my $c = $d->accept ) {
120 while (my $r = $c->get_request) {
121 if ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) {
122 $c->send_basic_header($1);
123 $c->send_crlf;
124 } elsif ($r->method eq "GET" and $r->url->path =~ m^/file/(.*)^) {
125 $c->send_basic_header;
126 $c->send_crlf;
127 $c->send_file_response("$Bin/var/$1");
128 } elsif ($r->method eq "GET" and $r->url->path eq "/slow") {
129 $c->send_basic_header;
130 $c->send_crlf;
131 sleep 1;
132 $c->send_response("slow");
133 } elsif ($r->url->path eq "/method") {
134 if ($r->method eq "DELETE") {
135 $c->send_error(HTTP::Status->RC_METHOD_NOT_ALLOWED);
136 } elsif ($r->method eq "foo") {
137 $c->send_error(HTTP::Status->RC_NOT_IMPLEMENTED);
138 } else {
139 $c->send_status_line(200, $r->method);
140 }
141 } elsif ($r->url->path eq "/postdata") {
142 $c->send_basic_header;
143 $c->send_crlf;
144 $c->send_response($r->method.":".$r->content);
145 } elsif ($r->url->path eq "/redirect") {
146 $c->send_redirect( "/redirect2" );
147 } elsif ($r->url->path eq "/redir_external") {
148 $c->send_redirect(($d->isa('HTTP::Daemon::SSL') ? "https" : "http") . "://169.254.169.254/redirect2" );
149 } elsif ($r->url->path eq "/redirect2") {
150 $c->send_basic_header;
151 $c->send_crlf;
152 $c->send_response(HTTP::Response->new( 200, 'OK', undef, 'redirected' ));
153 } elsif ($r->url->path eq "/redir_timeout") {
154 $c->send_redirect( "/timeout" );
155 } elsif ($r->url->path eq "/timeout") {
156 # Keep $c from being destroyed, but prevent severe leaks
157 unshift @persist, $c;
158 delete($persist[1000]);
159 next MAINLOOP;
160 } elsif ($r->url->path eq "/header_check") {
161 $c->send_basic_header;
162 $c->send_header('foo');
163 $c->send_crlf;
164 } else {
165 $c->send_error(HTTP::Status->RC_FORBIDDEN);
166 }
167 $c->close;
168 }
169 }
170}
171
172END {
173 foreach my $pid (@pids) {
174 if ($pid) { print "Killing $pid\n"; kill "INT", $pid }
175 }
176};
177
178if ($ARGV[0] && $ARGV[0] eq "-d") {
179 while (1) {
180 sleep 100;
181 }
182}
183
184my $result;
185my $command = "./$plugin -H 127.0.0.1";
186
187run_common_tests( { command => "$command -p $port_http" } );
188SKIP: {
189 skip "HTTP::Daemon::SSL not installed", $common_tests + $ssl_only_tests if ! exists $servers->{https};
190 run_common_tests( { command => "$command -p $port_https", ssl => 1 } );
191
192 $result = NPTest->testCmd( "$command -p $port_https -S -C 14" );
193 is( $result->return_code, 0, "$command -p $port_https -S -C 14" );
194 is( $result->output, 'OK - Certificate \'Ton Voon\' will expire on Sun Mar 3 21:41:28 2019 +0000.', "output ok" );
195
196 $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" );
197 is( $result->return_code, 1, "$command -p $port_https -S -C 14000" );
198 like( $result->output, '/WARNING - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar 3 21:41:28 2019 \+0000\)./', "output ok" );
199
200 # Expired cert tests
201 $result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" );
202 is( $result->return_code, 2, "$command -p $port_https -S -C 13960,14000" );
203 like( $result->output, '/CRITICAL - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar 3 21:41:28 2019 \+0000\)./', "output ok" );
204
205 $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" );
206 is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" );
207 is( $result->output,
208 'CRITICAL - Certificate \'Ton Voon\' expired on Thu Mar 5 00:13:16 2009 +0000.',
209 "output ok" );
210
211}
212
213sub run_common_tests {
214 my ($opts) = @_;
215 my $command = $opts->{command};
216 if ($opts->{ssl}) {
217 $command .= " --ssl";
218 }
219
220 $result = NPTest->testCmd( "$command -u /file/root" );
221 is( $result->return_code, 0, "/file/root");
222 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
223
224 $result = NPTest->testCmd( "$command -u /file/root -s Root" );
225 is( $result->return_code, 0, "/file/root search for string");
226 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
227
228 $result = NPTest->testCmd( "$command -u /file/root -s NonRoot" );
229 is( $result->return_code, 2, "Missing string check");
230 like( $result->output, qr%^HTTP CRITICAL: HTTP/1\.1 200 OK - string 'NonRoot' not found on 'https?://127\.0\.0\.1:\d+/file/root'%, "Shows search string and location");
231
232 $result = NPTest->testCmd( "$command -u /file/root -s NonRootWithOver30charsAndMoreFunThanAWetFish" );
233 is( $result->return_code, 2, "Missing string check");
234 like( $result->output, qr%HTTP CRITICAL: HTTP/1\.1 200 OK - string 'NonRootWithOver30charsAndM...' not found on 'https?://127\.0\.0\.1:\d+/file/root'%, "Shows search string and location");
235
236 $result = NPTest->testCmd( "$command -u /header_check -d foo" );
237 is( $result->return_code, 0, "header_check search for string");
238 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 96 bytes in [\d\.]+ second/', "Output correct" );
239
240 $result = NPTest->testCmd( "$command -u /header_check -d bar" );
241 is( $result->return_code, 2, "Missing header string check");
242 like( $result->output, qr%^HTTP CRITICAL: HTTP/1\.1 200 OK - header 'bar' not found on 'https?://127\.0\.0\.1:\d+/header_check'%, "Shows search string and location");
243
244 my $cmd;
245 $cmd = "$command -u /slow";
246 $result = NPTest->testCmd( $cmd );
247 is( $result->return_code, 0, "$cmd");
248 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
249 $result->output =~ /in ([\d\.]+) second/;
250 cmp_ok( $1, ">", 1, "Time is > 1 second" );
251
252 $cmd = "$command -u /statuscode/200";
253 $result = NPTest->testCmd( $cmd );
254 is( $result->return_code, 0, $cmd);
255 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
256
257 $cmd = "$command -u /statuscode/200 -e 200";
258 $result = NPTest->testCmd( $cmd );
259 is( $result->return_code, 0, $cmd);
260 like( $result->output, '/^HTTP OK: Status line output matched "200" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
261
262 $cmd = "$command -u /statuscode/201";
263 $result = NPTest->testCmd( $cmd );
264 is( $result->return_code, 0, $cmd);
265 like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
266
267 $cmd = "$command -u /statuscode/201 -e 201";
268 $result = NPTest->testCmd( $cmd );
269 is( $result->return_code, 0, $cmd);
270 like( $result->output, '/^HTTP OK: Status line output matched "201" - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
271
272 $cmd = "$command -u /statuscode/201 -e 200";
273 $result = NPTest->testCmd( $cmd );
274 is( $result->return_code, 2, $cmd);
275 like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output );
276
277 $cmd = "$command -u /statuscode/200 -e 200,201,202";
278 $result = NPTest->testCmd( $cmd );
279 is( $result->return_code, 0, $cmd);
280 like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
281
282 $cmd = "$command -u /statuscode/201 -e 200,201,202";
283 $result = NPTest->testCmd( $cmd );
284 is( $result->return_code, 0, $cmd);
285 like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
286
287 $cmd = "$command -u /statuscode/203 -e 200,201,202";
288 $result = NPTest->testCmd( $cmd );
289 is( $result->return_code, 2, $cmd);
290 like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port (\d+): HTTP/1.1 203 Non-Authoritative Information/', "Output correct: ".$result->output );
291
292 $cmd = "$command -j HEAD -u /method";
293 $result = NPTest->testCmd( $cmd );
294 is( $result->return_code, 0, $cmd);
295 like( $result->output, '/^HTTP OK: HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
296
297 $cmd = "$command -j POST -u /method";
298 $result = NPTest->testCmd( $cmd );
299 is( $result->return_code, 0, $cmd);
300 like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
301
302 $cmd = "$command -j GET -u /method";
303 $result = NPTest->testCmd( $cmd );
304 is( $result->return_code, 0, $cmd);
305 like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
306
307 $cmd = "$command -u /method";
308 $result = NPTest->testCmd( $cmd );
309 is( $result->return_code, 0, $cmd);
310 like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
311
312 $cmd = "$command -P foo -u /method";
313 $result = NPTest->testCmd( $cmd );
314 is( $result->return_code, 0, $cmd);
315 like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
316
317 $cmd = "$command -j DELETE -u /method";
318 $result = NPTest->testCmd( $cmd );
319 is( $result->return_code, 1, $cmd);
320 like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output );
321
322 $cmd = "$command -j foo -u /method";
323 $result = NPTest->testCmd( $cmd );
324 is( $result->return_code, 2, $cmd);
325 like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output );
326
327 $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude";
328 $result = NPTest->testCmd( $cmd );
329 is( $result->return_code, 0, $cmd);
330 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
331
332 $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude";
333 $result = NPTest->testCmd( $cmd );
334 is( $result->return_code, 0, $cmd);
335 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
336
337 # To confirm that the free doesn't segfault
338 $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude";
339 $result = NPTest->testCmd( $cmd );
340 is( $result->return_code, 0, $cmd);
341 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
342
343 $cmd = "$command -u /redirect";
344 $result = NPTest->testCmd( $cmd );
345 is( $result->return_code, 0, $cmd);
346 like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
347
348 $cmd = "$command -f follow -u /redirect";
349 $result = NPTest->testCmd( $cmd );
350 is( $result->return_code, 0, $cmd);
351 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
352
353 $cmd = "$command -u /redirect -k 'follow: me'";
354 $result = NPTest->testCmd( $cmd );
355 is( $result->return_code, 0, $cmd);
356 like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
357
358 $cmd = "$command -f follow -u /redirect -k 'follow: me'";
359 $result = NPTest->testCmd( $cmd );
360 is( $result->return_code, 0, $cmd);
361 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
362
363 $cmd = "$command -f sticky -u /redirect -k 'follow: me'";
364 $result = NPTest->testCmd( $cmd );
365 is( $result->return_code, 0, $cmd);
366 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
367
368 $cmd = "$command -f stickyport -u /redirect -k 'follow: me'";
369 $result = NPTest->testCmd( $cmd );
370 is( $result->return_code, 0, $cmd);
371 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
372
373 # These tests may block
374 print "ALRM\n";
375
376 # stickyport - on full urlS port is set back to 80 otherwise
377 $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected";
378 eval {
379 local $SIG{ALRM} = sub { die "alarm\n" };
380 alarm(2);
381 $result = NPTest->testCmd( $cmd );
382 alarm(0); };
383 isnt( $@, "alarm\n", $cmd );
384 is( $result->return_code, 0, $cmd );
385
386 # Let's hope there won't be any web server on :80 returning "redirected"!
387 $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected";
388 eval {
389 local $SIG{ALRM} = sub { die "alarm\n" };
390 alarm(2);
391 $result = NPTest->testCmd( $cmd );
392 alarm(0); };
393 isnt( $@, "alarm\n", $cmd );
394 isnt( $result->return_code, 0, $cmd );
395
396 # Test an external address - timeout
397 SKIP: {
398 skip "This doesn't seems to work all the time", 1 unless ($ENV{HTTP_EXTERNAL});
399 $cmd = "$command -f follow -u /redir_external -t 5";
400 eval {
401 $result = NPTest->testCmd( $cmd, 2 );
402 };
403 like( $@, "/timeout in command: $cmd/", $cmd );
404 }
405
406 $cmd = "$command -u /timeout -t 5";
407 eval {
408 $result = NPTest->testCmd( $cmd, 2 );
409 };
410 like( $@, "/timeout in command: $cmd/", $cmd );
411
412 $cmd = "$command -f follow -u /redir_timeout -t 2";
413 eval {
414 $result = NPTest->testCmd( $cmd, 5 );
415 };
416 is( $@, "", $cmd );
417
418}