Commit b27d3843 authored by Ash McKenzie's avatar Ash McKenzie

Merge branch 'debian_destroy_distribution_service' into 'master'

Add Packages::Debian::DestroyDistributionService

See merge request gitlab-org/gitlab!51894
parents ad202b73 4d48ee01
# frozen_string_literal: true
module Packages
module Debian
class DestroyDistributionService
def initialize(distribution)
@distribution = distribution
end
def execute
destroy_distribution
end
private
def destroy_distribution
if @distribution.destroy
success
else
error("Unable to destroy Debian #{@distribution.model_name.human.downcase}")
end
end
def success
ServiceResponse.success
end
def error(message)
ServiceResponse.error(message: message, payload: { distribution: @distribution })
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Packages::Debian::DestroyDistributionService do
RSpec.shared_examples 'Destroy Debian Distribution' do |expected_message|
it 'returns ServiceResponse', :aggregate_failures do
if expected_message.nil?
expect { response }
.to change { container.debian_distributions.klass.all.count }
.from(1).to(0)
.and change { container.debian_distributions.count }
.from(1).to(0)
.and change { component1.class.all.count }
.from(2).to(0)
.and change { architecture1.class.all.count }
.from(3).to(0)
else
expect { response }
.to not_change { container.debian_distributions.klass.all.count }
.and not_change { container.debian_distributions.count }
.and not_change { component1.class.all.count }
.and not_change { architecture1.class.all.count }
end
expect(response).to be_a(ServiceResponse)
expect(response.success?).to eq(expected_message.nil?)
expect(response.error?).to eq(!expected_message.nil?)
expect(response.message).to eq(expected_message)
if expected_message.nil?
expect(response.payload).to eq({})
else
expect(response.payload).to eq(distribution: distribution)
end
end
end
RSpec.shared_examples 'Debian Destroy Distribution Service' do |container_type, can_freeze|
context "with a Debian #{container_type} distribution" do
let_it_be(:container, freeze: can_freeze) { create(container_type) } # rubocop:disable Rails/SaveBang
let_it_be(:distribution, freeze: can_freeze) { create("debian_#{container_type}_distribution", container: container) }
let_it_be(:component1, freeze: can_freeze) { create("debian_#{container_type}_component", distribution: distribution, name: 'component1') }
let_it_be(:component2, freeze: can_freeze) { create("debian_#{container_type}_component", distribution: distribution, name: 'component2') }
let_it_be(:architecture0, freeze: true) { create("debian_#{container_type}_architecture", distribution: distribution, name: 'all') }
let_it_be(:architecture1, freeze: can_freeze) { create("debian_#{container_type}_architecture", distribution: distribution, name: 'architecture1') }
let_it_be(:architecture2, freeze: can_freeze) { create("debian_#{container_type}_architecture", distribution: distribution, name: 'architecture2') }
subject { described_class.new(distribution) }
let(:response) { subject.execute }
context 'with a distribution' do
it_behaves_like 'Destroy Debian Distribution'
end
context 'when destroy fails' do
before do
expect(distribution).to receive(:destroy).and_return(false)
end
it_behaves_like 'Destroy Debian Distribution', "Unable to destroy Debian #{container_type} distribution"
end
end
end
it_behaves_like 'Debian Destroy Distribution Service', :project, true
it_behaves_like 'Debian Destroy Distribution Service', :group, false
end
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