Add unit test case for balancer.route_to_alternative_balancer()
This commit is contained in:
parent
ce418168f4
commit
7a15f52cf1
2 changed files with 55 additions and 0 deletions
|
@ -257,6 +257,7 @@ end
|
||||||
if _TEST then
|
if _TEST then
|
||||||
_M.get_implementation = get_implementation
|
_M.get_implementation = get_implementation
|
||||||
_M.sync_backend = sync_backend
|
_M.sync_backend = sync_backend
|
||||||
|
_M.route_to_alternative_balancer = route_to_alternative_balancer
|
||||||
end
|
end
|
||||||
|
|
||||||
return _M
|
return _M
|
||||||
|
|
|
@ -1,6 +1,17 @@
|
||||||
_G._TEST = true
|
_G._TEST = true
|
||||||
|
|
||||||
local balancer, expected_implementations, backends
|
local balancer, expected_implementations, backends
|
||||||
|
local original_ngx = ngx
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
local function reset_balancer()
|
local function reset_balancer()
|
||||||
package.loaded["balancer"] = nil
|
package.loaded["balancer"] = nil
|
||||||
|
@ -30,6 +41,12 @@ local function reset_backends()
|
||||||
{ address = "10.184.98.239", port = "8080", maxFails = 0, failTimeout = 0 },
|
{ address = "10.184.98.239", port = "8080", maxFails = 0, failTimeout = 0 },
|
||||||
},
|
},
|
||||||
sessionAffinityConfig = { name = "", cookieSessionAffinity = { name = "" } },
|
sessionAffinityConfig = { name = "", cookieSessionAffinity = { name = "" } },
|
||||||
|
trafficShapingPolicy = {
|
||||||
|
weight = 0,
|
||||||
|
header = "",
|
||||||
|
headerValue = "",
|
||||||
|
cookie = ""
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{ name = "my-dummy-app-1", ["load-balance"] = "round_robin", },
|
{ name = "my-dummy-app-1", ["load-balance"] = "round_robin", },
|
||||||
{
|
{
|
||||||
|
@ -55,6 +72,10 @@ describe("Balancer", function()
|
||||||
reset_backends()
|
reset_backends()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
after_each(function()
|
||||||
|
reset_ngx()
|
||||||
|
end)
|
||||||
|
|
||||||
describe("get_implementation()", function()
|
describe("get_implementation()", function()
|
||||||
it("returns correct implementation for given backend", function()
|
it("returns correct implementation for given backend", function()
|
||||||
for _, backend in pairs(backends) do
|
for _, backend in pairs(backends) do
|
||||||
|
@ -65,6 +86,39 @@ describe("Balancer", function()
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe("route_to_alternative_balancer()", function()
|
||||||
|
local backend, _balancer
|
||||||
|
|
||||||
|
before_each(function()
|
||||||
|
backend = backends[1]
|
||||||
|
_balancer = {
|
||||||
|
alternative_backends = {
|
||||||
|
backend.name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mock_ngx({ var = { request_uri = "/" } })
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("returns false when no trafficShapingPolicy is set", function()
|
||||||
|
balancer.sync_backend(backend)
|
||||||
|
assert.equal(false, balancer.route_to_alternative_balancer(_balancer))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("returns false when no alternative backends is set", function()
|
||||||
|
backend.trafficShapingPolicy.weight = 100
|
||||||
|
balancer.sync_backend(backend)
|
||||||
|
_balancer.alternative_backends = nil
|
||||||
|
assert.equal(false, balancer.route_to_alternative_balancer(_balancer))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("returns false when alternative backends name does not match", function()
|
||||||
|
backend.trafficShapingPolicy.weight = 100
|
||||||
|
balancer.sync_backend(backend)
|
||||||
|
_balancer.alternative_backends[1] = "nonExistingBackend"
|
||||||
|
assert.equal(false, balancer.route_to_alternative_balancer(_balancer))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
describe("sync_backend()", function()
|
describe("sync_backend()", function()
|
||||||
local backend, implementation
|
local backend, implementation
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue