Commit c591b430 authored by Tyler Amos's avatar Tyler Amos

Consolidate namespace storage app setting check

Includes the automatic_purchased_storage_allocation? app setting check
in Namespace#additional_repo_storage_by_namespace_enabled?. This
allows for more consolidation of the app setting code and ensure
consistency across the app where the namespace storage feature is
gated.  The code can be simplified, especially in tests.
parent 2c4cdee7
......@@ -47,8 +47,7 @@ module EE
end
def can_purchase_storage_for_namespace?(namespace)
::Gitlab::CurrentSettings.automatic_purchased_storage_allocation? &&
::Feature.enabled?(:buy_storage_link) &&
::Feature.enabled?(:buy_storage_link) &&
namespace.additional_repo_storage_by_namespace_enabled?
end
......
......@@ -373,7 +373,9 @@ module EE
end
def additional_repo_storage_by_namespace_enabled?
!::Feature.enabled?(:namespace_storage_limit, self) && ::Feature.enabled?(:additional_repo_storage_by_namespace, self)
!::Feature.enabled?(:namespace_storage_limit, self) &&
::Feature.enabled?(:additional_repo_storage_by_namespace, self) &&
::Gitlab::CurrentSettings.automatic_purchased_storage_allocation?
end
def root_storage_size
......
......@@ -32,10 +32,7 @@ module EE
end
def enforce_limit?
return false unless ::Gitlab::CurrentSettings.automatic_purchased_storage_allocation?
return false unless root_namespace.additional_repo_storage_by_namespace_enabled?
true
root_namespace.additional_repo_storage_by_namespace_enabled?
end
private
......
......@@ -26,8 +26,6 @@ module EE
private
def additional_repo_storage_available?
return false unless ::Gitlab::CurrentSettings.automatic_purchased_storage_allocation?
!!namespace&.additional_repo_storage_by_namespace_enabled?
end
......
......@@ -9,13 +9,16 @@ RSpec.describe Groups::UsageQuotasController do
before do
sign_in(user)
group.add_owner(user)
allow_next_found_instance_of(Group) do |group|
allow(group).to receive(:additional_repo_storage_by_namespace_enabled?)
.and_return(additional_repo_storage_by_namespace_enabled)
end
end
describe 'Pushing the `additionalRepoStorageByNamespace` feature flag to the frontend' do
context 'when both flags are true' do
before do
stub_feature_flags(additional_repo_storage_by_namespace: true, namespace_storage_limit: true)
end
context 'when additional_repo_storage_by_namespace_enabled is false' do
let(:additional_repo_storage_by_namespace_enabled) { false }
it 'is disabled' do
get :index, params: { group_id: group }
......@@ -24,10 +27,8 @@ RSpec.describe Groups::UsageQuotasController do
end
end
context 'when `namespace_storage_limit` flag is false' do
before do
stub_feature_flags(additional_repo_storage_by_namespace: true, namespace_storage_limit: false)
end
context 'when additional_repo_storage_by_namespace_enabled is true' do
let(:additional_repo_storage_by_namespace_enabled) { true }
it 'is enabled' do
get :index, params: { group_id: group }
......@@ -35,17 +36,5 @@ RSpec.describe Groups::UsageQuotasController do
expect(Gon.features).to include('additionalRepoStorageByNamespace' => true)
end
end
context 'when both flags are false' do
before do
stub_feature_flags(additional_repo_storage_by_namespace: false, namespace_storage_limit: false)
end
it 'is disabled' do
get :index, params: { group_id: group }
expect(Gon.features).to include('additionalRepoStorageByNamespace' => false)
end
end
end
end
......@@ -18,10 +18,15 @@ RSpec.describe Profiles::UsageQuotasController do
end
describe 'Pushing the `additionalRepoStorageByNamespace` feature flag to the frontend' do
context 'when both flags are true' do
before do
stub_feature_flags(additional_repo_storage_by_namespace: true, namespace_storage_limit: true)
before do
allow_next_found_instance_of(Namespace) do |namespace|
allow(namespace).to receive(:additional_repo_storage_by_namespace_enabled?)
.and_return(additional_repo_storage_by_namespace_enabled)
end
end
context 'when additional_repo_storage_by_namespace_enabled is false' do
let(:additional_repo_storage_by_namespace_enabled) { false }
it 'is disabled' do
get :index
......@@ -30,10 +35,8 @@ RSpec.describe Profiles::UsageQuotasController do
end
end
context 'when `namespace_storage_limit` flag is false' do
before do
stub_feature_flags(additional_repo_storage_by_namespace: true, namespace_storage_limit: false)
end
context 'when additional_repo_storage_by_namespace_enabled is true' do
let(:additional_repo_storage_by_namespace_enabled) { true }
it 'is enabled' do
get :index
......@@ -41,17 +44,5 @@ RSpec.describe Profiles::UsageQuotasController do
expect(Gon.features).to include('additionalRepoStorageByNamespace' => true)
end
end
context 'when both flags are false' do
before do
stub_feature_flags(additional_repo_storage_by_namespace: false, namespace_storage_limit: false)
end
it 'is disabled' do
get :index
expect(Gon.features).to include('additionalRepoStorageByNamespace' => false)
end
end
end
end
......@@ -112,17 +112,15 @@ RSpec.describe EE::NamespaceStorageLimitAlertHelper do
}
end
where(:namespace_storage_limit_enabled, :additional_repo_storage_by_namespace_enabled, :service_class_name) do
true | false | Namespaces::CheckStorageSizeService
true | true | Namespaces::CheckStorageSizeService
false | true | Namespaces::CheckExcessStorageSizeService
false | false | Namespaces::CheckStorageSizeService
where(:additional_repo_storage_by_namespace_enabled, :service_class_name) do
false | Namespaces::CheckStorageSizeService
true | Namespaces::CheckExcessStorageSizeService
end
with_them do
before do
stub_feature_flags(namespace_storage_limit: namespace_storage_limit_enabled)
stub_feature_flags(additional_repo_storage_by_namespace: additional_repo_storage_by_namespace_enabled)
allow(namespace).to receive(:additional_repo_storage_by_namespace_enabled?)
.and_return(additional_repo_storage_by_namespace_enabled)
allow(helper).to receive(:current_user).and_return(admin)
allow_next_instance_of(service_class_name, namespace, admin) do |service|
......@@ -203,28 +201,18 @@ RSpec.describe EE::NamespaceStorageLimitAlertHelper do
let_it_be(:namespace) { build(:namespace) }
where(
auto_storage_allocation_enabled: [true, false],
buy_storage_link_enabled: [true, false],
namespace_storage_limit_enabled: [true, false],
additional_storage_enabled: [true, false]
)
where(:buy_storage_link_enabled, :additional_repo_storage_by_namespace_enabled, :result) do
false | false | false
false | true | false
true | false | false
true | true | true
end
with_them do
let(:result) do
auto_storage_allocation_enabled &&
buy_storage_link_enabled &&
!namespace_storage_limit_enabled &&
additional_storage_enabled
end
before do
stub_application_setting(automatic_purchased_storage_allocation: auto_storage_allocation_enabled)
stub_feature_flags(
namespace_storage_limit: namespace_storage_limit_enabled,
additional_repo_storage_by_namespace: additional_storage_enabled,
buy_storage_link: buy_storage_link_enabled
)
stub_feature_flags(buy_storage_link: buy_storage_link_enabled)
allow(namespace).to receive(:additional_repo_storage_by_namespace_enabled?)
.and_return(additional_repo_storage_by_namespace_enabled)
end
it { is_expected.to eq(result) }
......
......@@ -9,7 +9,7 @@ RSpec.describe Gitlab::RepositorySizeChecker do
let(:total_repository_size_excess) { 0 }
let(:additional_purchased_storage) { 0 }
let(:enabled) { true }
let(:gitlab_setting_enabled) { true }
let(:additional_repo_storage_by_namespace_enabled) { true }
subject do
described_class.new(
......@@ -21,9 +21,12 @@ RSpec.describe Gitlab::RepositorySizeChecker do
end
before do
allow(Gitlab::CurrentSettings).to receive(:automatic_purchased_storage_allocation?).and_return(gitlab_setting_enabled)
allow(namespace).to receive(:total_repository_size_excess).and_return(total_repository_size_excess.megabytes) if namespace
if namespace
allow(namespace).to receive(:additional_repo_storage_by_namespace_enabled?)
.and_return(additional_repo_storage_by_namespace_enabled)
allow(namespace).to receive(:total_repository_size_excess)
.and_return(total_repository_size_excess.megabytes)
end
end
describe '#above_size_limit?' do
......@@ -59,10 +62,8 @@ RSpec.describe Gitlab::RepositorySizeChecker do
end
end
include_examples 'original logic (additional storage not considered)'
context 'when Gitlab app setting for automatic purchased storage allocation is not enabled' do
let(:gitlab_setting_enabled) { false }
context 'when additional_repo_storage_by_namespace_enabled is false' do
let(:additional_repo_storage_by_namespace_enabled) { false }
include_examples 'original logic (additional storage not considered)'
end
......@@ -73,54 +74,40 @@ RSpec.describe Gitlab::RepositorySizeChecker do
include_examples 'original logic (additional storage not considered)'
end
context 'with feature flag :namespace_storage_limit disabled' do
before do
stub_feature_flags(namespace_storage_limit: false)
end
context 'when there are no locked projects (total repository excess < additional storage)' do
let(:current_size) { 100 } # current_size > limit
let(:total_repository_size_excess) { 5 }
let(:additional_purchased_storage) { 10 }
it 'returns false' do
expect(subject.above_size_limit?).to eq(false)
end
end
context 'when there are no locked projects (total repository excess == additional storage)' do
let(:current_size) { 100 } # current_size > limit
let(:total_repository_size_excess) { 10 }
let(:additional_purchased_storage) { 10 }
context 'when there are no locked projects (total repository excess < additional storage)' do
let(:current_size) { 100 } # current_size > limit
let(:total_repository_size_excess) { 5 }
let(:additional_purchased_storage) { 10 }
it 'returns false' do
expect(subject.above_size_limit?).to eq(false)
end
it 'returns false' do
expect(subject.above_size_limit?).to eq(false)
end
end
context 'when there are locked projects (total repository excess > additional storage)' do
let(:total_repository_size_excess) { 12 }
let(:additional_purchased_storage) { 10 }
context 'when there are no locked projects (total repository excess == additional storage)' do
let(:current_size) { 100 } # current_size > limit
let(:total_repository_size_excess) { 10 }
let(:additional_purchased_storage) { 10 }
include_examples 'checker size above limit'
include_examples 'checker size not over limit'
it 'returns false' do
expect(subject.above_size_limit?).to eq(false)
end
end
context 'with feature flag :additional_repo_storage_by_namespace disabled' do
before do
stub_feature_flags(additional_repo_storage_by_namespace: false)
end
context 'when there are locked projects (total repository excess > additional storage)' do
let(:total_repository_size_excess) { 12 }
let(:additional_purchased_storage) { 10 }
include_examples 'original logic (additional storage not considered)'
include_examples 'checker size above limit'
include_examples 'checker size not over limit'
end
end
describe '#exceeded_size' do
include_examples 'checker size exceeded'
context 'when Gitlab app setting for automatic purchased storage allocation is not enabled' do
let(:gitlab_setting_enabled) { false }
context 'when additional_repo_storage_by_namespace_enabled is false' do
let(:additional_repo_storage_by_namespace_enabled) { false }
include_examples 'checker size exceeded'
end
......@@ -131,90 +118,76 @@ RSpec.describe Gitlab::RepositorySizeChecker do
include_examples 'checker size exceeded'
end
context 'with feature flag :namespace_storage_limit disabled' do
before do
stub_feature_flags(namespace_storage_limit: false)
end
context 'with additional purchased storage' do
let(:total_repository_size_excess) { 10 }
let(:additional_purchased_storage) { 10 }
context 'with additional purchased storage' do
let(:total_repository_size_excess) { 10 }
let(:additional_purchased_storage) { 10 }
context 'when no change size provided' do
context 'when current size + total repository size excess is below the limit (additional purchase storage not used)' do
let(:current_size) { limit - 1 }
context 'when no change size provided' do
context 'when current size + total repository size excess is below the limit (additional purchase storage not used)' do
let(:current_size) { limit - 1 }
it 'returns zero' do
expect(subject.exceeded_size).to eq(0)
end
end
context 'when current size + total repository size excess is equal to the limit (additional purchase storage not used)' do
let(:current_size) { limit }
it 'returns zero' do
expect(subject.exceeded_size).to eq(0)
end
end
context 'when there is remaining additional purchased storage (current size + other project excess use some additional purchased storage)' do
let(:current_size) { limit + 1 }
it 'returns zero' do
expect(subject.exceeded_size).to eq(0)
end
it 'returns zero' do
expect(subject.exceeded_size).to eq(0)
end
end
context 'when additional purchased storage is depleted (current size + other project excess exceed additional purchased storage)' do
let(:total_repository_size_excess) { 15 }
let(:current_size) { 61 }
context 'when current size + total repository size excess is equal to the limit (additional purchase storage not used)' do
let(:current_size) { limit }
it 'returns a positive number' do
expect(subject.exceeded_size).to eq(5.megabytes)
end
it 'returns zero' do
expect(subject.exceeded_size).to eq(0)
end
end
context 'when a change size is provided' do
let(:change_size) { 1.megabyte }
context 'when there is remaining additional purchased storage (current size + other project excess use some additional purchased storage)' do
let(:current_size) { limit + 1 }
context 'when current size + total repository size excess is below the limit (additional purchase storage not used)' do
let(:current_size) { limit - 1 }
it 'returns zero' do
expect(subject.exceeded_size(change_size)).to eq(0)
end
it 'returns zero' do
expect(subject.exceeded_size).to eq(0)
end
end
context 'when current size + total repository size excess is equal to the limit (additional purchase storage depleted)' do
let(:current_size) { limit }
context 'when additional purchased storage is depleted (current size + other project excess exceed additional purchased storage)' do
let(:total_repository_size_excess) { 15 }
let(:current_size) { 61 }
it 'returns a positive number' do
expect(subject.exceeded_size(change_size)).to eq(1.megabyte)
end
it 'returns a positive number' do
expect(subject.exceeded_size).to eq(5.megabytes)
end
end
end
context 'without additional purchased storage' do
context 'when namespace has total_repository_size_excess but project is below limit' do
let(:total_repository_size_excess) { 50 }
let(:change_size) { 1.megabyte }
let(:limit) { 10 }
let(:current_size) { 5 }
context 'when a change size is provided' do
let(:change_size) { 1.megabyte }
context 'when current size + total repository size excess is below the limit (additional purchase storage not used)' do
let(:current_size) { limit - 1 }
it 'returns zero' do
expect(subject.exceeded_size(change_size)).to eq(0)
end
end
include_examples 'checker size exceeded'
context 'when current size + total repository size excess is equal to the limit (additional purchase storage depleted)' do
let(:current_size) { limit }
it 'returns a positive number' do
expect(subject.exceeded_size(change_size)).to eq(1.megabyte)
end
end
end
end
context 'with feature flag :additional_repo_storage_by_namespace disabled' do
before do
stub_feature_flags(additional_repo_storage_by_namespace: false)
context 'without additional purchased storage' do
context 'when namespace has total_repository_size_excess but project is below limit' do
let(:total_repository_size_excess) { 50 }
let(:change_size) { 1.megabyte }
let(:limit) { 10 }
let(:current_size) { 5 }
it 'returns zero' do
expect(subject.exceeded_size(change_size)).to eq(0)
end
end
include_examples 'checker size exceeded'
......
......@@ -16,40 +16,26 @@ RSpec.describe EE::Namespace::RootExcessStorageSize do
subject { model.above_size_limit? }
before do
allow(Gitlab::CurrentSettings).to receive(:automatic_purchased_storage_allocation?) { storage_allocation_enabled }
allow(model).to receive(:enforce_limit?) { enforce_limit }
end
context 'when limit enforcement is off' do
let(:storage_allocation_enabled) { false }
let(:enforce_limit) { false }
it { is_expected.to eq(false) }
end
context 'when limit enforcement is on' do
let(:storage_allocation_enabled) { true }
let(:enforce_limit) { true }
context 'with feature flag :namespace_storage_limit disabled' do
before do
stub_feature_flags(namespace_storage_limit: false)
end
context 'when below limit' do
it { is_expected.to eq(false) }
end
context 'when above limit' do
let(:total_repository_size_excess) { 101.megabytes }
it { is_expected.to eq(true) }
end
context 'when below limit' do
it { is_expected.to eq(false) }
end
context 'with feature flag :additional_repo_storage_by_namespace disabled' do
before do
stub_feature_flags(additional_repo_storage_by_namespace: false)
end
context 'when above limit' do
let(:total_repository_size_excess) { 101.megabytes }
it { is_expected.to eq(false) }
it { is_expected.to eq(true) }
end
end
end
......@@ -99,36 +85,21 @@ RSpec.describe EE::Namespace::RootExcessStorageSize do
describe '#enforce_limit?' do
subject { model.enforce_limit? }
let(:storage_allocation_enabled) { true }
before do
allow(Gitlab::CurrentSettings).to receive(:automatic_purchased_storage_allocation?) { storage_allocation_enabled }
allow(namespace).to receive(:additional_repo_storage_by_namespace_enabled?)
.and_return(additional_repo_storage_by_namespace_enabled)
end
context 'with application setting is disabled' do
let(:storage_allocation_enabled) { false }
context 'when additional_repo_storage_by_namespace_enabled is false' do
let(:additional_repo_storage_by_namespace_enabled) { false }
it { is_expected.to eq(false) }
end
context 'with feature flags (:namespace_storage_limit & :additional_repo_storage_by_namespace) enabled' do
it { is_expected.to eq(false) }
end
context 'with feature flag :namespace_storage_limit disabled' do
before do
stub_feature_flags(namespace_storage_limit: false)
end
let(:additional_repo_storage_by_namespace_enabled) { true }
it { is_expected.to eq(true) }
end
context 'with feature flag :additional_repo_storage_by_namespace disabled' do
before do
stub_feature_flags(additional_repo_storage_by_namespace: false)
end
it { is_expected.to eq(false) }
end
end
end
......@@ -3,6 +3,8 @@
require 'spec_helper'
RSpec.describe Namespace do
using RSpec::Parameterized::TableSyntax
include EE::GeoHelpers
let(:namespace) { create(:namespace) }
......@@ -1770,12 +1772,48 @@ RSpec.describe Namespace do
end
end
describe '#additional_repo_storage_by_namespace_enabled?' do
let_it_be(:namespace) { build(:namespace) }
subject { namespace.additional_repo_storage_by_namespace_enabled? }
where(:namespace_storage_limit, :additional_repo_storage_by_namespace, :automatic_purchased_storage_allocation, :result) do
false | false | false | false
false | false | true | false
false | true | false | false
true | false | false | false
false | true | true | true
true | true | false | false
true | false | true | false
true | true | true | false
end
with_them do
before do
stub_feature_flags(
namespace_storage_limit: namespace_storage_limit,
additional_repo_storage_by_namespace: additional_repo_storage_by_namespace
)
stub_application_setting(automatic_purchased_storage_allocation: automatic_purchased_storage_allocation)
end
it { is_expected.to eq(result) }
end
end
describe '#root_storage_size' do
let_it_be(:namespace) { build(:namespace) }
subject { namespace.root_storage_size }
context 'with feature flag :namespace_storage_limit enabled' do
before do
allow(namespace).to receive(:additional_repo_storage_by_namespace_enabled?)
.and_return(additional_repo_storage_by_namespace_enabled)
end
context 'when additional_repo_storage_by_namespace_enabled is false' do
let(:additional_repo_storage_by_namespace_enabled) { false }
it 'initializes a new instance of EE::Namespace::RootStorageSize' do
expect(EE::Namespace::RootStorageSize).to receive(:new).with(namespace)
......@@ -1783,10 +1821,8 @@ RSpec.describe Namespace do
end
end
context 'with feature flag :namespace_storage_limit disabled' do
before do
stub_feature_flags(namespace_storage_limit: false)
end
context 'when additional_repo_storage_by_namespace_enabled is true' do
let(:additional_repo_storage_by_namespace_enabled) { true }
it 'initializes a new instance of EE::Namespace::RootExcessStorageSize' do
expect(EE::Namespace::RootExcessStorageSize).to receive(:new).with(namespace)
......
......@@ -108,17 +108,15 @@ RSpec.describe PostReceiveService, :geo do
let(:check_storage_size_response) { ServiceResponse.success }
where(:namespace_storage_limit_enabled, :additional_repo_storage_by_namespace_enabled, :service_class_name) do
true | false | Namespaces::CheckStorageSizeService
true | true | Namespaces::CheckStorageSizeService
false | true | Namespaces::CheckExcessStorageSizeService
false | false | Namespaces::CheckStorageSizeService
where(:additional_repo_storage_by_namespace_enabled, :service_class_name) do
true | Namespaces::CheckExcessStorageSizeService
false | Namespaces::CheckStorageSizeService
end
with_them do
before do
stub_feature_flags(namespace_storage_limit: namespace_storage_limit_enabled)
stub_feature_flags(additional_repo_storage_by_namespace: additional_repo_storage_by_namespace_enabled)
allow(project.namespace).to receive(:additional_repo_storage_by_namespace_enabled?)
.and_return(additional_repo_storage_by_namespace_enabled)
allow_next_instance_of(service_class_name, project.namespace, user) do |service|
expect(service).to receive(:execute).and_return(check_storage_size_response)
......
......@@ -8,43 +8,24 @@ RSpec.describe Namespaces::CheckExcessStorageSizeService, '#execute' do
let(:service) { described_class.new(namespace, user) }
let(:total_repository_size_excess) { 150.megabytes }
let(:additional_purchased_storage_size) { 100 }
let(:namespace_storage_limit_enabled) { false }
let(:storage_allocation_enabled) { true }
let(:additional_repo_storage_by_namespace_enabled) { true }
let(:actual_size_limit) { 10.gigabytes }
let(:locked_project_count) { 1 }
subject(:response) { service.execute }
before do
stub_feature_flags(namespace_storage_limit: namespace_storage_limit_enabled)
allow(Gitlab::CurrentSettings).to receive(:automatic_purchased_storage_allocation?) { storage_allocation_enabled }
allow(namespace).to receive(:additional_repo_storage_by_namespace_enabled?).and_return(additional_repo_storage_by_namespace_enabled)
allow(namespace).to receive(:root_ancestor).and_return(namespace)
allow(namespace).to receive(:total_repository_size_excess).and_return(total_repository_size_excess)
allow(namespace).to receive(:actual_size_limit).and_return(actual_size_limit)
allow(namespace).to receive(:repository_size_excess_project_count).and_return(locked_project_count)
end
context 'without limit enforcement' do
context 'with application setting disabled' do
let(:storage_allocation_enabled) { false }
it { is_expected.to be_success }
end
context 'with feature flag :additional_repo_storage_by_namespace disabled' do
before do
stub_feature_flags(additional_repo_storage_by_namespace: false)
end
it { is_expected.to be_success }
end
context 'when additional_repo_storage_by_namespace_enabled is false' do
let(:additional_repo_storage_by_namespace_enabled) { false }
context 'with feature flag :namespace_storage_limit enabled' do
let(:namespace_storage_limit_enabled) { true }
it { is_expected.to be_success }
end
it { is_expected.to be_success }
end
context 'when additional_purchased_storage_size is set to 0' do
......@@ -77,14 +58,7 @@ RSpec.describe Namespaces::CheckExcessStorageSizeService, '#execute' do
end
context 'when not admin of the namespace' do
let(:other_namespace) { build(:namespace, additional_purchased_storage_size: additional_purchased_storage_size) }
subject(:response) { described_class.new(other_namespace, user).execute }
before do
allow(other_namespace).to receive(:root_ancestor).and_return(other_namespace)
allow(other_namespace).to receive(:total_repository_size_excess).and_return(total_repository_size_excess)
end
let(:user) { build(:user) }
it 'errors and has no payload' do
expect(response).to be_error
......
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