Ensure mute time intervals are unique in config, add associated test

Signed-off-by: Ben Ridley <benridley29@gmail.com>
This commit is contained in:
Ben Ridley 2020-11-24 14:38:32 +11:00
parent ae116cfc26
commit 5152a2fbba
2 changed files with 41 additions and 0 deletions

View File

@ -442,6 +442,9 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
tiNames := make(map[string]struct{})
for _, mt := range c.MuteTimeIntervals {
if _, ok := tiNames[mt.Name]; ok {
return fmt.Errorf("mute time interval %q is not unique", mt.Name)
}
tiNames[mt.Name] = struct{}{}
}
return checkTimeInterval(c.Route, tiNames)

View File

@ -210,6 +210,44 @@ route:
}
func TestMuteTimeNoDuplicates(t *testing.T) {
in := `
mute_time_intervals:
- name: duplicate
time_intervals:
- times:
- start_time: '09:00'
end_time: '17:00'
- name: duplicate
time_intervals:
- times:
- start_time: '10:00'
end_time: '14:00'
receivers:
- name: 'team-X-mails'
route:
receiver: 'team-X-mails'
routes:
- match:
severity: critical
mute_times:
- business_hours
`
_, err := Load(in)
expected := "mute time interval \"duplicate\" is not unique"
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 TestGroupByHasNoDuplicatedLabels(t *testing.T) {
in := `
route: