From 7e9d9a56298ad27e7d68f19d46c6b60ce12a2759 Mon Sep 17 00:00:00 2001 From: Christopher Odenbach Date: Fri, 29 Jun 2018 17:15:47 +0200 Subject: added some advanced tests for check_curl --- plugins/tests/check_curl.t | 80 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 19 deletions(-) (limited to 'plugins') diff --git a/plugins/tests/check_curl.t b/plugins/tests/check_curl.t index 28dacd0d..e182623c 100755 --- a/plugins/tests/check_curl.t +++ b/plugins/tests/check_curl.t @@ -20,7 +20,6 @@ use FindBin qw($Bin); $ENV{'LC_TIME'} = "C"; my $common_tests = 70; -my $virtual_port_tests = 8; my $ssl_only_tests = 8; # Check that all dependent modules are available eval "use HTTP::Daemon 6.01;"; @@ -33,11 +32,36 @@ eval { my $plugin = 'check_http'; $plugin = 'check_curl' if $0 =~ m/check_curl/mx; +# look for libcurl version to see if some advanced checks are possible (>= 7.49.0) +my $advanced_checks = 12; +my $use_advanced_checks = 0; +my $required_version = '7.49.0'; +my $virtual_host = 'www.somefunnyhost.com'; +my $virtual_port = 42; +my $curl_version = ''; +open (my $fh, '-|', "./$plugin --version") or die; +while (<$fh>) { + if (m{libcurl/([\d.]+)\s}) { + $curl_version = $1; + last; + } +} +close ($fh); +if ($curl_version) { + my ($major, $minor, $release) = split (/\./, $curl_version); + my ($req_major, $req_minor, $req_release) = split (/\./, $required_version); + my $check = ($major <=> $req_major or $minor <=> $req_minor or $release <=> $req_release); + if ($check >= 0) { + $use_advanced_checks = 1; + print "Found libcurl $major.$minor.$release. Using advanced checks\n"; + } +} + if ($@) { plan skip_all => "Missing required module for test: $@"; } else { if (-x "./$plugin") { - plan tests => $common_tests * 2 + $ssl_only_tests + $virtual_port_tests; + plan tests => $common_tests * 2 + $ssl_only_tests + $advanced_checks; } else { plan skip_all => "No $plugin compiled"; } @@ -217,36 +241,54 @@ SKIP: { } my $cmd; -# check virtual port behaviour -# -# http without virtual port -$cmd = "$command -p $port_http -u /virtual_port -r ^127.0.0.1:$port_http\$"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); - -# http with virtual port -$cmd = "$command:80 -p $port_http -u /virtual_port -r ^127.0.0.1\$"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); +# advanced checks with virtual hostname and virtual port +SKIP: { + skip "libcurl version is smaller than $required_version", 6 unless $use_advanced_checks; + + # http without virtual port + $cmd = "./$plugin -H $virtual_host -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host:$port_http\$"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); + + # http with virtual port (!= 80) + $cmd = "./$plugin -H $virtual_host:$virtual_port -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host:$virtual_port\$"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); + + # http with virtual port (80) + $cmd = "./$plugin -H $virtual_host:80 -I 127.0.0.1 -p $port_http -u /virtual_port -r ^$virtual_host\$"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); +} + +# and the same for SSL SKIP: { - skip "HTTP::Daemon::SSL not installed", 4 if ! exists $servers->{https}; + skip "libcurl version is smaller than $required_version and/or HTTP::Daemon::SSL not installed", 6 if ! exists $servers->{https} or not $use_advanced_checks; # https without virtual port - $cmd = "$command -p $port_https --ssl -u /virtual_port -r ^127.0.0.1:$port_https\$"; + $cmd = "./$plugin -H $virtual_host -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host:$port_https\$"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); + + # https with virtual port (!= 443) + $cmd = "./$plugin -H $virtual_host:$virtual_port -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host:$virtual_port\$"; $result = NPTest->testCmd( $cmd ); is( $result->return_code, 0, $cmd); like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); - # https with virtual port - $cmd = "$command:443 -p $port_https --ssl -u /virtual_port -r ^127.0.0.1\$"; + # https with virtual port (443) + $cmd = "./$plugin -H $virtual_host:443 -I 127.0.0.1 -p $port_https --ssl -u /virtual_port -r ^$virtual_host\$"; $result = NPTest->testCmd( $cmd ); is( $result->return_code, 0, $cmd); like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); } + sub run_common_tests { my ($opts) = @_; my $command = $opts->{command}; -- cgit v1.2.3-74-g34f1