debian-mirror-gitlab/spec/lib/gitlab/database/load_balancing/resolver_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

85 lines
2.8 KiB
Ruby
Raw Normal View History

2021-09-04 01:27:46 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-17 16:20:25 +05:30
RSpec.describe Gitlab::Database::LoadBalancing::Resolver, :freeze_time, feature_category: :database do
2021-09-04 01:27:46 +05:30
describe '#resolve' do
let(:ip_addr) { IPAddr.new('127.0.0.2') }
context 'when nameserver is an IP' do
it 'returns an IPAddr object' do
service = described_class.new('127.0.0.2')
2023-03-17 16:20:25 +05:30
response = service.resolve
2021-09-04 01:27:46 +05:30
2023-03-17 16:20:25 +05:30
expect(response.address).to eq(ip_addr)
2021-09-04 01:27:46 +05:30
end
end
context 'when nameserver is not an IP' do
subject { described_class.new('localhost').resolve }
it 'looks the nameserver up in the hosts file' do
allow_next_instance_of(Resolv::Hosts) do |instance|
allow(instance).to receive(:getaddress).with('localhost').and_return('127.0.0.2')
end
2023-03-17 16:20:25 +05:30
expect(subject.address).to eq(ip_addr)
2021-09-04 01:27:46 +05:30
end
context 'when nameserver is not in the hosts file' do
2023-03-17 16:20:25 +05:30
let(:raw_ttl) { 10 }
2021-09-04 01:27:46 +05:30
it 'looks the nameserver up in DNS' do
2023-03-17 16:20:25 +05:30
resource = double(:resource, address: ip_addr, ttl: raw_ttl)
2021-09-04 01:27:46 +05:30
packet = double(:packet, answer: [resource])
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_return(packet)
2023-03-17 16:20:25 +05:30
expect(subject.address).to eq(ip_addr)
expect(subject.ttl).to eq(raw_ttl.seconds.from_now)
2021-09-04 01:27:46 +05:30
end
context 'when nameserver is not in DNS' 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_return(double(:packet, answer: []))
expect { subject }.to raise_exception(
described_class::UnresolvableNameserverError,
'could not resolve localhost'
)
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
end