diff options
Diffstat (limited to 'plugins/tests/check_http.t')
-rwxr-xr-x | plugins/tests/check_http.t | 87 |
1 files changed, 67 insertions, 20 deletions
diff --git a/plugins/tests/check_http.t b/plugins/tests/check_http.t index 1bc0ecb..188f5e7 100755 --- a/plugins/tests/check_http.t +++ b/plugins/tests/check_http.t | |||
@@ -4,13 +4,15 @@ | |||
4 | # | 4 | # |
5 | # To create the https server certificate: | 5 | # To create the https server certificate: |
6 | # openssl req -new -x509 -keyout server-key.pem -out server-cert.pem -days 3650 -nodes | 6 | # openssl req -new -x509 -keyout server-key.pem -out server-cert.pem -days 3650 -nodes |
7 | # Country Name (2 letter code) [AU]:UK | 7 | # to create a new expired certificate: |
8 | # State or Province Name (full name) [Some-State]:Derbyshire | 8 | # faketime '2008-01-01 12:00:00' openssl req -new -x509 -keyout expired-key.pem -out expired-cert.pem -days 1 -nodes |
9 | # Locality Name (eg, city) []:Belper | 9 | # Country Name (2 letter code) [AU]:DE |
10 | # State or Province Name (full name) [Some-State]:Bavaria | ||
11 | # Locality Name (eg, city) []:Munich | ||
10 | # Organization Name (eg, company) [Internet Widgits Pty Ltd]:Monitoring Plugins | 12 | # Organization Name (eg, company) [Internet Widgits Pty Ltd]:Monitoring Plugins |
11 | # Organizational Unit Name (eg, section) []: | 13 | # Organizational Unit Name (eg, section) []: |
12 | # Common Name (eg, YOUR name) []:Ton Voon | 14 | # Common Name (e.g. server FQDN or YOUR name) []:Monitoring Plugins |
13 | # Email Address []:tonvoon@mac.com | 15 | # Email Address []:devel@monitoring-plugins.org |
14 | 16 | ||
15 | use strict; | 17 | use strict; |
16 | use Test::More; | 18 | use Test::More; |
@@ -20,6 +22,7 @@ use FindBin qw($Bin); | |||
20 | $ENV{'LC_TIME'} = "C"; | 22 | $ENV{'LC_TIME'} = "C"; |
21 | 23 | ||
22 | my $common_tests = 70; | 24 | my $common_tests = 70; |
25 | my $virtual_port_tests = 8; | ||
23 | my $ssl_only_tests = 8; | 26 | my $ssl_only_tests = 8; |
24 | # Check that all dependent modules are available | 27 | # Check that all dependent modules are available |
25 | eval "use HTTP::Daemon 6.01;"; | 28 | eval "use HTTP::Daemon 6.01;"; |
@@ -29,13 +32,16 @@ eval { | |||
29 | require HTTP::Response; | 32 | require HTTP::Response; |
30 | }; | 33 | }; |
31 | 34 | ||
35 | my $plugin = 'check_http'; | ||
36 | $plugin = 'check_curl' if $0 =~ m/check_curl/mx; | ||
37 | |||
32 | if ($@) { | 38 | if ($@) { |
33 | plan skip_all => "Missing required module for test: $@"; | 39 | plan skip_all => "Missing required module for test: $@"; |
34 | } else { | 40 | } else { |
35 | if (-x "./check_http") { | 41 | if (-x "./$plugin") { |
36 | plan tests => $common_tests * 2 + $ssl_only_tests; | 42 | plan tests => $common_tests * 2 + $ssl_only_tests + $virtual_port_tests; |
37 | } else { | 43 | } else { |
38 | plan skip_all => "No check_http compiled"; | 44 | plan skip_all => "No $plugin compiled"; |
39 | } | 45 | } |
40 | } | 46 | } |
41 | 47 | ||
@@ -85,6 +91,8 @@ if ($pid) { | |||
85 | exit; | 91 | exit; |
86 | } | 92 | } |
87 | } else { | 93 | } else { |
94 | # closing the connection after -C cert checks make the daemon exit with a sigpipe otherwise | ||
95 | local $SIG{'PIPE'} = 'IGNORE'; | ||
88 | my $d = HTTP::Daemon::SSL->new( | 96 | my $d = HTTP::Daemon::SSL->new( |
89 | LocalPort => $port_https, | 97 | LocalPort => $port_https, |
90 | LocalAddr => "127.0.0.1", | 98 | LocalAddr => "127.0.0.1", |
@@ -96,8 +104,6 @@ if ($pid) { | |||
96 | exit; | 104 | exit; |
97 | } | 105 | } |
98 | } | 106 | } |
99 | # give our webservers some time to startup | ||
100 | sleep(1); | ||
101 | } else { | 107 | } else { |
102 | # Child | 108 | # Child |
103 | #print "child\n"; | 109 | #print "child\n"; |
@@ -110,6 +116,9 @@ if ($pid) { | |||
110 | exit; | 116 | exit; |
111 | } | 117 | } |
112 | 118 | ||
119 | # give our webservers some time to startup | ||
120 | sleep(3); | ||
121 | |||
113 | # Run the same server on http and https | 122 | # Run the same server on http and https |
114 | sub run_server { | 123 | sub run_server { |
115 | my $d = shift; | 124 | my $d = shift; |
@@ -158,6 +167,11 @@ sub run_server { | |||
158 | $c->send_basic_header; | 167 | $c->send_basic_header; |
159 | $c->send_header('foo'); | 168 | $c->send_header('foo'); |
160 | $c->send_crlf; | 169 | $c->send_crlf; |
170 | } elsif ($r->url->path eq "/virtual_port") { | ||
171 | # return sent Host header | ||
172 | $c->send_basic_header; | ||
173 | $c->send_crlf; | ||
174 | $c->send_response(HTTP::Response->new( 200, 'OK', undef, $r->header ('Host'))); | ||
161 | } else { | 175 | } else { |
162 | $c->send_error(HTTP::Status->RC_FORBIDDEN); | 176 | $c->send_error(HTTP::Status->RC_FORBIDDEN); |
163 | } | 177 | } |
@@ -179,7 +193,7 @@ if ($ARGV[0] && $ARGV[0] eq "-d") { | |||
179 | } | 193 | } |
180 | 194 | ||
181 | my $result; | 195 | my $result; |
182 | my $command = "./check_http -H 127.0.0.1"; | 196 | my $command = "./$plugin -H 127.0.0.1"; |
183 | 197 | ||
184 | run_common_tests( { command => "$command -p $port_http" } ); | 198 | run_common_tests( { command => "$command -p $port_http" } ); |
185 | SKIP: { | 199 | SKIP: { |
@@ -188,25 +202,56 @@ SKIP: { | |||
188 | 202 | ||
189 | $result = NPTest->testCmd( "$command -p $port_https -S -C 14" ); | 203 | $result = NPTest->testCmd( "$command -p $port_https -S -C 14" ); |
190 | is( $result->return_code, 0, "$command -p $port_https -S -C 14" ); | 204 | is( $result->return_code, 0, "$command -p $port_https -S -C 14" ); |
191 | is( $result->output, 'OK - Certificate \'Ton Voon\' will expire on Sun Mar 3 21:41:28 2019 +0000.', "output ok" ); | 205 | is( $result->output, "OK - Certificate 'Monitoring Plugins' will expire on Fri Feb 16 15:31:44 2029 +0000.", "output ok" ); |
192 | 206 | ||
193 | $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" ); | 207 | $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" ); |
194 | is( $result->return_code, 1, "$command -p $port_https -S -C 14000" ); | 208 | is( $result->return_code, 1, "$command -p $port_https -S -C 14000" ); |
195 | like( $result->output, '/WARNING - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar 3 21:41:28 2019 \+0000\)./', "output ok" ); | 209 | like( $result->output, '/WARNING - Certificate \'Monitoring Plugins\' expires in \d+ day\(s\) \(Fri Feb 16 15:31:44 2029 \+0000\)./', "output ok" ); |
196 | 210 | ||
197 | # Expired cert tests | 211 | # Expired cert tests |
198 | $result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" ); | 212 | $result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" ); |
199 | is( $result->return_code, 2, "$command -p $port_https -S -C 13960,14000" ); | 213 | is( $result->return_code, 2, "$command -p $port_https -S -C 13960,14000" ); |
200 | like( $result->output, '/CRITICAL - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar 3 21:41:28 2019 \+0000\)./', "output ok" ); | 214 | like( $result->output, '/CRITICAL - Certificate \'Monitoring Plugins\' expires in \d+ day\(s\) \(Fri Feb 16 15:31:44 2029 \+0000\)./', "output ok" ); |
201 | 215 | ||
202 | $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" ); | 216 | $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" ); |
203 | is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" ); | 217 | is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" ); |
204 | is( $result->output, | 218 | is( $result->output, |
205 | 'CRITICAL - Certificate \'Ton Voon\' expired on Thu Mar 5 00:13:16 2009 +0000.', | 219 | 'CRITICAL - Certificate \'Monitoring Plugins\' expired on Wed Jan 2 11:00:26 2008 +0000.', |
206 | "output ok" ); | 220 | "output ok" ); |
207 | 221 | ||
208 | } | 222 | } |
209 | 223 | ||
224 | my $cmd; | ||
225 | # check virtual port behaviour | ||
226 | # | ||
227 | # http without virtual port | ||
228 | $cmd = "$command -p $port_http -u /virtual_port -r ^127.0.0.1:$port_http\$"; | ||
229 | $result = NPTest->testCmd( $cmd ); | ||
230 | is( $result->return_code, 0, $cmd); | ||
231 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
232 | |||
233 | # http with virtual port | ||
234 | $cmd = "$command:80 -p $port_http -u /virtual_port -r ^127.0.0.1\$"; | ||
235 | $result = NPTest->testCmd( $cmd ); | ||
236 | is( $result->return_code, 0, $cmd); | ||
237 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
238 | |||
239 | SKIP: { | ||
240 | skip "HTTP::Daemon::SSL not installed", 4 if ! exists $servers->{https}; | ||
241 | # https without virtual port | ||
242 | $cmd = "$command -p $port_https --ssl -u /virtual_port -r ^127.0.0.1:$port_https\$"; | ||
243 | $result = NPTest->testCmd( $cmd ); | ||
244 | is( $result->return_code, 0, $cmd); | ||
245 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
246 | |||
247 | # https with virtual port | ||
248 | $cmd = "$command:443 -p $port_https --ssl -u /virtual_port -r ^127.0.0.1\$"; | ||
249 | $result = NPTest->testCmd( $cmd ); | ||
250 | is( $result->return_code, 0, $cmd); | ||
251 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
252 | } | ||
253 | |||
254 | |||
210 | sub run_common_tests { | 255 | sub run_common_tests { |
211 | my ($opts) = @_; | 256 | my ($opts) = @_; |
212 | my $command = $opts->{command}; | 257 | my $command = $opts->{command}; |
@@ -372,27 +417,29 @@ sub run_common_tests { | |||
372 | 417 | ||
373 | # stickyport - on full urlS port is set back to 80 otherwise | 418 | # stickyport - on full urlS port is set back to 80 otherwise |
374 | $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected"; | 419 | $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected"; |
420 | alarm(2); | ||
375 | eval { | 421 | eval { |
376 | local $SIG{ALRM} = sub { die "alarm\n" }; | 422 | local $SIG{ALRM} = sub { die "alarm\n" }; |
377 | alarm(2); | ||
378 | $result = NPTest->testCmd( $cmd ); | 423 | $result = NPTest->testCmd( $cmd ); |
379 | alarm(0); }; | 424 | }; |
380 | isnt( $@, "alarm\n", $cmd ); | 425 | isnt( $@, "alarm\n", $cmd ); |
426 | alarm(0); | ||
381 | is( $result->return_code, 0, $cmd ); | 427 | is( $result->return_code, 0, $cmd ); |
382 | 428 | ||
383 | # Let's hope there won't be any web server on :80 returning "redirected"! | 429 | # Let's hope there won't be any web server on :80 returning "redirected"! |
384 | $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected"; | 430 | $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected"; |
431 | alarm(2); | ||
385 | eval { | 432 | eval { |
386 | local $SIG{ALRM} = sub { die "alarm\n" }; | 433 | local $SIG{ALRM} = sub { die "alarm\n" }; |
387 | alarm(2); | ||
388 | $result = NPTest->testCmd( $cmd ); | 434 | $result = NPTest->testCmd( $cmd ); |
389 | alarm(0); }; | 435 | }; |
390 | isnt( $@, "alarm\n", $cmd ); | 436 | isnt( $@, "alarm\n", $cmd ); |
437 | alarm(0); | ||
391 | isnt( $result->return_code, 0, $cmd ); | 438 | isnt( $result->return_code, 0, $cmd ); |
392 | 439 | ||
393 | # Test an external address - timeout | 440 | # Test an external address - timeout |
394 | SKIP: { | 441 | SKIP: { |
395 | skip "This doesn't seems to work all the time", 1 unless ($ENV{HTTP_EXTERNAL}); | 442 | skip "This doesn't seem to work all the time", 1 unless ($ENV{HTTP_EXTERNAL}); |
396 | $cmd = "$command -f follow -u /redir_external -t 5"; | 443 | $cmd = "$command -f follow -u /redir_external -t 5"; |
397 | eval { | 444 | eval { |
398 | $result = NPTest->testCmd( $cmd, 2 ); | 445 | $result = NPTest->testCmd( $cmd, 2 ); |