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,6 +366,7 @@ module Ci ...@@ -366,6 +366,7 @@ module Ci
end end
context 'when job variables are defined' do context 'when job variables are defined' do
context 'when syntax is correct' do
it 'returns job variables' do it 'returns job variables' do
variables = { variables = {
KEY1: 'value1', KEY1: 'value1',
...@@ -385,6 +386,24 @@ module Ci ...@@ -385,6 +386,24 @@ module Ci
end end
end end
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
context 'when job variables are not defined' do context 'when job variables are not defined' do
it 'returns empty array' do it 'returns empty array' do
config = YAML.dump({ config = YAML.dump({
......
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