summaryrefslogtreecommitdiff
path: root/spec/models/alert_management/alert_spec.rb
blob: 71211b3841863c0df2cddf8f0e32d3601ae74b6e (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe AlertManagement::Alert do
  let_it_be(:project) { create(:project) }
  let_it_be(:project2) { create(:project) }
  let_it_be(:triggered_alert, reload: true) { create(:alert_management_alert, :triggered, project: project) }
  let_it_be(:acknowledged_alert, reload: true) { create(:alert_management_alert, :acknowledged, project: project) }
  let_it_be(:resolved_alert, reload: true) { create(:alert_management_alert, :resolved, project: project2) }
  let_it_be(:ignored_alert, reload: true) { create(:alert_management_alert, :ignored, project: project2) }

  describe 'associations' do
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:issue).optional }
    it { is_expected.to belong_to(:prometheus_alert).optional }
    it { is_expected.to belong_to(:environment).optional }
    it { is_expected.to have_many(:assignees).through(:alert_assignees) }
    it { is_expected.to have_many(:notes) }
    it { is_expected.to have_many(:ordered_notes) }
    it { is_expected.to have_many(:user_mentions) }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:title) }
    it { is_expected.to validate_presence_of(:events) }
    it { is_expected.to validate_presence_of(:severity) }
    it { is_expected.to validate_presence_of(:status) }
    it { is_expected.to validate_presence_of(:started_at) }

    it { is_expected.to validate_length_of(:title).is_at_most(200) }
    it { is_expected.to validate_length_of(:description).is_at_most(1000) }
    it { is_expected.to validate_length_of(:service).is_at_most(100) }
    it { is_expected.to validate_length_of(:monitoring_tool).is_at_most(100) }

    context 'when status is triggered' do
      subject { triggered_alert }

      context 'when ended_at is blank' do
        it { is_expected.to be_valid }
      end

      context 'when ended_at is present' do
        before do
          triggered_alert.ended_at = Time.current
        end

        it { is_expected.to be_invalid }
      end
    end

    context 'when status is acknowledged' do
      subject { acknowledged_alert }

      context 'when ended_at is blank' do
        it { is_expected.to be_valid }
      end

      context 'when ended_at is present' do
        before do
          acknowledged_alert.ended_at = Time.current
        end

        it { is_expected.to be_invalid }
      end
    end

    context 'when status is resolved' do
      subject { resolved_alert }

      context 'when ended_at is blank' do
        before do
          resolved_alert.ended_at = nil
        end

        it { is_expected.to be_invalid }
      end

      context 'when ended_at is present' do
        it { is_expected.to be_valid }
      end
    end

    context 'when status is ignored' do
      subject { ignored_alert }

      context 'when ended_at is blank' do
        it { is_expected.to be_valid }
      end

      context 'when ended_at is present' do
        before do
          ignored_alert.ended_at = Time.current
        end

        it { is_expected.to be_invalid }
      end
    end

    describe 'fingerprint' do
      let_it_be(:fingerprint) { 'fingerprint' }
      let(:new_alert) { build(:alert_management_alert, fingerprint: fingerprint, project: project) }

      subject { new_alert }

      context 'adding an alert with the same fingerprint' do
        context 'same project, various states' do
          using RSpec::Parameterized::TableSyntax

          let_it_be(:existing_alert) { create(:alert_management_alert, fingerprint: fingerprint, project: project) }

          # We are only validating uniqueness for non-resolved alerts
          where(:existing_status, :new_status, :valid) do
            :resolved      | :triggered    | true
            :resolved      | :acknowledged | true
            :resolved      | :ignored      | true
            :resolved      | :resolved     | true
            :triggered     | :triggered    | false
            :triggered     | :acknowledged | false
            :triggered     | :ignored      | false
            :triggered     | :resolved     | true
            :acknowledged  | :triggered    | false
            :acknowledged  | :acknowledged | false
            :acknowledged  | :ignored      | false
            :acknowledged  | :resolved     | true
            :ignored       | :triggered    | false
            :ignored       | :acknowledged | false
            :ignored       | :ignored      | false
            :ignored       | :resolved     | true
          end

          with_them do
            let(:new_alert) { build(:alert_management_alert, new_status, fingerprint: fingerprint, project: project) }

            before do
              existing_alert.public_send(described_class::STATUS_EVENTS[existing_status])
            end

            if params[:valid]
              it { is_expected.to be_valid }
            else
              it { is_expected.to be_invalid }
            end
          end
        end

        context 'different project' do
          let_it_be(:existing_alert) { create(:alert_management_alert, fingerprint: fingerprint, project: project2) }

          it { is_expected.to be_valid }
        end
      end
    end

    describe 'hosts' do
      subject(:alert) { triggered_alert }

      before do
        triggered_alert.hosts = hosts
      end

      context 'over 255 total chars' do
        let(:hosts) { ['111.111.111.111'] * 18 }

        it { is_expected.not_to be_valid }
      end

      context 'under 255 chars' do
        let(:hosts) { ['111.111.111.111'] * 17 }

        it { is_expected.to be_valid }
      end
    end
  end

  describe 'enums' do
    let(:severity_values) do
      { critical: 0, high: 1, medium: 2, low: 3, info: 4, unknown: 5 }
    end

    it { is_expected.to define_enum_for(:severity).with_values(severity_values) }
  end

  describe 'scopes' do
    describe '.for_iid' do
      subject { project.alert_management_alerts.for_iid(triggered_alert.iid) }

      it { is_expected.to match_array(triggered_alert) }
    end

    describe '.for_status' do
      let(:status) { AlertManagement::Alert::STATUSES[:resolved] }

      subject { AlertManagement::Alert.for_status(status) }

      it { is_expected.to match_array(resolved_alert) }

      context 'with multiple statuses' do
        let(:status) { AlertManagement::Alert::STATUSES.values_at(:resolved, :ignored) }

        it { is_expected.to match_array([resolved_alert, ignored_alert]) }
      end
    end

    describe '.for_fingerprint' do
      let(:fingerprint) { SecureRandom.hex }
      let(:alert_with_fingerprint) { triggered_alert }
      let(:unrelated_alert_with_finger_print) { resolved_alert }

      subject { described_class.for_fingerprint(project, fingerprint) }

      before do
        alert_with_fingerprint.update!(fingerprint: fingerprint)
        unrelated_alert_with_finger_print.update!(fingerprint: fingerprint)
      end

      it { is_expected.to contain_exactly(alert_with_fingerprint) }
    end

    describe '.for_environment' do
      let(:environment) { create(:environment, project: project) }
      let(:env_alert) { triggered_alert }

      subject { described_class.for_environment(environment) }

      before do
        triggered_alert.update!(environment: environment)
      end

      it { is_expected.to match_array(env_alert) }
    end

    describe '.order_severity_with_open_prometheus_alert' do
      subject { described_class.where(project: alert_project).order_severity_with_open_prometheus_alert }

      let_it_be(:alert_project) { create(:project) }
      let_it_be(:resolved_critical_alert) { create(:alert_management_alert, :resolved, :critical, project: alert_project) }
      let_it_be(:triggered_critical_alert) { create(:alert_management_alert, :triggered, :critical, project: alert_project) }
      let_it_be(:triggered_high_alert) { create(:alert_management_alert, :triggered, :high, project: alert_project) }

      it { is_expected.to eq([triggered_critical_alert, triggered_high_alert]) }
    end

    describe '.counts_by_status' do
      subject { described_class.counts_by_status }

      it do
        is_expected.to eq(
          triggered_alert.status => 1,
          acknowledged_alert.status => 1,
          resolved_alert.status => 1,
          ignored_alert.status => 1
        )
      end
    end

    describe '.counts_by_project_id' do
      subject { described_class.counts_by_project_id }

      it do
        is_expected.to eq(
          project.id => 2,
          project2.id => 2
        )
      end
    end

    describe '.open' do
      subject { described_class.open }

      it { is_expected.to contain_exactly(acknowledged_alert, triggered_alert) }
    end

    describe '.not_resolved' do
      subject { described_class.not_resolved }

      it { is_expected.to contain_exactly(acknowledged_alert, triggered_alert, ignored_alert) }
    end
  end

  describe '.last_prometheus_alert_by_project_id' do
    subject { described_class.last_prometheus_alert_by_project_id }

    let!(:p1_alert_1) { triggered_alert }
    let!(:p1_alert_2) { acknowledged_alert }

    let!(:p2_alert_1) { resolved_alert }
    let!(:p2_alert_2) { ignored_alert }

    it 'returns the latest alert for each project' do
      expect(subject).to contain_exactly(p1_alert_2, p2_alert_2)
    end
  end

  describe '.search' do
    let(:alert) { triggered_alert }

    before do
      alert.update!(title: 'Title', description: 'Desc', service: 'Service', monitoring_tool: 'Monitor')
    end

    subject { AlertManagement::Alert.search(query) }

    context 'does not contain search string' do
      let(:query) { 'something else' }

      it { is_expected.to be_empty }
    end

    context 'title includes query' do
      let(:query) { alert.title.upcase }

      it { is_expected.to contain_exactly(alert) }
    end

    context 'description includes query' do
      let(:query) { alert.description.upcase }

      it { is_expected.to contain_exactly(alert) }
    end

    context 'service includes query' do
      let(:query) { alert.service.upcase }

      it { is_expected.to contain_exactly(alert) }
    end

    context 'monitoring tool includes query' do
      let(:query) { alert.monitoring_tool.upcase }

      it { is_expected.to contain_exactly(alert) }
    end
  end

  describe '.reference_pattern' do
    subject { described_class.reference_pattern }

    it { is_expected.to match('gitlab-org/gitlab^alert#123') }
  end

  describe '.link_reference_pattern' do
    subject { described_class.link_reference_pattern }

    it { is_expected.to match(triggered_alert.details_url) }
    it { is_expected.not_to match("#{Gitlab.config.gitlab.url}/gitlab-org/gitlab/alert_management/123") }
    it { is_expected.not_to match("#{Gitlab.config.gitlab.url}/gitlab-org/gitlab/issues/123") }
    it { is_expected.not_to match("gitlab-org/gitlab/-/alert_management/123") }
  end

  describe '.reference_valid?' do
    using RSpec::Parameterized::TableSyntax

    where(:ref, :result) do
      '123456' | true
      '1' | true
      '-1' | false
      nil | false
      '123456891012345678901234567890' | false
    end

    with_them do
      it { expect(described_class.reference_valid?(ref)).to eq(result) }
    end
  end

  describe '#to_reference' do
    it { expect(triggered_alert.to_reference).to eq("^alert##{triggered_alert.iid}") }
  end

  describe '#trigger' do
    subject { alert.trigger }

    context 'when alert is in triggered state' do
      let(:alert) { triggered_alert }

      it 'does not change the alert status' do
        expect { subject }.not_to change { alert.reload.status }
      end
    end

    context 'when alert not in triggered state' do
      let(:alert) { resolved_alert }

      it 'changes the alert status to triggered' do
        expect { subject }.to change { alert.triggered? }.to(true)
      end

      it 'resets ended at' do
        expect { subject }.to change { alert.reload.ended_at }.to nil
      end
    end
  end

  describe '#acknowledge' do
    subject { alert.acknowledge }

    let(:alert) { resolved_alert }

    it 'changes the alert status to acknowledged' do
      expect { subject }.to change { alert.acknowledged? }.to(true)
    end

    it 'resets ended at' do
      expect { subject }.to change { alert.reload.ended_at }.to nil
    end
  end

  describe '#resolve' do
    let!(:ended_at) { Time.current }

    subject do
      alert.ended_at = ended_at
      alert.resolve
    end

    context 'when alert already resolved' do
      let(:alert) { resolved_alert }

      it 'does not change the alert status' do
        expect { subject }.not_to change { resolved_alert.reload.status }
      end
    end

    context 'when alert is not resolved' do
      let(:alert) { triggered_alert }

      it 'changes alert status to "resolved"' do
        expect { subject }.to change { alert.resolved? }.to(true)
      end
    end
  end

  describe '#ignore' do
    subject { alert.ignore }

    let(:alert) { resolved_alert }

    it 'changes the alert status to ignored' do
      expect { subject }.to change { alert.ignored? }.to(true)
    end

    it 'resets ended at' do
      expect { subject }.to change { alert.reload.ended_at }.to nil
    end
  end

  describe '#register_new_event!' do
    subject { alert.register_new_event! }

    let(:alert) { triggered_alert }

    it 'increments the events count by 1' do
      expect { subject }.to change { alert.events }.by(1)
    end
  end

  describe '#present' do
    context 'when alert is generic' do
      let(:alert) { triggered_alert }

      it 'uses generic alert presenter' do
        expect(alert.present).to be_kind_of(AlertManagement::AlertPresenter)
      end
    end

    context 'when alert is Prometheus specific' do
      let(:alert) { build(:alert_management_alert, :prometheus, project: project) }

      it 'uses Prometheus Alert presenter' do
        expect(alert.present).to be_kind_of(AlertManagement::PrometheusAlertPresenter)
      end
    end
  end
end