Skip to content

Commit

Permalink
Refactor client testing
Browse files Browse the repository at this point in the history
0. Ran "make gogenerate" again

1. Reword fields in acs client to use "Message_"

2. Rework reflection utils in wsclient to use "Message_"

3. Improve tests to get better output
  • Loading branch information
petderek authored and yhlee-aws committed Nov 1, 2018
1 parent 4963a95 commit 426cc36
Show file tree
Hide file tree
Showing 12 changed files with 665 additions and 118 deletions.
4 changes: 2 additions & 2 deletions agent/acs/client/acs_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func TestConnect(t *testing.T) {
}

func TestConnectClientError(t *testing.T) {
testServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
w.Write([]byte(`{"InvalidClusterException":"Invalid cluster"}` + "\n"))
}))
Expand All @@ -313,7 +313,7 @@ func TestConnectClientError(t *testing.T) {
cs := New(testServer.URL, testCfg, testCreds, rwTimeout)
err := cs.Connect()
_, ok := err.(*wsclient.WSError)
assert.True(t, ok)
assert.True(t, ok, "Connect error expected to be a WSError type")
assert.EqualError(t, err, "InvalidClusterException: Invalid cluster")
}

Expand Down
57 changes: 16 additions & 41 deletions agent/acs/client/acs_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/aws/amazon-ecs-agent/agent/acs/model/ecsacs"
"github.com/stretchr/testify/require"
)

var acsErr *acsError
Expand All @@ -31,70 +32,44 @@ func init() {

func TestInvalidInstanceException(t *testing.T) {
errMsg := "Invalid instance"
err := acsErr.NewError(&ecsacs.InvalidInstanceException{Message: &errMsg})
err := acsErr.NewError(&ecsacs.InvalidInstanceException{Message_: &errMsg})

if err.Retry() {
t.Fatal("Expected InvalidInstanceException to not be retriable")
}

if err.Error() != "InvalidInstanceException: "+errMsg {
t.Fatal("Error string did not match expected: " + err.Error())
}
require.False(t, err.Retry(), "Expected InvalidInstanceException to not be retriable")
require.EqualError(t, err, "InvalidInstanceException: "+errMsg)
}

func TestInvalidClusterException(t *testing.T) {
errMsg := "Invalid cluster"
err := acsErr.NewError(&ecsacs.InvalidClusterException{Message: &errMsg})

if err.Retry() {
t.Fatal("Expected to not be retriable")
}
err := acsErr.NewError(&ecsacs.InvalidClusterException{Message_: &errMsg})

if err.Error() != "InvalidClusterException: "+errMsg {
t.Fatal("Error string did not match expected: " + err.Error())
}
require.False(t, err.Retry(), "Expected to not be retriable")
require.EqualError(t, err, "InvalidClusterException: "+errMsg)
}

func TestServerException(t *testing.T) {
err := acsErr.NewError(&ecsacs.ServerException{Message: nil})
err := acsErr.NewError(&ecsacs.ServerException{Message_: nil})

if !err.Retry() {
t.Fatal("Server exceptions are retriable")
}

if err.Error() != "ServerException: null" {
t.Fatal("Error string did not match expected: " + err.Error())
}
require.True(t, err.Retry(), "Server exceptions are retriable")
require.EqualError(t, err, "ServerException: null")
}

func TestGenericErrorConversion(t *testing.T) {
err := acsErr.NewError(errors.New("generic error"))

if !err.Retry() {
t.Error("Should default to retriable")
}

if err.Error() != "ACSError: generic error" {
t.Error("Did not match expected error: " + err.Error())
}
require.True(t, err.Retry(),"Should default to retriable")
require.EqualError(t, err, "ACSError: generic error")
}

func TestSomeRandomTypeConversion(t *testing.T) {
// This is really just an 'it doesn't panic' check.
err := acsErr.NewError(t)
if !err.Retry() {
t.Error("Should default to retriable")
}
if !strings.HasPrefix(err.Error(), "ACSError: Unknown error") {
t.Error("Expected unknown error")
}
require.True(t, err.Retry(),"Should default to retriable")
require.True(t, strings.HasPrefix(err.Error(), "ACSError: Unknown error"))
}

func TestBadlyTypedMessage(t *testing.T) {
// Another 'does not panic' check
err := acsErr.NewError(struct{ Message int }{1})
if !err.Retry() {
t.Error("Should default to retriable")
}
_ = err.Error()
require.True(t, err.Retry(),"Should default to retriable")
require.NotNil(t, err.Error())
}
Loading

0 comments on commit 426cc36

Please sign in to comment.