From 7a15f52cf13205872056be81815f38e648a8909d Mon Sep 17 00:00:00 2001 From: s-shirayama Date: Tue, 11 Jun 2019 22:20:14 +0900 Subject: [PATCH 1/4] 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 7eb49893b8..dbdeea6dc4 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 8cfd3de046..e9ef12d104 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 From e9f4c0bb0ed8e1a177bae5d0f4f04e8b819cbe9f Mon Sep 17 00:00:00 2001 From: s-shirayama Date: Tue, 11 Jun 2019 22:21:03 +0900 Subject: [PATCH 2/4] Add unit test case for canary by weight --- rootfs/etc/nginx/lua/test/balancer_test.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/rootfs/etc/nginx/lua/test/balancer_test.lua b/rootfs/etc/nginx/lua/test/balancer_test.lua index e9ef12d104..29ec7d791c 100644 --- a/rootfs/etc/nginx/lua/test/balancer_test.lua +++ b/rootfs/etc/nginx/lua/test/balancer_test.lua @@ -117,6 +117,20 @@ describe("Balancer", function() _balancer.alternative_backends[1] = "nonExistingBackend" assert.equal(false, balancer.route_to_alternative_balancer(_balancer)) end) + + context("canary by weight", function() + it("returns true when weight is 100", function() + backend.trafficShapingPolicy.weight = 100 + balancer.sync_backend(backend) + assert.equal(true, balancer.route_to_alternative_balancer(_balancer)) + end) + + it("returns false when weight is 0", function() + backend.trafficShapingPolicy.weight = 0 + balancer.sync_backend(backend) + assert.equal(false, balancer.route_to_alternative_balancer(_balancer)) + end) + end) end) describe("sync_backend()", function() From 0ff679baa7a18367ad1e1e09d94706d40c7c275b Mon Sep 17 00:00:00 2001 From: s-shirayama Date: Tue, 11 Jun 2019 22:21:21 +0900 Subject: [PATCH 3/4] Add unit test case for canary by cookie --- rootfs/etc/nginx/lua/test/balancer_test.lua | 42 +++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/rootfs/etc/nginx/lua/test/balancer_test.lua b/rootfs/etc/nginx/lua/test/balancer_test.lua index 29ec7d791c..01233db8d6 100644 --- a/rootfs/etc/nginx/lua/test/balancer_test.lua +++ b/rootfs/etc/nginx/lua/test/balancer_test.lua @@ -131,6 +131,48 @@ describe("Balancer", function() assert.equal(false, balancer.route_to_alternative_balancer(_balancer)) end) end) + + context("canary by cookie", function() + it("returns correct result for given cookies", function() + backend.trafficShapingPolicy.cookie = "canaryCookie" + balancer.sync_backend(backend) + local test_patterns = { + { + case_title = "cookie_value is 'always'", + request_cookie_name = "canaryCookie", + request_cookie_value = "always", + expected_result = true, + }, + { + case_title = "cookie_value is 'never'", + request_cookie_name = "canaryCookie", + request_cookie_value = "never", + expected_result = false, + }, + { + case_title = "cookie_value is undefined", + request_cookie_name = "canaryCookie", + request_cookie_value = "foo", + expected_result = false, + }, + { + case_title = "cookie_name is undefined", + request_cookie_name = "foo", + request_cookie_value = "always", + expected_result = false + }, + } + for _, test_pattern in pairs(test_patterns) do + mock_ngx({ var = { + ["cookie_" .. test_pattern.request_cookie_name] = test_pattern.request_cookie_value, + request_uri = "/" + }}) + assert.message("\nTest data pattern: " .. test_pattern.case_title) + .equal(test_pattern.expected_result, balancer.route_to_alternative_balancer(_balancer)) + reset_ngx() + end + end) + end) end) describe("sync_backend()", function() From 6f0d6b38b89a61a2abc9fa8f2fcae4f9da7f2627 Mon Sep 17 00:00:00 2001 From: s-shirayama Date: Tue, 11 Jun 2019 22:21:35 +0900 Subject: [PATCH 4/4] Add unit test case for canary by header --- rootfs/etc/nginx/lua/test/balancer_test.lua | 79 +++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/rootfs/etc/nginx/lua/test/balancer_test.lua b/rootfs/etc/nginx/lua/test/balancer_test.lua index 01233db8d6..379e6fcf64 100644 --- a/rootfs/etc/nginx/lua/test/balancer_test.lua +++ b/rootfs/etc/nginx/lua/test/balancer_test.lua @@ -173,6 +173,85 @@ describe("Balancer", function() end end) end) + + context("canary by header", function() + it("returns correct result for given headers", function() + local test_patterns = { + -- with no header value setting + { + case_title = "no custom header value and header value is 'always'", + header_name = "canaryHeader", + header_value = "", + request_header_name = "canaryHeader", + request_header_value = "always", + expected_result = true, + }, + { + case_title = "no custom header value and header value is 'never'", + header_name = "canaryHeader", + header_value = "", + request_header_name = "canaryHeader", + request_header_value = "never", + expected_result = false, + }, + { + case_title = "no custom header value and header value is undefined", + header_name = "canaryHeader", + header_value = "", + request_header_name = "canaryHeader", + request_header_value = "foo", + expected_result = false, + }, + { + case_title = "no custom header value and header name is undefined", + header_name = "canaryHeader", + header_value = "", + request_header_name = "foo", + request_header_value = "always", + expected_result = false, + }, + -- with header value setting + { + case_title = "custom header value is set and header value is 'always'", + header_name = "canaryHeader", + header_value = "foo", + request_header_name = "canaryHeader", + request_header_value = "always", + expected_result = false, + }, + { + case_title = "custom header value is set and header value match custom header value", + header_name = "canaryHeader", + header_value = "foo", + request_header_name = "canaryHeader", + request_header_value = "foo", + expected_result = true, + }, + { + case_title = "custom header value is set and header name is undefined", + header_name = "canaryHeader", + header_value = "foo", + request_header_name = "bar", + request_header_value = "foo", + expected_result = false + }, + } + + for _, test_pattern in pairs(test_patterns) do + reset_balancer() + backend.trafficShapingPolicy.header = test_pattern.header_name + backend.trafficShapingPolicy.headerValue = test_pattern.header_value + balancer.sync_backend(backend) + mock_ngx({ var = { + ["http_" .. test_pattern.request_header_name] = test_pattern.request_header_value, + request_uri = "/" + }}) + assert.message("\nTest data pattern: " .. test_pattern.case_title) + .equal(test_pattern.expected_result, balancer.route_to_alternative_balancer(_balancer)) + reset_ngx() + end + end) + end) end) describe("sync_backend()", function()