2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-10 10:19:29 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Ci::RetryBuildService do
|
2017-09-05 04:13:46 -04:00
|
|
|
set(:user) { create(:user) }
|
|
|
|
set(:project) { create(:project) }
|
|
|
|
set(:pipeline) { create(:ci_pipeline, project: project) }
|
|
|
|
|
2018-01-25 09:15:51 -05:00
|
|
|
let(:stage) do
|
2018-04-17 07:47:35 -04:00
|
|
|
create(:ci_stage_entity, project: project,
|
|
|
|
pipeline: pipeline,
|
|
|
|
name: 'test')
|
2018-01-25 09:15:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline, stage_id: stage.id) }
|
2017-02-10 10:19:29 -05:00
|
|
|
|
|
|
|
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 =
|
2018-12-06 04:42:18 -05:00
|
|
|
%i[id status user token token_encrypted coverage trace runner
|
2019-03-22 08:38:45 -04:00
|
|
|
artifacts_expire_at
|
2018-12-06 04:42:18 -05:00
|
|
|
created_at updated_at started_at finished_at queued_at erased_by
|
2018-02-06 09:18:32 -05:00
|
|
|
erased_at auto_canceled_by job_artifacts job_artifacts_archive
|
2018-09-27 17:15:08 -04:00
|
|
|
job_artifacts_metadata job_artifacts_trace job_artifacts_junit
|
|
|
|
job_artifacts_sast job_artifacts_dependency_scanning
|
2018-09-26 01:37:11 -04:00
|
|
|
job_artifacts_container_scanning job_artifacts_dast
|
2018-10-07 04:22:40 -04:00
|
|
|
job_artifacts_license_management job_artifacts_performance
|
2019-07-29 03:43:10 -04:00
|
|
|
job_artifacts_codequality job_artifacts_metrics scheduled_at
|
|
|
|
job_variables].freeze
|
2017-03-02 05:51:24 -05:00
|
|
|
|
|
|
|
IGNORE_ACCESSORS =
|
2017-09-25 12:54:08 -04:00
|
|
|
%i[type lock_version target_url base_tags trace_sections
|
2018-11-04 19:37:40 -05:00
|
|
|
commit_id deployment erased_by_id project_id
|
2017-03-02 05:51:24 -05:00
|
|
|
runner_id tag_taggings taggings tags trigger_request_id
|
2018-02-28 14:06:12 -05:00
|
|
|
user_id auto_canceled_by_id retried failure_reason
|
2019-04-10 06:28:01 -04:00
|
|
|
sourced_pipelines artifacts_file_store artifacts_metadata_store
|
Backport the EE schema and migrations to CE
This backports all EE schema changes to CE, including EE migrations,
ensuring both use the same schema.
== Updated tests
A spec related to ghost and support bot users had to be modified to make
it pass. The spec in question assumes that the "support_bot" column
exists when defining the spec. In the single codebase setup this is not
the case, as the column is backported in a later migration. Any attempt
to use a different schema version or use of "around" blocks to
conditionally disable specs won't help, as reverting the backport
migration would also drop the "support_bot" column. Removing the
"support_bot" tests entirely appears to be the only solution.
We also need to update some foreign key tests now that we have
backported the EE columns. Fortunately, these changes are very minor.
== Backporting migrations
This commit moves EE specific migrations (except those for the Geo
tracking database) and related files to CE, and also removes any traces
of the ee/db directory.
Some migrations had to be modified or removed, as they no longer work
with the schema being backported. These migrations were all quite old,
so we opted for removing them where modifying them would take too much
time and effort.
Some old migrations were modified in EE, while also existing in CE. In
these cases we took the EE code, and in one case removed them entirely.
It's not worth spending time trying to merge these changes somehow as we
plan to remove old migrations around the release of 12.0, see
https://gitlab.com/gitlab-org/gitlab-ce/issues/59177 for more details.
2019-04-29 08:16:03 -04:00
|
|
|
metadata runner_session trace_chunks upstream_pipeline_id
|
2019-03-22 08:38:45 -04:00
|
|
|
artifacts_file artifacts_metadata artifacts_size].freeze
|
2017-03-02 05:51:24 -05:00
|
|
|
|
2017-02-15 10:04:05 -05:00
|
|
|
shared_examples 'build duplication' do
|
2018-01-25 09:15:51 -05:00
|
|
|
let(:another_pipeline) { create(:ci_empty_pipeline, project: project) }
|
2017-06-07 04:05:40 -04:00
|
|
|
|
2017-02-15 10:04:05 -05:00
|
|
|
let(:build) do
|
2018-09-27 17:15:08 -04:00
|
|
|
create(:ci_build, :failed, :expired, :erased, :queued, :coverage, :tags,
|
|
|
|
:allowed_to_fail, :on_tag, :triggered, :teardown_environment,
|
2018-01-25 09:15:51 -05:00
|
|
|
description: 'my-job', stage: 'test', stage_id: stage.id,
|
2018-09-26 01:37:11 -04:00
|
|
|
pipeline: pipeline, auto_canceled_by: another_pipeline,
|
|
|
|
scheduled_at: 10.seconds.since)
|
2018-01-25 09:15:51 -05:00
|
|
|
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-07-02 06:43:06 -04:00
|
|
|
build.update(stage: 'test', stage_id: stage.id)
|
2018-09-27 17:15:08 -04:00
|
|
|
|
|
|
|
# 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-07-29 03:43:10 -04:00
|
|
|
create(:ci_job_variable, job: build)
|
2019-08-01 10:26:49 -04:00
|
|
|
create(:ci_build_need, build: build)
|
2019-07-29 03:43:10 -04:00
|
|
|
|
2018-09-27 17:15:08 -04:00
|
|
|
build.reload
|
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
|
2018-01-25 09:15:51 -05:00
|
|
|
expect(build.send(attribute)).not_to be_nil
|
2017-09-01 04:54:07 -04:00
|
|
|
expect(new_build.send(attribute)).not_to be_nil
|
2017-02-16 07:13:10 -05:00
|
|
|
expect(new_build.send(attribute)).to eq build.send(attribute)
|
|
|
|
end
|
|
|
|
end
|
2017-09-06 10:09:41 -04: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 12:13:49 -04:00
|
|
|
expect(new_build.protected).to be_nil
|
2017-09-06 10:09:41 -04:00
|
|
|
expect(new_build.protected).to eq build.protected
|
|
|
|
end
|
|
|
|
end
|
2017-02-16 07:13:10 -05:00
|
|
|
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
|
2019-04-10 06:28:01 -04:00
|
|
|
processed_accessors = CLONE_ACCESSORS + REJECT_ACCESSORS
|
|
|
|
known_accessors = processed_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!
|
|
|
|
|
2019-04-10 06:28:01 -04:00
|
|
|
expect(current_accessors).to include(*processed_accessors)
|
|
|
|
expect(known_accessors).to include(*current_accessors)
|
2017-02-15 10:04:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-14 04:38:17 -05:00
|
|
|
describe '#execute' do
|
2018-07-07 07:46:23 -04:00
|
|
|
let(:new_build) do
|
|
|
|
Timecop.freeze(1.second.from_now) do
|
|
|
|
service.execute(build)
|
|
|
|
end
|
|
|
|
end
|
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-07-18 10:32:34 -04:00
|
|
|
stub_not_protect_default_branch
|
|
|
|
|
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
|
2018-01-25 09:15:51 -05:00
|
|
|
create(:ci_build, :skipped, stage_idx: 2,
|
|
|
|
pipeline: pipeline,
|
|
|
|
stage: 'deploy')
|
2017-02-10 10:19:29 -05:00
|
|
|
end
|
|
|
|
|
2018-01-25 09:15:51 -05:00
|
|
|
it 'resumes pipeline processing in a subsequent stage' 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
|
2018-07-07 07:46:23 -04:00
|
|
|
let(:new_build) do
|
|
|
|
Timecop.freeze(1.second.from_now) do
|
|
|
|
service.reprocess!(build)
|
|
|
|
end
|
|
|
|
end
|
2017-02-14 09:22:49 -05:00
|
|
|
|
|
|
|
context 'when user has ability to execute build' do
|
|
|
|
before do
|
2017-07-18 10:32:34 -04:00
|
|
|
stub_not_protect_default_branch
|
|
|
|
|
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
|
2017-04-16 07:14:39 -04:00
|
|
|
|
2017-10-13 11:26:17 -04:00
|
|
|
it 'does mark old build as retried in the database and on the instance' do
|
2017-04-16 07:14:39 -04:00
|
|
|
expect(new_build).to be_latest
|
2017-10-13 11:26:17 -04:00
|
|
|
expect(build).to be_retried
|
2017-04-16 07:14:39 -04:00
|
|
|
expect(build.reload).to be_retried
|
|
|
|
end
|
2017-02-14 09:22:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does not have ability to execute build' do
|
|
|
|
it 'raises an error' do
|
2017-04-16 07:14:39 -04:00
|
|
|
expect { service.reprocess!(build) }
|
2017-02-14 09:22:49 -05:00
|
|
|
.to raise_error Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-10 10:19:29 -05:00
|
|
|
end
|