2018-10-15 05:03:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-29 14:48:45 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Clusters::Cluster do
|
2018-12-03 08:01:30 -05:00
|
|
|
it_behaves_like 'having unique enum values'
|
2018-12-03 07:47:26 -05:00
|
|
|
|
2017-10-29 14:48:45 -04:00
|
|
|
it { is_expected.to belong_to(:user) }
|
2018-10-14 16:42:29 -04:00
|
|
|
it { is_expected.to have_many(:cluster_projects) }
|
2017-10-29 14:48:45 -04:00
|
|
|
it { is_expected.to have_many(:projects) }
|
2018-10-14 16:42:29 -04:00
|
|
|
it { is_expected.to have_many(:cluster_groups) }
|
|
|
|
it { is_expected.to have_many(:groups) }
|
2017-10-29 14:48:45 -04:00
|
|
|
it { is_expected.to have_one(:provider_gcp) }
|
|
|
|
it { is_expected.to have_one(:platform_kubernetes) }
|
2017-12-22 12:23:43 -05:00
|
|
|
it { is_expected.to have_one(:application_helm) }
|
|
|
|
it { is_expected.to have_one(:application_ingress) }
|
|
|
|
it { is_expected.to have_one(:application_prometheus) }
|
2018-03-01 18:46:02 -05:00
|
|
|
it { is_expected.to have_one(:application_runner) }
|
2018-10-16 16:03:59 -04:00
|
|
|
it { is_expected.to have_many(:kubernetes_namespaces) }
|
|
|
|
it { is_expected.to have_one(:kubernetes_namespace) }
|
2018-11-02 11:46:15 -04:00
|
|
|
it { is_expected.to have_one(:cluster_project) }
|
2018-10-16 16:03:59 -04:00
|
|
|
|
2017-10-29 14:48:45 -04:00
|
|
|
it { is_expected.to delegate_method(:status).to(:provider) }
|
|
|
|
it { is_expected.to delegate_method(:status_reason).to(:provider) }
|
|
|
|
it { is_expected.to delegate_method(:status_name).to(:provider) }
|
|
|
|
it { is_expected.to delegate_method(:on_creation?).to(:provider) }
|
2018-09-06 06:03:38 -04:00
|
|
|
it { is_expected.to delegate_method(:active?).to(:platform_kubernetes).with_prefix }
|
|
|
|
it { is_expected.to delegate_method(:rbac?).to(:platform_kubernetes).with_prefix }
|
2018-10-15 05:03:15 -04:00
|
|
|
it { is_expected.to delegate_method(:available?).to(:application_helm).with_prefix }
|
|
|
|
it { is_expected.to delegate_method(:available?).to(:application_ingress).with_prefix }
|
|
|
|
it { is_expected.to delegate_method(:available?).to(:application_prometheus).with_prefix }
|
2018-10-16 16:03:59 -04:00
|
|
|
|
2017-10-29 14:48:45 -04:00
|
|
|
it { is_expected.to respond_to :project }
|
|
|
|
|
|
|
|
describe '.enabled' do
|
|
|
|
subject { described_class.enabled }
|
|
|
|
|
|
|
|
let!(:cluster) { create(:cluster, enabled: true) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:cluster, enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(cluster) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.disabled' do
|
|
|
|
subject { described_class.disabled }
|
|
|
|
|
|
|
|
let!(:cluster) { create(:cluster, enabled: false) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:cluster, enabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(cluster) }
|
|
|
|
end
|
|
|
|
|
2018-03-29 15:17:18 -04:00
|
|
|
describe '.user_provided' do
|
|
|
|
subject { described_class.user_provided }
|
|
|
|
|
|
|
|
let!(:cluster) { create(:cluster, :provided_by_user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:cluster, :provided_by_gcp)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(cluster) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.gcp_provided' do
|
|
|
|
subject { described_class.gcp_provided }
|
|
|
|
|
|
|
|
let!(:cluster) { create(:cluster, :provided_by_gcp) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:cluster, :provided_by_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(cluster) }
|
|
|
|
end
|
|
|
|
|
2018-03-30 11:34:10 -04:00
|
|
|
describe '.gcp_installed' do
|
|
|
|
subject { described_class.gcp_installed }
|
|
|
|
|
|
|
|
let!(:cluster) { create(:cluster, :provided_by_gcp) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:cluster, :providing_by_gcp)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(cluster) }
|
|
|
|
end
|
|
|
|
|
2017-10-29 14:48:45 -04:00
|
|
|
describe 'validation' do
|
|
|
|
subject { cluster.valid? }
|
|
|
|
|
|
|
|
context 'when validates name' do
|
|
|
|
context 'when provided by user' do
|
|
|
|
let!(:cluster) { build(:cluster, :provided_by_user, name: name) }
|
|
|
|
|
|
|
|
context 'when name is empty' do
|
|
|
|
let(:name) { '' }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when name is nil' do
|
|
|
|
let(:name) { nil }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when name is present' do
|
|
|
|
let(:name) { 'cluster-name-1' }
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when provided by gcp' do
|
|
|
|
let!(:cluster) { build(:cluster, :provided_by_gcp, name: name) }
|
|
|
|
|
|
|
|
context 'when name is shorter than 1' do
|
|
|
|
let(:name) { '' }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when name is longer than 63' do
|
|
|
|
let(:name) { 'a' * 64 }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when name includes invalid character' do
|
|
|
|
let(:name) { '!!!!!!' }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when name is present' do
|
|
|
|
let(:name) { 'cluster-name-1' }
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when record is persisted' do
|
|
|
|
let(:name) { 'cluster-name-1' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
cluster.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when name is changed' do
|
|
|
|
before do
|
|
|
|
cluster.name = 'new-cluster-name'
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when name is same' do
|
|
|
|
before do
|
|
|
|
cluster.name = name
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when validates restrict_modification' do
|
|
|
|
context 'when creation is on going' do
|
|
|
|
let!(:cluster) { create(:cluster, :providing_by_gcp) }
|
|
|
|
|
|
|
|
it { expect(cluster.update(enabled: false)).to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when creation is done' do
|
|
|
|
let!(:cluster) { create(:cluster, :provided_by_gcp) }
|
|
|
|
|
|
|
|
it { expect(cluster.update(enabled: false)).to be_truthy }
|
|
|
|
end
|
|
|
|
end
|
2018-10-14 16:42:29 -04:00
|
|
|
|
|
|
|
describe 'cluster_type validations' do
|
|
|
|
let(:instance_cluster) { create(:cluster, :instance) }
|
|
|
|
let(:group_cluster) { create(:cluster, :group) }
|
|
|
|
let(:project_cluster) { create(:cluster, :project) }
|
|
|
|
|
|
|
|
it 'validates presence' do
|
|
|
|
cluster = build(:cluster, :project, cluster_type: nil)
|
|
|
|
|
|
|
|
expect(cluster).not_to be_valid
|
|
|
|
expect(cluster.errors.full_messages).to include("Cluster type can't be blank")
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'project_type cluster' do
|
|
|
|
it 'does not allow setting group' do
|
|
|
|
project_cluster.groups << build(:group)
|
|
|
|
|
|
|
|
expect(project_cluster).not_to be_valid
|
|
|
|
expect(project_cluster.errors.full_messages).to include('Cluster cannot have groups assigned')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'group_type cluster' do
|
|
|
|
it 'does not allow setting project' do
|
|
|
|
group_cluster.projects << build(:project)
|
|
|
|
|
|
|
|
expect(group_cluster).not_to be_valid
|
|
|
|
expect(group_cluster.errors.full_messages).to include('Cluster cannot have projects assigned')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'instance_type cluster' do
|
|
|
|
it 'does not allow setting group' do
|
|
|
|
instance_cluster.groups << build(:group)
|
|
|
|
|
|
|
|
expect(instance_cluster).not_to be_valid
|
|
|
|
expect(instance_cluster.errors.full_messages).to include('Cluster cannot have groups assigned')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow setting project' do
|
|
|
|
instance_cluster.projects << build(:project)
|
|
|
|
|
|
|
|
expect(instance_cluster).not_to be_valid
|
|
|
|
expect(instance_cluster.errors.full_messages).to include('Cluster cannot have projects assigned')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-10-29 14:48:45 -04:00
|
|
|
end
|
|
|
|
|
2018-10-28 22:28:36 -04:00
|
|
|
describe '.ordered_group_clusters_for_project' do
|
|
|
|
let(:group_cluster) { create(:cluster, :provided_by_gcp, :group) }
|
|
|
|
let(:group) { group_cluster.group }
|
|
|
|
|
|
|
|
subject { described_class.ordered_group_clusters_for_project(project.id) }
|
|
|
|
|
|
|
|
context 'when project does not belong to this group' do
|
|
|
|
let(:project) { create(:project, group: create(:group)) }
|
|
|
|
|
|
|
|
it 'returns nothing' do
|
|
|
|
expect(subject).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when group has a configured kubernetes cluster' do
|
|
|
|
let(:project) { create(:project, group: group) }
|
|
|
|
|
|
|
|
it 'returns the group cluster' do
|
|
|
|
expect(subject).to eq([group_cluster])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when sub-group has configured kubernetes cluster', :postgresql do
|
|
|
|
let(:sub_group_cluster) { create(:cluster, :provided_by_gcp, :group) }
|
|
|
|
let(:sub_group) { sub_group_cluster.group }
|
|
|
|
let(:project) { create(:project, group: sub_group) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sub_group.update!(parent: group)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns clusters in order, ascending the hierachy' do
|
|
|
|
expect(subject).to eq([group_cluster, sub_group_cluster])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'cluster_scope arg' do
|
|
|
|
let(:project) { create(:project, group: group) }
|
|
|
|
|
|
|
|
subject { described_class.none.ordered_group_clusters_for_project(project.id) }
|
|
|
|
|
|
|
|
it 'returns nothing' do
|
|
|
|
expect(subject).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-29 14:48:45 -04:00
|
|
|
describe '#provider' do
|
|
|
|
subject { cluster.provider }
|
|
|
|
|
|
|
|
context 'when provider is gcp' do
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_gcp) }
|
|
|
|
|
|
|
|
it 'returns a provider' do
|
|
|
|
is_expected.to eq(cluster.provider_gcp)
|
|
|
|
expect(subject.class.name.deconstantize).to eq(Clusters::Providers.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when provider is user' do
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_user) }
|
|
|
|
|
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#platform' do
|
|
|
|
subject { cluster.platform }
|
|
|
|
|
|
|
|
context 'when platform is kubernetes' do
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_user) }
|
|
|
|
|
|
|
|
it 'returns a platform' do
|
|
|
|
is_expected.to eq(cluster.platform_kubernetes)
|
|
|
|
expect(subject.class.name.deconstantize).to eq(Clusters::Platforms.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-26 04:01:18 -05:00
|
|
|
describe '#all_projects' do
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:cluster) { create(:cluster, projects: [project]) }
|
|
|
|
|
|
|
|
subject { cluster.all_projects }
|
|
|
|
|
|
|
|
context 'project cluster' do
|
|
|
|
it 'returns project' do
|
|
|
|
is_expected.to eq([project])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'group cluster' do
|
|
|
|
let(:cluster) { create(:cluster, :group) }
|
|
|
|
let(:group) { cluster.group }
|
|
|
|
let(:project) { create(:project, group: group) }
|
|
|
|
let(:subgroup) { create(:group, parent: group) }
|
|
|
|
let(:subproject) { create(:project, group: subgroup) }
|
|
|
|
|
|
|
|
it 'returns all projects for group' do
|
|
|
|
is_expected.to contain_exactly(project, subproject)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-29 14:48:45 -04:00
|
|
|
describe '#first_project' do
|
|
|
|
subject { cluster.first_project }
|
|
|
|
|
|
|
|
context 'when cluster belongs to a project' do
|
|
|
|
let(:cluster) { create(:cluster, :project) }
|
|
|
|
let(:project) { Clusters::Project.find_by_cluster_id(cluster.id).project }
|
|
|
|
|
|
|
|
it { is_expected.to eq(project) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when cluster does not belong to projects' do
|
|
|
|
let(:cluster) { create(:cluster) }
|
|
|
|
|
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
|
|
|
end
|
2017-11-07 09:10:40 -05:00
|
|
|
|
2018-10-14 16:42:29 -04:00
|
|
|
describe '#group' do
|
|
|
|
subject { cluster.group }
|
|
|
|
|
|
|
|
context 'when cluster belongs to a group' do
|
|
|
|
let(:cluster) { create(:cluster, :group) }
|
|
|
|
let(:group) { cluster.groups.first }
|
|
|
|
|
|
|
|
it { is_expected.to eq(group) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when cluster does not belong to any group' do
|
|
|
|
let(:cluster) { create(:cluster) }
|
|
|
|
|
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-07 09:10:40 -05:00
|
|
|
describe '#applications' do
|
|
|
|
set(:cluster) { create(:cluster) }
|
|
|
|
|
|
|
|
subject { cluster.applications }
|
|
|
|
|
|
|
|
context 'when none of applications are created' do
|
|
|
|
it 'returns a list of a new objects' do
|
|
|
|
is_expected.not_to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when applications are created' do
|
2017-12-22 12:23:43 -05:00
|
|
|
let!(:helm) { create(:clusters_applications_helm, cluster: cluster) }
|
|
|
|
let!(:ingress) { create(:clusters_applications_ingress, cluster: cluster) }
|
2018-11-19 15:45:33 -05:00
|
|
|
let!(:cert_manager) { create(:clusters_applications_cert_managers, cluster: cluster) }
|
2017-12-22 12:23:43 -05:00
|
|
|
let!(:prometheus) { create(:clusters_applications_prometheus, cluster: cluster) }
|
2018-03-01 18:46:02 -05:00
|
|
|
let!(:runner) { create(:clusters_applications_runner, cluster: cluster) }
|
2018-05-16 05:01:13 -04:00
|
|
|
let!(:jupyter) { create(:clusters_applications_jupyter, cluster: cluster) }
|
2018-11-03 17:43:48 -04:00
|
|
|
let!(:knative) { create(:clusters_applications_knative, cluster: cluster) }
|
2017-11-07 09:10:40 -05:00
|
|
|
|
|
|
|
it 'returns a list of created applications' do
|
2018-11-19 15:45:33 -05:00
|
|
|
is_expected.to contain_exactly(helm, ingress, cert_manager, prometheus, runner, jupyter, knative)
|
2017-11-07 09:10:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-01 10:49:56 -05:00
|
|
|
|
|
|
|
describe '#created?' do
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_gcp) }
|
|
|
|
|
|
|
|
subject { cluster.created? }
|
|
|
|
|
|
|
|
context 'when status_name is :created' do
|
|
|
|
before do
|
|
|
|
allow(cluster).to receive_message_chain(:provider, :status_name).and_return(:created)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when status_name is not :created' do
|
|
|
|
before do
|
|
|
|
allow(cluster).to receive_message_chain(:provider, :status_name).and_return(:creating)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(false) }
|
|
|
|
end
|
|
|
|
end
|
2018-11-06 18:30:36 -05:00
|
|
|
|
|
|
|
describe '#allow_user_defined_namespace?' do
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_gcp) }
|
|
|
|
|
|
|
|
subject { cluster.allow_user_defined_namespace? }
|
|
|
|
|
|
|
|
context 'project type cluster' do
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'group type cluster' do
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_gcp, :group) }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'instance type cluster' do
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_gcp, :instance) }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
2017-10-29 14:48:45 -04:00
|
|
|
end
|