2016-08-11 09:22:35 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-02-13 07:19:23 -05:00
|
|
|
describe Ci::ProcessPipelineService, :services do
|
2016-08-11 09:22:35 -04:00
|
|
|
let(:user) { create(:user) }
|
2017-02-13 07:19:23 -05:00
|
|
|
let(:project) { create(:empty_project) }
|
|
|
|
|
|
|
|
let(:pipeline) do
|
|
|
|
create(:ci_empty_pipeline, ref: 'master', project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2017-02-15 05:12:07 -05:00
|
|
|
project.add_developer(user)
|
2017-02-13 07:19:23 -05:00
|
|
|
end
|
2016-08-11 09:22:35 -04:00
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
context 'start queuing next builds' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'linux', stage_idx: 0)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'mac', stage_idx: 0)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'rspec', stage_idx: 1)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'rubocop', stage_idx: 1)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'deploy', stage_idx: 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'processes a pipeline' do
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_truthy
|
2016-08-11 09:22:35 -04:00
|
|
|
succeed_pending
|
|
|
|
expect(builds.success.count).to eq(2)
|
|
|
|
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_truthy
|
2016-08-11 09:22:35 -04:00
|
|
|
succeed_pending
|
|
|
|
expect(builds.success.count).to eq(4)
|
|
|
|
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_truthy
|
2016-08-11 09:22:35 -04:00
|
|
|
succeed_pending
|
|
|
|
expect(builds.success.count).to eq(5)
|
|
|
|
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_falsey
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not process pipeline if existing stage is running' do
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_truthy
|
2016-08-11 09:22:35 -04:00
|
|
|
expect(builds.pending.count).to eq(2)
|
2016-08-19 06:43:11 -04:00
|
|
|
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_falsey
|
2016-08-11 09:22:35 -04:00
|
|
|
expect(builds.pending.count).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'custom stage with first job allowed to fail' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'clean_job', stage_idx: 0, allow_failure: true)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'test_job', stage_idx: 1, allow_failure: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'automatically triggers a next stage when build finishes' do
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_truthy
|
2016-08-11 09:22:35 -04:00
|
|
|
expect(builds.pluck(:status)).to contain_exactly('pending')
|
|
|
|
|
|
|
|
pipeline.builds.running_or_pending.each(&:drop)
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('failed', 'pending')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'properly creates builds when "when" is defined' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'build', stage_idx: 0)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'test', stage_idx: 1)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'test_failure', stage_idx: 2, when: 'on_failure')
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'deploy', stage_idx: 3)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'production', stage_idx: 3, when: 'manual')
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'cleanup', stage_idx: 4, when: 'always')
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'clear cache', stage_idx: 4, when: 'manual')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when builds are successful' do
|
|
|
|
it 'properly creates builds' do
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_truthy
|
2016-08-11 09:22:35 -04:00
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'deploy')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'success', 'pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'deploy', 'cleanup')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'success', 'success', 'pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'success', 'success', 'success')
|
|
|
|
pipeline.reload
|
|
|
|
expect(pipeline.status).to eq('success')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when test job fails' do
|
|
|
|
it 'properly creates builds' do
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_truthy
|
2016-08-11 09:22:35 -04:00
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:drop)
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'failed', 'pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure', 'cleanup')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'failed', 'success', 'pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'failed', 'success', 'success')
|
|
|
|
pipeline.reload
|
|
|
|
expect(pipeline.status).to eq('failed')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when test and test_failure jobs fail' do
|
|
|
|
it 'properly creates builds' do
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_truthy
|
2016-08-11 09:22:35 -04:00
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:drop)
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'failed', 'pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:drop)
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure', 'cleanup')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'failed', 'failed', 'pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure', 'cleanup')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'failed', 'failed', 'success')
|
|
|
|
pipeline.reload
|
|
|
|
expect(pipeline.status).to eq('failed')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when deploy job fails' do
|
|
|
|
it 'properly creates builds' do
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_truthy
|
2016-08-11 09:22:35 -04:00
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'deploy')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'success', 'pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:drop)
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test', 'deploy', 'cleanup')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'success', 'failed', 'pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'success', 'failed', 'success')
|
|
|
|
pipeline.reload
|
|
|
|
expect(pipeline.status).to eq('failed')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is canceled in the second stage' do
|
|
|
|
it 'does not schedule builds after build has been canceled' do
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_truthy
|
2016-08-11 09:22:35 -04:00
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
|
|
|
expect(builds.running_or_pending).not_to be_empty
|
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build', 'test')
|
|
|
|
expect(builds.pluck(:status)).to contain_exactly('success', 'pending')
|
|
|
|
pipeline.builds.running_or_pending.each(&:cancel)
|
|
|
|
|
|
|
|
expect(builds.running_or_pending).to be_empty
|
|
|
|
expect(pipeline.reload.status).to eq('canceled')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when listing manual actions' do
|
|
|
|
it 'returns only for skipped builds' do
|
|
|
|
# currently all builds are created
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_truthy
|
2016-08-11 09:22:35 -04:00
|
|
|
expect(manual_actions).to be_empty
|
|
|
|
|
|
|
|
# succeed stage build
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
expect(manual_actions).to be_empty
|
|
|
|
|
|
|
|
# succeed stage test
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
expect(manual_actions).to be_one # production
|
|
|
|
|
|
|
|
# succeed stage deploy
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
expect(manual_actions).to be_many # production and clear cache
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-01 02:38:39 -04:00
|
|
|
context 'when there are manual/on_failure jobs in earlier stages' do
|
2016-09-29 15:41:38 -04:00
|
|
|
before do
|
|
|
|
builds
|
|
|
|
process_pipeline
|
|
|
|
builds.each(&:reload)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when first stage has only manual jobs' do
|
|
|
|
let(:builds) do
|
|
|
|
[create_build('build', 0, 'manual'),
|
|
|
|
create_build('check', 1),
|
|
|
|
create_build('test', 2)]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'starts from the second stage' do
|
2016-10-01 01:56:41 -04:00
|
|
|
expect(builds.map(&:status)).to eq(%w[skipped pending created])
|
2016-09-29 15:41:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when second stage has only manual jobs' do
|
|
|
|
let(:builds) do
|
|
|
|
[create_build('check', 0),
|
|
|
|
create_build('build', 1, 'manual'),
|
|
|
|
create_build('test', 2)]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'skips second stage and continues on third stage' do
|
2016-10-01 01:56:41 -04:00
|
|
|
expect(builds.map(&:status)).to eq(%w[pending created created])
|
2016-10-01 02:38:39 -04:00
|
|
|
|
|
|
|
builds.first.success
|
|
|
|
builds.each(&:reload)
|
|
|
|
|
|
|
|
expect(builds.map(&:status)).to eq(%w[success skipped pending])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when second stage has only on_failure jobs' do
|
|
|
|
let(:builds) do
|
|
|
|
[create_build('check', 0),
|
|
|
|
create_build('build', 1, 'on_failure'),
|
|
|
|
create_build('test', 2)]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'skips second stage and continues on third stage' do
|
|
|
|
expect(builds.map(&:status)).to eq(%w[pending created created])
|
2016-09-29 15:41:38 -04:00
|
|
|
|
|
|
|
builds.first.success
|
|
|
|
builds.each(&:reload)
|
|
|
|
|
2016-10-01 01:56:41 -04:00
|
|
|
expect(builds.map(&:status)).to eq(%w[success skipped pending])
|
2016-09-29 15:41:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-19 06:43:11 -04:00
|
|
|
context 'when failed build in the middle stage is retried' do
|
|
|
|
context 'when failed build is the only unsuccessful build in the stage' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'build:1', stage_idx: 0)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'build:2', stage_idx: 0)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'test:1', stage_idx: 1)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'test:2', stage_idx: 1)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'deploy:1', stage_idx: 2)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'deploy:2', stage_idx: 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does trigger builds in the next stage' do
|
2016-09-29 15:41:22 -04:00
|
|
|
expect(process_pipeline).to be_truthy
|
2016-08-19 06:43:11 -04:00
|
|
|
expect(builds.pluck(:name)).to contain_exactly('build:1', 'build:2')
|
|
|
|
|
|
|
|
pipeline.builds.running_or_pending.each(&:success)
|
|
|
|
|
2017-02-22 17:54:59 -05:00
|
|
|
expect(builds.pluck(:name))
|
|
|
|
.to contain_exactly('build:1', 'build:2', 'test:1', 'test:2')
|
2016-08-19 06:43:11 -04:00
|
|
|
|
|
|
|
pipeline.builds.find_by(name: 'test:1').success
|
|
|
|
pipeline.builds.find_by(name: 'test:2').drop
|
|
|
|
|
2017-02-22 17:54:59 -05:00
|
|
|
expect(builds.pluck(:name))
|
|
|
|
.to contain_exactly('build:1', 'build:2', 'test:1', 'test:2')
|
2016-08-19 06:43:11 -04:00
|
|
|
|
2017-02-13 07:19:23 -05:00
|
|
|
Ci::Build.retry(pipeline.builds.find_by(name: 'test:2'), user).success
|
2016-08-19 06:43:11 -04:00
|
|
|
|
|
|
|
expect(builds.pluck(:name)).to contain_exactly(
|
|
|
|
'build:1', 'build:2', 'test:1', 'test:2', 'test:2', 'deploy:1', 'deploy:2')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-25 09:11:56 -05:00
|
|
|
context 'when there are builds that are not created yet' do
|
|
|
|
let(:pipeline) do
|
2016-11-27 10:24:43 -05:00
|
|
|
create(:ci_pipeline, config: config)
|
2016-11-25 09:11:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:config) do
|
|
|
|
{ rspec: { stage: 'test', script: 'rspec' },
|
|
|
|
deploy: { stage: 'deploy', script: 'rsync' } }
|
|
|
|
end
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
before do
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'linux', stage: 'build', stage_idx: 0)
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'mac', stage: 'build', stage_idx: 0)
|
|
|
|
end
|
|
|
|
|
2016-11-25 04:10:55 -05:00
|
|
|
it 'processes the pipeline' do
|
|
|
|
# Currently we have five builds with state created
|
|
|
|
#
|
2016-08-11 09:22:35 -04:00
|
|
|
expect(builds.count).to eq(0)
|
2016-11-25 09:11:56 -05:00
|
|
|
expect(all_builds.count).to eq(2)
|
2016-11-25 04:10:55 -05:00
|
|
|
|
2016-11-25 09:11:56 -05:00
|
|
|
# Process builds service will enqueue builds from the first stage.
|
2016-11-25 04:10:55 -05:00
|
|
|
#
|
|
|
|
process_pipeline
|
2016-08-11 09:22:35 -04:00
|
|
|
|
|
|
|
expect(builds.count).to eq(2)
|
2016-11-25 09:11:56 -05:00
|
|
|
expect(all_builds.count).to eq(2)
|
2016-08-11 09:22:35 -04:00
|
|
|
|
2016-11-25 09:11:56 -05:00
|
|
|
# When builds succeed we will enqueue remaining builds.
|
|
|
|
#
|
|
|
|
# We will have 2 succeeded, 1 pending (from stage test), total 4 (two
|
|
|
|
# additional build from `.gitlab-ci.yml`).
|
2016-11-25 04:10:55 -05:00
|
|
|
#
|
2016-08-11 09:22:35 -04:00
|
|
|
succeed_pending
|
2016-11-25 04:10:55 -05:00
|
|
|
process_pipeline
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
expect(builds.success.count).to eq(2)
|
2016-11-25 09:11:56 -05:00
|
|
|
expect(builds.pending.count).to eq(1)
|
|
|
|
expect(all_builds.count).to eq(4)
|
2016-08-11 09:22:35 -04:00
|
|
|
|
2017-02-17 08:56:13 -05:00
|
|
|
# When pending merge_when_pipeline_succeeds in stage test, we enqueue deploy stage.
|
2016-11-25 04:10:55 -05:00
|
|
|
#
|
2016-08-11 09:22:35 -04:00
|
|
|
succeed_pending
|
2016-11-25 04:10:55 -05:00
|
|
|
process_pipeline
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
expect(builds.pending.count).to eq(1)
|
2016-11-25 09:11:56 -05:00
|
|
|
expect(builds.success.count).to eq(3)
|
|
|
|
expect(all_builds.count).to eq(4)
|
2016-08-11 09:22:35 -04:00
|
|
|
|
2016-11-25 09:11:56 -05:00
|
|
|
# When the last one succeeds we have 4 successful builds.
|
2016-11-25 04:10:55 -05:00
|
|
|
#
|
2016-08-11 09:22:35 -04:00
|
|
|
succeed_pending
|
2016-11-25 04:10:55 -05:00
|
|
|
process_pipeline
|
|
|
|
|
2016-11-25 09:11:56 -05:00
|
|
|
expect(builds.success.count).to eq(4)
|
|
|
|
expect(all_builds.count).to eq(4)
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-11-25 04:44:13 -05:00
|
|
|
|
|
|
|
def all_builds
|
|
|
|
pipeline.builds
|
|
|
|
end
|
|
|
|
|
|
|
|
def builds
|
|
|
|
all_builds.where.not(status: [:created, :skipped])
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_pipeline
|
|
|
|
described_class.new(pipeline.project, user).execute(pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
def succeed_pending
|
|
|
|
builds.pending.update_all(status: 'success')
|
|
|
|
end
|
|
|
|
|
2017-02-22 12:51:46 -05:00
|
|
|
delegate :manual_actions, to: :pipeline
|
2016-11-25 04:44:13 -05:00
|
|
|
|
|
|
|
def create_build(name, stage_idx, when_value = nil)
|
|
|
|
create(:ci_build,
|
|
|
|
:created,
|
|
|
|
pipeline: pipeline,
|
|
|
|
name: name,
|
|
|
|
stage_idx: stage_idx,
|
|
|
|
when: when_value)
|
|
|
|
end
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|