Commit b16b754f authored by Grzegorz Bizon's avatar Grzegorz Bizon

Make it easier to test pipeline stage seeds

parent 9d1c1d54
...@@ -395,6 +395,9 @@ module Ci ...@@ -395,6 +395,9 @@ module Ci
end end
end end
##
# TODO, setting yaml_errors should be moved to the pipeline creation chain.
#
def config_processor def config_processor
return unless ci_yaml_file return unless ci_yaml_file
return @config_processor if defined?(@config_processor) return @config_processor if defined?(@config_processor)
......
...@@ -9,16 +9,14 @@ module Gitlab ...@@ -9,16 +9,14 @@ module Gitlab
::Ci::Pipeline.transaction do ::Ci::Pipeline.transaction do
pipeline.save! 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 # Create environments before the pipeline starts.
# makes it available as an environment variable
# #
pipeline.builds.each do |build| pipeline.builds.each do |build|
if build.has_environment? if build.has_environment?
environment_name = build.expanded_environment_name project.environments.find_or_create_by(
project.environments.find_or_create_by(name: environment_name) name: build.expanded_environment_name
)
end end
end end
end end
......
...@@ -9,12 +9,13 @@ module Gitlab ...@@ -9,12 +9,13 @@ module Gitlab
def perform! def perform!
## ##
# Populate pipeline with seeds block. # Populate pipeline with block `CreatePipelineService#execute`.
#
# It comes from a block argument to CreatePipelineService#execute.
# #
@command.seeds_block&.call(pipeline) @command.seeds_block&.call(pipeline)
##
# Populate pipeline with all stages and builds.
#
pipeline.stage_seeds.each do |seed| pipeline.stage_seeds.each do |seed|
seed.user = current_user seed.user = current_user
......
...@@ -16,7 +16,7 @@ module Gitlab ...@@ -16,7 +16,7 @@ module Gitlab
@pipeline.drop!(:config_error) @pipeline.drop!(:config_error)
end end
return error(@pipeline.yaml_errors) error(@pipeline.yaml_errors)
end end
end end
......
...@@ -3,6 +3,10 @@ module Gitlab ...@@ -3,6 +3,10 @@ module Gitlab
module Pipeline module Pipeline
module Seed module Seed
class Build < Seed::Base class Build < Seed::Base
attr_reader :pipeline, :attributes
delegate :dig, to: :attributes
def initialize(pipeline, attributes) def initialize(pipeline, attributes)
@pipeline = pipeline @pipeline = pipeline
@attributes = attributes @attributes = attributes
......
...@@ -3,7 +3,10 @@ module Gitlab ...@@ -3,7 +3,10 @@ module Gitlab
module Pipeline module Pipeline
module Seed module Seed
class Stage < Seed::Base 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) def initialize(pipeline, name, builds)
@pipeline = pipeline @pipeline = pipeline
...@@ -24,12 +27,6 @@ module Gitlab ...@@ -24,12 +27,6 @@ module Gitlab
project: @pipeline.project } project: @pipeline.project }
end end
# TODO decouple
#
def builds_attributes
@seeds.map(&:attributes)
end
def to_resource def to_resource
::Ci::Stage.new(attributes).tap do |stage| ::Ci::Stage.new(attributes).tap do |stage|
@seeds.each do |seed| @seeds.each do |seed|
......
...@@ -17,20 +17,20 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do ...@@ -17,20 +17,20 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do
end end
end end
describe '#stage_attributes' do describe '#attributes' do
it 'returns hash attributes of a stage' do it 'returns hash attributes of a stage' do
expect(subject.attributes).to be_a Hash expect(subject.attributes).to be_a Hash
expect(subject.attributes).to include(:name, :project) expect(subject.attributes).to include(:name, :project)
end end
end end
describe '#builds_attributes' do describe '#seeds' do
it 'returns hash attributes of all builds' do it 'returns hash attributes of all builds' do
expect(subject.builds_attributes.size).to eq 2 expect(subject.seeds.size).to eq 2
expect(subject.builds_attributes).to all(include(ref: 'master')) expect(subject.seeds.map(&:attributes)).to all(include(ref: 'master'))
expect(subject.builds_attributes).to all(include(tag: false)) expect(subject.seeds.map(&:attributes)).to all(include(tag: false))
expect(subject.builds_attributes).to all(include(project: pipeline.project)) expect(subject.seeds.map(&:attributes)).to all(include(project: pipeline.project))
expect(subject.builds_attributes) expect(subject.seeds.map(&:attributes))
.to all(include(trigger_request: pipeline.trigger_requests.first)) .to all(include(trigger_request: pipeline.trigger_requests.first))
end end
...@@ -40,17 +40,17 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do ...@@ -40,17 +40,17 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do
end end
it 'returns protected builds' do 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
end end
context 'when a ref is unprotected' do context 'when a ref is not protected' do
before do before do
allow_any_instance_of(Project).to receive(:protected_for?).and_return(false) allow_any_instance_of(Project).to receive(:protected_for?).and_return(false)
end end
it 'returns unprotected builds' do 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 end
end end
...@@ -61,7 +61,7 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do ...@@ -61,7 +61,7 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do
it 'assignes relevant pipeline attributes' do it 'assignes relevant pipeline attributes' do
subject.user = user subject.user = user
expect(subject.builds_attributes).to all(include(user: user)) expect(subject.seeds.map(&:attributes)).to all(include(user: user))
end end
end end
......
...@@ -121,9 +121,9 @@ module Gitlab ...@@ -121,9 +121,9 @@ module Gitlab
expect(seeds.size).to eq 2 expect(seeds.size).to eq 2
expect(seeds.first.attributes[:name]).to eq 'test' expect(seeds.first.attributes[:name]).to eq 'test'
expect(seeds.second.attributes[:name]).to eq 'deploy' expect(seeds.second.attributes[:name]).to eq 'deploy'
expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'rspec' expect(seeds.dig(0, 0, :name)).to eq 'rspec'
expect(seeds.first.builds_attributes.dig(1, :name)).to eq 'spinach' expect(seeds.dig(0, 1, :name)).to eq 'spinach'
expect(seeds.second.builds_attributes.dig(0, :name)).to eq 'production' expect(seeds.dig(1, 0, :name)).to eq 'production'
end end
end end
...@@ -142,7 +142,7 @@ module Gitlab ...@@ -142,7 +142,7 @@ module Gitlab
expect(seeds.size).to eq 1 expect(seeds.size).to eq 1
expect(seeds.first.attributes[:name]).to eq 'test' 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
end end
...@@ -161,7 +161,7 @@ module Gitlab ...@@ -161,7 +161,7 @@ module Gitlab
expect(seeds.size).to eq 1 expect(seeds.size).to eq 1
expect(seeds.first.attributes[:name]).to eq 'test' 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
end end
...@@ -183,8 +183,8 @@ module Gitlab ...@@ -183,8 +183,8 @@ module Gitlab
seeds = subject.stage_seeds(pipeline) seeds = subject.stage_seeds(pipeline)
expect(seeds.size).to eq 2 expect(seeds.size).to eq 2
expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach' expect(seeds.dig(0, 0, :name)).to eq 'spinach'
expect(seeds.second.builds_attributes.dig(0, :name)).to eq 'production' expect(seeds.dig(1, 0, :name)).to eq 'production'
end end
end end
...@@ -209,7 +209,7 @@ module Gitlab ...@@ -209,7 +209,7 @@ module Gitlab
seeds = subject.stage_seeds(pipeline) seeds = subject.stage_seeds(pipeline)
expect(seeds.size).to eq 1 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 end
end end
......
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