Skip to content

Commit

Permalink
Improve unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hnrytrn committed Aug 2, 2018
1 parent 9ed2dd4 commit e385337
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions rootfs/etc/nginx/lua/test/configuration_test.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
_G._TEST = true
local cjson = require("cjson")
local configuration = require("configuration")

local unmocked_ngx = _G.ngx
local certificate_data = ngx.shared.certificate_data

function get_mocked_ngx_env()
local _ngx = {}
Expand All @@ -14,9 +18,6 @@ function get_mocked_ngx_env()
return _ngx
end

local unmocked_ngx = _G.ngx
local configuration = require("configuration")

describe("Configuration", function()
before_each(function()
_G.ngx = get_mocked_ngx_env()
Expand Down Expand Up @@ -70,6 +71,7 @@ describe("Configuration", function()
ngx.req.get_body_data = function() return mock_servers end

assert.has_no.errors(configuration.handle_servers)
assert.same(certificate_data:get("hostname"), "pemCertKey")
assert.same(ngx.status, ngx.HTTP_CREATED)
end)

Expand All @@ -82,17 +84,24 @@ describe("Configuration", function()
sslCert = {
pemCertKey = "pemCertKey"
}
},
{
hostname = "hostname2",
sslCert = {
pemCertKey = "pemCertKey2"
}
}
})
ngx.req.get_body_data = function() return mock_servers end

local s = spy.on(ngx, "log")
assert.has_no.errors(configuration.handle_servers)
assert.spy(s).was_called_with(ngx.ERR, "error setting certificate for hostname: error\n")
assert.spy(s).was_called_with(ngx.ERR,
"error setting certificate for hostname: error\nerror setting certificate for hostname2: error\n")
assert.same(ngx.status, ngx.HTTP_INTERNAL_SERVER_ERROR)
end)

it("should log an err and set status to Internal Server Error when shared dictionary is full", function()
it("should log an err, set status to Internal Server Error, and short circuit when shared dictionary is full", function()
ngx.var.request_method = "POST"
ngx.shared.certificate_data.safe_set = function(self, data) return false, "no memory" end
local mock_servers = cjson.encode({
Expand All @@ -101,13 +110,21 @@ describe("Configuration", function()
sslCert = {
pemCertKey = "pemCertKey"
}
},
{
hostname = "hostname2",
sslCert = {
pemCertKey = "pemCertKey2"
}
}
})
ngx.req.get_body_data = function() return mock_servers end

local s = spy.on(ngx, "log")
local s1 = spy.on(ngx, "log")
local s2 = spy.on(ngx.shared.certificate_data, "safe_set")
assert.has_no.errors(configuration.handle_servers)
assert.spy(s).was_called_with(ngx.ERR, "no memory in certificate_data dictionary")
assert.spy(s1).was_called_with(ngx.ERR, "no memory in certificate_data dictionary")
assert.spy(s2).was_not_called_with("hostname2", "pemCertKey2")
assert.same(ngx.status, ngx.HTTP_INTERNAL_SERVER_ERROR)
end)
end)
Expand Down

0 comments on commit e385337

Please sign in to comment.