diff --git a/utils/record/record.go b/utils/record/record.go index d6f00bd469..3ad05671b2 100644 --- a/utils/record/record.go +++ b/utils/record/record.go @@ -278,11 +278,22 @@ func (e *EventRecorderAdapter) sendNotifications(object runtime.Object, opts Eve return err } + res, err := notificationsAPI.RunTrigger(trigger, objMap) + if err != nil { + log.Errorf("Failed to execute condition of trigger %s: %v", trigger, err) + return err + } + log.Infof("Trigger %s result: %v", trigger, res) + for _, dest := range destinations { - err = notificationsAPI.Send(objMap, triggerActions[0].Send, dest) - if err != nil { - log.Errorf("notification error: %s", err.Error()) - return err + for _, c := range res { + if c.Triggered == true { + err = notificationsAPI.Send(objMap, triggerActions[0].Send, dest) + if err != nil { + log.Errorf("notification error: %s", err.Error()) + return err + } + } } } return nil diff --git a/utils/record/record_test.go b/utils/record/record_test.go index b3b34e7a3d..931ce42c9b 100644 --- a/utils/record/record_test.go +++ b/utils/record/record_test.go @@ -95,6 +95,12 @@ func TestSendNotifications(t *testing.T) { } mockCtrl := gomock.NewController(t) mockAPI := mocks.NewMockAPI(mockCtrl) + cr := []triggers.ConditionResult{{ + Key: "", + Triggered: true, + Templates: []string{"my-template"}, + }} + mockAPI.EXPECT().RunTrigger(gomock.Any(), gomock.Any()).Return(cr, nil).AnyTimes() mockAPI.EXPECT().Send(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() mockAPI.EXPECT().GetConfig().Return(api.Config{ Triggers: map[string][]triggers.Condition{"on-foo-reason": {triggers.Condition{Send: []string{"my-template"}}}}}).AnyTimes() @@ -194,6 +200,52 @@ func TestSendNotificationsFails(t *testing.T) { t.Run("SendError", func(t *testing.T) { mockCtrl := gomock.NewController(t) mockAPI := mocks.NewMockAPI(mockCtrl) + cr := []triggers.ConditionResult{{ + Key: "", + Triggered: true, + Templates: []string{"my-template"}, + }} + mockAPI.EXPECT().RunTrigger(gomock.Any(), gomock.Any()).Return(cr, nil).AnyTimes() + mockAPI.EXPECT().Send(gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("failed to send")).AnyTimes() + mockAPI.EXPECT().GetConfig().Return(api.Config{ + Triggers: map[string][]triggers.Condition{"on-foo-reason": {triggers.Condition{Send: []string{"my-template"}}}}}).AnyTimes() + apiFactory := &mocks.FakeFactory{Api: mockAPI} + rec := NewFakeEventRecorder() + rec.EventRecorderAdapter.apiFactory = apiFactory + + err := rec.sendNotifications(&r, EventOptions{EventReason: "FooReason"}) + assert.Error(t, err) + }) + + t.Run("GetAPIError", func(t *testing.T) { + apiFactory := &mocks.FakeFactory{Err: errors.New("failed to get API")} + rec := NewFakeEventRecorder() + rec.EventRecorderAdapter.apiFactory = apiFactory + + err := rec.sendNotifications(&r, EventOptions{EventReason: "FooReason"}) + assert.Error(t, err) + }) + +} + +func TestSendNotificationsFailsWithRunTriggerError(t *testing.T) { + r := v1alpha1.Rollout{ + ObjectMeta: metav1.ObjectMeta{ + Name: "guestbook", + Namespace: "default", + Annotations: map[string]string{"notifications.argoproj.io/subscribe.on-foo-reason.console": "console"}, + }, + } + + t.Run("SendError", func(t *testing.T) { + mockCtrl := gomock.NewController(t) + mockAPI := mocks.NewMockAPI(mockCtrl) + cr := []triggers.ConditionResult{{ + Key: "", + Triggered: true, + Templates: []string{"my-template"}, + }} + mockAPI.EXPECT().RunTrigger(gomock.Any(), gomock.Any()).Return(cr, errors.New("fail")).AnyTimes() mockAPI.EXPECT().Send(gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("failed to send")).AnyTimes() mockAPI.EXPECT().GetConfig().Return(api.Config{ Triggers: map[string][]triggers.Condition{"on-foo-reason": {triggers.Condition{Send: []string{"my-template"}}}}}).AnyTimes()