Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test coverage for number of retries #4

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions email/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"github.com/stretchr/testify/assert"
)

// Global Testing Variables
var sendMailInvocations int = 0

func TestAccEmail_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
Expand Down Expand Up @@ -61,16 +64,19 @@ func testAccCheckEmailExists(n string) resource.TestCheckFunc {
}
}
func mockSendMailReturn421(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
sendMailInvocations += 1
return errors.New("421 Service not available")
}

// function to test non-421 error
func mockReturn500(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
func mockSendMailReturn500(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
sendMailInvocations += 1
return errors.New("500 Internal Server Error")
}

// email to return no error
func mockSendMailSuccess(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
sendMailInvocations += 1
return nil
}

Expand All @@ -79,37 +85,45 @@ func TestRetryWorkflow(t *testing.T) {
maxRetries := 5
// write the tests
tests := []struct {
name string
sendMailFunc SendMailFunc
expectedErr error
name string
sendMailFunc SendMailFunc
expectedErr error
expectedSendMailInvocations int
}{
{
name: "Retry with 421 error",
sendMailFunc: mockSendMailReturn421,
expectedErr: errors.New("421 Service not available"),
name: "Retry with 421 error",
sendMailFunc: mockSendMailReturn421,
expectedErr: errors.New("421 Service not available"),
expectedSendMailInvocations: maxRetries,
},
{
name: "Success on first try",
sendMailFunc: mockSendMailSuccess,
expectedErr: nil,
name: "Success on first try",
sendMailFunc: mockSendMailSuccess,
expectedErr: nil,
expectedSendMailInvocations: 1,
},
{
name: "Other error",
sendMailFunc: mockReturn500,
expectedErr: errors.New("500 Internal Server Error"),
name: "Other error",
sendMailFunc: mockSendMailReturn500,
expectedErr: errors.New("500 Internal Server Error"),
expectedSendMailInvocations: 1,
},
}
// execute the tests
for _, test := range tests {
// run subtests
t.Run(test.name, func(t *testing.T) {
// set test retries zero before each test
sendMailInvocations = 0
err := sendMail(test.sendMailFunc, maxRetries, "localhost", "2525", "username", "password", "[email protected]", "[email protected]", "message")
if test.expectedErr != nil {
// assert that the errors are equal
assert.EqualError(t, err, test.expectedErr.Error())
} else {
assert.NoError(t, err)
}
// assert testing number invocations regardless
assert.Equal(t, sendMailInvocations, test.expectedSendMailInvocations)
})
}
}
Expand Down