mirror of
https://github.com/prometheus/alertmanager
synced 2024-12-24 15:12:37 +00:00
Merge pull request #1780 from prometheus/fix-receiver-check
Fix receiver name checking in deep sub-routes
This commit is contained in:
commit
0788670f81
@ -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
|
||||
}
|
||||
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user