Commit 3e33c161 authored by Jason Plum's avatar Jason Plum

Catch NoResponseError from Net::DNS in LoadBalancing::Resolver

Catch the `NoResponseError` rendered from `Net::DNS::Resolver` when it
detects no repsonse from the DNS server. We do this so that we can
`raise` our own `UnresolvableNameserverError` stating that there was
no repsonse. This allows us to differentiate between getting no answers
and getting no response from the system's DNS servers.

Related to https://gitlab.com/gitlab-org/gitlab/-/issues/271575

Changelog: fixed
parent b4bf4e7a
---
title: Catch NoResponseError from Net::DNS in LoadBalancing::Resolver
merge_request: 57187
author:
type: fixed
......@@ -43,6 +43,8 @@ module Gitlab
return if answer.empty?
answer.first.address
rescue Net::DNS::Resolver::NoResponseError
raise UnresolvableNameserverError, "no response from DNS server(s)"
end
end
end
......
......@@ -57,6 +57,23 @@ RSpec.describe Gitlab::Database::LoadBalancing::Resolver do
)
end
end
context 'when DNS does not respond' do
it 'raises an exception' do
allow_next_instance_of(Resolv::Hosts) do |instance|
allow(instance).to receive(:getaddress).with('localhost').and_raise(Resolv::ResolvError)
end
allow(Net::DNS::Resolver).to receive(:start)
.with('localhost', Net::DNS::A)
.and_raise(Net::DNS::Resolver::NoResponseError)
expect { subject }.to raise_exception(
described_class::UnresolvableNameserverError,
'no response from DNS server(s)'
)
end
end
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment