2021-01-18 14:27:31 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-20 14:54:24 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
2021-01-20 13:01:57 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
|
|
|
|
|
|
|
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
|
|
|
2021-01-18 14:27:31 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
awssdk "github.com/aws/aws-sdk-go/aws"
|
2021-01-20 13:01:57 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2021-01-18 14:27:31 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/parallel"
|
2021-05-20 14:54:24 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/deserializer"
|
2021-01-18 14:27:31 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
2021-05-20 14:54:24 +00:00
|
|
|
awsdeserializer "github.com/cloudskiff/driftctl/pkg/resource/aws/deserializer"
|
2021-01-18 14:27:31 +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"
|
2021-01-20 13:01:57 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-01-18 14:27:31 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRouteTableAssociationSupplier_Resources(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
test string
|
|
|
|
dirName string
|
2021-05-20 14:54:24 +00:00
|
|
|
mocks func(client *repository.MockEC2Repository)
|
2021-01-18 14:27:31 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
test: "no route table associations (test for nil values)",
|
|
|
|
dirName: "route_table_assoc_empty",
|
2021-05-20 14:54:24 +00:00
|
|
|
mocks: func(client *repository.MockEC2Repository) {
|
|
|
|
client.On("ListAllRouteTables").Once().Return([]*ec2.RouteTable{
|
|
|
|
{
|
|
|
|
RouteTableId: awssdk.String("assoc_with_nil"),
|
|
|
|
Associations: []*ec2.RouteTableAssociation{
|
|
|
|
{
|
|
|
|
AssociationState: nil,
|
|
|
|
GatewayId: nil,
|
|
|
|
Main: nil,
|
|
|
|
RouteTableAssociationId: nil,
|
|
|
|
RouteTableId: nil,
|
|
|
|
SubnetId: nil,
|
2021-01-18 14:27:31 +00:00
|
|
|
},
|
2021-05-20 14:54:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RouteTableId: awssdk.String("nil_assoc"),
|
|
|
|
},
|
|
|
|
}, nil)
|
2021-01-18 14:27:31 +00:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: "route_table_association (mixed subnet and gateway associations)",
|
|
|
|
dirName: "route_table_assoc",
|
2021-05-20 14:54:24 +00:00
|
|
|
mocks: func(client *repository.MockEC2Repository) {
|
|
|
|
client.On("ListAllRouteTables").Once().Return([]*ec2.RouteTable{
|
|
|
|
{
|
|
|
|
RouteTableId: aws.String("rtb-05aa6c5673311a17b"), // route
|
|
|
|
Associations: []*ec2.RouteTableAssociation{
|
|
|
|
{ // Should be ignored
|
|
|
|
AssociationState: &ec2.RouteTableAssociationState{
|
|
|
|
State: awssdk.String("disassociated"),
|
|
|
|
},
|
|
|
|
GatewayId: awssdk.String("dummy-id"),
|
|
|
|
},
|
|
|
|
{ // Should be ignored
|
|
|
|
SubnetId: nil,
|
|
|
|
GatewayId: nil,
|
|
|
|
},
|
|
|
|
{ // assoc_route_subnet1
|
|
|
|
AssociationState: &ec2.RouteTableAssociationState{
|
|
|
|
State: awssdk.String("associated"),
|
2021-01-18 14:27:31 +00:00
|
|
|
},
|
2021-05-20 14:54:24 +00:00
|
|
|
Main: awssdk.Bool(false),
|
|
|
|
RouteTableAssociationId: awssdk.String("rtbassoc-0809598f92dbec03b"),
|
|
|
|
RouteTableId: awssdk.String("rtb-05aa6c5673311a17b"),
|
|
|
|
SubnetId: awssdk.String("subnet-05185af647b2eeda3"),
|
|
|
|
},
|
|
|
|
{ // assoc_route_subnet
|
|
|
|
AssociationState: &ec2.RouteTableAssociationState{
|
|
|
|
State: awssdk.String("associated"),
|
|
|
|
},
|
|
|
|
Main: awssdk.Bool(false),
|
|
|
|
RouteTableAssociationId: awssdk.String("rtbassoc-01957791b2cfe6ea4"),
|
|
|
|
RouteTableId: awssdk.String("rtb-05aa6c5673311a17b"),
|
|
|
|
SubnetId: awssdk.String("subnet-0e93dbfa2e5dd8282"),
|
|
|
|
},
|
|
|
|
{ // assoc_route_subnet2
|
|
|
|
AssociationState: &ec2.RouteTableAssociationState{
|
|
|
|
State: awssdk.String("associated"),
|
2021-01-18 14:27:31 +00:00
|
|
|
},
|
2021-05-20 14:54:24 +00:00
|
|
|
GatewayId: nil,
|
|
|
|
Main: awssdk.Bool(false),
|
|
|
|
RouteTableAssociationId: awssdk.String("rtbassoc-0b4f97ea57490e213"),
|
|
|
|
RouteTableId: awssdk.String("rtb-05aa6c5673311a17b"),
|
|
|
|
SubnetId: awssdk.String("subnet-0fd966efd884d0362"),
|
2021-01-18 14:27:31 +00:00
|
|
|
},
|
2021-05-20 14:54:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RouteTableId: aws.String("rtb-09df7cc9d16de9f8f"), // route2
|
|
|
|
Associations: []*ec2.RouteTableAssociation{
|
|
|
|
{ // assoc_route2_gateway
|
|
|
|
AssociationState: &ec2.RouteTableAssociationState{
|
|
|
|
State: awssdk.String("associated"),
|
|
|
|
},
|
|
|
|
RouteTableAssociationId: awssdk.String("rtbassoc-0a79ccacfceb4944b"),
|
|
|
|
RouteTableId: awssdk.String("rtb-09df7cc9d16de9f8f"),
|
|
|
|
GatewayId: awssdk.String("igw-0238f6e09185ac954"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
2021-01-18 14:27:31 +00:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
2021-01-20 13:01:57 +00:00
|
|
|
{
|
|
|
|
test: "Cannot list route table",
|
|
|
|
dirName: "route_table_assoc_empty",
|
2021-05-20 14:54:24 +00:00
|
|
|
mocks: func(client *repository.MockEC2Repository) {
|
|
|
|
client.On("ListAllRouteTables").Once().Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
2021-01-20 13:01:57 +00:00
|
|
|
},
|
|
|
|
err: remoteerror.NewResourceEnumerationErrorWithType(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsRouteTableAssociationResourceType, resourceaws.AwsRouteTableResourceType),
|
|
|
|
},
|
2021-01-18 14:27:31 +00:00
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
shouldUpdate := c.dirName == *goldenfile.Update
|
2021-01-22 17:06:17 +00:00
|
|
|
|
|
|
|
providerLibrary := terraform.NewProviderLibrary()
|
|
|
|
supplierLibrary := resource.NewSupplierLibrary()
|
|
|
|
|
2021-01-18 14:27:31 +00:00
|
|
|
if shouldUpdate {
|
2021-02-09 13:56:11 +00:00
|
|
|
provider, err := InitTestAwsProvider(providerLibrary)
|
2021-01-18 14:27:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-05-20 14:54:24 +00:00
|
|
|
supplierLibrary.AddSupplier(NewRouteTableAssociationSupplier(provider))
|
2021-01-18 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(c.test, func(tt *testing.T) {
|
2021-05-20 14:54:24 +00:00
|
|
|
fakeEC2 := repository.MockEC2Repository{}
|
2021-01-18 14:27:31 +00:00
|
|
|
c.mocks(&fakeEC2)
|
2021-01-22 17:06:17 +00:00
|
|
|
provider := mocks2.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
|
2021-05-20 14:54:24 +00:00
|
|
|
routeTableAssociationDeserializer := awsdeserializer.NewRouteTableAssociationDeserializer()
|
2021-01-18 14:27:31 +00:00
|
|
|
s := &RouteTableAssociationSupplier{
|
|
|
|
provider,
|
2021-05-20 14:54:24 +00:00
|
|
|
routeTableAssociationDeserializer,
|
2021-01-18 14:27:31 +00:00
|
|
|
&fakeEC2,
|
|
|
|
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
|
|
|
|
}
|
|
|
|
got, err := s.Resources()
|
2021-01-20 13:01:57 +00:00
|
|
|
assert.Equal(tt, c.err, err)
|
2021-01-18 14:27:31 +00:00
|
|
|
|
|
|
|
mock.AssertExpectationsForObjects(tt)
|
2021-05-20 14:54:24 +00:00
|
|
|
deserializers := []deserializer.CTYDeserializer{routeTableAssociationDeserializer}
|
|
|
|
test.CtyTestDiffMixed(got, c.dirName, provider, deserializers, shouldUpdate, tt)
|
2021-01-18 14:27:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|