diff options
author | Tomasz Maczukin <tomasz@maczukin.pl> | 2017-01-14 14:39:53 -0500 |
---|---|---|
committer | Tomasz Maczukin <tomasz@maczukin.pl> | 2017-01-20 15:26:54 +0100 |
commit | eb6aec51ca73319d0714384b28951f32c3096efc (patch) | |
tree | 7c37082dbe2ff62ddd30e96e82d7a0f61994c5b9 | |
parent | bf8e174f0ae21b320c17b5a8f8d45aefcfef9520 (diff) | |
download | gitlab-ce-feature/add-support-for-services-configuration.tar.gz |
Add support for services configuration in .gitlab-ci.ymlfeature/add-support-for-services-configuration
-rw-r--r-- | lib/gitlab/ci/config/entry/service.rb | 56 | ||||
-rw-r--r-- | lib/gitlab/ci/config/entry/services.rb | 25 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/service_spec.rb | 101 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/services_spec.rb | 41 |
4 files changed, 198 insertions, 25 deletions
diff --git a/lib/gitlab/ci/config/entry/service.rb b/lib/gitlab/ci/config/entry/service.rb new file mode 100644 index 00000000000..f3b7e602f99 --- /dev/null +++ b/lib/gitlab/ci/config/entry/service.rb @@ -0,0 +1,56 @@ +module Gitlab + module Ci + class Config + module Entry + ## + # Entry that represents a configuration of Docker service. + # + class Service < Node + include Validatable + + ALLOWED_KEYS = %i[image alias command] + + validations do + validate do + unless hash? || string? + errors.add(:config, 'should be a hash or a string') + end + end + + validates :image, type: String, presence: true + validates :alias, type: String, allow_nil: true + validates :command, type: String, allow_nil: true + end + + def hash? + @config.is_a?(Hash) + end + + def string? + @config.is_a?(String) + end + + def image + value[:image] + end + + def alias + value[:alias] + end + + def command + value[:command] + end + + def value + case @config + when String then { image: @config } + when Hash then @config + else {} + end + end + end + end + end + end +end diff --git a/lib/gitlab/ci/config/entry/services.rb b/lib/gitlab/ci/config/entry/services.rb index 84f8ab780f5..0066894e069 100644 --- a/lib/gitlab/ci/config/entry/services.rb +++ b/lib/gitlab/ci/config/entry/services.rb @@ -9,7 +9,30 @@ module Gitlab include Validatable validations do - validates :config, array_of_strings: true + validates :config, type: Array + end + + def compose!(deps = nil) + super do + @entries = [] + @config.each do |config| + @entries << Entry::Factory.new(Entry::Service) + .value(config || {}) + .create! + end + + @entries.each do |entry| + entry.compose!(deps) + end + end + end + + def value + @entries.map(&:value) + end + + def descendants + @entries end end end diff --git a/spec/lib/gitlab/ci/config/entry/service_spec.rb b/spec/lib/gitlab/ci/config/entry/service_spec.rb new file mode 100644 index 00000000000..e4092bddd0d --- /dev/null +++ b/spec/lib/gitlab/ci/config/entry/service_spec.rb @@ -0,0 +1,101 @@ +require 'spec_helper' + +describe Gitlab::Ci::Config::Entry::Service do + let(:entry) { described_class.new(config) } + + before { entry.compose! } + + context 'when configuration is a string' do + let(:config) { 'postgresql:9.5' } + + describe '#string?' do + it 'is string configuration' do + expect(entry).to be_string + end + end + + describe '#hash?' do + it 'is not hash configuration' do + expect(entry).not_to be_hash + end + end + + describe '#valid?' do + it 'is valid' do + expect(entry).to be_valid + end + end + + describe '#value' do + it 'returns valid hash' do + expect(entry.value).to include(image: 'postgresql:9.5') + end + end + + describe '#image' do + it 'returns service image' do + expect(entry.image).to eq 'postgresql:9.5' + end + end + + describe '#alias' do + it 'returns service alias' do + expect(entry.alias).to be_nil + end + end + + describe '#command' do + it 'returns service command' do + expect(entry.command).to be_nil + end + end + end + + context 'when configuration is a hash' do + let(:config) do + { image: 'postgresql:9.5', alias: 'db', command: 'cmd' } + end + + describe '#string?' do + it 'is not string configuration' do + expect(entry).not_to be_string + end + end + + describe '#hash?' do + it 'is hash configuration' do + expect(entry).to be_hash + end + end + + describe '#valid?' do + it 'is valid' do + expect(entry).to be_valid + end + end + + describe '#value' do + it 'returns valid hash' do + expect(entry.value).to eq config + end + end + + describe '#image' do + it 'returns service image' do + expect(entry.image).to eq 'postgresql:9.5' + end + end + + describe '#alias' do + it 'returns service alias' do + expect(entry.alias).to eq 'db' + end + end + + describe '#command' do + it 'returns service command' do + expect(entry.command).to eq 'cmd' + end + end + end +end diff --git a/spec/lib/gitlab/ci/config/entry/services_spec.rb b/spec/lib/gitlab/ci/config/entry/services_spec.rb index 66fad3b6b16..14986cd4627 100644 --- a/spec/lib/gitlab/ci/config/entry/services_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/services_spec.rb @@ -3,37 +3,30 @@ require 'spec_helper' describe Gitlab::Ci::Config::Entry::Services do let(:entry) { described_class.new(config) } - describe 'validations' do - context 'when entry config value is correct' do - let(:config) { ['postgres:9.1', 'mysql:5.5'] } + before { entry.compose! } - describe '#value' do - it 'returns array of services as is' do - expect(entry.value).to eq config - end - end + context 'when configuration is valid' do + let(:config) { [ 'postgresql:9.5', { image: 'postgresql:9.1', alias: 'postgres_old' } ] } - describe '#valid?' do - it 'is valid' do - expect(entry).to be_valid - end + describe '#valid?' do + it 'is valid' do + expect(entry).to be_valid end end - context 'when entry value is not correct' do - let(:config) { 'ls' } - - describe '#errors' do - it 'saves errors' do - expect(entry.errors) - .to include 'services config should be an array of strings' - end + describe '#value' do + it 'returns valid array' do + expect(entry.value).to eq([{ image: 'postgresql:9.5' }, { image: 'postgresql:9.1', alias: 'postgres_old' }]) end + end + end + + context 'when configuration is invalid' do + let(:config) { 'postgresql:9.5' } - describe '#valid?' do - it 'is not valid' do - expect(entry).not_to be_valid - end + describe '#valid?' do + it 'is invalid' do + expect(entry).not_to be_valid end end end |