Commit c1d5704f authored by Kamil Trzcinski's avatar Kamil Trzcinski

Fix specs failures

parent 5071a971
......@@ -4,7 +4,7 @@ module EE
# This module is intended to encapsulate EE-specific model logic
# and be included in the `ApplicationSetting` model
module ApplicationSetting
extend ActiveSupport::Prependable
extend ::Prependable
prepended do
validates :shared_runners_minutes,
......
......@@ -4,7 +4,7 @@ module EE
# This module is intended to encapsulate EE-specific model logic
# and be included in the `Namespace` model
module Namespace
extend ActiveSupport::Prependable
extend ::Prependable
prepended do
has_one :namespace_statistics, dependent: :destroy
......
......@@ -4,13 +4,13 @@ module EE
# This module is intended to encapsulate EE-specific model logic
# and be included in the `Project` model
module Project
extend ActiveSupport::Prependable
extend ::Prependable
prepended do
scope :with_shared_runners_limit_enabled, -> { with_shared_runners.non_public_only }
delegate :shared_runners_minutes, :shared_runners_minutes_last_reset,
to: :project_statistics, allow_nil: true
to: :statistics, allow_nil: true
delegate :actual_shared_runners_minutes_limit,
:shared_runners_minutes_used?, to: :namespace
......
......@@ -5,7 +5,7 @@ module EE
# This module is intended to encapsulate EE-specific service logic
# and be included in the `RegisterBuildService` service
module RegisterBuildService
extend ActiveSupport::Prependable
extend ::Prependable
def builds_for_shared_runner
return super unless shared_runner_build_limits_feature_enabled?
......
# This module is based on: https://gist.github.com/bcardarella/5735987
module ActiveSupport
module Prependable
include ActiveSupport::Concern
def self.extended(base) #:nodoc:
base.instance_variable_set(:@_dependencies, [])
end
def prepend_features(base)
if base.instance_variable_defined?(:@_dependencies)
base.instance_variable_get(:@_dependencies) << self
return false
else
return false if base < self
super
base.singleton_class.send(:prepend, const_get('ClassMethods')) if const_defined?(:ClassMethods)
@_dependencies.each { |dep| base.send(:include, dep) }
base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block)
end
end
alias_method :prepended, :included
end
end
# This module is based on: https://gist.github.com/bcardarella/5735987
module Prependable
include ActiveSupport::Concern
def self.extended(base) #:nodoc:
base.instance_variable_set(:@_dependencies, [])
end
def prepend_features(base)
if base.instance_variable_defined?(:@_dependencies)
base.instance_variable_get(:@_dependencies) << self
return false
else
return false if base < self
super
base.singleton_class.send(:prepend, const_get('ClassMethods')) if const_defined?(:ClassMethods)
@_dependencies.each { |dep| base.send(:include, dep) }
base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block)
end
end
alias_method :prepended, :included
end
......@@ -12,8 +12,8 @@ describe Ci::Build, models: true do
let(:build) { create(:ci_build, pipeline: pipeline) }
describe '#shared_runners_minutes_quota?' do
subject { build.shared_runners_minutes_quota? }
describe '#shared_runners_minutes_limit_enabled?' do
subject { build.shared_runners_minutes_limit_enabled? }
context 'for shared runner' do
before do
......@@ -21,7 +21,7 @@ describe Ci::Build, models: true do
end
it do
expect(build.project).to receive(:shared_runners_minutes_quota?).
expect(build.project).to receive(:shared_runners_minutes_limit_enabled?).
and_return(true)
is_expected.to be_truthy
......
......@@ -2,8 +2,8 @@ require 'spec_helper'
describe Project, models: true do
describe 'associations' do
it { is_expected.to delegate_method(:shared_runners_minutes).to(:project_statistics) }
it { is_expected.to delegate_method(:shared_runners_minutes_last_reset).to(:project_statistics) }
it { is_expected.to delegate_method(:shared_runners_minutes).to(:statistics) }
it { is_expected.to delegate_method(:shared_runners_minutes_last_reset).to(:statistics) }
it { is_expected.to delegate_method(:actual_shared_runners_minutes_limit).to(:namespace) }
it { is_expected.to delegate_method(:shared_runners_minutes_limit_enabled?).to(:namespace) }
......@@ -45,10 +45,36 @@ describe Project, models: true do
end
end
describe '#shared_runners_minutes_quota?' do
describe '#shared_runners_available?' do
subject { project.shared_runners_available? }
context 'with used build minutes' do
let(:namespace) { create(:namespace, :with_used_build_minutes_limit) }
let(:project) do
create(:empty_project,
namespace: namespace,
shared_runners_enabled: true)
end
before do
expect(namespace).to receive(:shared_runners_minutes_used?).and_call_original
end
it 'shared runners are not available' do
expect(project.shared_runners_available?).to be_falsey
end
end
end
describe '#shared_runners_minutes_limit_enabled?' do
let(:project) { create(:empty_project) }
subject { project.shared_runners_minutes_quota? }
subject { project.shared_runners_minutes_limit_enabled? }
before do
allow(project.namespace).to receive(:shared_runners_minutes_limit_enabled?).
and_return(true)
end
context 'with shared runners enabled' do
before do
......
......@@ -136,13 +136,13 @@ module Ci
end
context 'shared runner' do
let(:build) { service.execute(shared_runner) }
let(:build) { execute(shared_runner) }
it { expect(build).to be_nil }
end
context 'specific runner' do
let(:build) { service.execute(specific_runner) }
let(:build) { execute(specific_runner) }
it { expect(build).to be_kind_of(Build) }
it { expect(build).to be_valid }
......
......@@ -19,7 +19,7 @@ describe UpdateBuildMinutesService, services: true do
it "creates a metrics and sets duration" do
subject
expect(project.project_statistics.reload.shared_runners_minutes).
expect(project.statistics.reload.shared_runners_minutes).
to eq(build.duration.to_i)
expect(namespace.namespace_statistics.reload.shared_runners_minutes).
......@@ -28,14 +28,14 @@ describe UpdateBuildMinutesService, services: true do
context 'when metrics are created' do
before do
project.create_project_statistics(shared_runners_minutes: 100)
namespace.create_namespace_metrics(shared_runners_minutes: 100)
project.create_statistics(shared_runners_minutes: 100)
namespace.create_namespace_statistics(shared_runners_minutes: 100)
end
it "updates metrics and adds duration" do
subject
expect(project.project_statistics.reload.shared_runners_minutes).
expect(project.statistics.reload.shared_runners_minutes).
to eq(100 + build.duration.to_i)
expect(namespace.namespace_statistics.reload.shared_runners_minutes).
......@@ -50,7 +50,7 @@ describe UpdateBuildMinutesService, services: true do
it "does not create metrics" do
subject
expect(project.project_statistics).to be_nil
expect(project.statistics).to be_nil
expect(namespace.namespace_statistics).to be_nil
end
end
......
......@@ -11,35 +11,40 @@ describe ClearSharedRunnersMinutesWorker do
subject { worker.perform }
context 'when project metrics are defined' do
let!(:project_statistics) { create(:project_statistics, shared_runners_minutes: 100) }
context 'when project statistics are defined' do
let(:project) { create(:empty_project) }
let(:statistics) { project.statistics }
before do
statistics.update(shared_runners_minutes: 100)
end
it 'clears counters' do
subject
expect(project_statistics.reload.shared_runners_minutes).to be_zero
expect(statistics.reload.shared_runners_minutes).to be_zero
end
it 'resets timer' do
subject
expect(project_statistics.reload.shared_runners_minutes_last_reset).to be_like_time(Time.now)
expect(statistics.reload.shared_runners_minutes_last_reset).to be_like_time(Time.now)
end
end
context 'when project metrics are defined' do
let!(:namespace_statistics) { create(:namespace_statistics, shared_runners_minutes: 100) }
context 'when namespace statistics are defined' do
let!(:statistics) { create(:namespace_statistics, shared_runners_minutes: 100) }
it 'clears counters' do
subject
expect(namespace_statistics.reload.shared_runners_minutes).to be_zero
expect(statistics.reload.shared_runners_minutes).to be_zero
end
it 'resets timer' do
subject
expect(namespace_statistics.reload.shared_runners_minutes_last_reset).to be_like_time(Time.now)
expect(statistics.reload.shared_runners_minutes_last_reset).to be_like_time(Time.now)
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