Commit 8bdd718f authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch '5968-extract-ee-specific-files-lines-for-clusters-related-files' into 'master'

Resolve "Extract EE specific files/lines for clusters related files"

Closes #5968

See merge request gitlab-org/gitlab-ee!5687
parents 582a1576 50ef042f
module Clusters
class Cluster < ActiveRecord::Base
prepend EE::Clusters::Cluster
include Presentable
prepend HasEnvironmentScope
self.table_name = 'clusters'
......@@ -32,7 +33,6 @@ module Clusters
accepts_nested_attributes_for :platform_kubernetes, update_only: true
validates :name, cluster_name: true
validate :unique_environment_scope
validate :restrict_modification, on: :update
delegate :status, to: :provider, allow_nil: true
......@@ -105,15 +105,6 @@ module Clusters
private
def unique_environment_scope
if project && project.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
def restrict_modification
if provider&.on_creation?
errors.add(:base, "cannot modify during creation")
......
module Clusters
class CreateService < BaseService
prepend EE::Clusters::CreateService
attr_reader :access_token
def execute(access_token = nil)
......@@ -29,9 +31,7 @@ module Clusters
end
def can_create_cluster?
return true if project.clusters.empty?
project.feature_available?(:multiple_clusters)
project.clusters.empty?
end
end
end
module EE
module Clusters
module Cluster
extend ActiveSupport::Concern
prepended do
prepend HasEnvironmentScope
validate :unique_environment_scope
end
def unique_environment_scope
if project && project.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
end
end
module EE
module Clusters
module CreateService
extend ::Gitlab::Utils::Override
override :can_create_cluster?
def can_create_cluster?
super || project.feature_available?(:multiple_clusters)
end
end
end
end
require 'spec_helper'
describe Clusters::CreateService do
let(:access_token) { 'xxx' }
let(:project) { create(:project) }
let(:user) { create(:user) }
let!(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, projects: [project]) }
subject { described_class.new(project, user, params).execute(access_token) }
before do
allow(project).to receive(:feature_available?).and_call_original
end
context 'when license has multiple clusters feature' do
before do
allow(project).to receive(:feature_available?).with(:multiple_clusters).and_return(true)
end
context 'when correct params' do
include_context 'valid cluster create params'
include_examples 'create cluster service success'
end
context 'when invalid params' do
include_context 'invalid cluster create params'
include_examples 'create cluster service error'
end
end
context 'when license does not have multiple clusters feature' do
include_context 'valid cluster create params'
before do
allow(project).to receive(:feature_available?).with(:multiple_clusters).and_return(false)
end
it 'does not create a cluster' do
expect(ClusterProvisionWorker).not_to receive(:perform_async)
expect { subject }.to raise_error(ArgumentError).and change { Clusters::Cluster.count }.by(0)
end
end
end
......@@ -8,116 +8,27 @@ describe Clusters::CreateService do
subject { described_class.new(project, user, params).execute(access_token) }
context 'when provider is gcp' do
shared_context 'valid params' do
let(:params) do
{
name: 'test-cluster',
provider_type: :gcp,
provider_gcp_attributes: {
gcp_project_id: 'gcp-project',
zone: 'us-central1-a',
num_nodes: 1,
machine_type: 'machine_type-a'
}
}
end
end
shared_context 'invalid params' do
let(:params) do
{
name: 'test-cluster',
provider_type: :gcp,
provider_gcp_attributes: {
gcp_project_id: '!!!!!!!',
zone: 'us-central1-a',
num_nodes: 1,
machine_type: 'machine_type-a'
}
}
end
end
shared_examples 'create cluster' do
it 'creates a cluster object and performs a worker' do
expect(ClusterProvisionWorker).to receive(:perform_async)
expect { subject }
.to change { Clusters::Cluster.count }.by(1)
.and change { Clusters::Providers::Gcp.count }.by(1)
expect(subject.name).to eq('test-cluster')
expect(subject.user).to eq(user)
expect(subject.project).to eq(project)
expect(subject.provider.gcp_project_id).to eq('gcp-project')
expect(subject.provider.zone).to eq('us-central1-a')
expect(subject.provider.num_nodes).to eq(1)
expect(subject.provider.machine_type).to eq('machine_type-a')
expect(subject.provider.access_token).to eq(access_token)
expect(subject.platform).to be_nil
end
end
shared_examples 'error' do
it 'returns an error' do
expect(ClusterProvisionWorker).not_to receive(:perform_async)
expect { subject }.to change { Clusters::Cluster.count }.by(0)
expect(subject.errors[:"provider_gcp.gcp_project_id"]).to be_present
end
end
context 'when project has no clusters' do
context 'when correct params' do
include_context 'valid params'
include_context 'valid cluster create params'
include_examples 'create cluster'
include_examples 'create cluster service success'
end
context 'when invalid params' do
include_context 'invalid params'
include_context 'invalid cluster create params'
include_examples 'error'
include_examples 'create cluster service error'
end
end
context 'when project has a cluster' do
include_context 'valid params'
include_context 'valid cluster create params'
let!(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, projects: [project]) }
before do
allow(project).to receive(:feature_available?).and_call_original
end
context 'when license has multiple clusters feature' do
before do
allow(project).to receive(:feature_available?).with(:multiple_clusters).and_return(true)
end
context 'when correct params' do
include_context 'valid params'
include_examples 'create cluster'
end
context 'when invalid params' do
include_context 'invalid params'
include_examples 'error'
end
end
context 'when license does not have multiple clusters feature' do
include_context 'valid params'
before do
allow(project).to receive(:feature_available?).with(:multiple_clusters).and_return(false)
end
it 'does not create a cluster' do
expect(ClusterProvisionWorker).not_to receive(:perform_async)
expect { subject }.to raise_error(ArgumentError).and change { Clusters::Cluster.count }.by(0)
end
it 'does not create a cluster' do
expect(ClusterProvisionWorker).not_to receive(:perform_async)
expect { subject }.to raise_error(ArgumentError).and change { Clusters::Cluster.count }.by(0)
end
end
end
......
shared_context 'valid cluster create params' do
let(:params) do
{
name: 'test-cluster',
provider_type: :gcp,
provider_gcp_attributes: {
gcp_project_id: 'gcp-project',
zone: 'us-central1-a',
num_nodes: 1,
machine_type: 'machine_type-a'
}
}
end
end
shared_context 'invalid cluster create params' do
let(:params) do
{
name: 'test-cluster',
provider_type: :gcp,
provider_gcp_attributes: {
gcp_project_id: '!!!!!!!',
zone: 'us-central1-a',
num_nodes: 1,
machine_type: 'machine_type-a'
}
}
end
end
shared_examples 'create cluster service success' do
it 'creates a cluster object and performs a worker' do
expect(ClusterProvisionWorker).to receive(:perform_async)
expect { subject }
.to change { Clusters::Cluster.count }.by(1)
.and change { Clusters::Providers::Gcp.count }.by(1)
expect(subject.name).to eq('test-cluster')
expect(subject.user).to eq(user)
expect(subject.project).to eq(project)
expect(subject.provider.gcp_project_id).to eq('gcp-project')
expect(subject.provider.zone).to eq('us-central1-a')
expect(subject.provider.num_nodes).to eq(1)
expect(subject.provider.machine_type).to eq('machine_type-a')
expect(subject.provider.access_token).to eq(access_token)
expect(subject.platform).to be_nil
end
end
shared_examples 'create cluster service error' do
it 'returns an error' do
expect(ClusterProvisionWorker).not_to receive(:perform_async)
expect { subject }.to change { Clusters::Cluster.count }.by(0)
expect(subject.errors[:"provider_gcp.gcp_project_id"]).to be_present
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