mirror of
https://github.com/prometheus/alertmanager
synced 2025-01-17 11:40:51 +00:00
Add proper checks whether full batches were received
This commit is contained in:
parent
d40bb99cee
commit
1fcc5d6717
@ -104,8 +104,8 @@ func (t *AcceptanceTest) Collector(name string) *Collector {
|
|||||||
t: t.T,
|
t: t.T,
|
||||||
name: name,
|
name: name,
|
||||||
opts: t.opts,
|
opts: t.opts,
|
||||||
collected: map[float64][]*types.Alert{},
|
collected: map[float64][][]*types.Alert{},
|
||||||
exepected: map[Interval][]*types.Alert{},
|
exepected: map[Interval][][]*types.Alert{},
|
||||||
}
|
}
|
||||||
t.collectors = append(t.collectors, co)
|
t.collectors = append(t.collectors, co)
|
||||||
|
|
||||||
|
@ -108,10 +108,12 @@ func TestBatching(t *testing.T) {
|
|||||||
// until the 5s repeat interval has ended.
|
// until the 5s repeat interval has ended.
|
||||||
co.Want(Between(7, 8.5),
|
co.Want(Between(7, 8.5),
|
||||||
Alert("alertname", "test1").Active(1),
|
Alert("alertname", "test1").Active(1),
|
||||||
|
Alert("alertname", "test5").Active(1),
|
||||||
|
)
|
||||||
|
co.Want(Between(7, 8.5),
|
||||||
Alert("alertname", "test2").Active(1.5),
|
Alert("alertname", "test2").Active(1.5),
|
||||||
Alert("alertname", "test3").Active(1.5),
|
Alert("alertname", "test3").Active(1.5),
|
||||||
Alert("alertname", "test4").Active(1.6),
|
Alert("alertname", "test4").Active(1.6),
|
||||||
Alert("alertname", "test5").Active(1),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
at.Run()
|
at.Run()
|
||||||
|
@ -2,6 +2,7 @@ package test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -15,14 +16,30 @@ type Collector struct {
|
|||||||
name string
|
name string
|
||||||
opts *AcceptanceOpts
|
opts *AcceptanceOpts
|
||||||
|
|
||||||
collected map[float64][]*types.Alert
|
collected map[float64][][]*types.Alert
|
||||||
exepected map[Interval][]*types.Alert
|
exepected map[Interval][][]*types.Alert
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Collector) String() string {
|
func (c *Collector) String() string {
|
||||||
return c.name
|
return c.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func batchesEqual(as, bs []*types.Alert, opts *AcceptanceOpts) bool {
|
||||||
|
if len(as) != len(bs) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(types.AlertTimeline(as))
|
||||||
|
sort.Sort(types.AlertTimeline(bs))
|
||||||
|
|
||||||
|
for i, a := range as {
|
||||||
|
if !equalAlerts(a, bs[i], opts) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// latest returns the latest relative point in time where a notification is
|
// latest returns the latest relative point in time where a notification is
|
||||||
// expected.
|
// expected.
|
||||||
func (c *Collector) latest() float64 {
|
func (c *Collector) latest() float64 {
|
||||||
@ -43,32 +60,36 @@ func (c *Collector) Want(iv Interval, alerts ...*TestAlert) {
|
|||||||
nas = append(nas, a.nativeAlert(c.opts))
|
nas = append(nas, a.nativeAlert(c.opts))
|
||||||
}
|
}
|
||||||
|
|
||||||
c.exepected[iv] = append(c.exepected[iv], nas...)
|
c.exepected[iv] = append(c.exepected[iv], nas)
|
||||||
}
|
}
|
||||||
|
|
||||||
// add the given alerts to the collected alerts.
|
// add the given alerts to the collected alerts.
|
||||||
func (c *Collector) add(alerts ...*types.Alert) {
|
func (c *Collector) add(alerts ...*types.Alert) {
|
||||||
arrival := c.opts.relativeTime(time.Now())
|
arrival := c.opts.relativeTime(time.Now())
|
||||||
|
|
||||||
c.collected[arrival] = append(c.collected[arrival], alerts...)
|
c.collected[arrival] = append(c.collected[arrival], alerts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Collector) check() string {
|
func (c *Collector) check() string {
|
||||||
report := fmt.Sprintf("\nCollector %q:\n\n", c)
|
report := fmt.Sprintf("\ncollector %q:\n\n", c)
|
||||||
|
|
||||||
for iv, expected := range c.exepected {
|
for iv, expected := range c.exepected {
|
||||||
report += fmt.Sprintf("interval %v\n", iv)
|
report += fmt.Sprintf("interval %v\n", iv)
|
||||||
|
|
||||||
for _, exp := range expected {
|
for _, exp := range expected {
|
||||||
var found *types.Alert
|
var found []*types.Alert
|
||||||
report += fmt.Sprintf("- %v ", exp)
|
report += fmt.Sprintf("---\n")
|
||||||
|
|
||||||
|
for _, e := range exp {
|
||||||
|
report += fmt.Sprintf("- %v\n", e)
|
||||||
|
}
|
||||||
|
|
||||||
for at, got := range c.collected {
|
for at, got := range c.collected {
|
||||||
if !iv.contains(at) {
|
if !iv.contains(at) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, a := range got {
|
for _, a := range got {
|
||||||
if equalAlerts(exp, a, c.opts) {
|
if batchesEqual(exp, a, c.opts) {
|
||||||
found = a
|
found = a
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -79,10 +100,10 @@ func (c *Collector) check() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if found != nil {
|
if found != nil {
|
||||||
report += fmt.Sprintf("✓\n")
|
report += fmt.Sprintf(" [ ✓ ]\n")
|
||||||
} else {
|
} else {
|
||||||
c.t.Fail()
|
c.t.Fail()
|
||||||
report += fmt.Sprintf("✗\n")
|
report += fmt.Sprintf(" [ ✗ ]\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,10 +111,17 @@ func (c *Collector) check() string {
|
|||||||
// Detect unexpected notifications.
|
// Detect unexpected notifications.
|
||||||
var totalExp, totalAct int
|
var totalExp, totalAct int
|
||||||
for _, exp := range c.exepected {
|
for _, exp := range c.exepected {
|
||||||
totalExp += len(exp)
|
for _, e := range exp {
|
||||||
|
totalExp += len(e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for _, act := range c.collected {
|
for _, act := range c.collected {
|
||||||
totalAct += len(act)
|
for _, a := range act {
|
||||||
|
if len(a) == 0 {
|
||||||
|
c.t.Error("received empty notifications")
|
||||||
|
}
|
||||||
|
totalAct += len(a)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if totalExp != totalAct {
|
if totalExp != totalAct {
|
||||||
c.t.Fail()
|
c.t.Fail()
|
||||||
@ -104,8 +132,11 @@ func (c *Collector) check() string {
|
|||||||
report += "\nreceived:\n"
|
report += "\nreceived:\n"
|
||||||
|
|
||||||
for at, col := range c.collected {
|
for at, col := range c.collected {
|
||||||
for _, a := range col {
|
for _, alerts := range col {
|
||||||
report += fmt.Sprintf("- %v @ %v\n", a.String(), at)
|
report += fmt.Sprintf("@ %v\n", at)
|
||||||
|
for _, a := range alerts {
|
||||||
|
report += fmt.Sprintf("- %v\n", a)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user