diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index f4ef449c84c77a0960d55a7947dec9ad84a0112d..2cb46448e76231a4a8010cc55050bd3d175d2eab 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -14,7 +14,7 @@ module Ci ALLOWED_CACHE_KEYS = [:key, :untracked, :paths] ALLOWED_ARTIFACTS_KEYS = [:name, :untracked, :paths, :when, :expire_in] - attr_reader :after_script, :services, :path, :cache + attr_reader :after_script, :path, :cache def initialize(config, path = nil) @ci_config = Gitlab::Ci::Config.new(config) @@ -68,7 +68,7 @@ module Ci @after_script = @config[:after_script] @image = @config[:image] - @services = @config[:services] + @services = @ci_config.services @stages = @config[:stages] || @config[:types] @variables = @config[:variables] || {} @cache = @config[:cache] @@ -127,10 +127,6 @@ module Ci raise ValidationError, "after_script should be an array of strings" end - unless @services.nil? || validate_array_of_strings(@services) - raise ValidationError, "services should be an array of strings" - end - unless @stages.nil? || validate_array_of_strings(@stages) raise ValidationError, "stages should be an array of strings" end diff --git a/lib/gitlab/ci/config.rb b/lib/gitlab/ci/config.rb index d02902a110a30aac18dddb839af42df3dcdaaf47..fb93d36c48f4acacaaa058756a539b61262099f5 100644 --- a/lib/gitlab/ci/config.rb +++ b/lib/gitlab/ci/config.rb @@ -7,7 +7,7 @@ module Gitlab ## # Temporary delegations that should be removed after refactoring # - delegate :before_script, :image, to: :@global + delegate :before_script, :image, :services, to: :@global def initialize(config) @config = Loader.new(config).load! diff --git a/lib/gitlab/ci/config/node/global.rb b/lib/gitlab/ci/config/node/global.rb index fa5f75beb9fcd079bd8b818b61d31ef258a8e50f..03ed7808d2fe418195c8d07c0dde814ac5c9bf72 100644 --- a/lib/gitlab/ci/config/node/global.rb +++ b/lib/gitlab/ci/config/node/global.rb @@ -14,6 +14,9 @@ module Gitlab allow_node :image, Image, description: 'Docker image that will be used to execute jobs.' + + allow_node :services, Services, + description: 'Docker images that will be linked to the container.' end end end diff --git a/lib/gitlab/ci/config/node/services.rb b/lib/gitlab/ci/config/node/services.rb new file mode 100644 index 0000000000000000000000000000000000000000..d9898d9a4a1a8224e52f29b372b94b03cbf535da --- /dev/null +++ b/lib/gitlab/ci/config/node/services.rb @@ -0,0 +1,22 @@ +module Gitlab + module Ci + class Config + module Node + ## + # Entry that represents a configuration of Docker services. + # + class Services < Entry + include Validatable + + validations do + validates :config, array_of_strings: true + end + + def value + @config + end + end + end + end + end +end diff --git a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb index 4c7070ad058da0e7d4ff0835b24be0a8209f1915..bed174e2495d1868a8b7258ca0b40ec171743527 100644 --- a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb +++ b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb @@ -1007,14 +1007,14 @@ EOT config = YAML.dump({ services: "test", rspec: { script: "test" } }) expect do GitlabCiYamlProcessor.new(config, path) - end.to raise_error(GitlabCiYamlProcessor::ValidationError, "services should be an array of strings") + end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Services config should be an array of strings") end it "returns errors if services parameter is not an array of strings" do config = YAML.dump({ services: [10, "test"], rspec: { script: "test" } }) expect do GitlabCiYamlProcessor.new(config, path) - end.to raise_error(GitlabCiYamlProcessor::ValidationError, "services should be an array of strings") + end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Services config should be an array of strings") end it "returns errors if job services parameter is not an array" do diff --git a/spec/lib/gitlab/ci/config/node/global_spec.rb b/spec/lib/gitlab/ci/config/node/global_spec.rb index ae911d81c498fcb1cf0d6468c257f743fab664df..7c8ddffc086046eff9136cb1ef7aa131134491f9 100644 --- a/spec/lib/gitlab/ci/config/node/global_spec.rb +++ b/spec/lib/gitlab/ci/config/node/global_spec.rb @@ -22,7 +22,8 @@ describe Gitlab::Ci::Config::Node::Global do context 'when hash is valid' do let(:hash) do { before_script: ['ls', 'pwd'], - image: 'ruby:2.2' } + image: 'ruby:2.2', + services: ['postgres:9.1', 'mysql:5.5'] } end describe '#process!' do @@ -33,7 +34,7 @@ describe Gitlab::Ci::Config::Node::Global do end it 'creates node object for each entry' do - expect(global.nodes.count).to eq 2 + expect(global.nodes.count).to eq 3 end it 'creates node object using valid class' do @@ -56,6 +57,7 @@ describe Gitlab::Ci::Config::Node::Global do expect(global).not_to be_leaf end end + context 'when not processed' do describe '#before_script' do it 'returns nil' do @@ -78,6 +80,12 @@ describe Gitlab::Ci::Config::Node::Global do expect(global.image).to eq 'ruby:2.2' end end + + describe '#services' do + it 'returns array of services' do + expect(global.services).to eq ['postgres:9.1', 'mysql:5.5'] + end + end end end diff --git a/spec/lib/gitlab/ci/config/node/services_spec.rb b/spec/lib/gitlab/ci/config/node/services_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..bda4b976cb995aab7384f5c0ff080758b467dcae --- /dev/null +++ b/spec/lib/gitlab/ci/config/node/services_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe Gitlab::Ci::Config::Node::Services do + let(:entry) { described_class.new(config) } + + describe '#process!' do + before { entry.process! } + + context 'when entry config value is correct' do + let(:config) { ['postgres:9.1', 'mysql:5.5'] } + + describe '#value' do + it 'returns array of services as is' 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 + let(:config) { 'ls' } + + describe '#errors' do + it 'saves errors' do + expect(entry.errors) + .to include 'Services config should be an array of strings' + end + end + + describe '#valid?' do + it 'is not valid' do + expect(entry).not_to be_valid + end + end + end + end +end