Commit 0421a2ac authored by Fabio Pitino's avatar Fabio Pitino

Drop extra_shared_runners_minutes_used? method

Method has been migrated into Ci::Minutes::Quota
module as `purchased_minutes_used_up?`

Move `shared_runners_minutes_used?` to Quota class

Move Namespace#shared_runners_minutes_used? to the new
Ci::Minutes::Quota#minutes_used_up?

Renamed minutes_used to total_minutes_used
parent b9f85b19
...@@ -3,12 +3,9 @@ ...@@ -3,12 +3,9 @@
module Ci module Ci
module Minutes module Minutes
class Context class Context
delegate :shared_runners_remaining_minutes_below_threshold?, delegate :shared_runners_minutes_limit_enabled?, to: :level
:shared_runners_minutes_used?,
:shared_runners_minutes_limit_enabled?, to: :level
delegate :name, to: :namespace, prefix: true delegate :name, to: :namespace, prefix: true
delegate :last_ci_minutes_usage_notification_level, delegate :shared_runners_remaining_minutes_percent, to: :namespace
:shared_runners_remaining_minutes_percent, to: :namespace
attr_reader :level attr_reader :level
......
...@@ -43,15 +43,18 @@ module Ci ...@@ -43,15 +43,18 @@ module Ci
100 * purchased_minutes_used.to_i / purchased_minutes 100 * purchased_minutes_used.to_i / purchased_minutes
end end
def minutes_used_up?
namespace.shared_runners_minutes_limit_enabled? &&
total_minutes_used >= total_minutes
end
private private
# TODO: maps to Namespace#shared_runners_minutes_used?
def monthly_minutes_used_up? def monthly_minutes_used_up?
namespace.shared_runners_minutes_limit_enabled? && namespace.shared_runners_minutes_limit_enabled? &&
monthly_minutes_used >= monthly_minutes monthly_minutes_used >= monthly_minutes
end end
# TODO: maps to Namespace#extra_shared_runners_minutes_used?
def purchased_minutes_used_up? def purchased_minutes_used_up?
namespace.shared_runners_minutes_limit_enabled? && namespace.shared_runners_minutes_limit_enabled? &&
any_minutes_purchased? && any_minutes_purchased? &&
...@@ -60,18 +63,18 @@ module Ci ...@@ -60,18 +63,18 @@ module Ci
# TODO: maps to NamespaceStatistics#shared_runners_minutes(include_extra: false) # TODO: maps to NamespaceStatistics#shared_runners_minutes(include_extra: false)
def monthly_minutes_used def monthly_minutes_used
minutes_used - purchased_minutes_used total_minutes_used - purchased_minutes_used
end end
def monthly_minutes_available? def monthly_minutes_available?
minutes_used <= monthly_minutes total_minutes_used <= monthly_minutes
end end
# TODO: maps to NamespaceStatistics#extra_shared_runners_minutes # TODO: maps to NamespaceStatistics#extra_shared_runners_minutes
def purchased_minutes_used def purchased_minutes_used
return 0 if no_minutes_purchased? || monthly_minutes_available? return 0 if no_minutes_purchased? || monthly_minutes_available?
minutes_used - monthly_minutes total_minutes_used - monthly_minutes
end end
def no_minutes_purchased? def no_minutes_purchased?
...@@ -82,12 +85,17 @@ module Ci ...@@ -82,12 +85,17 @@ module Ci
purchased_minutes > 0 purchased_minutes > 0
end end
# TODO: maps to Namespace#actual_shared_runners_minutes_limit(include_extra: true)
def total_minutes
@total_minutes ||= monthly_minutes + purchased_minutes
end
# TODO: maps to NamespaceStatistics#shared_runners_minutes(include_extra: true) # TODO: maps to NamespaceStatistics#shared_runners_minutes(include_extra: true)
def minutes_used def total_minutes_used
@minutes_used ||= namespace.shared_runners_seconds.to_i / 60 @total_minutes_used ||= namespace.shared_runners_seconds.to_i / 60
end end
# TODO: maps to Namespace#actual_shared_runners_minuts_limit(include_extra: false) # TODO: maps to Namespace#actual_shared_runners_minutes_limit(include_extra: false)
def monthly_minutes def monthly_minutes
@monthly_minutes ||= (namespace.shared_runners_minutes_limit || ::Gitlab::CurrentSettings.shared_runners_minutes).to_i @monthly_minutes ||= (namespace.shared_runners_minutes_limit || ::Gitlab::CurrentSettings.shared_runners_minutes).to_i
end end
......
...@@ -250,11 +250,6 @@ module EE ...@@ -250,11 +250,6 @@ module EE
actual_shared_runners_minutes_limit.nonzero? actual_shared_runners_minutes_limit.nonzero?
end end
def shared_runners_minutes_used?
shared_runners_minutes_limit_enabled? &&
shared_runners_minutes.to_i >= actual_shared_runners_minutes_limit
end
def shared_runners_remaining_minutes_percent def shared_runners_remaining_minutes_percent
return 0 if shared_runners_remaining_minutes.to_f <= 0 return 0 if shared_runners_remaining_minutes.to_f <= 0
return 0 if actual_shared_runners_minutes_limit.to_f == 0 return 0 if actual_shared_runners_minutes_limit.to_f == 0
......
...@@ -178,7 +178,6 @@ module EE ...@@ -178,7 +178,6 @@ module EE
to: :statistics, allow_nil: true to: :statistics, allow_nil: true
delegate :actual_shared_runners_minutes_limit, delegate :actual_shared_runners_minutes_limit,
:shared_runners_minutes_used?,
:shared_runners_remaining_minutes_below_threshold?, to: :shared_runners_limit_namespace :shared_runners_remaining_minutes_below_threshold?, to: :shared_runners_limit_namespace
delegate :last_update_succeeded?, :last_update_failed?, delegate :last_update_succeeded?, :last_update_failed?,
...@@ -311,7 +310,7 @@ module EE ...@@ -311,7 +310,7 @@ module EE
end end
def shared_runners_available? def shared_runners_available?
super && !shared_runners_limit_namespace.shared_runners_minutes_used? super && !::Ci::Minutes::Quota.new(shared_runners_limit_namespace).minutes_used_up?
end end
def link_pool_repository def link_pool_repository
......
...@@ -9,10 +9,7 @@ RSpec.describe Ci::Minutes::Context do ...@@ -9,10 +9,7 @@ RSpec.describe Ci::Minutes::Context do
describe 'delegation' do describe 'delegation' do
subject { described_class.new(project, group) } subject { described_class.new(project, group) }
it { is_expected.to delegate_method(:shared_runners_remaining_minutes_below_threshold?).to(:level) }
it { is_expected.to delegate_method(:shared_runners_minutes_used?).to(:level) }
it { is_expected.to delegate_method(:shared_runners_minutes_limit_enabled?).to(:level) } it { is_expected.to delegate_method(:shared_runners_minutes_limit_enabled?).to(:level) }
it { is_expected.to delegate_method(:name).to(:namespace).with_prefix } it { is_expected.to delegate_method(:name).to(:namespace).with_prefix }
it { is_expected.to delegate_method(:last_ci_minutes_usage_notification_level).to(:namespace) }
end end
end end
...@@ -227,4 +227,32 @@ RSpec.describe Ci::Minutes::Quota do ...@@ -227,4 +227,32 @@ RSpec.describe Ci::Minutes::Quota do
end end
end end
end end
describe '#minutes_used_up?' do
subject { quota.minutes_used_up? }
where(:limit_enabled, :monthly_limit, :purchased_limit, :minutes_used, :result, :title) do
false | 0 | 0 | 40 | false | 'limit not enabled'
true | 0 | 200 | 40 | false | 'monthly limit not set and purchased limit set and low usage'
true | 200 | 0 | 40 | false | 'monthly limit set and purchased limit not set and usage below monthly'
true | 200 | 0 | 240 | true | 'monthly limit set and purchased limit not set and usage above monthly'
true | 200 | 200 | 0 | false | 'monthly and purchased limits set and no usage'
true | 200 | 200 | 40 | false | 'monthly and purchased limits set and usage below monthly'
true | 200 | 200 | 200 | false | 'monthly and purchased limits set and monthly minutes maxed out'
true | 200 | 200 | 300 | false | 'monthly and purchased limits set and some purchased minutes used'
true | 200 | 200 | 400 | true | 'monthly and purchased limits set and all minutes used'
true | 200 | 200 | 430 | true | 'monthly and purchased limits set and usage beyond all limits'
end
with_them do
before do
allow(namespace).to receive(:shared_runners_minutes_limit_enabled?).and_return(limit_enabled)
namespace.shared_runners_minutes_limit = monthly_limit
namespace.extra_shared_runners_minutes_limit = purchased_limit
namespace.namespace_statistics.shared_runners_seconds = minutes_used.minutes
end
it { is_expected.to eq(result) }
end
end
end end
...@@ -648,117 +648,6 @@ RSpec.describe Namespace do ...@@ -648,117 +648,6 @@ RSpec.describe Namespace do
end end
end end
describe '#extra_shared_runners_minutes_used?' do
subject { namespace.extra_shared_runners_minutes_used? }
context 'with project' do
let!(:project) do
create(:project, namespace: namespace, shared_runners_enabled: true)
end
context 'shared_runners_minutes_limit is not enabled' do
before do
allow(namespace).to receive(:shared_runners_minutes_limit_enabled?).and_return(false)
end
it { is_expected.to be_falsey }
end
context 'shared_runners_minutes_limit is enabled' do
context 'when limit is defined' do
before do
namespace.update_attribute(:extra_shared_runners_minutes_limit, 100)
end
context "when usage is below the quota" do
before do
allow(namespace).to receive(:extra_shared_runners_minutes).and_return(50)
end
it { is_expected.to be_falsey }
end
context "when usage is above the quota" do
before do
allow(namespace).to receive(:extra_shared_runners_minutes).and_return(101)
end
it { is_expected.to be_truthy }
end
context 'and main limit is unlimited' do
before do
namespace.update_attribute(:shared_runners_minutes_limit, 0)
end
context "and it's above the quota" do
it { is_expected.to be_falsey }
end
end
end
context 'without limit' do
before do
namespace.update_attribute(:shared_runners_minutes_limit, 100)
namespace.update_attribute(:extra_shared_runners_minutes_limit, nil)
end
context 'when main usage is above the quota' do
before do
allow(namespace).to receive(:shared_runners_minutes).and_return(101)
end
it { is_expected.to be_falsey }
end
end
end
end
context 'without project' do
it { is_expected.to be_falsey }
end
end
describe '#shared_runners_minutes_used?' do
subject { namespace.shared_runners_minutes_used? }
context 'with project' do
let!(:project) do
create(:project,
namespace: namespace,
shared_runners_enabled: true)
end
context 'when limit is defined' do
context 'when limit is used' do
let(:namespace) { create(:namespace, :with_used_build_minutes_limit) }
it { is_expected.to be_truthy }
end
context 'when limit not yet used' do
let(:namespace) { create(:namespace, :with_not_used_build_minutes_limit) }
it { is_expected.to be_falsey }
end
context 'when minutes are not yet set' do
it { is_expected.to be_falsey }
end
end
context 'without limit' do
let(:namespace) { create(:namespace, :with_build_minutes_limit) }
it { is_expected.to be_falsey }
end
end
context 'without project' do
it { is_expected.to be_falsey }
end
end
describe '#shared_runners_remaining_minutes_percent' do describe '#shared_runners_remaining_minutes_percent' do
let(:namespace) { build(:namespace) } let(:namespace) { build(:namespace) }
......
...@@ -16,7 +16,6 @@ RSpec.describe Project do ...@@ -16,7 +16,6 @@ RSpec.describe Project do
it { is_expected.to delegate_method(:actual_shared_runners_minutes_limit).to(:shared_runners_limit_namespace) } it { is_expected.to delegate_method(:actual_shared_runners_minutes_limit).to(:shared_runners_limit_namespace) }
it { is_expected.to delegate_method(:shared_runners_minutes_limit_enabled?).to(:shared_runners_limit_namespace) } it { is_expected.to delegate_method(:shared_runners_minutes_limit_enabled?).to(:shared_runners_limit_namespace) }
it { is_expected.to delegate_method(:shared_runners_minutes_used?).to(:shared_runners_limit_namespace) }
it { is_expected.to delegate_method(:shared_runners_remaining_minutes_below_threshold?).to(:shared_runners_limit_namespace) } it { is_expected.to delegate_method(:shared_runners_remaining_minutes_below_threshold?).to(:shared_runners_limit_namespace) }
it { is_expected.to delegate_method(:closest_gitlab_subscription).to(:namespace) } it { is_expected.to delegate_method(:closest_gitlab_subscription).to(:namespace) }
...@@ -966,12 +965,21 @@ RSpec.describe Project do ...@@ -966,12 +965,21 @@ RSpec.describe Project do
shared_runners_enabled: true) shared_runners_enabled: true)
end end
before do it 'shared runners are not available' do
expect(namespace).to receive(:shared_runners_minutes_used?).and_call_original expect(project.shared_runners_available?).to be_falsey
end
end
context 'without used pipeline minutes' do
let(:namespace) { create(:namespace, :with_not_used_build_minutes_limit) }
let(:project) do
create(:project,
namespace: namespace,
shared_runners_enabled: true)
end end
it 'shared runners are not available' do it 'shared runners are not available' do
expect(project.shared_runners_available?).to be_falsey expect(project.shared_runners_available?).to be_truthy
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