2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe ContainerExpirationPolicyWorker do
|
2020-03-13 15:44:24 +05:30
|
|
|
include ExclusiveLeaseHelpers
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
let(:worker) { described_class.new }
|
|
|
|
let(:started_at) { nil }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
describe '#perform' do
|
|
|
|
subject { worker.perform }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
shared_examples 'not executing any policy' do
|
2021-01-29 00:20:46 +05:30
|
|
|
it 'does not run any policy' do
|
|
|
|
expect(ContainerExpirationPolicyService).not_to receive(:new)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
shared_examples 'handling a taken exclusive lease' do
|
|
|
|
context 'with exclusive lease taken' do
|
|
|
|
before do
|
|
|
|
stub_exclusive_lease_taken(worker.lease_key, timeout: 5.hours)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not do anything' do
|
|
|
|
expect(ContainerExpirationPolicies::CleanupContainerRepositoryWorker).not_to receive(:perform_with_capacity)
|
|
|
|
expect(worker).not_to receive(:runnable_policies)
|
|
|
|
|
|
|
|
expect { subject }.not_to change { ContainerRepository.cleanup_scheduled.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with throttling enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(container_registry_expiration_policies_throttling: true)
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
it 'calls the limited capacity worker' do
|
|
|
|
expect(ContainerExpirationPolicies::CleanupContainerRepositoryWorker).to receive(:perform_with_capacity)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
subject
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
it_behaves_like 'handling a taken exclusive lease'
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'with throttling disabled' do
|
2020-06-23 00:09:42 +05:30
|
|
|
before do
|
2021-01-29 00:20:46 +05:30
|
|
|
stub_feature_flags(container_registry_expiration_policies_throttling: false)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'with no container expiration policies' do
|
|
|
|
it_behaves_like 'not executing any policy'
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'with container expiration policies' do
|
|
|
|
let_it_be(:container_expiration_policy, reload: true) { create(:container_expiration_policy, :runnable) }
|
|
|
|
let_it_be(:container_repository) { create(:container_repository, project: container_expiration_policy.project) }
|
|
|
|
|
|
|
|
context 'a valid policy' do
|
|
|
|
it 'runs the policy' do
|
|
|
|
expect(ContainerExpirationPolicyService)
|
2022-04-04 11:22:00 +05:30
|
|
|
.to receive(:new).with(container_expiration_policy.project, nil).and_call_original
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(CleanupContainerRepositoryWorker).to receive(:perform_async).once.and_call_original
|
|
|
|
|
|
|
|
expect { subject }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'a disabled policy' do
|
|
|
|
before do
|
|
|
|
container_expiration_policy.disable!
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'not executing any policy'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'a policy that is not due for a run' do
|
|
|
|
before do
|
|
|
|
container_expiration_policy.update_column(:next_run_at, 2.minutes.from_now)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'not executing any policy'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'a policy linked to no container repository' do
|
|
|
|
before do
|
|
|
|
container_expiration_policy.container_repositories.delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'not executing any policy'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'an invalid policy' do
|
|
|
|
before do
|
|
|
|
container_expiration_policy.update_column(:name_regex, '*production')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'disables the policy and tracks an error' do
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(ContainerExpirationPolicyService).not_to receive(:new).with(container_expiration_policy, nil)
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(Gitlab::ErrorTracking).to receive(:log_exception).with(instance_of(described_class::InvalidPolicyError), container_expiration_policy_id: container_expiration_policy.id)
|
|
|
|
|
|
|
|
expect { subject }.to change { container_expiration_policy.reload.enabled }.from(true).to(false)
|
|
|
|
end
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
context 'process stale ongoing cleanups' do
|
|
|
|
let_it_be(:stuck_cleanup) { create(:container_repository, :cleanup_ongoing, expiration_policy_started_at: 1.day.ago) }
|
2021-09-30 23:02:18 +05:30
|
|
|
let_it_be(:container_repository1) { create(:container_repository, :cleanup_scheduled) }
|
|
|
|
let_it_be(:container_repository2) { create(:container_repository, :cleanup_unfinished) }
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
it 'set them as unfinished' do
|
|
|
|
expect { subject }
|
|
|
|
.to change { ContainerRepository.cleanup_ongoing.count }.from(1).to(0)
|
|
|
|
.and change { ContainerRepository.cleanup_unfinished.count }.from(1).to(2)
|
|
|
|
expect(stuck_cleanup.reload).to be_cleanup_unfinished
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'policies without container repositories' do
|
|
|
|
let_it_be(:container_expiration_policy1) { create(:container_expiration_policy, enabled: true) }
|
|
|
|
let_it_be(:container_repository1) { create(:container_repository, project_id: container_expiration_policy1.project_id) }
|
|
|
|
let_it_be(:container_expiration_policy2) { create(:container_expiration_policy, enabled: true) }
|
|
|
|
let_it_be(:container_repository2) { create(:container_repository, project_id: container_expiration_policy2.project_id) }
|
|
|
|
let_it_be(:container_expiration_policy3) { create(:container_expiration_policy, enabled: true) }
|
|
|
|
|
|
|
|
it 'disables them' do
|
|
|
|
expect { subject }
|
|
|
|
.to change { ::ContainerExpirationPolicy.active.count }.from(3).to(2)
|
|
|
|
expect(container_expiration_policy3.reload.enabled).to be false
|
|
|
|
end
|
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
context 'counts logging' do
|
|
|
|
let_it_be(:container_repository1) { create(:container_repository, :cleanup_scheduled) }
|
|
|
|
let_it_be(:container_repository2) { create(:container_repository, :cleanup_unfinished) }
|
|
|
|
let_it_be(:container_repository3) { create(:container_repository, :cleanup_unfinished) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
ContainerExpirationPolicy.update_all(enabled: true)
|
|
|
|
container_repository1.project.container_expiration_policy.update_column(:next_run_at, 5.minutes.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs all the counts' do
|
|
|
|
expect(worker).to receive(:log_extra_metadata_on_done).with(:cleanup_required_count, 1)
|
|
|
|
expect(worker).to receive(:log_extra_metadata_on_done).with(:cleanup_unfinished_count, 2)
|
|
|
|
expect(worker).to receive(:log_extra_metadata_on_done).with(:cleanup_total_count, 3)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
context 'with load balancing enabled' do
|
2021-09-30 23:02:18 +05:30
|
|
|
it 'reads the counts from the replica' do
|
|
|
|
expect(Gitlab::Database::LoadBalancing::Session.current).to receive(:use_replicas_for_read_queries).and_call_original
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|