-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
collector.go
258 lines (220 loc) · 5.68 KB
/
collector.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
// 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 test
import (
"encoding/json"
"fmt"
"sync"
"testing"
"time"
"github.com/prometheus/alertmanager/api/v2/models"
)
// Collector gathers alerts received by a notification receiver
// and verifies whether all arrived and within the correct time boundaries.
type Collector struct {
t *testing.T
name string
opts *AcceptanceOpts
collected map[float64][]models.GettableAlerts
expected map[Interval][]models.GettableAlerts
mtx sync.RWMutex
}
func (c *Collector) String() string {
return c.name
}
// Collected returns a map of alerts collected by the collector indexed with the
// receive timestamp.
func (c *Collector) Collected() map[float64][]models.GettableAlerts {
c.mtx.RLock()
defer c.mtx.RUnlock()
return c.collected
}
func batchesEqual(as, bs models.GettableAlerts, opts *AcceptanceOpts) bool {
if len(as) != len(bs) {
return false
}
for _, a := range as {
found := false
for _, b := range bs {
if equalAlerts(a, b, opts) {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// latest returns the latest relative point in time where a notification is
// expected.
func (c *Collector) latest() float64 {
c.mtx.RLock()
defer c.mtx.RUnlock()
var latest float64
for iv := range c.expected {
if iv.end > latest {
latest = iv.end
}
}
return latest
}
// Want declares that the Collector expects to receive the given alerts
// within the given time boundaries.
func (c *Collector) Want(iv Interval, alerts ...*TestAlert) {
c.mtx.Lock()
defer c.mtx.Unlock()
var nas models.GettableAlerts
for _, a := range alerts {
nas = append(nas, a.nativeAlert(c.opts))
}
c.expected[iv] = append(c.expected[iv], nas)
}
// add the given alerts to the collected alerts.
func (c *Collector) add(alerts ...*models.GettableAlert) {
c.mtx.Lock()
defer c.mtx.Unlock()
arrival := c.opts.relativeTime(time.Now())
c.collected[arrival] = append(c.collected[arrival], models.GettableAlerts(alerts))
}
func (c *Collector) Check() string {
report := fmt.Sprintf("\ncollector %q:\n\n", c)
c.mtx.RLock()
defer c.mtx.RUnlock()
for iv, expected := range c.expected {
report += fmt.Sprintf("interval %v\n", iv)
var alerts []models.GettableAlerts
for at, got := range c.collected {
if iv.contains(at) {
alerts = append(alerts, got...)
}
}
for _, exp := range expected {
found := len(exp) == 0 && len(alerts) == 0
report += "---\n"
for _, e := range exp {
report += fmt.Sprintf("- %v\n", c.opts.alertString(e))
}
for _, a := range alerts {
if batchesEqual(exp, a, c.opts) {
found = true
break
}
}
if found {
report += " [ ✓ ]\n"
} else {
c.t.Fail()
report += " [ ✗ ]\n"
}
}
}
// Detect unexpected notifications.
var totalExp, totalAct int
for _, exp := range c.expected {
for _, e := range exp {
totalExp += len(e)
}
}
for _, act := range c.collected {
for _, a := range act {
if len(a) == 0 {
c.t.Error("received empty notifications")
}
totalAct += len(a)
}
}
if totalExp != totalAct {
c.t.Fail()
report += fmt.Sprintf("\nExpected total of %d alerts, got %d", totalExp, totalAct)
}
if c.t.Failed() {
report += "\nreceived:\n"
for at, col := range c.collected {
for _, alerts := range col {
report += fmt.Sprintf("@ %v\n", at)
for _, a := range alerts {
report += fmt.Sprintf("- %v\n", c.opts.alertString(a))
}
}
}
}
return report
}
// alertsToString returns a string representation of the given Alerts. Use for
// debugging.
func alertsToString(as []*models.GettableAlert) (string, error) {
b, err := json.Marshal(as)
if err != nil {
return "", err
}
return string(b), nil
}
// CompareCollectors compares two collectors based on their collected alerts
func CompareCollectors(a, b *Collector, opts *AcceptanceOpts) (bool, error) {
f := func(collected map[float64][]models.GettableAlerts) []*models.GettableAlert {
result := []*models.GettableAlert{}
for _, batches := range collected {
for _, batch := range batches {
for _, alert := range batch {
result = append(result, alert)
}
}
}
return result
}
aAlerts := f(a.Collected())
bAlerts := f(b.Collected())
if len(aAlerts) != len(bAlerts) {
aAsString, err := alertsToString(aAlerts)
if err != nil {
return false, err
}
bAsString, err := alertsToString(bAlerts)
if err != nil {
return false, err
}
err = fmt.Errorf(
"first collector has %v alerts, second collector has %v alerts\n%v\n%v",
len(aAlerts), len(bAlerts),
aAsString, bAsString,
)
return false, err
}
for _, aAlert := range aAlerts {
found := false
for _, bAlert := range bAlerts {
if equalAlerts(aAlert, bAlert, opts) {
found = true
break
}
}
if !found {
aAsString, err := alertsToString([]*models.GettableAlert{aAlert})
if err != nil {
return false, err
}
bAsString, err := alertsToString(bAlerts)
if err != nil {
return false, err
}
err = fmt.Errorf(
"could not find matching alert for alert from first collector\n%v\nin alerts of second collector\n%v",
aAsString, bAsString,
)
return false, err
}
}
return true, nil
}