Commit 95520dfc authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add prototype of CI config node validator

This makes use of `ActiveModel::Validations` encapsulated in a separate
class that is accessible from a node object.
parent 76aea978
......@@ -17,11 +17,11 @@ module Gitlab
end
def valid?
errors.none?
@global.valid?
end
def errors
@global.errors.map(&:to_s)
@global.errors
end
def to_hash
......
......@@ -16,21 +16,27 @@ module Gitlab
module Configurable
extend ActiveSupport::Concern
included do
validations do
validate :hash_config_value
def hash_config_value
unless self.config.is_a?(Hash)
errors.add(:config, 'should be a configuration entry hash')
end
end
end
end
def allowed_nodes
self.class.allowed_nodes || {}
end
private
def prevalidate!
unless @value.is_a?(Hash)
add_error('should be a configuration entry with hash value')
end
end
def create_node(key, factory)
factory.with(value: @value[key], key: key)
factory.nullify! unless @value.has_key?(key)
factory.with(value: @config[key], key: key)
factory.nullify! unless @config.has_key?(key)
factory.create!
end
......@@ -47,7 +53,6 @@ module Gitlab
define_method(symbol) do
raise Entry::InvalidError unless valid?
@nodes[symbol].try(:value)
end
......
......@@ -7,16 +7,15 @@ module Gitlab
#
class Entry
class InvalidError < StandardError; end
include Validatable
attr_writer :key
attr_accessor :description
attr_reader :config
attr_accessor :key, :description
def initialize(value)
@value = value
def initialize(config)
@config = config
@nodes = {}
@errors = []
prevalidate!
@validator = self.class.validator.new(self)
end
def process!
......@@ -24,19 +23,13 @@ module Gitlab
return unless valid?
compose!
nodes.each(&:process!)
nodes.each(&:validate!)
process_nodes!
end
def nodes
@nodes.values
end
def valid?
errors.none?
end
def leaf?
allowed_nodes.none?
end
......@@ -45,37 +38,35 @@ module Gitlab
@key || self.class.name.demodulize.underscore
end
def errors
@errors + nodes.map(&:errors).flatten
def valid?
errors.none?
end
def add_error(message)
@errors << Error.new(message, self)
def errors
@validator.full_errors +
nodes.map(&:errors).flatten
end
def allowed_nodes
{}
end
def validate!
raise NotImplementedError
end
def value
raise NotImplementedError
end
private
def prevalidate!
end
def compose!
allowed_nodes.each do |key, essence|
@nodes[key] = create_node(key, essence)
end
end
def process_nodes!
nodes.each(&:process!)
end
def create_node(key, essence)
raise NotImplementedError
end
......
......@@ -11,17 +11,21 @@ module Gitlab
# implementation in Runner.
#
class Script < Entry
validations do
include ValidationHelpers
def value
@value.join("\n")
end
validate :array_of_strings
def validate!
unless validate_array_of_strings(@value)
add_error('should be an array of strings')
def array_of_strings
unless validate_array_of_strings(self.config)
errors.add(:config, 'should be an array of strings')
end
end
end
def value
@config.join("\n")
end
end
end
end
......
module Gitlab
module Ci
class Config
module Node
module Validatable
extend ActiveSupport::Concern
class_methods do
def validator
validator = Class.new(Node::Validator)
validator.include(ActiveModel::Validations)
if defined?(@validations)
@validations.each { |rules| validator.class_eval(&rules) }
end
validator
end
private
def validations(&block)
(@validations ||= []).append(block)
end
end
end
end
end
end
end
......@@ -2,22 +2,21 @@ module Gitlab
module Ci
class Config
module Node
class Error
def initialize(message, parent)
@message = message
@parent = parent
class Validator < SimpleDelegator
def initialize(node)
@node = node
super(node)
validate
end
def key
@parent.key
def full_errors
errors.full_messages.map do |error|
"#{@node.key} #{error}".humanize
end
def to_s
"#{key}: #{@message}"
end
def ==(other)
other.to_s == to_s
def self.name
'Validator'
end
end
end
......
......@@ -820,7 +820,7 @@ EOT
config = YAML.dump({ before_script: "bundle update", rspec: { script: "test" } })
expect do
GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "before_script: should be an array of strings")
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Before script config should be an array of strings")
end
it "returns errors if job before_script parameter is not an array of strings" do
......
......@@ -4,6 +4,7 @@ describe Gitlab::Ci::Config::Node::Configurable do
let(:node) { Class.new }
before do
node.include(Gitlab::Ci::Config::Node::Validatable)
node.include(described_class)
end
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Error do
let(:error) { described_class.new(message, parent) }
let(:message) { 'some error' }
let(:parent) { spy('parent') }
before do
allow(parent).to receive(:key).and_return('parent_key')
end
describe '#key' do
it 'returns underscored class name' do
expect(error.key).to eq 'parent_key'
end
end
describe '#to_s' do
it 'returns valid error message' do
expect(error.to_s).to eq 'parent_key: some error'
end
end
end
......@@ -85,7 +85,7 @@ describe Gitlab::Ci::Config::Node::Global do
describe '#errors' do
it 'reports errors from child nodes' do
expect(global.errors)
.to include 'before_script: should be an array of strings'
.to include 'Before script config should be an array of strings'
end
end
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Script do
let(:entry) { described_class.new(value) }
let(:entry) { described_class.new(config) }
describe '#validate!' do
before { entry.validate! }
describe '#process!' do
before { entry.process! }
context 'when entry value is correct' do
let(:value) { ['ls', 'pwd'] }
context 'when entry config value is correct' do
let(:config) { ['ls', 'pwd'] }
describe '#value' do
it 'returns concatenated command' do
......@@ -29,12 +29,12 @@ describe Gitlab::Ci::Config::Node::Script do
end
context 'when entry value is not correct' do
let(:value) { 'ls' }
let(:config) { 'ls' }
describe '#errors' do
it 'saves errors' do
expect(entry.errors)
.to include 'script: should be an array of strings'
.to include 'Script config should be an array of strings'
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