2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
RSpec.describe Ci::JobArtifacts::DestroyAllExpiredService, :clean_gitlab_redis_shared_state do
|
2019-03-02 22:35:43 +05:30
|
|
|
include ExclusiveLeaseHelpers
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
let(:service) { described_class.new }
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
describe '.execute' do
|
|
|
|
subject { service.execute }
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
let_it_be(:locked_pipeline) { create(:ci_pipeline, :artifacts_locked) }
|
|
|
|
let_it_be(:pipeline) { create(:ci_pipeline, :unlocked) }
|
|
|
|
let_it_be(:locked_job) { create(:ci_build, :success, pipeline: locked_pipeline) }
|
|
|
|
let_it_be(:job) { create(:ci_build, :success, pipeline: pipeline) }
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'when artifact is expired' do
|
2021-12-11 22:18:48 +05:30
|
|
|
let!(:artifact) { create(:ci_job_artifact, :expired, job: job, locked: job.pipeline.locked) }
|
2021-11-11 11:23:49 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'with preloaded relationships' do
|
2020-05-24 23:13:21 +05:30
|
|
|
before do
|
2021-04-29 21:17:54 +05:30
|
|
|
stub_const("#{described_class}::LOOP_LIMIT", 1)
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
context 'with ci_destroy_unlocked_job_artifacts feature flag disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(ci_destroy_unlocked_job_artifacts: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'performs the smallest number of queries for job_artifacts' do
|
|
|
|
log = ActiveRecord::QueryRecorder.new { subject }
|
|
|
|
|
|
|
|
# SELECT expired ci_job_artifacts - 3 queries from each_batch
|
|
|
|
# PRELOAD projects, routes, project_statistics
|
|
|
|
# BEGIN
|
|
|
|
# INSERT into ci_deleted_objects
|
|
|
|
# DELETE loaded ci_job_artifacts
|
|
|
|
# DELETE security_findings -- for EE
|
|
|
|
# COMMIT
|
|
|
|
# SELECT next expired ci_job_artifacts
|
|
|
|
|
|
|
|
expect(log.count).to be_within(1).of(10)
|
|
|
|
end
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
context 'with ci_destroy_unlocked_job_artifacts feature flag enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(ci_destroy_unlocked_job_artifacts: true)
|
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
it 'performs the smallest number of queries for job_artifacts' do
|
|
|
|
log = ActiveRecord::QueryRecorder.new { subject }
|
|
|
|
expect(log.count).to be_within(1).of(8)
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
context 'with several locked-unknown artifact records' do
|
|
|
|
before do
|
|
|
|
stub_const("#{described_class}::LOOP_LIMIT", 10)
|
|
|
|
stub_const("#{described_class}::BATCH_SIZE", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:lockable_artifact_records) do
|
|
|
|
[
|
|
|
|
create(:ci_job_artifact, :metadata, :expired, locked: ::Ci::JobArtifact.lockeds[:unknown], job: locked_job),
|
|
|
|
create(:ci_job_artifact, :junit, :expired, locked: ::Ci::JobArtifact.lockeds[:unknown], job: locked_job),
|
|
|
|
create(:ci_job_artifact, :sast, :expired, locked: ::Ci::JobArtifact.lockeds[:unknown], job: locked_job),
|
|
|
|
create(:ci_job_artifact, :cobertura, :expired, locked: ::Ci::JobArtifact.lockeds[:unknown], job: locked_job),
|
|
|
|
create(:ci_job_artifact, :trace, :expired, locked: ::Ci::JobArtifact.lockeds[:unknown], job: locked_job)
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:unlockable_artifact_records) do
|
|
|
|
[
|
|
|
|
create(:ci_job_artifact, :metadata, :expired, locked: ::Ci::JobArtifact.lockeds[:unknown], job: job),
|
|
|
|
create(:ci_job_artifact, :junit, :expired, locked: ::Ci::JobArtifact.lockeds[:unknown], job: job),
|
|
|
|
create(:ci_job_artifact, :sast, :expired, locked: ::Ci::JobArtifact.lockeds[:unknown], job: job),
|
|
|
|
create(:ci_job_artifact, :cobertura, :expired, locked: ::Ci::JobArtifact.lockeds[:unknown], job: job),
|
|
|
|
create(:ci_job_artifact, :trace, :expired, locked: ::Ci::JobArtifact.lockeds[:unknown], job: job),
|
|
|
|
artifact
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the locked status of job artifacts from artifacts-locked pipelines' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(lockable_artifact_records).to be_all(&:persisted?)
|
|
|
|
expect(lockable_artifact_records).to be_all { |artifact| artifact.reload.artifact_artifacts_locked? }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'unlocks and then destroys job artifacts from artifacts-unlocked pipelines' do
|
|
|
|
expect { subject }.to change { Ci::JobArtifact.count }.by(-6)
|
|
|
|
expect(Ci::JobArtifact.where(id: unlockable_artifact_records.map(&:id))).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when artifact is not locked' do
|
|
|
|
it 'deletes job artifact record' do
|
2020-05-24 23:13:21 +05:30
|
|
|
expect { subject }.to change { Ci::JobArtifact.count }.by(-1)
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
context 'when the artifact does not have a file attached to it' do
|
2021-01-29 00:20:46 +05:30
|
|
|
it 'does not create deleted objects' do
|
|
|
|
expect(artifact.exists?).to be_falsy # sanity check
|
|
|
|
|
|
|
|
expect { subject }.not_to change { Ci::DeletedObject.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the artifact has a file attached to it' do
|
2021-12-11 22:18:48 +05:30
|
|
|
let!(:artifact) { create(:ci_job_artifact, :expired, :zip, job: job, locked: job.pipeline.locked) }
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
it 'creates a deleted object' do
|
|
|
|
expect { subject }.to change { Ci::DeletedObject.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets project statistics' do
|
|
|
|
expect(ProjectStatistics).to receive(:increment_statistic).once
|
|
|
|
.with(artifact.project, :build_artifacts_size, -artifact.file.size)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not remove the files' do
|
|
|
|
expect { subject }.not_to change { artifact.file.exists? }
|
|
|
|
end
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when artifact is locked' do
|
2021-12-11 22:18:48 +05:30
|
|
|
let!(:artifact) { create(:ci_job_artifact, :expired, job: locked_job, locked: locked_job.pipeline.locked) }
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
it 'does not destroy job artifact' do
|
|
|
|
expect { subject }.not_to change { Ci::JobArtifact.count }
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when artifact is not expired' do
|
2021-12-11 22:18:48 +05:30
|
|
|
let!(:artifact) { create(:ci_job_artifact, job: job, locked: job.pipeline.locked) }
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
it 'does not destroy expired job artifacts' do
|
|
|
|
expect { subject }.not_to change { Ci::JobArtifact.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when artifact is permanent' do
|
2021-12-11 22:18:48 +05:30
|
|
|
let!(:artifact) { create(:ci_job_artifact, expire_at: nil, job: job, locked: job.pipeline.locked) }
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
it 'does not destroy expired job artifacts' do
|
|
|
|
expect { subject }.not_to change { Ci::JobArtifact.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when failed to destroy artifact' do
|
2021-12-11 22:18:48 +05:30
|
|
|
let!(:artifact) { create(:ci_job_artifact, :expired, job: job, locked: job.pipeline.locked) }
|
2021-11-11 11:23:49 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
before do
|
2021-04-29 21:17:54 +05:30
|
|
|
stub_const("#{described_class}::LOOP_LIMIT", 10)
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'when the import fails' do
|
|
|
|
before do
|
|
|
|
expect(Ci::DeletedObject)
|
|
|
|
.to receive(:bulk_import)
|
|
|
|
.once
|
|
|
|
.and_raise(ActiveRecord::RecordNotDestroyed)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an exception and stop destroying' do
|
|
|
|
expect { subject }.to raise_error(ActiveRecord::RecordNotDestroyed)
|
|
|
|
.and not_change { Ci::JobArtifact.count }.from(1)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'when the delete fails' do
|
|
|
|
before do
|
|
|
|
expect(Ci::JobArtifact)
|
|
|
|
.to receive(:id_in)
|
|
|
|
.once
|
|
|
|
.and_raise(ActiveRecord::RecordNotDestroyed)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an exception rolls back the insert' do
|
|
|
|
expect { subject }.to raise_error(ActiveRecord::RecordNotDestroyed)
|
|
|
|
.and not_change { Ci::DeletedObject.count }.from(0)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when exclusive lease has already been taken by the other instance' do
|
2021-12-11 22:18:48 +05:30
|
|
|
let!(:artifact) { create(:ci_job_artifact, :expired, job: job, locked: job.pipeline.locked) }
|
2021-11-11 11:23:49 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
before do
|
|
|
|
stub_exclusive_lease_taken(described_class::EXCLUSIVE_LOCK_KEY, timeout: described_class::LOCK_TIMEOUT)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error and does not start destroying' do
|
|
|
|
expect { subject }.to raise_error(Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError)
|
2021-11-11 11:23:49 +05:30
|
|
|
.and not_change { Ci::JobArtifact.count }.from(1)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
context 'with a second artifact and batch size of 1' do
|
|
|
|
let(:second_job) { create(:ci_build, :success, pipeline: pipeline) }
|
2021-12-11 22:18:48 +05:30
|
|
|
let!(:second_artifact) { create(:ci_job_artifact, :archive, expire_at: 1.day.ago, job: second_job, locked: job.pipeline.locked) }
|
|
|
|
let!(:artifact) { create(:ci_job_artifact, :expired, job: job, locked: job.pipeline.locked) }
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
before do
|
2021-04-29 21:17:54 +05:30
|
|
|
stub_const("#{described_class}::BATCH_SIZE", 1)
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
context 'when timeout happens' do
|
|
|
|
before do
|
|
|
|
stub_const("#{described_class}::LOOP_TIMEOUT", 0.seconds)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
it 'destroys one artifact' do
|
|
|
|
expect { subject }.to change { Ci::JobArtifact.count }.by(-1)
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
it 'reports the number of destroyed artifacts' do
|
|
|
|
is_expected.to eq(1)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
context 'when loop reached loop limit' do
|
|
|
|
before do
|
|
|
|
stub_const("#{described_class}::LOOP_LIMIT", 1)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
it 'destroys one artifact' do
|
|
|
|
expect { subject }.to change { Ci::JobArtifact.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reports the number of destroyed artifacts' do
|
|
|
|
is_expected.to eq(1)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
context 'when the number of artifacts is greater than than batch size' do
|
|
|
|
it 'destroys all expired artifacts' do
|
|
|
|
expect { subject }.to change { Ci::JobArtifact.count }.by(-2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reports the number of destroyed artifacts' do
|
|
|
|
is_expected.to eq(2)
|
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are no artifacts' do
|
|
|
|
it 'does not raise error' do
|
|
|
|
expect { subject }.not_to raise_error
|
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
it 'reports the number of destroyed artifacts' do
|
|
|
|
is_expected.to eq(0)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
context 'when some artifacts are locked' do
|
2021-12-11 22:18:48 +05:30
|
|
|
let!(:artifact) { create(:ci_job_artifact, :expired, job: job, locked: job.pipeline.locked) }
|
|
|
|
let!(:locked_artifact) { create(:ci_job_artifact, :expired, job: locked_job, locked: locked_job.pipeline.locked) }
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
it 'destroys only unlocked artifacts' do
|
|
|
|
expect { subject }.to change { Ci::JobArtifact.count }.by(-1)
|
2021-11-11 11:23:49 +05:30
|
|
|
expect(locked_artifact).to be_persisted
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
context 'when all artifacts are locked' do
|
2021-12-11 22:18:48 +05:30
|
|
|
let!(:artifact) { create(:ci_job_artifact, :expired, job: locked_job, locked: locked_job.pipeline.locked) }
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
it 'destroys no artifacts' do
|
|
|
|
expect { subject }.to change { Ci::JobArtifact.count }.by(0)
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|