Commit 4491bf28 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Move CI job config validations to new classes

parent 580c4e18
...@@ -71,8 +71,6 @@ module Ci ...@@ -71,8 +71,6 @@ module Ci
@ci_config.jobs.each do |name, param| @ci_config.jobs.each do |name, param|
add_job(name, param) add_job(name, param)
end end
raise ValidationError, "Please define at least one job" if @jobs.none?
end end
def add_job(name, job) def add_job(name, job)
......
...@@ -24,6 +24,7 @@ module Gitlab ...@@ -24,6 +24,7 @@ module Gitlab
compose! compose!
process_nodes! process_nodes!
@validator.validate(:processed)
end end
def leaf? def leaf?
......
...@@ -10,12 +10,27 @@ module Gitlab ...@@ -10,12 +10,27 @@ module Gitlab
validations do validations do
validates :config, type: Hash validates :config, type: Hash
validate :jobs_presence, on: :processed
def jobs_presence
unless relevant?
errors.add(:config, 'should contain at least one visible job')
end
end
end end
def nodes def nodes
@config @config
end end
def relevant?
@nodes.values.any?(&:relevant?)
end
def leaf?
false
end
private private
def create_node(key, essence) def create_node(key, essence)
......
...@@ -1061,7 +1061,14 @@ EOT ...@@ -1061,7 +1061,14 @@ EOT
config = YAML.dump({ before_script: ["bundle update"] }) config = YAML.dump({ before_script: ["bundle update"] })
expect do expect do
GitlabCiYamlProcessor.new(config, path) GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Please define at least one job") end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs config should contain at least one visible job")
end
it "returns errors if there are no visible jobs defined" do
config = YAML.dump({ before_script: ["bundle update"], '.hidden'.to_sym => {} })
expect do
GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs config should contain at least one visible job")
end end
it "returns errors if job allow_failure parameter is not an boolean" do it "returns errors if job allow_failure parameter is not an boolean" do
......
...@@ -108,7 +108,10 @@ describe Gitlab::Ci::Config::Node::Global do ...@@ -108,7 +108,10 @@ describe Gitlab::Ci::Config::Node::Global do
end end
context 'when deprecated types key defined' do context 'when deprecated types key defined' do
let(:hash) { { types: ['test', 'deploy'] } } let(:hash) do
{ types: ['test', 'deploy'],
rspec: { script: 'rspec' } }
end
it 'returns array of types as stages' do it 'returns array of types as stages' do
expect(global.stages).to eq %w[test deploy] expect(global.stages).to eq %w[test deploy]
...@@ -174,7 +177,7 @@ describe Gitlab::Ci::Config::Node::Global do ...@@ -174,7 +177,7 @@ describe Gitlab::Ci::Config::Node::Global do
# details. # details.
# #
context 'when entires specified but not defined' do context 'when entires specified but not defined' do
let(:hash) { { variables: nil } } let(:hash) { { variables: nil, rspec: { script: 'rspec' } } }
before { global.process! } before { global.process! }
describe '#variables' do describe '#variables' do
......
...@@ -4,17 +4,9 @@ describe Gitlab::Ci::Config::Node::Jobs do ...@@ -4,17 +4,9 @@ describe Gitlab::Ci::Config::Node::Jobs do
let(:entry) { described_class.new(config) } let(:entry) { described_class.new(config) }
describe 'validations' do describe 'validations' do
before { entry.process! }
context 'when entry config value is correct' do context 'when entry config value is correct' do
let(:config) { { rspec: { script: 'rspec' } } } let(:config) { { rspec: { script: 'rspec' } } }
describe '#value' do
it 'returns key value' do
expect(entry.value).to eq(rspec: { script: 'rspec' })
end
end
describe '#valid?' do describe '#valid?' do
it 'is valid' do it 'is valid' do
expect(entry).to be_valid expect(entry).to be_valid
...@@ -23,15 +15,34 @@ describe Gitlab::Ci::Config::Node::Jobs do ...@@ -23,15 +15,34 @@ describe Gitlab::Ci::Config::Node::Jobs do
end end
context 'when entry value is not correct' do context 'when entry value is not correct' do
context 'incorrect config value type' do describe '#errors' do
let(:config) { ['incorrect'] } context 'incorrect config value type' do
let(:config) { ['incorrect'] }
describe '#errors' do it 'returns error about incorrect type' do
it 'saves errors' do
expect(entry.errors) expect(entry.errors)
.to include 'jobs config should be a hash' .to include 'jobs config should be a hash'
end end
end end
context 'when no visible jobs present' do
let(:config) { { '.hidden'.to_sym => {} } }
context 'when not processed' do
it 'is valid' do
expect(entry.errors).to be_empty
end
end
context 'when processed' do
before { entry.process! }
it 'returns error about no visible jobs defined' do
expect(entry.errors)
.to include 'jobs config should contain at least one visible job'
end
end
end
end end
end end
end end
...@@ -45,6 +56,13 @@ describe Gitlab::Ci::Config::Node::Jobs do ...@@ -45,6 +56,13 @@ describe Gitlab::Ci::Config::Node::Jobs do
'.hidden'.to_sym => {} } '.hidden'.to_sym => {} }
end end
describe '#value' do
it 'returns key value' do
expect(entry.value).to eq(rspec: { script: 'rspec' },
spinach: { script: 'spinach' })
end
end
describe '#descendants' do describe '#descendants' do
it 'creates valid descendant nodes' do it 'creates valid descendant nodes' do
expect(entry.descendants.count).to eq 3 expect(entry.descendants.count).to eq 3
......
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