driftctl/pkg/remote/aws/internet_gateway_supplier_t...

121 lines
3.7 KiB
Go
Raw Normal View History

2021-01-20 21:54:41 +00:00
package aws
import (
"context"
"testing"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
2021-05-04 16:37:45 +00:00
awstest "github.com/cloudskiff/driftctl/test/aws"
2021-05-21 14:09:45 +00:00
testresource "github.com/cloudskiff/driftctl/test/resource"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
2021-01-20 21:54:41 +00:00
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/pkg/resource"
2021-05-21 14:09:45 +00:00
2021-01-20 21:54:41 +00:00
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/goldenfile"
mocks2 "github.com/cloudskiff/driftctl/test/mocks"
"github.com/stretchr/testify/assert"
2021-01-20 21:54:41 +00:00
"github.com/stretchr/testify/mock"
)
func TestInternetGatewaySupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
2021-05-04 16:37:45 +00:00
mocks func(client *awstest.MockFakeEC2)
2021-01-20 21:54:41 +00:00
err error
}{
{
test: "no internet gateways",
dirName: "internet_gateway_empty",
2021-05-04 16:37:45 +00:00
mocks: func(client *awstest.MockFakeEC2) {
2021-01-20 21:54:41 +00:00
client.On("DescribeInternetGatewaysPages",
&ec2.DescribeInternetGatewaysInput{},
mock.MatchedBy(func(callback func(res *ec2.DescribeInternetGatewaysOutput, lastPage bool) bool) bool {
callback(&ec2.DescribeInternetGatewaysOutput{}, true)
return true
})).Return(nil)
},
err: nil,
},
{
test: "multiple internet gateways",
dirName: "internet_gateway_multiple",
2021-05-04 16:37:45 +00:00
mocks: func(client *awstest.MockFakeEC2) {
2021-01-20 21:54:41 +00:00
client.On("DescribeInternetGatewaysPages",
&ec2.DescribeInternetGatewaysInput{},
mock.MatchedBy(func(callback func(res *ec2.DescribeInternetGatewaysOutput, lastPage bool) bool) bool {
callback(&ec2.DescribeInternetGatewaysOutput{
InternetGateways: []*ec2.InternetGateway{
{
InternetGatewayId: awssdk.String("igw-0184eb41aadc62d1c"),
},
{
InternetGatewayId: awssdk.String("igw-047b487f5c60fca99"),
},
},
}, true)
return true
})).Return(nil)
},
err: nil,
},
{
test: "cannot list internet gateways",
dirName: "internet_gateway_empty",
2021-05-04 16:37:45 +00:00
mocks: func(client *awstest.MockFakeEC2) {
client.On("DescribeInternetGatewaysPages",
&ec2.DescribeInternetGatewaysInput{},
mock.MatchedBy(func(callback func(res *ec2.DescribeInternetGatewaysOutput, lastPage bool) bool) bool {
return true
})).Return(awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsInternetGatewayResourceType),
},
2021-01-20 21:54:41 +00:00
}
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-01-20 21:54:41 +00:00
if shouldUpdate {
provider, err := InitTestAwsProvider(providerLibrary)
2021-01-20 21:54:41 +00:00
if err != nil {
t.Fatal(err)
}
2021-05-21 14:09:45 +00:00
supplierLibrary.AddSupplier(NewInternetGatewaySupplier(provider, deserializer))
2021-01-20 21:54:41 +00:00
}
t.Run(c.test, func(tt *testing.T) {
2021-05-04 16:37:45 +00:00
fakeEC2 := awstest.MockFakeEC2{}
2021-01-20 21:54:41 +00:00
c.mocks(&fakeEC2)
provider := mocks2.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
2021-01-20 21:54:41 +00:00
s := &InternetGatewaySupplier{
provider,
2021-05-21 14:09:45 +00:00
deserializer,
2021-01-20 21:54:41 +00:00
&fakeEC2,
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
}
got, err := s.Resources()
assert.Equal(tt, c.err, err)
2021-01-20 21:54:41 +00:00
mock.AssertExpectationsForObjects(tt)
2021-05-21 14:09:45 +00:00
test.CtyTestDiff(got, c.dirName, provider, deserializer, shouldUpdate, tt)
2021-01-20 21:54:41 +00:00
})
}
}