Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Export table backends_with_external_name and function sync_backends f…
Browse files Browse the repository at this point in the history
…or unit test purposes
  • Loading branch information
freddyesteban committed May 1, 2022
1 parent 23929ef commit 4f299ea
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rootfs/etc/nginx/lua/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,17 @@ function _M.log()
balancer:after_balance()
end

function _M.get_backends_with_external_name()
return util.deepcopy(backends_with_external_name)
end

setmetatable(_M, {__index = {
get_implementation = get_implementation,
sync_backend = sync_backend,
route_to_alternative_balancer = route_to_alternative_balancer,
get_balancer = get_balancer,
get_balancer_by_upstream_name = get_balancer_by_upstream_name,
sync_backends = sync_backends,
}})

return _M
87 changes: 87 additions & 0 deletions rootfs/etc/nginx/lua/test/balancer_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -532,5 +532,92 @@ describe("Balancer", function()
assert.not_equal(balancer.get_balancer(), nil)
end)

it("sync backends nothing to remove from backends_with_external_name only example-com should exist", function()
backends = {
{
name = "example-com", service = { spec = { ["type"] = "ExternalName" } },
endpoints = {
{ address = "example.com", port = "80", maxFails = 0, failTimeout = 0 }
}
},
{
name = "access-router-production-web-80", port = "80", secure = false,
sslPassthrough = false,
endpoints = {
{ address = "10.184.7.40", port = "8080", maxFails = 0, failTimeout = 0 },
{ address = "10.184.97.100", port = "8080", maxFails = 0, failTimeout = 0 },
{ address = "10.184.98.239", port = "8080", maxFails = 0, failTimeout = 0 },
}
}
}

mock_ngx({ var = { proxy_upstream_name = "access-router-production-web-80" }, ctx = { } }, function()
ngx.shared.configuration_data:set("backends", cjson.encode(backends))
end)

balancer.sync_backends()
local backends_with_external_name = balancer.get_backends_with_external_name()
local externals_count = 0
for _ in pairs(backends_with_external_name) do externals_count = externals_count + 1 end
local expected_external_backend_count = 1
assert.equal(expected_external_backend_count, externals_count)

assert.are.same(backends[1], backends_with_external_name["example-com"])
end)

it("sync backends no external backends exist, should not interfere with non-external backends", function()
backends = {
{
name = "access-router-production-web-80", port = "80", secure = false,
sslPassthrough = false,
endpoints = {
{ address = "10.184.7.40", port = "8080", maxFails = 0, failTimeout = 0 },
{ address = "10.184.97.100", port = "8080", maxFails = 0, failTimeout = 0 },
{ address = "10.184.98.239", port = "8080", maxFails = 0, failTimeout = 0 },
}
}
}

mock_ngx({ var = { proxy_upstream_name = "access-router-production-web-80" }, ctx = { } }, function()
ngx.shared.configuration_data:set("backends", cjson.encode(backends))
end)

balancer.sync_backends()
local backends_with_external_name = balancer.get_backends_with_external_name()
local externals_count = 0
for _ in pairs(backends_with_external_name) do externals_count = externals_count + 1 end
local expected_external_backend_count = 0
assert.equal(expected_external_backend_count, externals_count)
end)

it("sync backends ensure last external backend is used backend backends_with_external_name", function()
backends = {
{
name = "example-com", service = { spec = { ["type"] = "ExternalName" } },
endpoints = {
{ address = "example.com", port = "80", maxFails = 0, failTimeout = 0 }
}
},
{
name = "example-com", service = { spec = { ["type"] = "ExternalName" } },
endpoints = {
{ address = "example2.com", port = "80", maxFails = 0, failTimeout = 0 }
}
}
}

mock_ngx({ var = { proxy_upstream_name = "access-router-production-web-80" }, ctx = { } }, function()
ngx.shared.configuration_data:set("backends", cjson.encode(backends))
end)

balancer.sync_backends()
local backends_with_external_name = balancer.get_backends_with_external_name()
local externals_count = 0
for _ in pairs(backends_with_external_name) do externals_count = externals_count + 1 end
local expected_external_backend_count = 1
assert.equal(expected_external_backend_count, externals_count)
assert.are.same(backends[2], backends_with_external_name["example-com"])
end)

end)
end)

0 comments on commit 4f299ea

Please sign in to comment.