|
| 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