Commit 72981f3d authored by Jarka Košanová's avatar Jarka Košanová

Merge branch 'refactor-shared-runners-seconds-3' into 'master'

Remove direct setup of `shared_runners_seconds` in specs

See merge request gitlab-org/gitlab!71676
parents ceb9438a 71e8307d
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe 'Reset namespace pipeline minutes', :js do RSpec.describe 'Reset namespace pipeline minutes', :js do
include ::Ci::MinutesHelpers
let(:admin) { create(:admin) } let(:admin) { create(:admin) }
before do before do
...@@ -13,7 +15,7 @@ RSpec.describe 'Reset namespace pipeline minutes', :js do ...@@ -13,7 +15,7 @@ RSpec.describe 'Reset namespace pipeline minutes', :js do
shared_examples 'resetting pipeline minutes' do shared_examples 'resetting pipeline minutes' do
context 'when namespace has namespace statistics' do context 'when namespace has namespace statistics' do
before do before do
namespace.create_namespace_statistics(shared_runners_seconds: 100) set_ci_minutes_used(namespace, 100)
end end
it 'resets pipeline minutes' do it 'resets pipeline minutes' do
...@@ -26,7 +28,7 @@ RSpec.describe 'Reset namespace pipeline minutes', :js do ...@@ -26,7 +28,7 @@ RSpec.describe 'Reset namespace pipeline minutes', :js do
expect(page).to have_selector('.gl-toast') expect(page).to have_selector('.gl-toast')
expect(current_path).to include(namespace.path) expect(current_path).to include(namespace.path)
expect(namespace.namespace_statistics.reload.shared_runners_seconds).to eq(0) expect(namespace.reload.ci_minutes_quota.total_minutes_used).to eq(0)
expect(namespace.namespace_statistics.reload.shared_runners_seconds_last_reset).to be_like_time(time) expect(namespace.namespace_statistics.reload.shared_runners_seconds_last_reset).to be_like_time(time)
end end
end end
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe 'Profile > Usage Quota' do RSpec.describe 'Profile > Usage Quota' do
include ::Ci::MinutesHelpers
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
let_it_be(:user, reload: true) { create(:user) } let_it_be(:user, reload: true) { create(:user) }
...@@ -36,7 +38,7 @@ RSpec.describe 'Profile > Usage Quota' do ...@@ -36,7 +38,7 @@ RSpec.describe 'Profile > Usage Quota' do
before do before do
project.update!(shared_runners_enabled: shared_runners_enabled) project.update!(shared_runners_enabled: shared_runners_enabled)
statistics.update!(shared_runners_seconds: used.minutes.to_i) set_ci_minutes_used(namespace, used)
namespace.update!(shared_runners_minutes_limit: quota) namespace.update!(shared_runners_minutes_limit: quota)
visit profile_usage_quotas_path visit profile_usage_quotas_path
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Ci::Minutes::Notification do RSpec.describe Ci::Minutes::Notification do
include ::Ci::MinutesHelpers
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:shared_runners_enabled) { true } let(:shared_runners_enabled) { true }
let!(:project) { create(:project, :repository, namespace: group, shared_runners_enabled: shared_runners_enabled) } let!(:project) { create(:project, :repository, namespace: group, shared_runners_enabled: shared_runners_enabled) }
...@@ -162,7 +164,7 @@ RSpec.describe Ci::Minutes::Notification do ...@@ -162,7 +164,7 @@ RSpec.describe Ci::Minutes::Notification do
shared_examples 'not eligible to see notifications' do shared_examples 'not eligible to see notifications' do
before do before do
group.shared_runners_minutes_limit = 10 group.shared_runners_minutes_limit = 10
allow(group).to receive(:shared_runners_seconds).and_return(8.minutes) set_ci_minutes_used(group, 8)
end end
context 'when not permitted to see notifications' do context 'when not permitted to see notifications' do
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Ci::Minutes::QuotaPresenter do RSpec.describe Ci::Minutes::QuotaPresenter do
include ::Ci::MinutesHelpers
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
let_it_be_with_reload(:namespace) do let_it_be_with_reload(:namespace) do
...@@ -48,7 +50,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do ...@@ -48,7 +50,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do
context 'when minutes are used' do context 'when minutes are used' do
before do before do
namespace.namespace_statistics.shared_runners_seconds = 20.minutes set_ci_minutes_used(namespace, 20)
end end
it 'returns unlimited report with usage' do it 'returns unlimited report with usage' do
...@@ -71,7 +73,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do ...@@ -71,7 +73,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do
context 'when minutes are not all used' do context 'when minutes are not all used' do
before do before do
namespace.namespace_statistics.shared_runners_seconds = 30.minutes set_ci_minutes_used(namespace, 30)
end end
it 'returns report with under quota' do it 'returns report with under quota' do
...@@ -85,7 +87,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do ...@@ -85,7 +87,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do
context 'when minutes are all used' do context 'when minutes are all used' do
before do before do
namespace.namespace_statistics.shared_runners_seconds = 101.minutes set_ci_minutes_used(namespace, 101)
end end
it 'returns report with over quota' do it 'returns report with over quota' do
...@@ -113,7 +115,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do ...@@ -113,7 +115,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do
context 'when all monthly minutes are used and some puarchased minutes are used' do context 'when all monthly minutes are used and some puarchased minutes are used' do
before do before do
namespace.namespace_statistics.shared_runners_seconds = 250.minutes set_ci_minutes_used(namespace, 250)
end end
it 'returns report with under quota' do it 'returns report with under quota' do
...@@ -127,7 +129,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do ...@@ -127,7 +129,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do
context 'when all monthly and all puarchased minutes have been used' do context 'when all monthly and all puarchased minutes have been used' do
before do before do
namespace.namespace_statistics.shared_runners_seconds = 301.minutes set_ci_minutes_used(namespace, 301)
end end
it 'returns report with over quota' do it 'returns report with over quota' do
...@@ -141,7 +143,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do ...@@ -141,7 +143,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do
context 'when not all monthly minutes have been used' do context 'when not all monthly minutes have been used' do
before do before do
namespace.namespace_statistics.shared_runners_seconds = 190.minutes set_ci_minutes_used(namespace, 190)
end end
it 'returns report with no usage' do it 'returns report with no usage' do
...@@ -161,7 +163,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do ...@@ -161,7 +163,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do
context 'when all monthly minutes have been used' do context 'when all monthly minutes have been used' do
before do before do
namespace.namespace_statistics.shared_runners_seconds = 201.minutes set_ci_minutes_used(namespace, 201)
end end
it 'returns report without usage' do it 'returns report without usage' do
...@@ -175,7 +177,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do ...@@ -175,7 +177,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do
context 'when not all monthly minutes have been used' do context 'when not all monthly minutes have been used' do
before do before do
namespace.namespace_statistics.shared_runners_seconds = 190.minutes set_ci_minutes_used(namespace, 190)
end end
it 'returns report with no usage' do it 'returns report with no usage' do
...@@ -212,7 +214,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do ...@@ -212,7 +214,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do
allow(namespace).to receive(:any_project_with_shared_runners_enabled?).and_return(true) allow(namespace).to receive(:any_project_with_shared_runners_enabled?).and_return(true)
namespace.shared_runners_minutes_limit = monthly_limit namespace.shared_runners_minutes_limit = monthly_limit
namespace.extra_shared_runners_minutes_limit = purchased_limit namespace.extra_shared_runners_minutes_limit = purchased_limit
namespace.namespace_statistics.shared_runners_seconds = minutes_used.minutes set_ci_minutes_used(namespace, minutes_used)
end end
it 'returns the percentage' do it 'returns the percentage' do
...@@ -242,7 +244,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do ...@@ -242,7 +244,7 @@ RSpec.describe Ci::Minutes::QuotaPresenter do
allow(quota).to receive(:enabled?).and_return(limit_enabled) allow(quota).to receive(:enabled?).and_return(limit_enabled)
namespace.shared_runners_minutes_limit = monthly_limit namespace.shared_runners_minutes_limit = monthly_limit
namespace.extra_shared_runners_minutes_limit = purchased_limit namespace.extra_shared_runners_minutes_limit = purchased_limit
namespace.namespace_statistics.shared_runners_seconds = minutes_used.minutes set_ci_minutes_used(namespace, minutes_used)
end end
it 'returns the percentage' do it 'returns the percentage' do
......
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do
let_it_be(:group) { create(:group, shared_runners_minutes_limit: 100) } let_it_be(:minutes_used) { 95 + Ci::Minutes::TrackLiveConsumptionService::CONSUMPTION_THRESHOLD.abs }
let_it_be(:group) { create(:group, :with_ci_minutes, ci_minutes_limit: 100, ci_minutes_used: minutes_used) }
let_it_be(:project) { create(:project, :private, namespace: group, shared_runners_enabled: true) } let_it_be(:project) { create(:project, :private, namespace: group, shared_runners_enabled: true) }
let_it_be(:pipeline) { create(:ci_pipeline, project: project, ref: 'master') } let_it_be(:pipeline) { create(:ci_pipeline, project: project, ref: 'master') }
let_it_be(:runner) { create(:ci_runner, :instance) } let_it_be(:runner) { create(:ci_runner, :instance) }
...@@ -24,16 +25,6 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do ...@@ -24,16 +25,6 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do
pipeline: pipeline) pipeline: pipeline)
end end
let(:minutes_already_consumed) do
95 + Ci::Minutes::TrackLiveConsumptionService::CONSUMPTION_THRESHOLD.abs
end
let!(:statistics) do
create(:namespace_statistics,
namespace: group,
shared_runners_seconds: minutes_already_consumed.minutes)
end
it 'tracks CI minutes usage of running job' do it 'tracks CI minutes usage of running job' do
expect(Ci::Minutes::TrackLiveConsumptionService).to receive(:new).with(job).and_call_original expect(Ci::Minutes::TrackLiveConsumptionService).to receive(:new).with(job).and_call_original
......
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do
let_it_be(:group) { create(:group, shared_runners_minutes_limit: 100) } let_it_be(:minutes_used) { 95 + Ci::Minutes::TrackLiveConsumptionService::CONSUMPTION_THRESHOLD.abs }
let_it_be(:group) { create(:group, :with_ci_minutes, ci_minutes_limit: 100, ci_minutes_used: minutes_used) }
let_it_be(:project) { create(:project, :private, namespace: group, shared_runners_enabled: true) } let_it_be(:project) { create(:project, :private, namespace: group, shared_runners_enabled: true) }
let_it_be(:pipeline) { create(:ci_pipeline, project: project, ref: 'master') } let_it_be(:pipeline) { create(:ci_pipeline, project: project, ref: 'master') }
let_it_be(:runner) { create(:ci_runner, :instance) } let_it_be(:runner) { create(:ci_runner, :instance) }
...@@ -24,16 +25,6 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do ...@@ -24,16 +25,6 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do
pipeline: pipeline) pipeline: pipeline)
end end
let(:minutes_already_consumed) do
95 + Ci::Minutes::TrackLiveConsumptionService::CONSUMPTION_THRESHOLD.abs
end
let!(:statistics) do
create(:namespace_statistics,
namespace: group,
shared_runners_seconds: minutes_already_consumed.minutes)
end
it 'tracks CI minutes usage of running job' do it 'tracks CI minutes usage of running job' do
expect(Ci::Minutes::TrackLiveConsumptionService).to receive(:new).with(job).and_call_original expect(Ci::Minutes::TrackLiveConsumptionService).to receive(:new).with(job).and_call_original
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Ci::Minutes::BatchResetService do RSpec.describe Ci::Minutes::BatchResetService do
include ::Ci::MinutesHelpers
let(:service) { described_class.new } let(:service) { described_class.new }
describe '#execute!' do describe '#execute!' do
...@@ -18,7 +20,7 @@ RSpec.describe Ci::Minutes::BatchResetService do ...@@ -18,7 +20,7 @@ RSpec.describe Ci::Minutes::BatchResetService do
last_ci_minutes_notification_at: Time.current, last_ci_minutes_notification_at: Time.current,
last_ci_minutes_usage_notification_level: 30) last_ci_minutes_usage_notification_level: 30)
create(:namespace_statistics, namespace: namespace, shared_runners_seconds: seconds_used) set_ci_minutes_used(namespace, seconds_used.to_f / 60)
create(:project, namespace: namespace).tap do |project| create(:project, namespace: namespace).tap do |project|
create(:project_statistics, create(:project_statistics,
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Ci::Minutes::EmailNotificationService do RSpec.describe Ci::Minutes::EmailNotificationService do
include ::Ci::MinutesHelpers
describe '#execute' do describe '#execute' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
...@@ -88,11 +90,9 @@ RSpec.describe Ci::Minutes::EmailNotificationService do ...@@ -88,11 +90,9 @@ RSpec.describe Ci::Minutes::EmailNotificationService do
Ci::Minutes::NamespaceMonthlyUsage.find_or_create_current(namespace_id: namespace.id) Ci::Minutes::NamespaceMonthlyUsage.find_or_create_current(namespace_id: namespace.id)
end end
let!(:namespace_statistics) do
create(:namespace_statistics, namespace: namespace, shared_runners_seconds: minutes_used * 60)
end
before do before do
set_ci_minutes_used(namespace, minutes_used)
namespace_usage.update_column(:notification_level, current_notification_level) namespace_usage.update_column(:notification_level, current_notification_level)
namespace.update_column(:shared_runners_minutes_limit, monthly_minutes_limit) namespace.update_column(:shared_runners_minutes_limit, monthly_minutes_limit)
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Ci::Minutes::UpdateBuildMinutesService do RSpec.describe Ci::Minutes::UpdateBuildMinutesService do
include ::Ci::MinutesHelpers
let(:namespace) { create(:namespace, shared_runners_minutes_limit: 100) } let(:namespace) { create(:namespace, shared_runners_minutes_limit: 100) }
let(:project) { create(:project, :private, namespace: namespace) } let(:project) { create(:project, :private, namespace: namespace) }
let(:pipeline) { create(:ci_pipeline, project: project) } let(:pipeline) { create(:ci_pipeline, project: project) }
...@@ -120,9 +122,9 @@ RSpec.describe Ci::Minutes::UpdateBuildMinutesService do ...@@ -120,9 +122,9 @@ RSpec.describe Ci::Minutes::UpdateBuildMinutesService do
let(:usage_in_minutes) { (100.to_f / 60).round(2) } let(:usage_in_minutes) { (100.to_f / 60).round(2) }
before do before do
set_ci_minutes_used(namespace, usage_in_minutes)
project.statistics.update!(shared_runners_seconds: usage_in_seconds) project.statistics.update!(shared_runners_seconds: usage_in_seconds)
namespace.create_namespace_statistics(shared_runners_seconds: usage_in_seconds)
create(:ci_namespace_monthly_usage, namespace: namespace, amount_used: usage_in_minutes)
create(:ci_project_monthly_usage, project: project, amount_used: usage_in_minutes) create(:ci_project_monthly_usage, project: project, amount_used: usage_in_minutes)
end end
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Ci::Minutes::UpdateProjectAndNamespaceUsageService do RSpec.describe Ci::Minutes::UpdateProjectAndNamespaceUsageService do
include ::Ci::MinutesHelpers
let(:project) { create(:project, :private) } let(:project) { create(:project, :private) }
let(:namespace) { project.namespace } let(:namespace) { project.namespace }
let(:build) { create(:ci_build) } let(:build) { create(:ci_build) }
...@@ -135,9 +137,9 @@ RSpec.describe Ci::Minutes::UpdateProjectAndNamespaceUsageService do ...@@ -135,9 +137,9 @@ RSpec.describe Ci::Minutes::UpdateProjectAndNamespaceUsageService do
before do before do
project.statistics.update!(shared_runners_seconds: existing_usage_in_seconds) project.statistics.update!(shared_runners_seconds: existing_usage_in_seconds)
namespace.create_namespace_statistics(shared_runners_seconds: existing_usage_in_seconds)
create(:ci_namespace_monthly_usage, namespace: namespace, amount_used: existing_usage_in_minutes)
create(:ci_project_monthly_usage, project: project, amount_used: existing_usage_in_minutes) create(:ci_project_monthly_usage, project: project, amount_used: existing_usage_in_minutes)
set_ci_minutes_used(namespace, existing_usage_in_minutes)
end end
it 'does not create nested transactions', :delete do it 'does not create nested transactions', :delete do
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Ci::RegisterJobService, '#execute' do RSpec.describe Ci::RegisterJobService, '#execute' do
include ::Ci::MinutesHelpers
let_it_be_with_refind(:shared_runner) { create(:ci_runner, :instance) } let_it_be_with_refind(:shared_runner) { create(:ci_runner, :instance) }
let!(:project) { create(:project, shared_runners_enabled: true) } let!(:project) { create(:project, shared_runners_enabled: true) }
...@@ -15,8 +17,7 @@ RSpec.describe Ci::RegisterJobService, '#execute' do ...@@ -15,8 +17,7 @@ RSpec.describe Ci::RegisterJobService, '#execute' do
shared_examples 'returns a build' do |runners_minutes_used| shared_examples 'returns a build' do |runners_minutes_used|
before do before do
project.namespace.create_namespace_statistics( set_ci_minutes_used(project.namespace, runners_minutes_used)
shared_runners_seconds: runners_minutes_used * 60)
end end
context 'with traversal_ids enabled' do context 'with traversal_ids enabled' do
...@@ -67,8 +68,7 @@ RSpec.describe Ci::RegisterJobService, '#execute' do ...@@ -67,8 +68,7 @@ RSpec.describe Ci::RegisterJobService, '#execute' do
shared_examples 'does not return a build' do |runners_minutes_used| shared_examples 'does not return a build' do |runners_minutes_used|
before do before do
project.namespace.create_namespace_statistics( set_ci_minutes_used(project.namespace, runners_minutes_used)
shared_runners_seconds: runners_minutes_used * 60)
pending_build.reload pending_build.reload
pending_build.create_queuing_entry! pending_build.create_queuing_entry!
end end
...@@ -241,8 +241,7 @@ RSpec.describe Ci::RegisterJobService, '#execute' do ...@@ -241,8 +241,7 @@ RSpec.describe Ci::RegisterJobService, '#execute' do
group.update_columns(shared_runners_minutes_limit: 20) group.update_columns(shared_runners_minutes_limit: 20)
root_ancestor.update!(shared_runners_minutes_limit: 10) root_ancestor.update!(shared_runners_minutes_limit: 10)
root_ancestor.create_namespace_statistics( set_ci_minutes_used(root_ancestor, 11)
shared_runners_seconds: 60 * 11)
end end
it_behaves_like 'does not return a build', 11 it_behaves_like 'does not return a build', 11
......
# frozen_string_literal: true
module Ci
module MinutesHelpers
# TODO: Remove with https://gitlab.com/gitlab-org/gitlab/-/issues/277452
def set_ci_minutes_used(namespace, minutes)
if namespace.namespace_statistics
namespace.namespace_statistics.update!(shared_runners_seconds: minutes.minutes)
else
namespace.create_namespace_statistics(shared_runners_seconds: minutes.minutes)
end
::Ci::Minutes::NamespaceMonthlyUsage
.find_or_create_current(namespace_id: namespace.id)
.update!(amount_used: minutes)
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