diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/factories/clusters/clusters.rb | 4 | ||||
-rw-r--r-- | spec/factories/clusters/providers/gcp.rb | 4 | ||||
-rw-r--r-- | spec/frontend/clusters/stores/clusters_store_spec.js | 3 | ||||
-rw-r--r-- | spec/initializers/google_api_client_spec.rb | 17 | ||||
-rw-r--r-- | spec/lib/gitlab/utils/inline_hash_spec.rb | 62 | ||||
-rw-r--r-- | spec/lib/gitlab/utils/safe_inline_hash_spec.rb | 35 | ||||
-rw-r--r-- | spec/lib/google_api/cloud_platform/client_spec.rb | 98 | ||||
-rw-r--r-- | spec/models/clusters/applications/knative_spec.rb | 7 | ||||
-rw-r--r-- | spec/models/clusters/cluster_spec.rb | 22 | ||||
-rw-r--r-- | spec/models/clusters/providers/gcp_spec.rb | 16 | ||||
-rw-r--r-- | spec/services/clusters/gcp/finalize_creation_service_spec.rb | 24 | ||||
-rw-r--r-- | spec/support/google_api/cloud_platform_helpers.rb | 2 | ||||
-rw-r--r-- | spec/support/shared_examples/models/cluster_application_status_shared_examples.rb | 14 |
13 files changed, 259 insertions, 49 deletions
diff --git a/spec/factories/clusters/clusters.rb b/spec/factories/clusters/clusters.rb index d294e6d055e..29aea5e403e 100644 --- a/spec/factories/clusters/clusters.rb +++ b/spec/factories/clusters/clusters.rb @@ -58,6 +58,10 @@ FactoryBot.define do platform_kubernetes factory: [:cluster_platform_kubernetes, :configured, :rbac_disabled] end + trait :cloud_run_enabled do + provider_gcp factory: [:cluster_provider_gcp, :created, :cloud_run_enabled] + end + trait :disabled do enabled false end diff --git a/spec/factories/clusters/providers/gcp.rb b/spec/factories/clusters/providers/gcp.rb index 22462651b6a..7fdcdebad34 100644 --- a/spec/factories/clusters/providers/gcp.rb +++ b/spec/factories/clusters/providers/gcp.rb @@ -34,5 +34,9 @@ FactoryBot.define do trait :abac_enabled do legacy_abac true end + + trait :cloud_run_enabled do + cloud_run true + end end end diff --git a/spec/frontend/clusters/stores/clusters_store_spec.js b/spec/frontend/clusters/stores/clusters_store_spec.js index ee3b7d8aa90..5ee06eb44c9 100644 --- a/spec/frontend/clusters/stores/clusters_store_spec.js +++ b/spec/frontend/clusters/stores/clusters_store_spec.js @@ -54,8 +54,11 @@ describe('Clusters Store', () => { environmentsHelpPath: null, clustersHelpPath: null, deployBoardsHelpPath: null, + cloudRunHelpPath: null, status: mockResponseData.status, statusReason: mockResponseData.status_reason, + providerType: null, + preInstalledKnative: false, rbac: false, applications: { helm: { diff --git a/spec/initializers/google_api_client_spec.rb b/spec/initializers/google_api_client_spec.rb new file mode 100644 index 00000000000..44a1bc0836c --- /dev/null +++ b/spec/initializers/google_api_client_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe './config/initializers/google_api_client.rb' do + subject { Google::Apis::ContainerV1beta1 } + + it 'is needed' do |example| + is_expected.not_to be_const_defined(:CloudRunConfig), + <<-MSG.strip_heredoc + The google-api-client gem has been upgraded! + Remove: + #{example.example_group.description} + #{example.file_path} + MSG + end +end diff --git a/spec/lib/gitlab/utils/inline_hash_spec.rb b/spec/lib/gitlab/utils/inline_hash_spec.rb new file mode 100644 index 00000000000..806117eddd0 --- /dev/null +++ b/spec/lib/gitlab/utils/inline_hash_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +describe Gitlab::Utils::InlineHash do + describe '.merge_keys' do + subject { described_class.merge_keys(source) } + + let(:source) do + { + nested_param: { + key: 'Value' + }, + 'root_param' => 'Root', + 'very' => { + 'deep' => { + 'nested' => { + 'param' => 'Deep nested value' + } + } + } + } + end + + it 'transforms a nested hash into a one-level hash' do + is_expected.to eq( + 'nested_param.key' => 'Value', + 'root_param' => 'Root', + 'very.deep.nested.param' => 'Deep nested value' + ) + end + + it 'retains key insertion order' do + expect(subject.keys) + .to eq(%w(nested_param.key root_param very.deep.nested.param)) + end + + context 'with a custom connector' do + subject { described_class.merge_keys(source, connector: '::') } + + it 'uses the connector to merge keys' do + is_expected.to eq( + 'nested_param::key' => 'Value', + 'root_param' => 'Root', + 'very::deep::nested::param' => 'Deep nested value' + ) + end + end + + context 'with a starter prefix' do + subject { described_class.merge_keys(source, prefix: 'options') } + + it 'prefixes all the keys' do + is_expected.to eq( + 'options.nested_param.key' => 'Value', + 'options.root_param' => 'Root', + 'options.very.deep.nested.param' => 'Deep nested value' + ) + end + end + end +end diff --git a/spec/lib/gitlab/utils/safe_inline_hash_spec.rb b/spec/lib/gitlab/utils/safe_inline_hash_spec.rb new file mode 100644 index 00000000000..8d8163f3baa --- /dev/null +++ b/spec/lib/gitlab/utils/safe_inline_hash_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +describe Gitlab::Utils::SafeInlineHash do + context '.merge_keys!' do + let(:source) { { 'foo' => { 'bar' => 'baz' } } } + let(:validator) { instance_double(Gitlab::Utils::DeepSize, valid?: valid) } + + subject { described_class.merge_keys!(source, prefix: 'safe', connector: '::') } + + before do + allow(Gitlab::Utils::DeepSize) + .to receive(:new) + .with(source) + .and_return(validator) + end + + context 'when hash is too big' do + let(:valid) { false } + + it 'raises an exception' do + expect { subject }.to raise_error ArgumentError, 'The Hash is too big' + end + end + + context 'when hash has an acceptaable size' do + let(:valid) { true } + + it 'returns a result of InlineHash' do + is_expected.to eq('safe::foo::bar' => 'baz') + end + end + end +end diff --git a/spec/lib/google_api/cloud_platform/client_spec.rb b/spec/lib/google_api/cloud_platform/client_spec.rb index c24998d32f8..2253feb376d 100644 --- a/spec/lib/google_api/cloud_platform/client_spec.rb +++ b/spec/lib/google_api/cloud_platform/client_spec.rb @@ -68,7 +68,7 @@ describe GoogleApi::CloudPlatform::Client do describe '#projects_zones_clusters_create' do subject do client.projects_zones_clusters_create( - project_id, zone, cluster_name, cluster_size, machine_type: machine_type, legacy_abac: legacy_abac) + project_id, zone, cluster_name, cluster_size, machine_type: machine_type, legacy_abac: legacy_abac, enable_addons: enable_addons) end let(:project_id) { 'project-123' } @@ -77,39 +77,51 @@ describe GoogleApi::CloudPlatform::Client do let(:cluster_size) { 1 } let(:machine_type) { 'n1-standard-2' } let(:legacy_abac) { true } - let(:create_cluster_request_body) { double('Google::Apis::ContainerV1::CreateClusterRequest') } + let(:enable_addons) { [] } + + let(:addons_config) do + enable_addons.each_with_object({}) do |addon, hash| + hash[addon] = { disabled: false } + end + end + + let(:cluster_options) do + { + cluster: { + name: cluster_name, + initial_node_count: cluster_size, + node_config: { + machine_type: machine_type + }, + master_auth: { + username: 'admin', + client_certificate_config: { + issue_client_certificate: true + } + }, + legacy_abac: { + enabled: legacy_abac + }, + addons_config: addons_config + } + } + end + + let(:create_cluster_request_body) { double('Google::Apis::ContainerV1beta1::CreateClusterRequest') } let(:operation) { double } before do - allow_any_instance_of(Google::Apis::ContainerV1::ContainerService) + allow_any_instance_of(Google::Apis::ContainerV1beta1::ContainerService) .to receive(:create_cluster).with(any_args) .and_return(operation) end it 'sets corresponded parameters' do - expect_any_instance_of(Google::Apis::ContainerV1::ContainerService) + expect_any_instance_of(Google::Apis::ContainerV1beta1::ContainerService) .to receive(:create_cluster).with(project_id, zone, create_cluster_request_body, options: user_agent_options) - expect(Google::Apis::ContainerV1::CreateClusterRequest) - .to receive(:new).with( - { - "cluster": { - "name": cluster_name, - "initial_node_count": cluster_size, - "node_config": { - "machine_type": machine_type - }, - "master_auth": { - "username": "admin", - "client_certificate_config": { - issue_client_certificate: true - } - }, - "legacy_abac": { - "enabled": true - } - } - } ).and_return(create_cluster_request_body) + expect(Google::Apis::ContainerV1beta1::CreateClusterRequest) + .to receive(:new).with(cluster_options).and_return(create_cluster_request_body) expect(subject).to eq operation end @@ -118,29 +130,25 @@ describe GoogleApi::CloudPlatform::Client do let(:legacy_abac) { false } it 'sets corresponded parameters' do - expect_any_instance_of(Google::Apis::ContainerV1::ContainerService) + expect_any_instance_of(Google::Apis::ContainerV1beta1::ContainerService) + .to receive(:create_cluster).with(project_id, zone, create_cluster_request_body, options: user_agent_options) + + expect(Google::Apis::ContainerV1beta1::CreateClusterRequest) + .to receive(:new).with(cluster_options).and_return(create_cluster_request_body) + + expect(subject).to eq operation + end + end + + context 'create with enable_addons for cloud_run' do + let(:enable_addons) { [:http_load_balancing, :istio_config, :cloud_run_config] } + + it 'sets corresponded parameters' do + expect_any_instance_of(Google::Apis::ContainerV1beta1::ContainerService) .to receive(:create_cluster).with(project_id, zone, create_cluster_request_body, options: user_agent_options) - expect(Google::Apis::ContainerV1::CreateClusterRequest) - .to receive(:new).with( - { - "cluster": { - "name": cluster_name, - "initial_node_count": cluster_size, - "node_config": { - "machine_type": machine_type - }, - "master_auth": { - "username": "admin", - "client_certificate_config": { - issue_client_certificate: true - } - }, - "legacy_abac": { - "enabled": false - } - } - } ).and_return(create_cluster_request_body) + expect(Google::Apis::ContainerV1beta1::CreateClusterRequest) + .to receive(:new).with(cluster_options).and_return(create_cluster_request_body) expect(subject).to eq operation end diff --git a/spec/models/clusters/applications/knative_spec.rb b/spec/models/clusters/applications/knative_spec.rb index 3825994b733..16247026e34 100644 --- a/spec/models/clusters/applications/knative_spec.rb +++ b/spec/models/clusters/applications/knative_spec.rb @@ -16,6 +16,13 @@ describe Clusters::Applications::Knative do allow(ClusterWaitForIngressIpAddressWorker).to receive(:perform_async) end + describe 'when cloud run is enabled' do + let(:cluster) { create(:cluster, :provided_by_gcp, :cloud_run_enabled) } + let(:knative_cloud_run) { create(:clusters_applications_knative, cluster: cluster) } + + it { expect(knative_cloud_run).to be_not_installable } + end + describe 'when rbac is not enabled' do let(:cluster) { create(:cluster, :provided_by_gcp, :rbac_disabled) } let(:knative_no_rbac) { create(:clusters_applications_knative, cluster: cluster) } diff --git a/spec/models/clusters/cluster_spec.rb b/spec/models/clusters/cluster_spec.rb index b8a6fd6f914..df6263eeed2 100644 --- a/spec/models/clusters/cluster_spec.rb +++ b/spec/models/clusters/cluster_spec.rb @@ -756,4 +756,26 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do end end end + + describe '#knative_pre_installed?' do + subject { cluster.knative_pre_installed? } + + context 'with a GCP provider without cloud_run' do + let(:cluster) { create(:cluster, :provided_by_gcp) } + + it { is_expected.to be_falsey } + end + + context 'with a GCP provider with cloud_run' do + let(:cluster) { create(:cluster, :provided_by_gcp, :cloud_run_enabled) } + + it { is_expected.to be_truthy } + end + + context 'with a user provider' do + let(:cluster) { create(:cluster, :provided_by_user) } + + it { is_expected.to be_falsey } + end + end end diff --git a/spec/models/clusters/providers/gcp_spec.rb b/spec/models/clusters/providers/gcp_spec.rb index 785db4febe0..7ac1bbfafd8 100644 --- a/spec/models/clusters/providers/gcp_spec.rb +++ b/spec/models/clusters/providers/gcp_spec.rb @@ -166,6 +166,22 @@ describe Clusters::Providers::Gcp do end end + describe '#knative_pre_installed?' do + subject { gcp.knative_pre_installed? } + + context 'when cluster is cloud_run' do + let(:gcp) { create(:cluster_provider_gcp) } + + it { is_expected.to be_falsey } + end + + context 'when cluster is not cloud_run' do + let(:gcp) { create(:cluster_provider_gcp, :cloud_run_enabled) } + + it { is_expected.to be_truthy } + end + end + describe '#api_client' do subject { gcp.api_client } diff --git a/spec/services/clusters/gcp/finalize_creation_service_spec.rb b/spec/services/clusters/gcp/finalize_creation_service_spec.rb index 5f91acb8e84..43dbea959a2 100644 --- a/spec/services/clusters/gcp/finalize_creation_service_spec.rb +++ b/spec/services/clusters/gcp/finalize_creation_service_spec.rb @@ -107,6 +107,9 @@ describe Clusters::Gcp::FinalizeCreationService, '#execute' do namespace: 'default' } ) + + stub_kubeclient_get_cluster_role_binding_error(api_url, 'gitlab-admin') + stub_kubeclient_create_cluster_role_binding(api_url) end end @@ -133,9 +136,6 @@ describe Clusters::Gcp::FinalizeCreationService, '#execute' do context 'With an RBAC cluster' do before do provider.legacy_abac = false - - stub_kubeclient_get_cluster_role_binding_error(api_url, 'gitlab-admin') - stub_kubeclient_create_cluster_role_binding(api_url) end include_context 'kubernetes information successfully fetched' @@ -152,4 +152,22 @@ describe Clusters::Gcp::FinalizeCreationService, '#execute' do it_behaves_like 'kubernetes information not successfully fetched' end + + 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 end diff --git a/spec/support/google_api/cloud_platform_helpers.rb b/spec/support/google_api/cloud_platform_helpers.rb index a1328ef0d13..38ffca8c5ae 100644 --- a/spec/support/google_api/cloud_platform_helpers.rb +++ b/spec/support/google_api/cloud_platform_helpers.rb @@ -65,7 +65,7 @@ module GoogleApi end def cloud_platform_create_cluster_url(project_id, zone) - "https://container.googleapis.com/v1/projects/#{project_id}/zones/#{zone}/clusters" + "https://container.googleapis.com/v1beta1/projects/#{project_id}/zones/#{zone}/clusters" end def cloud_platform_get_zone_operation_url(project_id, zone, operation_id) diff --git a/spec/support/shared_examples/models/cluster_application_status_shared_examples.rb b/spec/support/shared_examples/models/cluster_application_status_shared_examples.rb index 5341aacb445..6f06d323a82 100644 --- a/spec/support/shared_examples/models/cluster_application_status_shared_examples.rb +++ b/spec/support/shared_examples/models/cluster_application_status_shared_examples.rb @@ -11,6 +11,20 @@ shared_examples 'cluster application status specs' do |application_name| end end + describe '#status_states' do + let(:cluster) { create(:cluster, :provided_by_gcp) } + + subject { described_class.new(cluster: cluster) } + + it 'returns a hash of state values' do + expect(subject.status_states).to include(:installed) + end + + it 'returns an integer for installed state value' do + expect(subject.status_states[:installed]).to eq(3) + end + end + describe '.available' do subject { described_class.available } |