driftctl/pkg/middlewares/aws_default_internet_gatewa...

157 lines
3.2 KiB
Go
Raw Normal View History

2021-01-20 21:54:41 +00:00
package middlewares
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/r3labs/diff/v2"
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/pkg/resource"
"github.com/snyk/driftctl/pkg/resource/aws"
2021-01-20 21:54:41 +00:00
)
func TestAwsDefaultInternetGateway_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-20 21:54:41 +00:00
}{
{
"default internet gateway is not ignored when managed by IaC",
2021-08-09 14:03:04 +00:00
[]*resource.Resource{
{
2021-01-20 21:54:41 +00:00
Id: "fake",
},
2021-08-09 14:03:04 +00:00
{
2021-05-21 14:09:45 +00:00
Id: "default-vpc",
Type: aws.AwsDefaultVpcResourceType,
2021-01-20 21:54:41 +00:00
},
2021-08-09 14:03:04 +00:00
{
2021-05-21 14:09:45 +00:00
Id: "dummy-vpc",
Type: aws.AwsVpcResourceType,
2021-01-20 21:54:41 +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-20 21:54:41 +00:00
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 11:24:58 +00:00
Id: "dummy-igw",
Type: aws.AwsInternetGatewayResourceType,
Attrs: &resource.Attributes{
"vpc_id": "dummy-vpc",
},
2021-01-20 21:54:41 +00:00
},
},
2021-08-09 14:03:04 +00:00
[]*resource.Resource{
{
2021-05-17 11:24:58 +00:00
Id: "default-igw",
Type: aws.AwsInternetGatewayResourceType,
Attrs: &resource.Attributes{
"vpc_id": "default-vpc",
},
2021-01-20 21:54:41 +00:00
},
},
2021-08-09 14:03:04 +00:00
[]*resource.Resource{
{
2021-01-20 21:54:41 +00:00
Id: "fake",
},
2021-08-09 14:03:04 +00:00
{
2021-05-21 14:09:45 +00:00
Id: "default-vpc",
Type: aws.AwsDefaultVpcResourceType,
2021-01-20 21:54:41 +00:00
},
2021-08-09 14:03:04 +00:00
{
2021-05-21 14:09:45 +00:00
Id: "dummy-vpc",
Type: aws.AwsVpcResourceType,
2021-01-20 21:54:41 +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-20 21:54:41 +00:00
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 11:24:58 +00:00
Id: "dummy-igw",
Type: aws.AwsInternetGatewayResourceType,
Attrs: &resource.Attributes{
"vpc_id": "dummy-vpc",
},
2021-01-20 21:54:41 +00:00
},
},
},
{
"default internet gateway is ignored when not managed by IaC",
2021-08-09 14:03:04 +00:00
[]*resource.Resource{
{
2021-01-20 21:54:41 +00:00
Id: "fake",
},
2021-08-09 14:03:04 +00:00
{
2021-05-21 14:09:45 +00:00
Id: "default-vpc",
Type: aws.AwsDefaultVpcResourceType,
2021-01-20 21:54:41 +00:00
},
2021-08-09 14:03:04 +00:00
{
2021-05-21 14:09:45 +00:00
Id: "dummy-vpc",
Type: aws.AwsVpcResourceType,
2021-01-20 21:54:41 +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-20 21:54:41 +00:00
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 11:24:58 +00:00
Id: "dummy-igw",
Type: aws.AwsInternetGatewayResourceType,
Attrs: &resource.Attributes{
"vpc_id": "dummy-vpc",
},
2021-01-20 21:54:41 +00:00
},
},
2021-08-09 14:03:04 +00:00
[]*resource.Resource{},
[]*resource.Resource{
{
2021-01-20 21:54:41 +00:00
Id: "fake",
},
2021-08-09 14:03:04 +00:00
{
2021-05-21 14:09:45 +00:00
Id: "default-vpc",
Type: aws.AwsDefaultVpcResourceType,
2021-01-20 21:54:41 +00:00
},
2021-08-09 14:03:04 +00:00
{
2021-05-21 14:09:45 +00:00
Id: "dummy-vpc",
Type: aws.AwsVpcResourceType,
2021-01-20 21:54:41 +00:00
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 11:24:58 +00:00
Id: "dummy-igw",
Type: aws.AwsInternetGatewayResourceType,
Attrs: &resource.Attributes{
"vpc_id": "dummy-vpc",
},
2021-01-20 21:54:41 +00:00
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := NewAwsDefaultInternetGateway()
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))
}
}
})
}
}