Skip to content
Snippets Groups Projects
Commit 568715fc authored by Kamil Trzcinski's avatar Kamil Trzcinski
Browse files

Make the YAML validation more stricter

- deny all globals that are not hases and doesn't contain script, the script is required now
- verify that script is either string or array of an strings
parent aab30f1c
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -46,8 +46,9 @@ class GitlabCiYamlProcessor
@variables = @config[:variables] || {}
@config.except!(*ALLOWED_YAML_KEYS)
 
# anything that doesn't have script is considered as unknown
@config.each do |name, param|
raise ValidationError, "Unknown parameter: #{name}" unless param.is_a?(Hash)
raise ValidationError, "Unknown parameter: #{name}" unless param.is_a?(Hash) && param.has_key?(:script)
end
 
unless @config.values.any?{|job| job.is_a?(Hash)}
Loading
Loading
@@ -148,6 +149,10 @@ class GitlabCiYamlProcessor
end
end
 
if !job[:script].is_a?(String) && !validate_array_of_strings(job[:script])
raise ValidationError, "#{name}: script should be a string or an array of a strings"
end
if job[:stage]
unless job[:stage].is_a?(String) && job[:stage].in?(stages)
raise ValidationError, "#{name}: stage parameter should be #{stages.join(", ")}"
Loading
Loading
Loading
Loading
@@ -176,7 +176,7 @@ describe GitlabCiYamlProcessor do
end
 
it "returns errors if tags parameter is invalid" do
config = YAML.dump({rspec: {tags: "mysql"}})
config = YAML.dump({rspec: {script: "test", tags: "mysql"}})
expect do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: tags parameter should be an array of strings")
Loading
Loading
@@ -197,7 +197,7 @@ describe GitlabCiYamlProcessor do
end
 
it "returns errors if job image parameter is invalid" do
config = YAML.dump({rspec: {image: ["test"]}})
config = YAML.dump({rspec: {script: "test", image: ["test"]}})
expect do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: image should be a string")
Loading
Loading
@@ -218,14 +218,14 @@ describe GitlabCiYamlProcessor do
end
 
it "returns errors if job services parameter is not an array" do
config = YAML.dump({rspec: {services: "test"}})
config = YAML.dump({rspec: {script: "test", services: "test"}})
expect do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: services should be an array of strings")
end
 
it "returns errors if job services parameter is not an array of strings" do
config = YAML.dump({rspec: {services: [10, "test"]}})
config = YAML.dump({rspec: {script: "test", services: [10, "test"]}})
expect do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: services should be an array of strings")
Loading
Loading
@@ -238,6 +238,13 @@ describe GitlabCiYamlProcessor do
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Unknown parameter: extra")
end
 
it "returns errors if there are unknown parameters that are hashes, but doesn't have a script" do
config = YAML.dump({extra: {services: "test"}})
expect do
GitlabCiYamlProcessor.new(config)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Unknown parameter: extra")
end
it "returns errors if there is no any jobs defined" do
config = YAML.dump({before_script: ["bundle update"]})
expect do
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment