summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/models
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support/shared_examples/models')
-rw-r--r--spec/support/shared_examples/models/boards/listable_shared_examples.rb97
-rw-r--r--spec/support/shared_examples/models/concerns/can_housekeep_repository_shared_examples.rb45
-rw-r--r--spec/support/shared_examples/models/concerns/repositories/can_housekeep_repository_shared_examples.rb45
-rw-r--r--spec/support/shared_examples/models/concerns/repository_storage_movable_shared_examples.rb4
-rw-r--r--spec/support/shared_examples/models/packages/debian/architecture_shared_examples.rb48
-rw-r--r--spec/support/shared_examples/models/packages/debian/distribution_shared_examples.rb225
6 files changed, 461 insertions, 3 deletions
diff --git a/spec/support/shared_examples/models/boards/listable_shared_examples.rb b/spec/support/shared_examples/models/boards/listable_shared_examples.rb
new file mode 100644
index 00000000000..e733a5488fb
--- /dev/null
+++ b/spec/support/shared_examples/models/boards/listable_shared_examples.rb
@@ -0,0 +1,97 @@
+# frozen_string_literal: true
+
+RSpec.shared_examples 'boards listable model' do |list_factory|
+ subject { build(list_factory) }
+
+ describe 'associations' do
+ it { is_expected.to validate_presence_of(:position) }
+ it { is_expected.to validate_numericality_of(:position).only_integer.is_greater_than_or_equal_to(0) }
+
+ context 'when list_type is set to closed' do
+ subject { build(list_factory, list_type: :closed) }
+
+ it { is_expected.not_to validate_presence_of(:label) }
+ it { is_expected.not_to validate_presence_of(:position) }
+ end
+ end
+
+ describe 'scopes' do
+ describe '.ordered' do
+ it 'returns lists ordered by type and position' do
+ # rubocop:disable Rails/SaveBang
+ lists = [
+ create(list_factory, list_type: :backlog),
+ create(list_factory, list_type: :closed),
+ create(list_factory, position: 1),
+ create(list_factory, position: 2)
+ ]
+ # rubocop:enable Rails/SaveBang
+
+ expect(described_class.where(id: lists).ordered).to eq([lists[0], lists[2], lists[3], lists[1]])
+ end
+ end
+ end
+
+ describe '#destroyable?' do
+ it 'returns true when list_type is set to label' do
+ subject.list_type = :label
+
+ expect(subject).to be_destroyable
+ end
+
+ it 'returns false when list_type is set to closed' do
+ subject.list_type = :closed
+
+ expect(subject).not_to be_destroyable
+ end
+ end
+
+ describe '#movable?' do
+ it 'returns true when list_type is set to label' do
+ subject.list_type = :label
+
+ expect(subject).to be_movable
+ end
+
+ it 'returns false when list_type is set to closed' do
+ subject.list_type = :closed
+
+ expect(subject).not_to be_movable
+ end
+ end
+
+ describe '#title' do
+ it 'returns label name when list_type is set to label' do
+ subject.list_type = :label
+ subject.label = Label.new(name: 'Development')
+
+ expect(subject.title).to eq 'Development'
+ end
+
+ it 'returns Open when list_type is set to backlog' do
+ subject.list_type = :backlog
+
+ expect(subject.title).to eq 'Open'
+ end
+
+ it 'returns Closed when list_type is set to closed' do
+ subject.list_type = :closed
+
+ expect(subject.title).to eq 'Closed'
+ end
+ end
+
+ describe '#destroy' do
+ it 'can be destroyed when list_type is set to label' do
+ subject = create(list_factory) # rubocop:disable Rails/SaveBang
+
+ expect(subject.destroy).to be_truthy
+ end
+
+ it 'can not be destroyed when list_type is set to closed' do
+ subject = create(list_factory, list_type: :closed) # rubocop:disable Rails/SaveBang
+
+ expect(subject.destroy).to be_falsey
+ end
+ end
+end
diff --git a/spec/support/shared_examples/models/concerns/can_housekeep_repository_shared_examples.rb b/spec/support/shared_examples/models/concerns/can_housekeep_repository_shared_examples.rb
new file mode 100644
index 00000000000..2f0b95427d2
--- /dev/null
+++ b/spec/support/shared_examples/models/concerns/can_housekeep_repository_shared_examples.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+RSpec.shared_examples 'can housekeep repository' do
+ context 'with a clean redis state', :clean_gitlab_redis_shared_state do
+ describe '#pushes_since_gc' do
+ context 'without any pushes' do
+ it 'returns 0' do
+ expect(resource.pushes_since_gc).to eq(0)
+ end
+ end
+
+ context 'with a number of pushes' do
+ it 'returns the number of pushes' do
+ 3.times { resource.increment_pushes_since_gc }
+
+ expect(resource.pushes_since_gc).to eq(3)
+ end
+ end
+ end
+
+ describe '#increment_pushes_since_gc' do
+ it 'increments the number of pushes since the last GC' do
+ 3.times { resource.increment_pushes_since_gc }
+
+ expect(resource.pushes_since_gc).to eq(3)
+ end
+ end
+
+ describe '#reset_pushes_since_gc' do
+ it 'resets the number of pushes since the last GC' do
+ 3.times { resource.increment_pushes_since_gc }
+
+ resource.reset_pushes_since_gc
+
+ expect(resource.pushes_since_gc).to eq(0)
+ end
+ end
+
+ describe '#pushes_since_gc_redis_shared_state_key' do
+ it 'returns the proper redis key format' do
+ expect(resource.send(:pushes_since_gc_redis_shared_state_key)).to eq("#{resource_key}/#{resource.id}/pushes_since_gc")
+ end
+ end
+ end
+end
diff --git a/spec/support/shared_examples/models/concerns/repositories/can_housekeep_repository_shared_examples.rb b/spec/support/shared_examples/models/concerns/repositories/can_housekeep_repository_shared_examples.rb
new file mode 100644
index 00000000000..2f0b95427d2
--- /dev/null
+++ b/spec/support/shared_examples/models/concerns/repositories/can_housekeep_repository_shared_examples.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+RSpec.shared_examples 'can housekeep repository' do
+ context 'with a clean redis state', :clean_gitlab_redis_shared_state do
+ describe '#pushes_since_gc' do
+ context 'without any pushes' do
+ it 'returns 0' do
+ expect(resource.pushes_since_gc).to eq(0)
+ end
+ end
+
+ context 'with a number of pushes' do
+ it 'returns the number of pushes' do
+ 3.times { resource.increment_pushes_since_gc }
+
+ expect(resource.pushes_since_gc).to eq(3)
+ end
+ end
+ end
+
+ describe '#increment_pushes_since_gc' do
+ it 'increments the number of pushes since the last GC' do
+ 3.times { resource.increment_pushes_since_gc }
+
+ expect(resource.pushes_since_gc).to eq(3)
+ end
+ end
+
+ describe '#reset_pushes_since_gc' do
+ it 'resets the number of pushes since the last GC' do
+ 3.times { resource.increment_pushes_since_gc }
+
+ resource.reset_pushes_since_gc
+
+ expect(resource.pushes_since_gc).to eq(0)
+ end
+ end
+
+ describe '#pushes_since_gc_redis_shared_state_key' do
+ it 'returns the proper redis key format' do
+ expect(resource.send(:pushes_since_gc_redis_shared_state_key)).to eq("#{resource_key}/#{resource.id}/pushes_since_gc")
+ end
+ end
+ end
+end
diff --git a/spec/support/shared_examples/models/concerns/repository_storage_movable_shared_examples.rb b/spec/support/shared_examples/models/concerns/repository_storage_movable_shared_examples.rb
index 5a8388d01df..4c617f3ba46 100644
--- a/spec/support/shared_examples/models/concerns/repository_storage_movable_shared_examples.rb
+++ b/spec/support/shared_examples/models/concerns/repository_storage_movable_shared_examples.rb
@@ -63,7 +63,6 @@ RSpec.shared_examples 'handles repository moves' do
context 'and transits to scheduled' do
it 'triggers the corresponding repository storage worker' do
- skip unless repository_storage_worker # TODO remove after https://gitlab.com/gitlab-org/gitlab/-/issues/218991 is implemented
expect(repository_storage_worker).to receive(:perform_async).with(container.id, 'test_second_storage', storage_move.id)
storage_move.schedule!
@@ -72,8 +71,7 @@ RSpec.shared_examples 'handles repository moves' do
end
context 'when the transition fails' do
- it 'does not trigger ProjectUpdateRepositoryStorageWorker and adds an error' do
- skip unless repository_storage_worker # TODO remove after https://gitlab.com/gitlab-org/gitlab/-/issues/218991 is implemented
+ it 'does not trigger the corresponding repository storage worker and adds an error' do
allow(storage_move.container).to receive(:set_repository_read_only!).and_raise(StandardError, 'foobar')
expect(repository_storage_worker).not_to receive(:perform_async)
diff --git a/spec/support/shared_examples/models/packages/debian/architecture_shared_examples.rb b/spec/support/shared_examples/models/packages/debian/architecture_shared_examples.rb
new file mode 100644
index 00000000000..38983f752f4
--- /dev/null
+++ b/spec/support/shared_examples/models/packages/debian/architecture_shared_examples.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.shared_examples 'Debian Distribution Architecture' do |factory, container, can_freeze|
+ let_it_be_with_refind(:architecture) { create(factory) } # rubocop:disable Rails/SaveBang
+ let_it_be(:architecture_same_distribution, freeze: can_freeze) { create(factory, distribution: architecture.distribution) }
+ let_it_be(:architecture_same_name, freeze: can_freeze) { create(factory, name: architecture.name) }
+
+ subject { architecture }
+
+ describe 'relationships' do
+ it { is_expected.to belong_to(:distribution).class_name("Packages::Debian::#{container.capitalize}Distribution").inverse_of(:architectures) }
+ end
+
+ describe 'validations' do
+ describe "#distribution" do
+ it { is_expected.to validate_presence_of(:distribution) }
+ end
+
+ describe '#name' do
+ it { is_expected.to validate_presence_of(:name) }
+
+ it { is_expected.to allow_value('amd64').for(:name) }
+ it { is_expected.to allow_value('kfreebsd-i386').for(:name) }
+ it { is_expected.not_to allow_value('-a').for(:name) }
+ it { is_expected.not_to allow_value('AMD64').for(:name) }
+ end
+ end
+
+ describe 'scopes' do
+ describe '.with_distribution' do
+ subject { described_class.with_distribution(architecture.distribution) }
+
+ it 'does not return other distributions' do
+ expect(subject.to_a).to eq([architecture, architecture_same_distribution])
+ end
+ end
+
+ describe '.with_name' do
+ subject { described_class.with_name(architecture.name) }
+
+ it 'does not return other distributions' do
+ expect(subject.to_a).to eq([architecture, architecture_same_name])
+ end
+ end
+ end
+end
diff --git a/spec/support/shared_examples/models/packages/debian/distribution_shared_examples.rb b/spec/support/shared_examples/models/packages/debian/distribution_shared_examples.rb
new file mode 100644
index 00000000000..af87d30099f
--- /dev/null
+++ b/spec/support/shared_examples/models/packages/debian/distribution_shared_examples.rb
@@ -0,0 +1,225 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.shared_examples 'Debian Distribution' do |factory, container, can_freeze|
+ let_it_be(:distribution_with_suite, freeze: can_freeze) { create(factory, suite: 'mysuite') }
+ 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 ) }
+
+ subject { distribution }
+
+ describe 'relationships' do
+ it { is_expected.to belong_to(container) }
+ it { is_expected.to belong_to(:creator).class_name('User') }
+
+ it { is_expected.to have_many(:architectures).class_name("Packages::Debian::#{container.capitalize}Architecture").inverse_of(:distribution) }
+ end
+
+ describe 'validations' do
+ describe "##{container}" do
+ it { is_expected.to validate_presence_of(container) }
+ end
+
+ describe "#creator" do
+ it { is_expected.not_to validate_presence_of(:creator) }
+ end
+
+ describe '#codename' do
+ it { is_expected.to validate_presence_of(:codename) }
+
+ it { is_expected.to allow_value('buster').for(:codename) }
+ it { is_expected.to allow_value('buster-updates').for(:codename) }
+ it { is_expected.to allow_value('Debian10.5').for(:codename) }
+ it { is_expected.not_to allow_value('jessie/updates').for(:codename) }
+ it { is_expected.not_to allow_value('hé').for(:codename) }
+ end
+
+ describe '#suite' do
+ it { is_expected.to allow_value(nil).for(:suite) }
+ it { is_expected.to allow_value('testing').for(:suite) }
+ it { is_expected.not_to allow_value('hé').for(:suite) }
+ end
+
+ describe '#unique_debian_suite_and_codename' do
+ using RSpec::Parameterized::TableSyntax
+
+ where(:with_existing_suite, :suite, :codename, :errors) do
+ false | nil | :keep | nil
+ false | 'testing' | :keep | nil
+ false | nil | :codename | ["Codename has already been taken"]
+ false | :codename | :keep | ["Suite has already been taken as Codename"]
+ false | :codename | :codename | ["Codename has already been taken", "Suite has already been taken as Codename"]
+ true | nil | :keep | nil
+ true | 'testing' | :keep | nil
+ true | nil | :codename | ["Codename has already been taken"]
+ true | :codename | :keep | ["Suite has already been taken as Codename"]
+ true | :codename | :codename | ["Codename has already been taken", "Suite has already been taken as Codename"]
+ true | nil | :suite | ["Codename has already been taken as Suite"]
+ true | :suite | :keep | ["Suite has already been taken"]
+ true | :suite | :suite | ["Suite has already been taken", "Codename has already been taken as Suite"]
+ end
+
+ with_them do
+ context factory do
+ let(:new_distribution) { build(factory, container: distribution.container) }
+
+ before do
+ distribution.update_column(:suite, 'suite-' + distribution.codename) if with_existing_suite
+
+ if suite.is_a?(Symbol)
+ new_distribution.suite = distribution.send suite unless suite == :keep
+ else
+ new_distribution.suite = suite
+ end
+
+ if codename.is_a?(Symbol)
+ new_distribution.codename = distribution.send codename unless codename == :keep
+ else
+ new_distribution.codename = codename
+ end
+ end
+
+ it do
+ if errors
+ expect(new_distribution).not_to be_valid
+ expect(new_distribution.errors.to_a).to eq(errors)
+ else
+ expect(new_distribution).to be_valid
+ end
+ end
+ end
+ end
+ end
+
+ describe '#origin' do
+ it { is_expected.to allow_value(nil).for(:origin) }
+ it { is_expected.to allow_value('Debian').for(:origin) }
+ it { is_expected.not_to allow_value('hé').for(:origin) }
+ end
+
+ describe '#label' do
+ it { is_expected.to allow_value(nil).for(:label) }
+ it { is_expected.to allow_value('Debian').for(:label) }
+ it { is_expected.not_to allow_value('hé').for(:label) }
+ end
+
+ describe '#version' do
+ it { is_expected.to allow_value(nil).for(:version) }
+ it { is_expected.to allow_value('10.6').for(:version) }
+ it { is_expected.not_to allow_value('hé').for(:version) }
+ end
+
+ describe '#description' do
+ it { is_expected.to allow_value(nil).for(:description) }
+ it { is_expected.to allow_value('Debian 10.6 Released 26 September 2020').for(:description) }
+ it { is_expected.to allow_value('Hé !').for(:description) }
+ end
+
+ describe '#valid_time_duration_seconds' do
+ it { is_expected.to allow_value(nil).for(:valid_time_duration_seconds) }
+ it { is_expected.to allow_value(24.hours.to_i).for(:valid_time_duration_seconds) }
+ it { is_expected.not_to allow_value(12.hours.to_i).for(:valid_time_duration_seconds) }
+ end
+
+ describe '#signing_keys' do
+ it { is_expected.to validate_absence_of(:signing_keys) }
+ end
+
+ describe '#file' do
+ it { is_expected.not_to validate_presence_of(:file) }
+ end
+
+ describe '#file_store' do
+ it { is_expected.to validate_presence_of(:file_store) }
+ end
+
+ describe '#file_signature' do
+ it { is_expected.to validate_absence_of(:file_signature) }
+ end
+ end
+
+ describe 'scopes' do
+ describe '.with_container' do
+ subject { described_class.with_container(distribution_with_suite.container) }
+
+ it 'does not return other distributions' do
+ expect(subject).to match_array([distribution_with_suite, distribution, distribution_with_same_container])
+ end
+ end
+
+ describe '.with_codename' do
+ subject { described_class.with_codename(distribution_with_suite.codename) }
+
+ it 'does not return other distributions' do
+ expect(subject).to match_array([distribution_with_suite, distribution_with_same_codename])
+ end
+ end
+
+ describe '.with_suite' do
+ subject { described_class.with_suite(distribution_with_suite.suite) }
+
+ it 'does not return other distributions' do
+ 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
+ subject { distribution.needs_update? }
+
+ context 'with new distribution' do
+ let(:distribution) { create(factory, container: distribution_with_suite.container) }
+
+ it { is_expected.to be_truthy }
+ end
+
+ context 'with file' do
+ context 'without valid_time_duration_seconds' do
+ let(:distribution) { create(factory, :with_file, container: distribution_with_suite.container) }
+
+ it { is_expected.to be_falsey }
+ end
+
+ context 'with valid_time_duration_seconds' do
+ let(:distribution) { create(factory, :with_file, container: distribution_with_suite.container, valid_time_duration_seconds: 2.days.to_i) }
+
+ context 'when not yet expired' do
+ it { is_expected.to be_falsey }
+ end
+
+ context 'when expired' do
+ it do
+ distribution
+
+ travel_to(4.days.from_now) do
+ is_expected.to be_truthy
+ end
+ end
+ end
+ end
+ end
+ end
+end