summaryrefslogtreecommitdiff
path: root/spec/models/clusters/integrations/prometheus_spec.rb
blob: 680786189ad064e911484c07070d98a2def98857 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Clusters::Integrations::Prometheus do
  include KubernetesHelpers
  include StubRequests

  describe 'associations' do
    it { is_expected.to belong_to(:cluster).class_name('Clusters::Cluster') }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:cluster) }
    it { is_expected.not_to allow_value(nil).for(:enabled) }
  end

  describe 'after_destroy' do
    subject(:integration) { create(:clusters_integrations_prometheus, cluster: cluster, enabled: true) }

    let(:cluster) { create(:cluster, :with_installed_helm) }

    it 'deactivates prometheus_service' do
      expect(Clusters::Applications::DeactivateServiceWorker)
        .to receive(:perform_async).with(cluster.id, 'prometheus')

      integration.destroy!
    end
  end

  describe 'after_save' do
    subject(:integration) { create(:clusters_integrations_prometheus, cluster: cluster, enabled: enabled) }

    let(:cluster) { create(:cluster, :with_installed_helm) }
    let(:enabled) { true }

    context 'when no change to enabled status' do
      it 'does not touch project services' do
        integration # ensure integration exists before we set the expectations

        expect(Clusters::Applications::DeactivateServiceWorker)
          .not_to receive(:perform_async)

        expect(Clusters::Applications::ActivateServiceWorker)
          .not_to receive(:perform_async)

        integration.update!(enabled: enabled)
      end
    end

    context 'when enabling' do
      let(:enabled) { false }

      it 'deactivates prometheus_service' do
        expect(Clusters::Applications::ActivateServiceWorker)
          .to receive(:perform_async).with(cluster.id, 'prometheus')

        integration.update!(enabled: true)
      end
    end

    context 'when disabling' do
      let(:enabled) { true }

      it 'activates prometheus_service' do
        expect(Clusters::Applications::DeactivateServiceWorker)
          .to receive(:perform_async).with(cluster.id, 'prometheus')

        integration.update!(enabled: false)
      end
    end
  end

  describe '#prometheus_client' do
    include_examples '#prometheus_client shared' do
      let(:factory) { :clusters_integrations_prometheus }
    end
  end

  describe '#configured?' do
    let(:prometheus) { create(:clusters_integrations_prometheus, cluster: cluster) }

    subject { prometheus.configured? }

    context 'when a kubenetes client is present' do
      let(:cluster) { create(:cluster, :project, :provided_by_gcp) }

      it { is_expected.to be_truthy }

      context 'when it is disabled' do
        let(:prometheus) { create(:clusters_integrations_prometheus, :disabled, cluster: cluster) }

        it { is_expected.to be_falsey }
      end

      context 'when the kubernetes URL is blocked' do
        before do
          blocked_ip = '127.0.0.1' # localhost addresses are blocked by default

          stub_all_dns(cluster.platform.api_url, ip_address: blocked_ip)
        end

        it { is_expected.to be_falsey }
      end
    end

    context 'when a kubenetes client is not present' do
      let(:cluster) { create(:cluster) }

      it { is_expected.to be_falsy }
    end
  end
end