diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/features/groups/clusters/user_spec.rb | 2 | ||||
-rw-r--r-- | spec/features/projects/clusters/user_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/clusters/cluster_spec.rb | 75 | ||||
-rw-r--r-- | spec/services/clusters/verify_service_spec.rb | 79 |
4 files changed, 69 insertions, 89 deletions
diff --git a/spec/features/groups/clusters/user_spec.rb b/spec/features/groups/clusters/user_spec.rb index 693f35926fc..84a8691a7f2 100644 --- a/spec/features/groups/clusters/user_spec.rb +++ b/spec/features/groups/clusters/user_spec.rb @@ -14,7 +14,7 @@ describe 'User Cluster', :js do allow(Groups::ClustersController).to receive(:STATUS_POLLING_INTERVAL) { 100 } allow_any_instance_of(Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService).to receive(:execute) - allow(Clusters::VerifyService).to receive(:new).and_return(double(execute: :connected)) + allow_any_instance_of(Clusters::Cluster).to receive(:retrieve_connection_status).and_return(:connected) end context 'when user does not have a cluster and visits cluster index page' do diff --git a/spec/features/projects/clusters/user_spec.rb b/spec/features/projects/clusters/user_spec.rb index 4bcced49326..31cc09ae911 100644 --- a/spec/features/projects/clusters/user_spec.rb +++ b/spec/features/projects/clusters/user_spec.rb @@ -12,7 +12,7 @@ describe 'User Cluster', :js do allow(Projects::ClustersController).to receive(:STATUS_POLLING_INTERVAL) { 100 } allow_any_instance_of(Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService).to receive(:execute) - allow(Clusters::VerifyService).to receive(:new).and_return(double(execute: :connected)) + allow_any_instance_of(Clusters::Cluster).to receive(:retrieve_connection_status).and_return(:connected) end context 'when user does not have a cluster and visits cluster index page' do diff --git a/spec/models/clusters/cluster_spec.rb b/spec/models/clusters/cluster_spec.rb index 929c75760d5..4739e62289a 100644 --- a/spec/models/clusters/cluster_spec.rb +++ b/spec/models/clusters/cluster_spec.rb @@ -4,6 +4,7 @@ require 'spec_helper' describe Clusters::Cluster, :use_clean_rails_memory_store_caching do include ReactiveCachingHelpers + include KubernetesHelpers it_behaves_like 'having unique enum values' @@ -610,7 +611,7 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do it { is_expected.to eq :errored } end - context 'there is a chached connection status' do + context 'there is a cached connection status' do let(:cluster) { create(:cluster, :provided_by_user) } before do @@ -655,21 +656,79 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do let(:cluster) { create(:cluster, :disabled) } it 'does not populate the cache' do - expect(Clusters::VerifyService).not_to receive(:new) + expect(cluster).not_to receive(:retrieve_connection_status) is_expected.to be_nil end end context 'cluster is enabled' do - let(:cluster) { create(:cluster) } - let(:status) { :connected } + let(:cluster) { create(:cluster, :provided_by_user, :group) } - it 'retrieves the connection status and adds it to the cache' do - expect(Clusters::VerifyService).to receive(:new).with(cluster) - .and_return(double(execute: status)) + context 'connection to the cluster is successful' do + before do + stub_kubeclient_discover(cluster.platform.api_url) + end + + it { is_expected.to eq(connection_status: :connected) } + end - is_expected.to include(connection_status: status) + context 'cluster cannot be reached' do + before do + allow(cluster.kubeclient.core_client).to receive(:discover) + .and_raise(SocketError) + end + + it { is_expected.to eq(connection_status: :unreachable) } + end + + context 'cluster cannot be authenticated to' do + before do + allow(cluster.kubeclient.core_client).to receive(:discover) + .and_raise(OpenSSL::X509::CertificateError.new("Certificate error")) + end + + it { is_expected.to eq(connection_status: :authentication_failure) } + end + + describe 'Kubeclient::HttpError' do + let(:error_code) { 403 } + let(:error_message) { "Forbidden" } + + before do + allow(cluster.kubeclient.core_client).to receive(:discover) + .and_raise(Kubeclient::HttpError.new(error_code, error_message, nil)) + end + + it { is_expected.to eq(connection_status: :authentication_failure) } + + context 'generic timeout' do + let(:error_message) { 'Timed out connecting to server'} + + it { is_expected.to eq(connection_status: :unreachable) } + end + + context 'gateway timeout' do + let(:error_message) { '504 Gateway Timeout for GET https://kubernetes.example.com/api/v1'} + + it { is_expected.to eq(connection_status: :unreachable) } + end + end + + context 'an uncategorised error is raised' do + before do + allow(cluster.kubeclient.core_client).to receive(:discover) + .and_raise(StandardError) + end + + it { is_expected.to eq(connection_status: :unknown_failure) } + + it 'notifies Sentry' do + expect(Gitlab::Sentry).to receive(:track_acceptable_exception) + .with(instance_of(StandardError), hash_including(extra: { cluster_id: cluster.id })) + + subject + end end end end diff --git a/spec/services/clusters/verify_service_spec.rb b/spec/services/clusters/verify_service_spec.rb deleted file mode 100644 index 33ea307f971..00000000000 --- a/spec/services/clusters/verify_service_spec.rb +++ /dev/null @@ -1,79 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe Clusters::VerifyService do - include KubernetesHelpers - - let(:cluster) { create(:cluster, :provided_by_user, :group) } - - describe '#execute' do - subject { described_class.new(cluster).execute } - - context 'connection to the cluster is successful' do - before do - stub_kubeclient_discover(cluster.platform.api_url) - end - - it { is_expected.to eq :connected } - end - - context 'cluster cannot be reached' do - before do - allow(cluster.kubeclient.core_client).to receive(:discover) - .and_raise(SocketError) - end - - it { is_expected.to eq :unreachable } - end - - context 'cluster cannot be authenticated to' do - before do - allow(cluster.kubeclient.core_client).to receive(:discover) - .and_raise(OpenSSL::X509::CertificateError.new("Certificate error")) - end - - it { is_expected.to eq :authentication_failure } - end - - describe 'Kubeclient::HttpError' do - let(:error_code) { 403 } - let(:error_message) { "Forbidden" } - - before do - allow(cluster.kubeclient.core_client).to receive(:discover) - .and_raise(Kubeclient::HttpError.new(error_code, error_message, nil)) - end - - it { is_expected.to eq :authentication_failure } - - context 'generic timeout' do - let(:error_message) { 'Timed out connecting to server'} - - it { is_expected.to eq :unreachable } - end - - context 'gateway timeout' do - let(:error_message) { '504 Gateway Timeout for GET https://kubernetes.example.com/api/v1'} - - it { is_expected.to eq :unreachable } - end - end - - context 'an uncategorised error is raised' do - before do - allow(cluster.kubeclient.core_client).to receive(:discover) - .and_raise(StandardError) - end - - it { is_expected.to eq :unknown_failure } - - it 'notifies Sentry' do - expect(Gitlab::Sentry).to receive(:track_acceptable_exception) - .with(instance_of(StandardError), hash_including(extra: { cluster_id: cluster.id })) - - subject - end - end - end -end |