generalize cidr parsing and improve lua tests

This commit is contained in:
Elvin Efendi 2021-01-04 15:01:55 -05:00
parent 2254a91866
commit 2cff9fa41d
7 changed files with 68 additions and 70 deletions

View file

@ -1,15 +1,12 @@
function mock_ngx(mock)
local _ngx = mock
setmetatable(_ngx, {__index = _G.ngx})
_G.ngx = _ngx
end
describe("Balancer chash", function()
after_each(function()
reset_ngx()
end)
describe("balance()", function()
it("uses correct key for given backend", function()
mock_ngx({var = { request_uri = "/alma/armud"}})
local balancer_chash = require("balancer.chash")
ngx.var = { request_uri = "/alma/armud"}
local balancer_chash = require_without_cache("balancer.chash")
local resty_chash = package.loaded["resty.chash"]
resty_chash.new = function(self, nodes)

View file

@ -11,6 +11,8 @@ do
-- if there's more constants need to be whitelisted for test runs, add here.
local GLOBALS_ALLOWED_IN_TEST = {
helpers = true,
require_without_cache = true,
reset_ngx = true,
}
local newindex = function(table, key, value)
rawset(table, key, value)
@ -69,6 +71,15 @@ end
ngx.log = function(...) end
ngx.print = function(...) end
local original_ngx = ngx
_G.reset_ngx = function()
ngx = original_ngx
end
_G.require_without_cache = function(module)
package.loaded[module] = nil
return require(module)
end
lua_ingress.init_worker()

View file

@ -1,27 +1,16 @@
local original_ngx = ngx
local util
local function reset_ngx()
_G.ngx = original_ngx
end
local function mock_ngx(mock)
local _ngx = mock
setmetatable(_ngx, { __index = ngx })
_G.ngx = _ngx
end
describe("utility", function()
before_each(function()
ngx.var = { remote_addr = "192.168.1.1", [1] = "nginx/regexp/1/group/capturing" }
util = require_without_cache("util")
end)
after_each(function()
reset_ngx()
end)
describe("ngx_complex_value", function()
before_each(function()
mock_ngx({ var = { remote_addr = "192.168.1.1", [1] = "nginx/regexp/1/group/capturing" } })
util = require("util")
end)
local ngx_complex_value = function(data)
local ret, err = util.parse_complex_value(data)