t/Util.pm
package t:Util
use strict;
use warnings;
use Test::More;
use Test::TCP;
use Proc::Guard qw(proc_guard);
use LWP::UserAgent;
sub nginx_start {
my ($class, $conf_name) = @_;
$conf_name ||= "basic.nginx.conf";
my $pwd = `pwd`;
chomp $pwd;
my $nginx_bin = $ENV{'NGINX_BIN'}
or die "please specify ENV NGINX_BIN";
my $proc = proc_guard($nginx_bin,
'-g', 'daemon off;',
'-p', $pwd . '/t/ngx_base/',
'-c', $pwd . "/t/ngx_base/etc/$conf_name"
);
Test::TCP::wait_port(8000);
return $proc;
}
nginxを起動するときに、-g "daemon off;"するのと、-pと-cを指定するのがポイント。
port番号をランダムにするのはけっこう難しくて、さらに頑張るなら、Test::TCP::empty_port()で空いているポート番号を調べたのちに、設定ファイルを一時ファイルに置換し、それをもとに起動してやればよいが、ふつうにnginxを起動させられる設定ファイルにならないのがいけてない。