Commit 6b970f4c authored by Robert Speicher's avatar Robert Speicher

Merge branch 'break-dns-resolution-in-specs' into 'master'

Stub DNS in the spec suite

See merge request gitlab-org/gitlab!22368
parents 397f5c05 f613eb4d
......@@ -2,7 +2,7 @@
require 'spec_helper'
describe Gitlab::TcpChecker do
describe Gitlab::TcpChecker, :permit_dns do
before do
@server = TCPServer.new('localhost', 0)
_, @port, _, @ip = @server.addr
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
describe Gitlab::UrlBlocker do
describe Gitlab::UrlBlocker, :stub_invalid_dns_only do
include StubRequests
describe '#validate!' do
......
# frozen_string_literal: true
require Rails.root.join("spec/support/helpers/dns_helpers")
RSpec.configure do |config|
config.include DnsHelpers
config.before do
block_dns!
end
config.before(:each, :permit_dns) do
permit_dns!
end
config.before(:each, :stub_invalid_dns_only) do
permit_dns!
stub_invalid_dns!
end
end
# frozen_string_literal: true
module DnsHelpers
def block_dns!
stub_all_dns!
stub_invalid_dns!
permit_local_dns!
end
def permit_dns!
allow(Addrinfo).to receive(:getaddrinfo).and_call_original
end
def stub_all_dns!
allow(Addrinfo).to receive(:getaddrinfo).with(anything, anything, nil, :STREAM).and_return([])
allow(Addrinfo).to receive(:getaddrinfo).with(anything, anything, nil, :STREAM, anything, anything).and_return([])
end
def stub_invalid_dns!
allow(Addrinfo).to receive(:getaddrinfo).with(/foobar\.\w|(\d{1,3}\.){4,}\d{1,3}/i, anything, nil, :STREAM) do
raise SocketError.new("getaddrinfo: Name or service not known")
end
end
def permit_local_dns!
local_addresses = /(127|10)\.0\.0\.\d{1,3}|(192\.168|172\.16)\.\d{1,3}\.\d{1,3}|0\.0\.0\.0|localhost/i
allow(Addrinfo).to receive(:getaddrinfo).with(local_addresses, anything, nil, :STREAM).and_call_original
allow(Addrinfo).to receive(:getaddrinfo).with(local_addresses, anything, nil, :STREAM, anything, anything).and_call_original
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