From 7a15f52cf13205872056be81815f38e648a8909d Mon Sep 17 00:00:00 2001 From: s-shirayama Date: Tue, 11 Jun 2019 22:20:14 +0900 Subject: [PATCH] Add unit test case for balancer.route_to_alternative_balancer() --- rootfs/etc/nginx/lua/balancer.lua | 1 + rootfs/etc/nginx/lua/test/balancer_test.lua | 54 +++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/rootfs/etc/nginx/lua/balancer.lua b/rootfs/etc/nginx/lua/balancer.lua index 7eb49893b..dbdeea6dc 100644 --- a/rootfs/etc/nginx/lua/balancer.lua +++ b/rootfs/etc/nginx/lua/balancer.lua @@ -257,6 +257,7 @@ end if _TEST then _M.get_implementation = get_implementation _M.sync_backend = sync_backend + _M.route_to_alternative_balancer = route_to_alternative_balancer end return _M diff --git a/rootfs/etc/nginx/lua/test/balancer_test.lua b/rootfs/etc/nginx/lua/test/balancer_test.lua index 8cfd3de04..e9ef12d10 100644 --- a/rootfs/etc/nginx/lua/test/balancer_test.lua +++ b/rootfs/etc/nginx/lua/test/balancer_test.lua @@ -1,6 +1,17 @@ _G._TEST = true 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() package.loaded["balancer"] = nil @@ -30,6 +41,12 @@ local function reset_backends() { address = "10.184.98.239", port = "8080", maxFails = 0, failTimeout = 0 }, }, sessionAffinityConfig = { name = "", cookieSessionAffinity = { name = "" } }, + trafficShapingPolicy = { + weight = 0, + header = "", + headerValue = "", + cookie = "" + }, }, { name = "my-dummy-app-1", ["load-balance"] = "round_robin", }, { @@ -55,6 +72,10 @@ describe("Balancer", function() reset_backends() end) + after_each(function() + reset_ngx() + end) + describe("get_implementation()", function() it("returns correct implementation for given backend", function() for _, backend in pairs(backends) do @@ -65,6 +86,39 @@ describe("Balancer", function() 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() local backend, implementation