2019-03-02 22:35:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Projects::ContainerRepository::CleanupTagsService do
|
2020-03-13 15:44:24 +05:30
|
|
|
let_it_be(:user) { create(:user) }
|
2020-06-23 00:09:42 +05:30
|
|
|
let_it_be(:project, reload: true) { create(:project, :private) }
|
2020-03-13 15:44:24 +05:30
|
|
|
let_it_be(:repository) { create(:container_repository, :root, project: project) }
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
let(:service) { described_class.new(project, user, params) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
|
|
|
|
stub_container_registry_config(enabled: true)
|
|
|
|
|
|
|
|
stub_container_registry_tags(
|
|
|
|
repository: repository.path,
|
|
|
|
tags: %w(latest A Ba Bb C D E))
|
|
|
|
|
|
|
|
stub_tag_digest('latest', 'sha256:configA')
|
|
|
|
stub_tag_digest('A', 'sha256:configA')
|
|
|
|
stub_tag_digest('Ba', 'sha256:configB')
|
|
|
|
stub_tag_digest('Bb', 'sha256:configB')
|
|
|
|
stub_tag_digest('C', 'sha256:configC')
|
|
|
|
stub_tag_digest('D', 'sha256:configD')
|
|
|
|
stub_tag_digest('E', nil)
|
|
|
|
|
|
|
|
stub_digest_config('sha256:configA', 1.hour.ago)
|
|
|
|
stub_digest_config('sha256:configB', 5.days.ago)
|
|
|
|
stub_digest_config('sha256:configC', 1.month.ago)
|
|
|
|
stub_digest_config('sha256:configD', nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
subject { service.execute(repository) }
|
|
|
|
|
|
|
|
context 'when no params are specified' do
|
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
it 'does not remove anything' do
|
2020-04-22 19:07:51 +05:30
|
|
|
expect_any_instance_of(Projects::ContainerRepository::DeleteTagsService)
|
|
|
|
.not_to receive(:execute)
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
is_expected.to include(status: :success, deleted: [])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when regex matching everything is specified' do
|
2020-04-08 14:13:33 +05:30
|
|
|
shared_examples 'removes all matches' do
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'does remove all tags except latest' do
|
|
|
|
expect_delete(%w(A Ba Bb C D E))
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
is_expected.to include(status: :success, deleted: %w(A Ba Bb C D E))
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
let(:params) do
|
2020-04-08 14:13:33 +05:30
|
|
|
{ 'name_regex_delete' => '.*' }
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it_behaves_like 'removes all matches'
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'with deprecated name_regex param' do
|
|
|
|
let(:params) do
|
|
|
|
{ 'name_regex' => '.*' }
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it_behaves_like 'removes all matches'
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
context 'with invalid regular expressions' do
|
|
|
|
RSpec.shared_examples 'handling an invalid regex' do
|
|
|
|
it 'keeps all tags' do
|
|
|
|
expect(Projects::ContainerRepository::DeleteTagsService)
|
|
|
|
.not_to receive(:new)
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error' do
|
|
|
|
response = subject
|
|
|
|
|
|
|
|
expect(response[:status]).to eq(:error)
|
|
|
|
expect(response[:message]).to eq('invalid regex')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls error tracking service' do
|
|
|
|
expect(Gitlab::ErrorTracking).to receive(:log_exception).and_call_original
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when name_regex_delete is invalid' do
|
|
|
|
let(:params) { { 'name_regex_delete' => '*test*' } }
|
|
|
|
|
|
|
|
it_behaves_like 'handling an invalid regex'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when name_regex is invalid' do
|
|
|
|
let(:params) { { 'name_regex' => '*test*' } }
|
|
|
|
|
|
|
|
it_behaves_like 'handling an invalid regex'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when name_regex_keep is invalid' do
|
|
|
|
let(:params) { { 'name_regex_keep' => '*test*' } }
|
|
|
|
|
|
|
|
it_behaves_like 'handling an invalid regex'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'when delete regex matching specific tags is used' do
|
2019-03-02 22:35:43 +05:30
|
|
|
let(:params) do
|
2020-04-08 14:13:33 +05:30
|
|
|
{ 'name_regex_delete' => 'C|D' }
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does remove C and D' do
|
2020-04-22 19:07:51 +05:30
|
|
|
expect_delete(%w(C D))
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
is_expected.to include(status: :success, deleted: %w(C D))
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
context 'with overriding allow regex' do
|
|
|
|
let(:params) do
|
|
|
|
{ 'name_regex_delete' => 'C|D',
|
|
|
|
'name_regex_keep' => 'C' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not remove C' do
|
2020-04-22 19:07:51 +05:30
|
|
|
expect_delete(%w(D))
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
is_expected.to include(status: :success, deleted: %w(D))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with name_regex_delete overriding deprecated name_regex' do
|
|
|
|
let(:params) do
|
|
|
|
{ 'name_regex' => 'C|D',
|
|
|
|
'name_regex_delete' => 'D' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not remove C' do
|
2020-04-22 19:07:51 +05:30
|
|
|
expect_delete(%w(D))
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
is_expected.to include(status: :success, deleted: %w(D))
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
context 'with allow regex value' do
|
2019-03-02 22:35:43 +05:30
|
|
|
let(:params) do
|
2020-04-22 19:07:51 +05:30
|
|
|
{ 'name_regex_delete' => '.*',
|
|
|
|
'name_regex_keep' => 'B.*' }
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'does not remove B*' do
|
|
|
|
expect_delete(%w(A C D E))
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
is_expected.to include(status: :success, deleted: %w(A C D E))
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
context 'when keeping only N tags' do
|
2020-04-08 14:13:33 +05:30
|
|
|
let(:params) do
|
2020-04-22 19:07:51 +05:30
|
|
|
{ 'name_regex' => 'A|B.*|C',
|
|
|
|
'keep_n' => 1 }
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'sorts tags by date' do
|
|
|
|
expect_delete(%w(Bb Ba C))
|
|
|
|
|
|
|
|
expect(service).to receive(:order_by_date).and_call_original
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
is_expected.to include(status: :success, deleted: %w(Bb Ba C))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not keeping N tags' do
|
|
|
|
let(:params) do
|
|
|
|
{ 'name_regex' => 'A|B.*|C' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not sort tags by date' do
|
|
|
|
expect_delete(%w(A Ba Bb C))
|
|
|
|
|
|
|
|
expect(service).not_to receive(:order_by_date)
|
|
|
|
|
|
|
|
is_expected.to include(status: :success, deleted: %w(A Ba Bb C))
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
context 'when removing keeping only 3' do
|
|
|
|
let(:params) do
|
2020-04-08 14:13:33 +05:30
|
|
|
{ 'name_regex_delete' => '.*',
|
2019-03-02 22:35:43 +05:30
|
|
|
'keep_n' => 3 }
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'does remove B* and C as they are the oldest' do
|
|
|
|
expect_delete(%w(Bb Ba C))
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
is_expected.to include(status: :success, deleted: %w(Bb Ba C))
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when removing older than 1 day' do
|
|
|
|
let(:params) do
|
2020-04-08 14:13:33 +05:30
|
|
|
{ 'name_regex_delete' => '.*',
|
2019-03-02 22:35:43 +05:30
|
|
|
'older_than' => '1 day' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does remove B* and C as they are older than 1 day' do
|
2020-04-22 19:07:51 +05:30
|
|
|
expect_delete(%w(Ba Bb C))
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
is_expected.to include(status: :success, deleted: %w(Ba Bb C))
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when combining all parameters' do
|
|
|
|
let(:params) do
|
2020-04-08 14:13:33 +05:30
|
|
|
{ 'name_regex_delete' => '.*',
|
2019-03-02 22:35:43 +05:30
|
|
|
'keep_n' => 1,
|
|
|
|
'older_than' => '1 day' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does remove B* and C' do
|
2020-04-22 19:07:51 +05:30
|
|
|
expect_delete(%w(Bb Ba C))
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
is_expected.to include(status: :success, deleted: %w(Bb Ba C))
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
context 'when running a container_expiration_policy' do
|
|
|
|
let(:user) { nil }
|
|
|
|
|
|
|
|
context 'with valid container_expiration_policy param' do
|
|
|
|
let(:params) do
|
2020-04-08 14:13:33 +05:30
|
|
|
{ 'name_regex_delete' => '.*',
|
2020-03-13 15:44:24 +05:30
|
|
|
'keep_n' => 1,
|
|
|
|
'older_than' => '1 day',
|
|
|
|
'container_expiration_policy' => true }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'succeeds without a user' do
|
2020-11-24 15:15:51 +05:30
|
|
|
expect_delete(%w(Bb Ba C), container_expiration_policy: true)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
is_expected.to include(status: :success, deleted: %w(Bb Ba C))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without container_expiration_policy param' do
|
|
|
|
let(:params) do
|
2020-04-08 14:13:33 +05:30
|
|
|
{ 'name_regex_delete' => '.*',
|
2020-03-13 15:44:24 +05:30
|
|
|
'keep_n' => 1,
|
|
|
|
'older_than' => '1 day' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails' do
|
|
|
|
is_expected.to include(status: :error, message: 'access denied')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def stub_tag_digest(tag, digest)
|
|
|
|
allow_any_instance_of(ContainerRegistry::Client)
|
|
|
|
.to receive(:repository_tag_digest)
|
|
|
|
.with(repository.path, tag) { digest }
|
|
|
|
|
|
|
|
allow_any_instance_of(ContainerRegistry::Client)
|
|
|
|
.to receive(:repository_manifest)
|
|
|
|
.with(repository.path, tag) do
|
|
|
|
{ 'config' => { 'digest' => digest } } if digest
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_digest_config(digest, created_at)
|
|
|
|
allow_any_instance_of(ContainerRegistry::Client)
|
|
|
|
.to receive(:blob)
|
|
|
|
.with(repository.path, digest, nil) do
|
|
|
|
{ 'created' => created_at.to_datetime.rfc3339 }.to_json if created_at
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def expect_delete(tags, container_expiration_policy: nil)
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(Projects::ContainerRepository::DeleteTagsService)
|
|
|
|
.to receive(:new)
|
2020-11-24 15:15:51 +05:30
|
|
|
.with(repository.project, user, tags: tags, container_expiration_policy: container_expiration_policy)
|
2020-04-22 19:07:51 +05:30
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
expect_any_instance_of(Projects::ContainerRepository::DeleteTagsService)
|
|
|
|
.to receive(:execute)
|
|
|
|
.with(repository) { { status: :success, deleted: tags } }
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|