-
Notifications
You must be signed in to change notification settings - Fork 3
/
alert_based_sli_test.go
152 lines (147 loc) · 4.8 KB
/
alert_based_sli_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
package shimesaba_test
import (
"fmt"
"testing"
"time"
"github.com/Songmu/flextime"
"github.com/mashiike/shimesaba"
"github.com/stretchr/testify/require"
)
func TestAlertBasedSLI(t *testing.T) {
restore := flextime.Fix(time.Date(2021, time.October, 1, 0, 6, 0, 0, time.UTC))
defer restore()
alerts := shimesaba.Alerts{
shimesaba.NewAlert(
shimesaba.NewMonitor(
"hogera",
"SLO hoge",
"expression",
),
time.Date(2021, time.October, 1, 0, 0, 0, 0, time.UTC),
ptrTime(time.Date(2021, time.October, 1, 0, 3, 0, 0, time.UTC)),
),
shimesaba.NewAlert(
shimesaba.NewMonitor(
"fugara",
"SLO fuga",
"service",
),
time.Date(2021, time.October, 1, 0, 2, 0, 0, time.UTC),
ptrTime(time.Date(2021, time.October, 1, 0, 4, 0, 0, time.UTC)),
),
shimesaba.NewAlert(
shimesaba.NewMonitor(
"fugara",
"SLO fuga",
"service",
),
time.Date(2021, time.October, 1, 0, 3, 0, 0, time.UTC),
ptrTime(time.Date(2021, time.October, 1, 0, 5, 0, 0, time.UTC)),
),
shimesaba.NewAlert(
shimesaba.NewMonitor(
"hogera",
"SLO hoge",
"expression",
),
time.Date(2021, time.October, 1, 0, 5, 0, 0, time.UTC),
nil,
),
}
cases := []struct {
cfg *shimesaba.AlertBasedSLIConfig
expected map[time.Time]bool
}{
{
cfg: &shimesaba.AlertBasedSLIConfig{
MonitorID: "hogera",
},
expected: map[time.Time]bool{
time.Date(2021, time.October, 1, 0, 0, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 1, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 2, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 5, 0, 0, time.UTC): false,
},
},
{
cfg: &shimesaba.AlertBasedSLIConfig{
MonitorNameSuffix: "hoge",
},
expected: map[time.Time]bool{
time.Date(2021, time.October, 1, 0, 0, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 1, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 2, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 5, 0, 0, time.UTC): false,
},
},
{
cfg: &shimesaba.AlertBasedSLIConfig{
MonitorID: "fugara",
},
expected: map[time.Time]bool{
time.Date(2021, time.October, 1, 0, 0, 0, 0, time.UTC): true,
time.Date(2021, time.October, 1, 0, 1, 0, 0, time.UTC): true,
time.Date(2021, time.October, 1, 0, 2, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 3, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 4, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 5, 0, 0, time.UTC): true,
},
},
{
cfg: &shimesaba.AlertBasedSLIConfig{
MonitorNamePrefix: "SLO",
},
expected: map[time.Time]bool{
time.Date(2021, time.October, 1, 0, 0, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 1, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 2, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 3, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 4, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 5, 0, 0, time.UTC): false,
},
},
{
cfg: &shimesaba.AlertBasedSLIConfig{
MonitorNamePrefix: "SLO",
MonitorType: "Expression",
},
expected: map[time.Time]bool{
time.Date(2021, time.October, 1, 0, 0, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 1, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 2, 0, 0, time.UTC): false,
time.Date(2021, time.October, 1, 0, 5, 0, 0, time.UTC): false,
},
},
{
cfg: &shimesaba.AlertBasedSLIConfig{
MonitorNameSuffix: "hoge",
MonitorType: "service",
},
expected: map[time.Time]bool{},
},
}
for i, c := range cases {
t.Run(fmt.Sprintf("case.%d", i), func(t *testing.T) {
obj := shimesaba.NewAlertBasedSLI(c.cfg)
actual, err := obj.EvaluateReliabilities(
time.Minute,
alerts,
time.Date(2021, time.October, 1, 0, 0, 0, 0, time.UTC),
time.Date(2021, time.October, 1, 0, 5, 0, 0, time.UTC),
)
require.NoError(t, err)
expected, _ := shimesaba.NewReliabilities([]*shimesaba.Reliability{
shimesaba.NewReliability(time.Date(2021, time.October, 1, 0, 0, 0, 0, time.UTC), time.Minute, c.expected),
shimesaba.NewReliability(time.Date(2021, time.October, 1, 0, 1, 0, 0, time.UTC), time.Minute, c.expected),
shimesaba.NewReliability(time.Date(2021, time.October, 1, 0, 2, 0, 0, time.UTC), time.Minute, c.expected),
shimesaba.NewReliability(time.Date(2021, time.October, 1, 0, 3, 0, 0, time.UTC), time.Minute, c.expected),
shimesaba.NewReliability(time.Date(2021, time.October, 1, 0, 4, 0, 0, time.UTC), time.Minute, c.expected),
shimesaba.NewReliability(time.Date(2021, time.October, 1, 0, 5, 0, 0, time.UTC), time.Minute, c.expected),
})
require.EqualValues(t, expected, actual)
})
}
}
func ptrTime(t time.Time) *time.Time {
return &t
}