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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::AlertManagement::Payload::Base do
let_it_be(:project) { create(:project) }
let(:raw_payload) { {} }
let(:payload_class) { described_class }
subject(:parsed_payload) { payload_class.new(project: project, payload: raw_payload) }
describe '.attribute' do
subject { parsed_payload.test }
context 'with a single path provided' do
let(:payload_class) do
Class.new(described_class) do
attribute :test, paths: [['test']]
end
end
it { is_expected.to be_nil }
context 'and a matching value' do
let(:raw_payload) { { 'test' => 'value' } }
it { is_expected.to eq 'value' }
end
end
context 'with multiple paths provided' do
let(:payload_class) do
Class.new(described_class) do
attribute :test, paths: [['test'], %w(alt test)]
end
end
it { is_expected.to be_nil }
context 'and a matching value' do
let(:raw_payload) { { 'alt' => { 'test' => 'value' } } }
it { is_expected.to eq 'value' }
end
end
context 'with a fallback provided' do
let(:payload_class) do
Class.new(described_class) do
attribute :test, paths: [['test']], fallback: -> { 'fallback' }
end
end
it { is_expected.to eq('fallback') }
context 'and a matching value' do
let(:raw_payload) { { 'test' => 'value' } }
it { is_expected.to eq 'value' }
end
end
context 'with a time type provided' do
let(:test_time) { Time.current.change(usec: 0) }
let(:payload_class) do
Class.new(described_class) do
attribute :test, paths: [['test']], type: :time
end
end
it { is_expected.to be_nil }
context 'with a compatible matching value' do
let(:raw_payload) { { 'test' => test_time.to_s } }
it { is_expected.to eq test_time }
end
context 'with a value in rfc3339 format' do
let(:raw_payload) { { 'test' => test_time.rfc3339 } }
it { is_expected.to eq test_time }
end
context 'with an incompatible matching value' do
let(:raw_payload) { { 'test' => 'bad time' } }
it { is_expected.to be_nil }
end
end
context 'with an integer type provided' do
let(:payload_class) do
Class.new(described_class) do
attribute :test, paths: [['test']], type: :integer
end
end
it { is_expected.to be_nil }
context 'with a compatible matching value' do
let(:raw_payload) { { 'test' => '15' } }
it { is_expected.to eq 15 }
end
context 'with an incompatible matching value' do
let(:raw_payload) { { 'test' => String } }
it { is_expected.to be_nil }
end
context 'with an incompatible matching value' do
let(:raw_payload) { { 'test' => 'apple' } }
it { is_expected.to be_nil }
end
end
end
describe '#alert_params' do
subject { parsed_payload.alert_params }
context 'with every key' do
let_it_be(:raw_payload) { { 'key' => 'value' } }
let_it_be(:stubs) do
{
description: 'description',
ends_at: Time.current,
environment: create(:environment, project: project),
gitlab_fingerprint: 'gitlab_fingerprint',
hosts: 'hosts',
monitoring_tool: 'monitoring_tool',
gitlab_alert: create(:prometheus_alert, project: project),
service: 'service',
severity: 'critical',
starts_at: Time.current,
title: 'title'
}
end
let(:expected_result) do
{
description: stubs[:description],
ended_at: stubs[:ends_at],
environment: stubs[:environment],
fingerprint: stubs[:gitlab_fingerprint],
hosts: [stubs[:hosts]],
monitoring_tool: stubs[:monitoring_tool],
payload: raw_payload,
project_id: project.id,
prometheus_alert: stubs[:gitlab_alert],
service: stubs[:service],
severity: stubs[:severity],
started_at: stubs[:starts_at],
title: stubs[:title]
}
end
before do
allow(parsed_payload).to receive_messages(stubs)
end
it { is_expected.to eq(expected_result) }
it 'can generate a valid new alert' do
expect(::AlertManagement::Alert.new(subject.except(:ended_at))).to be_valid
end
end
context 'with too-long strings' do
let_it_be(:stubs) do
{
description: 'a' * (::AlertManagement::Alert::DESCRIPTION_MAX_LENGTH + 1),
hosts: 'b' * (::AlertManagement::Alert::HOSTS_MAX_LENGTH + 1),
monitoring_tool: 'c' * (::AlertManagement::Alert::TOOL_MAX_LENGTH + 1),
service: 'd' * (::AlertManagement::Alert::SERVICE_MAX_LENGTH + 1),
title: 'e' * (::AlertManagement::Alert::TITLE_MAX_LENGTH + 1)
}
end
before do
allow(parsed_payload).to receive_messages(stubs)
end
it do
is_expected.to eq({
description: stubs[:description].truncate(AlertManagement::Alert::DESCRIPTION_MAX_LENGTH),
hosts: ['b' * ::AlertManagement::Alert::HOSTS_MAX_LENGTH],
monitoring_tool: stubs[:monitoring_tool].truncate(::AlertManagement::Alert::TOOL_MAX_LENGTH),
service: stubs[:service].truncate(::AlertManagement::Alert::SERVICE_MAX_LENGTH),
project_id: project.id,
title: stubs[:title].truncate(::AlertManagement::Alert::TITLE_MAX_LENGTH)
})
end
end
context 'with too-long hosts array' do
let(:hosts) { %w(abc def ghij) }
let(:shortened_hosts) { %w(abc def ghi) }
before do
stub_const('::AlertManagement::Alert::HOSTS_MAX_LENGTH', 9)
allow(parsed_payload).to receive(:hosts).and_return(hosts)
end
it { is_expected.to eq(hosts: shortened_hosts, project_id: project.id) }
context 'with host cut off between elements' do
let(:hosts) { %w(abcde fghij) }
let(:shortened_hosts) { %w(abcde fghi) }
it { is_expected.to eq({ hosts: shortened_hosts, project_id: project.id }) }
end
context 'with nested hosts' do
let(:hosts) { ['abc', ['de', 'f'], 'g', 'hij'] } # rubocop:disable Style/WordArray
let(:shortened_hosts) { %w(abc de f g hi) }
it { is_expected.to eq({ hosts: shortened_hosts, project_id: project.id }) }
end
end
end
describe '#gitlab_fingerprint' do
subject { parsed_payload.gitlab_fingerprint }
it { is_expected.to be_nil }
context 'when plain_gitlab_fingerprint is defined' do
before do
allow(parsed_payload)
.to receive(:plain_gitlab_fingerprint)
.and_return('fingerprint')
end
it 'returns a fingerprint' do
is_expected.to eq(Digest::SHA1.hexdigest('fingerprint'))
end
end
end
describe '#environment' do
let_it_be(:environment) { create(:environment, project: project, name: 'production') }
subject { parsed_payload.environment }
before do
allow(parsed_payload).to receive(:environment_name).and_return(environment_name)
end
context 'without an environment name' do
let(:environment_name) { nil }
it { is_expected.to be_nil }
end
context 'with a non-matching environment name' do
let(:environment_name) { 'other_environment' }
it { is_expected.to be_nil }
end
context 'with a matching environment name' do
let(:environment_name) { 'production' }
it { is_expected.to eq(environment) }
end
end
describe '#resolved?' do
before do
allow(parsed_payload).to receive(:status).and_return(status)
end
subject { parsed_payload.resolved? }
context 'when status is not defined' do
let(:status) { nil }
it { is_expected.to be_falsey }
end
context 'when status is not resovled' do
let(:status) { 'firing' }
it { is_expected.to be_falsey }
end
context 'when status is resovled' do
let(:status) { 'resolved' }
it { is_expected.to be_truthy }
end
end
describe '#has_required_attributes?' do
subject { parsed_payload.has_required_attributes? }
it { is_expected.to be(true) }
end
end
|