debian-mirror-gitlab/spec/models/clusters/clusters_hierarchy_spec.rb

126 lines
4.6 KiB
Ruby
Raw Normal View History

2019-09-30 21:07:59 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Clusters::ClustersHierarchy do
2019-09-30 21:07:59 +05:30
describe '#base_and_ancestors' do
2019-12-21 20:55:43 +05:30
def base_and_ancestors(clusterable, include_management_project: true)
described_class.new(clusterable, include_management_project: include_management_project).base_and_ancestors
2019-09-30 21:07:59 +05:30
end
context 'project in nested group with clusters at every level' do
let!(:cluster) { create(:cluster, :project, projects: [project]) }
let!(:child) { create(:cluster, :group, groups: [child_group]) }
let!(:parent) { create(:cluster, :group, groups: [parent_group]) }
let!(:ancestor) { create(:cluster, :group, groups: [ancestor_group]) }
let(:ancestor_group) { create(:group) }
let(:parent_group) { create(:group, parent: ancestor_group) }
let(:child_group) { create(:group, parent: parent_group) }
let(:project) { create(:project, group: child_group) }
it 'returns clusters for project' do
expect(base_and_ancestors(project)).to eq([cluster, child, parent, ancestor])
end
it 'returns clusters for child_group' do
expect(base_and_ancestors(child_group)).to eq([child, parent, ancestor])
end
it 'returns clusters for parent_group' do
expect(base_and_ancestors(parent_group)).to eq([parent, ancestor])
end
it 'returns clusters for ancestor_group' do
expect(base_and_ancestors(ancestor_group)).to eq([ancestor])
end
end
context 'project in a namespace' do
let!(:cluster) { create(:cluster, :project) }
it 'returns clusters for project' do
expect(base_and_ancestors(cluster.project)).to eq([cluster])
end
2019-12-26 22:10:19 +05:30
context 'cluster has management project' do
let(:management_project) { create(:project, namespace: cluster.first_project.namespace) }
before do
cluster.update!(management_project: management_project)
end
context 'management_project is in same namespace as cluster' do
it 'returns cluster for management_project' do
expect(base_and_ancestors(management_project)).to eq([cluster])
end
end
context 'management_project is in a different namespace from cluster' do
let(:management_project) { create(:project) }
it 'returns nothing' do
expect(base_and_ancestors(management_project)).to be_empty
end
end
end
2019-09-30 21:07:59 +05:30
end
2019-12-21 20:55:43 +05:30
context 'cluster has management project' do
let!(:project_cluster) { create(:cluster, :project, projects: [project]) }
let!(:group_cluster) { create(:cluster, :group, groups: [group], management_project: management_project) }
let(:group) { create(:group) }
let(:project) { create(:project, group: group) }
2019-12-26 22:10:19 +05:30
let(:management_project) { create(:project, group: group) }
2019-12-21 20:55:43 +05:30
it 'returns clusters for management_project' do
expect(base_and_ancestors(management_project)).to eq([group_cluster])
end
it 'returns clusters for project' do
expect(base_and_ancestors(project)).to eq([project_cluster, group_cluster])
end
it 'returns clusters for group' do
expect(base_and_ancestors(group)).to eq([group_cluster])
end
end
2019-09-30 21:07:59 +05:30
context 'project in nested group with clusters at some levels' do
2019-12-26 22:10:19 +05:30
let!(:child) { create(:cluster, :group, groups: [child_group]) }
let!(:ancestor) { create(:cluster, :group, groups: [ancestor_group], management_project: management_project) }
2019-09-30 21:07:59 +05:30
let(:ancestor_group) { create(:group) }
let(:parent_group) { create(:group, parent: ancestor_group) }
let(:child_group) { create(:group, parent: parent_group) }
let(:project) { create(:project, group: child_group) }
2019-12-26 22:10:19 +05:30
let(:management_project) { create(:project, group: child_group) }
it 'returns clusters for management_project' do
expect(base_and_ancestors(management_project)).to eq([ancestor, child])
end
2019-12-21 20:55:43 +05:30
it 'returns clusters for management_project' do
2019-12-26 22:10:19 +05:30
expect(base_and_ancestors(management_project, include_management_project: false)).to eq([child, ancestor])
2019-12-21 20:55:43 +05:30
end
2019-09-30 21:07:59 +05:30
it 'returns clusters for project' do
expect(base_and_ancestors(project)).to eq([child, ancestor])
end
it 'returns clusters for child_group' do
expect(base_and_ancestors(child_group)).to eq([child, ancestor])
end
it 'returns clusters for parent_group' do
expect(base_and_ancestors(parent_group)).to eq([ancestor])
end
it 'returns clusters for ancestor_group' do
expect(base_and_ancestors(ancestor_group)).to eq([ancestor])
end
end
end
end