Commit f613eb4d authored by Robert May's avatar Robert May Committed by Robert Speicher

Stub DNS in the spec suite

This implements stubbing throughout the spec suite for DNS queries,
of which there are many. It includes a few tweaks to continue to allow
resolution for local addresses. This solves an issue where non-standard
DNS setups prevent most of the spec suite from working locally, and
should also provide speed improvements to the CI pipeline on average
by removing a source of external requests.
parent 397f5c05
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::TcpChecker do describe Gitlab::TcpChecker, :permit_dns do
before do before do
@server = TCPServer.new('localhost', 0) @server = TCPServer.new('localhost', 0)
_, @port, _, @ip = @server.addr _, @port, _, @ip = @server.addr
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::UrlBlocker do describe Gitlab::UrlBlocker, :stub_invalid_dns_only do
include StubRequests include StubRequests
describe '#validate!' do 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