Skip to content

Commit

Permalink
fix(declarative-config): empty hash for default config
Browse files Browse the repository at this point in the history
ensure the default (empty) config returns the appropriate hash that
identifies the empty configuration: 00000000000000000000000000000000
  • Loading branch information
samugi committed Dec 12, 2022
1 parent dc1b7a6 commit 190c9a3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
7 changes: 6 additions & 1 deletion kong/db/declarative/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,11 @@ end
declarative.unique_field_key = unique_field_key


local function config_is_empty(entities)
-- empty configuration has no entries other than workspaces
return entities["workspaces"] and tablex.size(entities) == 1
end


-- entities format:
-- {
Expand Down Expand Up @@ -687,7 +692,7 @@ function declarative.load_into_cache(entities, meta, hash)

assert(type(fallback_workspace) == "string")

if not hash or hash == "" then
if not hash or hash == "" or config_is_empty(entities) then
hash = DECLARATIVE_EMPTY_CONFIG_HASH
end

Expand Down
48 changes: 47 additions & 1 deletion spec/02-integration/02-cmd/02-start_stop_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ describe("kong start/stop #" .. strategy, function()
end, 10)
end)

it("configuration hash is set correctly", function()
it("hash is set correctly for a non-empty configuration", function()
local yaml_file = helpers.make_yaml_file [[
_format_version: "1.1"
services:
Expand Down Expand Up @@ -531,6 +531,52 @@ describe("kong start/stop #" .. strategy, function()
assert.equals(32, #json_body.configuration_hash)
assert.not_equal(constants.DECLARATIVE_EMPTY_CONFIG_HASH, json_body.configuration_hash)
end)

it("hash is set correctly for an empty configuration", function()

local admin_client, json_body

finally(function()
helpers.stop_kong(helpers.test_conf.prefix)
if admin_client then
admin_client:close()
end
end)

-- not specifying declarative_config this time
assert(helpers.start_kong({
database = "off",
nginx_conf = "spec/fixtures/custom_nginx.template",
}))

helpers.wait_until(function()
helpers.wait_until(function()
local pok
pok, admin_client = pcall(helpers.admin_client)
return pok
end, 10)

local res = assert(admin_client:send {
method = "GET",
path = "/status"
})
if res.status ~= 200 then
return false
end
local body = assert.res_status(200, res)
json_body = cjson.decode(body)

if admin_client then
admin_client:close()
admin_client = nil
end

return true
end, 10)

assert.is_string(json_body.configuration_hash)
assert.equals(constants.DECLARATIVE_EMPTY_CONFIG_HASH, json_body.configuration_hash)
end)
end)
end

Expand Down
4 changes: 1 addition & 3 deletions spec/02-integration/04-admin_api/02-kong_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ describe("Admin API - Kong routes with strategy #" .. strategy, function()
assert.is_number(json.server.connections_waiting)
assert.is_number(json.server.total_requests)
if strategy == "off" then
assert.is_string(json.configuration_hash)
assert.equal(32, #json.configuration_hash)
assert.is_not_equal(empty_config_hash, json.configuration_hash)
assert.is_equal(empty_config_hash, json.configuration_hash) -- all 0 in DBLESS mode until configuration is applied
else
assert.is_nil(json.configuration_hash) -- not present in DB mode
end
Expand Down

0 comments on commit 190c9a3

Please sign in to comment.