Commit 1dbf1e6e authored by Kamil Trzcinski's avatar Kamil Trzcinski

Update tests

parent 4f841b12
......@@ -77,7 +77,7 @@ module GroupsHelper
def group_shared_runner_limits_percent_used(group)
return 0 unless group.shared_runners_minutes_limit_enabled?
100 * group.shared_runners_minutes / group.actual_shared_runners_minutes_limit
100 * group.shared_runners_minutes.to_i / group.actual_shared_runners_minutes_limit
end
def group_shared_runner_limits_progress_bar(group)
......
......@@ -103,7 +103,7 @@ module Ci
after_transition any => [:success, :failed, :canceled] do |build|
build.run_after_commit do
UpdateBuildMinutesService.new(project, nil).execute(build)
UpdateBuildMinutesService.new(project, nil).execute(self)
end
end
......@@ -537,6 +537,10 @@ module Ci
Gitlab::Ci::Build::Credentials::Factory.new(self).create!
end
def shared_runners_minutes_quota?
runner && runner.shared? && !project.public?
end
private
def update_artifacts_size
......
......@@ -1567,10 +1567,6 @@ class Project < ActiveRecord::Base
end
end
def shared_runners_minutes_limit_enabled?
!public? && namespace.shared_runners_minutes_limit_enabled?
end
private
# Check if a reference is being done cross-project
......
class UpdateBuildMinutesService < BaseService
def execute(build)
return unless build.runner.try(:shared?)
return unless build.project.try(:shared_runners_minutes_limit_enabled?)
return unless build.finished?
return unless build.shared_runners_minutes_quota?
return unless build.complete?
return unless build.duration
project.find_or_create_project_metrics.
update_all('shared_runners_minutes = shared_runners_minutes + ?', build.duration)
ProjectMetrics.update_counters(project_metrics,
shared_runners_minutes: build.duration)
project.namespace.find_or_create_namespace_metrics.
update_all('shared_runners_minutes = shared_runners_minutes + ?', build.duration)
NamespaceMetrics.update_counters(namespace_metrics,
shared_runners_minutes: build.duration)
end
private
def namespace_metrics
namespace.namespace_metrics || namespace.create_namespace_metrics
end
def project_metrics
project.project_metrics || project.create_project_metrics
end
def namespace
project.namespace
end
end
......@@ -46,7 +46,7 @@
- if @group.shared_runners_enabled?
%li
%span.light Shared CI Runner Quota:
%span.light Build minutes quota:
%strong
= group_shared_runner_limits_quota(@group)
= link_to icon('question-circle'), help_page_path("user/admin_area/settings/continuous_integration", anchor: "shared-runners-minutes")
......
......@@ -4,7 +4,7 @@
- if current_user && can?(current_user, :download_code, @project)
= render 'shared/no_ssh'
= render 'shared/no_password'
= render 'shared/shared_runner_minutes_limit', project: @project
= render 'shared/shared_runners_minutes_limit', project: @project
= render "home_panel"
......
......@@ -2,7 +2,7 @@
- page_title "Pipelines"
= content_for :flash_message do
= render 'shared/shared_runner_minutes_limit', project: @project
= render 'shared/shared_runners_minutes_limit', project: @project
= render "projects/pipelines/head"
......
......@@ -8,7 +8,7 @@
- if current_user && can?(current_user, :download_code, @project)
= render 'shared/no_ssh'
= render 'shared/no_password'
= render 'shared/shared_runner_minutes_limit', project: @project
= render 'shared/shared_runners_minutes_limit', project: @project
- if @project.above_size_limit?
= render 'above_size_limit_warning'
......
......@@ -12,6 +12,10 @@ FactoryGirl.define do
is_shared true
end
trait :specific do
is_shared false
end
trait :inactive do
active false
end
......
......@@ -1321,4 +1321,63 @@ describe Ci::Build, models: true do
.to be_a Gitlab::Ci::Status::Build::Cancelable
end
end
describe '#shared_runners_minutes_quota?' do
subject { build.shared_runners_minutes_quota? }
context 'for shared runner' do
before do
build.runner = create(:ci_runner, :shared)
end
context 'for public project' do
before do
build.project.visibility_level = Gitlab::VisibilityLevel::PUBLIC
end
it { is_expected.to be_falsey }
end
context 'for internal project' do
before do
build.project.visibility_level = Gitlab::VisibilityLevel::INTERNAL
end
it { is_expected.to be_truthy }
end
context 'for private project' do
before do
build.project.visibility_level = Gitlab::VisibilityLevel::INTERNAL
end
it { is_expected.to be_truthy }
end
end
context 'with specific runner' do
before do
build.runner = create(:ci_runner, :specific)
end
it { is_expected.to be_falsey }
end
context 'without runner' do
it { is_expected.to be_falsey }
end
end
context 'updates build minutes' do
let(:build) { create(:ci_build, :running, pipeline: pipeline) }
%w(success drop cancel).each do |event|
it "for event #{event}" do
expect(UpdateBuildMinutesService).
to receive(:new).and_call_original
build.public_send(event)
end
end
end
end
......@@ -17,8 +17,8 @@ describe Namespace, models: true do
it { is_expected.to validate_presence_of(:owner) }
it { is_expected.to delegate_method(:shared_runner_minutes).to(:namespace_metrics) }
it { is_expected.to delegate_method(:shared_runner_last_reset).to(:namespace_metrics) }
it { is_expected.to delegate_method(:shared_runners_minutes).to(:namespace_metrics) }
it { is_expected.to delegate_method(:shared_runners_minutes_last_reset).to(:namespace_metrics) }
describe "Respond to" do
it { is_expected.to respond_to(:human_name) }
......@@ -154,7 +154,11 @@ describe Namespace, models: true do
context 'with project' do
context 'and disabled shared runners' do
let!(:project) { create(:empty_project, namespace: namespace) }
let!(:project) do
create(:empty_project,
namespace: namespace,
shared_runners_enabled: false)
end
it { is_expected.to be_falsey }
end
......@@ -175,7 +179,7 @@ describe Namespace, models: true do
subject { namespace.actual_shared_runners_minutes_limit }
context 'when no limit defined' do
it { is_expected.to be_nil }
it { is_expected.to be_zero }
end
context 'when application settings limit is set' do
......
......@@ -74,8 +74,8 @@ describe Project, models: true do
it { is_expected.to have_many(:forks).through(:forked_project_links) }
it { is_expected.to have_many(:approver_groups).dependent(:destroy) }
it { is_expected.to delegate_method(:shared_runner_minutes).to(:project_metrics) }
it { is_expected.to delegate_method(:shared_runner_last_reset).to(:project_metrics) }
it { is_expected.to delegate_method(:shared_runners_minutes).to(:project_metrics) }
it { is_expected.to delegate_method(:shared_runners_last_reset).to(:project_metrics) }
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) }
......@@ -2072,42 +2072,6 @@ describe Project, models: true do
end
end
describe '#shared_runners_minutes_limit_enabled?' do
subject { project.shared_runners_minutes_limit_enabled? }
context 'with limit enabled' do
let(:namespace) { create(:namespace, :with_build_minutes_limit) }
context 'for public project' do
let(:project) { create(:empty_project, :public, namespace: namespace) }
it { is_expected.to be_truthy }
end
context 'for internal project' do
let(:project) { create(:empty_project, :internal, namespace: namespace) }
it { is_expected.to be_falsey }
end
context 'for private project' do
let(:project) { create(:empty_project, :private, namespace: namespace) }
it { is_expected.to be_falsey }
end
end
context 'with limit not set' do
let(:namespace) { create(:namespace) }
context 'for public project' do
let(:project) { create(:empty_project, :public, namespace: namespace) }
it { is_expected.to be_falsey }
end
end
end
describe '#deployment_variables' do
context 'when project has no deployment service' do
let(:project) { create(:empty_project) }
......
......@@ -11,7 +11,7 @@ describe UpdateBuildMinutesService, services: true do
started_at: 2.hours.ago, finished_at: 1.hour.ago)
end
subject { described_class.new.execute(build) }
subject { described_class.new(project, nil).execute(build) }
context 'with shared runner' do
let(:runner) { create(:ci_runner, :shared) }
......@@ -19,13 +19,11 @@ describe UpdateBuildMinutesService, services: true do
it "creates a metrics and sets duration" do
subject
# expect(project.reload.project_metrics.shared_runners_minutes).to(
# eq(build.duration)
# )
expect(project.project_metrics.reload.shared_runners_minutes).
to eq(build.duration.to_i)
expect(namespace.reload.namespace_metrics.shared_runners_minutes).to(
eq(build.duration)
)
expect(namespace.namespace_metrics.reload.shared_runners_minutes).
to eq(build.duration.to_i)
end
context 'when metrics are created' do
......@@ -37,13 +35,11 @@ describe UpdateBuildMinutesService, services: true do
it "updates metrics and adds duration" do
subject
expect(project.project_metrics.shared_runners_minutes).to(
eq(100 + build.duration)
)
expect(project.project_metrics.reload.shared_runners_minutes).
to eq(100 + build.duration.to_i)
expect(namespace.namespace_metrics.shared_runners_minutes).to(
eq(100 + build.duration)
)
expect(namespace.namespace_metrics.reload.shared_runners_minutes).
to eq(100 + build.duration.to_i)
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