2021-01-14 17:11:02 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awsutil"
|
2021-03-29 16:10:50 +00:00
|
|
|
"github.com/r3labs/diff/v2"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
|
2021-02-03 15:24:55 +00:00
|
|
|
"github.com/cloudskiff/driftctl/mocks"
|
2021-01-14 17:11:02 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-03-29 16:10:50 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
2021-01-14 17:11:02 +00:00
|
|
|
resource2 "github.com/cloudskiff/driftctl/test/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAwsRouteTableExpander_Execute(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
input []resource.Resource
|
|
|
|
expected []resource.Resource
|
2021-03-29 16:10:50 +00:00
|
|
|
mock func(factory *terraform.MockResourceFactory)
|
2021-01-14 17:11:02 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-03-29 16:10:50 +00:00
|
|
|
name: "test with nil route attributes",
|
|
|
|
input: []resource.Resource{
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "table_from_state",
|
|
|
|
Type: aws.AwsRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route": nil,
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
2021-03-29 16:10:50 +00:00
|
|
|
expected: []resource.Resource{
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "table_from_state",
|
|
|
|
Type: aws.AwsRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route": nil,
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-29 16:10:50 +00:00
|
|
|
name: "test with empty route attributes",
|
|
|
|
input: []resource.Resource{
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "table_from_state",
|
|
|
|
Type: aws.AwsRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route": []interface{}{},
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
2021-03-29 16:10:50 +00:00
|
|
|
expected: []resource.Resource{
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
2021-01-14 17:11:02 +00:00
|
|
|
Id: "table_from_state",
|
2021-05-07 18:55:49 +00:00
|
|
|
Type: aws.AwsRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-29 16:10:50 +00:00
|
|
|
name: "test route are expanded",
|
|
|
|
input: []resource.Resource{
|
2021-01-14 17:11:02 +00:00
|
|
|
&resource2.FakeResource{
|
|
|
|
Id: "fake_resource",
|
|
|
|
},
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "table_from_state",
|
|
|
|
Type: aws.AwsRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"cidr_block": "0.0.0.0/0",
|
|
|
|
"ipv6_cidr_block": "",
|
|
|
|
"vpc_endpoint_id": "",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"cidr_block": "",
|
|
|
|
"ipv6_cidr_block": "::/0",
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-29 16:10:50 +00:00
|
|
|
expected: []resource.Resource{
|
2021-01-14 17:11:02 +00:00
|
|
|
&resource2.FakeResource{
|
|
|
|
Id: "fake_resource",
|
|
|
|
},
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
2021-01-14 17:11:02 +00:00
|
|
|
Id: "table_from_state",
|
2021-05-07 18:55:49 +00:00
|
|
|
Type: aws.AwsRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "r-table_from_state1080289494",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_cidr_block": "0.0.0.0/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "r-table_from_state2750132062",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_ipv6_cidr_block": "::/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
2021-03-29 16:10:50 +00:00
|
|
|
mock: func(factory *terraform.MockResourceFactory) {
|
2021-05-07 18:55:49 +00:00
|
|
|
factory.On("CreateAbstractResource", "aws_route", mock.Anything, mock.MatchedBy(func(input map[string]interface{}) bool {
|
|
|
|
return input["id"] == "r-table_from_state1080289494"
|
|
|
|
})).Times(1).Return(&resource.AbstractResource{
|
|
|
|
Id: "r-table_from_state1080289494",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_cidr_block": "0.0.0.0/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
factory.On("CreateAbstractResource", "aws_route", mock.Anything, mock.MatchedBy(func(input map[string]interface{}) bool {
|
|
|
|
return input["id"] == "r-table_from_state2750132062"
|
|
|
|
})).Times(1).Return(&resource.AbstractResource{
|
|
|
|
Id: "r-table_from_state2750132062",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_ipv6_cidr_block": "::/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
|
|
|
}, nil)
|
2021-03-29 16:10:50 +00:00
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
{
|
2021-03-29 16:10:50 +00:00
|
|
|
name: "test route are expanded on default route tables",
|
|
|
|
input: []resource.Resource{
|
2021-01-14 17:11:02 +00:00
|
|
|
&resource2.FakeResource{
|
|
|
|
Id: "fake_resource",
|
|
|
|
},
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "default_route_table_from_state",
|
|
|
|
Type: aws.AwsDefaultRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"cidr_block": "0.0.0.0/0",
|
|
|
|
"ipv6_cidr_block": "",
|
|
|
|
"vpc_endpoint_id": "",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"cidr_block": "",
|
|
|
|
"ipv6_cidr_block": "::/0",
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-29 16:10:50 +00:00
|
|
|
expected: []resource.Resource{
|
2021-01-14 17:11:02 +00:00
|
|
|
&resource2.FakeResource{
|
|
|
|
Id: "fake_resource",
|
|
|
|
},
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
2021-01-14 17:11:02 +00:00
|
|
|
Id: "default_route_table_from_state",
|
2021-05-07 18:55:49 +00:00
|
|
|
Type: aws.AwsDefaultRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "r-default_route_table_from_state1080289494",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "default_route_table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_cidr_block": "0.0.0.0/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "r-default_route_table_from_state2750132062",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "default_route_table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_ipv6_cidr_block": "::/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
|
|
|
},
|
2021-03-29 16:10:50 +00:00
|
|
|
mock: func(factory *terraform.MockResourceFactory) {
|
2021-05-07 18:55:49 +00:00
|
|
|
factory.On("CreateAbstractResource", "aws_route", mock.Anything, mock.MatchedBy(func(input map[string]interface{}) bool {
|
|
|
|
return input["id"] == "r-default_route_table_from_state1080289494"
|
|
|
|
})).Times(1).Return(&resource.AbstractResource{
|
|
|
|
Id: "r-default_route_table_from_state1080289494",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "default_route_table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_cidr_block": "0.0.0.0/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
factory.On("CreateAbstractResource", "aws_route", mock.Anything, mock.MatchedBy(func(input map[string]interface{}) bool {
|
|
|
|
return input["id"] == "r-default_route_table_from_state2750132062"
|
|
|
|
})).Times(1).Return(&resource.AbstractResource{
|
|
|
|
Id: "r-default_route_table_from_state2750132062",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "default_route_table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_ipv6_cidr_block": "::/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
|
|
|
}, nil)
|
2021-03-29 16:10:50 +00:00
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
},
|
2021-04-06 13:16:12 +00:00
|
|
|
{
|
|
|
|
"test routes are expanded from default route tables except when they already exist",
|
|
|
|
[]resource.Resource{
|
|
|
|
&resource2.FakeResource{
|
|
|
|
Id: "fake_resource",
|
|
|
|
},
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "r-default_route_table_from_state2750132062",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "default_route_table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_ipv6_cidr_block": "::/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "default_route_table_from_state",
|
|
|
|
Type: aws.AwsDefaultRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"cidr_block": "0.0.0.0/0",
|
|
|
|
"ipv6_cidr_block": "",
|
|
|
|
"vpc_endpoint_id": "",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"cidr_block": "",
|
|
|
|
"ipv6_cidr_block": "::/0",
|
|
|
|
},
|
2021-04-06 13:16:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]resource.Resource{
|
|
|
|
&resource2.FakeResource{
|
|
|
|
Id: "fake_resource",
|
|
|
|
},
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
2021-04-06 13:16:12 +00:00
|
|
|
Id: "default_route_table_from_state",
|
2021-05-07 18:55:49 +00:00
|
|
|
Type: aws.AwsDefaultRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "r-default_route_table_from_state1080289494",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "default_route_table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_cidr_block": "0.0.0.0/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "r-default_route_table_from_state2750132062",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "default_route_table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_ipv6_cidr_block": "::/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
2021-04-06 13:16:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
func(factory *terraform.MockResourceFactory) {
|
2021-05-07 18:55:49 +00:00
|
|
|
factory.On("CreateAbstractResource", "aws_route", mock.Anything, mock.MatchedBy(func(input map[string]interface{}) bool {
|
|
|
|
return input["id"] == "r-default_route_table_from_state1080289494"
|
|
|
|
})).Times(1).Return(&resource.AbstractResource{
|
|
|
|
Id: "r-default_route_table_from_state1080289494",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "default_route_table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_cidr_block": "0.0.0.0/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
|
|
|
}, nil)
|
2021-04-06 13:16:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test routes are expanded except when they already exist",
|
|
|
|
[]resource.Resource{
|
|
|
|
&resource2.FakeResource{
|
|
|
|
Id: "fake_resource",
|
|
|
|
},
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "r-table_from_state1080289494",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_cidr_block": "0.0.0.0/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "table_from_state",
|
|
|
|
Type: aws.AwsRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"cidr_block": "0.0.0.0/0",
|
|
|
|
"ipv6_cidr_block": "",
|
|
|
|
"vpc_endpoint_id": "",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"cidr_block": "",
|
|
|
|
"ipv6_cidr_block": "::/0",
|
|
|
|
},
|
2021-04-06 13:16:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]resource.Resource{
|
|
|
|
&resource2.FakeResource{
|
|
|
|
Id: "fake_resource",
|
|
|
|
},
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
2021-04-06 13:16:12 +00:00
|
|
|
Id: "table_from_state",
|
2021-05-07 18:55:49 +00:00
|
|
|
Type: aws.AwsRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "r-table_from_state1080289494",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_cidr_block": "0.0.0.0/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "r-table_from_state2750132062",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_ipv6_cidr_block": "::/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
2021-04-06 13:16:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
func(factory *terraform.MockResourceFactory) {
|
2021-05-07 18:55:49 +00:00
|
|
|
factory.On("CreateAbstractResource", "aws_route", mock.Anything, mock.MatchedBy(func(input map[string]interface{}) bool {
|
|
|
|
return input["id"] == "r-table_from_state2750132062"
|
|
|
|
})).Times(1).Return(&resource.AbstractResource{
|
|
|
|
Id: "r-table_from_state2750132062",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "table_from_state",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_ipv6_cidr_block": "::/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
"destination_prefix_list_id": "",
|
|
|
|
"instance_owner_id": "",
|
|
|
|
},
|
|
|
|
}, nil)
|
2021-04-06 13:16:12 +00:00
|
|
|
},
|
|
|
|
},
|
2021-01-14 17:11:02 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-02-03 15:24:55 +00:00
|
|
|
mockedAlerter := &mocks.AlerterInterface{}
|
2021-03-29 16:10:50 +00:00
|
|
|
|
|
|
|
factory := &terraform.MockResourceFactory{}
|
|
|
|
if tt.mock != nil {
|
|
|
|
tt.mock(factory)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := NewAwsRouteTableExpander(mockedAlerter, factory)
|
2021-05-07 18:55:49 +00:00
|
|
|
err := m.Execute(&[]resource.Resource{}, &tt.input)
|
2021-01-14 17:11:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
changelog, err := diff.Diff(tt.expected, tt.input)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 15:24:55 +00:00
|
|
|
|
|
|
|
func TestAwsRouteTableExpander_ExecuteWithInvalidRoutes(t *testing.T) {
|
|
|
|
|
|
|
|
mockedAlerter := &mocks.AlerterInterface{}
|
2021-02-12 21:36:25 +00:00
|
|
|
mockedAlerter.On("SendAlert", aws.AwsRouteTableResourceType, newInvalidRouteAlert(
|
|
|
|
"aws_route_table", "table_from_state",
|
|
|
|
))
|
|
|
|
mockedAlerter.On("SendAlert", aws.AwsDefaultRouteTableResourceType, newInvalidRouteAlert(
|
|
|
|
"aws_default_route_table", "default_table_from_state",
|
|
|
|
))
|
2021-02-03 15:24:55 +00:00
|
|
|
|
|
|
|
input := []resource.Resource{
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "table_from_state",
|
|
|
|
Type: aws.AwsRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"cidr_block": "",
|
|
|
|
"ipv6_cidr_block": "",
|
|
|
|
},
|
2021-02-03 15:24:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "default_table_from_state",
|
|
|
|
Type: aws.AwsDefaultRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"cidr_block": "",
|
|
|
|
"ipv6_cidr_block": "",
|
|
|
|
},
|
2021-02-03 15:24:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []resource.Resource{
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
2021-02-03 15:24:55 +00:00
|
|
|
Id: "table_from_state",
|
2021-05-07 18:55:49 +00:00
|
|
|
Type: aws.AwsRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-02-03 15:24:55 +00:00
|
|
|
},
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
2021-02-03 15:24:55 +00:00
|
|
|
Id: "default_table_from_state",
|
2021-05-07 18:55:49 +00:00
|
|
|
Type: aws.AwsDefaultRouteTableResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-02-03 15:24:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-29 16:10:50 +00:00
|
|
|
factory := &terraform.MockResourceFactory{}
|
|
|
|
|
|
|
|
m := NewAwsRouteTableExpander(mockedAlerter, factory)
|
2021-05-07 18:55:49 +00:00
|
|
|
err := m.Execute(&[]resource.Resource{}, &input)
|
2021-02-03 15:24:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
changelog, err := diff.Diff(expected, input)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mockedAlerter.AssertExpectations(t)
|
|
|
|
}
|