Skip to content

Commit

Permalink
fix(etcd): reloaded data may be in res.body.node
Browse files Browse the repository at this point in the history
Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander committed Jan 29, 2023
1 parent 9c899b8 commit 9b77e70
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
4 changes: 1 addition & 3 deletions apisix/core/config_etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,6 @@ local function sync_data(self)
local dir_res, headers = res.body.list or {}, res.headers
log.debug("readdir key: ", self.key, " res: ",
json.delay_encode(dir_res))
if not dir_res then
return false, err
end

if self.values then
for i, val in ipairs(self.values) do
Expand Down Expand Up @@ -673,6 +670,7 @@ local function _automatic_fetch(premature, self)
end

-- for test
_M.test_sync_data = sync_data
_M.test_automatic_fetch = _automatic_fetch
function _M.inject_sync_data(f)
sync_data = f
Expand Down
97 changes: 97 additions & 0 deletions t/core/config_etcd.t
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,100 @@ GET /t
reconnected to etcd
--- response_body
passed



=== TEST 11: reloaded data may be in res.body.node (special kvs structure)
--- yaml_config
deployment:
role: traditional
role_traditional:
config_provider: etcd
admin:
admin_key: null
--- config
location /t {
content_by_lua_block {
local config_etcd = require("apisix.core.config_etcd")
local etcd_cli = {}
function etcd_cli.readdir()
return {
status = 200,
headers = {},
body = {
header = {revision = 1},
kvs = {{key = "foo", value = "bar"}},
}
}
end
config_etcd.test_sync_data({
etcd_cli = etcd_cli,
key = "fake",
single_item = true,
-- need_reload because something wrong happened before
need_reload = true,
upgrade_version = function() end,
conf_version = 1,
})
}
}
--- request
GET /t
--- log_level: debug
--- grep_error_log eval
qr/readdir key: fake res: .+/
--- grep_error_log_out eval
qr/readdir key: fake res: \{("value":"bar","key":"foo"|"key":"foo","value":"bar")\}/
--- wait: 1
--- no_error_log
[error]



=== TEST 12: reloaded data may be in res.body.node (admin_api_version is v2)
--- yaml_config
deployment:
role: traditional
role_traditional:
config_provider: etcd
admin:
admin_key: null
admin_api_version: v2
--- config
location /t {
content_by_lua_block {
local config_etcd = require("apisix.core.config_etcd")
local etcd_cli = {}
function etcd_cli.readdir()
return {
status = 200,
headers = {},
body = {
header = {revision = 1},
kvs = {
{key = "/foo"},
{key = "/foo/bar", value = {"bar"}}
},
}
}
end
config_etcd.test_sync_data({
etcd_cli = etcd_cli,
key = "fake",
-- need_reload because something wrong happened before
need_reload = true,
upgrade_version = function() end,
conf_version = 1,
})
}
}
--- request
GET /t
--- log_level: debug
--- grep_error_log eval
qr/readdir key: fake res: .+/
--- grep_error_log_out eval
qr/readdir key: fake res: \{.*"nodes":\[\{.*"value":\["bar"\].*\}\].*\}/
--- wait: 1
--- no_error_log
[error]

0 comments on commit 9b77e70

Please sign in to comment.