diff options
author | Matija Čupić <matteeyah@gmail.com> | 2018-09-07 20:53:28 +0200 |
---|---|---|
committer | Matija Čupić <matteeyah@gmail.com> | 2018-09-07 20:54:27 +0200 |
commit | 5f33690ebbe19dd8698bf8b432a5e6740305f007 (patch) | |
tree | 0f9714da4c8a8b2e70144df8967688550302efff /spec | |
parent | c3e33f06c2920a9f032ee8166cccf8423bd24b78 (diff) | |
download | gitlab-ce-5f33690ebbe19dd8698bf8b432a5e6740305f007.tar.gz |
Load external files in config
CE mirror of 8e03a6619be44fdaf19a6c13284ea8e51377b311
Diffstat (limited to 'spec')
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/global_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/includes_spec.rb | 97 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config_spec.rb | 57 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/external_files/external_file_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/external_files/mapper_spec.rb | 10 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/external_files/processor_spec.rb | 30 |
6 files changed, 82 insertions, 124 deletions
diff --git a/spec/lib/gitlab/ci/config/entry/global_spec.rb b/spec/lib/gitlab/ci/config/entry/global_spec.rb index ff623b95be8..1860ed79bfd 100644 --- a/spec/lib/gitlab/ci/config/entry/global_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/global_spec.rb @@ -13,7 +13,7 @@ describe Gitlab::Ci::Config::Entry::Global do expect(described_class.nodes.keys) .to match_array(%i[before_script image services after_script variables stages - types cache includes]) + types cache]) end end end @@ -42,7 +42,7 @@ describe Gitlab::Ci::Config::Entry::Global do end it 'creates node object for each entry' do - expect(global.descendants.count).to eq 9 + expect(global.descendants.count).to eq 8 end it 'creates node object using valid class' do @@ -189,7 +189,7 @@ describe Gitlab::Ci::Config::Entry::Global do describe '#nodes' do it 'instantizes all nodes' do - expect(global.descendants.count).to eq 9 + expect(global.descendants.count).to eq 8 end it 'contains unspecified nodes' do diff --git a/spec/lib/gitlab/ci/config/entry/includes_spec.rb b/spec/lib/gitlab/ci/config/entry/includes_spec.rb deleted file mode 100644 index d72503535ed..00000000000 --- a/spec/lib/gitlab/ci/config/entry/includes_spec.rb +++ /dev/null @@ -1,97 +0,0 @@ -require 'rails_helper' - -describe Gitlab::Ci::Config::Entry::Includes do - let(:entry) { described_class.new(config) } - - shared_examples 'valid external file' do - it 'should be valid' do - expect(entry).to be_valid - end - - it 'should not return any error' do - expect(entry.errors).to be_empty - end - end - - shared_examples 'invalid external file' do - it 'should not be valid' do - expect(entry).not_to be_valid - end - - it 'should return an error' do - expect(entry.errors.first).to match(/should be a valid local or remote file/) - end - end - - describe "#valid?" do - context 'with no external file given' do - let(:config) { nil } - - it_behaves_like 'valid external file' - end - - context 'with multiple external files' do - let(:config) { %w(https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-2.yml) } - - it_behaves_like 'valid external file' - end - - context 'with just one external file' do - let(:config) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } - - it_behaves_like 'valid external file' - end - - context 'when they contain valid URLs' do - let(:config) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } - - it_behaves_like 'valid external file' - end - - context 'when they contain valid relative URLs' do - let(:config) { '/vendor/gitlab-ci-yml/Auto-DevOps.gitlab-ci.yml' } - - it_behaves_like 'valid external file' - end - - context 'when they not contain valid URLs' do - let(:config) { 'not-valid://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } - - it_behaves_like 'invalid external file' - end - - context 'when they not contain valid relative URLs' do - let(:config) { '/vendor/gitlab-ci-yml/non-existent-file.yml' } - - it_behaves_like 'invalid external file' - end - end - - describe "#value" do - context 'with multiple external files' do - let(:config) { %w(https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-2.yml) } - it 'should return an array' do - expect(entry.value).to be_an(Array) - expect(entry.value.count).to eq(2) - end - end - - context 'with just one external file' do - let(:config) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } - - it 'should return an array' do - expect(entry.value).to be_an(Array) - expect(entry.value.count).to eq(1) - end - end - - context 'with no external file given' do - let(:config) { nil } - - it 'should return an empty array' do - expect(entry.value).to be_an(Array) - expect(entry.value).to be_empty - end - end - end -end diff --git a/spec/lib/gitlab/ci/config_spec.rb b/spec/lib/gitlab/ci/config_spec.rb index 5a78ce783dd..99896b9be5d 100644 --- a/spec/lib/gitlab/ci/config_spec.rb +++ b/spec/lib/gitlab/ci/config_spec.rb @@ -124,4 +124,61 @@ describe Gitlab::Ci::Config do end end end + + + context "when yml has valid 'includes' defined" do + let(:yml) do + <<-EOS + includes: + - /spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml + - /spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-2.yml + - https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml + + image: ruby:2.2 + EOS + end + + before do + allow_any_instance_of(Kernel).to receive_message_chain(:open, :read).and_return(yml) + end + + it 'should return a composed hash' do + before_script_values = [ + "apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs", "ruby -v", + "which ruby", + "gem install bundler --no-ri --no-rdoc", + "bundle install --jobs $(nproc) \"${FLAGS[@]}\"" + ] + variables = { + AUTO_DEVOPS_DOMAIN: "domain.example.com", + POSTGRES_USER: "user", + POSTGRES_PASSWORD: "testing-password", + POSTGRES_ENABLED: "true", + POSTGRES_DB: "$CI_ENVIRONMENT_SLUG" + } + composed_hash = { + before_script: before_script_values, + image: "ruby:2.2", + rspec: { script: ["bundle exec rspec"] }, + variables: variables + } + + expect(config.to_hash).to eq(composed_hash) + end + end + + context "when config has invalid 'includes' defined" do + let(:yml) do + <<-EOS + includes: invalid + EOS + end + + it 'raises error' do + expect { config }.to raise_error( + ::Gitlab::Ci::ExternalFiles::Processor::ExternalFileError, + /External files should be a valid local or remote file/ + ) + end + end end 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 80468f63435..33e5e0b3b77 100644 --- a/spec/lib/gitlab/ci/external_files/external_file_spec.rb +++ b/spec/lib/gitlab/ci/external_files/external_file_spec.rb @@ -13,7 +13,7 @@ describe Gitlab::Ci::ExternalFiles::ExternalFile do end context 'when is not a valid remote url' do - let(:value) { 'not-valid://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } + let(:value) { 'not-valid://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } it 'should return false' do expect(external_file.valid?).to be_falsy @@ -39,7 +39,7 @@ describe Gitlab::Ci::ExternalFiles::ExternalFile do end describe "#content" do - let(:external_file_content) { + let(:external_file_content) do <<-HEREDOC before_script: - apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs @@ -48,7 +48,7 @@ describe Gitlab::Ci::ExternalFiles::ExternalFile do - gem install bundler --no-ri --no-rdoc - bundle install --jobs $(nproc) "${FLAGS[@]}" HEREDOC - } + end context 'with a local file' do let(:value) { '/vendor/gitlab-ci-yml/non-existent-file.yml' } diff --git a/spec/lib/gitlab/ci/external_files/mapper_spec.rb b/spec/lib/gitlab/ci/external_files/mapper_spec.rb index dfbf8bfa098..57cf5e74cdc 100644 --- a/spec/lib/gitlab/ci/external_files/mapper_spec.rb +++ b/spec/lib/gitlab/ci/external_files/mapper_spec.rb @@ -3,7 +3,7 @@ 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(:values) { { includes: '/vendor/gitlab-ci-yml/non-existent-file.yml', image: 'ruby:2.2' } } it 'returns an array' do expect(described_class.fetch_paths(values)).to be_an(Array) @@ -15,21 +15,19 @@ describe Gitlab::Ci::ExternalFiles::Mapper do 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) { { includes: ['https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml', '/vendor/gitlab-ci-yml/template.yml'], image: 'ruby:2.2' } } it 'returns an array' do expect(described_class.fetch_paths(values)).to be_an(Array) end it 'returns ExternalFile instances' do paths = described_class.fetch_paths(values) - paths.each do |path| - expect(path).to be_an_instance_of(::Gitlab::Ci::ExternalFiles::ExternalFile) - end + expect(paths).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) { { image: 'ruby:2.2' } } it 'returns an empty array' do expect(described_class.fetch_paths(values)).to be_empty diff --git a/spec/lib/gitlab/ci/external_files/processor_spec.rb b/spec/lib/gitlab/ci/external_files/processor_spec.rb index 51ed14de4cf..7a0374a4bce 100644 --- a/spec/lib/gitlab/ci/external_files/processor_spec.rb +++ b/spec/lib/gitlab/ci/external_files/processor_spec.rb @@ -13,7 +13,7 @@ describe Gitlab::Ci::ExternalFiles::Processor do end context 'when an invalid local file is defined' do - let(:values) { { includes: '/vendor/gitlab-ci-yml/non-existent-file.yml', image: 'ruby:2.2'} } + let(:values) { { includes: '/vendor/gitlab-ci-yml/non-existent-file.yml', image: 'ruby:2.2' } } it 'should raise an error' do expect { processor.perform }.to raise_error(described_class::ExternalFileError) @@ -21,7 +21,7 @@ describe Gitlab::Ci::ExternalFiles::Processor do end context 'when an invalid remote file is defined' do - let(:values) { { includes: 'not-valid://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml', image: 'ruby:2.2'} } + let(:values) { { includes: 'not-valid://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml', image: 'ruby:2.2' } } it 'should raise an error' do expect { processor.perform }.to raise_error(described_class::ExternalFileError) @@ -30,7 +30,7 @@ describe Gitlab::Ci::ExternalFiles::Processor do context 'with a valid remote external file is defined' do let(:values) { { includes: 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml', image: 'ruby:2.2' } } - let(:external_file_content) { + let(:external_file_content) do <<-HEREDOC before_script: - apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs @@ -47,7 +47,7 @@ describe Gitlab::Ci::ExternalFiles::Processor do script: - bundle exec rubocop HEREDOC - } + end before do allow_any_instance_of(Kernel).to receive_message_chain(:open, :read).and_return(external_file_content) @@ -55,7 +55,7 @@ describe Gitlab::Ci::ExternalFiles::Processor do it 'should append the file to the values' do output = processor.perform - expect(output.keys).to match_array([:image, :before_script, :rspec, :rubocop]) + expect(output.keys).to match_array([:image, :before_script, :rspec, :rubocop]) end it "should remove the 'includes' keyword" do @@ -64,8 +64,8 @@ describe Gitlab::Ci::ExternalFiles::Processor do end context 'with a valid local external file is defined' do - let(:values) { { includes: '/vendor/gitlab-ci-yml/template.yml' , image: 'ruby:2.2'} } - let(:external_file_content) { + let(:values) { { includes: '/vendor/gitlab-ci-yml/template.yml', image: 'ruby:2.2' } } + let(:external_file_content) do <<-HEREDOC before_script: - apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs @@ -74,7 +74,7 @@ describe Gitlab::Ci::ExternalFiles::Processor do - gem install bundler --no-ri --no-rdoc - bundle install --jobs $(nproc) "${FLAGS[@]}" HEREDOC - } + end before do allow(File).to receive(:exists?).and_return(true) @@ -83,7 +83,7 @@ describe Gitlab::Ci::ExternalFiles::Processor do it 'should append the file to the values' do output = processor.perform - expect(output.keys).to match_array([:image, :before_script]) + expect(output.keys).to match_array([:image, :before_script]) end it "should remove the 'includes' keyword" do @@ -92,23 +92,23 @@ describe Gitlab::Ci::ExternalFiles::Processor do end context 'with multiple external files are defined' do - let(:external_files) { + 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' ] - } - let(:values) { { includes: external_files, image: 'ruby:2.2'} } + end + let(:values) { { includes: external_files, image: 'ruby:2.2' } } - let(:remote_file_content) { + let(:remote_file_content) do <<-HEREDOC stages: - build - review - cleanup HEREDOC - } + end before do allow_any_instance_of(Kernel).to receive_message_chain(:open, :read).and_return(remote_file_content) @@ -124,7 +124,7 @@ describe Gitlab::Ci::ExternalFiles::Processor do end context 'when external files are defined but not valid' do - let(:values) { { includes: '/vendor/gitlab-ci-yml/template.yml', image: 'ruby:2.2'} } + let(:values) { { includes: '/vendor/gitlab-ci-yml/template.yml', image: 'ruby:2.2' } } let(:external_file_content) { 'invalid content file ////' } |