debian-mirror-gitlab/spec/services/container_expiration_policies/cleanup_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

218 lines
8.2 KiB
Ruby
Raw Normal View History

2021-01-29 00:20:46 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ContainerExpirationPolicies::CleanupService do
2021-06-08 01:23:25 +05:30
let_it_be(:repository, reload: true) { create(:container_repository, expiration_policy_started_at: 30.minutes.ago) }
2021-01-29 00:20:46 +05:30
let_it_be(:project) { repository.project }
let(:service) { described_class.new(repository) }
describe '#execute' do
2021-09-04 01:27:46 +05:30
let(:policy) { repository.project.container_expiration_policy }
2021-01-29 00:20:46 +05:30
subject { service.execute }
2021-09-04 01:27:46 +05:30
before do
policy.update!(enabled: true)
policy.update_column(:next_run_at, 5.minutes.ago)
end
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
context 'with a successful cleanup tags service execution' do
let(:cleanup_tags_service_params) { project.container_expiration_policy.policy_params.merge('container_expiration_policy' => true) }
let(:cleanup_tags_service) { instance_double(Projects::ContainerRepository::CleanupTagsService) }
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
it 'completely clean up the repository' do
expect(Projects::ContainerRepository::CleanupTagsService)
2021-11-18 22:05:49 +05:30
.to receive(:new).with(repository, nil, cleanup_tags_service_params).and_return(cleanup_tags_service)
2022-07-16 23:28:13 +05:30
expect(cleanup_tags_service).to receive(:execute).and_return(status: :success, deleted_size: 1)
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
response = subject
aggregate_failures "checking the response and container repositories" do
expect(response.success?).to eq(true)
expect(response.payload).to include(cleanup_status: :finished, container_repository_id: repository.id)
expect(ContainerRepository.waiting_for_cleanup.count).to eq(0)
expect(repository.reload.cleanup_unscheduled?).to be_truthy
expect(repository.expiration_policy_completed_at).not_to eq(nil)
expect(repository.expiration_policy_started_at).not_to eq(nil)
2022-07-16 23:28:13 +05:30
expect(repository.last_cleanup_deleted_tags_count).to eq(1)
2021-01-29 00:20:46 +05:30
end
end
2021-09-04 01:27:46 +05:30
end
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
context 'without a successful cleanup tags service execution' do
let(:cleanup_tags_service_response) { { status: :error, message: 'timeout' } }
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
before do
expect(Projects::ContainerRepository::CleanupTagsService)
.to receive(:new).and_return(double(execute: cleanup_tags_service_response))
end
it 'partially clean up the repository' do
response = subject
aggregate_failures "checking the response and container repositories" do
expect(response.success?).to eq(true)
expect(response.payload).to include(cleanup_status: :unfinished, container_repository_id: repository.id)
expect(ContainerRepository.waiting_for_cleanup.count).to eq(1)
expect(repository.reload.cleanup_unfinished?).to be_truthy
expect(repository.expiration_policy_started_at).not_to eq(nil)
expect(repository.expiration_policy_completed_at).to eq(nil)
2022-07-16 23:28:13 +05:30
expect(repository.last_cleanup_deleted_tags_count).to eq(nil)
2021-09-04 01:27:46 +05:30
end
end
context 'with a truncated cleanup tags service response' do
let(:cleanup_tags_service_response) do
{
status: :error,
original_size: 1000,
before_truncate_size: 800,
after_truncate_size: 200,
before_delete_size: 100,
2021-11-18 22:05:49 +05:30
cached_tags_count: 0,
2021-09-04 01:27:46 +05:30
deleted_size: 100
}
2021-03-08 18:12:59 +05:30
end
it 'partially clean up the repository' do
response = subject
aggregate_failures "checking the response and container repositories" do
expect(response.success?).to eq(true)
2021-09-04 01:27:46 +05:30
expect(response.payload)
.to include(
cleanup_status: :unfinished,
container_repository_id: repository.id,
cleanup_tags_service_original_size: 1000,
cleanup_tags_service_before_truncate_size: 800,
cleanup_tags_service_after_truncate_size: 200,
cleanup_tags_service_before_delete_size: 100,
2021-11-18 22:05:49 +05:30
cleanup_tags_service_cached_tags_count: 0,
2021-09-04 01:27:46 +05:30
cleanup_tags_service_deleted_size: 100
)
2021-03-08 18:12:59 +05:30
expect(ContainerRepository.waiting_for_cleanup.count).to eq(1)
expect(repository.reload.cleanup_unfinished?).to be_truthy
expect(repository.expiration_policy_started_at).not_to eq(nil)
expect(repository.expiration_policy_completed_at).to eq(nil)
2022-07-16 23:28:13 +05:30
expect(repository.last_cleanup_deleted_tags_count).to eq(nil)
2021-03-08 18:12:59 +05:30
end
end
end
2021-09-04 01:27:46 +05:30
end
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
context 'with no repository' do
let(:service) { described_class.new(nil) }
2021-06-08 01:23:25 +05:30
2021-09-04 01:27:46 +05:30
it 'returns an error response' do
expect(subject.success?).to eq(false)
expect(subject.message).to eq('no repository')
2021-06-08 01:23:25 +05:30
end
2021-09-04 01:27:46 +05:30
end
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
context 'with an invalid policy' do
let(:policy) { repository.project.container_expiration_policy }
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
before do
policy.name_regex = nil
policy.enabled = true
repository.expiration_policy_cleanup_status = :cleanup_ongoing
end
2021-06-08 01:23:25 +05:30
2021-09-04 01:27:46 +05:30
it 'returns an error response' do
expect { subject }.to change { repository.expiration_policy_cleanup_status }.from('cleanup_ongoing').to('cleanup_unscheduled')
expect(subject.success?).to eq(false)
expect(subject.message).to eq('invalid policy')
expect(policy).not_to be_enabled
2021-06-08 01:23:25 +05:30
end
2021-09-04 01:27:46 +05:30
end
2021-06-08 01:23:25 +05:30
2021-09-04 01:27:46 +05:30
context 'with a network error' do
before do
expect(Projects::ContainerRepository::CleanupTagsService)
.to receive(:new).and_raise(Faraday::TimeoutError)
end
2021-06-08 01:23:25 +05:30
2021-09-04 01:27:46 +05:30
it 'raises an error' do
expect { subject }.to raise_error(Faraday::TimeoutError)
2021-06-08 01:23:25 +05:30
2021-09-04 01:27:46 +05:30
expect(ContainerRepository.waiting_for_cleanup.count).to eq(1)
expect(repository.reload.cleanup_unfinished?).to be_truthy
expect(repository.expiration_policy_started_at).not_to eq(nil)
expect(repository.expiration_policy_completed_at).to eq(nil)
2022-07-16 23:28:13 +05:30
expect(repository.last_cleanup_deleted_tags_count).to eq(nil)
2021-01-29 00:20:46 +05:30
end
end
2021-03-11 19:13:27 +05:30
2021-09-04 01:27:46 +05:30
context 'next run scheduling' do
let_it_be_with_reload(:repository2) { create(:container_repository, project: project) }
let_it_be_with_reload(:repository3) { create(:container_repository, project: project) }
2021-06-08 01:23:25 +05:30
2021-03-11 19:13:27 +05:30
before do
2021-09-04 01:27:46 +05:30
cleanup_tags_service = instance_double(Projects::ContainerRepository::CleanupTagsService)
allow(Projects::ContainerRepository::CleanupTagsService)
.to receive(:new).and_return(cleanup_tags_service)
allow(cleanup_tags_service).to receive(:execute).and_return(status: :success)
2021-03-11 19:13:27 +05:30
end
2021-09-04 01:27:46 +05:30
shared_examples 'not scheduling the next run' do
it 'does not scheduled the next run' do
expect(policy).not_to receive(:schedule_next_run!)
2021-06-08 01:23:25 +05:30
2021-09-04 01:27:46 +05:30
expect { subject }.not_to change { policy.reload.next_run_at }
2021-06-08 01:23:25 +05:30
end
2021-09-04 01:27:46 +05:30
end
2021-06-08 01:23:25 +05:30
2021-09-04 01:27:46 +05:30
shared_examples 'scheduling the next run' do
it 'schedules the next run' do
expect(policy).to receive(:schedule_next_run!).and_call_original
2021-06-08 01:23:25 +05:30
2021-09-04 01:27:46 +05:30
expect { subject }.to change { policy.reload.next_run_at }
2021-06-08 01:23:25 +05:30
end
2021-09-04 01:27:46 +05:30
end
2021-06-08 01:23:25 +05:30
2021-09-04 01:27:46 +05:30
context 'with cleanups started_at before policy next_run_at' do
before do
ContainerRepository.update_all(expiration_policy_started_at: 10.minutes.ago)
2021-06-08 01:23:25 +05:30
end
2021-09-04 01:27:46 +05:30
it_behaves_like 'not scheduling the next run'
end
2021-03-11 19:13:27 +05:30
2021-09-04 01:27:46 +05:30
context 'with cleanups started_at around policy next_run_at' do
before do
repository3.update!(expiration_policy_started_at: policy.next_run_at + 10.minutes.ago)
2021-06-08 01:23:25 +05:30
end
2021-09-04 01:27:46 +05:30
it_behaves_like 'not scheduling the next run'
end
2021-06-08 01:23:25 +05:30
2021-09-04 01:27:46 +05:30
context 'with only the current repository started_at before the policy next_run_at' do
before do
repository2.update!(expiration_policy_started_at: policy.next_run_at + 10.minutes)
repository3.update!(expiration_policy_started_at: policy.next_run_at + 12.minutes)
2021-06-08 01:23:25 +05:30
end
2021-09-04 01:27:46 +05:30
it_behaves_like 'scheduling the next run'
end
2021-06-08 01:23:25 +05:30
2021-09-04 01:27:46 +05:30
context 'with cleanups started_at after policy next_run_at' do
before do
ContainerRepository.update_all(expiration_policy_started_at: policy.next_run_at + 10.minutes)
2021-06-08 01:23:25 +05:30
end
2021-09-04 01:27:46 +05:30
it_behaves_like 'scheduling the next run'
end
2021-06-08 01:23:25 +05:30
2021-09-04 01:27:46 +05:30
context 'with a future policy next_run_at' do
before do
policy.update_column(:next_run_at, 5.minutes.from_now)
2021-06-08 01:23:25 +05:30
end
2021-09-04 01:27:46 +05:30
it_behaves_like 'not scheduling the next run'
2021-06-08 01:23:25 +05:30
end
end
2021-01-29 00:20:46 +05:30
end
end