driftctl/pkg/middlewares/aws_default_igw_route_test.go

230 lines
5.7 KiB
Go
Raw Normal View History

2021-01-22 10:32:56 +00:00
package middlewares
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/r3labs/diff/v2"
"github.com/snyk/driftctl/enumeration/resource"
2022-07-21 08:37:03 +00:00
"github.com/snyk/driftctl/pkg/resource/aws"
2021-01-22 10:32:56 +00:00
)
func TestAwsDefaultInternetGatewayRoute_Execute(t *testing.T) {
tests := []struct {
name string
2021-08-09 14:03:04 +00:00
remoteResources []*resource.Resource
resourcesFromState []*resource.Resource
expected []*resource.Resource
2021-01-22 10:32:56 +00:00
}{
{
"default internet gateway route is not ignored when managed by IaC",
2021-08-09 14:03:04 +00:00
[]*resource.Resource{
{
2021-05-21 14:09:45 +00:00
Id: "default-vpc",
Type: aws.AwsDefaultVpcResourceType,
2021-01-22 10:32:56 +00:00
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 11:24:58 +00:00
Id: "default-igw",
Type: aws.AwsInternetGatewayResourceType,
Attrs: &resource.Attributes{
"vpc_id": "default-vpc",
},
2021-01-22 10:32:56 +00:00
},
2021-08-09 14:03:04 +00:00
{
Id: "default-route-table",
Type: aws.AwsDefaultRouteTableResourceType,
Attrs: &resource.Attributes{
"vpc_id": "default-vpc",
},
},
2021-08-09 14:03:04 +00:00
{
Id: "default-igw-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "default-igw",
},
},
2021-08-09 14:03:04 +00:00
{
Id: "dummy-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "local",
},
2021-01-22 10:32:56 +00:00
},
},
2021-08-09 14:03:04 +00:00
[]*resource.Resource{
{
Id: "default-igw-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "default-igw",
},
2021-01-22 10:32:56 +00:00
},
},
2021-08-09 14:03:04 +00:00
[]*resource.Resource{
{
2021-05-21 14:09:45 +00:00
Id: "default-vpc",
Type: aws.AwsDefaultVpcResourceType,
2021-01-22 10:32:56 +00:00
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 11:24:58 +00:00
Id: "default-igw",
Type: aws.AwsInternetGatewayResourceType,
Attrs: &resource.Attributes{
"vpc_id": "default-vpc",
},
2021-01-22 10:32:56 +00:00
},
2021-08-09 14:03:04 +00:00
{
Id: "default-route-table",
Type: aws.AwsDefaultRouteTableResourceType,
Attrs: &resource.Attributes{
"vpc_id": "default-vpc",
},
},
2021-08-09 14:03:04 +00:00
{
Id: "default-igw-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "default-igw",
},
},
2021-08-09 14:03:04 +00:00
{
Id: "dummy-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "local",
},
2021-01-22 10:32:56 +00:00
},
},
},
{
"default internet gateway route is ignored when not managed by IaC",
2021-08-09 14:03:04 +00:00
[]*resource.Resource{
{
2021-05-21 14:09:45 +00:00
Id: "default-vpc",
Type: aws.AwsDefaultVpcResourceType,
2021-01-22 10:32:56 +00:00
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 11:24:58 +00:00
Id: "default-igw",
Type: aws.AwsInternetGatewayResourceType,
Attrs: &resource.Attributes{
"vpc_id": "default-vpc",
},
2021-01-22 10:32:56 +00:00
},
2021-08-09 14:03:04 +00:00
{
Id: "default-route-table",
Type: aws.AwsDefaultRouteTableResourceType,
Attrs: &resource.Attributes{
"vpc_id": "default-vpc",
},
},
2021-08-09 14:03:04 +00:00
{
Id: "default-igw-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "default-igw",
"destination_cidr_block": "0.0.0.0/0",
},
},
2021-08-09 14:03:04 +00:00
{
Id: "default-igw-non-default-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "default-igw",
"destination_cidr_block": "10.0.1.0/24",
},
},
2021-08-09 14:03:04 +00:00
{
Id: "default-igw-default-ipv6-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "default-igw",
"destination_ipv6_cidr_block": "::/0",
},
},
2021-08-09 14:03:04 +00:00
{
Id: "dummy-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "local",
},
2021-01-22 10:32:56 +00:00
},
},
2021-08-09 14:03:04 +00:00
[]*resource.Resource{},
[]*resource.Resource{
{
2021-05-21 14:09:45 +00:00
Id: "default-vpc",
Type: aws.AwsDefaultVpcResourceType,
2021-01-22 10:32:56 +00:00
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 11:24:58 +00:00
Id: "default-igw",
Type: aws.AwsInternetGatewayResourceType,
Attrs: &resource.Attributes{
"vpc_id": "default-vpc",
},
2021-01-22 10:32:56 +00:00
},
2021-08-09 14:03:04 +00:00
{
Id: "default-route-table",
Type: aws.AwsDefaultRouteTableResourceType,
Attrs: &resource.Attributes{
"vpc_id": "default-vpc",
},
},
2021-08-09 14:03:04 +00:00
{
Id: "default-igw-non-default-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "default-igw",
"destination_cidr_block": "10.0.1.0/24",
},
},
2021-08-09 14:03:04 +00:00
{
Id: "default-igw-default-ipv6-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "default-igw",
"destination_ipv6_cidr_block": "::/0",
},
},
2021-08-09 14:03:04 +00:00
{
Id: "dummy-route",
Type: aws.AwsRouteResourceType,
Attrs: &resource.Attributes{
"route_table_id": "default-route-table",
"gateway_id": "local",
},
2021-01-22 10:32:56 +00:00
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := NewAwsDefaultInternetGatewayRoute()
err := m.Execute(&tt.remoteResources, &tt.resourcesFromState)
if err != nil {
t.Fatal(err)
}
changelog, err := diff.Diff(tt.expected, tt.remoteResources)
if err != nil {
t.Fatal(err)
}
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s got = %v, want %v", strings.Join(change.Path, "."), awsutil.Prettify(change.From), awsutil.Prettify(change.To))
}
}
})
}
}