Commit 9b5a912f authored by Grzegorz Bizon's avatar Grzegorz Bizon

Make it easier to test pipeline stage seeds

parent b82de0f0
......@@ -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)
......
......@@ -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
......
......@@ -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
......
......@@ -16,7 +16,7 @@ module Gitlab
@pipeline.drop!(:config_error)
end
return error(@pipeline.yaml_errors)
error(@pipeline.yaml_errors)
end
end
......
......@@ -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
......
......@@ -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|
......
......@@ -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
......
......@@ -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
......
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