2019-04-11 12:17:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-17 06:06:37 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 06:09:01 +00:00
|
|
|
RSpec.describe Ci::DestroyExpiredJobArtifactsService, :clean_gitlab_redis_shared_state do
|
2019-01-17 06:06:37 +00:00
|
|
|
include ExclusiveLeaseHelpers
|
|
|
|
|
|
|
|
describe '.execute' do
|
|
|
|
subject { service.execute }
|
|
|
|
|
|
|
|
let(:service) { described_class.new }
|
|
|
|
|
2020-10-08 06:08:35 +00:00
|
|
|
let_it_be(:artifact) { create(:ci_job_artifact, expire_at: 1.day.ago) }
|
|
|
|
|
|
|
|
before(:all) do
|
2020-09-01 18:10:48 +00:00
|
|
|
artifact.job.pipeline.unlocked!
|
|
|
|
end
|
|
|
|
|
2020-04-27 21:10:10 +00:00
|
|
|
context 'when artifact is expired' do
|
|
|
|
context 'when artifact is not locked' do
|
|
|
|
before do
|
2020-07-14 12:09:14 +00:00
|
|
|
artifact.job.pipeline.unlocked!
|
2020-04-27 21:10:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'destroys job artifact' do
|
|
|
|
expect { subject }.to change { Ci::JobArtifact.count }.by(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when artifact is locked' do
|
|
|
|
before do
|
2020-07-14 12:09:14 +00:00
|
|
|
artifact.job.pipeline.artifacts_locked!
|
2020-04-27 21:10:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not destroy job artifact' do
|
|
|
|
expect { subject }.not_to change { Ci::JobArtifact.count }
|
|
|
|
end
|
|
|
|
end
|
2019-01-17 06:06:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when artifact is not expired' do
|
2020-10-08 06:08:35 +00:00
|
|
|
before do
|
|
|
|
artifact.update_column(:expire_at, 1.day.since)
|
|
|
|
end
|
2019-01-17 06:06:37 +00:00
|
|
|
|
|
|
|
it 'does not destroy expired job artifacts' do
|
|
|
|
expect { subject }.not_to change { Ci::JobArtifact.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when artifact is permanent' do
|
2020-10-08 06:08:35 +00:00
|
|
|
before do
|
|
|
|
artifact.update_column(:expire_at, nil)
|
|
|
|
end
|
2019-01-17 06:06:37 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
before do
|
|
|
|
stub_const('Ci::DestroyExpiredJobArtifactsService::LOOP_LIMIT', 10)
|
|
|
|
|
|
|
|
allow_any_instance_of(Ci::JobArtifact)
|
|
|
|
.to receive(:destroy!)
|
|
|
|
.and_raise(ActiveRecord::RecordNotDestroyed)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an exception and stop destroying' do
|
|
|
|
expect { subject }.to raise_error(ActiveRecord::RecordNotDestroyed)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when exclusive lease has already been taken by the other instance' do
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when timeout happens' do
|
|
|
|
before do
|
|
|
|
stub_const('Ci::DestroyExpiredJobArtifactsService::LOOP_TIMEOUT', 1.second)
|
|
|
|
allow_any_instance_of(described_class).to receive(:destroy_batch) { true }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false and does not continue destroying' do
|
|
|
|
is_expected.to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when loop reached loop limit' do
|
|
|
|
before do
|
|
|
|
stub_const('Ci::DestroyExpiredJobArtifactsService::LOOP_LIMIT', 1)
|
|
|
|
stub_const('Ci::DestroyExpiredJobArtifactsService::BATCH_SIZE', 1)
|
2020-09-01 18:10:48 +00:00
|
|
|
|
|
|
|
second_artifact.job.pipeline.unlocked!
|
2019-01-17 06:06:37 +00:00
|
|
|
end
|
|
|
|
|
2020-04-27 21:10:10 +00:00
|
|
|
let!(:second_artifact) { create(:ci_job_artifact, expire_at: 1.day.ago) }
|
2019-01-17 06:06:37 +00:00
|
|
|
|
|
|
|
it 'raises an error and does not continue destroying' do
|
|
|
|
is_expected.to be_falsy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'destroys one artifact' do
|
|
|
|
expect { subject }.to change { Ci::JobArtifact.count }.by(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are no artifacts' do
|
2020-09-01 18:10:48 +00:00
|
|
|
before do
|
|
|
|
artifact.destroy!
|
|
|
|
end
|
2019-01-17 06:06:37 +00:00
|
|
|
|
|
|
|
it 'does not raise error' do
|
|
|
|
expect { subject }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are artifacts more than batch sizes' do
|
|
|
|
before do
|
|
|
|
stub_const('Ci::DestroyExpiredJobArtifactsService::BATCH_SIZE', 1)
|
2020-09-01 18:10:48 +00:00
|
|
|
|
|
|
|
second_artifact.job.pipeline.unlocked!
|
2019-01-17 06:06:37 +00:00
|
|
|
end
|
|
|
|
|
2020-04-27 21:10:10 +00:00
|
|
|
let!(:second_artifact) { create(:ci_job_artifact, expire_at: 1.day.ago) }
|
2019-01-17 06:06:37 +00:00
|
|
|
|
|
|
|
it 'destroys all expired artifacts' do
|
|
|
|
expect { subject }.to change { Ci::JobArtifact.count }.by(-2)
|
|
|
|
end
|
|
|
|
end
|
2020-09-01 12:11:01 +00:00
|
|
|
|
|
|
|
context 'when artifact is a pipeline artifact' do
|
|
|
|
context 'when artifacts are expired' do
|
|
|
|
let!(:pipeline_artifact_1) { create(:ci_pipeline_artifact, expire_at: 1.week.ago) }
|
|
|
|
let!(:pipeline_artifact_2) { create(:ci_pipeline_artifact, expire_at: 1.week.ago) }
|
|
|
|
|
2020-09-01 18:10:48 +00:00
|
|
|
before do
|
|
|
|
[pipeline_artifact_1, pipeline_artifact_2].each { |pipeline_artifact| pipeline_artifact.pipeline.unlocked! }
|
|
|
|
end
|
|
|
|
|
2020-09-01 12:11:01 +00:00
|
|
|
it 'destroys pipeline artifacts' do
|
|
|
|
expect { subject }.to change { Ci::PipelineArtifact.count }.by(-2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when artifacts are not expired' do
|
2020-09-03 18:08:29 +00:00
|
|
|
let!(:pipeline_artifact_1) { create(:ci_pipeline_artifact, expire_at: 2.days.from_now) }
|
|
|
|
let!(:pipeline_artifact_2) { create(:ci_pipeline_artifact, expire_at: 2.days.from_now) }
|
2020-09-01 12:11:01 +00:00
|
|
|
|
2020-09-01 18:10:48 +00:00
|
|
|
before do
|
|
|
|
[pipeline_artifact_1, pipeline_artifact_2].each { |pipeline_artifact| pipeline_artifact.pipeline.unlocked! }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not destroy pipeline artifacts' do
|
2020-09-01 12:11:01 +00:00
|
|
|
expect { subject }.not_to change { Ci::PipelineArtifact.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-01 18:10:48 +00:00
|
|
|
|
|
|
|
context 'when some artifacts are locked' do
|
|
|
|
before do
|
|
|
|
pipeline = create(:ci_pipeline, locked: :artifacts_locked)
|
|
|
|
job = create(:ci_build, pipeline: pipeline)
|
|
|
|
create(:ci_job_artifact, expire_at: 1.day.ago, job: job)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'destroys only unlocked artifacts' do
|
|
|
|
expect { subject }.to change { Ci::JobArtifact.count }.by(-1)
|
|
|
|
end
|
|
|
|
end
|
2019-01-17 06:06:37 +00:00
|
|
|
end
|
|
|
|
end
|