driftctl/pkg/middlewares/vpc_security_group_default_...

99 lines
2.2 KiB
Go
Raw Normal View History

package middlewares
import (
"testing"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
)
func TestDefaultVPCSecurityGroupShouldBeIgnored(t *testing.T) {
middleware := NewVPCDefaultSecurityGroupSanitizer()
2021-08-09 14:03:04 +00:00
remoteResources := []*resource.Resource{
{
Id: "sg-test",
2021-05-10 16:02:57 +00:00
Type: aws.AwsSecurityGroupResourceType,
Attrs: &resource.Attributes{
"name": "test",
2021-05-10 16:02:57 +00:00
},
},
2021-08-09 14:03:04 +00:00
{
Id: "sg-foo",
2021-05-10 16:02:57 +00:00
Type: aws.AwsSecurityGroupResourceType,
Attrs: &resource.Attributes{
"name": "foo",
2021-05-10 16:02:57 +00:00
},
},
2021-08-09 14:03:04 +00:00
{
Id: "sg-default",
2021-05-10 16:02:57 +00:00
Type: aws.AwsDefaultSecurityGroupResourceType,
Attrs: &resource.Attributes{
"name": "default",
2021-05-10 16:02:57 +00:00
},
},
}
2021-08-09 14:03:04 +00:00
stateResources := []*resource.Resource{
{
Id: "sg-bar",
2021-05-10 16:02:57 +00:00
Type: aws.AwsSecurityGroupResourceType,
Attrs: &resource.Attributes{
"name": "bar",
2021-05-10 16:02:57 +00:00
},
},
}
err := middleware.Execute(&remoteResources, &stateResources)
if err != nil {
t.Error(err)
}
if len(remoteResources) != 2 {
t.Error("Default security group was not ignored")
}
}
func TestDefaultVPCSecurityGroupShouldNotBeIgnoredWhenManaged(t *testing.T) {
middleware := NewVPCDefaultSecurityGroupSanitizer()
2021-08-09 14:03:04 +00:00
remoteResources := []*resource.Resource{
{
Id: "sg-test",
2021-05-10 16:02:57 +00:00
Type: aws.AwsSecurityGroupResourceType,
Attrs: &resource.Attributes{
"name": "test",
2021-05-10 16:02:57 +00:00
},
},
2021-08-09 14:03:04 +00:00
{
Id: "sg-foo",
2021-05-10 16:02:57 +00:00
Type: aws.AwsSecurityGroupResourceType,
Attrs: &resource.Attributes{
"name": "foo",
2021-05-10 16:02:57 +00:00
},
},
2021-08-09 14:03:04 +00:00
{
Id: "sg-default",
2021-05-10 16:02:57 +00:00
Type: aws.AwsDefaultSecurityGroupResourceType,
Attrs: &resource.Attributes{
"name": "default",
2021-05-10 16:02:57 +00:00
},
},
}
2021-08-09 14:03:04 +00:00
stateResources := []*resource.Resource{
{
Id: "sg-default",
2021-05-10 16:02:57 +00:00
Type: aws.AwsDefaultSecurityGroupResourceType,
Attrs: &resource.Attributes{
"name": "default",
2021-05-10 16:02:57 +00:00
},
},
}
err := middleware.Execute(&remoteResources, &stateResources)
if err != nil {
t.Error(err)
}
if len(remoteResources) != 3 {
t.Error("Default security group was ignored")
}
2021-08-09 14:03:04 +00:00
managedDefaultSecurityGroup := remoteResources[2]
if *managedDefaultSecurityGroup.Attrs.GetString("name") != "default" {
t.Error("Default security group is ignored when it should not be")
}
}