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

82 lines
2.3 KiB
Go
Raw Normal View History

2021-02-17 14:33:08 +00:00
package repository
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/service/cloudfront"
2021-05-04 16:37:45 +00:00
awstest "github.com/cloudskiff/driftctl/test/aws"
2021-02-17 14:33:08 +00:00
"github.com/aws/aws-sdk-go/aws"
"github.com/stretchr/testify/mock"
"github.com/r3labs/diff/v2"
"github.com/stretchr/testify/assert"
)
func Test_cloudfrontRepository_ListAllDistributions(t *testing.T) {
tests := []struct {
name string
2021-05-04 16:37:45 +00:00
mocks func(client *awstest.MockFakeCloudFront)
2021-02-17 14:33:08 +00:00
want []*cloudfront.DistributionSummary
wantErr error
}{
{
name: "list multiple distributions",
2021-05-04 16:37:45 +00:00
mocks: func(client *awstest.MockFakeCloudFront) {
2021-02-17 14:33:08 +00:00
client.On("ListDistributionsPages",
&cloudfront.ListDistributionsInput{},
mock.MatchedBy(func(callback func(res *cloudfront.ListDistributionsOutput, lastPage bool) bool) bool {
callback(&cloudfront.ListDistributionsOutput{
DistributionList: &cloudfront.DistributionList{
Items: []*cloudfront.DistributionSummary{
{Id: aws.String("distribution1")},
{Id: aws.String("distribution2")},
{Id: aws.String("distribution3")},
},
},
}, false)
callback(&cloudfront.ListDistributionsOutput{
DistributionList: &cloudfront.DistributionList{
Items: []*cloudfront.DistributionSummary{
{Id: aws.String("distribution4")},
{Id: aws.String("distribution5")},
{Id: aws.String("distribution6")},
},
},
}, true)
return true
})).Return(nil)
},
want: []*cloudfront.DistributionSummary{
{Id: aws.String("distribution1")},
{Id: aws.String("distribution2")},
{Id: aws.String("distribution3")},
{Id: aws.String("distribution4")},
{Id: aws.String("distribution5")},
{Id: aws.String("distribution6")},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-05-04 16:37:45 +00:00
client := awstest.MockFakeCloudFront{}
tt.mocks(&client)
2021-02-17 14:33:08 +00:00
r := &cloudfrontRepository{
2021-05-04 16:37:45 +00:00
client: &client,
2021-02-17 14:33:08 +00:00
}
got, err := r.ListAllDistributions()
assert.Equal(t, tt.wantErr, err)
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()
}
})
}
}