Commit a6bfa4b8 authored by ayufanpl's avatar ayufanpl

Update tests

parent 29f148a2
......@@ -94,7 +94,7 @@ module EE
end
def shared_runner_minutes_supported?
if parent_id
if has_parent?
Feature.enabled?(:shared_runner_minutes_on_subnamespace)
else
true
......@@ -117,6 +117,14 @@ module EE
shared_runners_minutes.to_i >= actual_shared_runners_minutes_limit
end
def shared_runners_enabled?
if Feature.enabled?(:shared_runner_minutes_on_subnamespace)
projects.with_shared_runners.any?
else
all_projects.with_shared_runners.any?
end
end
# These helper methods are required to not break the Namespace API.
def plan=(plan_name)
if plan_name.is_a?(String)
......
......@@ -18,7 +18,7 @@ class UpdateBuildMinutesService < BaseService
end
def project_statistics
project.statistics || project.create_statistics(namespace: namespace)
project.statistics || project.create_statistics(namespace: project.namespace)
end
def namespace
......
......@@ -95,8 +95,56 @@ feature 'Groups > Pipeline Quota' do
end
page.within('.pipeline-project-metrics') do
expect(page).to have_content(project.name)
expect(page).not_to have_content(other_project.name)
expect(page).to have_content(project.full_name)
expect(page).not_to have_content(other_project.full_name)
end
end
end
context 'with shared_runner_minutes_on_subnamespace enabled' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: true)
end
context 'when accessing group with subgroups' do
let(:group) { create(:group, :with_used_build_minutes_limit) }
let!(:subgroup) { create(:group, parent: group) }
let!(:subproject) { create(:project, namespace: subgroup, shared_runners_enabled: true) }
it 'does not show project of subgroup' do
visit_pipeline_quota_page
expect(page).to have_content(project.full_name)
expect(page).not_to have_content(subproject.full_name)
end
end
end
context 'with shared_runner_minutes_on_subnamespace disabled' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: false)
end
context 'when accessing subgroup' do
let(:root_ancestor) { create(:group) }
let(:group) { create(:group, :with_used_build_minutes_limit, parent: root_ancestor) }
it 'does not show subproject' do
visit_pipeline_quota_page
expect(page).to have_http_status(:not_found)
end
end
context 'when accesing root group' do
let!(:subgroup) { create(:group, parent: group) }
let!(:subproject) { create(:project, namespace: subgroup, shared_runners_enabled: true) }
it 'does show projects of subgroup' do
visit_pipeline_quota_page
expect(page).to have_content(project.full_name)
expect(page).to have_content(subproject.full_name)
end
end
end
......
......@@ -63,6 +63,34 @@ describe Namespace do
end
end
end
describe '#validate_shared_runner_minutes_support' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: false)
end
context 'when changing :shared_runners_minutes_limit' do
before do
namespace.shared_runners_minutes_limit = 100
end
context 'when group is subgroup' do
set(:root_ancestor) { create(:group) }
let(:namespace) { create(:namespace, parent: root_ancestor) }
it 'is invalid' do
expect(namespace).not_to be_valid
expect(namespace.errors[:shared_runners_minutes_limit]).to include('is not supported for this namespace')
end
end
context 'when group is root' do
it 'is valid' do
expect(namespace).to be_valid
end
end
end
end
end
describe '#move_dir' do
......@@ -295,6 +323,42 @@ describe Namespace do
end
end
describe '#shared_runner_minutes_supported?' do
subject { namespace.shared_runner_minutes_supported? }
context 'when is subgroup' do
before do
namespace.parent = build(:group)
end
context 'when shared_runner_minutes_on_subnamespace is enabled' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: true)
end
it 'returns true' do
is_expected.to eq(true)
end
end
context 'when shared_runner_minutes_on_subnamespace is disalbed' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: false)
end
it 'returns false' do
is_expected.to eq(false)
end
end
end
context 'when is root' do
it 'returns true' do
is_expected.to eq(true)
end
end
end
describe '#shared_runners_minutes_limit_enabled?' do
subject { namespace.shared_runners_minutes_limit_enabled? }
......@@ -315,6 +379,15 @@ describe Namespace do
end
it { is_expected.to be_truthy }
context 'when is subgroup' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: false)
namespace.parent = build(:group)
end
it { is_expected.to be_falsey }
end
end
end
......@@ -323,6 +396,49 @@ describe Namespace do
end
end
describe '#shared_runners_enabled?' do
subject { namespace.shared_runners_enabled? }
context 'subgroup with shared runners enabled project' do
let(:subgroup) { create(:group, parent: namespace) }
let!(:subproject) { create(:project, namespace: subgroup, shared_runners_enabled: true) }
context 'when shared_runner_minutes_on_subnamespace is enabled' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: true)
end
it "returns false" do
is_expected.to eq(false)
end
end
context 'when shared_runner_minutes_on_subnamespace is disabled' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: false)
end
it "returns true" do
is_expected.to eq(true)
end
end
end
context 'group with shared runners enabled project' do
let!(:project) { create(:project, namespace: namespace, shared_runners_enabled: true) }
it "returns true" do
is_expected.to eq(true)
end
end
context 'group without projects' do
it "returns false" do
is_expected.to eq(false)
end
end
end
describe '#shared_runners_minutes_used?' do
subject { namespace.shared_runners_minutes_used? }
......@@ -400,4 +516,8 @@ describe Namespace do
end
end
end
describe '#validate_shared_runner_minutes_support' do
end
end
......@@ -8,9 +8,9 @@ describe Project do
it { is_expected.to delegate_method(:shared_runners_seconds).to(:statistics) }
it { is_expected.to delegate_method(:shared_runners_seconds_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) }
it { is_expected.to delegate_method(:shared_runners_minutes_used?).to(:namespace) }
it { is_expected.to delegate_method(:actual_shared_runners_minutes_limit).to(:shared_runners_limit_namespace) }
it { is_expected.to delegate_method(:shared_runners_minutes_limit_enabled?).to(:shared_runners_limit_namespace) }
it { is_expected.to delegate_method(:shared_runners_minutes_used?).to(:shared_runners_limit_namespace) }
it { is_expected.to have_one(:mirror_data).class_name('ProjectMirrorData') }
it { is_expected.to have_many(:path_locks) }
......@@ -532,6 +532,34 @@ describe Project do
end
end
describe '#shared_runners_limit_namespace' do
set(:root_ancestor) { create(:group) }
set(:group) { create(:group, parent: root_ancestor) }
let(:project) { create(:project, namespace: group) }
subject { project.shared_runners_limit_namespace }
context 'when shared_runner_minutes_on_subnamespace is enabled' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: true)
end
it 'returns parent namespace' do
is_expected.to eq(group)
end
end
context 'when shared_runner_minutes_on_subnamespace is disabled' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: false)
end
it 'returns root namespace' do
is_expected.to eq(root_ancestor)
end
end
end
describe '#shared_runners_minutes_limit_enabled?' do
let(:project) { create(:project) }
......
......@@ -62,6 +62,66 @@ module Ci
end
end
end
context 'when group is subgroup' do
let!(:root_ancestor) { create(:group, shared_runners_minutes_limit: 10) }
let!(:group) { create(:group, parent: root_ancestor, shared_runners_minutes_limit: 10) }
let!(:project) { create :project, shared_runners_enabled: true, group: group }
let(:build) { execute(shared_runner) }
context 'when shared_runner_minutes_on_subnamespace is enabled' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: true)
end
it "does return a build" do
expect(build).not_to be_nil
end
context 'when we are over limit on subnamespace' do
before do
group.create_namespace_statistics(
shared_runners_seconds: 6001)
end
it "does not return a build" do
expect(build).to be_nil
end
end
end
context 'when shared_runner_minutes_on_subnamespace is disabled' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: false)
end
it "does return a build" do
expect(build).not_to be_nil
end
context 'when we are over limit on subnamespace' do
before do
group.create_namespace_statistics(
shared_runners_seconds: 6001)
end
it "limit is ignored and build is returned" do
expect(build).not_to be_nil
end
end
context 'when we are over limit on root namespace' do
before do
root_ancestor.create_namespace_statistics(
shared_runners_seconds: 6001)
end
it "does not return a build" do
expect(build).to be_nil
end
end
end
end
end
def execute(runner)
......
require 'spec_helper'
describe 'admin/groups/_form' do
set(:admin) { create(:admin) }
before do
assign(:group, group)
allow(view).to receive(:can?) { true }
allow(view).to receive(:current_user) { admin }
allow(view).to receive(:visibility_level) { group.visibility_level }
end
describe 'when :shared_runner_minutes_on_subnamespace is enabled' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: true)
end
context 'when sub group is used' do
let(:root_ancestor) { create(:group) }
let(:group) { build(:group, parent: root_ancestor) }
it 'renders shared_runners_minutes_setting' do
render
expect(rendered).to render_template('namespaces/_shared_runners_minutes_setting')
end
end
end
describe 'when :shared_runner_minutes_on_subnamespace is disabled' do
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: false)
end
context 'when sub group is used' do
let(:root_ancestor) { create(:group) }
let(:group) { build(:group, parent: root_ancestor) }
it 'does not render shared_runners_minutes_setting' do
render
expect(rendered).not_to render_template('namespaces/_shared_runners_minutes_setting')
end
end
context 'when root group is used' do
let(:group) { build(:group) }
it 'does not render shared_runners_minutes_setting' do
render
expect(rendered).to render_template('namespaces/_shared_runners_minutes_setting')
end
end
end
end
RSpec.configure do |config|
config.before(:all) do
puts "this feature is by default disabled"
Feature.get(:shared_runner_minutes_on_subnamespace).disable
end
end
......@@ -83,6 +83,20 @@ describe Gitlab::GroupHierarchy, :postgresql do
end
end
describe '#root' do
it 'includes only the roots' do
relation = described_class.new(Group.where(id: child2)).roots
expect(relation).to contain_exactly(parent)
end
it 'when quering parent it includes parent' do
relation = described_class.new(Group.where(id: parent)).roots
expect(relation).to contain_exactly(parent)
end
end
describe '#all_groups' do
let(:relation) do
described_class.new(Group.where(id: child1.id)).all_groups
......
......@@ -3474,4 +3474,27 @@ describe Project do
expect(project.wiki_repository_exists?).to eq(false)
end
end
describe '#root_namespace' do
let(:project) { build(:project, namespace: parent) }
subject { project.root_namespace }
context 'when namespace has parent group' do
let(:root_ancestor) { create(:group) }
let(:parent) { build(:group, parent: root_ancestor) }
it 'returns root ancestor' do
is_expected.to eq(root_ancestor)
end
end
context 'when namespace is root ancestor' do
let(:parent) { build(:group) }
it 'returns current namespace' do
is_expected.to eq(parent)
end
end
end
end
......@@ -42,6 +42,40 @@ describe UpdateBuildMinutesService do
.to eq(100 + build.duration.to_i)
end
end
context 'when namespace is subgroup' do
let(:root_ancestor) { create(:group, shared_runners_minutes_limit: 100) }
context 'when shared_runner_minutes_on_subnamespace is enabled' do
let(:namespace) { create(:namespace, parent: root_ancestor, shared_runners_minutes_limit: 100) }
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: true)
end
it 'creates a statistics in current namespace' do
subject
expect(namespace.namespace_statistics.reload.shared_runners_seconds)
.to eq(build.duration.to_i)
end
end
context 'when shared_runner_minutes_on_subnamespace is disabled' do
let(:namespace) { create(:namespace, parent: root_ancestor) }
before do
stub_feature_flags(shared_runner_minutes_on_subnamespace: false)
end
it 'creates a statistics in root namespace' do
subject
expect(root_ancestor.namespace_statistics.reload.shared_runners_seconds)
.to eq(build.duration.to_i)
end
end
end
end
context 'for specific runner' do
......
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