Commit 56e88b8c authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add new ci config entry that handles cache config

parent ce4478ed
module Gitlab
module Ci
class Config
module Node
##
# Entry that represents a cache configuration
#
class Cache < Entry
include Configurable
validations do
validate :allowed_keys
def unknown_keys
return [] unless @node.config.is_a?(Hash)
@node.config.keys - @node.class.nodes.keys
end
def allowed_keys
if unknown_keys.any?
errors.add(:config, "contains unknown keys #{unknown_keys}")
end
end
end
node :key, Node::Key,
description: 'Cache key used to define a cache affinity.'
node :untracked, Boolean,
description: 'Cache all untracked files.'
node :paths, Paths,
description: 'Specify which paths should be cached across builds.'
end
end
end
end
end
...@@ -32,12 +32,20 @@ module Gitlab ...@@ -32,12 +32,20 @@ module Gitlab
class_methods do class_methods do
def nodes def nodes
Hash[@nodes.map { |key, factory| [key, factory.dup] }] Hash[(@nodes || {}).map { |key, factory| [key, factory.dup] }]
end end
private private
def node(symbol, entry_class, metadata) def node(symbol, entry_class, metadata)
factory = Node::Factory.new(entry_class)
.with(description: metadata[:description])
(@nodes ||= {}).merge!(symbol.to_sym => factory)
end
def helpers(*nodes)
nodes.each do |symbol|
define_method("#{symbol}_defined?") do define_method("#{symbol}_defined?") do
@nodes[symbol].try(:defined?) @nodes[symbol].try(:defined?)
end end
...@@ -48,11 +56,7 @@ module Gitlab ...@@ -48,11 +56,7 @@ module Gitlab
end end
alias_method symbol.to_sym, "#{symbol}_value".to_sym alias_method symbol.to_sym, "#{symbol}_value".to_sym
end
factory = Node::Factory.new(entry_class)
.with(description: metadata[:description])
(@nodes ||= {}).merge!(symbol.to_sym => factory)
end end
end end
end end
......
...@@ -30,6 +30,9 @@ module Gitlab ...@@ -30,6 +30,9 @@ module Gitlab
node :types, Stages, node :types, Stages,
description: 'Stages for this pipeline (deprecated key).' description: 'Stages for this pipeline (deprecated key).'
helpers :before_script, :image, :services, :after_script, :variables,
:stages, :types
def stages def stages
stages_defined? ? stages_value : types_value stages_defined? ? stages_value : types_value
end end
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Cache do
let(:entry) { described_class.new(config) }
describe 'validations' do
before { entry.process! }
context 'when entry config value is correct' do
let(:config) do
{ key: 'some key',
untracked: true,
paths: ['some/path/'] }
end
describe '#value' do
it 'returns hash value' do
expect(entry.value).to eq config
end
end
describe '#valid?' do
it 'is valid' do
expect(entry).to be_valid
end
end
end
context 'when entry value is not correct' do
describe '#errors' do
context 'when is not a hash' do
let(:config) { 'ls' }
it 'reports errors with config value' do
expect(entry.errors)
.to include 'Cache config should be a hash'
end
end
context 'when descendants are invalid' do
let(:config) { { key: 1 } }
it 'reports error with descendants' do
expect(entry.errors)
.to include 'Key config should be a string or symbol'
end
end
context 'when there is an unknown key present' do
let(:config) { { invalid: true } }
it 'reports error with descendants' do
expect(entry.errors)
.to include 'Cache config contains unknown keys [:invalid]'
end
end
end
end
end
end
...@@ -7,6 +7,39 @@ describe Gitlab::Ci::Config::Node::Configurable do ...@@ -7,6 +7,39 @@ describe Gitlab::Ci::Config::Node::Configurable do
node.include(described_class) node.include(described_class)
end end
describe 'validations' do
let(:validator) { node.validator.new(instance) }
before do
node.class_eval do
attr_reader :config
def initialize(config)
@config = config
end
end
validator.validate
end
context 'when node validator is invalid' do
let(:instance) { node.new('ls') }
it 'returns invalid validator' do
expect(validator).to be_invalid
end
end
context 'when node instance is valid' do
let(:instance) { node.new(key: 'value') }
it 'returns valid validator' do
expect(validator).to be_valid
end
end
end
describe 'configured nodes' do describe 'configured nodes' do
before do before do
node.class_eval do node.class_eval do
......
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