mirror of
https://github.com/prometheus/alertmanager
synced 2025-01-01 11:02:07 +00:00
Fix alert batch comparison, improve debug output
This commit is contained in:
parent
ecf6a7b48f
commit
f14ada020a
@ -36,6 +36,13 @@ type AcceptanceOpts struct {
|
||||
baseTime time.Time
|
||||
}
|
||||
|
||||
func (opts *AcceptanceOpts) alertString(a *model.Alert) string {
|
||||
if a.EndsAt.IsZero() {
|
||||
return fmt.Sprintf("%s[%v:]", a, opts.relativeTime(a.StartsAt))
|
||||
}
|
||||
return fmt.Sprintf("%s[%v:%v]", a, opts.relativeTime(a.StartsAt), opts.relativeTime(a.EndsAt))
|
||||
}
|
||||
|
||||
// expandTime returns the absolute time for the relative time
|
||||
// calculated from the test's base time.
|
||||
func (opts *AcceptanceOpts) expandTime(rel float64) time.Time {
|
||||
|
@ -52,15 +52,15 @@ inhibit_rules:
|
||||
am.Push(At(2.2), Alert("alertname", "JobDown", "job", "testjob", "zone", "aa"))
|
||||
|
||||
co.Want(Between(2, 2.5),
|
||||
Alert("alertname", "test1", "job", "testjob", "zone", "aa"),
|
||||
Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa"),
|
||||
Alert("alertname", "InstanceDown", "job", "testjob", "zone", "ab"),
|
||||
Alert("alertname", "test1", "job", "testjob", "zone", "aa").Active(1),
|
||||
Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa").Active(1),
|
||||
Alert("alertname", "InstanceDown", "job", "testjob", "zone", "ab").Active(1),
|
||||
)
|
||||
|
||||
co.Want(Between(3, 3.5),
|
||||
Alert("alertname", "test1", "job", "testjob", "zone", "aa"),
|
||||
Alert("alertname", "InstanceDown", "job", "testjob", "zone", "ab"),
|
||||
Alert("alertname", "JobDown", "job", "testjob", "zone", "aa"),
|
||||
Alert("alertname", "test1", "job", "testjob", "zone", "aa").Active(1),
|
||||
Alert("alertname", "InstanceDown", "job", "testjob", "zone", "ab").Active(1),
|
||||
Alert("alertname", "JobDown", "job", "testjob", "zone", "aa").Active(2.2),
|
||||
)
|
||||
|
||||
at.Run()
|
||||
|
@ -2,7 +2,6 @@ package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -29,12 +28,15 @@ func batchesEqual(as, bs model.Alerts, opts *AcceptanceOpts) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Ensure sorting.
|
||||
sort.Sort(as)
|
||||
sort.Sort(bs)
|
||||
|
||||
for i, a := range as {
|
||||
if !equalAlerts(a, bs[i], opts) {
|
||||
for _, a := range as {
|
||||
found := false
|
||||
for _, b := range bs {
|
||||
if equalAlerts(a, b, opts) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -83,7 +85,7 @@ func (c *Collector) check() string {
|
||||
report += fmt.Sprintf("---\n")
|
||||
|
||||
for _, e := range exp {
|
||||
report += fmt.Sprintf("- %v\n", e)
|
||||
report += fmt.Sprintf("- %v\n", c.opts.alertString(e))
|
||||
}
|
||||
|
||||
for at, got := range c.collected {
|
||||
@ -137,7 +139,7 @@ func (c *Collector) check() string {
|
||||
for _, alerts := range col {
|
||||
report += fmt.Sprintf("@ %v\n", at)
|
||||
for _, a := range alerts {
|
||||
report += fmt.Sprintf("- %v\n", a)
|
||||
report += fmt.Sprintf("- %v\n", c.opts.alertString(a))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user