Mercurial > hg > nginx-tests
comparison lib/Test/Nginx.pm @ 14:d4b74207a627
Tests: refactor common functions.
Let it be something more structured, avoid globals.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Fri, 12 Sep 2008 20:50:35 +0400 |
parents | _common.pm@d19146b30334 |
children | c6c36d7a4d90 |
comparison
equal
deleted
inserted
replaced
13:e8edb765595d | 14:d4b74207a627 |
---|---|
1 package Test::Nginx; | |
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 | |
14 our @EXPORT = qw/ log_in log_out http /; | |
15 | |
16 ############################################################################### | |
17 | |
18 use File::Temp qw/ tempdir /; | |
19 use IO::Socket; | |
20 use Socket qw/ CRLF /; | |
21 | |
22 ############################################################################### | |
23 | |
24 sub new { | |
25 my $self = {}; | |
26 bless $self; | |
27 return $self; | |
28 } | |
29 | |
30 sub DESTROY { | |
31 my ($self) = @_; | |
32 $self->stop(); | |
33 } | |
34 | |
35 # Create temp directory and run nginx instance. | |
36 | |
37 sub run { | |
38 my ($self, $conf) = @_; | |
39 | |
40 my $testdir = tempdir('nginx-test-XXXXXXXXXX', TMPDIR => 1, | |
41 CLEANUP => not $ENV{LEAVE}) | |
42 or die "Can't create temp directory: $!\n"; | |
43 | |
44 $self->{_testdir} = $testdir; | |
45 | |
46 system("cat $conf | sed 's!%%TESTDIR%%!$testdir!g' " | |
47 . "> $testdir/nginx.conf"); | |
48 | |
49 my $pid = fork(); | |
50 die "Unable to fork(): $!\n" unless defined $pid; | |
51 | |
52 if ($pid == 0) { | |
53 exec('../nginx/objs/nginx', '-c', "$testdir/nginx.conf", '-g', | |
54 "pid $testdir/nginx.pid; " | |
55 . "error_log $testdir/nginx-error.log debug;") | |
56 or die "Unable to exec(): $!\n"; | |
57 } | |
58 | |
59 # wait for nginx to start | |
60 | |
61 sleep 1; | |
62 | |
63 return $self; | |
64 } | |
65 | |
66 sub stop { | |
67 my ($self) = @_; | |
68 | |
69 # terminate nginx by SIGTERM | |
70 kill 15, `cat $self->{_testdir}/nginx.pid`; | |
71 wait; | |
72 | |
73 return $self; | |
74 } | |
75 | |
76 sub write_file { | |
77 my ($self, $name, $content) = @_; | |
78 | |
79 open F, '>' . $self->{_testdir} . '/' . $name | |
80 or die "Can't create $name: $!"; | |
81 print F $content; | |
82 close F; | |
83 | |
84 return $self; | |
85 } | |
86 | |
87 ############################################################################### | |
88 | |
89 sub log_out { | |
90 my ($msg) = @_; | |
91 $msg =~ s/^/# >> /gm; | |
92 $msg .= "\n" unless $msg =~ /\n\Z/; | |
93 print $msg; | |
94 } | |
95 | |
96 sub log_in { | |
97 my ($msg) = @_; | |
98 $msg =~ s/^/# << /gm; | |
99 $msg =~ s/([^\x20-\x7e])/sprintf('\\x%02x', ord($1)) . (($1 eq "\n") ? "\n" : '')/gmxe; | |
100 $msg .= "\n" unless $msg =~ /\n\Z/; | |
101 print $msg; | |
102 } | |
103 | |
104 ############################################################################### | |
105 | |
106 sub http { | |
107 my ($request) = @_; | |
108 my $reply; | |
109 eval { | |
110 local $SIG{ALRM} = sub { die "alarm\n" }; | |
111 alarm(2); | |
112 my $s = IO::Socket::INET->new( | |
113 Proto => 'tcp', | |
114 PeerHost => 'localhost:8080' | |
115 ); | |
116 log_out($request); | |
117 $s->print($request); | |
118 local $/; | |
119 $reply = $s->getline(); | |
120 log_in($reply); | |
121 alarm(0); | |
122 }; | |
123 alarm(0); | |
124 if ($@) { | |
125 log_in('(timeout)'); | |
126 return undef; | |
127 } | |
128 return $reply; | |
129 } | |
130 | |
131 ############################################################################### | |
132 | |
133 1; | |
134 | |
135 ############################################################################### |