2019-07-08 06:03:09 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Clusters::ClustersHierarchy do
|
2019-07-08 06:03:09 +00:00
|
|
|
describe '#base_and_ancestors' do
|
2019-10-14 12:06:14 +00:00
|
|
|
def base_and_ancestors(clusterable, include_management_project: true)
|
|
|
|
described_class.new(clusterable, include_management_project: include_management_project).base_and_ancestors
|
2019-07-08 06:03:09 +00:00
|
|
|
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-10-24 12:06:03 +00:00
|
|
|
|
|
|
|
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-07-08 06:03:09 +00:00
|
|
|
end
|
|
|
|
|
2019-10-14 12:06:14 +00:00
|
|
|
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-10-24 12:06:03 +00:00
|
|
|
let(:management_project) { create(:project, group: group) }
|
2019-10-14 12:06:14 +00:00
|
|
|
|
|
|
|
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-07-08 06:03:09 +00:00
|
|
|
context 'project in nested group with clusters at some levels' do
|
2019-10-24 12:06:03 +00:00
|
|
|
let!(:child) { create(:cluster, :group, groups: [child_group]) }
|
|
|
|
let!(:ancestor) { create(:cluster, :group, groups: [ancestor_group], management_project: management_project) }
|
2019-07-08 06:03:09 +00:00
|
|
|
|
|
|
|
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-10-24 12:06:03 +00:00
|
|
|
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-10-14 12:06:14 +00:00
|
|
|
|
|
|
|
it 'returns clusters for management_project' do
|
2019-10-24 12:06:03 +00:00
|
|
|
expect(base_and_ancestors(management_project, include_management_project: false)).to eq([child, ancestor])
|
2019-10-14 12:06:14 +00:00
|
|
|
end
|
2019-07-08 06:03:09 +00:00
|
|
|
|
|
|
|
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
|