Mercurial > hg > nginx-tests
comparison lib/Test/Nginx.pm @ 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 | 4f57d57543e1 |
comparison
equal
deleted
inserted
replaced
28:8f1519472ece | 29:71ea39729fa0 |
---|---|
9 use warnings; | 9 use warnings; |
10 use strict; | 10 use strict; |
11 | 11 |
12 use base qw/ Exporter /; | 12 use base qw/ Exporter /; |
13 | 13 |
14 our @EXPORT = qw/ log_in log_out http /; | 14 our @EXPORT = qw/ log_in log_out http http_get /; |
15 | 15 |
16 ############################################################################### | 16 ############################################################################### |
17 | 17 |
18 use File::Temp qw/ tempdir /; | 18 use File::Temp qw/ tempdir /; |
19 use IO::Socket; | 19 use IO::Socket; |
56 unless `$NGINX -V 2>&1` =~ $regex{$feature}; | 56 unless `$NGINX -V 2>&1` =~ $regex{$feature}; |
57 | 57 |
58 return $self; | 58 return $self; |
59 } | 59 } |
60 | 60 |
61 sub plan { | 61 sub has_daemon($) { |
62 my ($self, $daemon) = @_; | |
63 | |
64 Test::More::plan(skip_all => "$daemon not found") | |
65 unless `which $daemon`; | |
66 | |
67 return $self; | |
68 } | |
69 | |
70 sub plan($) { | |
62 my ($self, $plan) = @_; | 71 my ($self, $plan) = @_; |
63 | 72 |
64 Test::More::plan(tests => $plan); | 73 Test::More::plan(tests => $plan); |
65 | 74 |
66 return $self; | 75 return $self; |
67 } | 76 } |
68 | 77 |
69 sub run { | 78 sub run(;$) { |
70 my ($self, $conf) = @_; | 79 my ($self, $conf) = @_; |
71 | 80 |
72 my $testdir = $self->{_testdir}; | 81 my $testdir = $self->{_testdir}; |
73 | 82 |
74 if (defined $conf) { | 83 if (defined $conf) { |
88 | 97 |
89 # wait for nginx to start | 98 # wait for nginx to start |
90 | 99 |
91 sleep 1; | 100 sleep 1; |
92 | 101 |
102 die "Can't start nginx" unless -e "$self->{_testdir}/nginx.pid"; | |
103 | |
93 $self->{_started} = 1; | 104 $self->{_started} = 1; |
94 return $self; | 105 return $self; |
95 } | 106 } |
96 | 107 |
97 sub stop() { | 108 sub stop() { |
130 $content =~ s/%%TESTDIR%%/$self->{_testdir}/gms; | 141 $content =~ s/%%TESTDIR%%/$self->{_testdir}/gms; |
131 | 142 |
132 return $self->write_file($name, $content); | 143 return $self->write_file($name, $content); |
133 } | 144 } |
134 | 145 |
135 sub run_daemon($) { | 146 sub run_daemon($;@) { |
136 my ($self, $code) = @_; | 147 my ($self, $code, @args) = @_; |
137 | 148 |
138 my $pid = fork(); | 149 my $pid = fork(); |
139 die "Can't fork daemon: $!\n" unless defined $pid; | 150 die "Can't fork daemon: $!\n" unless defined $pid; |
140 | 151 |
141 if ($pid == 0) { | 152 if ($pid == 0) { |
142 $code->(); | 153 if (ref($code) eq 'CODE') { |
143 exit 0; | 154 $code->(@args); |
155 exit 0; | |
156 } else { | |
157 exec($code, @args); | |
158 } | |
144 } | 159 } |
145 | 160 |
146 $self->{_daemons} = [] unless defined $self->{_daemons}; | 161 $self->{_daemons} = [] unless defined $self->{_daemons}; |
147 push @{$self->{_daemons}}, $pid; | 162 push @{$self->{_daemons}}, $pid; |
148 | 163 |
166 print $msg; | 181 print $msg; |
167 } | 182 } |
168 | 183 |
169 ############################################################################### | 184 ############################################################################### |
170 | 185 |
171 sub http { | 186 sub http_get($) { |
187 my ($url) = @_; | |
188 return http(<<EOF); | |
189 GET $url HTTP/1.0 | |
190 Host: localhost | |
191 | |
192 EOF | |
193 } | |
194 | |
195 sub http($) { | |
172 my ($request) = @_; | 196 my ($request) = @_; |
173 my $reply; | 197 my $reply; |
174 eval { | 198 eval { |
175 local $SIG{ALRM} = sub { die "alarm\n" }; | 199 local $SIG{ALRM} = sub { die "alarm\n" }; |
176 alarm(2); | 200 alarm(2); |