2018-10-16 09:09:36 -04:00
|
|
|
require 'spec_helper'
|
2016-06-03 08:20:34 -04:00
|
|
|
|
|
|
|
describe Gitlab::Ci::Config do
|
2019-04-21 06:03:26 -04:00
|
|
|
include StubRequests
|
|
|
|
|
2018-12-01 06:39:13 -05:00
|
|
|
set(:user) { create(:user) }
|
|
|
|
|
2016-06-03 08:20:34 -04:00
|
|
|
let(:config) do
|
2018-12-01 06:39:13 -05:00
|
|
|
described_class.new(yml, project: nil, sha: nil, user: nil)
|
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-08 08:55:36 -04:00
|
|
|
let(: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
|
|
|
|
2019-07-02 02:23:06 -04:00
|
|
|
context 'when yml is too big' do
|
|
|
|
let(:yml) do
|
|
|
|
<<~YAML
|
|
|
|
--- &1
|
|
|
|
- hi
|
|
|
|
- *1
|
|
|
|
YAML
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.new' do
|
|
|
|
it 'raises error' do
|
|
|
|
expect(Gitlab::Sentry).to receive(:track_exception)
|
|
|
|
|
|
|
|
expect { config }.to raise_error(
|
|
|
|
described_class::ConfigError,
|
|
|
|
/The parsed YAML is too big/
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
2019-04-03 05:50:54 -04:00
|
|
|
|
|
|
|
context 'when ports have been set' do
|
|
|
|
context 'in the main image' do
|
|
|
|
let(:yml) do
|
|
|
|
<<-EOS
|
|
|
|
image:
|
|
|
|
name: ruby:2.2
|
|
|
|
ports:
|
|
|
|
- 80
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect(config.errors).to include("image config contains disallowed keys: ports")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'in the job image' do
|
|
|
|
let(:yml) do
|
|
|
|
<<-EOS
|
|
|
|
image: ruby:2.2
|
|
|
|
|
|
|
|
test:
|
|
|
|
script: rspec
|
|
|
|
image:
|
|
|
|
name: ruby:2.2
|
|
|
|
ports:
|
|
|
|
- 80
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect(config.errors).to include("jobs:test:image config contains disallowed keys: ports")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'in the services' do
|
|
|
|
let(:yml) do
|
|
|
|
<<-EOS
|
|
|
|
image: ruby:2.2
|
|
|
|
|
|
|
|
test:
|
|
|
|
script: rspec
|
|
|
|
image: ruby:2.2
|
|
|
|
services:
|
|
|
|
- name: test
|
|
|
|
alias: test
|
|
|
|
ports:
|
|
|
|
- 80
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect(config.errors).to include("jobs:test:services:service config contains disallowed keys: ports")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-03 08:20:34 -04:00
|
|
|
end
|
2018-09-07 14:53:28 -04:00
|
|
|
|
2018-09-08 08:55:36 -04:00
|
|
|
context "when using 'include' directive" do
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:remote_location) { 'https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
|
2018-09-08 15:43:42 -04:00
|
|
|
let(:local_location) { 'spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml' }
|
2018-09-08 08:55:36 -04:00
|
|
|
|
2018-09-07 16:37:37 -04:00
|
|
|
let(:remote_file_content) do
|
2018-09-07 15:18:26 -04:00
|
|
|
<<~HEREDOC
|
|
|
|
variables:
|
|
|
|
POSTGRES_USER: user
|
|
|
|
POSTGRES_PASSWORD: testing-password
|
|
|
|
POSTGRES_ENABLED: "true"
|
|
|
|
POSTGRES_DB: $CI_ENVIRONMENT_SLUG
|
|
|
|
HEREDOC
|
|
|
|
end
|
2018-09-08 08:55:36 -04:00
|
|
|
|
|
|
|
let(:local_file_content) do
|
|
|
|
File.read(Rails.root.join(local_location))
|
|
|
|
end
|
|
|
|
|
2018-09-07 16:37:37 -04:00
|
|
|
let(:gitlab_ci_yml) do
|
2018-09-08 08:55:36 -04:00
|
|
|
<<~HEREDOC
|
|
|
|
include:
|
|
|
|
- #{local_location}
|
|
|
|
- #{remote_location}
|
2018-09-07 14:53:28 -04:00
|
|
|
|
2018-09-08 08:55:36 -04:00
|
|
|
image: ruby:2.2
|
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:config) do
|
2018-12-01 06:39:13 -05:00
|
|
|
described_class.new(gitlab_ci_yml, project: project, sha: '12345', user: user)
|
2018-09-07 14:53:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2019-04-21 06:03:26 -04:00
|
|
|
stub_full_request(remote_location).to_return(body: remote_file_content)
|
2018-09-07 14:53:28 -04:00
|
|
|
|
2018-09-08 08:55:36 -04:00
|
|
|
allow(project.repository)
|
|
|
|
.to receive(:blob_data_at).and_return(local_file_content)
|
2018-09-07 14:53:28 -04:00
|
|
|
end
|
|
|
|
|
2018-09-08 08:55:36 -04:00
|
|
|
context "when gitlab_ci_yml has valid 'include' defined" do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'returns a composed hash' do
|
2018-09-08 08:55:36 -04:00
|
|
|
before_script_values = [
|
|
|
|
"apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs", "ruby -v",
|
|
|
|
"which ruby",
|
|
|
|
"bundle install --jobs $(nproc) \"${FLAGS[@]}\""
|
|
|
|
]
|
|
|
|
variables = {
|
|
|
|
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
|
2018-09-07 14:53:28 -04:00
|
|
|
end
|
|
|
|
|
2019-01-16 07:09:29 -05:00
|
|
|
context "when gitlab_ci.yml has invalid 'include' defined" do
|
2018-09-08 08:55:36 -04:00
|
|
|
let(:gitlab_ci_yml) do
|
|
|
|
<<~HEREDOC
|
|
|
|
include: invalid
|
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises error YamlProcessor validationError' do
|
|
|
|
expect { config }.to raise_error(
|
2018-10-18 05:06:32 -04:00
|
|
|
described_class::ConfigError,
|
2018-10-18 08:13:26 -04:00
|
|
|
"Included file `invalid` does not have YAML extension!"
|
2018-09-08 08:55:36 -04:00
|
|
|
)
|
|
|
|
end
|
2018-09-07 14:53:28 -04:00
|
|
|
end
|
2018-09-07 15:37:44 -04:00
|
|
|
|
2019-01-16 07:09:29 -05:00
|
|
|
context "when gitlab_ci.yml has ambigious 'include' defined" do
|
2019-01-02 06:51:15 -05:00
|
|
|
let(:gitlab_ci_yml) do
|
|
|
|
<<~HEREDOC
|
|
|
|
include:
|
|
|
|
remote: http://url
|
|
|
|
local: /local/file.yml
|
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises error YamlProcessor validationError' do
|
|
|
|
expect { config }.to raise_error(
|
|
|
|
described_class::ConfigError,
|
|
|
|
'Include `{"remote":"http://url","local":"/local/file.yml"}` needs to match exactly one accessor!'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-08 08:55:36 -04:00
|
|
|
describe 'external file version' do
|
|
|
|
context 'when external local file SHA is defined' do
|
|
|
|
it 'is using a defined value' do
|
|
|
|
expect(project.repository).to receive(:blob_data_at)
|
|
|
|
.with('eeff1122', local_location)
|
|
|
|
|
2018-12-01 06:39:13 -05:00
|
|
|
described_class.new(gitlab_ci_yml, project: project, sha: 'eeff1122', user: user)
|
2018-09-08 08:55:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when external local file SHA is not defined' do
|
|
|
|
it 'is using latest SHA on the default branch' do
|
|
|
|
expect(project.repository).to receive(:root_ref_sha)
|
|
|
|
|
2018-12-01 06:39:13 -05:00
|
|
|
described_class.new(gitlab_ci_yml, project: project, sha: nil, user: user)
|
2018-09-08 08:55:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when both external files and gitlab_ci.yml defined the same key" do
|
|
|
|
let(:gitlab_ci_yml) do
|
|
|
|
<<~HEREDOC
|
2018-09-07 15:37:44 -04:00
|
|
|
include:
|
2018-09-07 16:37:37 -04:00
|
|
|
- #{remote_location}
|
2018-09-07 15:37:44 -04:00
|
|
|
|
|
|
|
image: ruby:2.2
|
2018-09-08 08:55:36 -04:00
|
|
|
HEREDOC
|
|
|
|
end
|
2018-09-07 15:37:44 -04:00
|
|
|
|
2018-09-08 08:55:36 -04:00
|
|
|
let(:remote_file_content) do
|
|
|
|
<<~HEREDOC
|
2018-09-07 15:37:44 -04:00
|
|
|
image: php:5-fpm-alpine
|
2018-09-08 08:55:36 -04:00
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'takes precedence' do
|
2018-09-08 08:55:36 -04:00
|
|
|
expect(config.to_hash).to eq({ image: 'ruby:2.2' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when both external files and gitlab_ci.yml define a dictionary of distinct variables" do
|
|
|
|
let(:remote_file_content) do
|
|
|
|
<<~HEREDOC
|
|
|
|
variables:
|
|
|
|
A: 'alpha'
|
|
|
|
B: 'beta'
|
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:gitlab_ci_yml) do
|
|
|
|
<<~HEREDOC
|
|
|
|
include:
|
|
|
|
- #{remote_location}
|
|
|
|
|
|
|
|
variables:
|
|
|
|
C: 'gamma'
|
|
|
|
D: 'delta'
|
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'merges the variables dictionaries' do
|
2018-09-08 08:55:36 -04:00
|
|
|
expect(config.to_hash).to eq({ variables: { A: 'alpha', B: 'beta', C: 'gamma', D: 'delta' } })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when both external files and gitlab_ci.yml define a dictionary of overlapping variables" do
|
|
|
|
let(:remote_file_content) do
|
|
|
|
<<~HEREDOC
|
|
|
|
variables:
|
|
|
|
A: 'alpha'
|
|
|
|
B: 'beta'
|
|
|
|
C: 'omnicron'
|
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:gitlab_ci_yml) do
|
|
|
|
<<~HEREDOC
|
|
|
|
include:
|
|
|
|
- #{remote_location}
|
|
|
|
|
|
|
|
variables:
|
|
|
|
C: 'gamma'
|
|
|
|
D: 'delta'
|
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'later declarations should take precedence' do
|
|
|
|
expect(config.to_hash).to eq({ variables: { A: 'alpha', B: 'beta', C: 'gamma', D: 'delta' } })
|
|
|
|
end
|
2018-09-07 15:37:44 -04:00
|
|
|
end
|
|
|
|
|
2018-09-08 08:55:36 -04:00
|
|
|
context 'when both external files and gitlab_ci.yml define a job' do
|
|
|
|
let(:remote_file_content) do
|
|
|
|
<<~HEREDOC
|
|
|
|
job1:
|
|
|
|
script:
|
|
|
|
- echo 'hello from remote file'
|
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:gitlab_ci_yml) do
|
|
|
|
<<~HEREDOC
|
|
|
|
include:
|
|
|
|
- #{remote_location}
|
|
|
|
|
|
|
|
job1:
|
|
|
|
variables:
|
|
|
|
VARIABLE_DEFINED_IN_MAIN_FILE: 'some value'
|
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'merges the jobs' do
|
|
|
|
expect(config.to_hash).to eq({
|
|
|
|
job1: {
|
|
|
|
script: ["echo 'hello from remote file'"],
|
|
|
|
variables: {
|
|
|
|
VARIABLE_DEFINED_IN_MAIN_FILE: 'some value'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the script key is in both' do
|
|
|
|
let(:gitlab_ci_yml) do
|
|
|
|
<<~HEREDOC
|
|
|
|
include:
|
|
|
|
- #{remote_location}
|
|
|
|
|
|
|
|
job1:
|
|
|
|
script:
|
|
|
|
- echo 'hello from main file'
|
|
|
|
variables:
|
|
|
|
VARIABLE_DEFINED_IN_MAIN_FILE: 'some value'
|
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the script from the gitlab_ci.yml' do
|
|
|
|
expect(config.to_hash).to eq({
|
|
|
|
job1: {
|
|
|
|
script: ["echo 'hello from main file'"],
|
|
|
|
variables: {
|
|
|
|
VARIABLE_DEFINED_IN_MAIN_FILE: 'some value'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2018-09-07 15:37:44 -04:00
|
|
|
end
|
|
|
|
end
|
2016-06-03 08:20:34 -04:00
|
|
|
end
|