driftctl/pkg/remote/aws/cloudfront_distribution_sup...

100 lines
3.2 KiB
Go
Raw Normal View History

2021-02-17 14:33:08 +00:00
package aws
import (
"context"
"testing"
"github.com/aws/aws-sdk-go/aws"
2021-05-31 15:39:16 +00:00
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
2021-05-21 14:09:45 +00:00
testresource "github.com/cloudskiff/driftctl/test/resource"
2021-02-17 14:33:08 +00:00
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/aws/aws-sdk-go/aws/awserr"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/pkg/resource"
2021-05-21 14:09:45 +00:00
2021-02-17 14:33:08 +00:00
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/goldenfile"
testmocks "github.com/cloudskiff/driftctl/test/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestCloudfrontDistributionSupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *repository.MockCloudfrontRepository)
2021-02-17 14:33:08 +00:00
err error
}{
{
test: "no cloudfront distribution",
dirName: "cloudfront_distribution_empty",
mocks: func(client *repository.MockCloudfrontRepository) {
2021-02-17 14:33:08 +00:00
client.On("ListAllDistributions").Return([]*cloudfront.DistributionSummary{}, nil)
},
err: nil,
},
{
test: "one cloudfront distribution",
dirName: "cloudfront_distribution_one",
mocks: func(client *repository.MockCloudfrontRepository) {
2021-02-17 14:33:08 +00:00
client.On("ListAllDistributions").Return([]*cloudfront.DistributionSummary{
{Id: aws.String("E1M9CNS0XSHI19")},
}, nil)
},
err: nil,
},
{
test: "cannot list cloudfront distributions",
dirName: "cloudfront_distribution_empty",
mocks: func(client *repository.MockCloudfrontRepository) {
2021-02-17 14:33:08 +00:00
client.On("ListAllDistributions").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsCloudfrontDistributionResourceType),
},
}
for _, c := range cases {
shouldUpdate := c.dirName == *goldenfile.Update
providerLibrary := terraform.NewProviderLibrary()
supplierLibrary := resource.NewSupplierLibrary()
2021-05-21 14:09:45 +00:00
repo := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(repo)
factory := terraform.NewTerraformResourceFactory(repo)
deserializer := resource.NewDeserializer(factory)
2021-02-17 14:33:08 +00:00
if shouldUpdate {
provider, err := InitTestAwsProvider(providerLibrary)
if err != nil {
t.Fatal(err)
}
supplierLibrary.AddSupplier(NewCloudfrontDistributionSupplier(provider, deserializer, repository.NewCloudfrontRepository(provider.session, cache.New(0))))
2021-02-17 14:33:08 +00:00
}
t.Run(c.test, func(tt *testing.T) {
fakeCloudfront := repository.MockCloudfrontRepository{}
2021-02-17 14:33:08 +00:00
c.mocks(&fakeCloudfront)
provider := testmocks.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
s := &CloudfrontDistributionSupplier{
provider,
2021-05-21 14:09:45 +00:00
deserializer,
2021-02-17 14:33:08 +00:00
&fakeCloudfront,
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
}
got, err := s.Resources()
assert.Equal(tt, c.err, err)
mock.AssertExpectationsForObjects(tt)
2021-05-21 14:09:45 +00:00
test.CtyTestDiff(got, c.dirName, provider, deserializer, shouldUpdate, tt)
2021-02-17 14:33:08 +00:00
})
}
}