From cd6a2afbbb508e96f67c1f192cee2c1d379b1749 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 21 Jun 2016 12:10:13 +0200 Subject: [PATCH] Move CI image configuration entry to new CI config --- lib/ci/gitlab_ci_yaml_processor.rb | 20 ++++++------- lib/gitlab/ci/config.rb | 2 +- lib/gitlab/ci/config/node/global.rb | 3 ++ spec/lib/ci/gitlab_ci_yaml_processor_spec.rb | 2 +- spec/lib/gitlab/ci/config/node/global_spec.rb | 28 +++++++++++++------ 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index c52d4d6338..f4ef449c84 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, :image, :services, :path, :cache + attr_reader :after_script, :services, :path, :cache def initialize(config, path = nil) @ci_config = Gitlab::Ci::Config.new(config) @@ -22,8 +22,11 @@ module Ci @path = path - initial_parsing + unless @ci_config.valid? + raise ValidationError, @ci_config.errors.first + end + initial_parsing validate! rescue Gitlab::Ci::Config::Loader::FormatError => e raise ValidationError, e.message @@ -60,6 +63,9 @@ module Ci private def initial_parsing + @before_script = @ci_config.before_script + @image = @ci_config.image + @after_script = @config[:after_script] @image = @config[:image] @services = @config[:services] @@ -87,7 +93,7 @@ module Ci { stage_idx: stages.index(job[:stage]), stage: job[:stage], - commands: [job[:before_script] || [@ci_config.before_script], job[:script]].flatten.compact.join("\n"), + commands: [job[:before_script] || [@before_script], job[:script]].flatten.compact.join("\n"), tag_list: job[:tags] || [], name: name, only: job[:only], @@ -107,10 +113,6 @@ module Ci end def validate! - unless @ci_config.valid? - raise ValidationError, @ci_config.errors.first - end - validate_global! @jobs.each do |name, job| @@ -125,10 +127,6 @@ module Ci raise ValidationError, "after_script should be an array of strings" end - unless @image.nil? || @image.is_a?(String) - raise ValidationError, "image should be a string" - end - unless @services.nil? || validate_array_of_strings(@services) raise ValidationError, "services should be an array of strings" end diff --git a/lib/gitlab/ci/config.rb b/lib/gitlab/ci/config.rb index adfd097736..d02902a110 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, to: :@global + delegate :before_script, :image, 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 044603423d..fa5f75beb9 100644 --- a/lib/gitlab/ci/config/node/global.rb +++ b/lib/gitlab/ci/config/node/global.rb @@ -11,6 +11,9 @@ module Gitlab allow_node :before_script, Script, description: 'Script that will be executed before each job.' + + allow_node :image, Image, + description: 'Docker image that will be used to execute jobs.' 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 200ca6aeee..4c7070ad05 100644 --- a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb +++ b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb @@ -979,7 +979,7 @@ EOT config = YAML.dump({ image: ["test"], rspec: { script: "test" } }) expect do GitlabCiYamlProcessor.new(config, path) - end.to raise_error(GitlabCiYamlProcessor::ValidationError, "image should be a string") + end.to raise_error(GitlabCiYamlProcessor::ValidationError, "Image config should be a string") end it "returns errors if job name is blank" do diff --git a/spec/lib/gitlab/ci/config/node/global_spec.rb b/spec/lib/gitlab/ci/config/node/global_spec.rb index fc9257d89d..ae911d81c4 100644 --- a/spec/lib/gitlab/ci/config/node/global_spec.rb +++ b/spec/lib/gitlab/ci/config/node/global_spec.rb @@ -21,7 +21,8 @@ describe Gitlab::Ci::Config::Node::Global do context 'when hash is valid' do let(:hash) do - { before_script: ['ls', 'pwd'] } + { before_script: ['ls', 'pwd'], + image: 'ruby:2.2' } end describe '#process!' do @@ -32,17 +33,21 @@ describe Gitlab::Ci::Config::Node::Global do end it 'creates node object for each entry' do - expect(global.nodes.count).to eq 1 + expect(global.nodes.count).to eq 2 end it 'creates node object using valid class' do expect(global.nodes.first) .to be_an_instance_of Gitlab::Ci::Config::Node::Script + expect(global.nodes.second) + .to be_an_instance_of Gitlab::Ci::Config::Node::Image end it 'sets correct description for nodes' do expect(global.nodes.first.description) .to eq 'Script that will be executed before each job.' + expect(global.nodes.second.description) + .to eq 'Docker image that will be used to execute jobs.' end end @@ -51,19 +56,26 @@ 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 + expect(global.before_script).to be nil + end + end + end - describe '#before_script' do - context 'when processed' do - before { global.process! } + context 'when processed' do + before { global.process! } + describe '#before_script' do it 'returns correct script' do expect(global.before_script).to eq "ls\npwd" end end - context 'when not processed' do - it 'returns nil' do - expect(global.before_script).to be nil + describe '#image' do + it 'returns valid image' do + expect(global.image).to eq 'ruby:2.2' end end end