Skip to content

Commit

Permalink
accumulate errors instead of failfast on multiple notifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
GaruGaru committed Dec 7, 2019
1 parent 4299620 commit f6d6d5c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
6 changes: 4 additions & 2 deletions notifier/notifier.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package notifier

import (
multierror "github.com/hashicorp/go-multierror"
v1 "k8s.io/api/core/v1"
)

Expand All @@ -17,12 +18,13 @@ func New() *Notifiers {
}

func (m *Notifiers) NotifyPodTermination(pod v1.Pod) error {
var result error
for _, n := range m.notifiers {
if err := n.NotifyPodTermination(pod); err != nil {
return err
result = multierror.Append(result, err)
}
}
return nil
return result
}

func (m *Notifiers) Add(notifier Notifier) {
Expand Down
38 changes: 38 additions & 0 deletions notifier/notifier_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package notifier

import (
"fmt"
"github.com/hashicorp/go-multierror"
"testing"

v1 "k8s.io/api/core/v1"
Expand All @@ -14,6 +16,12 @@ type NotifierSuite struct {
testutil.TestSuite
}

type FailingNotifier struct{}

func (f FailingNotifier) NotifyPodTermination(pod v1.Pod) error {
return fmt.Errorf("notify error")
}

func (suite *NotifierSuite) TestMultiNotifierWithoutNotifiers() {
manager := New()
err := manager.NotifyPodTermination(v1.Pod{})
Expand Down Expand Up @@ -44,6 +52,36 @@ func (suite *NotifierSuite) TestMultiNotifierWithMultipleNotifier() {
suite.Equal(1, n2.Calls)
}

func (suite *NotifierSuite) TestMultiNotifierWithNotifierError() {
manager := New()
f := FailingNotifier{}
manager.Add(&f)
err := manager.NotifyPodTermination(v1.Pod{})
suite.Require().Error(err)
}

func (suite *NotifierSuite) TestMultiNotifierWithNotifierMultipleError() {
manager := New()
f0 := FailingNotifier{}
f1 := FailingNotifier{}
manager.Add(&f0)
manager.Add(&f1)
err := manager.NotifyPodTermination(v1.Pod{}).(*multierror.Error)
suite.Require().Error(err)
suite.Require().Len(err.Errors, 2)
}

func (suite *NotifierSuite) TestMultiNotifierWithOneFailingNotifier() {
manager := New()
f := FailingNotifier{}
n := Noop{}
manager.Add(&n)
manager.Add(&f)
err := manager.NotifyPodTermination(v1.Pod{}).(*multierror.Error)
suite.Require().Error(err)
suite.Require().Len(err.Errors, 1)
}

func TestNotifierSuite(t *testing.T) {
suite.Run(t, new(NotifierSuite))
}

0 comments on commit f6d6d5c

Please sign in to comment.