diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2019-07-30 01:27:52 +0000 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2019-07-30 01:27:52 +0000 |
commit | 534f8fc7975f1fe5f38f8d4b2ba3c15ddb8ac9d1 (patch) | |
tree | b90c08cb6f57746d6fcbb02d6193c94fbcb188c9 | |
parent | 8d9451dff764300334faf27d40226fb1ae8767ac (diff) | |
parent | 9c8f6e0cdfcf3be90bd819751cf76ba760556d13 (diff) | |
download | gitlab-ce-534f8fc7975f1fe5f38f8d4b2ba3c15ddb8ac9d1.tar.gz |
Merge branch 'determinstic_dns_specs' into 'master'
Have deterministic DNS for specs
See merge request gitlab-org/gitlab-ce!31086
-rw-r--r-- | Gemfile | 1 | ||||
-rw-r--r-- | Gemfile.lock | 3 | ||||
-rw-r--r-- | lib/container_registry/client.rb | 5 | ||||
-rw-r--r-- | spec/lib/container_registry/blob_spec.rb | 19 | ||||
-rw-r--r-- | spec/lib/gitlab/http_connection_adapter_spec.rb | 6 | ||||
-rw-r--r-- | spec/services/projects/download_service_spec.rb | 9 | ||||
-rw-r--r-- | spec/support/helpers/stub_requests.rb | 13 |
7 files changed, 43 insertions, 13 deletions
@@ -388,7 +388,6 @@ group :test do gem 'json-schema', '~> 2.8.0' gem 'webmock', '~> 3.5.1' gem 'rails-controller-testing' - gem 'sham_rack', '~> 1.3.6' gem 'concurrent-ruby', '~> 1.1' gem 'test-prof', '~> 0.2.5' gem 'rspec_junit_formatter' diff --git a/Gemfile.lock b/Gemfile.lock index a87bf239fb9..f9f6616bad2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -884,8 +884,6 @@ GEM faraday (>= 0.7.6, < 1.0) settingslogic (2.0.9) sexp_processor (4.12.0) - sham_rack (1.3.6) - rack shoulda-matchers (4.0.1) activesupport (>= 4.2.0) sidekiq (5.2.7) @@ -1232,7 +1230,6 @@ DEPENDENCIES selenium-webdriver (~> 3.141) sentry-raven (~> 2.9) settingslogic (~> 2.0.9) - sham_rack (~> 1.3.6) shoulda-matchers (~> 4.0.1) sidekiq (~> 5.2.7) sidekiq-cron (~> 1.0) diff --git a/lib/container_registry/client.rb b/lib/container_registry/client.rb index c3a19af7a94..82810ea4076 100644 --- a/lib/container_registry/client.rb +++ b/lib/container_registry/client.rb @@ -82,7 +82,10 @@ module ContainerRegistry def redirect_response(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 def faraday diff --git a/spec/lib/container_registry/blob_spec.rb b/spec/lib/container_registry/blob_spec.rb index ba7f76cfa3b..be7be2f3719 100644 --- a/spec/lib/container_registry/blob_spec.rb +++ b/spec/lib/container_registry/blob_spec.rb @@ -112,11 +112,28 @@ describe ContainerRegistry::Blob do 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 let(:location) { 'file:///etc/passwd' } 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 diff --git a/spec/lib/gitlab/http_connection_adapter_spec.rb b/spec/lib/gitlab/http_connection_adapter_spec.rb index 930d1f62272..1532fd1103e 100644 --- a/spec/lib/gitlab/http_connection_adapter_spec.rb +++ b/spec/lib/gitlab/http_connection_adapter_spec.rb @@ -3,7 +3,13 @@ require 'spec_helper' describe Gitlab::HTTPConnectionAdapter do + include StubRequests + 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 it 'sets up the connection' do uri = URI('https://example.org') diff --git a/spec/services/projects/download_service_spec.rb b/spec/services/projects/download_service_spec.rb index f25233ceeb1..06efc2ff825 100644 --- a/spec/services/projects/download_service_spec.rb +++ b/spec/services/projects/download_service_spec.rb @@ -20,13 +20,8 @@ describe Projects::DownloadService do context 'for URLs that are on the whitelist' do before do - sham_rack_app = ShamRack.at('mycompany.fogbugz.com').stub - sham_rack_app.register_resource('/rails_sample.jpg', File.read(Rails.root + 'spec/fixtures/rails_sample.jpg'), 'image/jpg') - 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 + stub_request(:get, 'http://mycompany.fogbugz.com/rails_sample.jpg').to_return(body: File.read(Rails.root + 'spec/fixtures/rails_sample.jpg')) + stub_request(:get, 'http://mycompany.fogbugz.com/doc_sample.txt').to_return(body: File.read(Rails.root + 'spec/fixtures/doc_sample.txt')) end context 'an image file' do diff --git a/spec/support/helpers/stub_requests.rb b/spec/support/helpers/stub_requests.rb index 6eb8007ed26..473f07dd413 100644 --- a/spec/support/helpers/stub_requests.rb +++ b/spec/support/helpers/stub_requests.rb @@ -28,6 +28,19 @@ module StubRequests .and_return([addr]) 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) url = parse_url(url) url.hostname = hostname |