Refactor ngx mock and indent using 2 spaces

This commit is contained in:
Francisco Mejia 2018-08-23 14:02:42 -04:00
parent 14145b3129
commit c7b75970ca

View file

@ -2,12 +2,10 @@ local sticky = require("balancer.sticky")
local cookie = require("resty.cookie") local cookie = require("resty.cookie")
local util = require("util") local util = require("util")
function get_mocked_ngx_env() function mock_ngx(mock)
local _ngx = { local _ngx = mock
var = {},
}
setmetatable(_ngx, {__index = _G.ngx}) setmetatable(_ngx, {__index = _G.ngx})
return _ngx _G.ngx = _ngx
end end
function get_mocked_cookie_new() function get_mocked_cookie_new()
@ -19,7 +17,7 @@ function get_mocked_cookie_new()
end end
end end
_G.ngx = get_mocked_ngx_env() mock_ngx({ var = {} })
cookie.new = get_mocked_cookie_new() cookie.new = get_mocked_cookie_new()
local function get_test_backend() local function get_test_backend()
@ -41,7 +39,7 @@ describe("Sticky", function()
describe("new(backend)", function() describe("new(backend)", function()
context("when backend specifies cookie name", function() context("when backend specifies cookie name", function()
it("should return an instance containing the corresponding cookie name", function() it("returns an instance containing the corresponding cookie name", function()
local sticky_balancer_instance = sticky:new(test_backend) local sticky_balancer_instance = sticky:new(test_backend)
local test_backend_cookie_name = test_backend.sessionAffinityConfig.cookieSessionAffinity.name local test_backend_cookie_name = test_backend.sessionAffinityConfig.cookieSessionAffinity.name
assert.equal(sticky_balancer_instance.cookie_name, test_backend_cookie_name) assert.equal(sticky_balancer_instance.cookie_name, test_backend_cookie_name)
@ -49,7 +47,7 @@ describe("Sticky", function()
end) end)
context("when backend does not specify cookie name", function() context("when backend does not specify cookie name", function()
it("should return an instance with 'route' as cookie name", function() it("returns an instance with 'route' as cookie name", function()
local temp_backend = util.deepcopy(test_backend) local temp_backend = util.deepcopy(test_backend)
temp_backend.sessionAffinityConfig.cookieSessionAffinity.name = nil temp_backend.sessionAffinityConfig.cookieSessionAffinity.name = nil
local sticky_balancer_instance = sticky:new(temp_backend) local sticky_balancer_instance = sticky:new(temp_backend)
@ -59,7 +57,7 @@ describe("Sticky", function()
end) end)
context("when backend specifies hash function", function() context("when backend specifies hash function", function()
it("should return an instance with the corresponding hash implementation", function() it("returns an instance with the corresponding hash implementation", function()
local sticky_balancer_instance = sticky:new(test_backend) local sticky_balancer_instance = sticky:new(test_backend)
local test_backend_hash_fn = test_backend.sessionAffinityConfig.cookieSessionAffinity.hash local test_backend_hash_fn = test_backend.sessionAffinityConfig.cookieSessionAffinity.hash
local test_backend_hash_implementation = util[test_backend_hash_fn .. "_digest"] local test_backend_hash_implementation = util[test_backend_hash_fn .. "_digest"]
@ -68,7 +66,7 @@ describe("Sticky", function()
end) end)
context("when backend does not specify hash function", function() context("when backend does not specify hash function", function()
it("should return an instance with the default implementation (md5)", function() it("returns an instance with the default implementation (md5)", function()
local temp_backend = util.deepcopy(test_backend) local temp_backend = util.deepcopy(test_backend)
temp_backend.sessionAffinityConfig.cookieSessionAffinity.hash = nil temp_backend.sessionAffinityConfig.cookieSessionAffinity.hash = nil
local sticky_balancer_instance = sticky:new(temp_backend) local sticky_balancer_instance = sticky:new(temp_backend)
@ -92,13 +90,13 @@ describe("Sticky", function()
end) end)
context("when client doesn't have a cookie set", function() context("when client doesn't have a cookie set", function()
it("should pick an endpoint for the client", function() it("picks an endpoint for the client", function()
local sticky_balancer_instance = sticky:new(test_backend) local sticky_balancer_instance = sticky:new(test_backend)
local peer = sticky_balancer_instance:balance() local peer = sticky_balancer_instance:balance()
assert.equal(peer, test_backend_endpoint) assert.equal(peer, test_backend_endpoint)
end) end)
it("should set a cookie on the client", function() it("sets a cookie on the client", function()
local s = {} local s = {}
cookie.new = function(self) cookie.new = function(self)
local test_backend_hash_fn = test_backend.sessionAffinityConfig.cookieSessionAffinity.hash local test_backend_hash_fn = test_backend.sessionAffinityConfig.cookieSessionAffinity.hash
@ -123,7 +121,7 @@ describe("Sticky", function()
end) end)
context("when client has a cookie set", function() context("when client has a cookie set", function()
it("should not set a cookie", function() it("does not set a cookie", function()
local s = {} local s = {}
cookie.new = function(self) cookie.new = function(self)
local return_obj = { local return_obj = {
@ -138,7 +136,7 @@ describe("Sticky", function()
assert.spy(s).was_not_called() assert.spy(s).was_not_called()
end) end)
it("should return the correct endpoint for the client", function() it("returns the correct endpoint for the client", function()
local sticky_balancer_instance = sticky:new(test_backend) local sticky_balancer_instance = sticky:new(test_backend)
local peer = sticky_balancer_instance:balance() local peer = sticky_balancer_instance:balance()
assert.equal(peer, test_backend_endpoint) assert.equal(peer, test_backend_endpoint)
@ -146,4 +144,3 @@ describe("Sticky", function()
end) end)
end) end)
end) end)