summaryrefslogtreecommitdiff
path: root/spec/factories/alert_management/alerts.rb
blob: ee1225b954241eb38cc2d992fa7dda4d8bc0cb8f (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
# frozen_string_literal: true
require 'ffaker'

FactoryBot.define do
  factory :alert_management_alert, class: 'AlertManagement::Alert' do
    triggered
    project
    title { FFaker::Lorem.sentence }
    started_at { Time.current }

    trait :with_validation_errors do
      after(:create) do |alert|
        too_many_hosts = Array.new(AlertManagement::Alert::HOSTS_MAX_LENGTH + 1) { |_| 'host' }
        alert.update_columns(hosts: too_many_hosts)
      end
    end

    trait :with_issue do
      after(:create) do |alert|
        create(:issue, alert_management_alert: alert, project: alert.project)
      end
    end

    trait :with_assignee do |alert|
      after(:create) do |alert|
        alert.alert_assignees.create!(assignee: create(:user))
      end
    end

    trait :with_fingerprint do
      fingerprint { SecureRandom.hex }
    end

    trait :with_service do
      service { FFaker::Product.product_name }
    end

    trait :with_monitoring_tool do
      monitoring_tool { FFaker::AWS.product_description }
    end

    trait :with_description do
      description { FFaker::Lorem.sentence }
    end

    trait :with_host do
      hosts { [FFaker::Internet.ip_v4_address] }
    end

    trait :without_ended_at do
      ended_at { nil }
    end

    trait :triggered do
      status { AlertManagement::Alert.status_value(:triggered) }
      without_ended_at
    end

    trait :acknowledged do
      status { AlertManagement::Alert.status_value(:acknowledged) }
      without_ended_at
    end

    trait :resolved do
      status { AlertManagement::Alert.status_value(:resolved) }
      ended_at { Time.current }
    end

    trait :ignored do
      status { AlertManagement::Alert.status_value(:ignored) }
      without_ended_at
    end

    trait :critical do
      severity { 'critical' }
    end

    trait :high do
      severity { 'high' }
    end

    trait :medium do
      severity { 'medium' }
    end

    trait :low do
      severity { 'low' }
    end

    trait :info do
      severity { 'info' }
    end

    trait :unknown do
      severity { 'unknown' }
    end

    trait :prometheus do
      monitoring_tool { Gitlab::AlertManagement::Payload::MONITORING_TOOLS[:prometheus] }
      payload do
        {
          annotations: {
            title: 'This is a prometheus error',
            summary: 'Summary of the error',
            description: 'Description of the error'
          },
          startsAt: started_at
        }.with_indifferent_access
      end
    end

    trait :all_fields do
      with_issue
      with_assignee
      with_fingerprint
      with_service
      with_monitoring_tool
      with_host
      with_description
      low
    end

    trait :from_payload do
      after(:build) do |alert|
        alert_params = ::Gitlab::AlertManagement::Payload.parse(
          alert.project,
          alert.payload,
          monitoring_tool: alert.monitoring_tool
        ).alert_params

        alert.assign_attributes(alert_params)
      end
    end
  end
end