Commit 2b1c08be authored by Grzegorz Bizon's avatar Grzegorz Bizon

Validate job-level variables in YAML config file

parent a2957291
...@@ -153,6 +153,7 @@ module Ci ...@@ -153,6 +153,7 @@ module Ci
validate_job_types!(name, job) validate_job_types!(name, job)
validate_job_stage!(name, job) if job[:stage] validate_job_stage!(name, job) if job[:stage]
validate_job_variables!(name, job) if job[:variables]
validate_job_cache!(name, job) if job[:cache] validate_job_cache!(name, job) if job[:cache]
validate_job_artifacts!(name, job) if job[:artifacts] validate_job_artifacts!(name, job) if job[:artifacts]
validate_job_dependencies!(name, job) if job[:dependencies] validate_job_dependencies!(name, job) if job[:dependencies]
...@@ -214,6 +215,13 @@ module Ci ...@@ -214,6 +215,13 @@ module Ci
end end
end end
def validate_job_variables!(name, job)
if job[:variables] && !validate_variables(job[:variables])
raise ValidationError,
"#{name} job: variables should be a map of key-valued strings"
end
end
def validate_job_cache!(name, job) def validate_job_cache!(name, job)
if job[:cache][:key] && !validate_string(job[:cache][:key]) if job[:cache][:key] && !validate_string(job[:cache][:key])
raise ValidationError, "#{name} job: cache:key parameter should be a string" raise ValidationError, "#{name} job: cache:key parameter should be a string"
......
...@@ -366,22 +366,41 @@ module Ci ...@@ -366,22 +366,41 @@ module Ci
end end
context 'when job variables are defined' do context 'when job variables are defined' do
it 'returns job variables' do context 'when syntax is correct' do
variables = { it 'returns job variables' do
KEY1: 'value1', variables = {
SOME_KEY_2: 'value2' KEY1: 'value1',
} SOME_KEY_2: 'value2'
}
config = YAML.dump( config = YAML.dump(
{ before_script: ['pwd'], { before_script: ['pwd'],
rspec: { rspec: {
variables: variables, variables: variables,
script: 'rspec' } script: 'rspec' }
}) })
config_processor = GitlabCiYamlProcessor.new(config, path) config_processor = GitlabCiYamlProcessor.new(config, path)
expect(config_processor.job_variables(:rspec)).to eq variables
end
end
expect(config_processor.job_variables(:rspec)).to eq variables context 'when syntax is incorrect' do
it 'raises error' do
variables = [:KEY1, 'value1', :KEY2, 'value2']
config = YAML.dump(
{ before_script: ['pwd'],
rspec: {
variables: variables,
script: 'rspec' }
})
expect { GitlabCiYamlProcessor.new(config, path) }
.to raise_error(GitlabCiYamlProcessor::ValidationError,
/job: variables should be a map/)
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