comparison lib/Test/Nginx.pm @ 15:c6c36d7a4d90

Tests: support config embedded in test itself.
author Maxim Dounin <mdounin@mdounin.ru>
date Sat, 13 Sep 2008 02:57:01 +0400
parents d4b74207a627
children c57e8bd7bfc7
comparison
equal deleted inserted replaced
14:d4b74207a627 15:c6c36d7a4d90
22 ############################################################################### 22 ###############################################################################
23 23
24 sub new { 24 sub new {
25 my $self = {}; 25 my $self = {};
26 bless $self; 26 bless $self;
27
28 $self->{_testdir} = tempdir(
29 'nginx-test-XXXXXXXXXX',
30 TMPDIR => 1,
31 CLEANUP => not $ENV{LEAVE}
32 )
33 or die "Can't create temp directory: $!\n";
34
27 return $self; 35 return $self;
28 } 36 }
29 37
30 sub DESTROY { 38 sub DESTROY {
31 my ($self) = @_; 39 my ($self) = @_;
32 $self->stop(); 40 $self->stop();
33 } 41 }
34 42
35 # Create temp directory and run nginx instance.
36
37 sub run { 43 sub run {
38 my ($self, $conf) = @_; 44 my ($self, $conf) = @_;
39 45
40 my $testdir = tempdir('nginx-test-XXXXXXXXXX', TMPDIR => 1, 46 my $testdir = $self->{_testdir};
41 CLEANUP => not $ENV{LEAVE})
42 or die "Can't create temp directory: $!\n";
43 47
44 $self->{_testdir} = $testdir; 48 if (defined $conf) {
45 49 my $c = `cat $conf`;
46 system("cat $conf | sed 's!%%TESTDIR%%!$testdir!g' " 50 $self->write_file_expand('nginx.conf', $c);
47 . "> $testdir/nginx.conf"); 51 }
48 52
49 my $pid = fork(); 53 my $pid = fork();
50 die "Unable to fork(): $!\n" unless defined $pid; 54 die "Unable to fork(): $!\n" unless defined $pid;
51 55
52 if ($pid == 0) { 56 if ($pid == 0) {
61 sleep 1; 65 sleep 1;
62 66
63 return $self; 67 return $self;
64 } 68 }
65 69
66 sub stop { 70 sub stop() {
67 my ($self) = @_; 71 my ($self) = @_;
68 72
69 # terminate nginx by SIGTERM 73 # terminate nginx by SIGTERM
70 kill 15, `cat $self->{_testdir}/nginx.pid`; 74 kill 15, `cat $self->{_testdir}/nginx.pid`;
71 wait; 75 wait;
72 76
73 return $self; 77 return $self;
74 } 78 }
75 79
76 sub write_file { 80 sub write_file($$) {
77 my ($self, $name, $content) = @_; 81 my ($self, $name, $content) = @_;
78 82
79 open F, '>' . $self->{_testdir} . '/' . $name 83 open F, '>' . $self->{_testdir} . '/' . $name
80 or die "Can't create $name: $!"; 84 or die "Can't create $name: $!";
81 print F $content; 85 print F $content;
82 close F; 86 close F;
83 87
84 return $self; 88 return $self;
89 }
90
91 sub write_file_expand($$) {
92 my ($self, $name, $content) = @_;
93
94 $content =~ s/%%TESTDIR%%/$self->{_testdir}/gms;
95
96 return $self->write_file($name, $content);
85 } 97 }
86 98
87 ############################################################################### 99 ###############################################################################
88 100
89 sub log_out { 101 sub log_out {