Skip to content

Commit

Permalink
Merge pull request #31 from Kong/fix/initialization
Browse files Browse the repository at this point in the history
fix(client) do not fail initialization without nameservers
  • Loading branch information
kikito authored Dec 18, 2017
2 parents b6a4be1 + 98e3a4e commit 63d10fb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ History

Versioning is strictly based on [Semantic Versioning](https://semver.org/)

### x.x.x (xx-Dec-2017) Fix

- Fix: do not fail initialization without nameservers.

### 1.0.0 (14-Dec-2017) Fixes and IPv6

- Change: BREAKING all IPv6 addresses are now returned with square brackets
Expand Down
21 changes: 17 additions & 4 deletions spec/client_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ describe("DNS client", function()
-- using the `query_func` upvalue defined above
local old_new = resolver.new
resolver.new = function(...)
local r = old_new(...)
local r, err = old_new(...)
if not r then
return nil, err
end
local original_query_func = r.query
r.query = function(self, ...)
return query_func(self, original_query_func, ...)
Expand All @@ -62,12 +65,11 @@ describe("DNS client", function()

describe("initialization", function()

it("fails with no nameservers", function()
it("does not fail with no nameservers", function()
-- empty list fallsback on resolv.conf
assert.has.no.error(function() client.init( {nameservers = {} } ) end)

assert.has.error(function() client.init( {nameservers = {}, resolvConf = {} } ) end,
"Invalid configuration, no valid nameservers found")
assert.has.no.error(function() client.init( {nameservers = {}, resolvConf = {} } ) end)
end)

it("fails with order being empty", function()
Expand Down Expand Up @@ -322,6 +324,17 @@ describe("DNS client", function()
end)


it("fetching a record without nameservers errors", function()
assert(client.init({ resolvConf = {} }))

local host = "thijsschreijer.nl"
local typ = client.TYPE_A

local answers, err, try_list = client.resolve(host, { qtype = typ })
assert.is_nil(answers)
assert(err:find("failed to create a resolver: no nameservers specified"))
end)

it("fetching a TXT record", function()
assert(client.init())

Expand Down
10 changes: 7 additions & 3 deletions src/resty/dns/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,13 @@ _M.init = function(options)
end
end
end
assert(#(options.nameservers or {}) > 0, "Invalid configuration, no valid nameservers found")
for _, r in ipairs(options.nameservers) do
log(log_DEBUG, "nameserver ", type(r) == "table" and (r[1]..":"..r[2]) or r)
options.nameservers = options.nameservers or {}
if #options.nameservers == 0 then
log(log_WARN, "Invalid configuration, no valid nameservers found")
else
for _, r in ipairs(options.nameservers) do
log(log_DEBUG, "nameserver ", type(r) == "table" and (r[1]..":"..r[2]) or r)
end
end

options.retrans = options.retrans or resolv.options.attempts or 5 -- 5 is openresty default
Expand Down

0 comments on commit 63d10fb

Please sign in to comment.