Maxim Dounin | d26e7c5 | 2008-09-12 20:50:35 +0400 | [diff] [blame] | 1 | package Test::Nginx; |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 2 | |
| 3 | # (C) Maxim Dounin |
| 4 | |
| 5 | # Generict module for nginx tests. |
| 6 | |
| 7 | ############################################################################### |
| 8 | |
| 9 | use warnings; |
| 10 | use strict; |
| 11 | |
| 12 | use base qw/ Exporter /; |
| 13 | |
Maxim Dounin | f79ada0 | 2008-11-01 16:19:28 +0300 | [diff] [blame] | 14 | our @EXPORT = qw/ log_in log_out http http_get http_head /; |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 15 | |
| 16 | ############################################################################### |
| 17 | |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 18 | use File::Temp qw/ tempdir /; |
| 19 | use IO::Socket; |
Maxim Dounin | 9ad211a | 2008-09-08 06:08:38 +0400 | [diff] [blame] | 20 | use Socket qw/ CRLF /; |
Maxim Dounin | e0a4a1b | 2008-10-10 20:09:14 +0400 | [diff] [blame] | 21 | use Test::More qw//; |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 22 | |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 23 | ############################################################################### |
| 24 | |
Maxim Dounin | 63257d0 | 2008-11-18 20:43:47 +0300 | [diff] [blame] | 25 | our $NGINX = defined $ENV{TEST_NGINX_BINARY} ? $ENV{TEST_NGINX_BINARY} |
| 26 | : '../nginx/objs/nginx'; |
Maxim Dounin | e0a4a1b | 2008-10-10 20:09:14 +0400 | [diff] [blame] | 27 | |
Maxim Dounin | d26e7c5 | 2008-09-12 20:50:35 +0400 | [diff] [blame] | 28 | sub new { |
| 29 | my $self = {}; |
| 30 | bless $self; |
Maxim Dounin | 46bf6af | 2008-09-13 02:57:01 +0400 | [diff] [blame] | 31 | |
| 32 | $self->{_testdir} = tempdir( |
| 33 | 'nginx-test-XXXXXXXXXX', |
| 34 | TMPDIR => 1, |
| 35 | CLEANUP => not $ENV{LEAVE} |
| 36 | ) |
| 37 | or die "Can't create temp directory: $!\n"; |
| 38 | |
Maxim Dounin | d26e7c5 | 2008-09-12 20:50:35 +0400 | [diff] [blame] | 39 | return $self; |
| 40 | } |
| 41 | |
| 42 | sub DESTROY { |
| 43 | my ($self) = @_; |
| 44 | $self->stop(); |
| 45 | } |
| 46 | |
Maxim Dounin | e0a4a1b | 2008-10-10 20:09:14 +0400 | [diff] [blame] | 47 | sub has { |
Maxim Dounin | 09263aa | 2008-10-10 20:23:39 +0400 | [diff] [blame] | 48 | my ($self, $feature) = @_; |
| 49 | |
| 50 | my %regex = ( |
| 51 | mail => '--with-mail', |
Maxim Dounin | a01dff6 | 2008-10-14 02:18:03 +0400 | [diff] [blame] | 52 | flv => '--with-http_flv_module', |
Maxim Dounin | 09263aa | 2008-10-10 20:23:39 +0400 | [diff] [blame] | 53 | rewrite => '(?s)^(?!.*--without-http_rewrite_module)', |
Maxim Dounin | 09263aa | 2008-10-10 20:23:39 +0400 | [diff] [blame] | 54 | ); |
| 55 | |
Maxim Dounin | e0a4a1b | 2008-10-10 20:09:14 +0400 | [diff] [blame] | 56 | Test::More::plan(skip_all => "$feature not compiled in") |
Maxim Dounin | 09263aa | 2008-10-10 20:23:39 +0400 | [diff] [blame] | 57 | unless `$NGINX -V 2>&1` =~ $regex{$feature}; |
| 58 | |
| 59 | return $self; |
| 60 | } |
| 61 | |
Maxim Dounin | 323cf24 | 2008-10-11 10:58:43 +0400 | [diff] [blame] | 62 | sub has_daemon($) { |
| 63 | my ($self, $daemon) = @_; |
| 64 | |
| 65 | Test::More::plan(skip_all => "$daemon not found") |
| 66 | unless `which $daemon`; |
| 67 | |
| 68 | return $self; |
| 69 | } |
| 70 | |
| 71 | sub plan($) { |
Maxim Dounin | 09263aa | 2008-10-10 20:23:39 +0400 | [diff] [blame] | 72 | my ($self, $plan) = @_; |
| 73 | |
| 74 | Test::More::plan(tests => $plan); |
| 75 | |
| 76 | return $self; |
Maxim Dounin | e0a4a1b | 2008-10-10 20:09:14 +0400 | [diff] [blame] | 77 | } |
| 78 | |
Maxim Dounin | 323cf24 | 2008-10-11 10:58:43 +0400 | [diff] [blame] | 79 | sub run(;$) { |
Maxim Dounin | d26e7c5 | 2008-09-12 20:50:35 +0400 | [diff] [blame] | 80 | my ($self, $conf) = @_; |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 81 | |
Maxim Dounin | 46bf6af | 2008-09-13 02:57:01 +0400 | [diff] [blame] | 82 | my $testdir = $self->{_testdir}; |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 83 | |
Maxim Dounin | 46bf6af | 2008-09-13 02:57:01 +0400 | [diff] [blame] | 84 | if (defined $conf) { |
| 85 | my $c = `cat $conf`; |
| 86 | $self->write_file_expand('nginx.conf', $c); |
| 87 | } |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 88 | |
| 89 | my $pid = fork(); |
| 90 | die "Unable to fork(): $!\n" unless defined $pid; |
| 91 | |
| 92 | if ($pid == 0) { |
Maxim Dounin | e0a4a1b | 2008-10-10 20:09:14 +0400 | [diff] [blame] | 93 | exec($NGINX, '-c', "$testdir/nginx.conf", '-g', |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 94 | "pid $testdir/nginx.pid; " |
Maxim Dounin | 5cd6315 | 2008-09-30 21:39:23 +0400 | [diff] [blame] | 95 | . "error_log $testdir/error.log debug;") |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 96 | or die "Unable to exec(): $!\n"; |
| 97 | } |
| 98 | |
| 99 | # wait for nginx to start |
| 100 | |
Maxim Dounin | fd1126f | 2008-10-24 14:42:21 +0400 | [diff] [blame] | 101 | for (1 .. 30) { |
| 102 | select undef, undef, undef, 0.05; |
| 103 | last if -e "$self->{_testdir}/nginx.pid"; |
| 104 | } |
Maxim Dounin | d26e7c5 | 2008-09-12 20:50:35 +0400 | [diff] [blame] | 105 | |
Maxim Dounin | 323cf24 | 2008-10-11 10:58:43 +0400 | [diff] [blame] | 106 | die "Can't start nginx" unless -e "$self->{_testdir}/nginx.pid"; |
| 107 | |
Maxim Dounin | c0ef016 | 2008-09-26 18:24:28 +0400 | [diff] [blame] | 108 | $self->{_started} = 1; |
Maxim Dounin | d26e7c5 | 2008-09-12 20:50:35 +0400 | [diff] [blame] | 109 | return $self; |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 110 | } |
| 111 | |
Maxim Dounin | 46bf6af | 2008-09-13 02:57:01 +0400 | [diff] [blame] | 112 | sub stop() { |
Maxim Dounin | d26e7c5 | 2008-09-12 20:50:35 +0400 | [diff] [blame] | 113 | my ($self) = @_; |
| 114 | |
Maxim Dounin | c0ef016 | 2008-09-26 18:24:28 +0400 | [diff] [blame] | 115 | while ($self->{_daemons} && scalar @{$self->{_daemons}}) { |
| 116 | my $p = shift @{$self->{_daemons}}; |
Maxim Dounin | 27a152e | 2009-03-28 01:17:25 +0300 | [diff] [blame^] | 117 | kill 'TERM', $p; |
Maxim Dounin | c0ef016 | 2008-09-26 18:24:28 +0400 | [diff] [blame] | 118 | wait; |
| 119 | } |
| 120 | |
| 121 | return $self unless $self->{_started}; |
| 122 | |
| 123 | kill 'TERM', `cat $self->{_testdir}/nginx.pid`; |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 124 | wait; |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 125 | |
Maxim Dounin | c0ef016 | 2008-09-26 18:24:28 +0400 | [diff] [blame] | 126 | $self->{_started} = 0; |
| 127 | |
Maxim Dounin | d26e7c5 | 2008-09-12 20:50:35 +0400 | [diff] [blame] | 128 | return $self; |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 129 | } |
| 130 | |
Maxim Dounin | 46bf6af | 2008-09-13 02:57:01 +0400 | [diff] [blame] | 131 | sub write_file($$) { |
Maxim Dounin | d26e7c5 | 2008-09-12 20:50:35 +0400 | [diff] [blame] | 132 | my ($self, $name, $content) = @_; |
Maxim Dounin | d4ea27b | 2008-09-11 19:55:04 +0400 | [diff] [blame] | 133 | |
Maxim Dounin | d26e7c5 | 2008-09-12 20:50:35 +0400 | [diff] [blame] | 134 | open F, '>' . $self->{_testdir} . '/' . $name |
Maxim Dounin | d4ea27b | 2008-09-11 19:55:04 +0400 | [diff] [blame] | 135 | or die "Can't create $name: $!"; |
| 136 | print F $content; |
| 137 | close F; |
Maxim Dounin | d26e7c5 | 2008-09-12 20:50:35 +0400 | [diff] [blame] | 138 | |
| 139 | return $self; |
Maxim Dounin | d4ea27b | 2008-09-11 19:55:04 +0400 | [diff] [blame] | 140 | } |
| 141 | |
Maxim Dounin | 46bf6af | 2008-09-13 02:57:01 +0400 | [diff] [blame] | 142 | sub write_file_expand($$) { |
| 143 | my ($self, $name, $content) = @_; |
| 144 | |
| 145 | $content =~ s/%%TESTDIR%%/$self->{_testdir}/gms; |
| 146 | |
| 147 | return $self->write_file($name, $content); |
| 148 | } |
| 149 | |
Maxim Dounin | 323cf24 | 2008-10-11 10:58:43 +0400 | [diff] [blame] | 150 | sub run_daemon($;@) { |
| 151 | my ($self, $code, @args) = @_; |
Maxim Dounin | c0ef016 | 2008-09-26 18:24:28 +0400 | [diff] [blame] | 152 | |
| 153 | my $pid = fork(); |
| 154 | die "Can't fork daemon: $!\n" unless defined $pid; |
| 155 | |
| 156 | if ($pid == 0) { |
Maxim Dounin | 323cf24 | 2008-10-11 10:58:43 +0400 | [diff] [blame] | 157 | if (ref($code) eq 'CODE') { |
| 158 | $code->(@args); |
| 159 | exit 0; |
| 160 | } else { |
| 161 | exec($code, @args); |
| 162 | } |
Maxim Dounin | c0ef016 | 2008-09-26 18:24:28 +0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | $self->{_daemons} = [] unless defined $self->{_daemons}; |
| 166 | push @{$self->{_daemons}}, $pid; |
| 167 | |
| 168 | return $self; |
| 169 | } |
| 170 | |
Maxim Dounin | 6fbc135 | 2008-11-10 19:04:34 +0300 | [diff] [blame] | 171 | sub testdir() { |
| 172 | my ($self) = @_; |
| 173 | return $self->{_testdir}; |
| 174 | } |
| 175 | |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 176 | ############################################################################### |
| 177 | |
| 178 | sub log_out { |
| 179 | my ($msg) = @_; |
| 180 | $msg =~ s/^/# >> /gm; |
| 181 | $msg .= "\n" unless $msg =~ /\n\Z/; |
| 182 | print $msg; |
| 183 | } |
| 184 | |
| 185 | sub log_in { |
| 186 | my ($msg) = @_; |
| 187 | $msg =~ s/^/# << /gm; |
Maxim Dounin | 4621cc5 | 2008-09-07 19:06:49 +0400 | [diff] [blame] | 188 | $msg =~ s/([^\x20-\x7e])/sprintf('\\x%02x', ord($1)) . (($1 eq "\n") ? "\n" : '')/gmxe; |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 189 | $msg .= "\n" unless $msg =~ /\n\Z/; |
| 190 | print $msg; |
| 191 | } |
| 192 | |
| 193 | ############################################################################### |
| 194 | |
Maxim Dounin | 323cf24 | 2008-10-11 10:58:43 +0400 | [diff] [blame] | 195 | sub http_get($) { |
| 196 | my ($url) = @_; |
| 197 | return http(<<EOF); |
| 198 | GET $url HTTP/1.0 |
| 199 | Host: localhost |
| 200 | |
| 201 | EOF |
| 202 | } |
| 203 | |
Maxim Dounin | f79ada0 | 2008-11-01 16:19:28 +0300 | [diff] [blame] | 204 | sub http_head($) { |
| 205 | my ($url) = @_; |
| 206 | return http(<<EOF); |
| 207 | HEAD $url HTTP/1.0 |
| 208 | Host: localhost |
| 209 | |
| 210 | EOF |
| 211 | } |
| 212 | |
Maxim Dounin | 323cf24 | 2008-10-11 10:58:43 +0400 | [diff] [blame] | 213 | sub http($) { |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 214 | my ($request) = @_; |
| 215 | my $reply; |
| 216 | eval { |
| 217 | local $SIG{ALRM} = sub { die "alarm\n" }; |
| 218 | alarm(2); |
| 219 | my $s = IO::Socket::INET->new( |
| 220 | Proto => 'tcp', |
Maxim Dounin | 3c47220 | 2008-11-02 15:01:41 +0300 | [diff] [blame] | 221 | PeerAddr => '127.0.0.1:8080' |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 222 | ); |
| 223 | log_out($request); |
| 224 | $s->print($request); |
| 225 | local $/; |
| 226 | $reply = $s->getline(); |
| 227 | log_in($reply); |
| 228 | alarm(0); |
| 229 | }; |
| 230 | alarm(0); |
| 231 | if ($@) { |
| 232 | log_in('(timeout)'); |
| 233 | return undef; |
| 234 | } |
| 235 | return $reply; |
| 236 | } |
| 237 | |
| 238 | ############################################################################### |
| 239 | |
Maxim Dounin | 668f89e | 2008-09-07 05:00:28 +0400 | [diff] [blame] | 240 | 1; |
| 241 | |
| 242 | ############################################################################### |