Fix receiver name checking in deep sub-routes

Fixes https://github.com/prometheus/alertmanager/issues/1759

Signed-off-by: Julius Volz <julius.volz@gmail.com>
This commit is contained in:
Julius Volz 2019-03-02 12:33:40 +01:00
parent 8642c0b46e
commit 3a73ca5b65
2 changed files with 33 additions and 5 deletions

View File

@ -435,17 +435,17 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
// checkReceiver returns an error if a node in the routing tree
// references a receiver not in the given map.
func checkReceiver(r *Route, receivers map[string]struct{}) error {
for _, sr := range r.Routes {
if err := checkReceiver(sr, receivers); err != nil {
return err
}
}
if r.Receiver == "" {
return nil
}
if _, ok := receivers[r.Receiver]; !ok {
return fmt.Errorf("undefined receiver %q used in route", r.Receiver)
}
for _, sr := range r.Routes {
if err := checkReceiver(sr, receivers); err != nil {
return err
}
}
return nil
}

View File

@ -103,6 +103,34 @@ receivers:
}
func TestReceiverExistsForDeepSubRoute(t *testing.T) {
in := `
route:
receiver: team-X
routes:
- match:
foo: bar
routes:
- match:
foo: bar
receiver: nonexistent
receivers:
- name: 'team-X'
`
_, err := Load(in)
expected := "undefined receiver \"nonexistent\" used in route"
if err == nil {
t.Fatalf("no error returned, expected:\n%q", expected)
}
if err.Error() != expected {
t.Errorf("\nexpected:\n%q\ngot:\n%q", expected, err.Error())
}
}
func TestReceiverHasName(t *testing.T) {
in := `
route: