-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_test.go
102 lines (85 loc) · 2.5 KB
/
message_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
package main
import (
"testing"
)
func TestFormatServicesListStr(t *testing.T) {
services := []string{"service1", "service2"}
expected := " - service1\n - service2\n"
result := formatServicesListStr(services)
if expected != result {
t.Errorf("Expected: %v, got: %v", expected, result)
}
}
func TestParseTemplate(t *testing.T) {
subtests := []struct {
name string
msg *Message
expected string
}{
{
name: "ServicesAreDown",
msg: NewMessage("FMC", []string{"service1", "service2"}, []string{}, 4, "00:00:00"),
expected: `Subject: Connection to FMC services lost
Hello,
connection to the pages/IPs below was lost:
- service1
- service2
Checked at 00:00:00.
Next check will be made after 4 hours.`,
},
{
name: "ServicesAreRecovered",
msg: NewMessage("FMC", []string{}, []string{"service1", "service2"}, 5, "00:00:00"),
expected: `Subject: Connection to FMC services recovered
Hello,
connection to the pages/IPs below was recovered:
- service1
- service2
Checked at 00:00:00.
Next check will be made after 5 hours.`,
},
{
name: "ServicesAreRecoveredAndSomeAreDown",
msg: NewMessage("FMC", []string{"service3", "service4"}, []string{"service1", "service2"}, 10, "00:00:00"),
expected: `Subject: Connection to some FMC services recovered
Hello,
connection to the pages/IPs below was recovered:
- service1
- service2
The following pages are still down:
- service3
- service4
Checked at 00:00:00.
Next check will be made after 10 hours.`,
},
{
name: "ServicesAreNotDownAndNotRecovered",
msg: NewMessage("FMC", []string{}, []string{}, 2, "00:00:00"),
expected: "",
},
}
for _, subtest := range subtests {
t.Run(subtest.name, func(t *testing.T) {
msg, err := ParseTemplate(subtest.msg, messageTemplate)
switch subtest.name {
case "ServicesAreDown":
if msg != subtest.expected {
t.Errorf("expected: %s, got %s", subtest.expected, msg)
}
case "ServicesAreRecovered":
if msg != subtest.expected {
t.Errorf("expected: %s, got %s", subtest.expected, msg)
}
case "ServicesAreRecoveredAndSomeAreDown":
if msg != subtest.expected {
t.Errorf("expected: %s, got %s", subtest.expected, msg)
}
case "ServicesAreNotDownAndNotRecovered":
if msg != subtest.expected && err.Error() != "struct is empty: nothing to parse" {
t.Errorf("expected: %s, got %s", subtest.expected, msg)
}
}
},
)
}
}