summaryrefslogtreecommitdiff
path: root/spec/services/clusters/verify_service_spec.rb
blob: 33ea307f9719e97996dc884d1dbe5a0d514fd5e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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