Skip to content

Commit bd83c65

Browse files
committed
tests: codecache.t: moved and reinforced a test case ensuring *by_lua directives with identical Lua chunks produce distinct cache keys.
1 parent 0c755ed commit bd83c65

File tree

2 files changed

+204
-123
lines changed

2 files changed

+204
-123
lines changed

t/025-codecache.t

Lines changed: 203 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# vim:set ft= ts=4 sw=4 et fdm=marker:
22

33
use Test::Nginx::Socket::Lua;
4+
use Cwd qw(abs_path realpath);
5+
use File::Basename;
46

57
repeat_each(2);
68

7-
plan tests => repeat_each() * 184;
9+
plan tests => repeat_each() * 198;
810

911
#$ENV{LUA_PATH} = $ENV{HOME} . '/work/JSON4Lua-0.9.30/json/?.lua';
12+
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
13+
$ENV{TEST_NGINX_CERT_DIR} ||= dirname(realpath(abs_path(__FILE__)));
1014

1115
no_long_string();
1216

@@ -1675,3 +1679,201 @@ grep me: b
16751679
"]
16761680
--- no_error_log
16771681
[error]
1682+
1683+
1684+
1685+
=== TEST 41: same chunk from different directives produces different closures
1686+
--- http_config
1687+
ssl_session_fetch_by_lua_block { ngx.log(ngx.INFO, "hello") }
1688+
1689+
ssl_session_store_by_lua_block { ngx.log(ngx.INFO, "hello") }
1690+
1691+
upstream backend {
1692+
server 0.0.0.1;
1693+
balancer_by_lua_block { ngx.log(ngx.INFO, "hello") }
1694+
}
1695+
1696+
server {
1697+
server_name test.com;
1698+
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
1699+
ssl_certificate $TEST_NGINX_CERT_DIR/cert/test.crt;
1700+
ssl_certificate_key $TEST_NGINX_CERT_DIR/cert/test.key;
1701+
ssl_session_tickets off;
1702+
1703+
ssl_certificate_by_lua_block { ngx.log(ngx.INFO, "hello") }
1704+
1705+
location /lua {
1706+
set_by_lua_block $res { ngx.log(ngx.INFO, "hello") }
1707+
1708+
rewrite_by_lua_block { ngx.log(ngx.INFO, "hello") }
1709+
1710+
access_by_lua_block { ngx.log(ngx.INFO, "hello") }
1711+
1712+
content_by_lua_block { ngx.log(ngx.INFO, "hello") }
1713+
1714+
header_filter_by_lua_block { ngx.log(ngx.INFO, "hello") }
1715+
1716+
body_filter_by_lua_block { ngx.log(ngx.INFO, "hello") }
1717+
1718+
log_by_lua_block { ngx.log(ngx.INFO, "hello") }
1719+
}
1720+
}
1721+
--- config
1722+
lua_ssl_trusted_certificate $TEST_NGINX_CERT_DIR/cert/test.crt;
1723+
1724+
location = /proxy {
1725+
proxy_pass http://backend;
1726+
}
1727+
1728+
location = /t {
1729+
set $html_dir $TEST_NGINX_HTML_DIR;
1730+
1731+
content_by_lua_block {
1732+
ngx.location.capture("/proxy")
1733+
1734+
local sock = ngx.socket.tcp()
1735+
sock:settimeout(2000)
1736+
1737+
local ok, err = sock:connect("unix:" .. ngx.var.html_dir .. "/nginx.sock")
1738+
if not ok then
1739+
ngx.log(ngx.ERR, "failed to connect: ", err)
1740+
return
1741+
end
1742+
1743+
local sess, err = sock:sslhandshake(nil, "test.com", true)
1744+
if not sess then
1745+
ngx.log(ngx.ERR, "failed to do SSL handshake: ", err)
1746+
return
1747+
end
1748+
package.loaded.session = sess
1749+
sock:close()
1750+
1751+
local ok, err = sock:connect("unix:" .. ngx.var.html_dir .. "/nginx.sock")
1752+
if not ok then
1753+
ngx.log(ngx.ERR, "failed to connect: ", err)
1754+
return
1755+
end
1756+
1757+
local sess, err = sock:sslhandshake(package.loaded.session, "test.com", true)
1758+
if not sess then
1759+
ngx.log(ngx.ERR, "failed to do SSL handshake: ", err)
1760+
return
1761+
end
1762+
1763+
local req = "GET /lua HTTP/1.0\r\nHost: test.com\r\nConnection: close\r\n\r\n"
1764+
local bytes, err = sock:send(req)
1765+
if not bytes then
1766+
ngx.log(ngx.ERR, "failed to send http request: ", err)
1767+
return
1768+
end
1769+
}
1770+
}
1771+
--- request
1772+
GET /t
1773+
--- ignore_response_body
1774+
--- grep_error_log eval: qr/code cache .*/
1775+
--- grep_error_log_out eval
1776+
[
1777+
"code cache lookup (key='=content_by_lua(nginx.conf:120)nhli_56ca4388611109b6ecfdeada050c8024', ref=-1)
1778+
code cache miss (key='=content_by_lua(nginx.conf:120)nhli_56ca4388611109b6ecfdeada050c8024', ref=-1)
1779+
code cache lookup (key='balancer_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1780+
code cache miss (key='balancer_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1781+
code cache lookup (key='ssl_certificate_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1782+
code cache miss (key='ssl_certificate_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1783+
code cache lookup (key='ssl_session_store_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1784+
code cache miss (key='ssl_session_store_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1785+
code cache lookup (key='ssl_session_fetch_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1786+
code cache miss (key='ssl_session_fetch_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1787+
code cache lookup (key='ssl_certificate_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=3)
1788+
code cache hit (key='ssl_certificate_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=3)
1789+
code cache lookup (key='ssl_session_store_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=4)
1790+
code cache hit (key='ssl_session_store_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=4)
1791+
code cache lookup (key='set_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1792+
code cache miss (key='set_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1793+
code cache lookup (key='=rewrite_by_lua(nginx.conf:46)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1794+
code cache miss (key='=rewrite_by_lua(nginx.conf:46)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1795+
code cache lookup (key='=access_by_lua(nginx.conf:48)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1796+
code cache miss (key='=access_by_lua(nginx.conf:48)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1797+
code cache lookup (key='=content_by_lua(nginx.conf:50)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1798+
code cache miss (key='=content_by_lua(nginx.conf:50)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1799+
code cache lookup (key='header_filter_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1800+
code cache miss (key='header_filter_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1801+
code cache lookup (key='body_filter_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1802+
code cache miss (key='body_filter_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1803+
code cache lookup (key='=log_by_lua(nginx.conf:56)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1804+
code cache miss (key='=log_by_lua(nginx.conf:56)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1805+
",
1806+
"code cache lookup (key='=content_by_lua(nginx.conf:120)nhli_56ca4388611109b6ecfdeada050c8024', ref=-1)
1807+
code cache miss (key='=content_by_lua(nginx.conf:120)nhli_56ca4388611109b6ecfdeada050c8024', ref=-1)
1808+
code cache lookup (key='balancer_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1809+
code cache miss (key='balancer_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1810+
code cache lookup (key='ssl_certificate_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1811+
code cache miss (key='ssl_certificate_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1812+
code cache lookup (key='ssl_session_store_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1813+
code cache miss (key='ssl_session_store_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1814+
code cache lookup (key='ssl_session_fetch_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1815+
code cache miss (key='ssl_session_fetch_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1816+
code cache lookup (key='ssl_certificate_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=3)
1817+
code cache hit (key='ssl_certificate_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=3)
1818+
code cache lookup (key='ssl_session_store_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=4)
1819+
code cache hit (key='ssl_session_store_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=4)
1820+
code cache lookup (key='set_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1821+
code cache miss (key='set_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1822+
code cache lookup (key='=rewrite_by_lua(nginx.conf:46)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1823+
code cache miss (key='=rewrite_by_lua(nginx.conf:46)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1824+
code cache lookup (key='=access_by_lua(nginx.conf:48)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1825+
code cache miss (key='=access_by_lua(nginx.conf:48)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1826+
code cache lookup (key='=content_by_lua(nginx.conf:50)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1827+
code cache miss (key='=content_by_lua(nginx.conf:50)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1828+
code cache lookup (key='header_filter_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1829+
code cache miss (key='header_filter_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1830+
code cache lookup (key='body_filter_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1831+
code cache miss (key='body_filter_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1832+
code cache lookup (key='=log_by_lua(nginx.conf:56)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1833+
code cache miss (key='=log_by_lua(nginx.conf:56)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=-1)
1834+
code cache lookup (key='=content_by_lua(nginx.conf:120)nhli_56ca4388611109b6ecfdeada050c8024', ref=1)
1835+
code cache hit (key='=content_by_lua(nginx.conf:120)nhli_56ca4388611109b6ecfdeada050c8024', ref=1)
1836+
code cache lookup (key='balancer_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=2)
1837+
code cache hit (key='balancer_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=2)
1838+
code cache lookup (key='ssl_certificate_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=3)
1839+
code cache hit (key='ssl_certificate_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=3)
1840+
code cache lookup (key='ssl_session_store_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=4)
1841+
code cache hit (key='ssl_session_store_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=4)
1842+
code cache lookup (key='ssl_session_fetch_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=5)
1843+
code cache hit (key='ssl_session_fetch_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=5)
1844+
code cache lookup (key='ssl_certificate_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=3)
1845+
code cache hit (key='ssl_certificate_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=3)
1846+
code cache lookup (key='ssl_session_store_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=4)
1847+
code cache hit (key='ssl_session_store_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=4)
1848+
code cache lookup (key='set_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=6)
1849+
code cache hit (key='set_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=6)
1850+
code cache lookup (key='=rewrite_by_lua(nginx.conf:46)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=7)
1851+
code cache hit (key='=rewrite_by_lua(nginx.conf:46)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=7)
1852+
code cache lookup (key='=access_by_lua(nginx.conf:48)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=8)
1853+
code cache hit (key='=access_by_lua(nginx.conf:48)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=8)
1854+
code cache lookup (key='=content_by_lua(nginx.conf:50)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=9)
1855+
code cache hit (key='=content_by_lua(nginx.conf:50)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=9)
1856+
code cache lookup (key='header_filter_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=10)
1857+
code cache hit (key='header_filter_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=10)
1858+
code cache lookup (key='body_filter_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=11)
1859+
code cache hit (key='body_filter_by_luanhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=11)
1860+
code cache lookup (key='=log_by_lua(nginx.conf:56)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=12)
1861+
code cache hit (key='=log_by_lua(nginx.conf:56)nhli_8a9441d0a30531ba8bb34ab11c55cfc3', ref=12)
1862+
"]
1863+
--- error_log eval
1864+
[
1865+
qr/balancer_by_lua:\d+: hello/,
1866+
qr/ssl_session_fetch_by_lua_block:\d+: hello/,
1867+
qr/ssl_certificate_by_lua:\d+: hello/,
1868+
qr/ssl_session_store_by_lua_block:\d+: hello/,
1869+
qr/set_by_lua:\d+: hello/,
1870+
qr/rewrite_by_lua\(nginx\.conf:\d+\):\d+: hello/,
1871+
qr/access_by_lua\(nginx\.conf:\d+\):\d+: hello/,
1872+
qr/content_by_lua\(nginx\.conf:\d+\):\d+: hello/,
1873+
qr/header_filter_by_lua:\d+: hello/,
1874+
qr/body_filter_by_lua:\d+: hello/,
1875+
qr/log_by_lua\(nginx\.conf:\d+\):\d+: hello/,
1876+
]
1877+
--- log_level: debug
1878+
--- no_error_log
1879+
[error]

t/132-lua-blocks.t

Lines changed: 1 addition & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use Test::Nginx::Socket::Lua;
1010
repeat_each(2);
1111
#repeat_each(1);
1212

13-
plan tests => repeat_each() * (blocks() * 3 + 13);
13+
plan tests => repeat_each() * (blocks() * 3 + 3);
1414

1515
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
1616

@@ -606,124 +606,3 @@ GET /t
606606
--- response_body_like: }
607607
--- no_error_log
608608
[error]
609-
610-
611-
612-
=== TEST 24: inline Lua code cache should not mess up across different phases
613-
--- http_config
614-
# The indent is enforced to generate the same hash tags
615-
ssl_session_fetch_by_lua_block {
616-
ngx.log(ngx.INFO, "hello")
617-
}
618-
ssl_session_store_by_lua_block {
619-
ngx.log(ngx.INFO, "hello")
620-
}
621-
622-
upstream backend {
623-
server 0.0.0.1;
624-
balancer_by_lua_block {
625-
ngx.log(ngx.INFO, "hello")
626-
}
627-
}
628-
629-
server {
630-
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
631-
server_name test.com;
632-
633-
ssl_certificate_by_lua_block {
634-
ngx.log(ngx.INFO, "hello")
635-
}
636-
637-
ssl_certificate ../../cert/test.crt;
638-
ssl_certificate_key ../../cert/test.key;
639-
ssl_session_tickets off;
640-
641-
location /foo {
642-
set_by_lua_block $x {
643-
ngx.log(ngx.INFO, "hello")
644-
}
645-
rewrite_by_lua_block {
646-
ngx.log(ngx.INFO, "hello")
647-
}
648-
access_by_lua_block {
649-
ngx.log(ngx.INFO, "hello")
650-
}
651-
content_by_lua_block {
652-
ngx.log(ngx.INFO, "hello")
653-
}
654-
header_filter_by_lua_block {
655-
ngx.log(ngx.INFO, "hello")
656-
}
657-
body_filter_by_lua_block {
658-
ngx.log(ngx.INFO, "hello")
659-
}
660-
log_by_lua_block {
661-
ngx.log(ngx.INFO, "hello")
662-
}
663-
}
664-
}
665-
--- config
666-
lua_ssl_trusted_certificate ../../cert/test.crt;
667-
668-
location = /proxy {
669-
proxy_pass http://backend;
670-
}
671-
672-
location /t {
673-
content_by_lua_block {
674-
ngx.location.capture("/proxy")
675-
676-
local sock = ngx.socket.tcp()
677-
sock:settimeout(2000)
678-
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
679-
if not ok then
680-
ngx.say("failed to connect: ", err)
681-
return
682-
end
683-
684-
local sess, err = sock:sslhandshake(nil, "test.com", true)
685-
if not sess then
686-
ngx.say("failed to do SSL handshake: ", err)
687-
return
688-
end
689-
package.loaded.session = sess
690-
sock:close()
691-
692-
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
693-
if not ok then
694-
ngx.say("failed to connect: ", err)
695-
return
696-
end
697-
698-
local sess, err = sock:sslhandshake(package.loaded.session, "test.com", true)
699-
if not sess then
700-
ngx.say("failed to do SSL handshake: ", err)
701-
return
702-
end
703-
704-
local req = "GET /foo HTTP/1.0\r\nHost: test.com\r\nConnection: close\r\n\r\n"
705-
local bytes, err = sock:send(req)
706-
if not bytes then
707-
ngx.say("failed to send http request: ", err)
708-
return
709-
end
710-
}
711-
}
712-
--- request
713-
GET /t
714-
--- no_error_log
715-
[error]
716-
--- error_log eval
717-
[
718-
qr/balancer_by_lua:\d+: hello/,
719-
qr/ssl_session_fetch_by_lua_block:\d+: hello/,
720-
qr/ssl_certificate_by_lua:\d+: hello/,
721-
qr/ssl_session_store_by_lua_block:\d+: hello/,
722-
qr/set_by_lua:\d+: hello/,
723-
qr/rewrite_by_lua\(nginx\.conf:\d+\):\d+: hello/,
724-
qr/access_by_lua\(nginx\.conf:\d+\):\d+: hello/,
725-
qr/content_by_lua\(nginx\.conf:\d+\):\d+: hello/,
726-
qr/header_filter_by_lua:\d+: hello/,
727-
qr/body_filter_by_lua:\d+: hello/,
728-
qr/log_by_lua\(nginx\.conf:\d+\):\d+: hello/,
729-
]

0 commit comments

Comments
 (0)