driftctl/pkg/remote/aws/repository/dynamodb_repository_test.go

90 lines
2.1 KiB
Go
Raw Normal View History

2021-02-11 11:21:49 +00:00
package repository
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
2021-02-11 11:21:49 +00:00
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
2021-05-04 16:37:45 +00:00
awstest "github.com/cloudskiff/driftctl/test/aws"
2021-02-11 11:21:49 +00:00
"github.com/stretchr/testify/mock"
"github.com/r3labs/diff/v2"
"github.com/stretchr/testify/assert"
)
func Test_dynamoDBRepository_ListAllTopics(t *testing.T) {
tests := []struct {
name string
2021-05-04 16:37:45 +00:00
mocks func(client *awstest.MockFakeDynamoDB)
2021-02-11 11:21:49 +00:00
want []*string
wantErr error
}{
{
name: "List with 2 pages",
2021-05-04 16:37:45 +00:00
mocks: func(client *awstest.MockFakeDynamoDB) {
2021-02-11 11:21:49 +00:00
client.On("ListTablesPages",
&dynamodb.ListTablesInput{},
mock.MatchedBy(func(callback func(res *dynamodb.ListTablesOutput, lastPage bool) bool) bool {
callback(&dynamodb.ListTablesOutput{
TableNames: []*string{
aws.String("1"),
aws.String("2"),
aws.String("3"),
},
}, false)
callback(&dynamodb.ListTablesOutput{
TableNames: []*string{
aws.String("4"),
aws.String("5"),
aws.String("6"),
},
}, true)
return true
})).Return(nil).Once()
2021-02-11 11:21:49 +00:00
},
want: []*string{
aws.String("1"),
aws.String("2"),
aws.String("3"),
aws.String("4"),
aws.String("5"),
aws.String("6"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := cache.New(1)
2021-05-04 16:37:45 +00:00
client := awstest.MockFakeDynamoDB{}
tt.mocks(&client)
2021-02-11 11:21:49 +00:00
r := &dynamoDBRepository{
2021-05-04 16:37:45 +00:00
client: &client,
cache: store,
2021-02-11 11:21:49 +00:00
}
got, err := r.ListAllTables()
assert.Equal(t, tt.wantErr, err)
if err == nil {
// Check that results were cached
cachedData, err := r.ListAllTables()
assert.NoError(t, err)
assert.Equal(t, got, cachedData)
assert.IsType(t, []*string{}, store.Get("dynamodbListAllTables"))
}
2021-02-11 11:21:49 +00:00
changelog, err := diff.Diff(got, tt.want)
assert.Nil(t, err)
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s: %s -> %s", strings.Join(change.Path, "."), change.From, change.To)
}
t.Fail()
}
})
}
}