Commit b81b8f3a authored by Thong Kuah's avatar Thong Kuah

Unique environment scope for group clusters

We allow setting of environment scopes in EE. Add validations for group
clusters.
parent 51679f61
......@@ -15,6 +15,11 @@ module EE
return false
end
if group && group.clusters.where(environment_scope: environment_scope).where.not(id: self.id).exists?
errors.add(:base, 'cannot add duplicated environment scope')
return false
end
true
end
end
......
......@@ -7,29 +7,57 @@ describe Clusters::Cluster do
subject { cluster.valid? }
context 'when validates unique_environment_scope' do
let(:project) { create(:project) }
context 'for a project cluster' do
let(:project) { create(:project) }
before do
create(:cluster, projects: [project], environment_scope: 'product/*')
end
before do
create(:cluster, projects: [project], environment_scope: 'product/*')
end
context 'when identical environment scope exists in project' do
let(:cluster) { build(:cluster, projects: [project], environment_scope: 'product/*') }
context 'when identical environment scope exists in project' do
let(:cluster) { build(:cluster, projects: [project], environment_scope: 'product/*') }
it { is_expected.to be_falsey }
end
it { is_expected.to be_falsey }
end
context 'when identical environment scope does not exist in project' do
let(:cluster) { build(:cluster, projects: [project], environment_scope: '*') }
context 'when identical environment scope does not exist in project' do
let(:cluster) { build(:cluster, projects: [project], environment_scope: '*') }
it { is_expected.to be_truthy }
end
it { is_expected.to be_truthy }
context 'when identical environment scope exists in different project' do
let(:project2) { create(:project) }
let(:cluster) { build(:cluster, projects: [project2], environment_scope: 'product/*') }
it { is_expected.to be_truthy }
end
end
context 'when identical environment scope exists in different project' do
let(:project2) { create(:project) }
let(:cluster) { build(:cluster, projects: [project2], environment_scope: 'product/*') }
context 'for a group cluster' do
let(:group) { create(:group) }
before do
create(:cluster, cluster_type: :group_type, groups: [group], environment_scope: 'product/*')
end
context 'when identical environment scope exists in group' do
let(:cluster) { build(:cluster, cluster_type: :group_type, groups: [group], environment_scope: 'product/*') }
it { is_expected.to be_falsey }
end
context 'when identical environment scope does not exist in group' do
let(:cluster) { build(:cluster, cluster_type: :group_type, groups: [group], environment_scope: '*') }
it { is_expected.to be_truthy }
end
context 'when identical environment scope exists in different group' do
let(:cluster) { build(:cluster, :group, environment_scope: 'product/*') }
it { is_expected.to be_truthy }
it { is_expected.to be_truthy }
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