2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Clusters::Gcp::FinalizeCreationService, '#execute' do
|
2018-03-17 18:26:18 +05:30
|
|
|
include GoogleApi::CloudPlatformHelpers
|
|
|
|
include KubernetesHelpers
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
let(:cluster) { create(:cluster, :project, :providing_by_gcp) }
|
|
|
|
let(:provider) { cluster.provider }
|
|
|
|
let(:platform) { cluster.platform }
|
|
|
|
let(:endpoint) { '111.111.111.111' }
|
|
|
|
let(:api_url) { 'https://' + endpoint }
|
|
|
|
let(:secret_name) { 'gitlab-token' }
|
|
|
|
let(:token) { 'sample-token' }
|
|
|
|
let(:namespace) { "#{cluster.project.path}-#{cluster.project.id}" }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
subject { described_class.new.execute(provider) }
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
shared_examples 'success' do
|
|
|
|
it 'configures provider and kubernetes' do
|
|
|
|
subject
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(provider).to be_created
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it 'properly configures database models' do
|
|
|
|
subject
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
cluster.reload
|
|
|
|
|
|
|
|
expect(provider.endpoint).to eq(endpoint)
|
|
|
|
expect(platform.api_url).to eq(api_url)
|
|
|
|
expect(platform.ca_cert).to eq(Base64.decode64(load_sample_cert).strip)
|
|
|
|
expect(platform.token).to eq(token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'error' do
|
|
|
|
it 'sets an error to provider object' do
|
|
|
|
subject
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(provider.reload).to be_errored
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
shared_examples 'kubernetes information not successfully fetched' do
|
|
|
|
context 'when failed to fetch gke cluster info' do
|
2018-03-17 18:26:18 +05:30
|
|
|
before do
|
2018-12-13 13:39:08 +05:30
|
|
|
stub_cloud_platform_get_zone_cluster_error(provider.gcp_project_id, provider.zone, cluster.name)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it_behaves_like 'error'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when token is empty' do
|
|
|
|
let(:token) { '' }
|
|
|
|
|
|
|
|
it_behaves_like 'error'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when failed to fetch kubernetes token' do
|
|
|
|
before do
|
|
|
|
stub_kubeclient_get_secret_error(api_url, secret_name, namespace: 'default')
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
it_behaves_like 'error'
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
context 'when service account fails to create' do
|
2018-03-17 18:26:18 +05:30
|
|
|
before do
|
2018-12-13 13:39:08 +05:30
|
|
|
stub_kubeclient_create_service_account_error(api_url, namespace: 'default')
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'error'
|
|
|
|
end
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
shared_context 'kubernetes information successfully fetched' do
|
|
|
|
before do
|
|
|
|
stub_cloud_platform_get_zone_cluster(
|
2021-09-04 01:27:46 +05:30
|
|
|
provider.gcp_project_id, provider.zone, cluster.name, { endpoint: endpoint }
|
2018-12-13 13:39:08 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
stub_kubeclient_discover(api_url)
|
|
|
|
stub_kubeclient_get_namespace(api_url)
|
|
|
|
stub_kubeclient_create_namespace(api_url)
|
2019-02-15 15:39:39 +05:30
|
|
|
stub_kubeclient_get_service_account_error(api_url, 'gitlab')
|
2018-12-13 13:39:08 +05:30
|
|
|
stub_kubeclient_create_service_account(api_url)
|
|
|
|
stub_kubeclient_create_secret(api_url)
|
2019-02-15 15:39:39 +05:30
|
|
|
stub_kubeclient_put_secret(api_url, 'gitlab-token')
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
stub_kubeclient_get_secret(
|
|
|
|
api_url,
|
2021-01-03 14:25:43 +05:30
|
|
|
metadata_name: secret_name,
|
|
|
|
token: Base64.encode64(token),
|
|
|
|
namespace: 'default'
|
2018-12-13 13:39:08 +05:30
|
|
|
)
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
stub_kubeclient_put_cluster_role_binding(api_url, 'gitlab-admin')
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'With a legacy ABAC cluster' do
|
|
|
|
before do
|
|
|
|
provider.legacy_abac = true
|
|
|
|
end
|
|
|
|
|
|
|
|
include_context 'kubernetes information successfully fetched'
|
|
|
|
|
|
|
|
it_behaves_like 'success'
|
|
|
|
|
|
|
|
it 'uses ABAC authorization type' do
|
|
|
|
subject
|
|
|
|
cluster.reload
|
|
|
|
|
|
|
|
expect(platform).to be_abac
|
|
|
|
expect(platform.authorization_type).to eq('abac')
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'kubernetes information not successfully fetched'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'With an RBAC cluster' do
|
|
|
|
before do
|
|
|
|
provider.legacy_abac = false
|
|
|
|
end
|
|
|
|
|
|
|
|
include_context 'kubernetes information successfully fetched'
|
|
|
|
|
|
|
|
it_behaves_like 'success'
|
|
|
|
|
|
|
|
it 'uses RBAC authorization type' do
|
|
|
|
subject
|
|
|
|
cluster.reload
|
|
|
|
|
|
|
|
expect(platform).to be_rbac
|
|
|
|
expect(platform.authorization_type).to eq('rbac')
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'kubernetes information not successfully fetched'
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
context 'With a Cloud Run cluster' do
|
|
|
|
before do
|
|
|
|
provider.cloud_run = true
|
|
|
|
end
|
|
|
|
|
|
|
|
include_context 'kubernetes information successfully fetched'
|
|
|
|
|
|
|
|
it_behaves_like 'success'
|
|
|
|
|
|
|
|
it 'has knative pre-installed' do
|
|
|
|
subject
|
|
|
|
cluster.reload
|
|
|
|
|
|
|
|
expect(cluster.application_knative).to be_present
|
|
|
|
expect(cluster.application_knative).to be_pre_installed
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|