2018-08-17 09:04:58 -04:00
|
|
|
require 'fast_spec_helper'
|
|
|
|
|
|
|
|
require_dependency 'active_model'
|
2016-06-03 08:20:34 -04:00
|
|
|
|
|
|
|
describe Gitlab::Ci::Config do
|
2018-09-07 15:00:52 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-06-03 08:20:34 -04:00
|
|
|
let(:config) do
|
2018-09-07 16:22:52 -04:00
|
|
|
described_class.new(gitlab_ci_yml, { project: project, sha: '12345' })
|
2016-06-03 08:20:34 -04:00
|
|
|
end
|
|
|
|
|
2016-06-03 14:54:33 -04:00
|
|
|
context 'when config is valid' do
|
2018-09-07 16:03:05 -04:00
|
|
|
let(:gitlab_ci_yml) do
|
2016-06-03 08:20:34 -04:00
|
|
|
<<-EOS
|
|
|
|
image: ruby:2.2
|
|
|
|
|
|
|
|
rspec:
|
|
|
|
script:
|
|
|
|
- gem install rspec
|
|
|
|
- rspec
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#to_hash' do
|
|
|
|
it 'returns hash created from string' do
|
|
|
|
hash = {
|
|
|
|
image: 'ruby:2.2',
|
|
|
|
rspec: {
|
|
|
|
script: ['gem install rspec',
|
|
|
|
'rspec']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(config.to_hash).to eq hash
|
|
|
|
end
|
2016-06-06 06:23:27 -04:00
|
|
|
|
|
|
|
describe '#valid?' do
|
|
|
|
it 'is valid' do
|
|
|
|
expect(config).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has no errors' do
|
|
|
|
expect(config.errors).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2016-06-03 08:20:34 -04:00
|
|
|
end
|
2016-06-22 08:26:33 -04:00
|
|
|
end
|
2016-06-03 14:54:33 -04:00
|
|
|
|
2018-08-17 09:04:58 -04:00
|
|
|
context 'when using extendable hash' do
|
|
|
|
let(:yml) do
|
|
|
|
<<-EOS
|
|
|
|
image: ruby:2.2
|
|
|
|
|
|
|
|
rspec:
|
|
|
|
script: rspec
|
|
|
|
|
|
|
|
test:
|
|
|
|
extends: rspec
|
|
|
|
image: ruby:alpine
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'correctly extends the hash' do
|
|
|
|
hash = {
|
|
|
|
image: 'ruby:2.2',
|
|
|
|
rspec: { script: 'rspec' },
|
|
|
|
test: {
|
|
|
|
extends: 'rspec',
|
|
|
|
image: 'ruby:alpine',
|
|
|
|
script: 'rspec'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(config).to be_valid
|
|
|
|
expect(config.to_hash).to eq hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-22 08:26:33 -04:00
|
|
|
context 'when config is invalid' do
|
|
|
|
context 'when yml is incorrect' do
|
|
|
|
let(:yml) { '// invalid' }
|
2016-06-06 06:23:27 -04:00
|
|
|
|
2016-06-22 08:26:33 -04:00
|
|
|
describe '.new' do
|
|
|
|
it 'raises error' do
|
|
|
|
expect { config }.to raise_error(
|
2018-08-17 09:04:58 -04:00
|
|
|
described_class::ConfigError,
|
2016-06-22 08:26:33 -04:00
|
|
|
/Invalid configuration format/
|
|
|
|
)
|
2016-06-06 06:23:27 -04:00
|
|
|
end
|
|
|
|
end
|
2016-06-22 08:26:33 -04:00
|
|
|
end
|
2016-06-06 06:23:27 -04:00
|
|
|
|
2016-06-22 08:26:33 -04:00
|
|
|
context 'when config logic is incorrect' do
|
|
|
|
let(:yml) { 'before_script: "ls"' }
|
2016-06-06 06:23:27 -04:00
|
|
|
|
2016-06-22 08:26:33 -04:00
|
|
|
describe '#valid?' do
|
|
|
|
it 'is not valid' do
|
|
|
|
expect(config).not_to be_valid
|
|
|
|
end
|
2016-06-06 06:23:27 -04:00
|
|
|
|
2016-06-22 08:26:33 -04:00
|
|
|
it 'has errors' do
|
|
|
|
expect(config.errors).not_to be_empty
|
2016-06-03 14:54:33 -04:00
|
|
|
end
|
2016-06-22 08:26:33 -04:00
|
|
|
end
|
2016-06-09 08:48:40 -04:00
|
|
|
|
2016-06-22 08:26:33 -04:00
|
|
|
describe '#errors' do
|
|
|
|
it 'returns an array of strings' do
|
|
|
|
expect(config.errors).to all(be_an_instance_of(String))
|
2016-06-09 08:48:40 -04:00
|
|
|
end
|
2016-06-03 14:54:33 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-17 09:27:15 -04:00
|
|
|
|
|
|
|
context 'when invalid extended hash has been provided' do
|
|
|
|
let(:yml) do
|
|
|
|
<<-EOS
|
|
|
|
test:
|
|
|
|
extends: test
|
|
|
|
script: rspec
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { config }.to raise_error(
|
2018-09-05 04:17:13 -04:00
|
|
|
described_class::ConfigError, /circular dependency detected/
|
2018-08-17 09:27:15 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2016-06-03 08:20:34 -04:00
|
|
|
end
|
2018-09-07 14:53:28 -04:00
|
|
|
|
2018-09-07 16:03:05 -04:00
|
|
|
context "when gitlab_ci_yml has valid 'include' defined" do
|
2018-09-07 15:18:26 -04:00
|
|
|
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") }
|
2018-09-07 14:53:28 -04:00
|
|
|
let(:yml) do
|
|
|
|
<<-EOS
|
2018-09-07 15:33:06 -04:00
|
|
|
include:
|
2018-09-07 14:53:28 -04:00
|
|
|
- /spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml
|
|
|
|
- https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml
|
|
|
|
|
|
|
|
image: ruby:2.2
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2018-09-07 16:03:05 -04:00
|
|
|
allow_any_instance_of(::Gitlab::Ci::External::File::Local).to receive(:local_file_content).and_return(local_file_content)
|
2018-09-07 15:33:06 -04:00
|
|
|
allow(HTTParty).to receive(:get).and_return(http_file_content)
|
2018-09-07 14:53:28 -04:00
|
|
|
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
|
|
|
|
|
2018-09-07 16:03:05 -04:00
|
|
|
context "when gitlab_ci.yml has invalid 'include' defined" do
|
|
|
|
let(:gitlab_ci_yml) do
|
2018-09-07 14:53:28 -04:00
|
|
|
<<-EOS
|
2018-09-07 15:33:06 -04:00
|
|
|
include: invalid
|
2018-09-07 14:53:28 -04:00
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2018-09-07 15:33:06 -04:00
|
|
|
it 'raises error YamlProcessor ValidationError' do
|
2018-09-07 14:53:28 -04:00
|
|
|
expect { config }.to raise_error(
|
2018-09-07 15:33:06 -04:00
|
|
|
::Gitlab::Ci::YamlProcessor::ValidationError,
|
|
|
|
"External file: 'invalid' should be a valid local or remote file"
|
2018-09-07 14:53:28 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2018-09-07 15:37:44 -04:00
|
|
|
|
2018-09-07 16:03:05 -04:00
|
|
|
context "when both external files and gitlab_ci.yml defined the same key" do
|
|
|
|
let(:gitlab_ci_yml) do
|
2018-09-07 15:37:44 -04:00
|
|
|
<<~HEREDOC
|
|
|
|
include:
|
2018-09-07 16:03:05 -04:00
|
|
|
- https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.gitlab_ci_yml
|
2018-09-07 15:37:44 -04:00
|
|
|
|
|
|
|
image: ruby:2.2
|
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:http_file_content) do
|
|
|
|
<<~HEREDOC
|
|
|
|
image: php:5-fpm-alpine
|
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should take precedence' do
|
|
|
|
allow(HTTParty).to receive(:get).and_return(http_file_content)
|
2018-09-07 16:03:05 -04:00
|
|
|
expect(config.to_hash).to eq({ image: 'ruby:2.2' })
|
2018-09-07 15:37:44 -04:00
|
|
|
end
|
|
|
|
end
|
2016-06-03 08:20:34 -04:00
|
|
|
end
|