2018-02-05 15:47:34 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Ci::EnsureStageService, '#execute' do
|
|
|
|
set(:project) { create(:project) }
|
|
|
|
set(:user) { create(:user) }
|
|
|
|
|
|
|
|
let(:stage) { create(:ci_stage_entity) }
|
|
|
|
let(:job) { build(:ci_build) }
|
|
|
|
|
|
|
|
let(:service) { described_class.new(project, user) }
|
|
|
|
|
|
|
|
context 'when build has a stage assigned' do
|
2018-02-06 08:29:40 +00:00
|
|
|
it 'does not create a new stage' do
|
|
|
|
job.assign_attributes(stage_id: stage.id)
|
2018-02-05 15:47:34 +00:00
|
|
|
|
2018-02-06 08:29:40 +00:00
|
|
|
expect { service.execute(job) }.not_to change { Ci::Stage.count }
|
|
|
|
end
|
2018-02-05 15:47:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build does not have a stage assigned' do
|
2018-02-06 08:29:40 +00:00
|
|
|
it 'creates a new stage' do
|
|
|
|
job.assign_attributes(stage_id: nil, stage: 'test')
|
2018-02-05 15:47:34 +00:00
|
|
|
|
2018-02-06 08:29:40 +00:00
|
|
|
expect { service.execute(job) }.to change { Ci::Stage.count }.by(1)
|
|
|
|
end
|
2018-02-05 15:47:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is invalid' do
|
2018-02-06 08:29:40 +00:00
|
|
|
it 'does not create a new stage' do
|
|
|
|
job.assign_attributes(stage_id: nil, ref: nil)
|
2018-02-05 15:47:34 +00:00
|
|
|
|
2018-02-06 08:29:40 +00:00
|
|
|
expect { service.execute(job) }.not_to change { Ci::Stage.count }
|
|
|
|
end
|
2018-02-05 15:47:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when new stage can not be created because of an exception' do
|
|
|
|
before do
|
|
|
|
allow(Ci::Stage).to receive(:create!)
|
|
|
|
.and_raise(ActiveRecord::RecordNotUnique.new('Duplicates!'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries up to two times' do
|
|
|
|
job.assign_attributes(stage_id: nil)
|
|
|
|
|
|
|
|
expect(service).to receive(:find_stage).exactly(2).times
|
|
|
|
|
|
|
|
expect { service.execute(job) }
|
2018-02-06 09:02:33 +00:00
|
|
|
.to raise_error(Ci::EnsureStageService::EnsureStageError)
|
2018-02-05 15:47:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|