summaryrefslogtreecommitdiff
path: root/spec/workers/incident_management/process_prometheus_alert_worker_spec.rb
blob: c9ea96df5c26a2da57d66e187ea6bcf9254f755b (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# frozen_string_literal: true

require 'spec_helper'

describe IncidentManagement::ProcessPrometheusAlertWorker do
  describe '#perform' do
    let_it_be(:project) { create(:project) }
    let_it_be(:prometheus_alert) { create(:prometheus_alert, project: project) }
    let(:payload_key) { Gitlab::Alerting::Alert.new(project: project, payload: alert_params).gitlab_fingerprint }
    let!(:prometheus_alert_event) { create(:prometheus_alert_event, prometheus_alert: prometheus_alert, payload_key: payload_key) }
    let!(:settings) { create(:project_incident_management_setting, project: project, create_issue: true) }

    let(:alert_params) do
      {
        startsAt: prometheus_alert.created_at.rfc3339,
        labels: {
          gitlab_alert_id: prometheus_alert.prometheus_metric_id
        }
      }.with_indifferent_access
    end

    it 'creates an issue' do
      expect { subject.perform(project.id, alert_params) }
        .to change(Issue, :count)
        .by(1)
    end

    it 'relates issue to an event' do
      expect { subject.perform(project.id, alert_params) }
        .to change(prometheus_alert.related_issues, :count)
        .from(0)
        .to(1)
    end

    context 'resolved event' do
      let(:issue) { create(:issue, project: project) }

      before do
        prometheus_alert_event.related_issues << issue
        prometheus_alert_event.resolve
      end

      it 'does not create an issue' do
        expect { subject.perform(project.id, alert_params) }
          .not_to change(Issue, :count)
      end

      it 'closes the existing issue' do
        expect { subject.perform(project.id, alert_params) }
          .to change { issue.reload.state }
          .from('opened')
          .to('closed')
      end

      it 'leaves a system note on the issue' do
        expect(SystemNoteService)
          .to receive(:auto_resolve_prometheus_alert)

        subject.perform(project.id, alert_params)
      end
    end

    context 'when project could not be found' do
      let(:non_existing_project_id) { non_existing_record_id }

      it 'does not create an issue' do
        expect { subject.perform(non_existing_project_id, alert_params) }
          .not_to change(Issue, :count)
      end

      it 'does not relate issue to an event' do
        expect { subject.perform(non_existing_project_id, alert_params) }
          .not_to change(prometheus_alert.related_issues, :count)
      end
    end

    context 'when event could not be found' do
      before do
        alert_params[:labels][:gitlab_alert_id] = non_existing_record_id
      end

      it 'does not create an issue' do
        expect { subject.perform(project.id, alert_params) }
          .not_to change(Issue, :count)
      end

      it 'does not relate issue to an event' do
        expect { subject.perform(project.id, alert_params) }
          .not_to change(prometheus_alert.related_issues, :count)
      end
    end

    context 'when issue could not be created' do
      before do
        allow_next_instance_of(IncidentManagement::CreateIssueService) do |instance|
          allow(instance).to receive(:execute).and_return( { error: true } )
        end
      end

      it 'does not relate issue to an event' do
        expect { subject.perform(project.id, alert_params) }
          .not_to change(prometheus_alert.related_issues, :count)
      end
    end

    context 'self-managed alert' do
      let(:alert_name) { 'alert' }
      let(:starts_at) { Time.now.rfc3339 }

      let!(:prometheus_alert_event) do
        create(:self_managed_prometheus_alert_event, project: project, payload_key: payload_key)
      end

      let(:alert_params) do
        {
          startsAt: starts_at,
          generatorURL: 'http://localhost:9090/graph?g0.expr=vector%281%29&g0.tab=1',
          labels: {
            alertname: alert_name
          }
        }.with_indifferent_access
      end

      it 'creates an issue' do
        expect { subject.perform(project.id, alert_params) }
          .to change(Issue, :count)
          .by(1)
      end

      it 'relates issue to an event' do
        expect { subject.perform(project.id, alert_params) }
          .to change(prometheus_alert_event.related_issues, :count)
          .from(0)
          .to(1)
      end

      context 'when event could not be found' do
        before do
          alert_params[:generatorURL] = 'http://somethingelse.com'
        end

        it 'creates an issue' do
          expect { subject.perform(project.id, alert_params) }
            .to change(Issue, :count)
            .by(1)
        end

        it 'does not relate issue to an event' do
          expect { subject.perform(project.id, alert_params) }
            .not_to change(prometheus_alert.related_issues, :count)
        end
      end
    end
  end
end