2018-10-14 23:37:51 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-03 16:03:21 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-10-31 20:39:01 -04:00
|
|
|
describe Projects::ClustersController do
|
2017-12-03 16:03:21 -05:00
|
|
|
include AccessMatchersForController
|
2018-05-31 07:48:04 -04:00
|
|
|
include GoogleApi::CloudPlatformHelpers
|
2018-11-02 11:46:15 -04:00
|
|
|
include KubernetesHelpers
|
2017-12-03 16:03:21 -05:00
|
|
|
|
|
|
|
set(:project) { create(:project) }
|
|
|
|
|
2018-10-14 17:24:25 -04:00
|
|
|
let(:user) { create(:user) }
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-14 17:24:25 -04:00
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-14 17:24:25 -04:00
|
|
|
describe 'GET index' do
|
2018-10-16 04:50:44 -04:00
|
|
|
def go(params = {})
|
2018-12-17 17:52:17 -05:00
|
|
|
get :index, params: params.reverse_merge(namespace_id: project.namespace.to_param, project_id: project)
|
2018-10-14 17:24:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'functionality' do
|
2017-12-03 16:03:21 -05:00
|
|
|
context 'when project has one or more clusters' do
|
|
|
|
let(:project) { create(:project) }
|
2017-12-05 10:12:11 -05:00
|
|
|
let!(:enabled_cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
|
2018-03-21 10:59:40 -04:00
|
|
|
let!(:disabled_cluster) { create(:cluster, :disabled, :provided_by_gcp, :production_environment, projects: [project]) }
|
2017-12-03 16:03:21 -05:00
|
|
|
it 'lists available clusters' do
|
2018-10-16 04:50:44 -04:00
|
|
|
go
|
2017-12-03 16:03:21 -05:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to render_template(:index)
|
|
|
|
expect(assigns(:clusters)).to match_array([enabled_cluster, disabled_cluster])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when page is specified' do
|
|
|
|
let(:last_page) { project.clusters.page.total_pages }
|
|
|
|
|
|
|
|
before do
|
2017-12-05 10:20:24 -05:00
|
|
|
allow(Clusters::Cluster).to receive(:paginates_per).and_return(1)
|
2018-03-21 10:59:40 -04:00
|
|
|
create_list(:cluster, 2, :provided_by_gcp, :production_environment, projects: [project])
|
2017-12-03 16:03:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to the page' do
|
2018-10-16 04:50:44 -04:00
|
|
|
go(page: last_page)
|
2018-10-14 17:24:25 -04:00
|
|
|
|
2017-12-03 16:03:21 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(assigns(:clusters).current_page).to eq(last_page)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project does not have a cluster' do
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
|
|
|
|
it 'returns an empty state page' do
|
2018-10-16 04:50:44 -04:00
|
|
|
go
|
2017-12-03 16:03:21 -05:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-12-05 09:26:37 -05:00
|
|
|
expect(response).to render_template(:index, partial: :empty_state)
|
2017-12-03 16:03:21 -05:00
|
|
|
expect(assigns(:clusters)).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
|
|
|
|
|
2018-10-16 04:50:44 -04:00
|
|
|
it { expect { go }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { go }.to be_allowed_for(:owner).of(project) }
|
|
|
|
it { expect { go }.to be_allowed_for(:maintainer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:developer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:reporter).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:guest).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:user) }
|
|
|
|
it { expect { go }.to be_denied_for(:external) }
|
2017-12-03 16:03:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-31 07:48:04 -04:00
|
|
|
describe 'GET new' do
|
2018-10-16 04:50:44 -04:00
|
|
|
def go
|
2018-12-17 17:52:17 -05:00
|
|
|
get :new, params: { namespace_id: project.namespace, project_id: project }
|
2018-10-14 17:24:25 -04:00
|
|
|
end
|
2018-05-31 07:48:04 -04:00
|
|
|
|
2018-10-14 17:24:25 -04:00
|
|
|
describe 'functionality for new cluster' do
|
2018-05-31 07:48:04 -04:00
|
|
|
context 'when omniauth has been configured' do
|
|
|
|
let(:key) { 'secret-key' }
|
|
|
|
let(:session_key_for_redirect_uri) do
|
|
|
|
GoogleApi::CloudPlatform::Client.session_key_for_redirect_uri(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(SecureRandom).to receive(:hex).and_return(key)
|
|
|
|
end
|
|
|
|
|
2018-06-12 01:57:04 -04:00
|
|
|
it 'has authorize_url' do
|
2018-10-16 04:50:44 -04:00
|
|
|
go
|
2018-05-31 07:48:04 -04:00
|
|
|
|
2018-06-12 01:57:04 -04:00
|
|
|
expect(assigns(:authorize_url)).to include(key)
|
2018-05-31 07:48:04 -04:00
|
|
|
expect(session[session_key_for_redirect_uri]).to eq(new_project_cluster_path(project))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when omniauth has not configured' do
|
|
|
|
before do
|
|
|
|
stub_omniauth_setting(providers: [])
|
|
|
|
end
|
|
|
|
|
2018-06-12 01:57:04 -04:00
|
|
|
it 'does not have authorize_url' do
|
2018-10-16 04:50:44 -04:00
|
|
|
go
|
2018-05-31 07:48:04 -04:00
|
|
|
|
2018-06-12 01:57:04 -04:00
|
|
|
expect(assigns(:authorize_url)).to be_nil
|
2018-05-31 07:48:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when access token is valid' do
|
|
|
|
before do
|
|
|
|
stub_google_api_validate_token
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has new object' do
|
2018-10-16 04:50:44 -04:00
|
|
|
go
|
2018-05-31 07:48:04 -04:00
|
|
|
|
2018-11-06 03:55:10 -05:00
|
|
|
expect(assigns(:gcp_cluster)).to be_an_instance_of(Clusters::ClusterPresenter)
|
2018-05-31 07:48:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when access token is expired' do
|
|
|
|
before do
|
|
|
|
stub_google_api_expired_token
|
|
|
|
end
|
|
|
|
|
2018-06-12 01:57:04 -04:00
|
|
|
it { expect(@valid_gcp_token).to be_falsey }
|
2018-05-31 07:48:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when access token is not stored in session' do
|
2018-06-12 01:57:04 -04:00
|
|
|
it { expect(@valid_gcp_token).to be_falsey }
|
2018-05-31 07:48:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-12 01:57:04 -04:00
|
|
|
describe 'functionality for existing cluster' do
|
2018-05-31 07:48:04 -04:00
|
|
|
it 'has new object' do
|
2018-10-16 04:50:44 -04:00
|
|
|
go
|
2018-05-31 07:48:04 -04:00
|
|
|
|
2018-11-06 03:55:10 -05:00
|
|
|
expect(assigns(:user_cluster)).to be_an_instance_of(Clusters::ClusterPresenter)
|
2018-05-31 07:48:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
2018-10-16 04:50:44 -04:00
|
|
|
it { expect { go }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { go }.to be_allowed_for(:owner).of(project) }
|
|
|
|
it { expect { go }.to be_allowed_for(:maintainer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:developer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:reporter).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:guest).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:user) }
|
|
|
|
it { expect { go }.to be_denied_for(:external) }
|
2018-05-31 07:48:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-12 01:57:04 -04:00
|
|
|
describe 'POST create for new cluster' do
|
2018-09-07 09:32:31 -04:00
|
|
|
let(:legacy_abac_param) { 'true' }
|
2018-06-12 01:57:04 -04:00
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: {
|
|
|
|
name: 'new-cluster',
|
2019-05-02 21:05:53 -04:00
|
|
|
managed: '1',
|
2018-06-12 01:57:04 -04:00
|
|
|
provider_gcp_attributes: {
|
2018-09-07 09:32:31 -04:00
|
|
|
gcp_project_id: 'gcp-project-12345',
|
|
|
|
legacy_abac: legacy_abac_param
|
2018-06-12 01:57:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-10-16 04:50:44 -04:00
|
|
|
def go
|
2018-12-17 17:52:17 -05:00
|
|
|
post :create_gcp, params: params.merge(namespace_id: project.namespace, project_id: project)
|
2018-10-14 17:24:25 -04:00
|
|
|
end
|
2018-06-12 01:57:04 -04:00
|
|
|
|
2018-10-14 17:24:25 -04:00
|
|
|
describe 'functionality' do
|
2018-06-12 01:57:04 -04:00
|
|
|
context 'when access token is valid' do
|
|
|
|
before do
|
|
|
|
stub_google_api_validate_token
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new cluster' do
|
|
|
|
expect(ClusterProvisionWorker).to receive(:perform_async)
|
2018-10-16 04:50:44 -04:00
|
|
|
expect { go }.to change { Clusters::Cluster.count }
|
2018-06-12 01:57:04 -04:00
|
|
|
.and change { Clusters::Providers::Gcp.count }
|
|
|
|
expect(response).to redirect_to(project_cluster_path(project, project.clusters.first))
|
|
|
|
expect(project.clusters.first).to be_gcp
|
|
|
|
expect(project.clusters.first).to be_kubernetes
|
2018-09-07 09:32:31 -04:00
|
|
|
expect(project.clusters.first.provider_gcp).to be_legacy_abac
|
2019-05-02 21:05:53 -04:00
|
|
|
expect(project.clusters.first.managed?).to be_truthy
|
2018-09-07 09:32:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when legacy_abac param is false' do
|
|
|
|
let(:legacy_abac_param) { 'false' }
|
|
|
|
|
|
|
|
it 'creates a new cluster with legacy_abac_disabled' do
|
|
|
|
expect(ClusterProvisionWorker).to receive(:perform_async)
|
2018-10-16 04:50:44 -04:00
|
|
|
expect { go }.to change { Clusters::Cluster.count }
|
2018-09-07 09:32:31 -04:00
|
|
|
.and change { Clusters::Providers::Gcp.count }
|
|
|
|
expect(project.clusters.first.provider_gcp).not_to be_legacy_abac
|
|
|
|
end
|
2018-06-12 01:57:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when access token is expired' do
|
|
|
|
before do
|
|
|
|
stub_google_api_expired_token
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(@valid_gcp_token).to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when access token is not stored in session' do
|
|
|
|
it { expect(@valid_gcp_token).to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(described_class)
|
2018-10-14 23:37:51 -04:00
|
|
|
.to receive(:token_in_session).and_return('token')
|
2018-06-12 01:57:04 -04:00
|
|
|
allow_any_instance_of(described_class)
|
2018-10-14 23:37:51 -04:00
|
|
|
.to receive(:expires_at_in_session).and_return(1.hour.since.to_i.to_s)
|
2018-06-12 01:57:04 -04:00
|
|
|
allow_any_instance_of(GoogleApi::CloudPlatform::Client)
|
|
|
|
.to receive(:projects_zones_clusters_create) do
|
|
|
|
OpenStruct.new(
|
|
|
|
self_link: 'projects/gcp-project-12345/zones/us-central1-a/operations/ope-123',
|
|
|
|
status: 'RUNNING'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
allow(WaitForClusterCreationWorker).to receive(:perform_in).and_return(nil)
|
|
|
|
end
|
|
|
|
|
2018-10-16 04:50:44 -04:00
|
|
|
it { expect { go }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { go }.to be_allowed_for(:owner).of(project) }
|
|
|
|
it { expect { go }.to be_allowed_for(:maintainer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:developer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:reporter).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:guest).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:user) }
|
|
|
|
it { expect { go }.to be_denied_for(:external) }
|
2018-06-12 01:57:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST create for existing cluster' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: {
|
|
|
|
name: 'new-cluster',
|
2019-05-02 21:05:53 -04:00
|
|
|
managed: '1',
|
2018-06-12 01:57:04 -04:00
|
|
|
platform_kubernetes_attributes: {
|
|
|
|
api_url: 'http://my-url',
|
|
|
|
token: 'test',
|
|
|
|
namespace: 'aaa'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-10-16 04:50:44 -04:00
|
|
|
def go
|
2018-12-17 17:52:17 -05:00
|
|
|
post :create_user, params: params.merge(namespace_id: project.namespace, project_id: project)
|
2018-10-14 17:24:25 -04:00
|
|
|
end
|
2018-06-12 01:57:04 -04:00
|
|
|
|
2018-10-14 17:24:25 -04:00
|
|
|
describe 'functionality' do
|
2018-06-12 01:57:04 -04:00
|
|
|
context 'when creates a cluster' do
|
|
|
|
it 'creates a new cluster' do
|
|
|
|
expect(ClusterProvisionWorker).to receive(:perform_async)
|
2018-09-06 06:03:38 -04:00
|
|
|
|
2018-10-16 04:50:44 -04:00
|
|
|
expect { go }.to change { Clusters::Cluster.count }
|
2018-06-12 01:57:04 -04:00
|
|
|
.and change { Clusters::Platforms::Kubernetes.count }
|
2018-09-06 06:03:38 -04:00
|
|
|
|
2018-06-12 01:57:04 -04:00
|
|
|
expect(response).to redirect_to(project_cluster_path(project, project.clusters.first))
|
2018-09-06 06:03:38 -04:00
|
|
|
|
|
|
|
expect(project.clusters.first).to be_user
|
|
|
|
expect(project.clusters.first).to be_kubernetes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when creates a RBAC-enabled cluster' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: {
|
|
|
|
name: 'new-cluster',
|
|
|
|
platform_kubernetes_attributes: {
|
|
|
|
api_url: 'http://my-url',
|
|
|
|
token: 'test',
|
|
|
|
namespace: 'aaa',
|
|
|
|
authorization_type: 'rbac'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new cluster' do
|
|
|
|
expect(ClusterProvisionWorker).to receive(:perform_async)
|
|
|
|
|
2018-10-16 04:50:44 -04:00
|
|
|
expect { go }.to change { Clusters::Cluster.count }
|
2018-09-06 06:03:38 -04:00
|
|
|
.and change { Clusters::Platforms::Kubernetes.count }
|
|
|
|
|
|
|
|
expect(response).to redirect_to(project_cluster_path(project, project.clusters.first))
|
|
|
|
|
2019-05-02 21:05:53 -04:00
|
|
|
cluster = project.clusters.first
|
|
|
|
|
|
|
|
expect(cluster).to be_user
|
|
|
|
expect(cluster).to be_kubernetes
|
|
|
|
expect(cluster).to be_platform_kubernetes_rbac
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when creates a user-managed cluster' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: {
|
|
|
|
name: 'new-cluster',
|
|
|
|
managed: '0',
|
|
|
|
platform_kubernetes_attributes: {
|
|
|
|
api_url: 'http://my-url',
|
|
|
|
token: 'test',
|
|
|
|
namespace: 'aaa',
|
|
|
|
authorization_type: 'rbac'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new user-managed cluster' do
|
|
|
|
go
|
|
|
|
cluster = project.clusters.first
|
|
|
|
|
|
|
|
expect(cluster.managed?).to be_falsy
|
2018-06-12 01:57:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
2018-11-02 11:46:15 -04:00
|
|
|
before do
|
|
|
|
stub_kubeclient_get_namespace('https://kubernetes.example.com', namespace: 'my-namespace')
|
|
|
|
end
|
|
|
|
|
2018-10-16 04:50:44 -04:00
|
|
|
it { expect { go }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { go }.to be_allowed_for(:owner).of(project) }
|
|
|
|
it { expect { go }.to be_allowed_for(:maintainer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:developer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:reporter).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:guest).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:user) }
|
|
|
|
it { expect { go }.to be_denied_for(:external) }
|
2018-06-12 01:57:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-31 20:39:01 -04:00
|
|
|
describe 'GET cluster_status' do
|
2017-12-03 16:03:21 -05:00
|
|
|
let(:cluster) { create(:cluster, :providing_by_gcp, projects: [project]) }
|
|
|
|
|
2018-10-16 04:50:44 -04:00
|
|
|
def go
|
2018-10-31 20:39:01 -04:00
|
|
|
get :cluster_status,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
namespace_id: project.namespace.to_param,
|
|
|
|
project_id: project.to_param,
|
|
|
|
id: cluster
|
|
|
|
},
|
2018-10-14 23:37:51 -04:00
|
|
|
format: :json
|
2018-10-14 17:24:25 -04:00
|
|
|
end
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-14 17:24:25 -04:00
|
|
|
describe 'functionality' do
|
2017-12-03 16:03:21 -05:00
|
|
|
it "responds with matching schema" do
|
2018-10-16 04:50:44 -04:00
|
|
|
go
|
2017-12-03 16:03:21 -05:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to match_response_schema('cluster_status')
|
|
|
|
end
|
2018-02-19 21:49:35 -05:00
|
|
|
|
2018-02-22 18:31:19 -05:00
|
|
|
it 'invokes schedule_status_update on each application' do
|
|
|
|
expect_any_instance_of(Clusters::Applications::Ingress).to receive(:schedule_status_update)
|
2018-02-19 21:49:35 -05:00
|
|
|
|
2018-10-16 04:50:44 -04:00
|
|
|
go
|
2018-02-19 21:49:35 -05:00
|
|
|
end
|
2017-12-03 16:03:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
2018-10-16 04:50:44 -04:00
|
|
|
it { expect { go }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { go }.to be_allowed_for(:owner).of(project) }
|
|
|
|
it { expect { go }.to be_allowed_for(:maintainer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:developer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:reporter).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:guest).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:user) }
|
|
|
|
it { expect { go }.to be_denied_for(:external) }
|
2017-12-03 16:03:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET show' do
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
|
|
|
|
|
2018-10-16 04:50:44 -04:00
|
|
|
def go
|
2018-10-14 23:37:51 -04:00
|
|
|
get :show,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
id: cluster
|
|
|
|
}
|
2018-10-14 17:24:25 -04:00
|
|
|
end
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-14 17:24:25 -04:00
|
|
|
describe 'functionality' do
|
2017-12-03 16:03:21 -05:00
|
|
|
it "renders view" do
|
2018-10-16 04:50:44 -04:00
|
|
|
go
|
2017-12-03 16:03:21 -05:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(assigns(:cluster)).to eq(cluster)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
2018-10-16 04:50:44 -04:00
|
|
|
it { expect { go }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { go }.to be_allowed_for(:owner).of(project) }
|
|
|
|
it { expect { go }.to be_allowed_for(:maintainer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:developer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:reporter).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:guest).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:user) }
|
|
|
|
it { expect { go }.to be_denied_for(:external) }
|
2017-12-03 16:03:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PUT update' do
|
2018-10-16 04:50:44 -04:00
|
|
|
def go(format: :html)
|
2018-12-17 17:52:17 -05:00
|
|
|
put :update, params: params.merge(namespace_id: project.namespace.to_param,
|
|
|
|
project_id: project.to_param,
|
|
|
|
id: cluster,
|
|
|
|
format: format
|
|
|
|
)
|
2018-10-14 17:24:25 -04:00
|
|
|
end
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-11-02 11:46:15 -04:00
|
|
|
before do
|
|
|
|
stub_kubeclient_get_namespace('https://kubernetes.example.com', namespace: 'my-namespace')
|
|
|
|
end
|
|
|
|
|
2018-10-22 23:51:29 -04:00
|
|
|
let(:cluster) { create(:cluster, :provided_by_user, projects: [project]) }
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-22 23:51:29 -04:00
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: {
|
|
|
|
enabled: false,
|
|
|
|
name: 'my-new-cluster-name',
|
2019-06-06 21:24:59 -04:00
|
|
|
managed: false,
|
2018-10-22 23:51:29 -04:00
|
|
|
platform_kubernetes_attributes: {
|
|
|
|
namespace: 'my-namespace'
|
2018-10-14 17:24:25 -04:00
|
|
|
}
|
|
|
|
}
|
2018-10-22 23:51:29 -04:00
|
|
|
}
|
|
|
|
end
|
2018-10-14 17:24:25 -04:00
|
|
|
|
2018-10-22 23:51:29 -04:00
|
|
|
it "updates and redirects back to show page" do
|
|
|
|
go
|
2018-10-14 17:24:25 -04:00
|
|
|
|
2018-10-22 23:51:29 -04:00
|
|
|
cluster.reload
|
|
|
|
expect(response).to redirect_to(project_cluster_path(project, cluster))
|
|
|
|
expect(flash[:notice]).to eq('Kubernetes cluster was successfully updated.')
|
|
|
|
expect(cluster.enabled).to be_falsey
|
|
|
|
expect(cluster.name).to eq('my-new-cluster-name')
|
2019-06-06 21:24:59 -04:00
|
|
|
expect(cluster).not_to be_managed
|
2018-10-22 23:51:29 -04:00
|
|
|
expect(cluster.platform_kubernetes.namespace).to eq('my-namespace')
|
|
|
|
end
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-22 23:51:29 -04:00
|
|
|
context 'when format is json' do
|
|
|
|
context 'when changing parameters' do
|
|
|
|
context 'when valid parameters are used' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: {
|
|
|
|
enabled: false,
|
|
|
|
name: 'my-new-cluster-name',
|
2019-06-06 21:24:59 -04:00
|
|
|
managed: false,
|
2018-10-22 23:51:29 -04:00
|
|
|
platform_kubernetes_attributes: {
|
|
|
|
namespace: 'my-namespace'
|
2017-12-03 16:03:21 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-22 23:51:29 -04:00
|
|
|
}
|
|
|
|
end
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-22 23:51:29 -04:00
|
|
|
it "updates and redirects back to show page" do
|
|
|
|
go(format: :json)
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-22 23:51:29 -04:00
|
|
|
cluster.reload
|
|
|
|
expect(response).to have_http_status(:no_content)
|
|
|
|
expect(cluster.enabled).to be_falsey
|
|
|
|
expect(cluster.name).to eq('my-new-cluster-name')
|
2019-06-06 21:24:59 -04:00
|
|
|
expect(cluster).not_to be_managed
|
2018-10-22 23:51:29 -04:00
|
|
|
expect(cluster.platform_kubernetes.namespace).to eq('my-namespace')
|
2017-12-03 16:03:21 -05:00
|
|
|
end
|
2018-10-22 23:51:29 -04:00
|
|
|
end
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-22 23:51:29 -04:00
|
|
|
context 'when invalid parameters are used' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: {
|
|
|
|
enabled: false,
|
|
|
|
platform_kubernetes_attributes: {
|
|
|
|
namespace: 'my invalid namespace #@'
|
2017-12-03 16:55:32 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-22 23:51:29 -04:00
|
|
|
}
|
|
|
|
end
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-22 23:51:29 -04:00
|
|
|
it "rejects changes" do
|
|
|
|
go(format: :json)
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-22 23:51:29 -04:00
|
|
|
expect(response).to have_http_status(:bad_request)
|
2017-12-03 16:03:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
2017-12-05 04:26:12 -05:00
|
|
|
set(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-16 04:50:44 -04:00
|
|
|
it { expect { go }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { go }.to be_allowed_for(:owner).of(project) }
|
|
|
|
it { expect { go }.to be_allowed_for(:maintainer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:developer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:reporter).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:guest).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:user) }
|
|
|
|
it { expect { go }.to be_denied_for(:external) }
|
2017-12-03 16:03:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE destroy' do
|
2018-10-14 17:24:25 -04:00
|
|
|
let!(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, projects: [project]) }
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-16 04:50:44 -04:00
|
|
|
def go
|
2018-10-14 23:37:51 -04:00
|
|
|
delete :destroy,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
id: cluster
|
|
|
|
}
|
2018-10-14 17:24:25 -04:00
|
|
|
end
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-14 17:24:25 -04:00
|
|
|
describe 'functionality' do
|
2017-12-05 04:26:12 -05:00
|
|
|
context 'when cluster is provided by GCP' do
|
2017-12-03 16:03:21 -05:00
|
|
|
context 'when cluster is created' do
|
|
|
|
it "destroys and redirects back to clusters list" do
|
2018-10-16 04:50:44 -04:00
|
|
|
expect { go }
|
2017-12-03 16:03:21 -05:00
|
|
|
.to change { Clusters::Cluster.count }.by(-1)
|
|
|
|
.and change { Clusters::Platforms::Kubernetes.count }.by(-1)
|
|
|
|
.and change { Clusters::Providers::Gcp.count }.by(-1)
|
|
|
|
|
2017-12-04 03:12:04 -05:00
|
|
|
expect(response).to redirect_to(project_clusters_path(project))
|
2018-01-29 14:49:00 -05:00
|
|
|
expect(flash[:notice]).to eq('Kubernetes cluster integration was successfully removed.')
|
2017-12-03 16:03:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when cluster is being created' do
|
2018-05-22 08:40:47 -04:00
|
|
|
let!(:cluster) { create(:cluster, :providing_by_gcp, :production_environment, projects: [project]) }
|
2017-12-03 16:03:21 -05:00
|
|
|
|
|
|
|
it "destroys and redirects back to clusters list" do
|
2018-10-16 04:50:44 -04:00
|
|
|
expect { go }
|
2017-12-03 16:03:21 -05:00
|
|
|
.to change { Clusters::Cluster.count }.by(-1)
|
|
|
|
.and change { Clusters::Providers::Gcp.count }.by(-1)
|
|
|
|
|
2017-12-04 03:12:04 -05:00
|
|
|
expect(response).to redirect_to(project_clusters_path(project))
|
2018-01-29 14:49:00 -05:00
|
|
|
expect(flash[:notice]).to eq('Kubernetes cluster integration was successfully removed.')
|
2017-12-03 16:03:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-05 04:26:12 -05:00
|
|
|
context 'when cluster is provided by user' do
|
2018-05-22 08:40:47 -04:00
|
|
|
let!(:cluster) { create(:cluster, :provided_by_user, :production_environment, projects: [project]) }
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2017-12-05 04:26:12 -05:00
|
|
|
it "destroys and redirects back to clusters list" do
|
2018-10-16 04:50:44 -04:00
|
|
|
expect { go }
|
2017-12-05 04:26:12 -05:00
|
|
|
.to change { Clusters::Cluster.count }.by(-1)
|
|
|
|
.and change { Clusters::Platforms::Kubernetes.count }.by(-1)
|
|
|
|
.and change { Clusters::Providers::Gcp.count }.by(0)
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2017-12-05 04:26:12 -05:00
|
|
|
expect(response).to redirect_to(project_clusters_path(project))
|
2018-01-29 14:49:00 -05:00
|
|
|
expect(flash[:notice]).to eq('Kubernetes cluster integration was successfully removed.')
|
2017-12-03 16:03:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
2018-05-22 08:40:47 -04:00
|
|
|
set(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, projects: [project]) }
|
2017-12-03 16:03:21 -05:00
|
|
|
|
2018-10-16 04:50:44 -04:00
|
|
|
it { expect { go }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { go }.to be_allowed_for(:owner).of(project) }
|
|
|
|
it { expect { go }.to be_allowed_for(:maintainer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:developer).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:reporter).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:guest).of(project) }
|
|
|
|
it { expect { go }.to be_denied_for(:user) }
|
|
|
|
it { expect { go }.to be_denied_for(:external) }
|
2017-12-03 16:03:21 -05:00
|
|
|
end
|
|
|
|
end
|
2018-10-14 23:37:51 -04:00
|
|
|
|
|
|
|
context 'no project_id param' do
|
|
|
|
it 'does not respond to any action without project_id param' do
|
|
|
|
expect { get :index }.to raise_error(ActionController::UrlGenerationError)
|
|
|
|
end
|
|
|
|
end
|
2017-12-03 16:03:21 -05:00
|
|
|
end
|