mirror of
https://github.com/prometheus/alertmanager
synced 2025-01-11 16:29:30 +00:00
provider/mesh: add NotificationInfos Get test
This commit is contained in:
parent
d241016f95
commit
cb74e2ad41
@ -80,14 +80,14 @@ func (ni *NotificationInfos) Set(ns ...*types.NotifyInfo) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ni *NotificationInfos) Get(dest string, fps ...model.Fingerprint) ([]*types.NotifyInfo, error) {
|
||||
func (ni *NotificationInfos) Get(recv string, fps ...model.Fingerprint) ([]*types.NotifyInfo, error) {
|
||||
res := make([]*types.NotifyInfo, 0, len(fps))
|
||||
for _, fp := range fps {
|
||||
k := fmt.Sprintf("%x:%s", fp, dest)
|
||||
k := fmt.Sprintf("%s:%s", fp, recv)
|
||||
if e, ok := ni.st.set[k]; ok {
|
||||
res = append(res, &types.NotifyInfo{
|
||||
Alert: fp,
|
||||
Receiver: dest,
|
||||
Receiver: recv,
|
||||
Resolved: e.Resolved,
|
||||
Timestamp: e.Timestamp,
|
||||
})
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/prometheus/alertmanager/types"
|
||||
"github.com/prometheus/common/log"
|
||||
"github.com/prometheus/common/model"
|
||||
"github.com/weaveworks/mesh"
|
||||
)
|
||||
|
||||
@ -238,6 +239,66 @@ func TestNotificationInfosSet(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationInfosGet(t *testing.T) {
|
||||
var (
|
||||
t0 = time.Now()
|
||||
t1 = t0.Add(time.Minute)
|
||||
)
|
||||
type query struct {
|
||||
recv string
|
||||
fps []model.Fingerprint
|
||||
want []*types.NotifyInfo
|
||||
}
|
||||
cases := []struct {
|
||||
state map[string]notificationEntry
|
||||
queries []query
|
||||
}{
|
||||
{
|
||||
state: map[string]notificationEntry{
|
||||
"0000000000000010:recv1": {true, t1},
|
||||
"0000000000000030:recv1": {true, t1},
|
||||
"0000000000000010:recv2": {false, t1},
|
||||
"0000000000000020:recv2": {false, t0},
|
||||
},
|
||||
queries: []query{
|
||||
{
|
||||
recv: "recv1",
|
||||
fps: []model.Fingerprint{0x1000, 0x10, 0x20},
|
||||
want: []*types.NotifyInfo{
|
||||
nil,
|
||||
{
|
||||
Alert: 0x10,
|
||||
Receiver: "recv1",
|
||||
Resolved: true,
|
||||
Timestamp: t1,
|
||||
},
|
||||
nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
recv: "unknown",
|
||||
fps: []model.Fingerprint{0x10, 0x1000},
|
||||
want: []*types.NotifyInfo{nil, nil},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
ni := NewNotificationInfos(log.Base())
|
||||
ni.st = ¬ificationState{set: c.state}
|
||||
|
||||
for _, q := range c.queries {
|
||||
have, err := ni.Get(q.recv, q.fps...)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(have, q.want) {
|
||||
t.Errorf("%v %v expected result %v, got %v", q.recv, q.fps, q.want, have)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// testGossip implements the mesh.Gossip interface. Received broadcast
|
||||
// updates are appended to a list.
|
||||
type testGossip struct {
|
||||
|
Loading…
Reference in New Issue
Block a user