|
| 1 | +# vim:set ft= ts=4 sw=4 et fdm=marker: |
| 2 | +# Tests for nginx -T (conf dump) completeness with large lua_block directives |
| 3 | +# Regression test for https://github.com/openresty/lua-nginx-module/issues/2469 |
| 4 | + |
| 5 | +use Test::Nginx::Socket::Lua; |
| 6 | + |
| 7 | +repeat_each(1); |
| 8 | + |
| 9 | +plan tests => repeat_each() * (blocks() * 3); |
| 10 | + |
| 11 | +$ENV{TEST_NGINX_HTML_DIR} ||= html_dir(); |
| 12 | + |
| 13 | +my $html_dir = $ENV{TEST_NGINX_HTML_DIR}; |
| 14 | +my $http_config = <<_EOC_; |
| 15 | + init_by_lua_block { |
| 16 | + function set_up_ngx_tmp_conf(conf) |
| 17 | + assert(os.execute("mkdir -p $html_dir/logs")) |
| 18 | +
|
| 19 | + local conf_file = "$html_dir/nginx.conf" |
| 20 | + local f, err = io.open(conf_file, "w") |
| 21 | + if not f then |
| 22 | + ngx.log(ngx.ERR, err) |
| 23 | + return |
| 24 | + end |
| 25 | +
|
| 26 | + assert(f:write(conf)) |
| 27 | + f:close() |
| 28 | +
|
| 29 | + return conf_file |
| 30 | + end |
| 31 | +
|
| 32 | + function get_ngx_bin_path() |
| 33 | + local ffi = require "ffi" |
| 34 | + ffi.cdef[[char **ngx_argv;]] |
| 35 | + return ffi.string(ffi.C.ngx_argv[0]) |
| 36 | + end |
| 37 | + } |
| 38 | +_EOC_ |
| 39 | + |
| 40 | +add_block_preprocessor(sub { |
| 41 | + my $block = shift; |
| 42 | + |
| 43 | + if (!defined $block->http_config) { |
| 44 | + $block->set_value("http_config", $http_config); |
| 45 | + } |
| 46 | + |
| 47 | + if (!defined $block->request) { |
| 48 | + $block->set_value("request", "GET /t"); |
| 49 | + } |
| 50 | +}); |
| 51 | + |
| 52 | +env_to_nginx("PATH"); |
| 53 | +log_level("warn"); |
| 54 | +no_long_string(); |
| 55 | +run_tests(); |
| 56 | + |
| 57 | +__DATA__ |
| 58 | +
|
| 59 | +=== TEST 1: nginx -T shows complete content of large content_by_lua_block (issue #2469) |
| 60 | +--- config |
| 61 | + location = /t { |
| 62 | + content_by_lua_block { |
| 63 | + -- Build a large lua block that forces a second buffer refill during |
| 64 | + -- config parsing. The nginx config buffer is 4096 bytes, so we need |
| 65 | + -- content that spans past that boundary in the config file. |
| 66 | + local big_comment = string.rep("x", 4096) |
| 67 | + local conf = string.format([[ |
| 68 | + pid logs/nginx_dump_test.pid; |
| 69 | + events { |
| 70 | + worker_connections 64; |
| 71 | + } |
| 72 | + http { |
| 73 | + server { |
| 74 | + listen 18989; |
| 75 | + location = /t { |
| 76 | + content_by_lua_block { |
| 77 | + -- %s |
| 78 | + -- lua_block_conf_dump_marker_end |
| 79 | + ngx.say("ok") |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + ]], big_comment) |
| 85 | +
|
| 86 | + local conf_file = set_up_ngx_tmp_conf(conf) |
| 87 | + if not conf_file then |
| 88 | + ngx.say("FAIL: could not create conf file") |
| 89 | + return |
| 90 | + end |
| 91 | +
|
| 92 | + local nginx = get_ngx_bin_path() |
| 93 | +
|
| 94 | + -- First verify the config is valid |
| 95 | + local cmd = nginx .. " -p $TEST_NGINX_HTML_DIR -c " .. conf_file .. " -t 2>&1" |
| 96 | + local p = io.popen(cmd) |
| 97 | + local out = p:read("*a") |
| 98 | + p:close() |
| 99 | +
|
| 100 | + if not out:find("test is successful") then |
| 101 | + ngx.say("FAIL: config test failed: " .. out) |
| 102 | + return |
| 103 | + end |
| 104 | +
|
| 105 | + -- Now run nginx -T and check dump contains the full lua block |
| 106 | + cmd = nginx .. " -p $TEST_NGINX_HTML_DIR -c " .. conf_file .. " -T 2>&1" |
| 107 | + p = io.popen(cmd) |
| 108 | + out = p:read("*a") |
| 109 | + p:close() |
| 110 | +
|
| 111 | + if out:find("lua_block_conf_dump_marker_end") then |
| 112 | + ngx.say("OK") |
| 113 | + else |
| 114 | + ngx.say("FAIL: marker not found in nginx -T output (truncated dump)") |
| 115 | + end |
| 116 | + } |
| 117 | + } |
| 118 | +--- request |
| 119 | +GET /t |
| 120 | +--- response_body |
| 121 | +OK |
| 122 | +--- no_error_log |
| 123 | +[error] |
0 commit comments