Skip to content

Commit

Permalink
do not use a fixed port for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
guillomovitch committed Aug 1, 2012
1 parent 1837230 commit cba5adb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 54 deletions.
59 changes: 29 additions & 30 deletions t/components/client/connection.t
Expand Up @@ -6,7 +6,7 @@ use lib 't';

use English qw(-no_match_vars);
use HTTP::Request;

use List::Util qw(first);
use Test::More;
use Test::Exception;

Expand All @@ -16,19 +16,18 @@ use FusionInventory::Test::Proxy;
use FusionInventory::Test::Server;
use FusionInventory::Test::Utils;

# check than test port is available
my $port_ok = test_port(8080);
# find an available port
my $port = first { test_port($_) } 8080 .. 8090;

# check than 'localhost resolves, to an IPv4 address only
my $localhost_ok = test_localhost();

if (!$port_ok) {
plan skip_all => 'test port unavailable';
if (!$port) {
plan skip_all => 'no port available';
} else {
plan tests => 36;
}


my $ok = sub {
print "HTTP/1.0 200 OK\r\n";
print "\r\n";
Expand All @@ -47,9 +46,9 @@ my $client = FusionInventory::Agent::HTTP::Client->new(
subtest "no response" => sub {
check_response_nok(
$client,
'http://localhost:8080/public',
"http://localhost:$port/public",
$logger,
qr/Can't connect to localhost:8080/
qr/Can't connect to localhost:$port/
);
};

Expand All @@ -60,7 +59,7 @@ my ($server, $response);
$SIG{__DIE__} = sub { $server->stop(); };

$server = FusionInventory::Test::Server->new(
port => 8080,
port => $port,
user => 'test',
realm => 'test',
password => 'test',
Expand All @@ -74,7 +73,7 @@ $server->background() or BAIL_OUT("can't launch the server");
subtest "correct response" => sub {
check_response_ok(
$client,
'http://localhost:8080/public'
"http://localhost:$port/public"
);
};

Expand All @@ -87,7 +86,7 @@ lives_ok {
subtest "no response" => sub {
check_response_nok(
$client,
'http://localhost:8080/private',
"http://localhost:$port/private",
$logger,
"[http client] authentication required, no credentials available",
);
Expand All @@ -104,7 +103,7 @@ lives_ok {
subtest "correct response" => sub {
check_response_ok(
$client,
'http://localhost:8080/private'
"http://localhost:$port/private"
);
};

Expand All @@ -117,7 +116,7 @@ skip 'IPv6 localhost resolution', 12 if !$localhost_ok;
# https connection tests

$server = FusionInventory::Test::Server->new(
port => 8080,
port => $port,
user => 'test',
realm => 'test',
password => 'test',
Expand All @@ -141,7 +140,7 @@ lives_ok {
subtest "correct response" => sub {
check_response_ok(
$client,
'https://localhost:8080/public'
"https://localhost:$port/public"
);
};

Expand All @@ -155,7 +154,7 @@ lives_ok {
subtest "no response" => sub {
check_response_nok(
$client,
'https://localhost:8080/private',
"https://localhost:$port/private",
$logger,
"[http client] authentication required, no credentials available",
);
Expand All @@ -173,7 +172,7 @@ lives_ok {
subtest "correct response" => sub {
check_response_ok(
$client,
'https://localhost:8080/private',
"https://localhost:$port/private",
);
};

Expand All @@ -187,7 +186,7 @@ lives_ok {
subtest "correct response" => sub {
check_response_ok(
$client,
'https://localhost:8080/public',
"https://localhost:$port/public",
);
};

Expand All @@ -201,7 +200,7 @@ lives_ok {
subtest "no response" => sub {
check_response_nok(
$client,
'https://localhost:8080/private',
"https://localhost:$port/private",
$logger,
"[http client] authentication required, no credentials available",
);
Expand All @@ -219,7 +218,7 @@ lives_ok {
subtest "correct response" => sub {
check_response_ok(
$client,
'https://localhost:8080/private',
"https://localhost:$port/private",
);
};

Expand All @@ -232,7 +231,7 @@ skip 'IPv6 localhost resolution', 18 if !$localhost_ok;
# http connection through proxy tests

$server = FusionInventory::Test::Server->new(
port => 8080,
port => $port,
user => 'test',
realm => 'test',
password => 'test',
Expand Down Expand Up @@ -261,7 +260,7 @@ lives_ok {
subtest "correct response" => sub {
check_response_ok(
$client,
'http://localhost:8080/public',
"http://localhost:$port/public",
);
};

Expand All @@ -275,7 +274,7 @@ lives_ok {
subtest "no response" => sub {
check_response_nok(
$client,
'http://localhost:8080/private',
"http://localhost:$port/private",
$logger,
"[http client] authentication required, no credentials available",
);
Expand All @@ -293,7 +292,7 @@ lives_ok {
subtest "correct response" => sub {
check_response_ok(
$client,
'http://localhost:8080/private',
"http://localhost:$port/private",
);
};

Expand All @@ -304,7 +303,7 @@ skip 'non working test under MacOS', 12 if $OSNAME eq 'darwin';
# https connection through proxy tests

$server = FusionInventory::Test::Server->new(
port => 8080,
port => $port,
user => 'test',
realm => 'test',
password => 'test',
Expand All @@ -329,7 +328,7 @@ lives_ok {
subtest "correct response" => sub {
check_response_ok(
$client,
'https://localhost:8080/public',
"https://localhost:$port/public",
);
};

Expand All @@ -344,7 +343,7 @@ lives_ok {
subtest "no response" => sub {
check_response_nok(
$client,
'https://localhost:8080/private',
"https://localhost:$port/private",
$logger,
"[http client] authentication required, no credentials available",
);
Expand All @@ -363,7 +362,7 @@ lives_ok {
subtest "correct response" => sub {
check_response_ok(
$client,
'https://localhost:8080/private',
'https://localhost:$port/private',
);
};

Expand All @@ -378,7 +377,7 @@ lives_ok {
subtest "correct response" => sub {
check_response_ok(
$client,
'https://localhost:8080/public',
'https://localhost:$port/public',
);
};

Expand All @@ -393,7 +392,7 @@ lives_ok {
subtest "no response" => sub {
check_response_nok(
$client,
'https://localhost:8080/private',
'https://localhost:$port/private',
$logger,
"[http client] authentication required, no credentials available",
);
Expand All @@ -412,7 +411,7 @@ lives_ok {
subtest "correct response" => sub {
check_response_ok(
$client,
'https://localhost:8080/private',
"https://localhost:$port/private",
);
};

Expand Down
25 changes: 13 additions & 12 deletions t/components/client/ocs/response.t
Expand Up @@ -6,6 +6,7 @@ use lib 't';

use Compress::Zlib;
use English qw(-no_match_vars);
use List::Util qw(first);
use Test::More;
use Test::Exception;

Expand All @@ -15,11 +16,11 @@ use FusionInventory::Agent::XML::Query;
use FusionInventory::Test::Server;
use FusionInventory::Test::Utils;

# check than test port is available
my $port_ok = test_port(8080);
# find an available port
my $port = first { test_port($_) } 8080 .. 8090;

if (!$port_ok) {
plan skip_all => 'test port unavailable';
if (!$port) {
plan skip_all => 'no available port';
} else {
plan tests => 7;
}
Expand All @@ -45,7 +46,7 @@ my $client = FusionInventory::Agent::HTTP::Client::OCS->new(
my ($server, $response);

$server = FusionInventory::Test::Server->new(
port => 8080,
port => $port,
);
my $header = "HTTP/1.0 200 OK\r\n\r\n";
my $xml_content = "<REPLY><word>hello</word></REPLY>";
Expand All @@ -65,7 +66,7 @@ subtest "error response" => sub {
check_response_nok(
scalar $client->send(
message => $message,
url => 'http://localhost:8080/error',
url => "http://localhost:$port/error",
),
$logger,
"[http client] communication error: 403 NOK",
Expand All @@ -76,7 +77,7 @@ subtest "empty content" => sub {
check_response_nok(
scalar $client->send(
message => $message,
url => 'http://localhost:8080/empty',
url => "http://localhost:$port/empty",
),
$logger,
"[http client] unknown content format",
Expand All @@ -88,7 +89,7 @@ subtest "mixedhtml content" => sub {
check_response_ok(
scalar $client->send(
message => $message,
url => 'http://localhost:8080/mixedhtml',
url => "http://localhost:$port/mixedhtml",
),
);
};
Expand All @@ -98,7 +99,7 @@ subtest "uncompressed content" => sub {
check_response_nok(
scalar $client->send(
message => $message,
url => 'http://localhost:8080/uncompressed',
url => "http://localhost:$port/uncompressed",
),
$logger,
"[http client] unexpected content, starting with $html_content",
Expand All @@ -109,7 +110,7 @@ subtest "unexpected content" => sub {
check_response_nok(
scalar $client->send(
message => $message,
url => 'http://localhost:8080/unexpected',
url => "http://localhost:$port/unexpected",
),
$logger,
"[http client] unexpected content, starting with $html_content",
Expand All @@ -120,7 +121,7 @@ subtest "correct response" => sub {
check_response_ok(
scalar $client->send(
message => $message,
url => 'http://localhost:8080/correct',
url => "http://localhost:$port/correct",
),
);
};
Expand All @@ -129,7 +130,7 @@ subtest "altered response" => sub {
check_response_ok(
scalar $client->send(
message => $message,
url => 'http://localhost:8080/altered',
url => "http://localhost:$port/altered",
),
);
};
Expand Down

0 comments on commit cba5adb

Please sign in to comment.