Skip to content

Commit 7c2b58e

Browse files
spacewanderthibaultcha
authored andcommitted
change: we now avoid running init_by_lua* in signaller processes and when testing the nginx configuration.
Signed-off-by: Thibault Charbonnier <[email protected]>
1 parent e76292d commit 7c2b58e

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

src/ngx_http_lua_module.c

+4
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,10 @@ ngx_http_lua_init(ngx_conf_t *cf)
647647
#endif
648648
ngx_str_t name = ngx_string("host");
649649

650+
if (ngx_process == NGX_PROCESS_SIGNALLER || ngx_test_config) {
651+
return NGX_OK;
652+
}
653+
650654
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
651655

652656
lmcf->host_var_index = ngx_http_get_variable_index(cf, &name);

t/160-disable-init-by-lua.t

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
use Test::Nginx::Socket::Lua;
2+
3+
repeat_each(2);
4+
5+
plan tests => repeat_each() * (blocks() * 2);
6+
7+
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
8+
9+
my $html_dir = $ENV{TEST_NGINX_HTML_DIR};
10+
my $http_config = <<_EOC_;
11+
init_by_lua_block {
12+
function set_up_ngx_tmp_conf()
13+
local conf = [[
14+
events {
15+
worker_connections 64;
16+
}
17+
http {
18+
init_by_lua_block {
19+
ngx.log(ngx.ERR, "run init_by_lua")
20+
}
21+
}
22+
]]
23+
24+
assert(os.execute("mkdir -p $html_dir/logs"))
25+
26+
local conf_file = "$html_dir/nginx.conf"
27+
local f, err = io.open(conf_file, "w")
28+
if not f then
29+
ngx.log(ngx.ERR, err)
30+
return
31+
end
32+
33+
assert(f:write(conf))
34+
35+
return conf_file
36+
end
37+
38+
function get_ngx_bin_path()
39+
local ffi = require "ffi"
40+
ffi.cdef[[char **ngx_argv;]]
41+
return ffi.string(ffi.C.ngx_argv[0])
42+
end
43+
}
44+
_EOC_
45+
46+
add_block_preprocessor(sub {
47+
my $block = shift;
48+
49+
if (!defined $block->http_config) {
50+
$block->set_value("http_config", $http_config);
51+
}
52+
53+
if (!defined $block->request) {
54+
$block->set_value("request", "GET /t");
55+
}
56+
});
57+
58+
log_level("warn");
59+
no_long_string();
60+
run_tests();
61+
62+
__DATA__
63+
64+
=== TEST 1: ensure init_by_lua* is not run in signaller process
65+
--- config
66+
location = /t {
67+
content_by_lua_block {
68+
local conf_file = set_up_ngx_tmp_conf()
69+
local nginx = get_ngx_bin_path()
70+
71+
local cmd = nginx .. " -p $TEST_NGINX_HTML_DIR -c " .. conf_file .. " -s reopen"
72+
local p, err = io.popen(cmd)
73+
if not p then
74+
ngx.log(ngx.ERR, err)
75+
return
76+
end
77+
78+
local out, err = p:read("*a")
79+
if not out then
80+
ngx.log(ngx.ERR, err)
81+
82+
else
83+
ngx.log(ngx.WARN, out)
84+
end
85+
}
86+
}
87+
--- no_error_log eval
88+
qr/\[error\] .*? init_by_lua:\d+: run init_by_lua/
89+
90+
91+
92+
=== TEST 2: init_by_lua* does not run when testing Nginx configuration
93+
--- config
94+
location = /t {
95+
content_by_lua_block {
96+
local conf_file = set_up_ngx_tmp_conf()
97+
local nginx = get_ngx_bin_path()
98+
99+
local cmd = nginx .. " -p $TEST_NGINX_HTML_DIR -c " .. conf_file .. " -t"
100+
local p, err = io.popen(cmd)
101+
if not p then
102+
ngx.log(ngx.ERR, err)
103+
return
104+
end
105+
106+
local out, err = p:read("*a")
107+
if not out then
108+
ngx.log(ngx.ERR, err)
109+
110+
else
111+
ngx.log(ngx.WARN, out)
112+
end
113+
114+
local cmd = nginx .. " -p $TEST_NGINX_HTML_DIR -c " .. conf_file .. " -T"
115+
local p, err = io.popen(cmd)
116+
if not p then
117+
ngx.log(ngx.ERR, err)
118+
return
119+
end
120+
121+
local out, err = p:read("*a")
122+
if not out then
123+
ngx.log(ngx.ERR, err)
124+
125+
else
126+
ngx.log(ngx.WARN, out)
127+
end
128+
}
129+
}
130+
--- no_error_log eval
131+
qr/\[error\] .*? init_by_lua:\d+: run init_by_lua/

0 commit comments

Comments
 (0)