Commit 599ba42d authored by Grzegorz Bizon's avatar Grzegorz Bizon

Implement active pipelines limit

parent 7696a43e
module EE
module Gitlab
module Ci
##
# Abstract base class for CI/CD Quotas
#
class Limit
def initialize(_context, _resource)
end
def enabled?
raise NotImplementedError
end
def exceeded?
raise NotImplementedError
end
def message
raise NotImplementedError
end
end
end
end
end
module EE
module Gitlab
module Ci
module Pipeline
module Quota
class Activity < Ci::Limit
include ActionView::Helpers::TextHelper
def initialize(namespace, project)
@namespace = namespace
@project = project
end
def enabled?
@namespace.max_active_pipelines > 0
end
def exceeded?
return false unless enabled?
excessive_pipelines_count > 0
end
def message
return unless exceeded?
'Active pipelines limit exceeded by ' \
"#{pluralize(excessive_pipelines_count, 'pipeline')}!"
end
private
def excessive_pipelines_count
@excessive ||= alive_pipelines_count - max_active_pipelines_count
end
def alive_pipelines_count
@project.pipelines.alive.count
end
def max_active_pipelines_count
@namespace.max_active_pipelines
end
end
end
end
end
end
end
require 'spec_helper'
describe EE::Gitlab::Ci::Pipeline::Quota::Activity do
set(:namespace) { create(:namespace, plan: EE::Namespace::GOLD_PLAN) }
set(:project) { create(:project, namespace: namespace) }
let(:limit) { described_class.new(namespace, project) }
shared_context 'pipeline activity limit exceeded' do
before do
create(:ci_pipeline, project: project, status: 'created')
create(:ci_pipeline, project: project, status: 'pending')
create(:ci_pipeline, project: project, status: 'running')
namespace.plan.update_column(:active_pipelines_limit, 1)
end
end
shared_context 'pipeline activity limit not exceeded' do
before do
namespace.plan.update_column(:active_pipelines_limit, 2)
end
end
describe '#enabled?' do
context 'when limit is enabled in plan' do
before do
namespace.plan.update_column(:active_pipelines_limit, 10)
end
it 'is enabled' do
expect(limit).to be_enabled
end
end
context 'when limit is not enabled' do
before do
namespace.plan.update_column(:active_pipelines_limit, 0)
end
it 'is not enabled' do
expect(limit).not_to be_enabled
end
end
end
describe '#exceeded?' do
context 'when limit is exceeded' do
include_context 'pipeline activity limit exceeded'
it 'is exceeded' do
expect(limit).to be_exceeded
end
end
context 'when limit is not exceeded' do
include_context 'pipeline activity limit not exceeded'
it 'is not exceeded' do
expect(limit).not_to be_exceeded
end
end
end
describe '#message' do
context 'when limit is exceeded' do
include_context 'pipeline activity limit exceeded'
it 'returns info about pipeline activity limit exceeded' do
expect(limit.message)
.to eq "Active pipelines limit exceeded by 2 pipelines!"
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