diff --git a/config/config.go b/config/config.go index fdf6cf46fb..05cbf5fdd5 100644 --- a/config/config.go +++ b/config/config.go @@ -516,9 +516,13 @@ func checkTimeInterval(r *Route, timeIntervals map[string]struct{}) error { return err } } - if len(r.MuteTimeIntervals) == 0 { - return nil + + for _, mt := range r.ActiveTimeIntervals { + if _, ok := timeIntervals[mt]; !ok { + return fmt.Errorf("undefined time interval %q used in route", mt) + } } + for _, mt := range r.MuteTimeIntervals { if _, ok := timeIntervals[mt]; !ok { return fmt.Errorf("undefined time interval %q used in route", mt) diff --git a/config/config_test.go b/config/config_test.go index 30a490700a..b0fca3e4cd 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -177,6 +177,32 @@ receivers: } +func TestActiveTimeExists(t *testing.T) { + in := ` +route: + receiver: team-Y + routes: + - match: + severity: critical + active_time_intervals: + - business_hours + +receivers: +- name: 'team-Y' +` + _, err := Load(in) + + expected := "undefined time interval \"business_hours\" 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 TestMuteTimeHasName(t *testing.T) { in := ` mute_time_intervals: diff --git a/notify/notify_test.go b/notify/notify_test.go index b120fd670d..808f7a4f9e 100644 --- a/notify/notify_test.go +++ b/notify/notify_test.go @@ -724,7 +724,8 @@ func TestMuteStageWithSilences(t *testing.T) { } func TestTimeMuteStage(t *testing.T) { - // Route mutes alerts outside business hours. + // Route mutes alerts outside business hours if it is a mute_time_interval + // Route mutes alerts inside business hours if it is an active time interval muteIn := ` --- - weekdays: ['monday:friday'] @@ -736,45 +737,80 @@ func TestTimeMuteStage(t *testing.T) { - weekdays: ['saturday', 'sunday']` cases := []struct { - fireTime string - labels model.LabelSet - shouldMute bool + fireTime string + labels model.LabelSet + setupAsActive bool + shouldMute bool }{ + //{ + // // Friday during business hours + // fireTime: "01 Jan 21 09:00 +0000", + // labels: model.LabelSet{"foo": "bar"}, + // setupAsActive: false, + // shouldMute: false, + //}, + //{ + // // Tuesday before 5pm + // fireTime: "01 Dec 20 16:59 +0000", + // labels: model.LabelSet{"dont": "mute"}, + // setupAsActive: false, + // shouldMute: false, + //}, + //{ + // // Saturday + // fireTime: "17 Oct 20 10:00 +0000", + // labels: model.LabelSet{"mute": "me"}, + // setupAsActive: false, + // shouldMute: true, + //}, + //{ + // // Wednesday before 9am + // fireTime: "14 Oct 20 05:00 +0000", + // labels: model.LabelSet{"mute": "me"}, + // setupAsActive: false, + // shouldMute: true, + //}, + //{ + // // Ensure comparisons are UTC only. 12:00 KST should be muted (03:00 UTC) + // fireTime: "14 Oct 20 12:00 +0900", + // labels: model.LabelSet{"mute": "kst"}, + // setupAsActive: false, + // shouldMute: true, + //}, + //{ + // // Ensure comparisons are UTC only. 22:00 KST should not be muted (13:00 UTC) + // fireTime: "14 Oct 20 22:00 +0900", + // labels: model.LabelSet{"kst": "dont_mute"}, + // setupAsActive: false, + // shouldMute: false, + //}, { // Friday during business hours - fireTime: "01 Jan 21 09:00 +0000", - labels: model.LabelSet{"foo": "bar"}, - shouldMute: false, + fireTime: "01 Jan 21 09:00 +0000", + labels: model.LabelSet{"mute": "me"}, + setupAsActive: true, + shouldMute: true, }, { // Tuesday before 5pm - fireTime: "01 Dec 20 16:59 +0000", - labels: model.LabelSet{"dont": "mute"}, - shouldMute: false, + fireTime: "01 Dec 20 16:59 +0000", + labels: model.LabelSet{"mute": "me"}, + setupAsActive: true, + shouldMute: true, }, { // Saturday - fireTime: "17 Oct 20 10:00 +0000", - labels: model.LabelSet{"mute": "me"}, - shouldMute: true, + fireTime: "17 Oct 20 10:00 +0000", + labels: model.LabelSet{"foo": "bar"}, + setupAsActive: true, + shouldMute: false, }, { // Wednesday before 9am - fireTime: "14 Oct 20 05:00 +0000", - labels: model.LabelSet{"mute": "me"}, - shouldMute: true, - }, - { - // Ensure comparisons are UTC only. 12:00 KST should be muted (03:00 UTC) - fireTime: "14 Oct 20 12:00 +0900", - labels: model.LabelSet{"mute": "kst"}, - shouldMute: true, - }, - { - // Ensure comparisons are UTC only. 22:00 KST should not be muted (13:00 UTC) - fireTime: "14 Oct 20 22:00 +0900", - labels: model.LabelSet{"kst": "dont_mute"}, - shouldMute: false, + fireTime: "14 Oct 20 05:00 +0000", + labels: model.LabelSet{"dont": "mute"}, + setupAsActive: true, + shouldMute: false, }, } var intervals []timeinterval.TimeInterval @@ -800,7 +836,13 @@ func TestTimeMuteStage(t *testing.T) { alerts := []*types.Alert{{Alert: a}} ctx := context.Background() ctx = WithNow(ctx, now) - ctx = WithMuteTimeIntervals(ctx, []string{"test"}) + if tc.setupAsActive { + ctx = WithActiveTimeIntervals(ctx, []string{"test"}) + ctx = WithMuteTimeIntervals(ctx, []string{}) + } else { + ctx = WithActiveTimeIntervals(ctx, []string{}) + ctx = WithMuteTimeIntervals(ctx, []string{"test"}) + } _, out, err := stage.Exec(ctx, log.NewNopLogger(), alerts...) if err != nil {