driftctl/pkg/middlewares/aws_nat_gateway_eip_assoc_t...

103 lines
2.7 KiB
Go
Raw Normal View History

2021-01-20 16:28:56 +00:00
package middlewares
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/r3labs/diff/v2"
)
func TestAwsNatGatewayEipAssoc_Execute(t *testing.T) {
tests := []struct {
name string
input []resource.Resource
expected []resource.Resource
}{
{
name: "test nil values do not crash middleware",
input: []resource.Resource{
2021-05-17 14:21:45 +00:00
&resource.AbstractResource{
Id: "nat-0a5408508b19ef490",
Type: aws.AwsNatGatewayResourceType,
Attrs: &resource.Attributes{},
2021-01-20 16:28:56 +00:00
},
2021-05-17 09:38:08 +00:00
&resource.AbstractResource{
Type: aws.AwsEipAssociationResourceType,
Id: "eipassoc-0d32af6acf31df913",
Attrs: &resource.Attributes{},
2021-01-20 16:28:56 +00:00
},
},
expected: []resource.Resource{
2021-05-17 14:21:45 +00:00
&resource.AbstractResource{
Id: "nat-0a5408508b19ef490",
Type: aws.AwsNatGatewayResourceType,
Attrs: &resource.Attributes{},
2021-01-20 16:28:56 +00:00
},
2021-05-17 09:38:08 +00:00
&resource.AbstractResource{
Type: aws.AwsEipAssociationResourceType,
Id: "eipassoc-0d32af6acf31df913",
Attrs: &resource.Attributes{},
2021-01-20 16:28:56 +00:00
},
},
},
{
name: "test eip assoc ignored when associated to a nat gateway",
input: []resource.Resource{
2021-05-17 14:21:45 +00:00
&resource.AbstractResource{
Type: aws.AwsNatGatewayResourceType,
Attrs: &resource.Attributes{
"allocation_id": "eipalloc-0f3e9fff457bb770b",
},
2021-01-20 16:28:56 +00:00
},
2021-05-17 09:38:08 +00:00
&resource.AbstractResource{
Type: aws.AwsEipAssociationResourceType,
Attrs: &resource.Attributes{
"allocation_id": "eipalloc-0f3e9fff457bb770b",
},
2021-01-20 16:28:56 +00:00
},
2021-05-17 09:38:08 +00:00
&resource.AbstractResource{
Type: aws.AwsEipAssociationResourceType,
Attrs: &resource.Attributes{
"allocation_id": "eipalloc-1234567890",
},
2021-01-20 16:28:56 +00:00
},
},
expected: []resource.Resource{
2021-05-17 14:21:45 +00:00
&resource.AbstractResource{
Type: aws.AwsNatGatewayResourceType,
Attrs: &resource.Attributes{
"allocation_id": "eipalloc-0f3e9fff457bb770b",
},
2021-01-20 16:28:56 +00:00
},
2021-05-17 09:38:08 +00:00
&resource.AbstractResource{
Type: aws.AwsEipAssociationResourceType,
Attrs: &resource.Attributes{
"allocation_id": "eipalloc-1234567890",
},
2021-01-20 16:28:56 +00:00
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
middleware := NewAwsNatGatewayEipAssoc()
err := middleware.Execute(&tt.input, &[]resource.Resource{})
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))
}
}
})
}
}