Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update Test::Nginx.

  • Loading branch information...
commit 2f9b18d1624f2737971ee065b2c5d1ac405ab115 1 parent e10e783
@PiotrSikora PiotrSikora authored
View
66 test/lib/Test/Nginx/LWP.pm
@@ -51,7 +51,7 @@ sub no_long_string () {
$NoLongString = 1;
}
-sub run_test_helper ($);
+sub run_test_helper ($$);
$RunTestHelper = \&run_test_helper;
@@ -96,8 +96,8 @@ sub chunk_it ($$$) {
}
}
-sub run_test_helper ($) {
- my ($block) = @_;
+sub run_test_helper ($$) {
+ my ($block, $dry_run) = @_;
my $request = $block->request;
@@ -162,14 +162,23 @@ sub run_test_helper ($) {
#warn "req: ", $req->as_string, "\n";
#warn "DONE!!!!!!!!!!!!!!!!!!!!";
- my $res = $UserAgent->request($req);
+ my $res = HTTP::Response->new;
+ unless ($dry_run) {
+ $res = $UserAgent->request($req);
+ }
#warn "res returned!!!";
- if (defined $block->error_code) {
- is($res->code, $block->error_code, "$name - status code ok");
+ if ($dry_run) {
+ SKIP: {
+ Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
+ }
} else {
- is($res->code, 200, "$name - status code ok");
+ if (defined $block->error_code) {
+ is($res->code, $block->error_code, "$name - status code ok");
+ } else {
+ is($res->code, 200, "$name - status code ok");
+ }
}
if (defined $block->response_headers) {
@@ -179,8 +188,14 @@ sub run_test_helper ($) {
if (!defined $expected_val) {
$expected_val = '';
}
- is $expected_val, $val,
- "$name - header $key ok";
+ if ($dry_run) {
+ SKIP: {
+ Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
+ }
+ } else {
+ is $expected_val, $val,
+ "$name - header $key ok";
+ }
}
} elsif (defined $block->response_headers_like) {
my $headers = parse_headers($block->response_headers_like);
@@ -189,8 +204,14 @@ sub run_test_helper ($) {
if (!defined $expected_val) {
$expected_val = '';
}
- like $expected_val, qr/^$val$/,
- "$name - header $key like ok";
+ if ($dry_run) {
+ SKIP: {
+ Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
+ }
+ } else {
+ like $expected_val, qr/^$val$/,
+ "$name - header $key like ok";
+ }
}
}
@@ -206,12 +227,18 @@ sub run_test_helper ($) {
$expected =~ s/\$ServerPortForClient\b/$ServerPortForClient/g;
#warn show_all_chars($content);
- if ($NoLongString) {
- is($content, $expected, "$name - response_body - response is expected");
+ if ($dry_run) {
+ SKIP: {
+ Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
+ }
} else {
- is_string($content, $expected, "$name - response_body - response is expected");
+ if ($NoLongString) {
+ is($content, $expected, "$name - response_body - response is expected");
+ } else {
+ is_string($content, $expected, "$name - response_body - response is expected");
+ }
+ #is($content, $expected, "$name - response_body - response is expected");
}
- #is($content, $expected, "$name - response_body - response is expected");
} elsif (defined $block->response_body_like) {
my $content = $res->content;
@@ -223,7 +250,14 @@ sub run_test_helper ($) {
$expected_pat =~ s/\$ServerPort\b/$ServerPort/g;
$expected_pat =~ s/\$ServerPortForClient\b/$ServerPortForClient/g;
my $summary = trim($content);
- like($content, qr/$expected_pat/s, "$name - response_body_like - response is expected ($summary)");
+
+ if ($dry_run) {
+ SKIP: {
+ Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
+ }
+ } else {
+ like($content, qr/$expected_pat/s, "$name - response_body_like - response is expected ($summary)");
+ }
}
}
View
91 test/lib/Test/Nginx/Socket.pm
@@ -15,7 +15,7 @@ use List::MoreUtils qw( any );
use IO::Select ();
our $ServerAddr = 'localhost';
-our $Timeout = 2;
+our $Timeout = $ENV{TEST_NGINX_TIMEOUT} || 2;
use Test::Nginx::Util qw(
setup_server_root
@@ -44,6 +44,7 @@ use Test::Nginx::Util qw(
no_root_location
server_root
html_dir
+ server_port
);
#use Smart::Comments::JSON '###';
@@ -60,12 +61,13 @@ our @EXPORT = qw( plan run_tests run_test
master_process_enabled
no_long_string workers master_on
log_level no_shuffle no_root_location
- server_addr server_root html_dir
+ server_addr server_root html_dir server_port
+ timeout
);
sub send_request ($$$$);
-sub run_test_helper ($);
+sub run_test_helper ($$);
sub error_event_handler ($);
sub read_event_handler ($);
@@ -84,6 +86,14 @@ sub server_addr (@) {
}
}
+sub timeout (@) {
+ if (@_) {
+ $Timeout = shift;
+ } else {
+ $Timeout;
+ }
+}
+
$RunTestHelper = \&run_test_helper;
sub parse_request ($$) {
@@ -116,8 +126,8 @@ sub parse_request ($$) {
};
}
-sub run_test_helper ($) {
- my $block = shift;
+sub run_test_helper ($$) {
+ my ($block, $dry_run) = @_;
my $name = $block->name;
@@ -215,8 +225,14 @@ $parsed_req->{content}";
$timeout = $Timeout;
}
- my $raw_resp = send_request($req, $block->raw_request_middle_delay,
- $timeout, $block->name);
+ my $raw_resp;
+
+ if ($dry_run) {
+ $raw_resp = "200 OK HTTP/1.0\r\nContent-Length: 0\r\n\r\n";
+ } else {
+ $raw_resp = send_request($req, $block->raw_request_middle_delay,
+ $timeout, $block->name);
+ }
#warn "raw resonse: [$raw_resp]\n";
@@ -275,10 +291,16 @@ $parsed_req->{content}";
$res->content($decoded);
}
- if (defined $block->error_code) {
- is($res->code || '', $block->error_code, "$name - status code ok");
+ if ($dry_run) {
+ SKIP: {
+ Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
+ }
} else {
- is($res->code || '', 200, "$name - status code ok");
+ if (defined $block->error_code) {
+ is($res->code || '', $block->error_code, "$name - status code ok");
+ } else {
+ is($res->code || '', 200, "$name - status code ok");
+ }
}
if (defined $block->response_headers) {
@@ -286,7 +308,13 @@ $parsed_req->{content}";
while (my ($key, $val) = each %$headers) {
if (!defined $val) {
#warn "HIT";
- unlike $raw_headers, qr/^\s*\Q$key\E\s*:/ms, "$name - header $key not present in the raw headers";
+ if ($dry_run) {
+ SKIP: {
+ Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
+ }
+ } else {
+ unlike $raw_headers, qr/^\s*\Q$key\E\s*:/ms, "$name - header $key not present in the raw headers";
+ }
next;
}
@@ -295,8 +323,14 @@ $parsed_req->{content}";
$actual_val = '';
}
- is $actual_val, $val,
- "$name - header $key ok";
+ if ($dry_run) {
+ SKIP: {
+ Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
+ }
+ } else {
+ is $actual_val, $val,
+ "$name - header $key ok";
+ }
}
} elsif (defined $block->response_headers_like) {
my $headers = parse_headers($block->response_headers_like);
@@ -305,8 +339,14 @@ $parsed_req->{content}";
if (!defined $expected_val) {
$expected_val = '';
}
- like $expected_val, qr/^$val$/,
- "$name - header $key like ok";
+ if ($dry_run) {
+ SKIP: {
+ Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
+ }
+ } else {
+ like $expected_val, qr/^$val$/,
+ "$name - header $key like ok";
+ }
}
}
@@ -337,10 +377,16 @@ $parsed_req->{content}";
#warn show_all_chars($content);
#warn "no long string: $NoLongString";
- if ($NoLongString) {
- is($content, $expected, "$name - response_body - response is expected");
+ if ($dry_run) {
+ SKIP: {
+ Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
+ }
} else {
- is_string($content, $expected, "$name - response_body - response is expected");
+ if ($NoLongString) {
+ is($content, $expected, "$name - response_body - response is expected");
+ } else {
+ is_string($content, $expected, "$name - response_body - response is expected");
+ }
}
} elsif (defined $block->response_body_like) {
@@ -353,7 +399,14 @@ $parsed_req->{content}";
$expected_pat =~ s/\$ServerPort\b/$ServerPort/g;
$expected_pat =~ s/\$ServerPortForClient\b/$ServerPortForClient/g;
my $summary = trim($content);
- like($content, qr/$expected_pat/s, "$name - response_body_like - response is expected ($summary)");
+
+ if ($dry_run) {
+ SKIP: {
+ Test::More::skip("$name - tests skipped due to the lack of directive $dry_run", 1);
+ }
+ } else {
+ like($content, qr/$expected_pat/s, "$name - response_body_like - response is expected ($summary)");
+ }
}
}
View
84 test/lib/Test/Nginx/Util.pm
@@ -52,6 +52,14 @@ our $ServerPortForClient = $ENV{TEST_NGINX_CLIENT_PORT} || $ENV{TEST_NGINX_PO
our $NoRootLocation = 0;
our $TestNginxSleep = $ENV{TEST_NGINX_SLEEP} || 0;
+sub server_port (@) {
+ if (@_) {
+ $ServerPort = shift;
+ } else {
+ $ServerPort;
+ }
+}
+
sub repeat_each (@) {
if (@_) {
$RepeatEach = shift;
@@ -130,6 +138,7 @@ our @EXPORT_OK = qw(
no_root_location
html_dir
server_root
+ server_port
);
@@ -169,6 +178,10 @@ sub server_root () {
return $ServRoot;
}
+sub bail_out ($) {
+ Test::More::BAIL_OUT(@_);
+}
+
sub run_tests () {
$NginxVersion = get_nginx_version();
@@ -270,6 +283,8 @@ sub write_user_files ($) {
sub write_config_file ($$$) {
my ($config, $http_config, $main_config) = @_;
+ $http_config = expand_env_in_config($http_config);
+
if (!defined $config) {
$config = '';
}
@@ -359,7 +374,7 @@ sub get_nginx_version () {
sub get_pid_from_pidfile ($) {
my ($name) = @_;
open my $in, $PidFile or
- Test::More::BAIL_OUT("$name - Failed to open the pid file $PidFile for reading: $!");
+ bail_out("$name - Failed to open the pid file $PidFile for reading: $!");
my $pid = do { local $/; <$in> };
#warn "Pid: $pid\n";
close $in;
@@ -400,13 +415,53 @@ sub parse_headers ($) {
return \%headers;
}
+sub expand_env_in_config ($) {
+ my $config = shift;
+
+ if (!defined $config) {
+ return;
+ }
+
+ $config =~ s/\$(TEST_NGINX_[_A-Z]+)/
+ if (!defined $ENV{$1}) {
+ bail_out "No environment $1 defined.\n";
+ }
+ $ENV{$1}/eg;
+
+ $config;
+}
+
+sub check_if_missing_directives () {
+ open my $in, $ErrLogFile or
+ bail_out "check_if_missing_directives: Cannot open $ErrLogFile for reading: $!\n";
+
+ while (<$in>) {
+ #warn $_;
+ if (/\[emerg\] \S+?: unknown directive "([^"]+)"/) {
+ #warn "MATCHED!!! $1";
+ return $1;
+ }
+ }
+
+ close $in;
+
+ #warn "NOT MATCHED!!!";
+
+ return 0;
+}
+
sub run_test ($) {
my $block = shift;
my $name = $block->name;
my $config = $block->config;
+
+ $config = expand_env_in_config($config);
+
+ my $dry_run = 0;
+
if (!defined $config) {
- Test::More::BAIL_OUT("$name - No '--- config' section specified");
+ bail_out("$name - No '--- config' section specified");
#$config = $PrevConfig;
die;
}
@@ -431,7 +486,7 @@ sub run_test ($) {
$should_skip = 1;
}
} else {
- Test::More::BAIL_OUT("$name - Invalid --- skip_nginx spec: " .
+ bail_out("$name - Invalid --- skip_nginx spec: " .
$skip_nginx);
die;
}
@@ -460,7 +515,7 @@ sub run_test ($) {
$should_skip = 1;
}
} else {
- Test::More::BAIL_OUT("$name - Invalid --- skip_nginx2 spec: " .
+ bail_out("$name - Invalid --- skip_nginx2 spec: " .
$skip_nginx2);
die;
}
@@ -487,7 +542,7 @@ sub run_test ($) {
$should_todo = 1;
}
} else {
- Test::More::BAIL_OUT("$name - Invalid --- todo_nginx spec: " .
+ bail_out("$name - Invalid --- todo_nginx spec: " .
$todo_nginx);
die;
}
@@ -538,7 +593,7 @@ start_nginx:
write_user_files($block);
write_config_file($config, $block->http_config, $block->main_config);
if ( ! can_run($NginxBinary) ) {
- Test::More::BAIL_OUT("$name - Cannot find the nginx executable in the PATH environment");
+ bail_out("$name - Cannot find the nginx executable in the PATH environment");
die;
}
#if (system("nginx -p $ServRoot -c $ConfFile -t") != 0) {
@@ -594,7 +649,14 @@ start_nginx:
}
} else {
if (system($cmd) != 0) {
- Test::More::BAIL_OUT("$name - Cannot start nginx using command \"$cmd\".");
+ if ($ENV{TEST_NGINX_IGNORE_MISSING_DIRECTIVES} and
+ my $directive = check_if_missing_directives())
+ {
+ $dry_run = $directive;
+
+ } else {
+ bail_out("$name - Cannot start nginx using command \"$cmd\".");
+ }
}
}
@@ -605,7 +667,7 @@ start_nginx:
if ($block->init) {
eval $block->init;
if ($@) {
- Test::More::BAIL_OUT("$name - init failed: $@");
+ bail_out("$name - init failed: $@");
}
}
@@ -615,16 +677,16 @@ start_nginx:
SKIP: {
Test::More::skip("$name - $skip_reason", $tests_to_skip);
- $RunTestHelper->($block);
+ $RunTestHelper->($block, $dry_run);
}
} elsif ($should_todo) {
TODO: {
local $TODO = "$name - $todo_reason";
- $RunTestHelper->($block);
+ $RunTestHelper->($block, $dry_run);
}
} else {
- $RunTestHelper->($block);
+ $RunTestHelper->($block, $dry_run);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.