Commit aa20cedc authored by Kamil Trzcinski's avatar Kamil Trzcinski

Abstract EE-code into separate mixins

parent 0b6ad31c
class ProjectMetrics < ActiveRecord::Base
belongs_to :project
validates :project, presence: true
end
module EE
module Ci
# This class responsible for assigning
# proper pending build to runner on runner API request
class RegisterBuildService
# RegisterBuildService EE mixin
#
# This module is intended to encapsulate EE-specific service logic
# and be included in the `RegisterBuildService` service
module RegisterBuildService
extend ActiveSupport::Prependable
def builds_for_shared_runner
......@@ -15,7 +17,7 @@ module EE
end
def builds_check_limit
Namespace.reorder(nil).
::Namespace.reorder(nil).
where('namespaces.id = projects.namespace_id').
joins('LEFT JOIN namespace_statistics ON namespace_statistics.namespace_id = namespaces.id').
where('COALESCE(namespaces.shared_runners_minutes_limit, ?, 0) = 0 OR ' \
......
FactoryGirl.define do
factory :project_metrics do
project factory: :empty_project
end
end
......@@ -130,75 +130,19 @@ module Ci
end
end
context 'for project with shared runners when global minutes limit is set' do
before do
project.update(shared_runners_enabled: true)
stub_application_setting(shared_runners_minutes: 500)
end
context 'allow to pick builds' do
let(:build) { execute(shared_runner) }
it { expect(build).to be_kind_of(Build) }
end
context 'when over the global quota' do
before do
project.namespace.create_namespace_metrics(
shared_runners_minutes: 600)
end
let(:build) { execute(shared_runner) }
it "does not return a build" do
expect(build).to be_nil
end
context 'when project is public' do
before do
project.update(visibility_level: Project::PUBLIC)
end
it "does return the build" do
expect(build).to be_kind_of(Build)
end
end
context 'when namespace limit is set to unlimited' do
before do
project.namespace.update(shared_runners_minutes_limit: 0)
end
it "does return the build" do
expect(build).to be_kind_of(Build)
end
end
context 'when namespace quota is bigger than a global one' do
before do
project.namespace.update(shared_runners_minutes_limit: 1000)
end
it "does return the build" do
expect(build).to be_kind_of(Build)
end
end
end
end
context 'disallow shared runners' do
before do
project.update(shared_runners_enabled: false)
end
context 'shared runner' do
let(:build) { execute(shared_runner) }
let(:build) { service.execute(shared_runner) }
it { expect(build).to be_nil }
end
context 'specific runner' do
let(:build) { execute(specific_runner) }
let(:build) { service.execute(specific_runner) }
it { expect(build).to be_kind_of(Build) }
it { expect(build).to be_valid }
......
require 'spec_helper'
module Ci
describe RegisterBuildService, services: true do
let!(:project) { create :empty_project, shared_runners_enabled: false }
let!(:pipeline) { create :ci_empty_pipeline, project: project }
let!(:pending_build) { create :ci_build, pipeline: pipeline }
let(:shared_runner) { create(:ci_runner, :shared) }
describe '#execute' do
context 'for project with shared runners when global minutes limit is set' do
before do
project.update(shared_runners_enabled: true)
stub_application_setting(shared_runners_minutes: 500)
end
context 'allow to pick builds' do
let(:build) { execute(shared_runner) }
it { expect(build).to be_kind_of(Build) }
end
context 'when over the global quota' do
before do
project.namespace.create_namespace_statistics(
shared_runners_minutes: 600)
end
let(:build) { execute(shared_runner) }
it "does not return a build" do
expect(build).to be_nil
end
context 'when project is public' do
before do
project.update(visibility_level: Project::PUBLIC)
end
it "does return the build" do
expect(build).to be_kind_of(Build)
end
end
context 'when namespace limit is set to unlimited' do
before do
project.namespace.update(shared_runners_minutes_limit: 0)
end
it "does return the build" do
expect(build).to be_kind_of(Build)
end
end
context 'when namespace quota is bigger than a global one' do
before do
project.namespace.update(shared_runners_minutes_limit: 1000)
end
it "does return the build" do
expect(build).to be_kind_of(Build)
end
end
end
end
def execute(runner)
described_class.new(runner).execute
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