Skip to content

Commit b02a309

Browse files
committed
feature: implemented the env_to_nginx() Perl utility function as per Rollin Crittendon's request. for example: env_to_nginx('foo', 'bar=hello world')
1 parent 8fccf47 commit b02a309

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

lib/Test/Nginx/Socket.pm

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use IO::Socket;
2727

2828
#our ($PrevRequest, $PrevConfig);
2929

30-
our @EXPORT = qw( is_str plan run_tests run_test
30+
our @EXPORT = qw( env_to_nginx is_str plan run_tests run_test
3131
repeat_each config_preamble worker_connections
3232
master_process_enabled
3333
no_long_string workers master_on master_off
@@ -2178,6 +2178,71 @@ to run the specified number of duplicate requests for each test block. When it i
21782178
21792179
Default to 1.
21802180
2181+
=head2 env_to_nginx
2182+
2183+
Specify additional system environmnt variables to be passed into the nginx server.
2184+
2185+
For example,
2186+
2187+
env_to_nginx("foo", "bar=123", "baz=hello world");
2188+
run_tests();
2189+
2190+
will result in the following lines to be inserted into the resulting F<nginx.conf> file generated by the test scaffold:
2191+
2192+
env foo;
2193+
env bar=123;
2194+
env 'baz=hello world';
2195+
2196+
The latter two are examples of setting values directly to the environments. You can also set values directly on the Perl land, before calling this C<env_to_nginx> function, for instance,
2197+
2198+
$ENV{baz} = 'hello world';
2199+
env_to_nginx("baz");
2200+
2201+
If you just want to pass certain environments to a particular test case (or test block), you can just
2202+
use the C<--- main_config> secion directly. For example,
2203+
2204+
--- main_config
2205+
env foo;
2206+
env bar=123;
2207+
2208+
You can check out nginx's official document on its C<env> directive below:
2209+
2210+
L<http://nginx.org/r/env>
2211+
2212+
By default, only the following environments are passed:
2213+
2214+
=over
2215+
2216+
=item *
2217+
2218+
MOCKEAGAIN_VERBOSE
2219+
2220+
=item *
2221+
2222+
MOCKEAGAIN
2223+
2224+
=item *
2225+
2226+
MOCKEAGAIN_WRITE_TIMEOUT_PATTERN
2227+
2228+
=item *
2229+
2230+
LD_PRELOAD
2231+
2232+
=item *
2233+
2234+
LD_LIBRARY_PATH
2235+
2236+
=item *
2237+
2238+
DYLD_INSERT_LIBRARIES
2239+
2240+
=item *
2241+
2242+
DYLD_FORCE_FLAT_NAMESPACE
2243+
2244+
=back
2245+
21812246
=head2 workers
21822247
21832248
Call this function before C<run_tests()> to configure Nginx's C<worker_processes> directive's value. For example,

lib/Test/Nginx/Util.pm

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use Scalar::Util qw( looks_like_number );
2020
use IO::Socket::INET;
2121
use IO::Socket::UNIX;
2222
use Test::LongString;
23+
use Carp qw( croak );
2324

2425
our $ConfigVersion;
2526
our $FilterHttpConfig;
@@ -240,6 +241,19 @@ our $ForceRestartOnTest = (defined $ENV{TEST_NGINX_FORCE_RESTART_ON_TEST})
240241
our $ChildPid;
241242
our $UdpServerPid;
242243
our $TcpServerPid;
244+
our @EnvToNginx;
245+
246+
sub env_to_nginx (@) {
247+
if (!@_) {
248+
croak "env_to_nginx: no arguments specified";
249+
}
250+
for my $v (@_) {
251+
if ($v !~ /^[A-Za-z_]/ || $v =~ /\n/) {
252+
croak "env_to_nginx: bad argument value: $v\n";
253+
}
254+
push @EnvToNginx, $v;
255+
}
256+
}
243257

244258
sub sleep_time {
245259
return $TestNginxSleep;
@@ -333,6 +347,7 @@ sub master_process_enabled (@) {
333347
}
334348

335349
our @EXPORT = qw(
350+
env_to_nginx
336351
is_str
337352
check_accum_error_log
338353
is_running
@@ -811,6 +826,16 @@ env LD_PRELOAD;
811826
env LD_LIBRARY_PATH;
812827
env DYLD_INSERT_LIBRARIES;
813828
env DYLD_FORCE_FLAT_NAMESPACE;
829+
_EOC_
830+
831+
for my $v (@EnvToNginx) {
832+
if ($v =~ /\s/) {
833+
$v = "'$v'";
834+
}
835+
print $out "env $v;\n";
836+
}
837+
838+
print $out <<_EOC_;
814839
#env LUA_PATH;
815840
#env LUA_CPATH;
816841

0 commit comments

Comments
 (0)