-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add retries to sendgrid emailer (#618) Signed-off-by: Katrina Rogan <[email protected]> * wat Signed-off-by: Katrina Rogan <[email protected]> --------- Signed-off-by: Katrina Rogan <[email protected]>
- Loading branch information
Showing
3 changed files
with
156 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,26 @@ | ||
package implementations | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
"github.com/sendgrid/rest" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/flyteorg/flyte/flyteadmin/pkg/async/notifications/mocks" | ||
runtimeInterfaces "github.com/flyteorg/flyte/flyteadmin/pkg/runtime/interfaces" | ||
"github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/admin" | ||
"github.com/flyteorg/flyte/flytestdlib/promutils" | ||
) | ||
|
||
func TestAddresses(t *testing.T) { | ||
addresses := []string{"[email protected]", "[email protected]"} | ||
sgAddresses := getEmailAddresses(addresses) | ||
assert.Equal(t, sgAddresses[0].Address, "[email protected]") | ||
assert.Equal(t, sgAddresses[1].Address, "[email protected]") | ||
} | ||
|
||
func TestGetEmail(t *testing.T) { | ||
emailNotification := &admin.EmailMessage{ | ||
var ( | ||
emailNotification = &admin.EmailMessage{ | ||
SubjectLine: "Notice: Execution \"name\" has succeeded in \"domain\".", | ||
SenderEmail: "[email protected]", | ||
RecipientsEmail: []string{ | ||
|
@@ -32,7 +31,16 @@ func TestGetEmail(t *testing.T) { | |
"<a href=\"https://example.com/executions/T/B/D\">" + | ||
"https://example.com/executions/T/B/D</a>.", | ||
} | ||
) | ||
|
||
func TestAddresses(t *testing.T) { | ||
addresses := []string{"[email protected]", "[email protected]"} | ||
sgAddresses := getEmailAddresses(addresses) | ||
assert.Equal(t, sgAddresses[0].Address, "[email protected]") | ||
assert.Equal(t, sgAddresses[1].Address, "[email protected]") | ||
} | ||
|
||
func TestGetEmail(t *testing.T) { | ||
sgEmail := getSendgridEmail(emailNotification) | ||
assert.Equal(t, `Notice: Execution "name" has succeeded in "domain".`, sgEmail.Personalizations[0].Subject) | ||
assert.Equal(t, "[email protected]", sgEmail.Personalizations[0].To[1].Address) | ||
|
@@ -98,3 +106,63 @@ func TestNoFile(t *testing.T) { | |
// shouldn't reach here | ||
t.Errorf("did not panic") | ||
} | ||
|
||
func TestSendEmail(t *testing.T) { | ||
ctx := context.TODO() | ||
expectedErr := errors.New("expected") | ||
t.Run("exhaust all retry attempts", func(t *testing.T) { | ||
sendgridClient := &mocks.SendgridClient{} | ||
expectedEmail := getSendgridEmail(emailNotification) | ||
sendgridClient.OnSendMatch(expectedEmail). | ||
Return(nil, expectedErr).Times(3) | ||
sendgridClient.OnSendMatch(expectedEmail). | ||
Return(&rest.Response{Body: "email body"}, nil).Once() | ||
scope := promutils.NewScope("bademailer") | ||
emailerMetrics := newEmailMetrics(scope) | ||
|
||
emailer := SendgridEmailer{ | ||
client: sendgridClient, | ||
systemMetrics: emailerMetrics, | ||
cfg: &runtimeInterfaces.NotificationsConfig{ | ||
ReconnectAttempts: 1, | ||
}, | ||
} | ||
|
||
err := emailer.SendEmail(ctx, emailNotification) | ||
assert.EqualError(t, err, expectedErr.Error()) | ||
|
||
assert.NoError(t, testutil.CollectAndCompare(emailerMetrics.SendError, strings.NewReader(` | ||
# HELP bademailer:send_error Number of errors when sending email via Emailer | ||
# TYPE bademailer:send_error counter | ||
bademailer:send_error 1 | ||
`))) | ||
}) | ||
t.Run("exhaust all retry attempts", func(t *testing.T) { | ||
ctx := context.TODO() | ||
sendgridClient := &mocks.SendgridClient{} | ||
expectedEmail := getSendgridEmail(emailNotification) | ||
sendgridClient.OnSendMatch(expectedEmail). | ||
Return(nil, expectedErr).Once() | ||
sendgridClient.OnSendMatch(expectedEmail). | ||
Return(&rest.Response{Body: "email body"}, nil).Once() | ||
scope := promutils.NewScope("goodemailer") | ||
emailerMetrics := newEmailMetrics(scope) | ||
|
||
emailer := SendgridEmailer{ | ||
client: sendgridClient, | ||
systemMetrics: emailerMetrics, | ||
cfg: &runtimeInterfaces.NotificationsConfig{ | ||
ReconnectAttempts: 1, | ||
}, | ||
} | ||
|
||
err := emailer.SendEmail(ctx, emailNotification) | ||
assert.NoError(t, err) | ||
|
||
assert.NoError(t, testutil.CollectAndCompare(emailerMetrics.SendError, strings.NewReader(` | ||
# HELP goodemailer:send_error Number of errors when sending email via Emailer | ||
# TYPE goodemailer:send_error counter | ||
goodemailer:send_error 0 | ||
`))) | ||
}) | ||
} |
56 changes: 56 additions & 0 deletions
56
flyteadmin/pkg/async/notifications/mocks/sendgrid_client.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.