2020-07-28 23:09:34 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Gitlab::Kubernetes::Node do
|
|
|
|
include KubernetesHelpers
|
|
|
|
|
|
|
|
describe '#all' do
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_user, :group) }
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:expected_nodes) { nil }
|
|
|
|
let(:nodes) { [kube_node.merge(kube_node_metrics)] }
|
|
|
|
|
|
|
|
subject { described_class.new(cluster).all }
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
stub_kubeclient_nodes_and_nodes_metrics(cluster.platform.api_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when connection to the cluster is successful' do
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:expected_nodes) { { nodes: nodes } }
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
it { is_expected.to eq(expected_nodes) }
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
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
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
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
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it { is_expected.to eq({ node_connection_error: error_status }) }
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
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
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it { is_expected.to eq({ nodes: nodes, metrics_connection_error: error_status }) }
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an uncategorised error is raised' do
|
|
|
|
before do
|
|
|
|
allow(cluster.kubeclient.core_client).to receive(:discover)
|
|
|
|
.and_raise(StandardError)
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it { is_expected.to eq({ node_connection_error: :unknown_error }) }
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
it 'notifies Sentry' do
|
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_exception)
|
|
|
|
.with(instance_of(StandardError), hash_including(cluster_id: cluster.id))
|
|
|
|
.once
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|