Commit acaa36cb authored by Kamil Trzcinski's avatar Kamil Trzcinski

Added a first bunch of tests

parent 9ca8ea6c
......@@ -174,8 +174,7 @@ class Namespace < ActiveRecord::Base
end
def shared_runners_enabled?
projects.where(shared_runners_enabled: true).any? ||
current_application_settings.shared_runners_enabled?
projects.where(shared_runners_enabled: true).any?
end
def shared_runners_minutes_limit
......
class UpdateBuildMinutesService < BaseService
class UpdateBuildMinutesService
def execute(build)
return unless build.runner
return unless build.runner.shared?
return unless build.runner.try(:shared?)
return unless build.project.try(:shared_runners_minutes_limit_enabled?)
return unless build.finished?
return unless build.duration
return unless build.project
return unless build.project.shared_runners_minutes_limit_enabled?
project.find_or_create_project_metrics.
update_all('shared_runners_minutes = shared_runners_minutes + ?', build.duration)
project.namespace.find_or_create_namespace_metrics.
update_all('shared_runners_minutes = shared_runners_minutes + ?', build.duration)
end
......
FactoryGirl.define do
factory :namespace_metrics do
namespace factory: :namespace
end
end
......@@ -4,11 +4,16 @@ FactoryGirl.define do
path { name.downcase.gsub(/\s/, '_') }
owner
trait :with_limit do
trait :with_build_minutes_limit do
shared_runners_minutes_limit 500
end
trait :with_used_limit do
trait :with_not_used_build_minutes_limit do
namespace_metrics factory: :namespace_metrics, shared_runners_minutes: 300
shared_runners_minutes_limit 500
end
trait :with_used_build_minutes_limit do
namespace_metrics factory: :namespace_metrics, shared_runners_minutes: 1000
shared_runners_minutes_limit 500
end
......
FactoryGirl.define do
factory :project_metrics do
project factory: :empty_project
end
end
require 'spec_helper'
describe NamespaceMetrics, models: true do
it { is_expected.to belong_to(:namespace) }
it { is_expected.to validate_presence_of(:namespace) }
end
......@@ -4,6 +4,7 @@ describe Namespace, models: true do
let!(:namespace) { create(:namespace) }
it { is_expected.to have_many :projects }
it { is_expected.to have_one(:namespace_metrics).dependent(:destroy) }
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_uniqueness_of(:name) }
......@@ -17,6 +18,9 @@ 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) }
describe "Mass assignment" do
end
......@@ -144,4 +148,112 @@ describe Namespace, models: true do
it { expect(group.full_path).to eq(group.path) }
it { expect(nested_group.full_path).to eq("#{group.path}/#{nested_group.path}") }
end
describe '#shared_runners_enabled?' do
subject { namespace.shared_runners_enabled? }
context 'without projects' do
it { is_expected.to be_falsey }
end
context 'with project' do
context 'and disabled shared runners' do
let!(:project) { create(:empty_project, namespace: namespace) }
it { is_expected.to be_falsey }
end
context 'and enabled shared runners' do
let!(:project) do
create(:empty_project,
namespace: namespace,
shared_runners_enabled: true)
end
it { is_expected.to be_truthy }
end
end
end
describe '#shared_runners_minutes_limit' do
subject { namespace.shared_runners_minutes_limit }
context 'when no limit defined' do
it { is_expected.to be_nil }
end
context 'when application settings limit is set' do
before do
stub_application_setting(shared_runners_minutes: 1000)
end
it 'returns global limit' do
is_expected.to eq(1000)
end
context 'when namespace limit is set' do
before do
namespace.shared_runners_minutes_limit = 500
end
it 'returns namespace limit' do
is_expected.to eq(500)
end
end
end
end
describe '#shared_runners_minutes_limit_enabled?' do
subject { namespace.shared_runners_minutes_limit_enabled? }
context 'when no limit defined' do
it { is_expected.to be_falsey }
end
context 'when limit is defined' do
before do
namespace.shared_runners_minutes_limit = 500
end
it { is_expected.to be_truthy }
end
end
describe '#shared_runners_minutes_used?' do
subject { namespace.shared_runners_minutes_used? }
context 'with project' do
let!(:project) do
create(:empty_project,
namespace: namespace,
shared_runners_enabled: true)
end
context 'when limit is defined' do
context 'when limit is used' do
let(:namespace) { create(:namespace, :with_used_build_minutes_limit) }
it { is_expected.to be_truthy }
end
context 'when limit not yet used' do
let(:namespace) { create(:namespace, :with_not_used_build_minutes_limit) }
it { is_expected.to be_falsey }
end
context 'when minutes are not yet set' do
it { is_expected.to be_falsey }
end
end
context 'without limit' do
it { is_expected.to be_falsey }
end
end
context 'without project' do
it { is_expected.to be_falsey }
end
end
end
require 'spec_helper'
describe ProjectMetrics, models: true do
it { is_expected.to belong_to(:project) }
it { is_expected.to validate_presence_of(:project) }
end
......@@ -5,6 +5,7 @@ describe Project, models: true do
it { is_expected.to belong_to(:group) }
it { is_expected.to belong_to(:namespace) }
it { is_expected.to belong_to(:creator).class_name('User') }
it { is_expected.to have_one(:project_metrics).dependent(:destroy) }
it { is_expected.to have_many(:users) }
it { is_expected.to have_many(:services) }
it { is_expected.to have_many(:events).dependent(:destroy) }
......@@ -72,6 +73,13 @@ 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_limit).to(:namespace) }
it { is_expected.to delegate_method(:shared_runners_minutes_limit_enabled?).to(:namespace) }
it { is_expected.to delegate_method(:shared_runners_minutes_used?).to(:namespace) }
context 'after initialized' do
it "has a project_feature" do
project = FactoryGirl.build(:project)
......@@ -1087,15 +1095,30 @@ describe Project, models: true do
context 'for shared runners enabled' do
let(:shared_runners_enabled) { true }
it 'has a shared runner' do
before do
shared_runner
end
it 'has a shared runner' do
expect(project.any_runners?).to be_truthy
end
it 'checks the presence of shared runner' do
shared_runner
expect(project.any_runners? { |runner| runner == shared_runner }).to be_truthy
end
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: shared_runners_enabled)
end
it 'does not have a shared runner' do
expect(project.any_runners?).to be_falsey
end
end
end
end
......@@ -2048,6 +2071,42 @@ 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
def enable_lfs
allow(Gitlab.config.lfs).to receive(:enabled).and_return(true)
end
......
require 'spec_helper'
describe UpdateBuildMinutesService, services: true do
context '#perform' do
let(:namespace) { create(:namespace) }
let(:project) { create(:empty_project, namespace: namespace) }
let(:pipeline) { create(:ci_pipeline) }
let(:build) do
create(:ci_build, :success,
runner: runner, pipeline: pipeline,
started_at: 2.hour.ago, finished_at: 1.hour.ago)
end
subject { described_class.new.execute(build) }
context 'with shared runner' do
let(:runner) { create(:ci_runner, :shared) }
it "creates a metrics and sets duration" do
subject
#expect(project.reload.project_metrics.shared_runners_minutes).to
# eq(build.duration)
expect(namespace.reload.namespace_metrics.shared_runners_minutes).to
eq(build.duration)
end
context 'when metrics are created' do
before do
project.create_project_metrics(shared_runners_minutes: 100)
namespace.create_namespace_metrics(shared_runners_minutes: 100)
end
it "updates metrics and adds duration" do
subject
expect(project.project_metrics.shared_runners_minutes).to
eq(100 + build.duration)
expect(namespace.namespace_metrics.shared_runners_minutes).to
eq(100 + build.duration)
end
end
end
context 'for specific runner' do
let(:runner) { create(:ci_runner) }
it "does not create metrics" do
subject
expect(project.project_metrics).to be_nil
expect(namespace.namespace_metrics).to be_nil
end
end
end
end
require 'spec_helper'
describe ClearSharedRunnerMinutesWorker do
let(:worker) { described_class.new }
describe '#perform' do
subject { worker.perform }
context 'when project metrics are defined' do
let!(:project_metrics) { create(:project_metrics, shared_runners_minutes: 100) }
it 'clears counters' do
subject
expect(project_metrics.reload.shared_runners_minutes).to be_zero
end
it 'resets timer' do
subject
expect(project_metrics.reload.shared_runners_minutes_last_reset).to be_like_time(Time.now)
end
end
context 'when project metrics are defined' do
let!(:namespace_metrics) { create(:namespace_metrics, shared_runners_minutes: 100) }
it 'clears counters' do
subject
expect(namespace_metrics.reload.shared_runners_minutes).to be_zero
end
it 'resets timer' do
subject
expect(namespace_metrics.reload.shared_runners_minutes_last_reset).to be_like_time(Time.now)
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