2015-09-25 16:14:46 +00:00
|
|
|
package main
|
2015-07-01 15:56:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-07-01 16:24:08 +00:00
|
|
|
"time"
|
2015-07-01 15:56:53 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"gopkg.in/yaml.v2"
|
2015-09-25 16:14:46 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/config"
|
2015-07-01 15:56:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRouteMatch(t *testing.T) {
|
|
|
|
in := `
|
|
|
|
send_to: 'notify-def'
|
|
|
|
|
|
|
|
routes:
|
|
|
|
- match:
|
|
|
|
owner: 'team-A'
|
|
|
|
|
|
|
|
send_to: 'notify-A'
|
|
|
|
|
2015-07-01 16:24:08 +00:00
|
|
|
routes:
|
|
|
|
- match:
|
|
|
|
env: 'testing'
|
|
|
|
|
|
|
|
send_to: 'notify-testing'
|
|
|
|
|
|
|
|
- match:
|
|
|
|
env: "production"
|
|
|
|
|
|
|
|
send_to: 'notify-productionA'
|
|
|
|
group_wait: 1m
|
|
|
|
|
|
|
|
continue: true
|
|
|
|
|
|
|
|
- match_re:
|
|
|
|
env: "^produ.*$"
|
|
|
|
|
|
|
|
send_to: 'notify-productionB'
|
|
|
|
group_wait: 10m
|
|
|
|
group_by: ['job']
|
|
|
|
|
|
|
|
|
2015-07-01 15:56:53 +00:00
|
|
|
- match_re:
|
|
|
|
owner: '^team-(B|C)$'
|
|
|
|
|
2015-07-01 16:24:08 +00:00
|
|
|
group_by: ['foo', 'bar']
|
|
|
|
group_wait: 2m
|
2015-07-01 15:56:53 +00:00
|
|
|
send_to: 'notify-BC'
|
|
|
|
`
|
|
|
|
|
2015-09-25 16:14:46 +00:00
|
|
|
var ctree config.Route
|
|
|
|
if err := yaml.Unmarshal([]byte(in), &ctree); err != nil {
|
2015-07-01 15:56:53 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-09-25 16:14:46 +00:00
|
|
|
tree := NewRoute(&ctree)
|
|
|
|
|
2015-07-01 16:24:08 +00:00
|
|
|
lset := func(labels ...string) map[model.LabelName]struct{} {
|
|
|
|
s := map[model.LabelName]struct{}{}
|
|
|
|
for _, ls := range labels {
|
|
|
|
s[model.LabelName(ls)] = struct{}{}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:56:53 +00:00
|
|
|
tests := []struct {
|
|
|
|
input model.LabelSet
|
|
|
|
result []*RouteOpts
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"owner": "team-A",
|
|
|
|
},
|
|
|
|
result: []*RouteOpts{
|
|
|
|
{
|
|
|
|
SendTo: "notify-A",
|
2015-07-01 16:24:08 +00:00
|
|
|
GroupBy: lset(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"owner": "team-A",
|
|
|
|
"env": "unset",
|
|
|
|
},
|
|
|
|
result: []*RouteOpts{
|
|
|
|
{
|
|
|
|
SendTo: "notify-A",
|
|
|
|
GroupBy: lset(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"owner": "team-C",
|
|
|
|
},
|
|
|
|
result: []*RouteOpts{
|
|
|
|
{
|
|
|
|
SendTo: "notify-BC",
|
|
|
|
GroupBy: lset("foo", "bar"),
|
2015-09-25 16:14:46 +00:00
|
|
|
GroupWait: 2 * time.Minute,
|
|
|
|
hasWait: true,
|
2015-07-01 16:24:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"owner": "team-A",
|
|
|
|
"env": "testing",
|
|
|
|
},
|
|
|
|
result: []*RouteOpts{
|
|
|
|
{
|
|
|
|
SendTo: "notify-testing",
|
|
|
|
GroupBy: lset(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"owner": "team-A",
|
|
|
|
"env": "production",
|
|
|
|
},
|
|
|
|
result: []*RouteOpts{
|
|
|
|
{
|
|
|
|
SendTo: "notify-productionA",
|
|
|
|
GroupBy: lset(),
|
2015-09-25 16:14:46 +00:00
|
|
|
GroupWait: 1 * time.Minute,
|
|
|
|
hasWait: true,
|
2015-07-01 16:24:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
SendTo: "notify-productionB",
|
|
|
|
GroupBy: lset("job"),
|
2015-09-25 16:14:46 +00:00
|
|
|
GroupWait: 10 * time.Minute,
|
|
|
|
hasWait: true,
|
2015-07-01 15:56:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
matches := tree.Match(test.input)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(matches, test.result) {
|
2015-09-25 16:14:46 +00:00
|
|
|
t.Errorf("\nexpected:\n%v\ngot:\n%v", test.result, matches)
|
2015-07-01 15:56:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|