summaryrefslogtreecommitdiff
path: root/spec/services/clusters/integrations/prometheus_health_check_service_spec.rb
blob: 2d527bb08725acd951e0173c84bbc3d0e6013d1f (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
114
115
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Clusters::Integrations::PrometheusHealthCheckService, '#execute', feature_category: :kubernetes_management do
  let(:service) { described_class.new(cluster) }

  subject { service.execute }

  RSpec.shared_examples 'no alert' do
    it 'does not send alert' do
      expect(Projects::Alerting::NotifyService).not_to receive(:new)

      subject
    end
  end

  RSpec.shared_examples 'sends alert' do
    it 'sends an alert' do
      expect_next_instance_of(Projects::Alerting::NotifyService) do |notify_service|
        expect(notify_service).to receive(:execute).with(integration.token, integration)
      end

      subject
    end
  end

  RSpec.shared_examples 'correct health stored' do
    it 'stores the correct health of prometheus' do
      subject

      expect(prometheus.healthy?).to eq(client_healthy)
    end
  end

  context 'when cluster is not project_type' do
    let(:cluster) { create(:cluster, :instance) }

    it { expect { subject }.to raise_error(RuntimeError, 'Invalid cluster type. Only project types are allowed.') }
  end

  context 'when cluster is project_type' do
    let_it_be(:project) { create(:project) }
    let_it_be(:integration) { create(:alert_management_http_integration, project: project) }

    let(:previous_health_status) { :healthy }
    let(:prometheus) { create(:clusters_integrations_prometheus, enabled: prometheus_enabled, health_status: previous_health_status) }
    let(:cluster) { create(:cluster, :project, integration_prometheus: prometheus, projects: [project]) }

    context 'when prometheus not enabled' do
      let(:prometheus_enabled) { false }

      it { expect(subject).to eq(nil) }

      include_examples 'no alert'
    end

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

      before do
        client = instance_double('Gitlab::PrometheusClient', healthy?: client_healthy)
        expect(prometheus).to receive(:prometheus_client).and_return(client)
      end

      context 'when newly unhealthy' do
        let(:previous_health_status) { :healthy }
        let(:client_healthy) { false }

        include_examples 'sends alert'
        include_examples 'correct health stored'
      end

      context 'when newly healthy' do
        let(:previous_health_status) { :unhealthy }
        let(:client_healthy) { true }

        include_examples 'no alert'
        include_examples 'correct health stored'
      end

      context 'when continuously unhealthy' do
        let(:previous_health_status) { :unhealthy }
        let(:client_healthy) { false }

        include_examples 'no alert'
        include_examples 'correct health stored'
      end

      context 'when continuously healthy' do
        let(:previous_health_status) { :healthy }
        let(:client_healthy) { true }

        include_examples 'no alert'
        include_examples 'correct health stored'
      end

      context 'when first health check and healthy' do
        let(:previous_health_status) { :unknown }
        let(:client_healthy) { true }

        include_examples 'no alert'
        include_examples 'correct health stored'
      end

      context 'when first health check and not healthy' do
        let(:previous_health_status) { :unknown }
        let(:client_healthy) { false }

        include_examples 'sends alert'
        include_examples 'correct health stored'
      end
    end
  end
end