Commit 3b46ce3c authored by Sean McGivern's avatar Sean McGivern

Merge branch 'ci-minutes-refactoring-drop-migrated-method' into 'master'

Move `extra_shared_runners_minutes_used?` to `Quota#minutes_used_up?`

See merge request gitlab-org/gitlab!33793
parents 62e5cc0d 0421a2ac
......@@ -3,12 +3,9 @@
module Ci
module Minutes
class Context
delegate :shared_runners_remaining_minutes_below_threshold?,
:shared_runners_minutes_used?,
:shared_runners_minutes_limit_enabled?, to: :level
delegate :shared_runners_minutes_limit_enabled?, to: :level
delegate :name, to: :namespace, prefix: true
delegate :last_ci_minutes_usage_notification_level,
:shared_runners_remaining_minutes_percent, to: :namespace
delegate :shared_runners_remaining_minutes_percent, to: :namespace
attr_reader :level
......
......@@ -43,15 +43,18 @@ module Ci
100 * purchased_minutes_used.to_i / purchased_minutes
end
def minutes_used_up?
namespace.shared_runners_minutes_limit_enabled? &&
total_minutes_used >= total_minutes
end
private
# TODO: maps to Namespace#shared_runners_minutes_used?
def monthly_minutes_used_up?
namespace.shared_runners_minutes_limit_enabled? &&
monthly_minutes_used >= monthly_minutes
end
# TODO: maps to Namespace#extra_shared_runners_minutes_used?
def purchased_minutes_used_up?
namespace.shared_runners_minutes_limit_enabled? &&
any_minutes_purchased? &&
......@@ -60,18 +63,18 @@ module Ci
# TODO: maps to NamespaceStatistics#shared_runners_minutes(include_extra: false)
def monthly_minutes_used
minutes_used - purchased_minutes_used
total_minutes_used - purchased_minutes_used
end
def monthly_minutes_available?
minutes_used <= monthly_minutes
total_minutes_used <= monthly_minutes
end
# TODO: maps to NamespaceStatistics#extra_shared_runners_minutes
def purchased_minutes_used
return 0 if no_minutes_purchased? || monthly_minutes_available?
minutes_used - monthly_minutes
total_minutes_used - monthly_minutes
end
def no_minutes_purchased?
......@@ -82,12 +85,17 @@ module Ci
purchased_minutes > 0
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)
def minutes_used
@minutes_used ||= namespace.shared_runners_seconds.to_i / 60
def total_minutes_used
@total_minutes_used ||= namespace.shared_runners_seconds.to_i / 60
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
@monthly_minutes ||= (namespace.shared_runners_minutes_limit || ::Gitlab::CurrentSettings.shared_runners_minutes).to_i
end
......
......@@ -250,11 +250,6 @@ module EE
actual_shared_runners_minutes_limit.nonzero?
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
return 0 if shared_runners_remaining_minutes.to_f <= 0
return 0 if actual_shared_runners_minutes_limit.to_f == 0
......
......@@ -178,7 +178,6 @@ module EE
to: :statistics, allow_nil: true
delegate :actual_shared_runners_minutes_limit,
:shared_runners_minutes_used?,
:shared_runners_remaining_minutes_below_threshold?, to: :shared_runners_limit_namespace
delegate :last_update_succeeded?, :last_update_failed?,
......@@ -312,7 +311,7 @@ module EE
end
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
def link_pool_repository
......
......@@ -9,10 +9,7 @@ RSpec.describe Ci::Minutes::Context do
describe 'delegation' do
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(:name).to(:namespace).with_prefix }
it { is_expected.to delegate_method(:last_ci_minutes_usage_notification_level).to(:namespace) }
end
end
......@@ -227,4 +227,32 @@ RSpec.describe Ci::Minutes::Quota do
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
......@@ -648,117 +648,6 @@ RSpec.describe Namespace do
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
let(:namespace) { build(:namespace) }
......
......@@ -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(: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(:closest_gitlab_subscription).to(:namespace) }
......@@ -968,12 +967,21 @@ RSpec.describe Project do
shared_runners_enabled: true)
end
before do
expect(namespace).to receive(:shared_runners_minutes_used?).and_call_original
it 'shared runners are not available' do
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
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
......
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