diff options
Diffstat (limited to 'spec/lib/gitlab/kubernetes/node_spec.rb')
-rw-r--r-- | spec/lib/gitlab/kubernetes/node_spec.rb | 52 |
1 files changed, 29 insertions, 23 deletions
diff --git a/spec/lib/gitlab/kubernetes/node_spec.rb b/spec/lib/gitlab/kubernetes/node_spec.rb index 732bf29bc44..fdc3433ff0f 100644 --- a/spec/lib/gitlab/kubernetes/node_spec.rb +++ b/spec/lib/gitlab/kubernetes/node_spec.rb @@ -7,45 +7,51 @@ RSpec.describe Gitlab::Kubernetes::Node do describe '#all' do let(:cluster) { create(:cluster, :provided_by_user, :group) } - let(:expected_nodes) { [] } + let(:expected_nodes) { nil } + let(:nodes) { [kube_node.merge(kube_node_metrics)] } + + subject { described_class.new(cluster).all } before do stub_kubeclient_nodes_and_nodes_metrics(cluster.platform.api_url) end - subject { described_class.new(cluster).all } - context 'when connection to the cluster is successful' do - let(:expected_nodes) { [kube_node.merge(kube_node_metrics)] } + let(:expected_nodes) { { nodes: nodes } } it { is_expected.to eq(expected_nodes) } end - context 'when cluster cannot be reached' do - before do - allow(cluster.kubeclient.core_client).to receive(:discover) - .and_raise(SocketError) + context 'when there is a connection error' do + using RSpec::Parameterized::TableSyntax + + where(:error, :error_status) do + SocketError | :kubernetes_connection_error + OpenSSL::X509::CertificateError | :kubernetes_authentication_error + StandardError | :unknown_error + Kubeclient::HttpError.new(408, "", nil) | :kubeclient_http_error end - it { is_expected.to eq(expected_nodes) } - end + context 'when there is an error while querying nodes' do + with_them do + before do + allow(cluster.kubeclient).to receive(:get_nodes).and_raise(error) + end - context 'when cluster cannot be authenticated to' do - before do - allow(cluster.kubeclient.core_client).to receive(:discover) - .and_raise(OpenSSL::X509::CertificateError.new('Certificate error')) + it { is_expected.to eq({ node_connection_error: error_status }) } + end end - it { is_expected.to eq(expected_nodes) } - end + context 'when there is an error while querying metrics' do + with_them do + before do + allow(cluster.kubeclient).to receive(:get_nodes).and_return({ response: nodes }) + allow(cluster.kubeclient).to receive(:metrics_client).and_raise(error) + end - context 'when Kubeclient::HttpError is raised' do - before do - allow(cluster.kubeclient.core_client).to receive(:discover) - .and_raise(Kubeclient::HttpError.new(403, 'Forbidden', nil)) + it { is_expected.to eq({ nodes: nodes, metrics_connection_error: error_status }) } + end end - - it { is_expected.to eq(expected_nodes) } end context 'when an uncategorised error is raised' do @@ -54,7 +60,7 @@ RSpec.describe Gitlab::Kubernetes::Node do .and_raise(StandardError) end - it { is_expected.to eq(expected_nodes) } + it { is_expected.to eq({ node_connection_error: :unknown_error }) } it 'notifies Sentry' do expect(Gitlab::ErrorTracking).to receive(:track_exception) |