Welcome to mirror list, hosted at ThFree Co, Russian Federation.

alert_params_spec.rb « alert_management « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3171be5e299dade2ccca76d7b3dc24c432ed472 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::AlertManagement::AlertParams do
  let_it_be(:project) { create(:project, :repository, :private) }

  describe '.from_generic_alert' do
    let(:started_at) { Time.current.change(usec: 0).rfc3339 }
    let(:default_payload) do
      {
        'title' => 'Alert title',
        'description' => 'Description',
        'monitoring_tool' => 'Monitoring tool name',
        'service' => 'Service',
        'hosts' => ['gitlab.com'],
        'start_time' => started_at,
        'some' => { 'extra' => { 'payload' => 'here' } }
      }
    end

    let(:payload) { default_payload }

    subject { described_class.from_generic_alert(project: project, payload: payload) }

    it 'returns Alert compatible parameters' do
      is_expected.to eq(
        project_id: project.id,
        title: 'Alert title',
        description: 'Description',
        monitoring_tool: 'Monitoring tool name',
        service: 'Service',
        severity: 'critical',
        hosts: ['gitlab.com'],
        payload: payload,
        started_at: started_at,
        ended_at: nil,
        fingerprint: nil,
        environment: nil
      )
    end

    context 'when severity given' do
      let(:payload) { default_payload.merge(severity: 'low') }

      it 'returns Alert compatible parameters' do
        expect(subject[:severity]).to eq('low')
      end
    end

    context 'when there are no hosts in the payload' do
      let(:payload) { {} }

      it 'hosts param is an empty array' do
        expect(subject[:hosts]).to be_empty
      end
    end
  end

  describe '.from_prometheus_alert' do
    let(:payload) do
      {
        'status' => 'firing',
        'labels' => {
          'alertname' => 'GitalyFileServerDown',
          'channel' => 'gitaly',
          'pager' => 'pagerduty',
          'severity' => 's1'
        },
        'annotations' => {
          'description' => 'Alert description',
          'runbook' => 'troubleshooting/gitaly-down.md',
          'title' => 'Alert title'
        },
        'startsAt' => '2020-04-27T10:10:22.265949279Z',
        'endsAt' => '0001-01-01T00:00:00Z',
        'generatorURL' => 'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1',
        'fingerprint' => 'b6ac4d42057c43c1'
      }
    end

    let(:parsed_alert) { Gitlab::Alerting::Alert.new(project: project, payload: payload) }

    subject { described_class.from_prometheus_alert(project: project, parsed_alert: parsed_alert) }

    it 'returns Alert-compatible params' do
      is_expected.to eq(
        project_id: project.id,
        title: 'Alert title',
        description: 'Alert description',
        monitoring_tool: 'Prometheus',
        payload: payload,
        started_at: parsed_alert.starts_at,
        ended_at: parsed_alert.ends_at,
        fingerprint: parsed_alert.gitlab_fingerprint,
        environment: parsed_alert.environment,
        prometheus_alert: parsed_alert.gitlab_alert
      )
    end
  end
end