provider/mesh: add NotificationInfos Get test

This commit is contained in:
Fabian Reinartz 2016-06-08 15:31:12 +02:00
parent d241016f95
commit cb74e2ad41
2 changed files with 64 additions and 3 deletions

View File

@ -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,
})

View File

@ -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 = &notificationState{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 {