forked from prometheus/alertmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifiers_test.go
696 lines (637 loc) · 15.9 KB
/
notifiers_test.go
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// Copyright 2018 Prometheus Team
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"strings"
"testing"
"gopkg.in/yaml.v2"
)
func TestEmailToIsPresent(t *testing.T) {
in := `
to: ''
`
var cfg EmailConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
expected := "missing to address in email config"
if err == nil {
t.Fatalf("no error returned, expected:\n%v", expected)
}
if err.Error() != expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
}
}
func TestEmailHeadersCollision(t *testing.T) {
in := `
to: 'to@email.com'
headers:
Subject: 'Alert'
subject: 'New Alert'
`
var cfg EmailConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
expected := "duplicate header \"Subject\" in email config"
if err == nil {
t.Fatalf("no error returned, expected:\n%v", expected)
}
if err.Error() != expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
}
}
func TestPagerdutyRoutingKeyIsPresent(t *testing.T) {
in := `
routing_key: ''
`
var cfg PagerdutyConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
expected := "missing service or routing key in PagerDuty config"
if err == nil {
t.Fatalf("no error returned, expected:\n%v", expected)
}
if err.Error() != expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
}
}
func TestPagerdutyServiceKeyIsPresent(t *testing.T) {
in := `
service_key: ''
`
var cfg PagerdutyConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
expected := "missing service or routing key in PagerDuty config"
if err == nil {
t.Fatalf("no error returned, expected:\n%v", expected)
}
if err.Error() != expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
}
}
func TestPagerdutyDetails(t *testing.T) {
tests := []struct {
in string
checkFn func(map[string]string)
}{
{
in: `
routing_key: 'xyz'
`,
checkFn: func(d map[string]string) {
if len(d) != 4 {
t.Errorf("expected 4 items, got: %d", len(d))
}
},
},
{
in: `
routing_key: 'xyz'
details:
key1: val1
`,
checkFn: func(d map[string]string) {
if len(d) != 5 {
t.Errorf("expected 5 items, got: %d", len(d))
}
},
},
{
in: `
routing_key: 'xyz'
details:
key1: val1
key2: val2
firing: firing
`,
checkFn: func(d map[string]string) {
if len(d) != 6 {
t.Errorf("expected 6 items, got: %d", len(d))
}
},
},
}
for _, tc := range tests {
var cfg PagerdutyConfig
err := yaml.UnmarshalStrict([]byte(tc.in), &cfg)
if err != nil {
t.Errorf("expected no error, got:%v", err)
}
if tc.checkFn != nil {
tc.checkFn(cfg.Details)
}
}
}
func TestWebhookURLIsPresent(t *testing.T) {
in := `{}`
var cfg WebhookConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
expected := "missing URL in webhook config"
if err == nil {
t.Fatalf("no error returned, expected:\n%v", expected)
}
if err.Error() != expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
}
}
func TestWebhookHttpConfigIsValid(t *testing.T) {
in := `
url: 'http://example.com'
http_config:
bearer_token: foo
bearer_token_file: /tmp/bar
`
var cfg WebhookConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
expected := "at most one of bearer_token & bearer_token_file must be configured"
if err == nil {
t.Fatalf("no error returned, expected:\n%v", expected)
}
if err.Error() != expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
}
}
func TestWebhookHttpConfigIsOptional(t *testing.T) {
in := `
url: 'http://example.com'
`
var cfg WebhookConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
if err != nil {
t.Fatalf("no error expected, returned:\n%v", err.Error())
}
}
func TestWebhookPasswordIsObfuscated(t *testing.T) {
in := `
url: 'http://example.com'
http_config:
basic_auth:
username: foo
password: supersecret
`
var cfg WebhookConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
if err != nil {
t.Fatalf("no error expected, returned:\n%v", err.Error())
}
ycfg, err := yaml.Marshal(cfg)
if err != nil {
t.Fatalf("no error expected, returned:\n%v", err.Error())
}
if strings.Contains(string(ycfg), "supersecret") {
t.Errorf("Found password in the YAML cfg: %s\n", ycfg)
}
}
func TestVictorOpsRoutingKeyIsPresent(t *testing.T) {
in := `
routing_key: ''
`
var cfg VictorOpsConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
expected := "missing Routing key in VictorOps config"
if err == nil {
t.Fatalf("no error returned, expected:\n%v", expected)
}
if err.Error() != expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
}
}
func TestVictorOpsCustomFieldsValidation(t *testing.T) {
in := `
routing_key: 'test'
custom_fields:
entity_state: 'state_message'
`
var cfg VictorOpsConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
expected := "victorOps config contains custom field entity_state which cannot be used as it conflicts with the fixed/static fields"
if err == nil {
t.Fatalf("no error returned, expected:\n%v", expected)
}
if err.Error() != expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
}
in = `
routing_key: 'test'
custom_fields:
my_special_field: 'special_label'
`
err = yaml.UnmarshalStrict([]byte(in), &cfg)
expected = "special_label"
if err != nil {
t.Fatalf("Unexpected error returned, got:\n%v", err.Error())
}
val, ok := cfg.CustomFields["my_special_field"]
if !ok {
t.Fatalf("Expected Custom Field to have value %v set, field is empty", expected)
}
if val != expected {
t.Errorf("\nexpected custom field my_special_field value:\n%v\ngot:\n%v", expected, val)
}
}
func TestPushoverUserKeyIsPresent(t *testing.T) {
in := `
user_key: ''
`
var cfg PushoverConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
expected := "missing user key in Pushover config"
if err == nil {
t.Fatalf("no error returned, expected:\n%v", expected)
}
if err.Error() != expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
}
}
func TestPushoverTokenIsPresent(t *testing.T) {
in := `
user_key: '<user_key>'
token: ''
`
var cfg PushoverConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
expected := "missing token in Pushover config"
if err == nil {
t.Fatalf("no error returned, expected:\n%v", expected)
}
if err.Error() != expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
}
}
func TestLoadSlackConfiguration(t *testing.T) {
tests := []struct {
in string
expected SlackConfig
}{
{
in: `
color: green
username: mark
channel: engineering
title_link: http://example.com/
image_url: https://example.com/logo.png
`,
expected: SlackConfig{
Color: "green", Username: "mark", Channel: "engineering",
TitleLink: "http://example.com/",
ImageURL: "https://example.com/logo.png",
},
},
{
in: `
color: green
username: mark
channel: alerts
title_link: http://example.com/alert1
mrkdwn_in:
- pretext
- text
`,
expected: SlackConfig{
Color: "green", Username: "mark", Channel: "alerts",
MrkdwnIn: []string{"pretext", "text"}, TitleLink: "http://example.com/alert1",
},
},
}
for _, rt := range tests {
var cfg SlackConfig
err := yaml.UnmarshalStrict([]byte(rt.in), &cfg)
if err != nil {
t.Fatalf("\nerror returned when none expected, error:\n%v", err)
}
if rt.expected.Color != cfg.Color {
t.Errorf("\nexpected:\n%v\ngot:\n%v", rt.expected.Color, cfg.Color)
}
if rt.expected.Username != cfg.Username {
t.Errorf("\nexpected:\n%v\ngot:\n%v", rt.expected.Username, cfg.Username)
}
if rt.expected.Channel != cfg.Channel {
t.Errorf("\nexpected:\n%v\ngot:\n%v", rt.expected.Channel, cfg.Channel)
}
if rt.expected.ThumbURL != cfg.ThumbURL {
t.Errorf("\nexpected:\n%v\ngot:\n%v", rt.expected.ThumbURL, cfg.ThumbURL)
}
if rt.expected.TitleLink != cfg.TitleLink {
t.Errorf("\nexpected:\n%v\ngot:\n%v", rt.expected.TitleLink, cfg.TitleLink)
}
if rt.expected.ImageURL != cfg.ImageURL {
t.Errorf("\nexpected:\n%v\ngot:\n%v", rt.expected.ImageURL, cfg.ImageURL)
}
if len(rt.expected.MrkdwnIn) != len(cfg.MrkdwnIn) {
t.Errorf("\nexpected:\n%v\ngot:\n%v", rt.expected.MrkdwnIn, cfg.MrkdwnIn)
}
for i := range cfg.MrkdwnIn {
if rt.expected.MrkdwnIn[i] != cfg.MrkdwnIn[i] {
t.Errorf("\nexpected:\n%v\ngot:\n%v\nat index %v", rt.expected.MrkdwnIn[i], cfg.MrkdwnIn[i], i)
}
}
}
}
func TestSlackFieldConfigValidation(t *testing.T) {
tests := []struct {
in string
expected string
}{
{
in: `
fields:
- title: first
value: hello
- title: second
`,
expected: "missing value in Slack field configuration",
},
{
in: `
fields:
- title: first
value: hello
short: true
- value: world
short: true
`,
expected: "missing title in Slack field configuration",
},
{
in: `
fields:
- title: first
value: hello
short: true
- title: second
value: world
`,
expected: "",
},
}
for _, rt := range tests {
var cfg SlackConfig
err := yaml.UnmarshalStrict([]byte(rt.in), &cfg)
// Check if an error occurred when it was NOT expected to.
if rt.expected == "" && err != nil {
t.Fatalf("\nerror returned when none expected, error:\n%v", err)
}
// Check that an error occurred if one was expected to.
if rt.expected != "" && err == nil {
t.Fatalf("\nno error returned, expected:\n%v", rt.expected)
}
// Check that the error that occurred was what was expected.
if err != nil && err.Error() != rt.expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", rt.expected, err.Error())
}
}
}
func TestSlackFieldConfigUnmarshaling(t *testing.T) {
in := `
fields:
- title: first
value: hello
short: true
- title: second
value: world
- title: third
value: slack field test
short: false
`
expected := []*SlackField{
{
Title: "first",
Value: "hello",
Short: newBoolPointer(true),
},
{
Title: "second",
Value: "world",
Short: nil,
},
{
Title: "third",
Value: "slack field test",
Short: newBoolPointer(false),
},
}
var cfg SlackConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
if err != nil {
t.Fatalf("\nerror returned when none expected, error:\n%v", err)
}
for index, field := range cfg.Fields {
exp := expected[index]
if field.Title != exp.Title {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.Title, field.Title)
}
if field.Value != exp.Value {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.Value, field.Value)
}
if exp.Short == nil && field.Short != nil {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.Short, *field.Short)
}
if exp.Short != nil && field.Short == nil {
t.Errorf("\nexpected:\n%v\ngot:\n%v", *exp.Short, field.Short)
}
if exp.Short != nil && *exp.Short != *field.Short {
t.Errorf("\nexpected:\n%v\ngot:\n%v", *exp.Short, *field.Short)
}
}
}
func TestSlackActionsValidation(t *testing.T) {
in := `
actions:
- type: button
text: hello
url: https://localhost
style: danger
- type: button
text: hello
name: something
style: default
confirm:
title: please confirm
text: are you sure?
ok_text: yes
dismiss_text: no
`
expected := []*SlackAction{
{
Type: "button",
Text: "hello",
URL: "https://localhost",
Style: "danger",
},
{
Type: "button",
Text: "hello",
Name: "something",
Style: "default",
ConfirmField: &SlackConfirmationField{
Title: "please confirm",
Text: "are you sure?",
OkText: "yes",
DismissText: "no",
},
},
}
var cfg SlackConfig
err := yaml.UnmarshalStrict([]byte(in), &cfg)
if err != nil {
t.Fatalf("\nerror returned when none expected, error:\n%v", err)
}
for index, action := range cfg.Actions {
exp := expected[index]
if action.Type != exp.Type {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.Type, action.Type)
}
if action.Text != exp.Text {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.Text, action.Text)
}
if action.URL != exp.URL {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.URL, action.URL)
}
if action.Style != exp.Style {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.Style, action.Style)
}
if action.Name != exp.Name {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.Name, action.Name)
}
if action.Value != exp.Value {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.Value, action.Value)
}
if action.ConfirmField != nil && exp.ConfirmField == nil || action.ConfirmField == nil && exp.ConfirmField != nil {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.ConfirmField, action.ConfirmField)
} else if action.ConfirmField != nil && exp.ConfirmField != nil {
if action.ConfirmField.Title != exp.ConfirmField.Title {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.ConfirmField.Title, action.ConfirmField.Title)
}
if action.ConfirmField.Text != exp.ConfirmField.Text {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.ConfirmField.Text, action.ConfirmField.Text)
}
if action.ConfirmField.OkText != exp.ConfirmField.OkText {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.ConfirmField.OkText, action.ConfirmField.OkText)
}
if action.ConfirmField.DismissText != exp.ConfirmField.DismissText {
t.Errorf("\nexpected:\n%v\ngot:\n%v", exp.ConfirmField.DismissText, action.ConfirmField.DismissText)
}
}
}
}
func TestOpsgenieTypeMatcher(t *testing.T) {
good := []string{"team", "user", "escalation", "schedule"}
for _, g := range good {
if !opsgenieTypeMatcher.MatchString(g) {
t.Fatalf("failed to match with %s", g)
}
}
bad := []string{"0user", "team1", "2escalation3", "sche4dule", "User", "TEAM"}
for _, b := range bad {
if opsgenieTypeMatcher.MatchString(b) {
t.Errorf("mistakenly match with %s", b)
}
}
}
func TestSNS(t *testing.T) {
for _, tc := range []struct {
in string
err bool
}{
{
// Valid configuration without sigv4.
in: `target_arn: target`,
err: false,
},
{
// Valid configuration without sigv4.
in: `topic_arn: topic`,
err: false,
},
{
// Valid configuration with sigv4.
in: `phone_number: phone
sigv4:
access_key: abc
secret_key: abc
`,
err: false,
},
{
// at most one of 'target_arn', 'topic_arn' or 'phone_number' must be provided without sigv4.
in: `topic_arn: topic
target_arn: target
`,
err: true,
},
{
// at most one of 'target_arn', 'topic_arn' or 'phone_number' must be provided without sigv4.
in: `topic_arn: topic
phone_number: phone
`,
err: true,
},
{
// one of 'target_arn', 'topic_arn' or 'phone_number' must be provided without sigv4.
in: "{}",
err: true,
},
{
// one of 'target_arn', 'topic_arn' or 'phone_number' must be provided with sigv4.
in: `sigv4:
access_key: abc
secret_key: abc
`,
err: true,
},
{
// 'secret_key' must be provided with 'access_key'.
in: `topic_arn: topic
sigv4:
access_key: abc
`,
err: true,
},
{
// 'access_key' must be provided with 'secret_key'.
in: `topic_arn: topic
sigv4:
secret_key: abc
`,
err: true,
},
} {
t.Run("", func(t *testing.T) {
var cfg SNSConfig
err := yaml.UnmarshalStrict([]byte(tc.in), &cfg)
if err != nil {
if !tc.err {
t.Errorf("expecting no error, got %q", err)
}
return
}
if tc.err {
t.Logf("%#v", cfg)
t.Error("expecting error, got none")
}
})
}
}
func TestWeChatTypeMatcher(t *testing.T) {
good := []string{"text", "markdown"}
for _, g := range good {
if !wechatTypeMatcher.MatchString(g) {
t.Fatalf("failed to match with %s", g)
}
}
bad := []string{"TEXT", "MarkDOwn"}
for _, b := range bad {
if wechatTypeMatcher.MatchString(b) {
t.Errorf("mistakenly match with %s", b)
}
}
}
func newBoolPointer(b bool) *bool {
return &b
}