From 9b5a912f9377a77e52e9acdf7471acd27c709264 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 21 Mar 2018 13:08:30 +0100 Subject: Make it easier to test pipeline stage seeds --- app/models/ci/pipeline.rb | 3 +++ lib/gitlab/ci/pipeline/chain/create.rb | 10 ++++------ lib/gitlab/ci/pipeline/chain/populate.rb | 7 ++++--- lib/gitlab/ci/pipeline/chain/validate/config.rb | 2 +- lib/gitlab/ci/pipeline/seed/build.rb | 4 ++++ lib/gitlab/ci/pipeline/seed/stage.rb | 11 ++++------- spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb | 22 +++++++++++----------- spec/lib/gitlab/ci/yaml_processor_spec.rb | 16 ++++++++-------- 8 files changed, 39 insertions(+), 36 deletions(-) diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 6b38c2eff98..bac5b3860e4 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -381,6 +381,9 @@ module Ci end end + ## + # TODO, setting yaml_errors should be moved to the pipeline creation chain. + # def config_processor return unless ci_yaml_file return @config_processor if defined?(@config_processor) diff --git a/lib/gitlab/ci/pipeline/chain/create.rb b/lib/gitlab/ci/pipeline/chain/create.rb index 930d767b0f3..f4c8d5342c1 100644 --- a/lib/gitlab/ci/pipeline/chain/create.rb +++ b/lib/gitlab/ci/pipeline/chain/create.rb @@ -9,16 +9,14 @@ module Gitlab ::Ci::Pipeline.transaction do pipeline.save! - # TODO populate environments with find_or_initialize_by in the chain too. - ## - # Create the environment before the build starts. This sets its slug and - # makes it available as an environment variable + # Create environments before the pipeline starts. # pipeline.builds.each do |build| if build.has_environment? - environment_name = build.expanded_environment_name - project.environments.find_or_create_by(name: environment_name) + project.environments.find_or_create_by( + name: build.expanded_environment_name + ) end end end diff --git a/lib/gitlab/ci/pipeline/chain/populate.rb b/lib/gitlab/ci/pipeline/chain/populate.rb index 989cb8312ba..56d9a692a2b 100644 --- a/lib/gitlab/ci/pipeline/chain/populate.rb +++ b/lib/gitlab/ci/pipeline/chain/populate.rb @@ -9,12 +9,13 @@ module Gitlab def perform! ## - # Populate pipeline with seeds block. - # - # It comes from a block argument to CreatePipelineService#execute. + # Populate pipeline with block `CreatePipelineService#execute`. # @command.seeds_block&.call(pipeline) + ## + # Populate pipeline with all stages and builds. + # pipeline.stage_seeds.each do |seed| seed.user = current_user diff --git a/lib/gitlab/ci/pipeline/chain/validate/config.rb b/lib/gitlab/ci/pipeline/chain/validate/config.rb index 655a6d0b3e7..a3bd2a5a23a 100644 --- a/lib/gitlab/ci/pipeline/chain/validate/config.rb +++ b/lib/gitlab/ci/pipeline/chain/validate/config.rb @@ -16,7 +16,7 @@ module Gitlab @pipeline.drop!(:config_error) end - return error(@pipeline.yaml_errors) + error(@pipeline.yaml_errors) end end diff --git a/lib/gitlab/ci/pipeline/seed/build.rb b/lib/gitlab/ci/pipeline/seed/build.rb index f49dbb2f0ba..2c29e647fe6 100644 --- a/lib/gitlab/ci/pipeline/seed/build.rb +++ b/lib/gitlab/ci/pipeline/seed/build.rb @@ -3,6 +3,10 @@ module Gitlab module Pipeline module Seed class Build < Seed::Base + attr_reader :pipeline, :attributes + + delegate :dig, to: :attributes + def initialize(pipeline, attributes) @pipeline = pipeline @attributes = attributes diff --git a/lib/gitlab/ci/pipeline/seed/stage.rb b/lib/gitlab/ci/pipeline/seed/stage.rb index b431206fd2c..6d824e52334 100644 --- a/lib/gitlab/ci/pipeline/seed/stage.rb +++ b/lib/gitlab/ci/pipeline/seed/stage.rb @@ -3,7 +3,10 @@ module Gitlab module Pipeline module Seed class Stage < Seed::Base - delegate :size, to: :@seeds + attr_reader :pipeline, :seeds + + delegate :size, to: :seeds + delegate :dig, to: :seeds def initialize(pipeline, name, builds) @pipeline = pipeline @@ -24,12 +27,6 @@ module Gitlab project: @pipeline.project } end - # TODO decouple - # - def builds_attributes - @seeds.map(&:attributes) - end - def to_resource ::Ci::Stage.new(attributes).tap do |stage| @seeds.each do |seed| diff --git a/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb b/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb index 00e8ebdb7dd..e71f2f54e96 100644 --- a/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb @@ -17,20 +17,20 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do end end - describe '#stage_attributes' do + describe '#attributes' do it 'returns hash attributes of a stage' do expect(subject.attributes).to be_a Hash expect(subject.attributes).to include(:name, :project) end end - describe '#builds_attributes' do + describe '#seeds' do it 'returns hash attributes of all builds' do - expect(subject.builds_attributes.size).to eq 2 - expect(subject.builds_attributes).to all(include(ref: 'master')) - expect(subject.builds_attributes).to all(include(tag: false)) - expect(subject.builds_attributes).to all(include(project: pipeline.project)) - expect(subject.builds_attributes) + expect(subject.seeds.size).to eq 2 + expect(subject.seeds.map(&:attributes)).to all(include(ref: 'master')) + expect(subject.seeds.map(&:attributes)).to all(include(tag: false)) + expect(subject.seeds.map(&:attributes)).to all(include(project: pipeline.project)) + expect(subject.seeds.map(&:attributes)) .to all(include(trigger_request: pipeline.trigger_requests.first)) end @@ -40,17 +40,17 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do end it 'returns protected builds' do - expect(subject.builds_attributes).to all(include(protected: true)) + expect(subject.seeds.map(&:attributes)).to all(include(protected: true)) end end - context 'when a ref is unprotected' do + context 'when a ref is not protected' do before do allow_any_instance_of(Project).to receive(:protected_for?).and_return(false) end it 'returns unprotected builds' do - expect(subject.builds_attributes).to all(include(protected: false)) + expect(subject.seeds.map(&:attributes)).to all(include(protected: false)) end end end @@ -61,7 +61,7 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do it 'assignes relevant pipeline attributes' do subject.user = user - expect(subject.builds_attributes).to all(include(user: user)) + expect(subject.seeds.map(&:attributes)).to all(include(user: user)) end end diff --git a/spec/lib/gitlab/ci/yaml_processor_spec.rb b/spec/lib/gitlab/ci/yaml_processor_spec.rb index e67b51b52b9..9934fb4ecf0 100644 --- a/spec/lib/gitlab/ci/yaml_processor_spec.rb +++ b/spec/lib/gitlab/ci/yaml_processor_spec.rb @@ -121,9 +121,9 @@ module Gitlab expect(seeds.size).to eq 2 expect(seeds.first.attributes[:name]).to eq 'test' expect(seeds.second.attributes[:name]).to eq 'deploy' - expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'rspec' - expect(seeds.first.builds_attributes.dig(1, :name)).to eq 'spinach' - expect(seeds.second.builds_attributes.dig(0, :name)).to eq 'production' + expect(seeds.dig(0, 0, :name)).to eq 'rspec' + expect(seeds.dig(0, 1, :name)).to eq 'spinach' + expect(seeds.dig(1, 0, :name)).to eq 'production' end end @@ -142,7 +142,7 @@ module Gitlab expect(seeds.size).to eq 1 expect(seeds.first.attributes[:name]).to eq 'test' - expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach' + expect(seeds.dig(0, 0, :name)).to eq 'spinach' end end @@ -161,7 +161,7 @@ module Gitlab expect(seeds.size).to eq 1 expect(seeds.first.attributes[:name]).to eq 'test' - expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach' + expect(seeds.dig(0, 0, :name)).to eq 'spinach' end end @@ -183,8 +183,8 @@ module Gitlab seeds = subject.stage_seeds(pipeline) expect(seeds.size).to eq 2 - expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach' - expect(seeds.second.builds_attributes.dig(0, :name)).to eq 'production' + expect(seeds.dig(0, 0, :name)).to eq 'spinach' + expect(seeds.dig(1, 0, :name)).to eq 'production' end end @@ -209,7 +209,7 @@ module Gitlab seeds = subject.stage_seeds(pipeline) expect(seeds.size).to eq 1 - expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach' + expect(seeds.dig(0, 0, :name)).to eq 'spinach' end end end -- cgit v1.2.1