Skip to content

Commit

Permalink
Finish simplifying unit tests without framework (#314)
Browse files Browse the repository at this point in the history
Updates the remaining few files in the SDK that were using a testing
framework. This completes the work of removing the testing dependencies
from the SDK.
  • Loading branch information
jasdel authored May 29, 2019
1 parent 65ad503 commit da6472f
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 64 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ get-deps: get-deps-tests get-deps-x-tests get-deps-codegen get-deps-verify

get-deps-tests:
@echo "go get SDK testing dependencies"
go get github.com/stretchr/testify
go get golang.org/x/net/html

get-deps-x-tests:
Expand Down
10 changes: 6 additions & 4 deletions aws/http_request_retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/internal/awstesting/mock"
"github.com/aws/aws-sdk-go-v2/internal/awstesting/unit"

"github.com/stretchr/testify/assert"
)

func TestRequestCancelRetry(t *testing.T) {
Expand All @@ -39,6 +37,10 @@ func TestRequestCancelRetry(t *testing.T) {

err := r.Send()
fmt.Println("request error", err)
assert.True(t, strings.Contains(err.Error(), "canceled"))
assert.Equal(t, 1, reqNum)
if e, a := "canceled", err.Error(); !strings.Contains(a, e) {
t.Errorf("expect %q to be in %q", e, a)
}
if e, a := 1, reqNum; e != a {
t.Errorf("expect %v, got %v", e, a)
}
}
82 changes: 60 additions & 22 deletions aws/offset_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestOffsetReaderRead(t *testing.T) {
Expand All @@ -19,39 +17,63 @@ func TestOffsetReaderRead(t *testing.T) {

n, err := reader.Read(tempBuf)

assert.Equal(t, n, len(buf))
assert.Nil(t, err)
assert.Equal(t, buf, tempBuf)
if e, a := n, len(buf); e != a {
t.Errorf("expect %v, got %v", e, a)
}
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if e, a := buf, tempBuf; !bytes.Equal(e, a) {
t.Errorf("expect %v, got %v", e, a)
}
}

func TestOffsetReaderSeek(t *testing.T) {
buf := []byte("testData")
reader := newOffsetReader(bytes.NewReader(buf), 0)

orig, err := reader.Seek(0, 1)
assert.NoError(t, err)
assert.Equal(t, int64(0), orig)
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if e, a := int64(0), orig; e != a {
t.Errorf("expect %v, got %v", e, a)
}

n, err := reader.Seek(0, 2)
assert.NoError(t, err)
assert.Equal(t, int64(len(buf)), n)
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if e, a := int64(len(buf)), n; e != a {
t.Errorf("expect %v, got %v", e, a)
}

n, err = reader.Seek(orig, 0)
assert.NoError(t, err)
assert.Equal(t, int64(0), n)
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if e, a := int64(0), n; e != a {
t.Errorf("expect %v, got %v", e, a)
}
}

func TestOffsetReaderClose(t *testing.T) {
buf := []byte("testData")
reader := &offsetReader{buf: bytes.NewReader(buf)}

err := reader.Close()
assert.Nil(t, err)
if err != nil {
t.Errorf("expect no error, got %v", err)
}

tempBuf := make([]byte, len(buf))
n, err := reader.Read(tempBuf)
assert.Equal(t, n, 0)
assert.Equal(t, err, io.EOF)
if e, a := 0, n; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := io.EOF, err; e != a {
t.Errorf("expect %v, got %v", e, a)
}
}

func TestOffsetReaderCloseAndCopy(t *testing.T) {
Expand All @@ -62,13 +84,23 @@ func TestOffsetReaderCloseAndCopy(t *testing.T) {
newReader := reader.CloseAndCopy(0)

n, err := reader.Read(tempBuf)
assert.Equal(t, n, 0)
assert.Equal(t, err, io.EOF)
if e, a := 0, n; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := io.EOF, err; e != a {
t.Errorf("expect %v, got %v", e, a)
}

n, err = newReader.Read(tempBuf)
assert.Equal(t, n, len(buf))
assert.Nil(t, err)
assert.Equal(t, buf, tempBuf)
if e, a := n, len(buf); e != a {
t.Errorf("expect %v, got %v", e, a)
}
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if e, a := buf, tempBuf; !bytes.Equal(e, a) {
t.Errorf("expect %v, got %v", e, a)
}
}

func TestOffsetReaderCloseAndCopyOffset(t *testing.T) {
Expand All @@ -78,11 +110,17 @@ func TestOffsetReaderCloseAndCopyOffset(t *testing.T) {

newReader := reader.CloseAndCopy(4)
n, err := newReader.Read(tempBuf)
assert.Equal(t, n, len(buf)-4)
assert.Nil(t, err)
if e, a := n, len(buf)-4; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if err != nil {
t.Errorf("expect no error, got %v", err)
}

expected := []byte{'D', 'a', 't', 'a', 0, 0, 0, 0}
assert.Equal(t, expected, tempBuf)
if e, a := expected, tempBuf; !bytes.Equal(e, a) {
t.Errorf("expect %v, got %v", e, a)
}
}

func TestOffsetReaderRace(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions aws/request_1_6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"errors"
"testing"

"github.com/stretchr/testify/assert"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/awserr"
"github.com/aws/aws-sdk-go-v2/internal/awstesting/unit"
Expand All @@ -29,7 +27,9 @@ func TestRequestInvalidEndpoint(t *testing.T) {
nil,
)

assert.Error(t, r.Error)
if r.Error == nil {
t.Errorf("expect error, got none")
}
}

type timeoutErr struct {
Expand Down
22 changes: 15 additions & 7 deletions aws/static_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package aws

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestStaticCredentialsProviderGet(t *testing.T) {
Expand All @@ -16,10 +14,18 @@ func TestStaticCredentialsProviderGet(t *testing.T) {
}

creds, err := s.Retrieve()
assert.Nil(t, err, "Expect no error")
assert.Equal(t, "AKID", creds.AccessKeyID, "Expect access key ID to match")
assert.Equal(t, "SECRET", creds.SecretAccessKey, "Expect secret access key to match")
assert.Empty(t, creds.SessionToken, "Expect no session token")
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if e, a := "AKID", creds.AccessKeyID; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := "SECRET", creds.SecretAccessKey; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if l := creds.SessionToken; len(l) != 0 {
t.Errorf("expect no token, got %v", l)
}
}

func TestStaticCredentialsProviderIsExpired(t *testing.T) {
Expand All @@ -31,5 +37,7 @@ func TestStaticCredentialsProviderIsExpired(t *testing.T) {
},
}

assert.False(t, s.IsExpired(), "Expect static credentials to never expire")
if s.IsExpired() {
t.Errorf("expect static credentials to never expire")
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
github.com/stretchr/testify v1.2.2 // indirect
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc
google.golang.org/appengine v1.2.0 // indirect
)
41 changes: 31 additions & 10 deletions private/protocol/idempotency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package protocol_test

import (
"reflect"
"strconv"
"testing"

"github.com/aws/aws-sdk-go-v2/private/protocol"
"github.com/stretchr/testify/assert"
)

func TestCanSetIdempotencyToken(t *testing.T) {
Expand Down Expand Up @@ -52,10 +52,14 @@ func TestCanSetIdempotencyToken(t *testing.T) {
}

for i, c := range cases {
v := reflect.Indirect(reflect.ValueOf(c.Case))
ty := v.Type()
canSet := protocol.CanSetIdempotencyToken(v.Field(0), ty.Field(0))
assert.Equal(t, c.CanSet, canSet, "Expect case %d can set to match", i)
t.Run(strconv.Itoa(i), func(t *testing.T) {
v := reflect.Indirect(reflect.ValueOf(c.Case))
ty := v.Type()
canSet := protocol.CanSetIdempotencyToken(v.Field(0), ty.Field(0))
if e, a := c.CanSet, canSet; e != a {
t.Errorf("expect %v can set, got %v", e, a)
}
})
}
}

Expand Down Expand Up @@ -86,21 +90,38 @@ func TestSetIdempotencyToken(t *testing.T) {
}

for i, c := range cases {
v := reflect.Indirect(reflect.ValueOf(c.Case))
t.Run(strconv.Itoa(i), func(t *testing.T) {
v := reflect.Indirect(reflect.ValueOf(c.Case))

protocol.SetIdempotencyToken(v.Field(0))
assert.NotEmpty(t, v.Field(0).Interface(), "Expect case %d to be set", i)
protocol.SetIdempotencyToken(v.Field(0))
switch tv := v.Field(0).Interface().(type) {
case *string:
if tv == nil || len(*tv) == 0 {
t.Errorf("expect to be set")
}
case string:
if len(tv) == 0 {
t.Errorf("expect to be set")
}
default:
t.Errorf("value is not a string")
}
})
}
}

func TestUUIDVersion4(t *testing.T) {
uuid := protocol.UUIDVersion4(make([]byte, 16))
assert.Equal(t, `00000000-0000-4000-8000-000000000000`, uuid)
if e, a := `00000000-0000-4000-8000-000000000000`, uuid; e != a {
t.Errorf("expect %v uuid, got %v", e, a)
}

b := make([]byte, 16)
for i := 0; i < len(b); i++ {
b[i] = 1
}
uuid = protocol.UUIDVersion4(b)
assert.Equal(t, `01010101-0101-4101-8101-010101010101`, uuid)
if e, a := `01010101-0101-4101-8101-010101010101`, uuid; e != a {
t.Errorf("expect %v uuid, got %v", e, a)
}
}
18 changes: 13 additions & 5 deletions private/protocol/json/jsonutil/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package jsonutil_test

import (
"encoding/json"
"strings"
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/private/protocol/json/jsonutil"
"github.com/stretchr/testify/assert"
)

func S(s string) *string {
Expand Down Expand Up @@ -83,11 +83,19 @@ func TestBuildJSON(t *testing.T) {
for _, test := range jsonTests {
out, err := jsonutil.BuildJSON(test.in)
if test.err != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), test.err)
if err == nil {
t.Fatalf("expect error, got none")
}
if e, a := test.err, err.Error(); !strings.Contains(a, e) {
t.Errorf("expect %q, within %q", e, a)
}
} else {
assert.NoError(t, err)
assert.Equal(t, string(out), test.out)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := test.out, string(out); e != a {
t.Errorf("expect %v output, got %v", e, a)
}
}
}
}
Expand Down
18 changes: 12 additions & 6 deletions private/protocol/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"net/url"
"testing"

"github.com/stretchr/testify/assert"

metadata "github.com/aws/aws-sdk-go-v2/aws"
request "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/internal/awstesting"
Expand Down Expand Up @@ -117,13 +115,21 @@ func checkForLeak(data interface{}, build, fn func(*request.Request), t *testing
fn(req)

if result.errExists {
assert.NotNil(t, req.Error)
if req.Error == nil {
t.Fatalf("expect error, got none")
}
} else {
assert.Nil(t, req.Error)
if req.Error != nil {
t.Fatalf("expect no error, got %v", req.Error)
}
}

assert.Equal(t, reader.Closed, result.closed)
assert.Equal(t, reader.Size, result.size)
if e, a := reader.Closed, result.closed; e != a {
t.Errorf("expect %v closed, got %v", e, a)
}
if e, a := reader.Size, result.size; e != a {
t.Errorf("expect %v size, got %v", e, a)
}
}

func TestJSONRpc(t *testing.T) {
Expand Down
Loading

0 comments on commit da6472f

Please sign in to comment.