Commit 6a8133b9 authored by Matija Čupić's avatar Matija Čupić

Stub http request on specs intead of mocking HTTParty

CE mirror of bb2a9fde8e6a4d1df13638fe336f641b9c72ef59
parent 3af363ec
...@@ -3,6 +3,7 @@ module Gitlab ...@@ -3,6 +3,7 @@ module Gitlab
module External module External
module File module File
class Remote class Remote
include Gitlab::Utils::StrongMemoize
attr_reader :location attr_reader :location
def initialize(location, opts = {}) def initialize(location, opts = {})
...@@ -16,11 +17,13 @@ module Gitlab ...@@ -16,11 +17,13 @@ module Gitlab
def content def content
return @content if defined?(@content) return @content if defined?(@content)
@content ||= begin @content = strong_memoize(:content) do
HTTParty.get(location) begin
rescue HTTParty::Error, Timeout::Error HTTParty.get(location)
false rescue HTTParty::Error, Timeout::Error
end false
end
end
end end
end end
end end
......
...@@ -127,7 +127,7 @@ describe Gitlab::Ci::Config do ...@@ -127,7 +127,7 @@ describe Gitlab::Ci::Config do
end end
context "when gitlab_ci_yml has valid 'include' defined" do context "when gitlab_ci_yml has valid 'include' defined" do
let(:http_file_content) do let(:remote_file_content) do
<<~HEREDOC <<~HEREDOC
variables: variables:
AUTO_DEVOPS_DOMAIN: domain.example.com AUTO_DEVOPS_DOMAIN: domain.example.com
...@@ -138,11 +138,12 @@ describe Gitlab::Ci::Config do ...@@ -138,11 +138,12 @@ describe Gitlab::Ci::Config do
HEREDOC HEREDOC
end end
let(:local_file_content) { File.read("#{Rails.root}/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml") } let(:local_file_content) { File.read("#{Rails.root}/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml") }
let(:yml) do let(:remote_location) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:gitlab_ci_yml) do
<<-EOS <<-EOS
include: include:
- /spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml - /spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml
- https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml - #{remote_location}
image: ruby:2.2 image: ruby:2.2
EOS EOS
...@@ -150,7 +151,7 @@ describe Gitlab::Ci::Config do ...@@ -150,7 +151,7 @@ describe Gitlab::Ci::Config do
before do before do
allow_any_instance_of(::Gitlab::Ci::External::File::Local).to receive(:local_file_content).and_return(local_file_content) allow_any_instance_of(::Gitlab::Ci::External::File::Local).to receive(:local_file_content).and_return(local_file_content)
allow(HTTParty).to receive(:get).and_return(http_file_content) WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
end end
it 'should return a composed hash' do it 'should return a composed hash' do
...@@ -194,23 +195,24 @@ describe Gitlab::Ci::Config do ...@@ -194,23 +195,24 @@ describe Gitlab::Ci::Config do
end end
context "when both external files and gitlab_ci.yml defined the same key" do context "when both external files and gitlab_ci.yml defined the same key" do
let(:remote_location) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:gitlab_ci_yml) do let(:gitlab_ci_yml) do
<<~HEREDOC <<~HEREDOC
include: include:
- https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.gitlab_ci_yml - #{remote_location}
image: ruby:2.2 image: ruby:2.2
HEREDOC HEREDOC
end end
let(:http_file_content) do let(:remote_file_content) do
<<~HEREDOC <<~HEREDOC
image: php:5-fpm-alpine image: php:5-fpm-alpine
HEREDOC HEREDOC
end end
it 'should take precedence' do it 'should take precedence' do
allow(HTTParty).to receive(:get).and_return(http_file_content) WebMock.stub_request(:get, remote_location).to_return(body: remote_file_content)
expect(config.to_hash).to eq({ image: 'ruby:2.2' }) expect(config.to_hash).to eq({ image: 'ruby:2.2' })
end end
end end
......
...@@ -36,8 +36,8 @@ describe Gitlab::Ci::External::Processor do ...@@ -36,8 +36,8 @@ describe Gitlab::Ci::External::Processor do
end end
context 'with a valid remote external file is defined' do context 'with a valid remote external file is defined' do
let(:remote_url) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } let(:remote_file) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:values) { { include: remote_url, image: 'ruby:2.2' } } let(:values) { { include: remote_file, image: 'ruby:2.2' } }
let(:external_file_content) do let(:external_file_content) do
<<-HEREDOC <<-HEREDOC
before_script: before_script:
...@@ -58,7 +58,7 @@ describe Gitlab::Ci::External::Processor do ...@@ -58,7 +58,7 @@ describe Gitlab::Ci::External::Processor do
end end
before do before do
WebMock.stub_request(:get, remote_url).to_return(body: external_file_content) WebMock.stub_request(:get, remote_file).to_return(body: external_file_content)
end end
it 'should append the file to the values' do it 'should append the file to the values' do
...@@ -99,11 +99,11 @@ describe Gitlab::Ci::External::Processor do ...@@ -99,11 +99,11 @@ describe Gitlab::Ci::External::Processor do
end end
context 'with multiple external files are defined' do context 'with multiple external files are defined' do
let(:remote_url) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } let(:remote_file) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:external_files) do let(:external_files) do
[ [
"/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml", "/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml",
remote_url remote_file
] ]
end end
let(:values) do let(:values) do
...@@ -125,7 +125,7 @@ describe Gitlab::Ci::External::Processor do ...@@ -125,7 +125,7 @@ describe Gitlab::Ci::External::Processor do
before do before do
local_file_content = File.read("#{Rails.root}/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml") local_file_content = File.read("#{Rails.root}/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml")
allow_any_instance_of(Gitlab::Ci::External::File::Local).to receive(:local_file_content).and_return(local_file_content) allow_any_instance_of(Gitlab::Ci::External::File::Local).to receive(:local_file_content).and_return(local_file_content)
WebMock.stub_request(:get, remote_url).to_return(body: remote_file_content) WebMock.stub_request(:get, remote_file).to_return(body: remote_file_content)
end end
it 'should append the files to the values' do it 'should append the files to the values' do
...@@ -152,9 +152,10 @@ describe Gitlab::Ci::External::Processor do ...@@ -152,9 +152,10 @@ describe Gitlab::Ci::External::Processor do
end end
context "when both external files and values defined the same key" do context "when both external files and values defined the same key" do
let(:remote_file) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
let(:values) do let(:values) do
{ {
include: 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml', include: remote_file,
image: 'ruby:2.2' image: 'ruby:2.2'
} }
end end
...@@ -166,7 +167,7 @@ describe Gitlab::Ci::External::Processor do ...@@ -166,7 +167,7 @@ describe Gitlab::Ci::External::Processor do
end end
it 'should take precedence' do it 'should take precedence' do
allow(HTTParty).to receive(:get).and_return(remote_file_content) WebMock.stub_request(:get, remote_file).to_return(body: remote_file_content)
expect(processor.perform[:image]).to eq('ruby:2.2') expect(processor.perform[:image]).to eq('ruby:2.2')
end end
end end
......
...@@ -2,22 +2,24 @@ require 'spec_helper' ...@@ -2,22 +2,24 @@ require 'spec_helper'
describe BlobViewer::GitlabCiYml do describe BlobViewer::GitlabCiYml do
include FakeBlobHelpers include FakeBlobHelpers
include RepoHelpers
let(:project) { build_stubbed(:project) } let(:project) { build_stubbed(:project, :repository) }
let(:data) { File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) } let(:data) { File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) }
let(:blob) { fake_blob(path: '.gitlab-ci.yml', data: data) } let(:blob) { fake_blob(path: '.gitlab-ci.yml', data: data) }
let(:sha) { sample_commit.id }
subject { described_class.new(blob) } subject { described_class.new(blob) }
describe '#validation_message' do describe '#validation_message' do
it 'calls prepare! on the viewer' do it 'calls prepare! on the viewer' do
expect(subject).to receive(:prepare!) expect(subject).to receive(:prepare!)
subject.validation_message(project, project.default_branch) subject.validation_message(project, sha)
end end
context 'when the configuration is valid' do context 'when the configuration is valid' do
it 'returns nil' do it 'returns nil' do
expect(subject.validation_message(project, project.default_branch)).to be_nil expect(subject.validation_message(project, sha)).to be_nil
end end
end end
...@@ -25,7 +27,7 @@ describe BlobViewer::GitlabCiYml do ...@@ -25,7 +27,7 @@ describe BlobViewer::GitlabCiYml do
let(:data) { 'oof' } let(:data) { 'oof' }
it 'returns the error message' do it 'returns the error message' do
expect(subject.validation_message(project, project.default_branch)).to eq('Invalid configuration format') expect(subject.validation_message(project, sha)).to eq('Invalid configuration format')
end end
end end
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