Skip to content

Commit

Permalink
feat(core) ensure required shms are declared
Browse files Browse the repository at this point in the history
Signed-off-by: Thibault Charbonnier <thibaultcha@me.com>

Applied with stronger test (more reliable in case of failure), style
changes and error message changes for readability.

From #2466
  • Loading branch information
subnetmarco authored and thibaultcha committed Apr 29, 2017
1 parent 75c5cb9 commit fab9c81
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
- Serf commands executed by a running Kong node are now logged in the Nginx
error logs with a `DEBUG` level.
[#2410](https://github.com/Mashape/kong/pull/2410)
- Logging retries and failure information.
[#2429](https://github.com/Mashape/kong/pull/2429).
- Checking that core Lua shared dictionaries are declared in the configuration.
[#2466](https://github.com/Mashape/kong/pull/2466)
- Plugins:
- :fireworks: **New Request termination plugin**. This plugin allows to
temporarily disable an API and return a pre-configured response status and
Expand Down
3 changes: 2 additions & 1 deletion kong/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ return {
},
CACHE = {
CLUSTER = "cluster"
}
},
DICTS = { "kong", "cache", "cache_locks", "process_events", "cassandra"},
}
11 changes: 11 additions & 0 deletions kong/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ end
local Kong = {}

function Kong.init()
-- let's ensure the required shared dictionaries are
-- declared via lua_shared_dict in the Nginx conf
for _, dict in ipairs(constants.DICTS) do
if not ngx.shared[dict] then
return error("missing shared dict '" .. dict .. "' in Nginx " ..
"configuration, are you using a custom template? " ..
"Make sure the 'lua_shared_dict " .. dict .. " [SIZE];' " ..
"directive is defined.")
end
end

local pl_path = require "pl.path"
local conf_loader = require "kong.conf_loader"

Expand Down
26 changes: 26 additions & 0 deletions spec/02-integration/01-cmd/02-start_stop_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,31 @@ describe("kong start/stop", function()

assert(kill.is_running(helpers.test_conf.nginx_pid))
end)
it("ensures the required shared dictionaries are defined", function()
local constants = require "kong.constants"
local pl_file = require "pl.file"
local fmt = string.format

local templ_fixture = "spec/fixtures/custom_nginx.template"
local new_templ_fixture = "spec/fixtures/custom_nginx.template.tmp"

finally(function()
pl_file.delete(new_templ_fixture)
helpers.stop_kong()
end)

for _, dict in ipairs(constants.DICTS) do
-- remove shared dictionary entry
assert(os.execute(fmt("sed '/lua_shared_dict %s .*;/d' %s > %s",
dict, templ_fixture, new_templ_fixture)))

local ok, err = helpers.start_kong({ nginx_conf = new_templ_fixture })
assert.falsy(ok)
assert.matches(
"missing shared dict '" .. dict .. "' in Nginx configuration, " ..
"are you using a custom template? Make sure the 'lua_shared_dict " ..
dict .. " [SIZE];' directive is defined.", err, nil, true)
end
end)
end)
end)

0 comments on commit fab9c81

Please sign in to comment.