This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
forked from moira-alert/moira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatatypes_test.go
686 lines (625 loc) · 22.9 KB
/
datatypes_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
package moira
import (
"fmt"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestIsScheduleAllows(t *testing.T) {
allDaysExcludedSchedule := ScheduleData{
TimezoneOffset: -300, // TimeZone: Asia/Ekaterinburg
StartOffset: 0, // 00:00
EndOffset: 1439, // 23:59
Days: []ScheduleDataDay{
{
Name: "Mon",
Enabled: false,
},
{
Name: "Tue",
Enabled: false,
},
{
Name: "Wed",
Enabled: false,
},
{
Name: "Thu",
Enabled: false,
},
{
Name: "Fri",
Enabled: false,
},
{
Name: "Sat",
Enabled: false,
},
{
Name: "Sun",
Enabled: false,
},
},
}
// Date Format: dd/mm/yyyy 24h:MM
// 367980 - 05/01/1970 18:13 Mon - 23:13 (YEKT)
// 454380 - 06/01/1970 18:13 Tue - 23:13 (YEKT)
Convey("No schedule", t, func() {
var noSchedule *ScheduleData
So(noSchedule.IsScheduleAllows(367980), ShouldBeTrue)
})
Convey("Full schedule", t, func() {
schedule := getDefaultSchedule()
So(schedule.IsScheduleAllows(367980), ShouldBeTrue)
})
Convey("Exclude monday", t, func() {
schedule := getDefaultSchedule()
schedule.Days[0].Enabled = false
So(schedule.IsScheduleAllows(367980), ShouldBeFalse)
So(schedule.IsScheduleAllows(454380), ShouldBeTrue)
So(schedule.IsScheduleAllows(367980+86400*2), ShouldBeTrue)
})
Convey("Exclude all days", t, func() {
schedule := allDaysExcludedSchedule
So(schedule.IsScheduleAllows(367980), ShouldBeFalse)
So(schedule.IsScheduleAllows(454380), ShouldBeFalse)
So(schedule.IsScheduleAllows(367980+86400*5), ShouldBeFalse)
})
Convey("Include only morning", t, func() {
schedule := getDefaultSchedule() // TimeZone: Asia/Ekaterinburg (YEKT)
schedule.StartOffset = 60 // 01:00
schedule.EndOffset = 540 // 09:00
So(schedule.IsScheduleAllows(86400+129*60), ShouldBeTrue) // 02/01/1970 2:09 - 02/01/1970 07:09 (YEKT)
So(schedule.IsScheduleAllows(86400-239*60), ShouldBeTrue) // 01/01/1970 20:01 - 02/01/1970 01:01 (YEKT)
So(schedule.IsScheduleAllows(86400-241*60), ShouldBeFalse) // 01/01/1970 19:59 - 02/01/1970 00:59 (YEKT)
So(schedule.IsScheduleAllows(86400+541*60), ShouldBeFalse) // 02/01/1970 9:01 - 02/01/1970 14:01 (YEKT)
So(schedule.IsScheduleAllows(86400-255*60), ShouldBeFalse) // 01/01/1970 19:45 - 02/01/1970 00:45 (YEKT)
})
Convey("Check border cases", t, func() {
schedule := getDefaultSchedule() // TimeZone: Asia/Ekaterinburg (YEKT)
So(schedule.IsScheduleAllows(68400), ShouldBeTrue) // 02/01/1970 00:00:00 (YEKT)
So(schedule.IsScheduleAllows(68401), ShouldBeTrue) // 02/01/1970 00:00:01 (YEKT)
So(schedule.IsScheduleAllows(68430), ShouldBeTrue) // 02/01/1970 00:00:30 (YEKT)
So(schedule.IsScheduleAllows(68459), ShouldBeTrue) // 02/01/1970 00:00:59 (YEKT)
So(schedule.IsScheduleAllows(154739), ShouldBeTrue) // 02/01/1970 23:58:59 (YEKT)
So(schedule.IsScheduleAllows(154740), ShouldBeTrue) // 02/01/1970 23:59:00 (YEKT)
So(schedule.IsScheduleAllows(154741), ShouldBeTrue) // 02/01/1970 23:59:01 (YEKT)
So(schedule.IsScheduleAllows(154770), ShouldBeTrue) // 02/01/1970 23:59:30 (YEKT)
So(schedule.IsScheduleAllows(154799), ShouldBeTrue) // 02/01/1970 23:59:59 (YEKT)
})
Convey("Exclude morning", t, func() {
schedule := getDefaultSchedule() // TimeZone: Asia/Ekaterinburg (YEKT)
schedule.StartOffset = 420 // 07:00
schedule.EndOffset = 1439 // 23:59
So(schedule.IsScheduleAllows(86400+129*60), ShouldBeTrue) // 02/01/1970 2:09 - 02/01/1970 07:09 (YEKT)
So(schedule.IsScheduleAllows(86400-239*60), ShouldBeFalse) // 01/01/1970 20:01 - 02/01/1970 01:01 (YEKT)
So(schedule.IsScheduleAllows(86400-242*60), ShouldBeFalse) // 01/01/1970 19:59 - 02/01/1970 00:59 (YEKT)
So(schedule.IsScheduleAllows(86400+541*60), ShouldBeTrue) // 02/01/1970 9:01 - 02/01/1970 14:01 (YEKT)
So(schedule.IsScheduleAllows(86400-255*60), ShouldBeFalse) // 01/01/1970 19:45 - 02/01/1970 00:45 (YEKT)
})
Convey("Exclude 10 minutes between 07:00 and 07:10", t, func() {
schedule := getDefaultSchedule() // TimeZone: Asia/Ekaterinburg (YEKT)
schedule.StartOffset = 430 // 07:10
schedule.EndOffset = 420 // 07:00
So(schedule.IsScheduleAllows(86400+129*60), ShouldBeFalse) // 02/01/1970 2:09 - 02/01/1970 07:09 (YEKT)
So(schedule.IsScheduleAllows(86400-239*60), ShouldBeTrue) // 01/01/1970 20:01 - 02/01/1970 01:01 (YEKT)
So(schedule.IsScheduleAllows(86400-242*60), ShouldBeTrue) // 01/01/1970 19:59 - 02/01/1970 00:59 (YEKT)
So(schedule.IsScheduleAllows(86400+541*60), ShouldBeTrue) // 02/01/1970 9:01 - 02/01/1970 14:01 (YEKT)
So(schedule.IsScheduleAllows(86400-255*60), ShouldBeTrue) // 01/01/1970 19:45 - 02/01/1970 00:45 (YEKT)
})
Convey("Exclude business hours", t, func() {
schedule := getDefaultSchedule() // TimeZone: Asia/Ekaterinburg (YEKT)
schedule.StartOffset = 1200 // 20:00
schedule.EndOffset = 420 // 07:00
So(schedule.IsScheduleAllows(86400+129*60), ShouldBeFalse) // 02/01/1970 2:09 - 02/01/1970 07:09 (YEKT)
So(schedule.IsScheduleAllows(86400-239*60), ShouldBeTrue) // 01/01/1970 20:01 - 02/01/1970 01:01 (YEKT)
So(schedule.IsScheduleAllows(86400-242*60), ShouldBeTrue) // 01/01/1970 19:59 - 02/01/1970 00:59 (YEKT)
So(schedule.IsScheduleAllows(86400+541*60), ShouldBeFalse) // 02/01/1970 9:01 - 02/01/1970 14:01 (YEKT)
So(schedule.IsScheduleAllows(86400-255*60), ShouldBeTrue) // 01/01/1970 19:45 - 02/01/1970 00:45 (YEKT)
})
}
func TestNotificationEvent_CreateMessage(t *testing.T) {
Convey("Test creating message", t, func() {
var (
startTime int64 = 100
startUser = "StartUser"
stopUser = "StopUser"
stopTime int64 = 200
)
Convey("Test: existence message", func() {
message := "Test message"
event := NotificationEvent{Message: &message}
So(event.CreateMessage(nil), ShouldEqual, message)
})
Convey("Test: creating remind message", func() {
message := "This metric has been in bad state for more than 24 hours - please, fix."
var interval int64 = 24
event := NotificationEvent{MessageEventInfo: &EventInfo{Interval: &interval}}
So(event.CreateMessage(nil), ShouldEqual, message)
})
Convey("Test: check for void MaintenanceInfo", func() {
event := NotificationEvent{MessageEventInfo: &EventInfo{}}
So(event.CreateMessage(nil), ShouldEqual, "")
})
Convey("Test: check for void location", func() {
expected := "This metric changed its state during maintenance interval. Maintenance was set at 00:01 01.01.1970."
event := NotificationEvent{MessageEventInfo: &EventInfo{
Maintenance: &MaintenanceInfo{StartTime: &startTime},
}}
So(event.CreateMessage(nil), ShouldEqual, expected)
})
Convey("Test: was set by start user", func() {
expected := "This metric changed its state during maintenance interval. Maintenance was set by StartUser."
event := NotificationEvent{MessageEventInfo: &EventInfo{
Maintenance: &MaintenanceInfo{StartUser: &startUser},
}}
So(event.CreateMessage(nil), ShouldEqual, expected)
})
Convey("Test: removed by stop user and time", func() {
expected := "This metric changed its state during maintenance interval. Maintenance was set by StartUser and removed by StopUser at 00:03 01.01.1970."
event := NotificationEvent{MessageEventInfo: &EventInfo{
Maintenance: &MaintenanceInfo{StartUser: &startUser, StopUser: &stopUser, StopTime: &stopTime},
}}
So(event.CreateMessage(time.UTC), ShouldEqual, expected)
})
})
}
func TestNotificationEvent_GetSubjectState(t *testing.T) {
Convey("Get ERROR state", t, func() {
states := NotificationEvents{{State: StateOK, Values: map[string]float64{"t1": 0}}, {State: StateERROR, Values: map[string]float64{"t1": 1}}}
So(states.GetSubjectState(), ShouldResemble, StateERROR)
So(states[0].String(), ShouldResemble, "TriggerId: , Metric: , Values: 0, OldState: , State: OK, Message: '', Timestamp: 0")
So(states[1].String(), ShouldResemble, "TriggerId: , Metric: , Values: 1, OldState: , State: ERROR, Message: '', Timestamp: 0")
})
}
func TestNotificationEvent_FormatTimestamp(t *testing.T) {
Convey("Test FormatTimestamp", t, func() {
event := NotificationEvent{Timestamp: 150000000}
location, _ := time.LoadLocation("UTC")
location1, _ := time.LoadLocation("Europe/Moscow")
location2, _ := time.LoadLocation("Asia/Yekaterinburg")
So(event.FormatTimestamp(location), ShouldResemble, "02:40")
So(event.FormatTimestamp(location1), ShouldResemble, "05:40")
So(event.FormatTimestamp(location2), ShouldResemble, "07:40")
})
}
func TestNotificationEvent_GetValue(t *testing.T) {
Convey("Test GetMetricsValues", t, func() {
event := NotificationEvent{}
event.Values = make(map[string]float64)
Convey("One target with zero", func() {
event.Values["t1"] = 0
So(event.GetMetricsValues(), ShouldResemble, "0")
})
Convey("One target with short fraction", func() {
event.Values["t1"] = 2.32
So(event.GetMetricsValues(), ShouldResemble, "2.32")
})
Convey("One target with long fraction", func() {
event.Values["t1"] = 2.3222222
So(event.GetMetricsValues(), ShouldResemble, "2.3222222")
})
Convey("Two targets", func() {
event.Values["t2"] = 0.12
event.Values["t1"] = 2.3222222
So(event.GetMetricsValues(), ShouldResemble, "t1: 2.3222222, t2: 0.12")
})
})
}
func TestTriggerData_GetTags(t *testing.T) {
Convey("Test one tag", t, func() {
triggerData := TriggerData{
Tags: []string{"tag1"},
}
So(triggerData.GetTags(), ShouldResemble, "[tag1]")
})
Convey("Test many tags", t, func() {
triggerData := TriggerData{
Tags: []string{"tag1", "tag2", "tag...orNot"},
}
So(triggerData.GetTags(), ShouldResemble, "[tag1][tag2][tag...orNot]")
})
Convey("Test no tags", t, func() {
triggerData := TriggerData{
Tags: make([]string, 0),
}
So(triggerData.GetTags(), ShouldBeEmpty)
})
}
func TestScheduledNotification_GetKey(t *testing.T) {
Convey("Get key", t, func() {
notification := ScheduledNotification{
Contact: ContactData{Type: "email", Value: "[email protected]"},
Event: NotificationEvent{Values: map[string]float64{"t1": 0}, State: StateNODATA, Metric: "my.metric"},
Timestamp: 123456789,
}
So(notification.GetKey(), ShouldResemble, "email:[email protected]::my.metric:NODATA:0:0:0:false:123456789")
})
}
func TestCheckData_GetOrCreateMetricState(t *testing.T) {
Convey("Test no metric", t, func() {
checkData := CheckData{
Metrics: make(map[string]MetricState),
}
So(checkData.GetOrCreateMetricState("my.metric", 12343, false), ShouldResemble, MetricState{State: StateNODATA, Timestamp: 12343})
})
Convey("Test no metric, notifyAboutNew = false", t, func() {
checkData := CheckData{
Metrics: make(map[string]MetricState),
}
So(checkData.GetOrCreateMetricState("my.metric", 12343, true), ShouldResemble, MetricState{State: StateOK, Timestamp: time.Now().Unix(), EventTimestamp: time.Now().Unix()})
})
Convey("Test has metric", t, func() {
metricState := MetricState{Timestamp: 11211}
checkData := CheckData{
Metrics: map[string]MetricState{
"my.metric": metricState,
},
}
So(checkData.GetOrCreateMetricState("my.metric", 12343, false), ShouldResemble, metricState)
})
}
func TestMetricState_GetCheckPoint(t *testing.T) {
Convey("Get check point", t, func() {
metricState := MetricState{Timestamp: 800, EventTimestamp: 700}
So(metricState.GetCheckPoint(120), ShouldEqual, 700)
metricState = MetricState{Timestamp: 830, EventTimestamp: 700}
So(metricState.GetCheckPoint(120), ShouldEqual, 710)
metricState = MetricState{Timestamp: 699, EventTimestamp: 700}
So(metricState.GetCheckPoint(1), ShouldEqual, 700)
})
}
func TestMetricState_GetEventTimestamp(t *testing.T) {
Convey("Get event timestamp", t, func() {
metricState := MetricState{Timestamp: 800, EventTimestamp: 0}
So(metricState.GetEventTimestamp(), ShouldEqual, 800)
metricState = MetricState{Timestamp: 830, EventTimestamp: 700}
So(metricState.GetEventTimestamp(), ShouldEqual, 700)
})
}
func TestTrigger_IsSimple(t *testing.T) {
Convey("Is Simple", t, func() {
trigger := Trigger{
Patterns: []string{"123"},
Targets: []string{"123"},
}
So(trigger.IsSimple(), ShouldBeTrue)
})
Convey("Not simple", t, func() {
triggers := []Trigger{
{Patterns: []string{"123", "1233"}},
{Patterns: []string{"123", "1233"}, Targets: []string{"123", "1233"}},
{Targets: []string{"123", "1233"}},
{Patterns: []string{"123"}, Targets: []string{"123", "1233"}},
{Patterns: []string{"123?"}, Targets: []string{"123"}},
{Patterns: []string{"12*3"}, Targets: []string{"123"}},
{Patterns: []string{"1{23"}, Targets: []string{"123"}},
{Patterns: []string{"[123"}, Targets: []string{"123"}},
{Patterns: []string{"[12*3"}, Targets: []string{"123"}},
}
for _, trigger := range triggers {
So(trigger.IsSimple(), ShouldBeFalse)
}
})
}
func TestCheckData_GetEventTimestamp(t *testing.T) {
Convey("Get event timestamp", t, func() {
checkData := CheckData{Timestamp: 800, EventTimestamp: 0}
So(checkData.GetEventTimestamp(), ShouldEqual, 800)
checkData = CheckData{Timestamp: 830, EventTimestamp: 700}
So(checkData.GetEventTimestamp(), ShouldEqual, 700)
})
}
func TestCheckData_UpdateScore(t *testing.T) {
Convey("Update score", t, func() {
checkData := CheckData{State: StateNODATA}
So(checkData.UpdateScore(), ShouldEqual, 1000)
So(checkData.Score, ShouldEqual, 1000)
checkData = CheckData{
State: StateOK,
Metrics: map[string]MetricState{
"123": {State: StateNODATA},
"321": {State: StateOK},
"345": {State: StateWARN},
},
}
So(checkData.UpdateScore(), ShouldEqual, 1001)
So(checkData.Score, ShouldEqual, 1001)
checkData = CheckData{
State: StateNODATA,
Metrics: map[string]MetricState{
"123": {State: StateNODATA},
"321": {State: StateOK},
"345": {State: StateWARN},
},
}
So(checkData.UpdateScore(), ShouldEqual, 2001)
So(checkData.Score, ShouldEqual, 2001)
})
}
func getDefaultSchedule() ScheduleData {
return ScheduleData{
TimezoneOffset: -300, // TimeZone: Asia/Ekaterinburg
StartOffset: 0, // 00:00
EndOffset: 1439, // 23:59
Days: []ScheduleDataDay{
{
Name: "Mon",
Enabled: true,
},
{
Name: "Tue",
Enabled: true,
},
{
Name: "Wed",
Enabled: true,
},
{
Name: "Thu",
Enabled: true,
},
{
Name: "Fri",
Enabled: true,
},
{
Name: "Sat",
Enabled: true,
},
{
Name: "Sun",
Enabled: true,
},
},
}
}
func TestSubscriptionData_MustIgnore(testing *testing.T) {
type testCase struct {
State State
OldState State
Ignored bool
}
assertIgnored := func(subscription SubscriptionData, eventCase testCase) {
Convey(fmt.Sprintf("%s -> %s", eventCase.OldState, eventCase.State), func() {
event := NotificationEvent{State: eventCase.State, OldState: eventCase.OldState}
actual := subscription.MustIgnore(&event)
So(actual, ShouldEqual, eventCase.Ignored)
})
}
Convey("Has one type of transitions marked to be ignored", testing, func() {
Convey("[TRUE] Send notifications when triggers degraded only", func() {
subscription := SubscriptionData{
Enabled: true,
IgnoreRecoverings: true,
IgnoreWarnings: false,
}
testCases := []testCase{
{StateWARN, StateOK, false},
{StateERROR, StateOK, false},
{StateNODATA, StateOK, false},
{StateERROR, StateWARN, false},
{StateNODATA, StateWARN, false},
{StateNODATA, StateERROR, false},
{StateOK, StateWARN, true},
{StateOK, StateERROR, true},
{StateOK, StateNODATA, true},
{StateWARN, StateERROR, true},
{StateWARN, StateNODATA, true},
{StateERROR, StateNODATA, true},
}
for _, testCase := range testCases {
assertIgnored(subscription, testCase)
}
})
Convey("[TRUE] Do not send WARN notifications", func() {
subscription := SubscriptionData{
Enabled: true,
IgnoreRecoverings: false,
IgnoreWarnings: true,
}
testCases := []testCase{
{StateERROR, StateOK, false},
{StateNODATA, StateOK, false},
{StateERROR, StateWARN, false},
{StateNODATA, StateWARN, false},
{StateNODATA, StateERROR, false},
{StateOK, StateERROR, false},
{StateOK, StateNODATA, false},
{StateWARN, StateERROR, false},
{StateWARN, StateNODATA, false},
{StateERROR, StateNODATA, false},
{StateOK, StateWARN, true},
{StateWARN, StateOK, true},
}
for _, testCase := range testCases {
assertIgnored(subscription, testCase)
}
})
})
Convey("Has both types of transitions marked to be ignored", testing, func() {
subscription := SubscriptionData{
Enabled: true,
IgnoreRecoverings: true,
IgnoreWarnings: true,
}
testCases := []testCase{
{StateERROR, StateOK, false},
{StateNODATA, StateOK, false},
{StateERROR, StateWARN, false},
{StateNODATA, StateWARN, false},
{StateNODATA, StateERROR, false},
{StateOK, StateWARN, true},
{StateWARN, StateOK, true},
{StateOK, StateERROR, true},
{StateOK, StateNODATA, true},
{StateWARN, StateERROR, true},
{StateWARN, StateNODATA, true},
{StateERROR, StateNODATA, true},
}
for _, testCase := range testCases {
assertIgnored(subscription, testCase)
}
})
Convey("Has no types of transitions marked to be ignored", testing, func() {
subscription := SubscriptionData{
Enabled: true,
IgnoreRecoverings: false,
IgnoreWarnings: false,
}
testCases := []testCase{
{StateOK, StateWARN, false},
{StateWARN, StateOK, false},
{StateERROR, StateOK, false},
{StateNODATA, StateOK, false},
{StateERROR, StateWARN, false},
{StateNODATA, StateWARN, false},
{StateNODATA, StateERROR, false},
{StateOK, StateERROR, false},
{StateOK, StateNODATA, false},
{StateWARN, StateNODATA, false},
{StateERROR, StateNODATA, false},
}
for _, testCase := range testCases {
assertIgnored(subscription, testCase)
}
})
}
func TestBuildTriggerURL(t *testing.T) {
Convey("Sender has no moira uri", t, func() {
url := TriggerData{ID: "SomeID"}.GetTriggerURI("")
So(url, ShouldResemble, "/trigger/SomeID")
})
Convey("Sender uri", t, func() {
url := TriggerData{ID: "SomeID"}.GetTriggerURI("https://my-moira.com")
So(url, ShouldResemble, "https://my-moira.com/trigger/SomeID")
})
Convey("Empty trigger", t, func() {
url := TriggerData{}.GetTriggerURI("https://my-moira.com")
So(url, ShouldBeEmpty)
})
}
func TestSetMaintenanceUserAndTime(t *testing.T) {
startMaintenanceUser := "testStartMtUser"
startMaintenanceUserOld := "testStartMtUserOld"
stopMaintenanceUser := "testStopMtUser"
stopMaintenanceUserOld := "testStopMtUserOld"
callTime := int64(3000)
Convey("Test MaintenanceInfo", t, func() {
testStartMaintenance(
"Not MaintenanceInfo, user anonymous.",
MaintenanceInfo{},
"anonymous",
MaintenanceInfo{},
)
testStopMaintenance(
"Not MaintenanceInfo, user anonymous.",
MaintenanceInfo{},
"anonymous",
MaintenanceInfo{},
)
testStartMaintenance(
"Not MaintenanceInfo, user real.",
MaintenanceInfo{},
startMaintenanceUser,
MaintenanceInfo{&startMaintenanceUser, &callTime, nil, nil},
)
testStopMaintenance(
"Not MaintenanceInfo, user real",
MaintenanceInfo{},
stopMaintenanceUser,
MaintenanceInfo{nil, nil, &stopMaintenanceUser, &callTime},
)
testStartMaintenance(
"Set Start in MaintenanceInfo, user anonymous.",
MaintenanceInfo{},
"anonymous",
MaintenanceInfo{},
)
testStopMaintenance(
"Set Start in MaintenanceInfo, user anonymous.",
MaintenanceInfo{},
"anonymous",
MaintenanceInfo{},
)
testStartMaintenance(
"Set Start in MaintenanceInfo, user real.",
MaintenanceInfo{&startMaintenanceUserOld, &callTime, nil, nil},
startMaintenanceUser,
MaintenanceInfo{&startMaintenanceUser, &callTime, nil, nil},
)
testStopMaintenance(
"Set Start in MaintenanceInfo, user real.",
MaintenanceInfo{&startMaintenanceUserOld, &callTime, nil, nil},
stopMaintenanceUser,
MaintenanceInfo{&startMaintenanceUserOld, &callTime, &stopMaintenanceUser, &callTime},
)
testStartMaintenance(
"Set Stop in MaintenanceInfo, user anonymous.",
MaintenanceInfo{nil, nil, &stopMaintenanceUserOld, &callTime},
"anonymous",
MaintenanceInfo{},
)
testStopMaintenance(
"Set Stop in MaintenanceInfo, user anonymous.",
MaintenanceInfo{nil, nil, &stopMaintenanceUserOld, &callTime},
"anonymous",
MaintenanceInfo{},
)
testStartMaintenance(
"Set Stop in MaintenanceInfo, user real.",
MaintenanceInfo{nil, nil, &stopMaintenanceUserOld, &callTime},
startMaintenanceUser,
MaintenanceInfo{&startMaintenanceUser, &callTime, nil, nil},
)
testStopMaintenance(
"Set Stop in MaintenanceInfo, user real.",
MaintenanceInfo{nil, nil, &stopMaintenanceUserOld, &callTime},
stopMaintenanceUser,
MaintenanceInfo{nil, nil, &stopMaintenanceUser, &callTime},
)
testStartMaintenance(
"Set Start and Stop in MaintenanceInfo, user anonymous.",
MaintenanceInfo{&startMaintenanceUserOld, &callTime, &stopMaintenanceUserOld, &callTime},
"anonymous",
MaintenanceInfo{},
)
testStopMaintenance(
"Set Start and Stop in MaintenanceInfo, user anonymous.",
MaintenanceInfo{&startMaintenanceUserOld, &callTime, &stopMaintenanceUserOld, &callTime},
"anonymous",
MaintenanceInfo{&startMaintenanceUserOld, &callTime, nil, nil},
)
testStartMaintenance(
"Set Start and Stop in MaintenanceInfo, user real.",
MaintenanceInfo{&startMaintenanceUserOld, &callTime, &stopMaintenanceUserOld, &callTime},
startMaintenanceUser,
MaintenanceInfo{&startMaintenanceUser, &callTime, nil, nil},
)
testStopMaintenance(
"Set Start and Stop in MaintenanceInfo, user real.",
MaintenanceInfo{&startMaintenanceUserOld, &callTime, &stopMaintenanceUserOld, &callTime},
stopMaintenanceUser,
MaintenanceInfo{&startMaintenanceUserOld, &callTime, &stopMaintenanceUser, &callTime},
)
})
}
func testStartMaintenance(message string, actualInfo MaintenanceInfo, user string, expectedInfo MaintenanceInfo) {
conveyMessage := fmt.Sprintf("%v Start maintenance.", message)
testMaintenance(conveyMessage, actualInfo, 3100, user, expectedInfo)
}
func testStopMaintenance(message string, actualInfo MaintenanceInfo, user string, expectedInfo MaintenanceInfo) {
conveyMessage := fmt.Sprintf("%v Stop maintenance.", message)
testMaintenance(conveyMessage, actualInfo, 0, user, expectedInfo)
}
func testMaintenance(conveyMessage string, actualInfo MaintenanceInfo, maintenance int64, user string, expectedInfo MaintenanceInfo) {
Convey(conveyMessage, func() {
var lastCheckTest = CheckData{
Maintenance: 1000,
}
lastCheckTest.MaintenanceInfo = actualInfo
SetMaintenanceUserAndTime(&lastCheckTest, maintenance, user, 3000)
So(lastCheckTest.MaintenanceInfo, ShouldResemble, expectedInfo)
So(lastCheckTest.Maintenance, ShouldEqual, maintenance)
})
}