# HG changeset patch # User Maxim Dounin # Date 1223708323 -14400 # Node ID 71ea39729fa0582b812028165e4ccc72f16fdfc8 # Parent 8f1519472ece74e7b32e2cefd5c05d1bfd6d2af3 Tests: memcached module generic tests. This also includes relevant framework improvements, notably the ability to test presence of external daemons and to run them. diff --git a/README b/README --- 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. diff --git a/lib/Test/Nginx.pm b/lib/Test/Nginx.pm --- 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(< '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'); + +############################################################################### diff --git a/proxy-noclose.t b/proxy-noclose.t --- 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(<new( Proto => 'tcp',