2020-10-26 18:08:27 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe ContainerExpirationPolicies::CleanupService do
|
|
|
|
let_it_be(:repository, reload: true) { create(:container_repository) }
|
|
|
|
let_it_be(:project) { repository.project }
|
|
|
|
|
|
|
|
let(:service) { described_class.new(repository) }
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
subject { service.execute }
|
|
|
|
|
|
|
|
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) }
|
|
|
|
|
|
|
|
it 'completely clean up the repository' do
|
|
|
|
expect(Projects::ContainerRepository::CleanupTagsService)
|
2021-01-11 15:10:32 +00:00
|
|
|
.to receive(:new).with(project, nil, cleanup_tags_service_params).and_return(cleanup_tags_service)
|
2020-10-26 18:08:27 +00:00
|
|
|
expect(cleanup_tags_service).to receive(:execute).with(repository).and_return(status: :success)
|
|
|
|
|
|
|
|
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_started_at).to eq(nil)
|
2020-12-15 12:10:17 +00:00
|
|
|
expect(repository.expiration_policy_completed_at).not_to eq(nil)
|
2020-10-26 18:08:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without a successful cleanup tags service execution' do
|
2021-01-11 15:10:32 +00:00
|
|
|
let(:cleanup_tags_service_response) { { status: :error, message: 'timeout' } }
|
|
|
|
|
|
|
|
before do
|
2020-10-26 18:08:27 +00:00
|
|
|
expect(Projects::ContainerRepository::CleanupTagsService)
|
2021-01-11 15:10:32 +00:00
|
|
|
.to receive(:new).and_return(double(execute: cleanup_tags_service_response))
|
|
|
|
end
|
2020-10-26 18:08:27 +00:00
|
|
|
|
2021-01-11 15:10:32 +00:00
|
|
|
it 'partially clean up the repository' do
|
2020-10-26 18:08:27 +00:00
|
|
|
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)
|
2020-12-15 12:10:17 +00:00
|
|
|
expect(repository.expiration_policy_completed_at).to eq(nil)
|
2020-10-26 18:08:27 +00:00
|
|
|
end
|
|
|
|
end
|
2021-01-11 15:10:32 +00:00
|
|
|
|
|
|
|
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,
|
2021-02-17 03:08:59 +00:00
|
|
|
before_delete_size: 100,
|
|
|
|
deleted_size: 100
|
2021-01-11 15:10:32 +00:00
|
|
|
}
|
|
|
|
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,
|
|
|
|
cleanup_tags_service_original_size: 1000,
|
|
|
|
cleanup_tags_service_before_truncate_size: 800,
|
|
|
|
cleanup_tags_service_after_truncate_size: 200,
|
2021-02-17 03:08:59 +00:00
|
|
|
cleanup_tags_service_before_delete_size: 100,
|
|
|
|
cleanup_tags_service_deleted_size: 100
|
2021-01-11 15:10:32 +00:00
|
|
|
)
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-10-26 18:08:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with no repository' do
|
|
|
|
let(:service) { described_class.new(nil) }
|
|
|
|
|
|
|
|
it 'returns an error response' do
|
|
|
|
response = subject
|
|
|
|
|
|
|
|
expect(response.success?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2021-02-12 03:08:53 +00:00
|
|
|
|
|
|
|
context 'with a network error' do
|
|
|
|
before do
|
|
|
|
expect(Projects::ContainerRepository::CleanupTagsService)
|
|
|
|
.to receive(:new).and_raise(Faraday::TimeoutError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { subject }.to raise_error(Faraday::TimeoutError)
|
|
|
|
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
end
|
2020-10-26 18:08:27 +00:00
|
|
|
end
|
|
|
|
end
|