2019-04-09 22:13:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 23:08:05 -04:00
|
|
|
RSpec.describe Admin::ClustersController do
|
2019-04-09 22:13:43 -04:00
|
|
|
include AccessMatchersForController
|
|
|
|
include GoogleApi::CloudPlatformHelpers
|
|
|
|
|
|
|
|
let(:admin) { create(:admin) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
2019-05-02 17:30:53 -04:00
|
|
|
describe 'GET #index' do
|
2019-05-01 21:07:58 -04:00
|
|
|
def get_index(params = {})
|
2019-04-09 22:13:43 -04:00
|
|
|
get :index, params: params
|
|
|
|
end
|
|
|
|
|
2019-06-27 08:29:19 -04:00
|
|
|
describe 'functionality' do
|
|
|
|
context 'when instance has one or more clusters' do
|
|
|
|
let!(:enabled_cluster) do
|
|
|
|
create(:cluster, :provided_by_gcp, :instance)
|
|
|
|
end
|
2019-04-09 22:13:43 -04:00
|
|
|
|
2019-06-27 08:29:19 -04:00
|
|
|
let!(:disabled_cluster) do
|
|
|
|
create(:cluster, :disabled, :provided_by_gcp, :production_environment, :instance)
|
|
|
|
end
|
2019-04-09 22:13:43 -04:00
|
|
|
|
2020-04-30 17:09:47 -04:00
|
|
|
it 'lists available clusters and displays html' do
|
2019-06-27 08:29:19 -04:00
|
|
|
get_index
|
2019-04-09 22:13:43 -04:00
|
|
|
|
2019-06-27 08:29:19 -04: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
|
2019-04-09 22:13:43 -04:00
|
|
|
|
2020-04-30 17:09:47 -04:00
|
|
|
it 'lists available clusters and renders json serializer' do
|
|
|
|
get_index(format: :json)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to match_response_schema('cluster_list')
|
|
|
|
end
|
|
|
|
|
2020-05-27 11:08:11 -04:00
|
|
|
it 'sets the polling interval header for json requests' do
|
|
|
|
get_index(format: :json)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.headers['Poll-Interval']).to eq("10000")
|
|
|
|
end
|
|
|
|
|
2019-06-27 08:29:19 -04:00
|
|
|
context 'when page is specified' do
|
|
|
|
let(:last_page) { Clusters::Cluster.instance_type.page.total_pages }
|
2020-04-30 17:09:47 -04:00
|
|
|
let(:total_count) { Clusters::Cluster.instance_type.page.total_count }
|
2019-04-09 22:13:43 -04:00
|
|
|
|
2019-06-27 08:29:19 -04:00
|
|
|
before do
|
2020-04-30 17:09:47 -04:00
|
|
|
create_list(:cluster, 30, :provided_by_gcp, :production_environment, :instance)
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
|
2019-06-27 08:29:19 -04:00
|
|
|
it 'redirects to the page' do
|
2020-04-30 17:09:47 -04:00
|
|
|
expect(last_page).to be > 1
|
|
|
|
|
2019-06-27 08:29:19 -04:00
|
|
|
get_index(page: last_page)
|
2019-04-09 22:13:43 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-06-27 08:29:19 -04:00
|
|
|
expect(assigns(:clusters).current_page).to eq(last_page)
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
2020-04-30 17:09:47 -04:00
|
|
|
|
|
|
|
it 'displays cluster list for associated page' do
|
|
|
|
expect(last_page).to be > 1
|
|
|
|
|
|
|
|
get_index(page: last_page, format: :json)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.headers['X-Page'].to_i).to eq(last_page)
|
|
|
|
expect(response.headers['X-Total'].to_i).to eq(total_count)
|
|
|
|
end
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
2019-06-27 08:29:19 -04:00
|
|
|
end
|
2019-04-09 22:13:43 -04:00
|
|
|
|
2019-06-27 08:29:19 -04:00
|
|
|
context 'when instance does not have a cluster' do
|
|
|
|
it 'returns an empty state page' do
|
|
|
|
get_index
|
2019-04-09 22:13:43 -04:00
|
|
|
|
2019-06-27 08:29:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to render_template(:index, partial: :empty_state)
|
|
|
|
expect(assigns(:clusters)).to eq([])
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_gcp, :instance) }
|
|
|
|
|
2019-05-01 21:07:58 -04:00
|
|
|
it { expect { get_index }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { get_index }.to be_denied_for(:user) }
|
|
|
|
it { expect { get_index }.to be_denied_for(:external) }
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-02 17:30:53 -04:00
|
|
|
describe 'GET #new' do
|
2020-09-02 11:10:54 -04:00
|
|
|
let(:user) { admin }
|
|
|
|
|
|
|
|
def go(provider: 'gcp')
|
2019-09-09 16:27:51 -04:00
|
|
|
get :new, params: { provider: provider }
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'functionality for new cluster' do
|
|
|
|
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
|
|
|
|
|
2019-11-26 19:06:23 -05:00
|
|
|
context 'when selected provider is gke and no valid gcp token exists' do
|
|
|
|
it 'redirects to gcp authorize_url' do
|
2020-09-02 11:10:54 -04:00
|
|
|
go
|
2019-09-09 16:27:51 -04:00
|
|
|
|
2019-11-26 19:06:23 -05:00
|
|
|
expect(response).to redirect_to(assigns(:authorize_url))
|
2019-09-09 16:27:51 -04:00
|
|
|
end
|
|
|
|
end
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when omniauth has not configured' do
|
|
|
|
before do
|
|
|
|
stub_omniauth_setting(providers: [])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not have authorize_url' do
|
2020-09-02 11:10:54 -04:00
|
|
|
go
|
2019-04-09 22:13:43 -04:00
|
|
|
|
|
|
|
expect(assigns(:authorize_url)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when access token is valid' do
|
|
|
|
before do
|
|
|
|
stub_google_api_validate_token
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has new object' do
|
2020-09-02 11:10:54 -04:00
|
|
|
go
|
2019-04-09 22:13:43 -04:00
|
|
|
|
|
|
|
expect(assigns(:gcp_cluster)).to be_an_instance_of(Clusters::ClusterPresenter)
|
|
|
|
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 'functionality for existing cluster' do
|
|
|
|
it 'has new object' do
|
2020-09-02 11:10:54 -04:00
|
|
|
go
|
2019-04-09 22:13:43 -04:00
|
|
|
|
|
|
|
expect(assigns(:user_cluster)).to be_an_instance_of(Clusters::ClusterPresenter)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
include_examples 'GET new cluster shared examples'
|
|
|
|
|
2019-04-09 22:13:43 -04:00
|
|
|
describe 'security' do
|
2020-09-02 11:10:54 -04:00
|
|
|
it { expect { go }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { go }.to be_denied_for(:user) }
|
|
|
|
it { expect { go }.to be_denied_for(:external) }
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
it_behaves_like 'GET #metrics_dashboard for dashboard', 'Cluster health' do
|
|
|
|
let(:cluster) { create(:cluster, :instance, :provided_by_gcp) }
|
|
|
|
|
|
|
|
let(:metrics_dashboard_req_params) do
|
|
|
|
{
|
|
|
|
id: cluster.id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-07 02:09:06 -04:00
|
|
|
describe 'GET #prometheus_proxy' do
|
|
|
|
let(:user) { admin }
|
|
|
|
let(:proxyable) do
|
|
|
|
create(:cluster, :instance, :provided_by_gcp)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'metrics dashboard prometheus api proxy' do
|
|
|
|
context 'with anonymous user' do
|
|
|
|
let(:prometheus_body) { nil }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_out(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 404' do
|
|
|
|
get :prometheus_proxy, params: prometheus_proxy_params
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-02 17:30:53 -04:00
|
|
|
describe 'POST #create_gcp' do
|
2019-04-09 22:13:43 -04:00
|
|
|
let(:legacy_abac_param) { 'true' }
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: {
|
|
|
|
name: 'new-cluster',
|
|
|
|
provider_gcp_attributes: {
|
|
|
|
gcp_project_id: 'gcp-project-12345',
|
|
|
|
legacy_abac: legacy_abac_param
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-05-01 21:07:58 -04:00
|
|
|
def post_create_gcp
|
2019-04-09 22:13:43 -04:00
|
|
|
post :create_gcp, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'functionality' do
|
|
|
|
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)
|
2019-05-01 21:07:58 -04:00
|
|
|
expect { post_create_gcp }.to change { Clusters::Cluster.count }
|
2019-04-09 22:13:43 -04:00
|
|
|
.and change { Clusters::Providers::Gcp.count }
|
|
|
|
|
|
|
|
cluster = Clusters::Cluster.instance_type.first
|
|
|
|
|
|
|
|
expect(response).to redirect_to(admin_cluster_path(cluster))
|
|
|
|
expect(cluster).to be_gcp
|
|
|
|
expect(cluster).to be_kubernetes
|
|
|
|
expect(cluster.provider_gcp).to be_legacy_abac
|
|
|
|
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)
|
2019-05-01 21:07:58 -04:00
|
|
|
expect { post_create_gcp }.to change { Clusters::Cluster.count }
|
2019-04-09 22:13:43 -04:00
|
|
|
.and change { Clusters::Providers::Gcp.count }
|
|
|
|
expect(Clusters::Cluster.instance_type.first.provider_gcp).not_to be_legacy_abac
|
|
|
|
end
|
|
|
|
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
|
2019-11-13 22:06:25 -05:00
|
|
|
allow_next_instance_of(described_class) do |instance|
|
|
|
|
allow(instance).to receive(:token_in_session).and_return('token')
|
|
|
|
allow(instance).to receive(:expires_at_in_session).and_return(1.hour.since.to_i.to_s)
|
|
|
|
end
|
|
|
|
allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |instance|
|
|
|
|
allow(instance).to receive(:projects_zones_clusters_create) do
|
2021-12-02 07:10:10 -05:00
|
|
|
double(
|
|
|
|
'instance',
|
2019-11-13 22:06:25 -05:00
|
|
|
self_link: 'projects/gcp-project-12345/zones/us-central1-a/operations/ope-123',
|
|
|
|
status: 'RUNNING'
|
|
|
|
)
|
|
|
|
end
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
allow(WaitForClusterCreationWorker).to receive(:perform_in).and_return(nil)
|
|
|
|
end
|
|
|
|
|
2019-05-01 21:07:58 -04:00
|
|
|
it { expect { post_create_gcp }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { post_create_gcp }.to be_denied_for(:user) }
|
|
|
|
it { expect { post_create_gcp }.to be_denied_for(:external) }
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-13 01:06:38 -05:00
|
|
|
describe 'POST #create_aws' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: {
|
|
|
|
name: 'new-cluster',
|
|
|
|
provider_aws_attributes: {
|
|
|
|
key_name: 'key',
|
|
|
|
role_arn: 'arn:role',
|
|
|
|
region: 'region',
|
|
|
|
vpc_id: 'vpc',
|
|
|
|
instance_type: 'instance type',
|
|
|
|
num_nodes: 3,
|
|
|
|
security_group_id: 'security group',
|
|
|
|
subnet_ids: %w(subnet1 subnet2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_create_aws
|
|
|
|
post :create_aws, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new cluster' do
|
|
|
|
expect(ClusterProvisionWorker).to receive(:perform_async)
|
|
|
|
expect { post_create_aws }.to change { Clusters::Cluster.count }
|
|
|
|
.and change { Clusters::Providers::Aws.count }
|
|
|
|
|
|
|
|
cluster = Clusters::Cluster.instance_type.first
|
|
|
|
|
2020-03-31 17:08:05 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2019-11-13 01:06:38 -05:00
|
|
|
expect(response.location).to eq(admin_cluster_path(cluster))
|
|
|
|
expect(cluster).to be_aws
|
|
|
|
expect(cluster).to be_kubernetes
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'params are invalid' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: { name: '' }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a cluster' do
|
|
|
|
expect { post_create_aws }.not_to change { Clusters::Cluster.count }
|
|
|
|
|
2020-03-31 17:08:05 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
2020-12-02 10:09:37 -05:00
|
|
|
expect(response.media_type).to eq('application/json')
|
2019-11-13 01:06:38 -05:00
|
|
|
expect(response.body).to include('is invalid')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
|
|
|
before do
|
|
|
|
allow(WaitForClusterCreationWorker).to receive(:perform_in)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect { post_create_aws }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { post_create_aws }.to be_denied_for(:user) }
|
|
|
|
it { expect { post_create_aws }.to be_denied_for(:external) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-02 17:30:53 -04:00
|
|
|
describe 'POST #create_user' do
|
2019-04-09 22:13:43 -04:00
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: {
|
|
|
|
name: 'new-cluster',
|
|
|
|
platform_kubernetes_attributes: {
|
|
|
|
api_url: 'http://my-url',
|
|
|
|
token: 'test'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-05-01 21:07:58 -04:00
|
|
|
def post_create_user
|
2019-04-09 22:13:43 -04:00
|
|
|
post :create_user, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'functionality' do
|
|
|
|
context 'when creates a cluster' do
|
|
|
|
it 'creates a new cluster' do
|
|
|
|
expect(ClusterProvisionWorker).to receive(:perform_async)
|
|
|
|
|
2019-05-01 21:07:58 -04:00
|
|
|
expect { post_create_user }.to change { Clusters::Cluster.count }
|
2019-04-09 22:13:43 -04:00
|
|
|
.and change { Clusters::Platforms::Kubernetes.count }
|
|
|
|
|
|
|
|
cluster = Clusters::Cluster.instance_type.first
|
|
|
|
|
|
|
|
expect(response).to redirect_to(admin_cluster_path(cluster))
|
|
|
|
expect(cluster).to be_user
|
|
|
|
expect(cluster).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',
|
|
|
|
authorization_type: 'rbac'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new cluster' do
|
|
|
|
expect(ClusterProvisionWorker).to receive(:perform_async)
|
|
|
|
|
2019-05-01 21:07:58 -04:00
|
|
|
expect { post_create_user }.to change { Clusters::Cluster.count }
|
2019-04-09 22:13:43 -04:00
|
|
|
.and change { Clusters::Platforms::Kubernetes.count }
|
|
|
|
|
|
|
|
cluster = Clusters::Cluster.instance_type.first
|
|
|
|
|
|
|
|
expect(response).to redirect_to(admin_cluster_path(cluster))
|
|
|
|
expect(cluster).to be_user
|
|
|
|
expect(cluster).to be_kubernetes
|
|
|
|
expect(cluster).to be_platform_kubernetes_rbac
|
2020-09-29 14:09:52 -04:00
|
|
|
expect(cluster).to be_namespace_per_environment
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
2019-05-01 21:07:58 -04:00
|
|
|
it { expect { post_create_user }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { post_create_user }.to be_denied_for(:user) }
|
|
|
|
it { expect { post_create_user }.to be_denied_for(:external) }
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-08 01:06:24 -05:00
|
|
|
describe 'POST authorize AWS role for EKS cluster' do
|
2020-09-02 11:10:54 -04:00
|
|
|
let!(:role) { create(:aws_role, user: admin) }
|
2019-11-08 01:06:24 -05:00
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
let(:role_arn) { 'arn:new-role' }
|
2019-11-08 01:06:24 -05:00
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: {
|
2020-09-02 11:10:54 -04:00
|
|
|
role_arn: role_arn
|
2019-11-08 01:06:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def go
|
|
|
|
post :authorize_aws_role, params: params
|
|
|
|
end
|
|
|
|
|
2019-12-11 19:07:43 -05:00
|
|
|
before do
|
|
|
|
allow(Clusters::Aws::FetchCredentialsService).to receive(:new)
|
|
|
|
.and_return(double(execute: double))
|
|
|
|
end
|
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
it 'updates the associated role with the supplied ARN' do
|
|
|
|
go
|
2019-11-08 01:06:24 -05:00
|
|
|
|
2020-03-31 17:08:05 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-09-02 11:10:54 -04:00
|
|
|
expect(role.reload.role_arn).to eq(role_arn)
|
2019-11-08 01:06:24 -05:00
|
|
|
end
|
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
context 'supplied role is invalid' do
|
2019-11-08 01:06:24 -05:00
|
|
|
let(:role_arn) { 'invalid-role' }
|
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
it 'does not update the associated role' do
|
|
|
|
expect { go }.not_to change { role.role_arn }
|
2019-11-08 01:06:24 -05:00
|
|
|
|
2020-03-31 17:08:05 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
2019-11-08 01:06:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
2020-09-02 11:10:54 -04:00
|
|
|
before do
|
|
|
|
allow_next_instance_of(Clusters::Aws::AuthorizeRoleService) do |service|
|
|
|
|
response = double(status: :ok, body: double)
|
|
|
|
|
|
|
|
allow(service).to receive(:execute).and_return(response)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-08 01:06:24 -05:00
|
|
|
it { expect { go }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { go }.to be_denied_for(:user) }
|
|
|
|
it { expect { go }.to be_denied_for(:external) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-26 04:08:36 -05:00
|
|
|
describe 'DELETE clear cluster cache' do
|
|
|
|
let(:cluster) { create(:cluster, :instance) }
|
|
|
|
let!(:kubernetes_namespace) do
|
|
|
|
create(:cluster_kubernetes_namespace,
|
|
|
|
cluster: cluster,
|
|
|
|
project: create(:project)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def go
|
|
|
|
delete :clear_cache, params: { id: cluster }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes the namespaces associated with the cluster' do
|
|
|
|
expect { go }.to change { Clusters::KubernetesNamespace.count }
|
|
|
|
|
|
|
|
expect(response).to redirect_to(admin_cluster_path(cluster))
|
|
|
|
expect(cluster.kubernetes_namespaces).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
|
|
|
it { expect { go }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { go }.to be_denied_for(:user) }
|
|
|
|
it { expect { go }.to be_denied_for(:external) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-02 17:30:53 -04:00
|
|
|
describe 'GET #cluster_status' do
|
2019-04-09 22:13:43 -04:00
|
|
|
let(:cluster) { create(:cluster, :providing_by_gcp, :instance) }
|
|
|
|
|
2019-05-01 21:07:58 -04:00
|
|
|
def get_cluster_status
|
2019-04-09 22:13:43 -04:00
|
|
|
get :cluster_status,
|
|
|
|
params: {
|
|
|
|
id: cluster
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'functionality' do
|
|
|
|
it 'responds with matching schema' do
|
2019-05-01 21:07:58 -04:00
|
|
|
get_cluster_status
|
2019-04-09 22:13:43 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to match_response_schema('cluster_status')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'invokes schedule_status_update on each application' do
|
2019-11-13 22:06:25 -05:00
|
|
|
expect_next_instance_of(Clusters::Applications::Ingress) do |instance|
|
|
|
|
expect(instance).to receive(:schedule_status_update)
|
|
|
|
end
|
2019-04-09 22:13:43 -04:00
|
|
|
|
2019-05-01 21:07:58 -04:00
|
|
|
get_cluster_status
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
2019-05-01 21:07:58 -04:00
|
|
|
it { expect { get_cluster_status }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { get_cluster_status }.to be_denied_for(:user) }
|
|
|
|
it { expect { get_cluster_status }.to be_denied_for(:external) }
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-02 17:30:53 -04:00
|
|
|
describe 'GET #show' do
|
2019-04-09 22:13:43 -04:00
|
|
|
let(:cluster) { create(:cluster, :provided_by_gcp, :instance) }
|
|
|
|
|
2021-04-06 20:09:26 -04:00
|
|
|
def get_show(tab: nil)
|
2019-04-09 22:13:43 -04:00
|
|
|
get :show,
|
|
|
|
params: {
|
2021-04-06 20:09:26 -04:00
|
|
|
id: cluster,
|
|
|
|
tab: tab
|
2019-04-09 22:13:43 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'functionality' do
|
2021-04-06 20:09:26 -04:00
|
|
|
render_views
|
|
|
|
|
2019-05-02 17:30:53 -04:00
|
|
|
it 'responds successfully' do
|
2019-05-01 21:07:58 -04:00
|
|
|
get_show
|
2019-04-09 22:13:43 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(assigns(:cluster)).to eq(cluster)
|
|
|
|
end
|
2021-04-06 20:09:26 -04:00
|
|
|
|
|
|
|
it 'renders integration tab view' do
|
|
|
|
get_show(tab: 'integrations')
|
|
|
|
|
|
|
|
expect(response).to render_template('clusters/clusters/_integrations')
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
2019-05-01 21:07:58 -04:00
|
|
|
it { expect { get_show }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { get_show }.to be_denied_for(:user) }
|
|
|
|
it { expect { get_show }.to be_denied_for(:external) }
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-02 17:30:53 -04:00
|
|
|
describe 'PUT #update' do
|
2019-05-01 21:07:58 -04:00
|
|
|
def put_update(format: :html)
|
2019-04-09 22:13:43 -04:00
|
|
|
put :update, params: params.merge(
|
|
|
|
id: cluster,
|
|
|
|
format: format
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_user, :instance) }
|
|
|
|
let(:domain) { 'test-domain.com' }
|
|
|
|
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: {
|
|
|
|
enabled: false,
|
|
|
|
name: 'my-new-cluster-name',
|
2019-06-06 21:24:59 -04:00
|
|
|
managed: false,
|
2020-09-29 14:09:52 -04:00
|
|
|
namespace_per_environment: false,
|
2019-04-09 22:13:43 -04:00
|
|
|
base_domain: domain
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates and redirects back to show page' do
|
2019-05-01 21:07:58 -04:00
|
|
|
put_update
|
2019-04-09 22:13:43 -04:00
|
|
|
|
|
|
|
cluster.reload
|
|
|
|
expect(response).to redirect_to(admin_cluster_path(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
|
2020-09-29 14:09:52 -04:00
|
|
|
expect(cluster).not_to be_namespace_per_environment
|
2019-04-09 22:13:43 -04:00
|
|
|
expect(cluster.domain).to eq('test-domain.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when domain is invalid' do
|
|
|
|
let(:domain) { 'http://not-a-valid-domain' }
|
|
|
|
|
|
|
|
it 'does not update cluster attributes' do
|
2019-05-01 21:07:58 -04:00
|
|
|
put_update
|
2019-04-09 22:13:43 -04:00
|
|
|
|
|
|
|
cluster.reload
|
|
|
|
expect(response).to render_template(:show)
|
|
|
|
expect(cluster.name).not_to eq('my-new-cluster-name')
|
|
|
|
expect(cluster.domain).not_to eq('test-domain.com')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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,
|
2020-09-29 14:09:52 -04:00
|
|
|
namespace_per_environment: false,
|
2019-04-09 22:13:43 -04:00
|
|
|
domain: domain
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates and redirects back to show page' do
|
2019-05-01 21:07:58 -04:00
|
|
|
put_update(format: :json)
|
2019-04-09 22:13:43 -04:00
|
|
|
|
|
|
|
cluster.reload
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2019-04-09 22:13:43 -04:00
|
|
|
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
|
2020-09-29 14:09:52 -04:00
|
|
|
expect(cluster).not_to be_namespace_per_environment
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when invalid parameters are used' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
cluster: {
|
|
|
|
enabled: false,
|
|
|
|
name: ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects changes' do
|
2019-05-01 21:07:58 -04:00
|
|
|
put_update(format: :json)
|
2019-04-09 22:13:43 -04:00
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
2019-10-01 20:06:26 -04:00
|
|
|
let_it_be(:cluster) { create(:cluster, :provided_by_gcp, :instance) }
|
2019-04-09 22:13:43 -04:00
|
|
|
|
2019-05-01 21:07:58 -04:00
|
|
|
it { expect { put_update }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { put_update }.to be_denied_for(:user) }
|
|
|
|
it { expect { put_update }.to be_denied_for(:external) }
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-02 17:30:53 -04:00
|
|
|
describe 'DELETE #destroy' do
|
2019-04-09 22:13:43 -04:00
|
|
|
let!(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, :instance) }
|
|
|
|
|
2019-05-01 21:07:58 -04:00
|
|
|
def delete_destroy
|
2019-04-09 22:13:43 -04:00
|
|
|
delete :destroy,
|
|
|
|
params: {
|
|
|
|
id: cluster
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'functionality' do
|
|
|
|
context 'when cluster is provided by GCP' do
|
|
|
|
context 'when cluster is created' do
|
|
|
|
it 'destroys and redirects back to clusters list' do
|
2019-05-01 21:07:58 -04:00
|
|
|
expect { delete_destroy }
|
2019-04-09 22:13:43 -04:00
|
|
|
.to change { Clusters::Cluster.count }.by(-1)
|
|
|
|
.and change { Clusters::Platforms::Kubernetes.count }.by(-1)
|
|
|
|
.and change { Clusters::Providers::Gcp.count }.by(-1)
|
|
|
|
|
|
|
|
expect(response).to redirect_to(admin_clusters_path)
|
|
|
|
expect(flash[:notice]).to eq('Kubernetes cluster integration was successfully removed.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when cluster is being created' do
|
|
|
|
let!(:cluster) { create(:cluster, :providing_by_gcp, :production_environment, :instance) }
|
|
|
|
|
|
|
|
it 'destroys and redirects back to clusters list' do
|
2019-05-01 21:07:58 -04:00
|
|
|
expect { delete_destroy }
|
2019-04-09 22:13:43 -04:00
|
|
|
.to change { Clusters::Cluster.count }.by(-1)
|
|
|
|
.and change { Clusters::Providers::Gcp.count }.by(-1)
|
|
|
|
|
|
|
|
expect(response).to redirect_to(admin_clusters_path)
|
|
|
|
expect(flash[:notice]).to eq('Kubernetes cluster integration was successfully removed.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when cluster is provided by user' do
|
|
|
|
let!(:cluster) { create(:cluster, :provided_by_user, :production_environment, :instance) }
|
|
|
|
|
|
|
|
it 'destroys and redirects back to clusters list' do
|
2019-05-01 21:07:58 -04:00
|
|
|
expect { delete_destroy }
|
2019-04-09 22:13:43 -04:00
|
|
|
.to change { Clusters::Cluster.count }.by(-1)
|
|
|
|
.and change { Clusters::Platforms::Kubernetes.count }.by(-1)
|
|
|
|
.and change { Clusters::Providers::Gcp.count }.by(0)
|
|
|
|
|
|
|
|
expect(response).to redirect_to(admin_clusters_path)
|
|
|
|
expect(flash[:notice]).to eq('Kubernetes cluster integration was successfully removed.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'security' do
|
2019-10-01 20:06:26 -04:00
|
|
|
let_it_be(:cluster) { create(:cluster, :provided_by_gcp, :production_environment, :instance) }
|
2019-04-09 22:13:43 -04:00
|
|
|
|
2019-05-01 21:07:58 -04:00
|
|
|
it { expect { delete_destroy }.to be_allowed_for(:admin) }
|
|
|
|
it { expect { delete_destroy }.to be_denied_for(:user) }
|
|
|
|
it { expect { delete_destroy }.to be_denied_for(:external) }
|
2019-04-09 22:13:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|