Commit 4f2e7ab3 authored by Marius Bobin's avatar Marius Bobin

Fix namespace checks for live quota consumption

Changelog: fixed
EE: true
parent 457ef961
...@@ -274,11 +274,6 @@ class Namespace < ApplicationRecord ...@@ -274,11 +274,6 @@ class Namespace < ApplicationRecord
projects.with_shared_runners.any? projects.with_shared_runners.any?
end end
# Internal Gitlab owned namespaces only (example: gitlab-org)
def unlimited_minutes?
shared_runners_minutes_limit == 0
end
def user_ids_for_project_authorizations def user_ids_for_project_authorizations
[owner_id] [owner_id]
end end
......
...@@ -77,8 +77,6 @@ module Ci ...@@ -77,8 +77,6 @@ module Ci
ServiceResponse.error(message: 'Feature not enabled') ServiceResponse.error(message: 'Feature not enabled')
elsif !build.running? elsif !build.running?
ServiceResponse.error(message: 'Build is not running') ServiceResponse.error(message: 'Build is not running')
elsif root_namespace.unlimited_minutes?
ServiceResponse.error(message: 'Namespace has unlimited minutes')
elsif !build.cost_factor_enabled? elsif !build.cost_factor_enabled?
ServiceResponse.error(message: 'Cost factor not enabled for build') ServiceResponse.error(message: 'Cost factor not enabled for build')
else else
......
...@@ -22,6 +22,7 @@ module Gitlab ...@@ -22,6 +22,7 @@ module Gitlab
def for_project(project) def for_project(project)
return 0.0 unless @runner_matcher.instance_type? return 0.0 unless @runner_matcher.instance_type?
return 0.0 unless project.ci_minutes_quota.enabled?
cost_factor = for_visibility(project.visibility_level) cost_factor = for_visibility(project.visibility_level)
......
...@@ -65,7 +65,7 @@ RSpec.describe Gitlab::Ci::Matching::RunnerMatcher do ...@@ -65,7 +65,7 @@ RSpec.describe Gitlab::Ci::Matching::RunnerMatcher do
before do before do
allow(project) allow(project)
.to receive(:ci_minutes_quota) .to receive(:ci_minutes_quota)
.and_return(double(minutes_used_up?: quota_minutes_used_up)) .and_return(double(minutes_used_up?: quota_minutes_used_up, enabled?: true))
end end
it { is_expected.to eq(result) } it { is_expected.to eq(result) }
......
...@@ -33,6 +33,7 @@ RSpec.describe Gitlab::Ci::Minutes::BuildConsumption do ...@@ -33,6 +33,7 @@ RSpec.describe Gitlab::Ci::Minutes::BuildConsumption do
project.update!(visibility_level: visibility_level) project.update!(visibility_level: visibility_level)
allow(build).to receive(:duration).and_return(duration) allow(build).to receive(:duration).and_return(duration)
allow(::Gitlab::CurrentSettings).to receive(:shared_runners_minutes) { 400 }
end end
it 'returns the expected consumption' do it 'returns the expected consumption' do
......
...@@ -51,6 +51,10 @@ RSpec.describe Ci::Build do ...@@ -51,6 +51,10 @@ RSpec.describe Ci::Build do
describe '#cost_factor_enabled?' do describe '#cost_factor_enabled?' do
subject { job.cost_factor_enabled? } subject { job.cost_factor_enabled? }
before do
allow(::Gitlab::CurrentSettings).to receive(:shared_runners_minutes) { 400 }
end
context 'for shared runner' do context 'for shared runner' do
before do before do
job.runner = create(:ci_runner, :instance) job.runner = create(:ci_runner, :instance)
......
...@@ -3,6 +3,12 @@ ...@@ -3,6 +3,12 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe EE::Ci::Runner do RSpec.describe EE::Ci::Runner do
let(:shared_runners_minutes) { 400 }
before do
allow(::Gitlab::CurrentSettings).to receive(:shared_runners_minutes) { shared_runners_minutes }
end
describe '#cost_factor_for_project' do describe '#cost_factor_for_project' do
subject { runner.cost_factor_for_project(project) } subject { runner.cost_factor_for_project(project) }
...@@ -42,6 +48,12 @@ RSpec.describe EE::Ci::Runner do ...@@ -42,6 +48,12 @@ RSpec.describe EE::Ci::Runner do
let(:project) { create(:project, visibility_level: ::Gitlab::VisibilityLevel::PRIVATE) } let(:project) { create(:project, visibility_level: ::Gitlab::VisibilityLevel::PRIVATE) }
it { is_expected.to eq(1.1) } it { is_expected.to eq(1.1) }
context 'with unlimited minutes' do
let(:shared_runners_minutes) { 400 }
it { is_expected.to eq(1.1) }
end
end end
context 'with public visibility level' do context 'with public visibility level' do
...@@ -85,18 +97,26 @@ RSpec.describe EE::Ci::Runner do ...@@ -85,18 +97,26 @@ RSpec.describe EE::Ci::Runner do
end end
describe '#cost_factor_enabled?' do describe '#cost_factor_enabled?' do
let_it_be(:project) do let_it_be_with_reload(:project) do
namespace = create(:group, created_at: Date.new(2021, 7, 16)) namespace = create(:group, created_at: Date.new(2021, 7, 16))
create(:project, namespace: namespace) create(:project, namespace: namespace)
end end
context 'when the project has any cost factor' do context 'when the project has any cost factor' do
it 'returns true' do let(:runner) do
runner = create(:ci_runner, :instance, create(:ci_runner, :instance,
private_projects_minutes_cost_factor: 1, private_projects_minutes_cost_factor: 1,
public_projects_minutes_cost_factor: 0) public_projects_minutes_cost_factor: 0)
end
subject { runner.cost_factor_enabled?(project) }
it { is_expected.to be_truthy }
context 'with unlimited minutes' do
let(:shared_runners_minutes) { 0 }
expect(runner.cost_factor_enabled?(project)).to be_truthy it { is_expected.to be_falsy }
end end
end end
......
...@@ -82,10 +82,11 @@ RSpec.describe Ci::Minutes::TrackLiveConsumptionService do ...@@ -82,10 +82,11 @@ RSpec.describe Ci::Minutes::TrackLiveConsumptionService do
context 'when namespace has unlimited minutes' do context 'when namespace has unlimited minutes' do
before do before do
namespace.update!(shared_runners_minutes_limit: 0) quota = double('quota', enabled?: false)
allow(project).to receive(:ci_minutes_quota).and_return(quota)
end end
it_behaves_like 'returns early', 'Namespace has unlimited minutes' it_behaves_like 'returns early', 'Cost factor not enabled for build'
end end
context 'when build has not been tracked recently' do context 'when build has not been tracked recently' do
......
...@@ -40,7 +40,7 @@ RSpec.describe Ci::PipelineCreation::DropNotRunnableBuildsService do ...@@ -40,7 +40,7 @@ RSpec.describe Ci::PipelineCreation::DropNotRunnableBuildsService do
shared_examples 'limit exceeded' do shared_examples 'limit exceeded' do
before do before do
allow(pipeline.project).to receive(:ci_minutes_quota) allow(pipeline.project).to receive(:ci_minutes_quota)
.and_return(double('quota', minutes_used_up?: true)) .and_return(double('quota', minutes_used_up?: true, enabled?: true))
end end
it 'drops the job with ci_quota_exceeded reason' do it 'drops the job with ci_quota_exceeded reason' do
...@@ -96,7 +96,7 @@ RSpec.describe Ci::PipelineCreation::DropNotRunnableBuildsService do ...@@ -96,7 +96,7 @@ RSpec.describe Ci::PipelineCreation::DropNotRunnableBuildsService do
context 'when the CI quota is exceeded' do context 'when the CI quota is exceeded' do
before do before do
allow(pipeline.project).to receive(:ci_minutes_quota) allow(pipeline.project).to receive(:ci_minutes_quota)
.and_return(double('quota', minutes_used_up?: true)) .and_return(double('quota', minutes_used_up?: true, enabled?: true))
end end
it_behaves_like 'jobs allowed to run' it_behaves_like 'jobs allowed to run'
......
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