Commit 5e2323d5 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '229878-conditional-storage-limit-enforcement' into 'master'

Conditional enforcement of namespace storage limit

See merge request gitlab-org/gitlab!37869
parents 04caed3a 3629c806
...@@ -5,12 +5,15 @@ module EE ...@@ -5,12 +5,15 @@ module EE
CURRENT_SIZE_CACHE_KEY = 'root_storage_current_size' CURRENT_SIZE_CACHE_KEY = 'root_storage_current_size'
LIMIT_CACHE_KEY = 'root_storage_size_limit' LIMIT_CACHE_KEY = 'root_storage_size_limit'
EXPIRATION_TIME = 10.minutes EXPIRATION_TIME = 10.minutes
EFFECTIVE_DATE = 99.years.from_now.to_date
ENFORCEMENT_DATE = 100.years.from_now.to_date
def initialize(root_namespace) def initialize(root_namespace)
@root_namespace = root_namespace @root_namespace = root_namespace
end end
def above_size_limit? def above_size_limit?
return false unless enforce_limit?
return false if root_namespace.temporary_storage_increase_enabled? return false if root_namespace.temporary_storage_increase_enabled?
usage_ratio > 1 usage_ratio > 1
...@@ -35,8 +38,18 @@ module EE ...@@ -35,8 +38,18 @@ module EE
end end
end end
def enforce_limit?
return false if Date.current < ENFORCEMENT_DATE
return true unless closest_gitlab_subscription&.has_a_paid_hosted_plan?
closest_gitlab_subscription.start_date >= EFFECTIVE_DATE
end
private private
attr_reader :root_namespace attr_reader :root_namespace
delegate :closest_gitlab_subscription, to: :root_namespace
end end
end end
...@@ -25,29 +25,43 @@ RSpec.describe Namespace::RootStorageSize, type: :model do ...@@ -25,29 +25,43 @@ RSpec.describe Namespace::RootStorageSize, type: :model do
allow(namespace).to receive(:temporary_storage_increase_enabled?).and_return(false) allow(namespace).to receive(:temporary_storage_increase_enabled?).and_return(false)
end end
context 'when limit is 0' do context 'when limit enforcement is off' do
let(:limit) { 0 } before do
allow(model).to receive(:enforce_limit?).and_return(false)
end
it { is_expected.to eq(false) } it { is_expected.to eq(false) }
end end
context 'when below limit' do context 'when limit enforcement is on' do
it { is_expected.to eq(false) } before do
end allow(model).to receive(:enforce_limit?).and_return(true)
end
context 'when above limit' do context 'when limit is 0' do
let(:current_size) { 101.megabytes } let(:limit) { 0 }
context 'when temporary storage increase is disabled' do it { is_expected.to eq(false) }
it { is_expected.to eq(true) }
end end
context 'when temporary storage increase is enabled' do context 'when below limit' do
before do it { is_expected.to eq(false) }
allow(namespace).to receive(:temporary_storage_increase_enabled?).and_return(true) end
context 'when above limit' do
let(:current_size) { 101.megabytes }
context 'when temporary storage increase is disabled' do
it { is_expected.to eq(true) }
end end
it { is_expected.to eq(false) } context 'when temporary storage increase is enabled' do
before do
allow(namespace).to receive(:temporary_storage_increase_enabled?).and_return(true)
end
it { is_expected.to eq(false) }
end
end end
end end
end end
...@@ -133,4 +147,56 @@ RSpec.describe Namespace::RootStorageSize, type: :model do ...@@ -133,4 +147,56 @@ RSpec.describe Namespace::RootStorageSize, type: :model do
end end
end end
end end
describe '#enforce_limit?' do
subject { model.enforce_limit? }
around do |example|
Timecop.travel(current_date) { example.run }
end
context 'when current date is before enforcement date' do
let(:current_date) { described_class::ENFORCEMENT_DATE - 1.day }
it { is_expected.to eq(false) }
end
context 'when current date is on or after enforcement date' do
let(:current_date) { described_class::ENFORCEMENT_DATE }
context 'when no subscription is found for namespace' do
before do
subscription.destroy!
end
it { is_expected.to eq(true) }
end
context 'when subscription is for a free plan' do
let!(:subscription) do
create(:gitlab_subscription, namespace: namespace, hosted_plan: create(:free_plan))
end
it { is_expected.to eq(true) }
end
context 'when subscription is for a paid plan' do
before do
allow(subscription).to receive(:start_date).and_return(start_date)
end
context 'when subscription start date is before effective date' do
let(:start_date) { described_class::EFFECTIVE_DATE - 1.day }
it { is_expected.to eq(false) }
end
context 'when subscription start date is on or after effective date' do
let(:start_date) { described_class::EFFECTIVE_DATE }
it { is_expected.to eq(true) }
end
end
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