debian-mirror-gitlab/spec/services/ci/retry_build_service_spec.rb

227 lines
7.1 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Ci::RetryBuildService do
2018-03-17 18:26:18 +05:30
set(:user) { create(:user) }
set(:project) { create(:project) }
set(:pipeline) { create(:ci_pipeline, project: project) }
let(:stage) do
2018-10-15 14:42:47 +05:30
create(:ci_stage_entity, project: project,
pipeline: pipeline,
name: 'test')
2018-03-17 18:26:18 +05:30
end
let(:build) { create(:ci_build, pipeline: pipeline, stage_id: stage.id) }
2017-08-17 22:00:37 +05:30
let(:service) do
described_class.new(project, user)
end
CLONE_ACCESSORS = described_class::CLONE_ACCESSORS
REJECT_ACCESSORS =
2019-02-15 15:39:39 +05:30
%i[id status user token token_encrypted coverage trace runner
2019-09-04 21:01:54 +05:30
artifacts_expire_at
2019-02-15 15:39:39 +05:30
created_at updated_at started_at finished_at queued_at erased_by
2018-03-17 18:26:18 +05:30
erased_at auto_canceled_by job_artifacts job_artifacts_archive
2018-12-05 23:21:45 +05:30
job_artifacts_metadata job_artifacts_trace job_artifacts_junit
job_artifacts_sast job_artifacts_dependency_scanning
job_artifacts_container_scanning job_artifacts_dast
2018-12-13 13:39:08 +05:30
job_artifacts_license_management job_artifacts_performance
2019-10-12 21:52:04 +05:30
job_artifacts_codequality job_artifacts_metrics scheduled_at
job_variables].freeze
2017-08-17 22:00:37 +05:30
IGNORE_ACCESSORS =
2018-03-17 18:26:18 +05:30
%i[type lock_version target_url base_tags trace_sections
2018-12-13 13:39:08 +05:30
commit_id deployment erased_by_id project_id
2017-08-17 22:00:37 +05:30
runner_id tag_taggings taggings tags trigger_request_id
2018-05-09 12:01:36 +05:30
user_id auto_canceled_by_id retried failure_reason
2019-07-31 22:56:46 +05:30
sourced_pipelines artifacts_file_store artifacts_metadata_store
2019-09-04 21:01:54 +05:30
metadata runner_session trace_chunks upstream_pipeline_id
2019-12-04 20:38:33 +05:30
artifacts_file artifacts_metadata artifacts_size commands].freeze
2017-08-17 22:00:37 +05:30
shared_examples 'build duplication' do
2018-03-17 18:26:18 +05:30
let(:another_pipeline) { create(:ci_empty_pipeline, project: project) }
2017-09-10 17:25:29 +05:30
2017-08-17 22:00:37 +05:30
let(:build) do
2018-12-05 23:21:45 +05:30
create(:ci_build, :failed, :expired, :erased, :queued, :coverage, :tags,
:allowed_to_fail, :on_tag, :triggered, :teardown_environment,
2018-03-17 18:26:18 +05:30
description: 'my-job', stage: 'test', stage_id: stage.id,
2018-12-05 23:21:45 +05:30
pipeline: pipeline, auto_canceled_by: another_pipeline,
scheduled_at: 10.seconds.since)
2018-03-17 18:26:18 +05:30
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.
2018-11-18 11:00:15 +05:30
build.update(stage: 'test', stage_id: stage.id)
2018-12-05 23:21:45 +05:30
# Make sure we have one instance for every possible job_artifact_X
# associations to check they are correctly rejected on build duplication.
Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS.each do |file_type, file_format|
create(:ci_job_artifact, file_format,
file_type: file_type, job: build, expire_at: build.artifacts_expire_at)
end
2019-10-12 21:52:04 +05:30
create(:ci_job_variable, job: build)
create(:ci_build_need, build: build)
2018-12-05 23:21:45 +05:30
build.reload
2017-08-17 22:00:37 +05:30
end
describe 'clone accessors' do
CLONE_ACCESSORS.each do |attribute|
it "clones #{attribute} build attribute" do
2018-03-17 18:26:18 +05:30
expect(build.send(attribute)).not_to be_nil
expect(new_build.send(attribute)).not_to be_nil
2017-08-17 22:00:37 +05:30
expect(new_build.send(attribute)).to eq build.send(attribute)
end
end
2018-03-17 18:26:18 +05:30
context 'when job has nullified protected' do
before do
build.update_attribute(:protected, nil)
end
it "clones protected build attribute" do
expect(new_build.protected).to be_nil
expect(new_build.protected).to eq build.protected
end
end
2017-08-17 22:00:37 +05:30
end
describe 'reject acessors' do
REJECT_ACCESSORS.each do |attribute|
it "does not clone #{attribute} build attribute" do
expect(new_build.send(attribute)).not_to eq build.send(attribute)
end
end
end
it 'has correct number of known attributes' do
2019-07-31 22:56:46 +05:30
processed_accessors = CLONE_ACCESSORS + REJECT_ACCESSORS
known_accessors = processed_accessors + IGNORE_ACCESSORS
2017-08-17 22:00:37 +05:30
# :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 =
Ci::Build.attribute_names.map(&:to_sym) +
Ci::Build.reflect_on_all_associations.map(&:name) +
[:tag_list]
current_accessors.uniq!
2019-07-31 22:56:46 +05:30
expect(current_accessors).to include(*processed_accessors)
expect(known_accessors).to include(*current_accessors)
2017-08-17 22:00:37 +05:30
end
end
describe '#execute' do
2018-11-18 11:00:15 +05:30
let(:new_build) do
Timecop.freeze(1.second.from_now) do
service.execute(build)
end
end
2017-08-17 22:00:37 +05:30
context 'when user has ability to execute build' do
before do
2017-09-10 17:25:29 +05:30
stub_not_protect_default_branch
2017-08-17 22:00:37 +05:30
project.add_developer(user)
end
it_behaves_like 'build duplication'
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
expect(MergeRequests::AddTodoWhenBuildFailsService)
.to receive_message_chain(:new, :close)
service.execute(build)
end
context 'when there are subsequent builds that are skipped' do
let!(:subsequent_build) do
2018-03-17 18:26:18 +05:30
create(:ci_build, :skipped, stage_idx: 2,
pipeline: pipeline,
stage: 'deploy')
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
it 'resumes pipeline processing in a subsequent stage' do
2017-08-17 22:00:37 +05:30
service.execute(build)
expect(subsequent_build.reload).to be_created
end
end
end
context 'when user does not have ability to execute build' do
it 'raises an error' do
expect { service.execute(build) }
.to raise_error Gitlab::Access::AccessDeniedError
end
end
end
describe '#reprocess' do
2018-11-18 11:00:15 +05:30
let(:new_build) do
Timecop.freeze(1.second.from_now) do
service.reprocess!(build)
end
end
2017-08-17 22:00:37 +05:30
context 'when user has ability to execute build' do
before do
2017-09-10 17:25:29 +05:30
stub_not_protect_default_branch
2017-08-17 22:00:37 +05:30
project.add_developer(user)
end
it_behaves_like 'build duplication'
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
2018-03-17 18:26:18 +05:30
it 'does mark old build as retried in the database and on the instance' do
2017-08-17 22:00:37 +05:30
expect(new_build).to be_latest
2018-03-17 18:26:18 +05:30
expect(build).to be_retried
2017-08-17 22:00:37 +05:30
expect(build.reload).to be_retried
end
2019-12-21 20:55:43 +05:30
context 'when build with deployment is retried' do
let!(:build) do
create(:ci_build, :with_deployment, :deploy_to_production, pipeline: pipeline, stage_id: stage.id)
end
it 'creates a new deployment' do
expect { new_build }.to change { Deployment.count }.by(1)
end
end
2017-08-17 22:00:37 +05:30
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
end