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
CURRENT_SIZE_CACHE_KEY = 'root_storage_current_size'
LIMIT_CACHE_KEY = 'root_storage_size_limit'
EXPIRATION_TIME = 10.minutes
EFFECTIVE_DATE = 99.years.from_now.to_date
ENFORCEMENT_DATE = 100.years.from_now.to_date
def initialize(root_namespace)
@root_namespace = root_namespace
end
def above_size_limit?
return false unless enforce_limit?
return false if root_namespace.temporary_storage_increase_enabled?
usage_ratio > 1
......@@ -35,8 +38,18 @@ module EE
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
attr_reader :root_namespace
delegate :closest_gitlab_subscription, to: :root_namespace
end
end
......@@ -25,29 +25,43 @@ RSpec.describe Namespace::RootStorageSize, type: :model do
allow(namespace).to receive(:temporary_storage_increase_enabled?).and_return(false)
end
context 'when limit is 0' do
let(:limit) { 0 }
context 'when limit enforcement is off' do
before do
allow(model).to receive(:enforce_limit?).and_return(false)
end
it { is_expected.to eq(false) }
end
context 'when below limit' do
it { is_expected.to eq(false) }
end
context 'when limit enforcement is on' do
before do
allow(model).to receive(:enforce_limit?).and_return(true)
end
context 'when above limit' do
let(:current_size) { 101.megabytes }
context 'when limit is 0' do
let(:limit) { 0 }
context 'when temporary storage increase is disabled' do
it { is_expected.to eq(true) }
it { is_expected.to eq(false) }
end
context 'when temporary storage increase is enabled' do
before do
allow(namespace).to receive(:temporary_storage_increase_enabled?).and_return(true)
context 'when below limit' do
it { is_expected.to eq(false) }
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
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
......@@ -133,4 +147,56 @@ RSpec.describe Namespace::RootStorageSize, type: :model do
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
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