changeset 107:1c0ec30614c6

Tests: add TEST_GLOBALS and TEST_GLOBALS_HTTP config chunks. TEST_GLOBALS replaces previously used -g switch. This allows tests to be executed on 0.6.* branch. For compatibility with old tests -g switch will be used if TEST_GLOBALS wasn't expaneded in config. TEST_GLOBALS_HTTP replaces multiple variables (access_log, root, client_body_temp_path, proxy_temp_path, fastcgi_temp_path) previously specified directly in test configs. This change reduce duplication and allows tests to be used with nginx compiled without fastcgi and/or proxy modules (as proxy_temp_path and fastcgi_temp_path are added conditionally).
author Maxim Dounin <mdounin@mdounin.ru>
date Wed, 14 Oct 2009 02:23:52 +0400
parents 7a712d3909ba
children 93a8f4202b16
files expect-100-continue.t fastcgi.t gzip.t imap.t lib/Test/Nginx.pm limit-req.t memcached.t pop3.t proxy-cache.t proxy-chunked.t proxy-noclose.t proxy-store.t proxy-xar.t proxy.t range-flv.t range.t rewrite.t smtp-greeting-delay.t smtp-xclient.t smtp.t ssi-include-big.t ssi.t
diffstat 22 files changed, 119 insertions(+), 121 deletions(-) [+]
line wrap: on
line diff
--- a/expect-100-continue.t
+++ b/expect-100-continue.t
@@ -25,6 +25,8 @@ my $t = Test::Nginx->new()->plan(2);
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -32,12 +34,7 @@ events {
 }
 
 http {
-    access_log    off;
-    root          %%TESTDIR%%;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/fastcgi.t
+++ b/fastcgi.t
@@ -24,9 +24,11 @@ select STDOUT; $| = 1;
 eval { require FCGI; };
 plan(skip_all => 'FCGI not installed') if $@;
 
-my $t = Test::Nginx->new()->plan(4)
+my $t = Test::Nginx->new()->has('fastcgi')->plan(4)
 	->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -34,11 +36,7 @@ events {
 }
 
 http {
-    access_log    off;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/gzip.t
+++ b/gzip.t
@@ -25,6 +25,8 @@ my $t = Test::Nginx->new()->has('gzip')-
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -32,12 +34,7 @@ events {
 }
 
 http {
-    access_log    off;
-    root          %%TESTDIR%%;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/imap.t
+++ b/imap.t
@@ -33,6 +33,8 @@ my $t = Test::Nginx->new()
 	->run_daemon(\&Test::Nginx::IMAP::imap_test_daemon)
 	->write_file_expand('nginx.conf', <<'EOF')->run();
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -50,11 +52,7 @@ mail {
 }
 
 http {
-    access_log    off;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/lib/Test/Nginx.pm
+++ b/lib/Test/Nginx.pm
@@ -51,7 +51,16 @@ sub DESTROY {
 	}
 }
 
-sub has {
+sub has($) {
+	my ($self, $feature) = @_;
+
+	Test::More::plan(skip_all => "$feature not compiled in")
+		unless $self->has_module($feature);
+
+	return $self;
+}
+
+sub has_module($) {
 	my ($self, $feature) = @_;
 
 	my %regex = (
@@ -60,15 +69,17 @@ sub has {
 		rewrite	=> '(?s)^(?!.*--without-http_rewrite_module)',
 		gzip	=> '(?s)^(?!.*--without-http_gzip_module)',
 		cache	=> '(?s)^(?!.*--without-http-cache)',
+		fastcgi	=> '(?s)^(?!.*--without-http_fastcgi_module)',
+		proxy	=> '(?s)^(?!.*--without-http_proxy_module)',
 	);
 
 	my $re = $regex{$feature};
 	$re = $feature if !defined $re;
 
-	Test::More::plan(skip_all => "$feature not compiled in")
-		unless `$NGINX -V 2>&1` =~ $re;
+	$self->{_configure_args} = `$NGINX -V 2>&1`
+		if !defined $self->{_configure_args};
 
-	return $self;
+	return ($self->{_configure_args} =~ $re) ? 1 : 0;
 }
 
 sub has_daemon($) {
@@ -102,9 +113,10 @@ sub run(;$) {
 	die "Unable to fork(): $!\n" unless defined $pid;
 
 	if ($pid == 0) {
-		exec($NGINX, '-c', "$testdir/nginx.conf", '-g',
-			"pid $testdir/nginx.pid; "
-			. "error_log $testdir/error.log debug;")
+		my @globals = $self->{_test_globals} ?
+			() : ('-g', "pid $testdir/nginx.pid; "
+			. "error_log $testdir/error.log debug;");
+		exec($NGINX, '-c', "$testdir/nginx.conf", @globals)
 			or die "Unable to exec(): $!\n";
 	}
 
@@ -182,6 +194,8 @@ sub write_file($$) {
 sub write_file_expand($$) {
 	my ($self, $name, $content) = @_;
 
+	$content =~ s/%%TEST_GLOBALS%%/$self->test_globals()/gmse;
+	$content =~ s/%%TEST_GLOBALS_HTTP%%/$self->test_globals_http()/gmse;
 	$content =~ s/%%TESTDIR%%/$self->{_testdir}/gms;
 
 	return $self->write_file($name, $content);
@@ -213,6 +227,41 @@ sub testdir() {
 	return $self->{_testdir};
 }
 
+sub test_globals() {
+	my ($self) = @_;
+
+	return $self->{_test_globals}
+		if defined $self->{_test_globals};
+
+	my $s = '';
+
+	$s .= "pid $self->{_testdir}/nginx.pid;\n";
+	$s .= "error_log $self->{_testdir}/error.log debug;\n";
+
+	$self->{_test_globals} = $s;
+}
+
+sub test_globals_http() {
+	my ($self) = @_;
+
+	return $self->{_test_globals_http}
+		if defined $self->{_test_globals_http};
+
+	my $s = '';
+
+	$s .= "root $self->{_testdir};\n";
+	$s .= "access_log $self->{_testdir}/access.log;\n";
+	$s .= "client_body_temp_path $self->{_testdir}/client_body_temp;\n";
+
+	$s .= "fastcgi_temp_path $self->{_testdir}/fastcgi_temp;\n" 
+		if $self->has_module('fastcgi');
+
+	$s .= "proxy_temp_path $self->{_testdir}/proxy_temp;\n"
+		if $self->has_module('proxy');
+
+	$self->{_test_globals_http} = $s;
+}
+
 ###############################################################################
 
 sub log_core {
--- a/limit-req.t
+++ b/limit-req.t
@@ -25,6 +25,8 @@ my $t = Test::Nginx->new()->plan(5);
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -32,12 +34,7 @@ events {
 }
 
 http {
-    access_log    off;
-    root          %%TESTDIR%%;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     limit_req_zone  $binary_remote_addr  zone=one:10m   rate=1r/s;
     limit_req_zone  $binary_remote_addr  zone=long:10m  rate=1r/m;
--- a/memcached.t
+++ b/memcached.t
@@ -27,6 +27,8 @@ plan(skip_all => 'Cache::Memcached not i
 my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(4)
 	->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -34,11 +36,7 @@ events {
 }
 
 http {
-    access_log    off;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/pop3.t
+++ b/pop3.t
@@ -33,6 +33,8 @@ my $t = Test::Nginx->new()
 	->run_daemon(\&Test::Nginx::POP3::pop3_test_daemon)
 	->write_file_expand('nginx.conf', <<'EOF')->run();
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -50,11 +52,7 @@ mail {
 }
 
 http {
-    access_log    off;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/proxy-cache.t
+++ b/proxy-cache.t
@@ -25,6 +25,8 @@ my $t = Test::Nginx->new()->has('gzip')-
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -32,12 +34,7 @@ events {
 }
 
 http {
-    access_log    off;
-    root          %%TESTDIR%%;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     proxy_cache_path   %%TESTDIR%%/cache  levels=1:2
                        keys_zone=NAME:10m;
--- a/proxy-chunked.t
+++ b/proxy-chunked.t
@@ -31,6 +31,8 @@ my $t = Test::Nginx->new();
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -38,12 +40,7 @@ events {
 }
 
 http {
-    access_log    off;
-    root          %%TESTDIR%%;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/proxy-noclose.t
+++ b/proxy-noclose.t
@@ -36,6 +36,8 @@ my $t = Test::Nginx->new();
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -43,12 +45,7 @@ events {
 }
 
 http {
-    access_log    off;
-    root          %%TESTDIR%%;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/proxy-store.t
+++ b/proxy-store.t
@@ -25,6 +25,8 @@ my $t = Test::Nginx->new();
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -32,12 +34,7 @@ events {
 }
 
 http {
-    access_log    off;
-    root          %%TESTDIR%%;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/proxy-xar.t
+++ b/proxy-xar.t
@@ -25,6 +25,8 @@ my $t = Test::Nginx->new()->has('rewrite
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -32,12 +34,7 @@ events {
 }
 
 http {
-    access_log    off;
-    root          %%TESTDIR%%;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/proxy.t
+++ b/proxy.t
@@ -25,6 +25,8 @@ my $t = Test::Nginx->new();
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -32,12 +34,7 @@ events {
 }
 
 http {
-    access_log    off;
-    root          %%TESTDIR%%;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/range-flv.t
+++ b/range-flv.t
@@ -25,6 +25,8 @@ my $t = Test::Nginx->new()->has('flv')->
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -32,12 +34,7 @@ events {
 }
 
 http {
-    access_log    off;
-    root          %%TESTDIR%%;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/range.t
+++ b/range.t
@@ -25,6 +25,8 @@ my $t = Test::Nginx->new()->plan(25);
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -32,12 +34,7 @@ events {
 }
 
 http {
-    access_log    off;
-    root          %%TESTDIR%%;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     charset_map B A {
         58 59; # X -> Y
--- a/rewrite.t
+++ b/rewrite.t
@@ -24,6 +24,8 @@ select STDOUT; $| = 1;
 my $t = Test::Nginx->new()->has('rewrite')->plan(5)
 	->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -31,11 +33,7 @@ events {
 }
 
 http {
-    access_log    off;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/smtp-greeting-delay.t
+++ b/smtp-greeting-delay.t
@@ -23,6 +23,8 @@ select STDOUT; $| = 1;
 my $t = Test::Nginx->new()->has('mail')->plan(2)
 	->write_file_expand('nginx.conf', <<'EOF')->run();
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
--- a/smtp-xclient.t
+++ b/smtp-xclient.t
@@ -29,6 +29,8 @@ my $t = Test::Nginx->new()->has('mail')-
 	->run_daemon(\&Test::Nginx::SMTP::smtp_test_daemon)
 	->write_file_expand('nginx.conf', <<'EOF')->run();
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -48,11 +50,7 @@ mail {
 }
 
 http {
-    access_log    off;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/smtp.t
+++ b/smtp.t
@@ -32,6 +32,8 @@ my $t = Test::Nginx->new()
 	->run_daemon(\&Test::Nginx::SMTP::smtp_test_daemon)
 	->write_file_expand('nginx.conf', <<'EOF')->run();
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -51,11 +53,7 @@ mail {
 }
 
 http {
-    access_log    off;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     server {
         listen       127.0.0.1:8080;
--- a/ssi-include-big.t
+++ b/ssi-include-big.t
@@ -25,6 +25,8 @@ my $t = Test::Nginx->new()->has('rewrite
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -32,12 +34,7 @@ events {
 }
 
 http {
-    access_log    off;
-    root          %%TESTDIR%%;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     output_buffers  2 512;
     ssi on;
--- a/ssi.t
+++ b/ssi.t
@@ -25,6 +25,8 @@ my $t = Test::Nginx->new()->has('cache')
 
 $t->write_file_expand('nginx.conf', <<'EOF');
 
+%%TEST_GLOBALS%%
+
 master_process off;
 daemon         off;
 
@@ -32,12 +34,7 @@ events {
 }
 
 http {
-    access_log    off;
-    root          %%TESTDIR%%;
-
-    client_body_temp_path  %%TESTDIR%%/client_body_temp;
-    fastcgi_temp_path      %%TESTDIR%%/fastcgi_temp;
-    proxy_temp_path        %%TESTDIR%%/proxy_temp;
+    %%TEST_GLOBALS_HTTP%%
 
     proxy_cache_path       %%TESTDIR%%/cache levels=1:2
                            keys_zone=NAME:10m;