changeset 29:71ea39729fa0

Tests: memcached module generic tests. This also includes relevant framework improvements, notably the ability to test presence of external daemons and to run them.
author Maxim Dounin <mdounin@mdounin.ru>
date Sat, 11 Oct 2008 10:58:43 +0400
parents 8f1519472ece
children a8e8b7a664c1
files README lib/Test/Nginx.pm memcached.t proxy-noclose.t
diffstat 4 files changed, 107 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/README
+++ b/README
@@ -9,8 +9,11 @@ nginx binary available as ../nginx/objs/
 
 Note: some tests may fail since they are for bugs not fixed in public code.
 
-Note: tests run nginx listening on localhost, and currently this includes
-following ports: 8025, 8026, 8080, 8081.
+Note: tests run nginx (and backend daemons if needed) listening on localhost.
+Currently this includes following ports: 8025, 8026, 8080, 8081.
+
+Tests for memcached required memcached itself and Cache::Memcached to be
+installed.
 
 Currently each test creates it's own temporary directory and uses it for
 logs etc.  One may instruct tests not to remove the temp directory (e.g.
--- a/lib/Test/Nginx.pm
+++ b/lib/Test/Nginx.pm
@@ -11,7 +11,7 @@ use strict;
 
 use base qw/ Exporter /;
 
-our @EXPORT = qw/ log_in log_out http /;
+our @EXPORT = qw/ log_in log_out http http_get /;
 
 ###############################################################################
 
@@ -58,7 +58,16 @@ sub has {
 	return $self;
 }
 
-sub plan {
+sub has_daemon($) {
+	my ($self, $daemon) = @_;
+
+	Test::More::plan(skip_all => "$daemon not found")
+		unless `which $daemon`;
+
+	return $self;
+}
+
+sub plan($) {
 	my ($self, $plan) = @_;
 
 	Test::More::plan(tests => $plan);
@@ -66,7 +75,7 @@ sub plan {
 	return $self;
 }
 
-sub run {
+sub run(;$) {
 	my ($self, $conf) = @_;
 
 	my $testdir = $self->{_testdir};
@@ -90,6 +99,8 @@ sub run {
 
 	sleep 1;
 
+	die "Can't start nginx" unless -e "$self->{_testdir}/nginx.pid";
+
 	$self->{_started} = 1;
 	return $self;
 }
@@ -132,15 +143,19 @@ sub write_file_expand($$) {
 	return $self->write_file($name, $content);
 }
 
-sub run_daemon($) {
-	my ($self, $code) = @_;
+sub run_daemon($;@) {
+	my ($self, $code, @args) = @_;
 
 	my $pid = fork();
 	die "Can't fork daemon: $!\n" unless defined $pid;
 
 	if ($pid == 0) {
-		$code->();
-		exit 0;
+		if (ref($code) eq 'CODE') {
+			$code->(@args);
+			exit 0;
+		} else {
+			exec($code, @args);
+		}
 	}
 
 	$self->{_daemons} = [] unless defined $self->{_daemons};
@@ -168,7 +183,16 @@ sub log_in {
 
 ###############################################################################
 
-sub http {
+sub http_get($) {
+	my ($url) = @_;
+	return http(<<EOF);
+GET $url HTTP/1.0
+Host: localhost
+
+EOF
+}
+
+sub http($) {
 	my ($request) = @_;
 	my $reply;
 	eval {
new file mode 100644
--- /dev/null
+++ b/memcached.t
@@ -0,0 +1,66 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Test for memcached backend.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+use IO::Select;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+eval { require Cache::Memcached; };
+plain(skip_all => 'Cache::Memcached not installed') if $@;
+
+my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(2)
+	->write_file_expand('nginx.conf', <<'EOF');
+
+master_process off;
+daemon         off;
+
+events {
+    worker_connections  1024;
+}
+
+http {
+    access_log    off;
+
+    server {
+        listen       localhost:8080;
+        server_name  localhost;
+
+        location / {
+            set $memcached_key $uri;
+            memcached_pass 127.0.0.1:8081;
+        }
+    }
+}
+
+EOF
+
+$t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081');
+$t->run();
+
+###############################################################################
+
+my $memd = Cache::Memcached->new(servers => [ '127.0.0.1:8081' ]);
+$memd->set('/', 'SEE-THIS');
+
+like(http_get('/'), qr/SEE-THIS/, 'memcached request');
+like(http_get('/notfound'), qr/404/, 'memcached not found');
+
+###############################################################################
--- a/proxy-noclose.t
+++ b/proxy-noclose.t
@@ -77,25 +77,15 @@ EOF
 TODO: {
 local $TODO = 'not fixed yet, patches under review';
 
-like(http_request('/'), qr/SEE-THIS/, 'request to bad backend');
-like(http_request('/multi'), qr/AND-THIS/, 'bad backend - multiple packets');
-like(http_request('/nolen'), qr/SEE-THIS/, 'bad backend - no content length');
-like(http_request('/uselen'), qr/SEE-THIS/, 'content-length actually used');
+like(http_get('/'), qr/SEE-THIS/, 'request to bad backend');
+like(http_get('/multi'), qr/AND-THIS/, 'bad backend - multiple packets');
+like(http_get('/nolen'), qr/SEE-THIS/, 'bad backend - no content length');
+like(http_get('/uselen'), qr/SEE-THIS/, 'content-length actually used');
 
 }
 
 ###############################################################################
 
-sub http_request {
-	my ($url) = @_;
-	my $r = http(<<EOF);
-GET $url HTTP/1.1
-Host: localhost
-Connection: close
-
-EOF
-}
-
 sub http_noclose_daemon {
 	my $server = IO::Socket::INET->new(
         	Proto => 'tcp',