diff options
author | Matija Čupić <matteeyah@gmail.com> | 2018-09-07 21:18:26 +0200 |
---|---|---|
committer | Matija Čupić <matteeyah@gmail.com> | 2018-09-07 21:18:26 +0200 |
commit | 95b296f8ac8578e142efd6a60a582be4da5b09be (patch) | |
tree | 9014eb0ae9730ec8fb32d1f76e277e92b8785631 /spec | |
parent | cd72189fc0127e9993eebaba9569f912394cc5c9 (diff) | |
download | gitlab-ce-95b296f8ac8578e142efd6a60a582be4da5b09be.tar.gz |
Change ExternalFile to retrieve local file from repository instead of GitLab project
CE mirror of 03c6094997023d9c8875ced421a6c9ef39a4af44
Diffstat (limited to 'spec')
-rw-r--r-- | spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-2.yml | 8 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config_spec.rb | 16 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/external_files/external_file_spec.rb | 10 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/external_files/mapper_spec.rb | 42 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/external_files/processor_spec.rb | 14 |
5 files changed, 59 insertions, 31 deletions
diff --git a/spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-2.yml b/spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-2.yml deleted file mode 100644 index b341cca8946..00000000000 --- a/spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-2.yml +++ /dev/null @@ -1,8 +0,0 @@ -variables: - # AUTO_DEVOPS_DOMAIN is the application deployment domain and should be set as a variable at the group or project level. - - AUTO_DEVOPS_DOMAIN: domain.example.com - POSTGRES_USER: user - POSTGRES_PASSWORD: testing-password - POSTGRES_ENABLED: "true" - POSTGRES_DB: $CI_ENVIRONMENT_SLUG diff --git a/spec/lib/gitlab/ci/config_spec.rb b/spec/lib/gitlab/ci/config_spec.rb index 57354e12aa3..b1c801ff052 100644 --- a/spec/lib/gitlab/ci/config_spec.rb +++ b/spec/lib/gitlab/ci/config_spec.rb @@ -127,6 +127,17 @@ describe Gitlab::Ci::Config do end context "when yml has valid 'includes' defined" do + let(:http_file_content) do + <<~HEREDOC + variables: + AUTO_DEVOPS_DOMAIN: domain.example.com + POSTGRES_USER: user + POSTGRES_PASSWORD: testing-password + POSTGRES_ENABLED: "true" + POSTGRES_DB: $CI_ENVIRONMENT_SLUG + HEREDOC + end + let(:local_file_content) { File.read("#{Rails.root}/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml") } let(:yml) do <<-EOS includes: @@ -139,7 +150,8 @@ describe Gitlab::Ci::Config do end before do - allow_any_instance_of(Kernel).to receive_message_chain(:open, :read).and_return(yml) + allow_any_instance_of(::Gitlab::Ci::ExternalFiles::ExternalFile).to receive(:local_file_content).and_return(local_file_content) + allow_any_instance_of(Kernel).to receive_message_chain(:open, :read).and_return(http_file_content) end it 'should return a composed hash' do @@ -167,7 +179,7 @@ describe Gitlab::Ci::Config do end end - context "when config has invalid 'includes' defined" do + context "when yml has invalid 'includes' defined" do let(:yml) do <<-EOS includes: invalid diff --git a/spec/lib/gitlab/ci/external_files/external_file_spec.rb b/spec/lib/gitlab/ci/external_files/external_file_spec.rb index 33e5e0b3b77..b2aeb0ac67a 100644 --- a/spec/lib/gitlab/ci/external_files/external_file_spec.rb +++ b/spec/lib/gitlab/ci/external_files/external_file_spec.rb @@ -1,12 +1,17 @@ require 'rails_helper' describe Gitlab::Ci::ExternalFiles::ExternalFile do - let(:external_file) { described_class.new(value) } + let(:project) { create(:project, :repository) } + let(:external_file) { described_class.new(value, project) } describe "#valid?" do context 'when is a valid remote url' do let(:value) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } + before do + allow_any_instance_of(described_class).to receive(:local_file_content).and_return("image: 'ruby2:2'") + end + it 'should return true' do expect(external_file.valid?).to be_truthy end @@ -54,8 +59,7 @@ describe Gitlab::Ci::ExternalFiles::ExternalFile do let(:value) { '/vendor/gitlab-ci-yml/non-existent-file.yml' } before do - allow(File).to receive(:exists?).and_return(true) - allow(File).to receive(:read).and_return(external_file_content) + allow_any_instance_of(described_class).to receive(:local_file_content).and_return(external_file_content) end it 'should return the content of the file' do diff --git a/spec/lib/gitlab/ci/external_files/mapper_spec.rb b/spec/lib/gitlab/ci/external_files/mapper_spec.rb index 57cf5e74cdc..4bdb6c200c4 100644 --- a/spec/lib/gitlab/ci/external_files/mapper_spec.rb +++ b/spec/lib/gitlab/ci/external_files/mapper_spec.rb @@ -1,36 +1,56 @@ require 'rails_helper' describe Gitlab::Ci::ExternalFiles::Mapper do - describe '.fetch_paths' do - context 'when includes is defined as string' do - let(:values) { { includes: '/vendor/gitlab-ci-yml/non-existent-file.yml', image: 'ruby:2.2' } } + let(:project) { create(:project, :repository) } + + describe '#process' do + subject { described_class.new(values, project).process } + + context 'when includes keyword is defined as string' do + let(:values) do + { + includes: '/vendor/gitlab-ci-yml/non-existent-file.yml', + image: 'ruby:2.2' + } + end it 'returns an array' do - expect(described_class.fetch_paths(values)).to be_an(Array) + expect(subject).to be_an(Array) end it 'returns ExternalFile instances' do - expect(described_class.fetch_paths(values).first).to be_an_instance_of(::Gitlab::Ci::ExternalFiles::ExternalFile) + expect(subject.first).to be_an_instance_of(::Gitlab::Ci::ExternalFiles::ExternalFile) end end context 'when includes is defined as an array' do - let(:values) { { includes: ['https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml', '/vendor/gitlab-ci-yml/template.yml'], image: 'ruby:2.2' } } + let(:values) do + { + includes: + [ + 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml', + '/vendor/gitlab-ci-yml/template.yml' + ], + image: 'ruby:2.2' + } + end + it 'returns an array' do - expect(described_class.fetch_paths(values)).to be_an(Array) + expect(subject).to be_an(Array) end it 'returns ExternalFile instances' do - paths = described_class.fetch_paths(values) - expect(paths).to all(be_an_instance_of(::Gitlab::Ci::ExternalFiles::ExternalFile)) + expect(subject).to all(be_an_instance_of(::Gitlab::Ci::ExternalFiles::ExternalFile)) end end context 'when includes is not defined' do - let(:values) { { image: 'ruby:2.2' } } + let(:values) do + { image: 'ruby:2.2' } + end it 'returns an empty array' do - expect(described_class.fetch_paths(values)).to be_empty + expect(subject).to be_empty end end end diff --git a/spec/lib/gitlab/ci/external_files/processor_spec.rb b/spec/lib/gitlab/ci/external_files/processor_spec.rb index 7a0374a4bce..f009ece8821 100644 --- a/spec/lib/gitlab/ci/external_files/processor_spec.rb +++ b/spec/lib/gitlab/ci/external_files/processor_spec.rb @@ -1,7 +1,8 @@ require 'rails_helper' describe Gitlab::Ci::ExternalFiles::Processor do - let(:processor) { described_class.new(values) } + let(:project) { create(:project, :repository) } + let(:processor) { described_class.new(values, project) } describe "#perform" do context 'when no external files defined' do @@ -77,8 +78,7 @@ describe Gitlab::Ci::ExternalFiles::Processor do end before do - allow(File).to receive(:exists?).and_return(true) - allow(File).to receive(:read).and_return(external_file_content) + allow_any_instance_of(::Gitlab::Ci::ExternalFiles::ExternalFile).to receive(:local_file_content).and_return(external_file_content) end it 'should append the file to the values' do @@ -95,7 +95,6 @@ describe Gitlab::Ci::ExternalFiles::Processor 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-2.yml", 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' ] end @@ -111,11 +110,13 @@ describe Gitlab::Ci::ExternalFiles::Processor do end before do + file_content = File.read("#{Rails.root}/spec/ee/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml") + allow_any_instance_of(::Gitlab::Ci::ExternalFiles::ExternalFile).to receive(:local_file_content).and_return(file_content) allow_any_instance_of(Kernel).to receive_message_chain(:open, :read).and_return(remote_file_content) end it 'should append the files to the values' do - expect(processor.perform.keys).to match_array([:image, :variables, :stages, :before_script, :rspec]) + expect(processor.perform.keys).to match_array([:image, :stages, :before_script, :rspec]) end it "should remove the 'includes' keyword" do @@ -129,8 +130,7 @@ describe Gitlab::Ci::ExternalFiles::Processor do let(:external_file_content) { 'invalid content file ////' } before do - allow(File).to receive(:exists?).and_return(true) - allow(File).to receive(:read).and_return(external_file_content) + allow_any_instance_of(::Gitlab::Ci::ExternalFiles::ExternalFile).to receive(:local_file_content).and_return(external_file_content) end it 'should raise an error' do |