2017-02-10 15:19:29 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe Ci::RetryBuildService do
|
2017-09-05 08:13:46 +00:00
|
|
|
set(:user) { create(:user) }
|
|
|
|
set(:project) { create(:project) }
|
|
|
|
set(:pipeline) { create(:ci_pipeline, project: project) }
|
|
|
|
|
2018-01-25 14:15:51 +00:00
|
|
|
let(:stage) do
|
|
|
|
Ci::Stage.create!(project: project, pipeline: pipeline, name: 'test')
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline, stage_id: stage.id) }
|
2017-02-10 15:19:29 +00:00
|
|
|
|
|
|
|
let(:service) do
|
2017-02-14 09:38:17 +00:00
|
|
|
described_class.new(project, user)
|
2017-02-10 15:19:29 +00:00
|
|
|
end
|
|
|
|
|
2017-03-02 10:51:24 +00:00
|
|
|
CLONE_ACCESSORS = described_class::CLONE_ACCESSORS
|
|
|
|
|
|
|
|
REJECT_ACCESSORS =
|
|
|
|
%i[id status user token coverage trace runner artifacts_expire_at
|
|
|
|
artifacts_file artifacts_metadata artifacts_size created_at
|
|
|
|
updated_at started_at finished_at queued_at erased_by
|
2018-01-29 09:00:53 +00:00
|
|
|
erased_at auto_canceled_by job_artifacts job_artifacts_archive
|
|
|
|
job_artifacts_metadata job_artifacts_trace].freeze
|
2017-03-02 10:51:24 +00:00
|
|
|
|
|
|
|
IGNORE_ACCESSORS =
|
2017-09-25 16:54:08 +00:00
|
|
|
%i[type lock_version target_url base_tags trace_sections
|
2017-03-02 10:51:24 +00:00
|
|
|
commit_id deployments erased_by_id last_deployment project_id
|
|
|
|
runner_id tag_taggings taggings tags trigger_request_id
|
2017-09-01 07:52:11 +00:00
|
|
|
user_id auto_canceled_by_id retried failure_reason].freeze
|
2017-03-02 10:51:24 +00:00
|
|
|
|
2017-02-15 15:04:05 +00:00
|
|
|
shared_examples 'build duplication' do
|
2018-01-25 14:15:51 +00:00
|
|
|
let(:another_pipeline) { create(:ci_empty_pipeline, project: project) }
|
2017-06-07 08:05:40 +00:00
|
|
|
|
2017-02-15 15:04:05 +00:00
|
|
|
let(:build) do
|
2017-11-02 18:38:25 +00:00
|
|
|
create(:ci_build, :failed, :artifacts, :expired, :erased,
|
2017-03-01 10:57:06 +00:00
|
|
|
:queued, :coverage, :tags, :allowed_to_fail, :on_tag,
|
2018-01-31 12:56:04 +00:00
|
|
|
:triggered, :trace_artifact, :teardown_environment,
|
2018-01-25 14:15:51 +00:00
|
|
|
description: 'my-job', stage: 'test', stage_id: stage.id,
|
|
|
|
pipeline: pipeline, auto_canceled_by: another_pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
# Make sure that build has both `stage_id` and `stage` because FactoryBot
|
|
|
|
# can reset one of the fields when assigning another. We plan to deprecate
|
|
|
|
# and remove legacy `stage` column in the future.
|
|
|
|
build.update_attributes(stage: 'test', stage_id: stage.id)
|
2017-02-15 15:04:05 +00:00
|
|
|
end
|
|
|
|
|
2017-03-02 10:51:24 +00:00
|
|
|
describe 'clone accessors' do
|
|
|
|
CLONE_ACCESSORS.each do |attribute|
|
2017-02-16 12:13:10 +00:00
|
|
|
it "clones #{attribute} build attribute" do
|
2018-01-25 14:15:51 +00:00
|
|
|
expect(build.send(attribute)).not_to be_nil
|
2017-09-01 08:54:07 +00:00
|
|
|
expect(new_build.send(attribute)).not_to be_nil
|
2017-02-16 12:13:10 +00:00
|
|
|
expect(new_build.send(attribute)).to eq build.send(attribute)
|
|
|
|
end
|
|
|
|
end
|
2017-09-06 14:09:41 +00:00
|
|
|
|
|
|
|
context 'when job has nullified protected' do
|
|
|
|
before do
|
|
|
|
build.update_attribute(:protected, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "clones protected build attribute" do
|
2017-09-06 16:13:49 +00:00
|
|
|
expect(new_build.protected).to be_nil
|
2017-09-06 14:09:41 +00:00
|
|
|
expect(new_build.protected).to eq build.protected
|
|
|
|
end
|
|
|
|
end
|
2017-02-16 12:13:10 +00:00
|
|
|
end
|
2017-02-15 15:04:05 +00:00
|
|
|
|
2017-03-02 10:51:24 +00:00
|
|
|
describe 'reject acessors' do
|
|
|
|
REJECT_ACCESSORS.each do |attribute|
|
2017-02-16 12:13:10 +00:00
|
|
|
it "does not clone #{attribute} build attribute" do
|
|
|
|
expect(new_build.send(attribute)).not_to eq build.send(attribute)
|
|
|
|
end
|
2017-02-15 15:04:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-16 12:13:10 +00:00
|
|
|
it 'has correct number of known attributes' do
|
2017-03-02 10:51:24 +00:00
|
|
|
known_accessors = CLONE_ACCESSORS + REJECT_ACCESSORS + IGNORE_ACCESSORS
|
2017-03-01 10:39:36 +00:00
|
|
|
|
|
|
|
# :tag_list is a special case, this accessor does not exist
|
|
|
|
# in reflected associations, comes from `act_as_taggable` and
|
|
|
|
# we use it to copy tags, instead of reusing tags.
|
|
|
|
#
|
|
|
|
current_accessors =
|
2017-03-02 10:51:24 +00:00
|
|
|
Ci::Build.attribute_names.map(&:to_sym) +
|
|
|
|
Ci::Build.reflect_on_all_associations.map(&:name) +
|
2017-03-01 10:39:36 +00:00
|
|
|
[:tag_list]
|
|
|
|
|
|
|
|
current_accessors.uniq!
|
|
|
|
|
|
|
|
expect(known_accessors).to contain_exactly(*current_accessors)
|
2017-02-15 15:04:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-14 09:38:17 +00:00
|
|
|
describe '#execute' do
|
|
|
|
let(:new_build) { service.execute(build) }
|
2017-02-10 15:19:29 +00:00
|
|
|
|
2017-02-14 09:38:17 +00:00
|
|
|
context 'when user has ability to execute build' do
|
2017-02-10 15:19:29 +00:00
|
|
|
before do
|
2017-07-18 14:32:34 +00:00
|
|
|
stub_not_protect_default_branch
|
|
|
|
|
|
|
|
project.add_developer(user)
|
2017-02-10 15:19:29 +00:00
|
|
|
end
|
|
|
|
|
2017-02-15 15:04:05 +00:00
|
|
|
it_behaves_like 'build duplication'
|
|
|
|
|
2017-02-10 15:19:29 +00:00
|
|
|
it 'creates a new build that represents the old one' do
|
|
|
|
expect(new_build.name).to eq build.name
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues the new build' do
|
|
|
|
expect(new_build).to be_pending
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resolves todos for old build that failed' do
|
2017-02-13 12:01:52 +00:00
|
|
|
expect(MergeRequests::AddTodoWhenBuildFailsService)
|
2017-02-10 15:19:29 +00:00
|
|
|
.to receive_message_chain(:new, :close)
|
|
|
|
|
2017-02-14 09:38:17 +00:00
|
|
|
service.execute(build)
|
2017-02-10 15:19:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are subsequent builds that are skipped' do
|
|
|
|
let!(:subsequent_build) do
|
2018-01-25 14:15:51 +00:00
|
|
|
create(:ci_build, :skipped, stage_idx: 2,
|
|
|
|
pipeline: pipeline,
|
|
|
|
stage: 'deploy')
|
2017-02-10 15:19:29 +00:00
|
|
|
end
|
|
|
|
|
2018-01-25 14:15:51 +00:00
|
|
|
it 'resumes pipeline processing in a subsequent stage' do
|
2017-02-14 09:38:17 +00:00
|
|
|
service.execute(build)
|
2017-02-10 15:19:29 +00:00
|
|
|
|
|
|
|
expect(subsequent_build.reload).to be_created
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-14 09:38:17 +00:00
|
|
|
context 'when user does not have ability to execute build' do
|
2017-02-10 15:19:29 +00:00
|
|
|
it 'raises an error' do
|
2017-02-14 09:38:17 +00:00
|
|
|
expect { service.execute(build) }
|
2017-02-10 15:19:29 +00:00
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-14 14:22:49 +00:00
|
|
|
|
|
|
|
describe '#reprocess' do
|
2017-04-16 11:14:39 +00:00
|
|
|
let(:new_build) { service.reprocess!(build) }
|
2017-02-14 14:22:49 +00:00
|
|
|
|
|
|
|
context 'when user has ability to execute build' do
|
|
|
|
before do
|
2017-07-18 14:32:34 +00:00
|
|
|
stub_not_protect_default_branch
|
|
|
|
|
|
|
|
project.add_developer(user)
|
2017-02-14 14:22:49 +00:00
|
|
|
end
|
|
|
|
|
2017-02-15 15:04:05 +00:00
|
|
|
it_behaves_like 'build duplication'
|
|
|
|
|
2017-02-14 14:22:49 +00:00
|
|
|
it 'creates a new build that represents the old one' do
|
|
|
|
expect(new_build.name).to eq build.name
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not enqueue the new build' do
|
|
|
|
expect(new_build).to be_created
|
|
|
|
end
|
2017-04-16 11:14:39 +00:00
|
|
|
|
2017-10-13 15:26:17 +00:00
|
|
|
it 'does mark old build as retried in the database and on the instance' do
|
2017-04-16 11:14:39 +00:00
|
|
|
expect(new_build).to be_latest
|
2017-10-13 15:26:17 +00:00
|
|
|
expect(build).to be_retried
|
2017-04-16 11:14:39 +00:00
|
|
|
expect(build.reload).to be_retried
|
|
|
|
end
|
2017-02-14 14:22:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does not have ability to execute build' do
|
|
|
|
it 'raises an error' do
|
2017-04-16 11:14:39 +00:00
|
|
|
expect { service.reprocess!(build) }
|
2017-02-14 14:22:49 +00:00
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-10 15:19:29 +00:00
|
|
|
end
|