2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Clusters::CreateService do
|
|
|
|
let(:access_token) { 'xxx' }
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
subject { described_class.new(user, params).execute(access_token: access_token) }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
context 'when provider is gcp' do
|
|
|
|
context 'when project has no clusters' do
|
|
|
|
context 'when correct params' do
|
2018-12-13 13:39:08 +05:30
|
|
|
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',
|
|
|
|
legacy_abac: 'true'
|
|
|
|
},
|
|
|
|
clusterable: project
|
|
|
|
}
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
include_examples 'create cluster service success'
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when invalid params' do
|
2018-12-13 13:39:08 +05:30
|
|
|
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'
|
|
|
|
},
|
|
|
|
clusterable: project
|
|
|
|
}
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
include_examples 'create cluster service error'
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project has a cluster' do
|
2018-11-08 19:23:39 +05:30
|
|
|
include_context 'valid cluster create params'
|
2018-05-01 15:08:00 +05:30
|
|
|
let!(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, projects: [project]) }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
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
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
context 'when params includes :management_project_id' do
|
|
|
|
subject(:cluster) { described_class.new(user, params).execute(access_token: access_token) }
|
|
|
|
|
|
|
|
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',
|
|
|
|
legacy_abac: 'true'
|
|
|
|
},
|
|
|
|
clusterable: clusterable,
|
|
|
|
management_project_id: management_project_id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:clusterable) { project }
|
|
|
|
let(:management_project_id) { management_project.id }
|
|
|
|
let(:management_project_namespace) { project.namespace }
|
|
|
|
let(:management_project) { create(:project, namespace: management_project_namespace) }
|
|
|
|
|
|
|
|
shared_examples 'invalid project or cluster permissions' do
|
|
|
|
it 'does not persist the cluster and adds errors' do
|
|
|
|
expect(cluster).not_to be_persisted
|
|
|
|
|
|
|
|
expect(cluster.errors[:management_project_id]).to include('Project does not exist or you don\'t have permission to perform this action')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'setting a management project' do
|
|
|
|
context 'when user is authorized to adminster manangement_project' do
|
|
|
|
before do
|
|
|
|
management_project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'persists the cluster' do
|
|
|
|
expect(cluster).to be_persisted
|
|
|
|
|
|
|
|
expect(cluster.management_project).to eq(management_project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not authorized to adminster manangement_project' do
|
|
|
|
include_examples 'invalid project or cluster permissions'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'setting a management project outside of scope' do
|
|
|
|
context 'when manangement_project is outside of the namespace scope' do
|
|
|
|
let(:management_project_namespace) { create(:group) }
|
|
|
|
|
|
|
|
it 'does not persist the cluster' do
|
|
|
|
expect(cluster).not_to be_persisted
|
|
|
|
|
|
|
|
expect(cluster.errors[:management_project_id]).to include('Project does not exist or you don\'t have permission to perform this action')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'management_project is non-existent' do
|
|
|
|
let(:management_project_id) { 0 }
|
|
|
|
|
|
|
|
include_examples 'invalid project or cluster permissions'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'project cluster' do
|
|
|
|
include_examples 'setting a management project'
|
|
|
|
include_examples 'setting a management project outside of scope'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'group cluster' do
|
|
|
|
let(:management_project_namespace) { create(:group) }
|
|
|
|
let(:clusterable) { management_project_namespace }
|
|
|
|
|
|
|
|
include_examples 'setting a management project'
|
|
|
|
include_examples 'setting a management project outside of scope'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'instance cluster' do
|
|
|
|
let(:clusterable) { Clusters::Instance.new }
|
|
|
|
|
|
|
|
include_examples 'setting a management project'
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|