2021-04-17 20:07:23 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
RSpec.describe Ci::JobArtifacts::DestroyBatchService do
|
2022-01-26 12:08:38 +05:30
|
|
|
let(:artifacts) { Ci::JobArtifact.where(id: [artifact_with_file.id, artifact_without_file.id]) }
|
2021-04-17 20:07:23 +05:30
|
|
|
let(:service) { described_class.new(artifacts, pick_up_at: Time.current) }
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
let_it_be(:artifact_with_file, refind: true) do
|
|
|
|
create(:ci_job_artifact, :zip)
|
|
|
|
end
|
|
|
|
|
|
|
|
let_it_be(:artifact_without_file, refind: true) do
|
|
|
|
create(:ci_job_artifact)
|
|
|
|
end
|
|
|
|
|
|
|
|
let_it_be(:undeleted_artifact, refind: true) do
|
|
|
|
create(:ci_job_artifact)
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
describe '.execute' do
|
|
|
|
subject(:execute) { service.execute }
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
it 'creates a deleted object for artifact with attached file' do
|
|
|
|
expect { subject }.to change { Ci::DeletedObject.count }.by(1)
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
it 'does not remove the attached file' do
|
|
|
|
expect { execute }.not_to change { artifact_with_file.file.exists? }
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
it 'deletes the artifact records' do
|
|
|
|
expect { subject }.to change { Ci::JobArtifact.count }.by(-2)
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
it 'reports metrics for destroyed artifacts' do
|
|
|
|
expect_next_instance_of(Gitlab::Ci::Artifacts::Metrics) do |metrics|
|
|
|
|
expect(metrics).to receive(:increment_destroyed_artifacts_count).with(2).and_call_original
|
|
|
|
expect(metrics).to receive(:increment_destroyed_artifacts_bytes).with(107464).and_call_original
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
execute
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'ProjectStatistics' do
|
|
|
|
it 'resets project statistics' do
|
|
|
|
expect(ProjectStatistics).to receive(:increment_statistic).once
|
|
|
|
.with(artifact_with_file.project, :build_artifacts_size, -artifact_with_file.file.size)
|
|
|
|
.and_call_original
|
|
|
|
expect(ProjectStatistics).to receive(:increment_statistic).once
|
|
|
|
.with(artifact_without_file.project, :build_artifacts_size, 0)
|
|
|
|
.and_call_original
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
execute
|
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
context 'with update_stats: false' do
|
|
|
|
it 'does not update project statistics' do
|
|
|
|
expect(ProjectStatistics).not_to receive(:increment_statistic)
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
service.execute(update_stats: false)
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
it 'returns size statistics' do
|
|
|
|
expected_updates = {
|
|
|
|
statistics_updates: {
|
|
|
|
artifact_with_file.project => -artifact_with_file.file.size,
|
|
|
|
artifact_without_file.project => 0
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(service.execute(update_stats: false)).to match(
|
|
|
|
a_hash_including(expected_updates))
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when failed to destroy artifact' do
|
|
|
|
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 { execute }.to raise_error(ActiveRecord::RecordNotDestroyed)
|
2022-01-26 12:08:38 +05:30
|
|
|
.and not_change { Ci::JobArtifact.count }
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are no artifacts' do
|
|
|
|
let(:artifacts) { Ci::JobArtifact.none }
|
|
|
|
|
|
|
|
it 'does not raise error' do
|
|
|
|
expect { execute }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reports the number of destroyed artifacts' do
|
2021-06-08 01:23:25 +05:30
|
|
|
is_expected.to eq(destroyed_artifacts_count: 0, statistics_updates: {}, status: :success)
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|