diff options
Diffstat (limited to 'spec/services/ci/pipeline_artifacts/destroy_all_expired_service_spec.rb')
-rw-r--r-- | spec/services/ci/pipeline_artifacts/destroy_all_expired_service_spec.rb | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/spec/services/ci/pipeline_artifacts/destroy_all_expired_service_spec.rb b/spec/services/ci/pipeline_artifacts/destroy_all_expired_service_spec.rb new file mode 100644 index 00000000000..3dc4f35df22 --- /dev/null +++ b/spec/services/ci/pipeline_artifacts/destroy_all_expired_service_spec.rb @@ -0,0 +1,81 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Ci::PipelineArtifacts::DestroyAllExpiredService do + let(:service) { described_class.new } + + describe '.execute' do + subject { service.execute } + + context 'when timeout happens' do + before do + stub_const('Ci::PipelineArtifacts::DestroyAllExpiredService::LOOP_TIMEOUT', 0.1.seconds) + allow(service).to receive(:destroy_artifacts_batch) { true } + end + + it 'returns 0 and does not continue destroying' do + is_expected.to eq(0) + end + end + + context 'when there are no artifacts' do + it 'does not raise error' do + expect { subject }.not_to raise_error + end + end + + context 'when the loop limit is reached' do + before do + stub_const('::Ci::PipelineArtifacts::DestroyAllExpiredService::LOOP_LIMIT', 1) + stub_const('::Ci::PipelineArtifacts::DestroyAllExpiredService::BATCH_SIZE', 1) + + create_list(:ci_pipeline_artifact, 2, expire_at: 1.week.ago) + end + + it 'destroys one artifact' do + expect { subject }.to change { Ci::PipelineArtifact.count }.by(-1) + end + + it 'reports the number of destroyed artifacts' do + is_expected.to eq(1) + end + end + + context 'when there are artifacts more than batch sizes' do + before do + stub_const('Ci::PipelineArtifacts::DestroyAllExpiredService::BATCH_SIZE', 1) + + create_list(:ci_pipeline_artifact, 2, expire_at: 1.week.ago) + end + + it 'destroys all expired artifacts' do + expect { subject }.to change { Ci::PipelineArtifact.count }.by(-2) + end + + it 'reports the number of destroyed artifacts' do + is_expected.to eq(2) + end + end + + context 'when artifacts are not expired' do + before do + create(:ci_pipeline_artifact, expire_at: 2.days.from_now) + end + + it 'does not destroy pipeline artifacts' do + expect { subject }.not_to change { Ci::PipelineArtifact.count } + end + + it 'reports the number of destroyed artifacts' do + is_expected.to eq(0) + end + end + end + + describe '.destroy_artifacts_batch' do + it 'returns a falsy value without artifacts' do + expect(service.send(:destroy_artifacts_batch)).to be_falsy + end + end +end |