Commit a7466af3 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve code related to removing container image tags

parent 600bbe15
...@@ -45,15 +45,14 @@ class ContainerRepository < ActiveRecord::Base ...@@ -45,15 +45,14 @@ class ContainerRepository < ActiveRecord::Base
# TODO, specs needed # TODO, specs needed
# #
def has_tags? def has_tags?
tags.any? tags.to_a.any?
end end
# TODO, add bang to this method def delete_tags!
# return unless has_tags?
def delete_tags
return unless tags digests = tags.map { |tag| tag.digest }.to_set
digests = tags.map {|tag| tag.digest }.to_set
digests.all? do |digest| digests.all? do |digest|
client.delete_repository_tag(self.path, digest) client.delete_repository_tag(self.path, digest)
end end
......
...@@ -8,13 +8,21 @@ FactoryGirl.define do ...@@ -8,13 +8,21 @@ FactoryGirl.define do
end end
after(:build) do |repository, evaluator| after(:build) do |repository, evaluator|
if evaluator.tags.any? next if evaluator.tags.to_a.none?
allow(repository.client)
.to receive(:repository_tags)
.and_return({
'name' => repository.path,
'tags' => evaluator.tags
})
evaluator.tags.each do |tag|
allow(repository.client) allow(repository.client)
.to receive(:repository_tags) .to receive(:repository_tag_digest)
.and_return({ .with(repository.path, tag)
name: repository.path, .and_return('sha256:4c8e63ca4cb663ce6c688cb06f1c3' \
tags: evaluator.tags '72b088dac5b6d7ad7d49cd620d85cf72a15')
})
end end
end end
end end
......
...@@ -38,7 +38,8 @@ describe "Container Registry" do ...@@ -38,7 +38,8 @@ describe "Container Registry" do
end end
it do it do
expect_any_instance_of(ContainerRepository).to receive(:delete_tags).and_return(true) expect_any_instance_of(ContainerRepository)
.to receive(:delete_tags!).and_return(true)
click_on 'Remove image' click_on 'Remove image'
end end
......
...@@ -57,38 +57,30 @@ describe ContainerRepository do ...@@ -57,38 +57,30 @@ describe ContainerRepository do
it { is_expected.not_to be_empty } it { is_expected.not_to be_empty }
end end
# TODO, improve these specs describe '#delete_tags!' do
# let(:container_repository) do
describe '#delete_tags' do create(:container_repository, name: 'my_image',
let(:tag) { ContainerRegistry::Tag.new(container_repository, 'tag') } tags: %w[latest rc1],
project: project)
before do
allow(container_repository).to receive(:tags).twice.and_return([tag])
allow(tag).to receive(:digest)
.and_return('sha256:4c8e63ca4cb663ce6c688cb06f1c3672a172b088dac5b6d7ad7d49cd620d85cf')
end end
context 'when action succeeds' do context 'when action succeeds' do
before do it 'returns status that indicates success' do
allow(container_repository.client) expect(container_repository.client)
.to receive(:delete_repository_tag) .to receive(:delete_repository_tag)
.and_return(true) .and_return(true)
end
it 'returns status that indicates success' do expect(container_repository.delete_tags!).to be_truthy
expect(container_repository.delete_tags).to be_truthy
end end
end end
context 'when action fails' do context 'when action fails' do
before do it 'returns status that indicates failure' do
allow(container_repository.client) expect(container_repository.client)
.to receive(:delete_repository_tag) .to receive(:delete_repository_tag)
.and_return(false) .and_return(false)
end
it 'returns status that indicates failure' do expect(container_repository.delete_tags!).to be_falsey
expect(container_repository.delete_tags).to be_falsey
end end
end end
end end
......
...@@ -100,7 +100,8 @@ describe Projects::DestroyService, services: true do ...@@ -100,7 +100,8 @@ describe Projects::DestroyService, services: true do
context 'images deletion succeeds' do context 'images deletion succeeds' do
it do it do
expect_any_instance_of(ContainerRepository).to receive(:delete_tags).and_return(true) expect_any_instance_of(ContainerRepository)
.to receive(:delete_tags!).and_return(true)
destroy_project(project, user, {}) destroy_project(project, user, {})
end end
...@@ -108,7 +109,8 @@ describe Projects::DestroyService, services: true do ...@@ -108,7 +109,8 @@ describe Projects::DestroyService, services: true do
context 'images deletion fails' do context 'images deletion fails' do
before do before do
expect_any_instance_of(ContainerRepository).to receive(:delete_tags).and_return(false) expect_any_instance_of(ContainerRepository)
.to receive(:delete_tags!).and_return(false)
end end
subject { destroy_project(project, user, {}) } subject { destroy_project(project, user, {}) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment