2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-13 09:14:40 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe Ci::RetryPipelineService, '#execute' do
|
2018-05-15 04:18:22 -04:00
|
|
|
include ProjectForksHelper
|
|
|
|
|
2017-02-13 09:14:40 -05:00
|
|
|
let(:user) { create(:user) }
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2017-02-13 09:14:40 -05:00
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project) }
|
2017-02-14 04:38:17 -05:00
|
|
|
let(:service) { described_class.new(project, user) }
|
2017-02-13 09:14:40 -05:00
|
|
|
|
2017-05-31 05:22:12 -04:00
|
|
|
context 'when user has full ability to modify pipeline' do
|
2017-05-05 13:35:32 -04:00
|
|
|
before do
|
2017-05-31 05:22:12 -04:00
|
|
|
project.add_developer(user)
|
|
|
|
|
|
|
|
create(:protected_branch, :developers_can_merge,
|
|
|
|
name: pipeline.ref, project: project)
|
2017-05-05 13:35:32 -04:00
|
|
|
end
|
2017-02-13 09:14:40 -05:00
|
|
|
|
2017-03-27 03:11:29 -04:00
|
|
|
context 'when there are already retried jobs present' do
|
|
|
|
before do
|
2017-04-16 07:14:39 -04:00
|
|
|
create_build('rspec', :canceled, 0, retried: true)
|
2017-03-27 03:11:29 -04:00
|
|
|
create_build('rspec', :failed, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not retry jobs that has already been retried' do
|
|
|
|
expect(statuses.first).to be_retried
|
|
|
|
expect { service.execute(pipeline) }
|
|
|
|
.to change { CommitStatus.count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-13 09:14:40 -05:00
|
|
|
context 'when there are failed builds in the last stage' do
|
|
|
|
before do
|
2017-02-14 07:29:58 -05:00
|
|
|
create_build('rspec 1', :success, 0)
|
|
|
|
create_build('rspec 2', :failed, 1)
|
|
|
|
create_build('rspec 3', :canceled, 1)
|
2017-02-13 09:14:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues all builds in the last stage' do
|
2017-02-14 04:38:17 -05:00
|
|
|
service.execute(pipeline)
|
2017-02-13 09:14:40 -05:00
|
|
|
|
|
|
|
expect(build('rspec 2')).to be_pending
|
|
|
|
expect(build('rspec 3')).to be_pending
|
2017-02-13 10:38:08 -05:00
|
|
|
expect(pipeline.reload).to be_running
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are failed or canceled builds in the first stage' do
|
|
|
|
before do
|
2017-02-14 07:29:58 -05:00
|
|
|
create_build('rspec 1', :failed, 0)
|
|
|
|
create_build('rspec 2', :canceled, 0)
|
|
|
|
create_build('rspec 3', :canceled, 1)
|
2017-02-14 09:22:49 -05:00
|
|
|
create_build('spinach 1', :canceled, 2)
|
2017-02-13 10:38:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries builds failed builds and marks subsequent for processing' do
|
2017-02-14 04:38:17 -05:00
|
|
|
service.execute(pipeline)
|
2017-02-13 10:38:08 -05:00
|
|
|
|
|
|
|
expect(build('rspec 1')).to be_pending
|
|
|
|
expect(build('rspec 2')).to be_pending
|
|
|
|
expect(build('rspec 3')).to be_created
|
2017-02-14 09:22:49 -05:00
|
|
|
expect(build('spinach 1')).to be_created
|
2017-02-13 10:38:08 -05:00
|
|
|
expect(pipeline.reload).to be_running
|
|
|
|
end
|
2020-12-21 10:10:05 -05:00
|
|
|
|
|
|
|
it 'changes ownership of subsequent builds' do
|
|
|
|
expect(build('rspec 2').user).not_to eq(user)
|
|
|
|
expect(build('rspec 3').user).not_to eq(user)
|
|
|
|
expect(build('spinach 1').user).not_to eq(user)
|
|
|
|
|
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(build('rspec 2').user).to eq(user)
|
|
|
|
expect(build('rspec 3').user).to eq(user)
|
|
|
|
expect(build('spinach 1').user).to eq(user)
|
|
|
|
end
|
2017-02-13 10:38:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is failed build present which was run on failure' do
|
|
|
|
before do
|
2017-02-14 07:29:58 -05:00
|
|
|
create_build('rspec 1', :failed, 0)
|
|
|
|
create_build('rspec 2', :canceled, 0)
|
|
|
|
create_build('rspec 3', :canceled, 1)
|
|
|
|
create_build('report 1', :failed, 2)
|
2017-02-13 10:38:08 -05:00
|
|
|
end
|
|
|
|
|
2017-02-14 09:22:49 -05:00
|
|
|
it 'retries builds only in the first stage' do
|
2017-02-14 04:38:17 -05:00
|
|
|
service.execute(pipeline)
|
2017-02-13 10:38:08 -05:00
|
|
|
|
|
|
|
expect(build('rspec 1')).to be_pending
|
|
|
|
expect(build('rspec 2')).to be_pending
|
|
|
|
expect(build('rspec 3')).to be_created
|
|
|
|
expect(build('report 1')).to be_created
|
|
|
|
expect(pipeline.reload).to be_running
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new job for report job in this case' do
|
2017-02-14 04:38:17 -05:00
|
|
|
service.execute(pipeline)
|
2017-02-13 10:38:08 -05:00
|
|
|
|
2020-03-31 08:08:09 -04:00
|
|
|
expect(statuses.find_by(name: 'report 1', status: 'failed')).to be_retried
|
2017-02-13 09:14:40 -05:00
|
|
|
end
|
|
|
|
end
|
2017-02-14 07:29:58 -05:00
|
|
|
|
2019-12-06 13:07:44 -05:00
|
|
|
context 'when there is a failed test in a DAG' do
|
|
|
|
before do
|
|
|
|
create_build('build', :success, 0)
|
|
|
|
create_build('build2', :success, 0)
|
2020-02-14 07:09:03 -05:00
|
|
|
test_build = create_build('test', :failed, 1, scheduling_type: :dag)
|
2019-12-06 13:07:44 -05:00
|
|
|
create(:ci_build_need, build: test_build, name: 'build')
|
|
|
|
create(:ci_build_need, build: test_build, name: 'build2')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries the test' do
|
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(build('build')).to be_success
|
|
|
|
expect(build('build2')).to be_success
|
|
|
|
expect(build('test')).to be_pending
|
|
|
|
expect(build('test').needs.map(&:name)).to match_array(%w(build build2))
|
|
|
|
end
|
2020-02-14 07:09:03 -05:00
|
|
|
|
|
|
|
context 'when there is a failed DAG test without needs' do
|
|
|
|
before do
|
|
|
|
create_build('deploy', :failed, 2, scheduling_type: :dag)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries the test' do
|
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(build('build')).to be_success
|
|
|
|
expect(build('build2')).to be_success
|
|
|
|
expect(build('test')).to be_pending
|
|
|
|
expect(build('deploy')).to be_pending
|
|
|
|
end
|
|
|
|
end
|
2019-12-06 13:07:44 -05:00
|
|
|
end
|
|
|
|
|
2017-02-22 02:39:50 -05:00
|
|
|
context 'when the last stage was skipepd' do
|
|
|
|
before do
|
|
|
|
create_build('build 1', :success, 0)
|
|
|
|
create_build('test 2', :failed, 1)
|
|
|
|
create_build('report 3', :skipped, 2)
|
|
|
|
create_build('report 4', :skipped, 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries builds only in the first stage' do
|
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(build('build 1')).to be_success
|
|
|
|
expect(build('test 2')).to be_pending
|
|
|
|
expect(build('report 3')).to be_created
|
|
|
|
expect(build('report 4')).to be_created
|
|
|
|
expect(pipeline.reload).to be_running
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-14 09:22:49 -05:00
|
|
|
context 'when pipeline contains manual actions' do
|
2017-03-06 07:40:50 -05:00
|
|
|
context 'when there are optional manual actions only' do
|
|
|
|
context 'when there is a canceled manual action in first stage' do
|
|
|
|
before do
|
|
|
|
create_build('rspec 1', :failed, 0)
|
|
|
|
create_build('staging', :canceled, 0, when: :manual, allow_failure: true)
|
|
|
|
create_build('rspec 2', :canceled, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries failed builds and marks subsequent for processing' do
|
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(build('rspec 1')).to be_pending
|
|
|
|
expect(build('staging')).to be_manual
|
|
|
|
expect(build('rspec 2')).to be_created
|
|
|
|
expect(pipeline.reload).to be_running
|
|
|
|
end
|
2020-12-21 10:10:05 -05:00
|
|
|
|
|
|
|
it 'changes ownership of subsequent builds' do
|
|
|
|
expect(build('staging').user).not_to eq(user)
|
|
|
|
expect(build('rspec 2').user).not_to eq(user)
|
|
|
|
|
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(build('staging').user).to eq(user)
|
|
|
|
expect(build('rspec 2').user).to eq(user)
|
|
|
|
end
|
2017-02-14 09:22:49 -05:00
|
|
|
end
|
2017-03-06 07:40:50 -05:00
|
|
|
end
|
2017-02-14 09:22:49 -05:00
|
|
|
|
2017-03-06 07:40:50 -05:00
|
|
|
context 'when pipeline has blocking manual actions defined' do
|
|
|
|
context 'when pipeline retry should enqueue builds' do
|
|
|
|
before do
|
|
|
|
create_build('test', :failed, 0)
|
|
|
|
create_build('deploy', :canceled, 0, when: :manual, allow_failure: false)
|
|
|
|
create_build('verify', :canceled, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries failed builds' do
|
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(build('test')).to be_pending
|
|
|
|
expect(build('deploy')).to be_manual
|
|
|
|
expect(build('verify')).to be_created
|
|
|
|
expect(pipeline.reload).to be_running
|
|
|
|
end
|
|
|
|
end
|
2017-02-14 09:22:49 -05:00
|
|
|
|
2017-03-06 07:40:50 -05:00
|
|
|
context 'when pipeline retry should block pipeline immediately' do
|
|
|
|
before do
|
|
|
|
create_build('test', :success, 0)
|
|
|
|
create_build('deploy:1', :success, 1, when: :manual, allow_failure: false)
|
|
|
|
create_build('deploy:2', :failed, 1, when: :manual, allow_failure: false)
|
|
|
|
create_build('verify', :canceled, 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reprocesses blocking manual action and blocks pipeline' do
|
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(build('deploy:1')).to be_success
|
|
|
|
expect(build('deploy:2')).to be_manual
|
|
|
|
expect(build('verify')).to be_created
|
|
|
|
expect(pipeline.reload).to be_blocked
|
|
|
|
end
|
2017-02-14 09:22:49 -05:00
|
|
|
end
|
2017-02-14 07:29:58 -05:00
|
|
|
end
|
|
|
|
|
2017-02-14 09:22:49 -05:00
|
|
|
context 'when there is a skipped manual action in last stage' do
|
|
|
|
before do
|
|
|
|
create_build('rspec 1', :canceled, 0)
|
2017-03-03 04:37:41 -05:00
|
|
|
create_build('rspec 2', :skipped, 0, when: :manual, allow_failure: true)
|
|
|
|
create_build('staging', :skipped, 1, when: :manual, allow_failure: true)
|
2017-02-14 09:22:49 -05:00
|
|
|
end
|
2017-02-14 07:29:58 -05:00
|
|
|
|
2017-02-22 02:39:50 -05:00
|
|
|
it 'retries canceled job and reprocesses manual actions' do
|
2017-02-14 09:22:49 -05:00
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(build('rspec 1')).to be_pending
|
2017-03-03 09:18:31 -05:00
|
|
|
expect(build('rspec 2')).to be_manual
|
2017-02-22 02:39:50 -05:00
|
|
|
expect(build('staging')).to be_created
|
2017-02-14 09:22:49 -05:00
|
|
|
expect(pipeline.reload).to be_running
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a created manual action in the last stage' do
|
|
|
|
before do
|
|
|
|
create_build('rspec 1', :canceled, 0)
|
2017-03-03 04:37:41 -05:00
|
|
|
create_build('staging', :created, 1, when: :manual, allow_failure: true)
|
2017-02-14 09:22:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries canceled job and does not update the manual action' do
|
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(build('rspec 1')).to be_pending
|
|
|
|
expect(build('staging')).to be_created
|
|
|
|
expect(pipeline.reload).to be_running
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a created manual action in the first stage' do
|
|
|
|
before do
|
|
|
|
create_build('rspec 1', :canceled, 0)
|
2017-03-03 04:37:41 -05:00
|
|
|
create_build('staging', :created, 0, when: :manual, allow_failure: true)
|
2017-02-14 09:22:49 -05:00
|
|
|
end
|
|
|
|
|
2017-03-03 09:18:31 -05:00
|
|
|
it 'retries canceled job and processes the manual action' do
|
2017-02-14 09:22:49 -05:00
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(build('rspec 1')).to be_pending
|
2017-03-03 09:18:31 -05:00
|
|
|
expect(build('staging')).to be_manual
|
2017-02-14 09:22:49 -05:00
|
|
|
expect(pipeline.reload).to be_running
|
|
|
|
end
|
2017-02-14 07:29:58 -05:00
|
|
|
end
|
|
|
|
end
|
2017-02-15 05:02:05 -05:00
|
|
|
|
|
|
|
it 'closes all todos about failed jobs for pipeline' do
|
2021-04-01 17:09:22 -04:00
|
|
|
expect(::MergeRequests::AddTodoWhenBuildFailsService)
|
2017-02-15 05:02:05 -05:00
|
|
|
.to receive_message_chain(:new, :close_all)
|
|
|
|
|
|
|
|
service.execute(pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reprocesses the pipeline' do
|
2019-11-19 07:06:00 -05:00
|
|
|
expect_any_instance_of(Ci::ProcessPipelineService).to receive(:execute)
|
2017-02-15 05:02:05 -05:00
|
|
|
|
|
|
|
service.execute(pipeline)
|
|
|
|
end
|
2020-05-15 11:08:04 -04:00
|
|
|
|
|
|
|
context 'when pipeline has processables with nil scheduling_type' do
|
|
|
|
let!(:build1) { create_build('build1', :success, 0) }
|
|
|
|
let!(:build2) { create_build('build2', :failed, 0) }
|
|
|
|
let!(:build3) { create_build('build3', :failed, 1) }
|
|
|
|
let!(:build3_needs_build1) { create(:ci_build_need, build: build3, name: build1.name) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
statuses.update_all(scheduling_type: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'populates scheduling_type of processables' do
|
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(build1.reload.scheduling_type).to eq('stage')
|
|
|
|
expect(build2.reload.scheduling_type).to eq('stage')
|
|
|
|
expect(build3.reload.scheduling_type).to eq('dag')
|
|
|
|
end
|
|
|
|
end
|
2020-09-17 08:09:37 -04:00
|
|
|
|
|
|
|
context 'when the pipeline is a downstream pipeline and the bridge is depended' do
|
|
|
|
let!(:bridge) { create(:ci_bridge, :strategy_depend, status: 'success') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:ci_sources_pipeline, pipeline: pipeline, source_job: bridge)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks source bridge as pending' do
|
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(bridge.reload).to be_pending
|
|
|
|
end
|
|
|
|
end
|
2017-02-13 09:14:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not allowed to retry pipeline' do
|
|
|
|
it 'raises an error' do
|
2017-02-14 04:38:17 -05:00
|
|
|
expect { service.execute(pipeline) }
|
2017-02-13 09:14:40 -05:00
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-05 13:35:32 -04:00
|
|
|
context 'when user is not allowed to trigger manual action' do
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
2018-07-11 10:36:08 -04:00
|
|
|
create(:protected_branch, :maintainers_can_push,
|
2018-04-18 09:52:55 -04:00
|
|
|
name: pipeline.ref, project: project)
|
2017-05-05 13:35:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a failed manual action present' do
|
|
|
|
before do
|
|
|
|
create_build('test', :failed, 0)
|
|
|
|
create_build('deploy', :failed, 0, when: :manual)
|
|
|
|
create_build('verify', :canceled, 1)
|
|
|
|
end
|
|
|
|
|
2017-07-03 17:58:28 -04:00
|
|
|
it 'raises an error' do
|
|
|
|
expect { service.execute(pipeline) }
|
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
2017-05-05 13:35:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a failed manual action in later stage' do
|
|
|
|
before do
|
|
|
|
create_build('test', :failed, 0)
|
|
|
|
create_build('deploy', :failed, 1, when: :manual)
|
|
|
|
create_build('verify', :canceled, 2)
|
|
|
|
end
|
|
|
|
|
2017-07-03 17:58:28 -04:00
|
|
|
it 'raises an error' do
|
|
|
|
expect { service.execute(pipeline) }
|
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
2017-05-05 13:35:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-15 04:18:22 -04:00
|
|
|
context 'when maintainer is allowed to push to forked project' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:forked_project) { fork_project(project) }
|
|
|
|
let(:pipeline) { create(:ci_pipeline, project: forked_project, ref: 'fixes') }
|
|
|
|
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2018-05-15 04:18:22 -04:00
|
|
|
create(:merge_request,
|
|
|
|
source_project: forked_project,
|
|
|
|
target_project: project,
|
|
|
|
source_branch: 'fixes',
|
2018-05-22 21:54:57 -04:00
|
|
|
allow_collaboration: true)
|
2018-05-15 04:18:22 -04:00
|
|
|
create_build('rspec 1', :failed, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows to retry failed pipeline' do
|
2018-12-28 06:18:50 -05:00
|
|
|
allow_any_instance_of(Project).to receive(:branch_allows_collaboration?).and_return(true)
|
2018-05-15 04:18:22 -04:00
|
|
|
allow_any_instance_of(Project).to receive(:empty_repo?).and_return(false)
|
|
|
|
|
|
|
|
service.execute(pipeline)
|
|
|
|
|
|
|
|
expect(build('rspec 1')).to be_pending
|
|
|
|
expect(pipeline.reload).to be_running
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-13 10:38:08 -05:00
|
|
|
def statuses
|
|
|
|
pipeline.reload.statuses
|
|
|
|
end
|
|
|
|
|
2017-02-13 09:14:40 -05:00
|
|
|
def build(name)
|
2017-02-13 10:38:08 -05:00
|
|
|
statuses.latest.find_by(name: name)
|
2017-02-13 09:14:40 -05:00
|
|
|
end
|
|
|
|
|
2017-03-03 04:37:41 -05:00
|
|
|
def create_build(name, status, stage_num, **opts)
|
2017-02-13 09:14:40 -05:00
|
|
|
create(:ci_build, name: name,
|
|
|
|
status: status,
|
|
|
|
stage: "stage_#{stage_num}",
|
|
|
|
stage_idx: stage_num,
|
2017-03-03 04:37:41 -05:00
|
|
|
pipeline: pipeline, **opts) do |build|
|
2020-08-17 08:10:12 -04:00
|
|
|
::Ci::ProcessPipelineService.new(pipeline).execute
|
2017-02-13 09:14:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|