Commit 534f8fc7 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'determinstic_dns_specs' into 'master'

Have deterministic DNS for specs

See merge request gitlab-org/gitlab-ce!31086
parents 8d9451df 9c8f6e0c
...@@ -388,7 +388,6 @@ group :test do ...@@ -388,7 +388,6 @@ group :test do
gem 'json-schema', '~> 2.8.0' gem 'json-schema', '~> 2.8.0'
gem 'webmock', '~> 3.5.1' gem 'webmock', '~> 3.5.1'
gem 'rails-controller-testing' gem 'rails-controller-testing'
gem 'sham_rack', '~> 1.3.6'
gem 'concurrent-ruby', '~> 1.1' gem 'concurrent-ruby', '~> 1.1'
gem 'test-prof', '~> 0.2.5' gem 'test-prof', '~> 0.2.5'
gem 'rspec_junit_formatter' gem 'rspec_junit_formatter'
......
...@@ -884,8 +884,6 @@ GEM ...@@ -884,8 +884,6 @@ GEM
faraday (>= 0.7.6, < 1.0) faraday (>= 0.7.6, < 1.0)
settingslogic (2.0.9) settingslogic (2.0.9)
sexp_processor (4.12.0) sexp_processor (4.12.0)
sham_rack (1.3.6)
rack
shoulda-matchers (4.0.1) shoulda-matchers (4.0.1)
activesupport (>= 4.2.0) activesupport (>= 4.2.0)
sidekiq (5.2.7) sidekiq (5.2.7)
...@@ -1232,7 +1230,6 @@ DEPENDENCIES ...@@ -1232,7 +1230,6 @@ DEPENDENCIES
selenium-webdriver (~> 3.141) selenium-webdriver (~> 3.141)
sentry-raven (~> 2.9) sentry-raven (~> 2.9)
settingslogic (~> 2.0.9) settingslogic (~> 2.0.9)
sham_rack (~> 1.3.6)
shoulda-matchers (~> 4.0.1) shoulda-matchers (~> 4.0.1)
sidekiq (~> 5.2.7) sidekiq (~> 5.2.7)
sidekiq-cron (~> 1.0) sidekiq-cron (~> 1.0)
......
...@@ -82,7 +82,10 @@ module ContainerRegistry ...@@ -82,7 +82,10 @@ module ContainerRegistry
def redirect_response(location) def redirect_response(location)
return unless location return unless location
faraday_redirect.get(location) uri = URI(@base_uri).merge(location)
raise ArgumentError, "Invalid scheme for #{location}" unless %w[http https].include?(uri.scheme)
faraday_redirect.get(uri)
end end
def faraday def faraday
......
...@@ -112,11 +112,28 @@ describe ContainerRegistry::Blob do ...@@ -112,11 +112,28 @@ describe ContainerRegistry::Blob do
end end
end end
context 'for a relative address' do
before do
stub_request(:get, 'http://registry.gitlab/relative')
.with { |request| !request.headers.include?('Authorization') }
.to_return(
status: 200,
headers: { 'Content-Type' => 'application/json' },
body: '{"key":"value"}')
end
let(:location) { '/relative' }
it 'returns correct data' do
expect(blob.data).to eq '{"key":"value"}'
end
end
context 'for invalid file' do context 'for invalid file' do
let(:location) { 'file:///etc/passwd' } let(:location) { 'file:///etc/passwd' }
it 'raises an error' do it 'raises an error' do
expect { blob.data }.to raise_error(ArgumentError, 'invalid address') expect { blob.data }.to raise_error(ArgumentError, 'Invalid scheme for file:///etc/passwd')
end end
end end
end end
......
...@@ -3,7 +3,13 @@ ...@@ -3,7 +3,13 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::HTTPConnectionAdapter do describe Gitlab::HTTPConnectionAdapter do
include StubRequests
describe '#connection' do describe '#connection' do
before do
stub_all_dns('https://example.org', ip_address: '93.184.216.34')
end
context 'when local requests are not allowed' do context 'when local requests are not allowed' do
it 'sets up the connection' do it 'sets up the connection' do
uri = URI('https://example.org') uri = URI('https://example.org')
......
...@@ -20,13 +20,8 @@ describe Projects::DownloadService do ...@@ -20,13 +20,8 @@ describe Projects::DownloadService do
context 'for URLs that are on the whitelist' do context 'for URLs that are on the whitelist' do
before do before do
sham_rack_app = ShamRack.at('mycompany.fogbugz.com').stub stub_request(:get, 'http://mycompany.fogbugz.com/rails_sample.jpg').to_return(body: File.read(Rails.root + 'spec/fixtures/rails_sample.jpg'))
sham_rack_app.register_resource('/rails_sample.jpg', File.read(Rails.root + 'spec/fixtures/rails_sample.jpg'), 'image/jpg') stub_request(:get, 'http://mycompany.fogbugz.com/doc_sample.txt').to_return(body: File.read(Rails.root + 'spec/fixtures/doc_sample.txt'))
sham_rack_app.register_resource('/doc_sample.txt', File.read(Rails.root + 'spec/fixtures/doc_sample.txt'), 'text/plain')
end
after do
ShamRack.unmount_all
end end
context 'an image file' do context 'an image file' do
......
...@@ -28,6 +28,19 @@ module StubRequests ...@@ -28,6 +28,19 @@ module StubRequests
.and_return([addr]) .and_return([addr])
end end
def stub_all_dns(url, ip_address:)
url = URI(url)
port = 80 # arbitarily chosen, does not matter as we are not going to connect
socket = Socket.sockaddr_in(port, ip_address)
addr = Addrinfo.new(socket)
# See Gitlab::UrlBlocker
allow(Addrinfo).to receive(:getaddrinfo).and_call_original
allow(Addrinfo).to receive(:getaddrinfo)
.with(url.hostname, anything, nil, :STREAM)
.and_return([addr])
end
def stubbed_hostname(url, hostname: IP_ADDRESS_STUB) def stubbed_hostname(url, hostname: IP_ADDRESS_STUB)
url = parse_url(url) url = parse_url(url)
url.hostname = hostname url.hostname = hostname
......
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