From d90d21eba2f7bcfb5c7bf5379b7873c350287ec5 Mon Sep 17 00:00:00 2001 From: AesthicEthics Date: Thu, 30 May 2024 08:03:16 -0400 Subject: [PATCH 1/2] Added test coverage for number of retries --- email/provider_test.go | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/email/provider_test.go b/email/provider_test.go index 264b1da..b37655a 100644 --- a/email/provider_test.go +++ b/email/provider_test.go @@ -12,6 +12,9 @@ import ( "github.com/stretchr/testify/assert" ) +// Global Testing Variables +var testRetries int = 0 + func TestAccEmail_basic(t *testing.T) { resource.Test(t, resource.TestCase{ Providers: map[string]*schema.Provider{ @@ -61,16 +64,19 @@ func testAccCheckEmailExists(n string) resource.TestCheckFunc { } } func mockSendMailReturn421(addr string, a smtp.Auth, from string, to []string, msg []byte) error { + testRetries += 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 { + testRetries += 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 { + testRetries += 1 return nil } @@ -79,34 +85,41 @@ func TestRetryWorkflow(t *testing.T) { maxRetries := 5 // write the tests tests := []struct { - name string - sendMailFunc SendMailFunc - expectedErr error + name string + sendMailFunc SendMailFunc + expectedErr error + expectedCount 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"), + expectedCount: maxRetries, }, { - name: "Success on first try", - sendMailFunc: mockSendMailSuccess, - expectedErr: nil, + name: "Success on first try", + sendMailFunc: mockSendMailSuccess, + expectedErr: nil, + expectedCount: 1, }, { - name: "Other error", - sendMailFunc: mockReturn500, - expectedErr: errors.New("500 Internal Server Error"), + name: "Other error", + sendMailFunc: mockReturn500, + expectedErr: errors.New("500 Internal Server Error"), + expectedCount: 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 + testRetries = 0 err := sendMail(test.sendMailFunc, maxRetries, "localhost", "2525", "username", "password", "from@example.com", "to@example.com", "message") if test.expectedErr != nil { // assert that the errors are equal assert.EqualError(t, err, test.expectedErr.Error()) + assert.Equal(t, testRetries, test.expectedCount) } else { assert.NoError(t, err) } From 18d81d0fa49d0856881d8bb0a7cba4290715f923 Mon Sep 17 00:00:00 2001 From: AesthicEthics Date: Fri, 31 May 2024 08:32:03 -0400 Subject: [PATCH 2/2] Fixed Nits --- email/provider_test.go | 47 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/email/provider_test.go b/email/provider_test.go index b37655a..ad3cb52 100644 --- a/email/provider_test.go +++ b/email/provider_test.go @@ -13,7 +13,7 @@ import ( ) // Global Testing Variables -var testRetries int = 0 +var sendMailInvocations int = 0 func TestAccEmail_basic(t *testing.T) { resource.Test(t, resource.TestCase{ @@ -64,19 +64,19 @@ func testAccCheckEmailExists(n string) resource.TestCheckFunc { } } func mockSendMailReturn421(addr string, a smtp.Auth, from string, to []string, msg []byte) error { - testRetries += 1 + 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 { - testRetries += 1 +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 { - testRetries += 1 + sendMailInvocations += 1 return nil } @@ -85,28 +85,28 @@ func TestRetryWorkflow(t *testing.T) { maxRetries := 5 // write the tests tests := []struct { - name string - sendMailFunc SendMailFunc - expectedErr error - expectedCount int + name string + sendMailFunc SendMailFunc + expectedErr error + expectedSendMailInvocations int }{ { - name: "Retry with 421 error", - sendMailFunc: mockSendMailReturn421, - expectedErr: errors.New("421 Service not available"), - expectedCount: maxRetries, + 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, - expectedCount: 1, + name: "Success on first try", + sendMailFunc: mockSendMailSuccess, + expectedErr: nil, + expectedSendMailInvocations: 1, }, { - name: "Other error", - sendMailFunc: mockReturn500, - expectedErr: errors.New("500 Internal Server Error"), - expectedCount: 1, + name: "Other error", + sendMailFunc: mockSendMailReturn500, + expectedErr: errors.New("500 Internal Server Error"), + expectedSendMailInvocations: 1, }, } // execute the tests @@ -114,15 +114,16 @@ func TestRetryWorkflow(t *testing.T) { // run subtests t.Run(test.name, func(t *testing.T) { // set test retries zero before each test - testRetries = 0 + sendMailInvocations = 0 err := sendMail(test.sendMailFunc, maxRetries, "localhost", "2525", "username", "password", "from@example.com", "to@example.com", "message") if test.expectedErr != nil { // assert that the errors are equal assert.EqualError(t, err, test.expectedErr.Error()) - assert.Equal(t, testRetries, test.expectedCount) } else { assert.NoError(t, err) } + // assert testing number invocations regardless + assert.Equal(t, sendMailInvocations, test.expectedSendMailInvocations) }) } }