Commit cbc8dc94 authored by Dylan Griffith's avatar Dylan Griffith

EE: Refactor: model errors for multi cluster validation

The current approach requires catching exceptions to handle these errors
and callers are already handling model validations so it seems more
appropriate.  Also it seemed to convoluted to add this logic directly to
the model since the model needs to check too many possible associations
to determine whether or not there are more than one cluster since the
model doesn't know what it's being created on. Additionally we only
wanted to validate during create to avoid the risk of existing models
becoming invalid by many different edge cases.
parent 8bc27d4c
# frozen_string_literal: true
module EE
module ClusterableActions
extend ::Gitlab::Utils::Override
private
override :multiple_clusters_available?
def multiple_clusters_available?
subject.feature_available?(:multiple_clusters)
end
end
end
......@@ -2,8 +2,17 @@
module EE
module ClusterablePresenter
extend ::Gitlab::Utils::Override
def metrics_cluster_path(cluster, params = {})
raise NotImplementedError
end
private
override :multiple_clusters_available?
def multiple_clusters_available?
clusterable.feature_available?(:multiple_clusters)
end
end
end
......@@ -7,40 +7,22 @@ describe Clusters::InstancePolicy do
let(:policy) { described_class.new(user, Clusters::Instance.new) }
describe 'rules' do
context 'multiple clusters allowed' do
before do
stub_feature_flags(multiple_clusters: true)
end
context 'no existing instance level cluster' do
context 'when admin' do
it { expect(policy).to be_allowed :read_cluster }
it { expect(policy).to be_allowed :add_cluster }
it { expect(policy).to be_allowed :create_cluster }
it { expect(policy).to be_allowed :update_cluster }
it { expect(policy).to be_allowed :admin_cluster }
end
context 'with an existing instance level cluster' do
before do
create(:cluster, :instance)
end
it { expect(policy).to be_allowed :add_cluster }
end
end
context 'multiple clusters disallowed' do
before do
stub_feature_flags(multiple_clusters: false)
end
context 'no existing instance level cluster' do
it { expect(policy).to be_allowed :add_cluster }
end
context 'with an existing instance level cluster' do
before do
create(:cluster, :instance)
end
context 'when not admin' do
let(:user) { create(:user) }
it { expect(policy).to be_disallowed :read_cluster }
it { expect(policy).to be_disallowed :add_cluster }
end
it { expect(policy).to be_disallowed :create_cluster }
it { expect(policy).to be_disallowed :update_cluster }
it { expect(policy).to be_disallowed :admin_cluster }
end
end
end
......@@ -374,14 +374,4 @@ describe GroupPolicy do
end
end
end
it_behaves_like 'ee clusterable policies' do
let(:clusterable) { create(:group) }
let(:cluster) do
create(:cluster,
:provided_by_gcp,
:group,
groups: [clusterable])
end
end
end
......@@ -737,17 +737,6 @@ describe ProjectPolicy do
end
end
it_behaves_like 'ee clusterable policies' do
let(:clusterable) { create(:project, :repository) }
let(:cluster) do
create(:cluster,
:provided_by_gcp,
:project,
projects: [clusterable])
end
end
context 'alert bot' do
let(:current_user) { User.alert_bot }
......
# frozen_string_literal: true
require 'spec_helper'
shared_examples 'ee clusterable policies' do
describe '#can_add_cluster?' do
let(:current_user) { create(:user) }
subject { described_class.new(current_user, clusterable) }
before do
clusterable.add_maintainer(current_user)
end
context 'when multiple_clusters feature is available' do
before do
stub_licensed_features(multiple_clusters: true)
end
context 'when clusterable has clusters' do
before do
cluster
end
it { is_expected.to be_allowed(:add_cluster) }
end
context 'when clusterable does not have clusters' do
it { is_expected.to be_allowed(:add_cluster) }
end
end
context 'when multiple_clusters feature is not available' do
before do
stub_licensed_features(multiple_clusters: false)
end
context 'when clusterable has clusters' do
before do
cluster
end
it { is_expected.to be_disallowed(:add_cluster) }
end
context 'when clusterable does not have clusters' do
it { is_expected.to be_allowed(:add_cluster) }
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