summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/load_balancing/resolver_spec.rb
blob: 4af36693383d54867ab1e761c6cb6ee74219c78a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::LoadBalancing::Resolver, :freeze_time, feature_category: :database do
  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')
        response = service.resolve

        expect(response.address).to eq(ip_addr)
      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

        expect(subject.address).to eq(ip_addr)
      end

      context 'when nameserver is not in the hosts file' do
        let(:raw_ttl) { 10 }

        it 'looks the nameserver up in DNS' do
          resource = double(:resource, address: ip_addr, ttl: raw_ttl)
          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)

          expect(subject.address).to eq(ip_addr)
          expect(subject.ttl).to eq(raw_ttl.seconds.from_now)
        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