Commit 0afd5cac authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'debian_distribution_finder' into 'master'

Add Packages::Debian::DistributionFinder

See merge request gitlab-org/gitlab!51266
parents 9bf2b199 36978866
# frozen_string_literal: true
module Packages
module Debian
class DistributionsFinder
def initialize(container, params = {})
@container, @params = container, params
end
def execute
collection = relation.with_container(container)
collection = by_codename(collection)
collection = by_suite(collection)
collection = by_codename_or_suite(collection)
collection
end
private
attr_reader :container, :params
def relation
case container
when Project
Packages::Debian::ProjectDistribution
when Group
Packages::Debian::GroupDistribution
else
raise ArgumentError, "Unexpected container type of '#{container.class}'"
end
end
def by_codename(collection)
return collection unless params[:codename].present?
collection.with_codename(params[:codename])
end
def by_suite(collection)
return collection unless params[:suite].present?
collection.with_suite(params[:suite])
end
def by_codename_or_suite(collection)
return collection unless params[:codename_or_suite].present?
collection.with_codename_or_suite(params[:codename_or_suite])
end
end
end
end
......@@ -70,6 +70,7 @@ module Packages
scope :with_container, ->(subject) { where(container_type => subject) }
scope :with_codename, ->(codename) { where(codename: codename) }
scope :with_suite, ->(suite) { where(suite: suite) }
scope :with_codename_or_suite, ->(codename_or_suite) { with_codename(codename_or_suite).or(with_suite(codename_or_suite)) }
attr_encrypted :signing_keys,
mode: :per_attribute_iv,
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Packages::Debian::DistributionsFinder do
it_behaves_like 'Debian Distributions Finder', :debian_project_distribution, true
it_behaves_like 'Debian Distributions Finder', :debian_group_distribution, false
context 'with nil container' do
let(:service) { described_class.new(nil) }
subject { service.execute.to_a }
it 'raises error' do
expect { subject }.to raise_error ArgumentError, "Unexpected container type of 'NilClass'"
end
end
context 'with unexpected container type' do
let(:service) { described_class.new(:invalid) }
subject { service.execute.to_a }
it 'raises error' do
expect { subject }.to raise_error ArgumentError, "Unexpected container type of 'Symbol'"
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.shared_examples 'Debian Distributions Finder' do |factory, can_freeze|
let_it_be(:distribution_with_suite, freeze: can_freeze) { create(factory, suite: 'mysuite') }
let_it_be(:container) { distribution_with_suite.container }
let_it_be(:distribution_with_same_container, freeze: can_freeze) { create(factory, container: container ) }
let_it_be(:distribution_with_same_codename, freeze: can_freeze) { create(factory, codename: distribution_with_suite.codename ) }
let_it_be(:distribution_with_same_suite, freeze: can_freeze) { create(factory, suite: distribution_with_suite.suite ) }
let_it_be(:distribution_with_codename_and_suite_flipped, freeze: can_freeze) { create(factory, codename: distribution_with_suite.suite, suite: distribution_with_suite.codename) }
let(:params) { {} }
let(:service) { described_class.new(container, params) }
subject { service.execute.to_a }
context 'by codename' do
context 'with existing codename' do
let(:params) { { codename: distribution_with_suite.codename } }
it 'finds distributions by codename' do
is_expected.to contain_exactly(distribution_with_suite)
end
end
context 'with non-existing codename' do
let(:params) { { codename: 'does_not_exists' } }
it 'finds nothing' do
is_expected.to be_empty
end
end
end
context 'by suite' do
context 'with existing suite' do
let(:params) { { suite: 'mysuite' } }
it 'finds distribution by suite' do
is_expected.to contain_exactly(distribution_with_suite)
end
end
context 'with non-existing suite' do
let(:params) { { suite: 'does_not_exists' } }
it 'finds nothing' do
is_expected.to be_empty
end
end
end
context 'by codename_or_suite' do
context 'with existing codename' do
let(:params) { { codename_or_suite: distribution_with_suite.codename } }
it 'finds distribution by codename' do
is_expected.to contain_exactly(distribution_with_suite)
end
end
context 'with existing suite' do
let(:params) { { codename_or_suite: 'mysuite' } }
it 'finds distribution by suite' do
is_expected.to contain_exactly(distribution_with_suite)
end
end
context 'with non-existing suite' do
let(:params) { { codename_or_suite: 'does_not_exists' } }
it 'finds nothing' do
is_expected.to be_empty
end
end
end
end
......@@ -7,6 +7,7 @@ RSpec.shared_examples 'Debian Distribution' do |factory, container, can_freeze|
let_it_be(:distribution_with_same_container, freeze: can_freeze) { create(factory, container: distribution_with_suite.container ) }
let_it_be(:distribution_with_same_codename, freeze: can_freeze) { create(factory, codename: distribution_with_suite.codename ) }
let_it_be(:distribution_with_same_suite, freeze: can_freeze) { create(factory, suite: distribution_with_suite.suite ) }
let_it_be(:distribution_with_codename_and_suite_flipped, freeze: can_freeze) { create(factory, codename: distribution_with_suite.suite, suite: distribution_with_suite.codename) }
let_it_be_with_refind(:distribution) { create(factory, container: distribution_with_suite.container ) }
......@@ -166,6 +167,24 @@ RSpec.shared_examples 'Debian Distribution' do |factory, container, can_freeze|
expect(subject).to match_array([distribution_with_suite, distribution_with_same_suite])
end
end
describe '.with_codename_or_suite' do
describe 'passing codename' do
subject { described_class.with_codename_or_suite(distribution_with_suite.codename) }
it 'does not return other distributions' do
expect(subject.to_a).to eq([distribution_with_suite, distribution_with_same_codename, distribution_with_codename_and_suite_flipped])
end
end
describe 'passing suite' do
subject { described_class.with_codename_or_suite(distribution_with_suite.suite) }
it 'does not return other distributions' do
expect(subject.to_a).to eq([distribution_with_suite, distribution_with_same_suite, distribution_with_codename_and_suite_flipped])
end
end
end
end
describe '#needs_update?' do
......
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