Commit 33648e5a authored by Markus Koller's avatar Markus Koller

Merge branch '268010_check_feature_flag_enabled_for_given_namespace' into 'master'

Check if feature flag is enabled for namespace

See merge request gitlab-org/gitlab!45385
parents e0eaa9e4 97cc5dce
...@@ -283,8 +283,7 @@ class Snippet < ApplicationRecord ...@@ -283,8 +283,7 @@ class Snippet < ApplicationRecord
::Gitlab::RepositorySizeChecker.new( ::Gitlab::RepositorySizeChecker.new(
current_size_proc: -> { repository.size.megabytes }, current_size_proc: -> { repository.size.megabytes },
limit: Gitlab::CurrentSettings.snippet_size_limit, limit: Gitlab::CurrentSettings.snippet_size_limit,
total_repository_size_excess: nil, namespace: nil
additional_purchased_storage: nil
) )
end end
end end
......
...@@ -532,8 +532,7 @@ module EE ...@@ -532,8 +532,7 @@ module EE
::Gitlab::RepositorySizeChecker.new( ::Gitlab::RepositorySizeChecker.new(
current_size_proc: -> { statistics.total_repository_size }, current_size_proc: -> { statistics.total_repository_size },
limit: actual_size_limit, limit: actual_size_limit,
total_repository_size_excess: namespace.total_repository_size_excess, namespace: namespace,
additional_purchased_storage: namespace.additional_purchased_storage_size.megabytes,
enabled: License.feature_available?(:repository_size_limit) enabled: License.feature_available?(:repository_size_limit)
) )
end end
......
...@@ -16,8 +16,7 @@ module EE ...@@ -16,8 +16,7 @@ module EE
::Gitlab::RepositorySizeChecker.new( ::Gitlab::RepositorySizeChecker.new(
current_size_proc: -> { repository.size.megabytes }, current_size_proc: -> { repository.size.megabytes },
limit: ::Gitlab::CurrentSettings.snippet_size_limit, limit: ::Gitlab::CurrentSettings.snippet_size_limit,
total_repository_size_excess: project&.namespace&.total_repository_size_excess, namespace: project&.namespace
additional_purchased_storage: project&.namespace&.additional_purchased_storage_size&.megabytes
) )
end end
end end
......
...@@ -27,7 +27,15 @@ module EE ...@@ -27,7 +27,15 @@ module EE
def additional_repo_storage_available? def additional_repo_storage_available?
return false unless ::Gitlab::CurrentSettings.automatic_purchased_storage_allocation? return false unless ::Gitlab::CurrentSettings.automatic_purchased_storage_allocation?
::Feature.enabled?(:additional_repo_storage_by_namespace) ::Feature.enabled?(:additional_repo_storage_by_namespace, namespace)
end
def total_repository_size_excess
namespace&.total_repository_size_excess.to_i
end
def additional_purchased_storage
namespace&.additional_purchased_storage_size&.megabytes.to_i
end end
def remaining_additional_purchased_storage def remaining_additional_purchased_storage
......
...@@ -5,6 +5,7 @@ require 'spec_helper' ...@@ -5,6 +5,7 @@ require 'spec_helper'
RSpec.describe Gitlab::RepositorySizeChecker do RSpec.describe Gitlab::RepositorySizeChecker do
let(:current_size) { 0 } let(:current_size) { 0 }
let(:limit) { 50 } let(:limit) { 50 }
let(:namespace) { build(:namespace, additional_purchased_storage_size: additional_purchased_storage) }
let(:total_repository_size_excess) { 0 } let(:total_repository_size_excess) { 0 }
let(:additional_purchased_storage) { 0 } let(:additional_purchased_storage) { 0 }
let(:enabled) { true } let(:enabled) { true }
...@@ -12,16 +13,17 @@ RSpec.describe Gitlab::RepositorySizeChecker do ...@@ -12,16 +13,17 @@ RSpec.describe Gitlab::RepositorySizeChecker do
subject do subject do
described_class.new( described_class.new(
current_size_proc: -> { current_size }, current_size_proc: -> { current_size.megabytes },
limit: limit, limit: limit.megabytes,
total_repository_size_excess: total_repository_size_excess, namespace: namespace,
additional_purchased_storage: additional_purchased_storage,
enabled: enabled enabled: enabled
) )
end end
before do before do
allow(Gitlab::CurrentSettings).to receive(:automatic_purchased_storage_allocation?).and_return(gitlab_setting_enabled) 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)
end end
describe '#above_size_limit?' do describe '#above_size_limit?' do
...@@ -33,13 +35,14 @@ RSpec.describe Gitlab::RepositorySizeChecker do ...@@ -33,13 +35,14 @@ RSpec.describe Gitlab::RepositorySizeChecker do
end end
context 'with feature flag :additional_repo_storage_by_namespace enabled' do context 'with feature flag :additional_repo_storage_by_namespace enabled' do
shared_examples 'feature flag :additional_repo_storage_by_namespace enabled' do
context 'when there is available excess storage' do context 'when there is available excess storage' do
it 'returns false' do it 'returns false' do
expect(subject.above_size_limit?).to eq(false) expect(subject.above_size_limit?).to eq(false)
end end
end end
context 'when size is above the limit and there is no exccess storage' do context 'when size is above the limit and there is no excess storage' do
let(:current_size) { 100 } let(:current_size) { 100 }
let(:total_repository_size_excess) { 20 } let(:total_repository_size_excess) { 20 }
let(:additional_purchased_storage) { 10 } let(:additional_purchased_storage) { 10 }
...@@ -54,6 +57,36 @@ RSpec.describe Gitlab::RepositorySizeChecker do ...@@ -54,6 +57,36 @@ RSpec.describe Gitlab::RepositorySizeChecker do
end end
end end
include_examples 'feature flag :additional_repo_storage_by_namespace enabled'
context 'when only enabled for a specific namespace' do
before do
stub_feature_flags(additional_repo_storage_by_namespace: false)
end
context 'when namespace is another one than the given one' do
let(:other_namespace) { create(:namespace) }
before do
stub_feature_flags(additional_repo_storage_by_namespace: other_namespace)
end
include_examples 'checker size above limit'
include_examples 'checker size not over limit'
end
context 'when namespace is the given one' do
let(:namespace) { create(:namespace, additional_purchased_storage_size: additional_purchased_storage) }
before do
stub_feature_flags(additional_repo_storage_by_namespace: namespace)
end
include_examples 'feature flag :additional_repo_storage_by_namespace enabled'
end
end
end
context 'with feature flag :additional_repo_storage_by_namespace disabled' do context 'with feature flag :additional_repo_storage_by_namespace disabled' do
before do before do
stub_feature_flags(additional_repo_storage_by_namespace: false) stub_feature_flags(additional_repo_storage_by_namespace: false)
...@@ -88,7 +121,7 @@ RSpec.describe Gitlab::RepositorySizeChecker do ...@@ -88,7 +121,7 @@ RSpec.describe Gitlab::RepositorySizeChecker do
let(:additional_purchased_storage) { 10 } let(:additional_purchased_storage) { 10 }
it 'returns 1' do it 'returns 1' do
expect(subject.exceeded_size).to eq(1) expect(subject.exceeded_size).to eq(1.megabytes)
end end
end end
...@@ -96,7 +129,7 @@ RSpec.describe Gitlab::RepositorySizeChecker do ...@@ -96,7 +129,7 @@ RSpec.describe Gitlab::RepositorySizeChecker do
let(:current_size) { 50 } let(:current_size) { 50 }
it 'returns 1' do it 'returns 1' do
expect(subject.exceeded_size(1)).to eq(1) expect(subject.exceeded_size(1.megabytes)).to eq(1.megabytes)
end end
end end
...@@ -104,7 +137,7 @@ RSpec.describe Gitlab::RepositorySizeChecker do ...@@ -104,7 +137,7 @@ RSpec.describe Gitlab::RepositorySizeChecker do
let(:current_size) { 49 } let(:current_size) { 49 }
it 'returns zero' do it 'returns zero' do
expect(subject.exceeded_size(1)).to eq(0) expect(subject.exceeded_size(1.megabytes)).to eq(0)
end end
end end
end end
......
...@@ -2245,22 +2245,6 @@ RSpec.describe Project do ...@@ -2245,22 +2245,6 @@ RSpec.describe Project do
end end
end end
describe '#total_repository_size_excess' do
it 'returns the total repository size excess of the namespace' do
allow(project.namespace).to receive(:total_repository_size_excess).and_return(50)
expect(checker.total_repository_size_excess).to eq(50)
end
end
describe '#additional_purchased_storage' do
it 'returns the additional purchased storage size of the namespace' do
allow(project.namespace).to receive(:additional_purchased_storage_size).and_return(100)
expect(checker.additional_purchased_storage).to eq(100.megabytes)
end
end
describe '#enabled?' do describe '#enabled?' do
it 'returns true when not equal to zero' do it 'returns true when not equal to zero' do
project.repository_size_limit = 1 project.repository_size_limit = 1
......
...@@ -14,21 +14,14 @@ RSpec.describe Snippet do ...@@ -14,21 +14,14 @@ RSpec.describe Snippet do
context 'when snippet belongs to a project' do context 'when snippet belongs to a project' do
subject { build(:project_snippet, project: project) } subject { build(:project_snippet, project: project) }
let(:namespace) { build(:namespace, additional_purchased_storage_size: 50) } let(:namespace) { build(:namespace) }
let(:project) { build(:project, namespace: namespace) } let(:project) { build(:project, namespace: namespace) }
let(:total_repository_size_excess) { 100 }
let(:additional_purchased_storage) { 50.megabytes }
before do
allow(namespace).to receive(:total_repository_size_excess).and_return(total_repository_size_excess)
end
include_examples 'size checker for snippet' include_examples 'size checker for snippet'
end end
context 'when snippet without a project' do context 'when snippet without a project' do
let(:total_repository_size_excess) { 0 } let(:namespace) { nil }
let(:additional_purchased_storage) { 0 }
include_examples 'size checker for snippet' include_examples 'size checker for snippet'
end end
......
...@@ -3,14 +3,13 @@ ...@@ -3,14 +3,13 @@
module Gitlab module Gitlab
# Centralized class for repository size related calculations. # Centralized class for repository size related calculations.
class RepositorySizeChecker class RepositorySizeChecker
attr_reader :limit, :total_repository_size_excess, :additional_purchased_storage attr_reader :limit
# @param current_size_proc [Proc] returns repository size in bytes # @param current_size_proc [Proc] returns repository size in bytes
def initialize(current_size_proc:, limit:, total_repository_size_excess:, additional_purchased_storage:, enabled: true) def initialize(current_size_proc:, limit:, namespace:, enabled: true)
@current_size_proc = current_size_proc @current_size_proc = current_size_proc
@limit = limit @limit = limit
@total_repository_size_excess = total_repository_size_excess.to_i @namespace = namespace
@additional_purchased_storage = additional_purchased_storage.to_i
@enabled = enabled && limit != 0 @enabled = enabled && limit != 0
end end
...@@ -44,6 +43,10 @@ module Gitlab ...@@ -44,6 +43,10 @@ module Gitlab
def error_message def error_message
@error_message_object ||= ::Gitlab::RepositorySizeErrorMessage.new(self) @error_message_object ||= ::Gitlab::RepositorySizeErrorMessage.new(self)
end end
private
attr_reader :namespace
end end
end end
......
...@@ -4,7 +4,7 @@ module Gitlab ...@@ -4,7 +4,7 @@ module Gitlab
class RepositorySizeErrorMessage class RepositorySizeErrorMessage
include ActiveSupport::NumberHelper include ActiveSupport::NumberHelper
delegate :current_size, :limit, :total_repository_size_excess, :additional_purchased_storage, :exceeded_size, to: :@checker delegate :current_size, :limit, :exceeded_size, to: :@checker
# @param checher [RepositorySizeChecker] # @param checher [RepositorySizeChecker]
def initialize(checker) def initialize(checker)
......
...@@ -3,16 +3,16 @@ ...@@ -3,16 +3,16 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::RepositorySizeChecker do RSpec.describe Gitlab::RepositorySizeChecker do
let_it_be(:namespace) { nil }
let(:current_size) { 0 } let(:current_size) { 0 }
let(:limit) { 50 } let(:limit) { 50 }
let(:enabled) { true } let(:enabled) { true }
subject do subject do
described_class.new( described_class.new(
current_size_proc: -> { current_size }, current_size_proc: -> { current_size.megabytes },
limit: limit, limit: limit.megabytes,
total_repository_size_excess: 0, namespace: namespace,
additional_purchased_storage: 0,
enabled: enabled enabled: enabled
) )
end end
...@@ -20,7 +20,7 @@ RSpec.describe Gitlab::RepositorySizeChecker do ...@@ -20,7 +20,7 @@ RSpec.describe Gitlab::RepositorySizeChecker do
describe '#enabled?' do describe '#enabled?' do
context 'when enabled' do context 'when enabled' do
it 'returns true' do it 'returns true' do
expect(subject.enabled?).to be_truthy expect(subject.enabled?).to eq(true)
end end
end end
...@@ -28,7 +28,7 @@ RSpec.describe Gitlab::RepositorySizeChecker do ...@@ -28,7 +28,7 @@ RSpec.describe Gitlab::RepositorySizeChecker do
let(:limit) { 0 } let(:limit) { 0 }
it 'returns false' do it 'returns false' do
expect(subject.enabled?).to be_falsey expect(subject.enabled?).to eq(false)
end end
end end
end end
...@@ -37,11 +37,11 @@ RSpec.describe Gitlab::RepositorySizeChecker do ...@@ -37,11 +37,11 @@ RSpec.describe Gitlab::RepositorySizeChecker do
let(:current_size) { 49 } let(:current_size) { 49 }
it 'returns true when changes go over' do it 'returns true when changes go over' do
expect(subject.changes_will_exceed_size_limit?(2)).to be_truthy expect(subject.changes_will_exceed_size_limit?(2.megabytes)).to eq(true)
end end
it 'returns false when changes do not go over' do it 'returns false when changes do not go over' do
expect(subject.changes_will_exceed_size_limit?(1)).to be_falsey expect(subject.changes_will_exceed_size_limit?(1.megabytes)).to eq(false)
end end
end end
......
...@@ -3,11 +3,11 @@ ...@@ -3,11 +3,11 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::RepositorySizeErrorMessage do RSpec.describe Gitlab::RepositorySizeErrorMessage do
let_it_be(:namespace) { build(:namespace) }
let(:checker) do let(:checker) do
Gitlab::RepositorySizeChecker.new( Gitlab::RepositorySizeChecker.new(
current_size_proc: -> { 15.megabytes }, current_size_proc: -> { 15.megabytes },
total_repository_size_excess: 0, namespace: namespace,
additional_purchased_storage: 0,
limit: 10.megabytes limit: 10.megabytes
) )
end end
...@@ -15,6 +15,10 @@ RSpec.describe Gitlab::RepositorySizeErrorMessage do ...@@ -15,6 +15,10 @@ RSpec.describe Gitlab::RepositorySizeErrorMessage do
let(:message) { checker.error_message } let(:message) { checker.error_message }
let(:base_message) { 'because this repository has exceeded its size limit of 10 MB by 5 MB' } let(:base_message) { 'because this repository has exceeded its size limit of 10 MB by 5 MB' }
before do
allow(namespace).to receive(:total_repository_size_excess).and_return(0)
end
describe 'error messages' do describe 'error messages' do
describe '#commit_error' do describe '#commit_error' do
it 'returns the correct message' do it 'returns the correct message' do
......
...@@ -666,8 +666,7 @@ RSpec.describe Snippet do ...@@ -666,8 +666,7 @@ RSpec.describe Snippet do
let(:checker) { subject.repository_size_checker } let(:checker) { subject.repository_size_checker }
let(:current_size) { 60 } let(:current_size) { 60 }
let(:total_repository_size_excess) { 0 } let(:namespace) { nil }
let(:additional_purchased_storage) { 0 }
before do before do
allow(subject.repository).to receive(:size).and_return(current_size) allow(subject.repository).to receive(:size).and_return(current_size)
......
...@@ -29,7 +29,7 @@ RSpec.shared_examples 'checker size exceeded' do ...@@ -29,7 +29,7 @@ RSpec.shared_examples 'checker size exceeded' do
let(:current_size) { 51 } let(:current_size) { 51 }
it 'returns zero' do it 'returns zero' do
expect(subject.exceeded_size).to eq(1) expect(subject.exceeded_size).to eq(1.megabytes)
end end
end end
...@@ -37,7 +37,7 @@ RSpec.shared_examples 'checker size exceeded' do ...@@ -37,7 +37,7 @@ RSpec.shared_examples 'checker size exceeded' do
let(:current_size) { 50 } let(:current_size) { 50 }
it 'returns zero' do it 'returns zero' do
expect(subject.exceeded_size(1)).to eq(1) expect(subject.exceeded_size(1.megabytes)).to eq(1.megabytes)
end end
end end
...@@ -45,7 +45,7 @@ RSpec.shared_examples 'checker size exceeded' do ...@@ -45,7 +45,7 @@ RSpec.shared_examples 'checker size exceeded' do
let(:current_size) { 49 } let(:current_size) { 49 }
it 'returns zero' do it 'returns zero' do
expect(subject.exceeded_size(1)).to eq(0) expect(subject.exceeded_size(1.megabytes)).to eq(0)
end end
end end
end end
...@@ -4,8 +4,7 @@ RSpec.shared_examples 'size checker for snippet' do |action| ...@@ -4,8 +4,7 @@ RSpec.shared_examples 'size checker for snippet' do |action|
it 'sets up size checker', :aggregate_failures do it 'sets up size checker', :aggregate_failures do
expect(checker.current_size).to eq(current_size.megabytes) expect(checker.current_size).to eq(current_size.megabytes)
expect(checker.limit).to eq(Gitlab::CurrentSettings.snippet_size_limit) expect(checker.limit).to eq(Gitlab::CurrentSettings.snippet_size_limit)
expect(checker.total_repository_size_excess).to eq(total_repository_size_excess)
expect(checker.additional_purchased_storage).to eq(additional_purchased_storage)
expect(checker.enabled?).to eq(true) expect(checker.enabled?).to eq(true)
expect(checker.instance_variable_get(:@namespace)).to eq(namespace)
end end
end 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