2019-03-30 07:15:48 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-07 07:58:15 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-26 09:08:59 +00:00
|
|
|
RSpec.describe UpdateHeadPipelineForMergeRequestWorker do
|
2017-09-07 07:58:15 +00:00
|
|
|
describe '#perform' do
|
2020-05-06 18:09:38 +00:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
let_it_be(:merge_request) { create(:merge_request, source_project: project) }
|
|
|
|
let_it_be(:latest_sha) { 'b83d6e391c22777fca1ed3012fce84f633d7fed0' }
|
2017-09-07 07:58:15 +00:00
|
|
|
|
|
|
|
context 'when pipeline exists for the source project and branch' do
|
2020-05-06 18:09:38 +00:00
|
|
|
let_it_be(:pipeline) { create(:ci_empty_pipeline, project: project, ref: merge_request.source_branch, sha: latest_sha) }
|
2017-09-07 07:58:15 +00:00
|
|
|
|
|
|
|
it 'updates the head_pipeline_id of the merge_request' do
|
2020-05-06 18:09:38 +00:00
|
|
|
expect { subject.perform(merge_request.id) }
|
|
|
|
.to change { merge_request.reload.head_pipeline_id }.from(nil).to(pipeline.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an idempotent worker' do
|
|
|
|
let(:job_args) { merge_request.id }
|
|
|
|
|
|
|
|
it 'sets the pipeline as the head pipeline when run multiple times' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(merge_request.reload.head_pipeline_id).to eq(pipeline.id)
|
|
|
|
end
|
2017-09-07 07:58:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when merge request sha does not equal pipeline sha' do
|
|
|
|
before do
|
2020-08-03 18:10:05 +00:00
|
|
|
merge_request.merge_request_diff.update!(head_commit_sha: Digest::SHA1.hexdigest(SecureRandom.hex))
|
2017-09-07 07:58:15 +00:00
|
|
|
end
|
|
|
|
|
2019-01-02 06:39:45 +00:00
|
|
|
it 'does not update head pipeline' do
|
|
|
|
expect { subject.perform(merge_request.id) }
|
|
|
|
.not_to change { merge_request.reload.head_pipeline_id }
|
2017-09-07 07:58:15 +00:00
|
|
|
end
|
2020-05-06 18:09:38 +00:00
|
|
|
|
|
|
|
it_behaves_like 'an idempotent worker' do
|
|
|
|
let(:job_args) { merge_request.id }
|
|
|
|
|
|
|
|
it 'does not update the head_pipeline_id when run multiple times' do
|
|
|
|
expect { subject }
|
|
|
|
.not_to change { merge_request.reload.head_pipeline_id }
|
|
|
|
end
|
|
|
|
end
|
2017-09-07 07:58:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline does not exist for the source project and branch' do
|
|
|
|
it 'does not update the head_pipeline_id of the merge_request' do
|
2019-01-02 06:39:45 +00:00
|
|
|
expect { subject.perform(merge_request.id) }
|
|
|
|
.not_to change { merge_request.reload.head_pipeline_id }
|
2017-09-07 07:58:15 +00:00
|
|
|
end
|
2020-05-06 18:09:38 +00:00
|
|
|
|
|
|
|
it_behaves_like 'an idempotent worker' do
|
|
|
|
let(:job_args) { merge_request.id }
|
|
|
|
|
|
|
|
it 'does not update the head_pipeline_id when run multiple times' do
|
|
|
|
expect { subject }
|
|
|
|
.not_to change { merge_request.reload.head_pipeline_id }
|
|
|
|
end
|
|
|
|
end
|
2017-09-07 07:58:15 +00:00
|
|
|
end
|
2018-12-05 06:57:00 +00:00
|
|
|
|
|
|
|
context 'when a merge request pipeline exists' do
|
2020-05-06 18:09:38 +00:00
|
|
|
let_it_be(:merge_request_pipeline) do
|
2018-12-05 06:57:00 +00:00
|
|
|
create(:ci_pipeline,
|
|
|
|
project: project,
|
2019-02-25 11:05:26 +00:00
|
|
|
source: :merge_request_event,
|
2018-12-05 06:57:00 +00:00
|
|
|
sha: latest_sha,
|
|
|
|
merge_request: merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the merge request pipeline as the head pipeline' do
|
|
|
|
expect { subject.perform(merge_request.id) }
|
|
|
|
.to change { merge_request.reload.head_pipeline_id }
|
|
|
|
.from(nil).to(merge_request_pipeline.id)
|
|
|
|
end
|
|
|
|
|
2020-05-06 18:09:38 +00:00
|
|
|
it_behaves_like 'an idempotent worker' do
|
|
|
|
let(:job_args) { merge_request.id }
|
|
|
|
|
|
|
|
it 'sets the merge request pipeline as the head pipeline when run multiple times' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(merge_request.reload.head_pipeline_id).to eq(merge_request_pipeline.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-05 06:57:00 +00:00
|
|
|
context 'when branch pipeline exists' do
|
|
|
|
let!(:branch_pipeline) do
|
|
|
|
create(:ci_pipeline, project: project, source: :push, sha: latest_sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'prioritizes the merge request pipeline as the head pipeline' do
|
|
|
|
expect { subject.perform(merge_request.id) }
|
|
|
|
.to change { merge_request.reload.head_pipeline_id }
|
|
|
|
.from(nil).to(merge_request_pipeline.id)
|
|
|
|
end
|
2020-05-06 18:09:38 +00:00
|
|
|
|
|
|
|
it_behaves_like 'an idempotent worker' do
|
|
|
|
let(:job_args) { merge_request.id }
|
|
|
|
|
|
|
|
it 'sets the merge request pipeline as the head pipeline when run multiple times' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(merge_request.reload.head_pipeline_id).to eq(merge_request_pipeline.id)
|
|
|
|
end
|
|
|
|
end
|
2018-12-05 06:57:00 +00:00
|
|
|
end
|
|
|
|
end
|
2017-09-07 07:58:15 +00:00
|
|
|
end
|
|
|
|
end
|