ingress-nginx-helm/rootfs/etc/nginx/lua/test/run.lua

76 lines
2.1 KiB
Lua
Raw Normal View History

2019-07-11 22:10:35 +00:00
local busted_runner
do
-- avoid warning during test runs caused by
-- https://github.com/openresty/lua-nginx-module/blob/2524330e59f0a385a9c77d4d1b957476dce7cb33/src/ngx_http_lua_util.c#L810
local traceback = require "debug".traceback
setmetatable(_G, { __newindex = function(table, key, value) rawset(table, key, value) end })
busted_runner = require "busted.runner"
-- if there's more constants need to be whitelisted for test runs, add here.
local GLOBALS_ALLOWED_IN_TEST = {
helpers = true,
2019-07-11 22:10:35 +00:00
}
local newindex = function(table, key, value)
rawset(table, key, value)
local phase = ngx.get_phase()
if phase == "init_worker" or phase == "init" then
return
end
-- we check only timer phase because resty-cli runs everything in timer phase
if phase == "timer" and GLOBALS_ALLOWED_IN_TEST[key] then
return
end
local message = "writing a global lua variable " .. key ..
" which may lead to race conditions between concurrent requests, so prefer the use of 'local' variables " .. traceback('', 2)
-- it's important to do print here because ngx.log is mocked below
print(message)
end
setmetatable(_G, { __newindex = newindex })
end
_G.helpers = require("test.helpers")
2019-07-11 22:10:35 +00:00
local ffi = require("ffi")
2018-12-19 13:46:53 +00:00
local lua_ingress = require("lua_ingress")
-- without this we get errors such as "attempt to redefine XXX"
local old_cdef = ffi.cdef
local exists = {}
ffi.cdef = function(def)
if exists[def] then
return
end
exists[def] = true
return old_cdef(def)
end
local old_udp = ngx.socket.udp
ngx.socket.udp = function(...)
local socket = old_udp(...)
socket.send = function(...)
error("ngx.socket.udp:send please mock this to use in tests")
end
return socket
end
local old_tcp = ngx.socket.tcp
ngx.socket.tcp = function(...)
local socket = old_tcp(...)
socket.send = function(...)
error("ngx.socket.tcp:send please mock this to use in tests")
end
return socket
end
2018-08-16 18:12:33 +00:00
ngx.log = function(...) end
ngx.print = function(...) end
2018-12-19 13:46:53 +00:00
lua_ingress.init_worker()
2018-12-03 11:56:58 +00:00
2019-07-11 22:10:35 +00:00
busted_runner({ standalone = false })