2017-02-10 10:19:29 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Ci::RetryBuildService, :services do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:empty_project) }
|
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project) }
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline) }
|
|
|
|
|
|
|
|
let(:service) do
|
2017-02-14 04:38:17 -05:00
|
|
|
described_class.new(project, user)
|
2017-02-10 10:19:29 -05:00
|
|
|
end
|
|
|
|
|
2017-03-02 05:51:24 -05: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
|
|
|
|
erased_at].freeze
|
|
|
|
|
|
|
|
IGNORE_ACCESSORS =
|
2017-03-17 19:06:11 -04:00
|
|
|
%i[type lock_version target_url base_tags
|
2017-03-02 05:51:24 -05:00
|
|
|
commit_id deployments erased_by_id last_deployment project_id
|
|
|
|
runner_id tag_taggings taggings tags trigger_request_id
|
|
|
|
user_id].freeze
|
|
|
|
|
2017-02-15 10:04:05 -05:00
|
|
|
shared_examples 'build duplication' do
|
|
|
|
let(:build) do
|
2017-03-01 05:57:06 -05:00
|
|
|
create(:ci_build, :failed, :artifacts_expired, :erased,
|
|
|
|
:queued, :coverage, :tags, :allowed_to_fail, :on_tag,
|
|
|
|
:teardown_environment, :triggered, :trace,
|
|
|
|
description: 'some build', pipeline: pipeline)
|
2017-02-15 10:04:05 -05:00
|
|
|
end
|
|
|
|
|
2017-03-02 05:51:24 -05:00
|
|
|
describe 'clone accessors' do
|
|
|
|
CLONE_ACCESSORS.each do |attribute|
|
2017-02-16 07:13:10 -05:00
|
|
|
it "clones #{attribute} build attribute" do
|
2017-03-01 05:57:06 -05:00
|
|
|
expect(new_build.send(attribute)).to be_present
|
2017-02-16 07:13:10 -05:00
|
|
|
expect(new_build.send(attribute)).to eq build.send(attribute)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-15 10:04:05 -05:00
|
|
|
|
2017-03-02 05:51:24 -05:00
|
|
|
describe 'reject acessors' do
|
|
|
|
REJECT_ACCESSORS.each do |attribute|
|
2017-02-16 07:13:10 -05:00
|
|
|
it "does not clone #{attribute} build attribute" do
|
|
|
|
expect(new_build.send(attribute)).not_to eq build.send(attribute)
|
|
|
|
end
|
2017-02-15 10:04:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-16 07:13:10 -05:00
|
|
|
it 'has correct number of known attributes' do
|
2017-03-02 05:51:24 -05:00
|
|
|
known_accessors = CLONE_ACCESSORS + REJECT_ACCESSORS + IGNORE_ACCESSORS
|
2017-03-01 05:39:36 -05: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 05:51:24 -05:00
|
|
|
Ci::Build.attribute_names.map(&:to_sym) +
|
|
|
|
Ci::Build.reflect_on_all_associations.map(&:name) +
|
2017-03-01 05:39:36 -05:00
|
|
|
[:tag_list]
|
|
|
|
|
|
|
|
current_accessors.uniq!
|
|
|
|
|
|
|
|
expect(known_accessors).to contain_exactly(*current_accessors)
|
2017-02-15 10:04:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-14 04:38:17 -05:00
|
|
|
describe '#execute' do
|
|
|
|
let(:new_build) { service.execute(build) }
|
2017-02-10 10:19:29 -05:00
|
|
|
|
2017-02-14 04:38:17 -05:00
|
|
|
context 'when user has ability to execute build' do
|
2017-02-10 10:19:29 -05:00
|
|
|
before do
|
2017-02-15 05:12:07 -05:00
|
|
|
project.add_developer(user)
|
2017-02-10 10:19:29 -05:00
|
|
|
end
|
|
|
|
|
2017-02-15 10:04:05 -05:00
|
|
|
it_behaves_like 'build duplication'
|
|
|
|
|
2017-02-10 10:19:29 -05: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 07:01:52 -05:00
|
|
|
expect(MergeRequests::AddTodoWhenBuildFailsService)
|
2017-02-10 10:19:29 -05:00
|
|
|
.to receive_message_chain(:new, :close)
|
|
|
|
|
2017-02-14 04:38:17 -05:00
|
|
|
service.execute(build)
|
2017-02-10 10:19:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are subsequent builds that are skipped' do
|
|
|
|
let!(:subsequent_build) do
|
|
|
|
create(:ci_build, :skipped, stage_idx: 1, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resumes pipeline processing in subsequent stages' do
|
2017-02-14 04:38:17 -05:00
|
|
|
service.execute(build)
|
2017-02-10 10:19:29 -05:00
|
|
|
|
|
|
|
expect(subsequent_build.reload).to be_created
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-14 04:38:17 -05:00
|
|
|
context 'when user does not have ability to execute build' do
|
2017-02-10 10:19:29 -05:00
|
|
|
it 'raises an error' do
|
2017-02-14 04:38:17 -05:00
|
|
|
expect { service.execute(build) }
|
2017-02-10 10:19:29 -05:00
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-14 09:22:49 -05:00
|
|
|
|
|
|
|
describe '#reprocess' do
|
|
|
|
let(:new_build) { service.reprocess(build) }
|
|
|
|
|
|
|
|
context 'when user has ability to execute build' do
|
|
|
|
before do
|
2017-02-15 05:12:07 -05:00
|
|
|
project.add_developer(user)
|
2017-02-14 09:22:49 -05:00
|
|
|
end
|
|
|
|
|
2017-02-15 10:04:05 -05:00
|
|
|
it_behaves_like 'build duplication'
|
|
|
|
|
2017-02-14 09:22:49 -05: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
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does not have ability to execute build' do
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { service.reprocess(build) }
|
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-10 10:19:29 -05:00
|
|
|
end
|