driftctl/pkg/middlewares/route53_records_test.go

124 lines
2.6 KiB
Go
Raw Normal View History

package middlewares
import (
"testing"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/resource"
)
func TestDefaultRoute53RecordShouldBeIgnored(t *testing.T) {
middleware := NewRoute53DefaultZoneRecordSanitizer()
2021-08-09 14:03:04 +00:00
remoteResources := []*resource.Resource{
{
2021-05-17 15:03:10 +00:00
Type: aws.AwsRoute53ZoneResourceType,
Attrs: &resource.Attributes{},
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 15:03:10 +00:00
Type: aws.AwsRoute53RecordResourceType,
2021-01-06 11:41:25 +00:00
Id: "123_foobar_NS",
2021-05-17 15:03:10 +00:00
Attrs: &resource.Attributes{
"type": "NS",
},
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 15:03:10 +00:00
Type: aws.AwsRoute53RecordResourceType,
2021-01-06 11:41:25 +00:00
Id: "123_foobar_SOA",
2021-05-17 15:03:10 +00:00
Attrs: &resource.Attributes{
"type": "SOA",
},
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 15:03:10 +00:00
Type: aws.AwsRoute53RecordResourceType,
2021-01-06 11:41:25 +00:00
Id: "123_foobar_A",
2021-05-17 15:03:10 +00:00
Attrs: &resource.Attributes{
"type": "A",
},
},
}
2021-08-09 14:03:04 +00:00
stateResources := []*resource.Resource{
{
2021-05-17 15:03:10 +00:00
Type: aws.AwsRoute53RecordResourceType,
2021-01-06 11:41:25 +00:00
Id: "456_barfoo_NS",
2021-05-17 15:03:10 +00:00
Attrs: &resource.Attributes{
"type": "NS",
},
},
}
err := middleware.Execute(&remoteResources, &stateResources)
if err != nil {
t.Error(err)
}
if len(remoteResources) != 2 {
t.Error("Default records were not ignored")
}
2021-08-09 14:03:04 +00:00
remainingResource := remoteResources[1]
2021-05-17 15:03:10 +00:00
ty, _ := remainingResource.Attrs.Get("type")
if ty != "A" {
t.Error("Default record is invalid")
}
}
func TestDefaultRoute53RecordShouldNotBeIgnoredWhenManaged(t *testing.T) {
middleware := NewRoute53DefaultZoneRecordSanitizer()
2021-08-09 14:03:04 +00:00
remoteResources := []*resource.Resource{
{
2021-05-17 15:03:10 +00:00
Type: aws.AwsRoute53ZoneResourceType,
Attrs: &resource.Attributes{},
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 15:03:10 +00:00
Type: aws.AwsRoute53RecordResourceType,
2021-01-06 11:41:25 +00:00
Id: "123_foobar_NS",
2021-05-17 15:03:10 +00:00
Attrs: &resource.Attributes{
"type": "NS",
},
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 15:03:10 +00:00
Type: aws.AwsRoute53RecordResourceType,
2021-01-06 11:41:25 +00:00
Id: "123_foobar_SOA",
2021-05-17 15:03:10 +00:00
Attrs: &resource.Attributes{
"type": "SOA",
},
},
2021-08-09 14:03:04 +00:00
{
2021-05-17 15:03:10 +00:00
Type: aws.AwsRoute53RecordResourceType,
2021-01-06 11:41:25 +00:00
Id: "123_foobar_A",
2021-05-17 15:03:10 +00:00
Attrs: &resource.Attributes{
"type": "A",
},
},
}
2021-08-09 14:03:04 +00:00
stateResources := []*resource.Resource{
{
2021-05-17 15:03:10 +00:00
Type: aws.AwsRoute53RecordResourceType,
2021-01-06 11:41:25 +00:00
Id: "123_foobar_NS",
2021-05-17 15:03:10 +00:00
Attrs: &resource.Attributes{
"type": "NS",
},
},
}
err := middleware.Execute(&remoteResources, &stateResources)
if err != nil {
t.Error(err)
}
if len(remoteResources) != 3 {
t.Error("Default records were not ignored")
}
2021-08-09 14:03:04 +00:00
managedDefaultRecord := remoteResources[1]
2021-05-17 15:03:10 +00:00
ty, _ := managedDefaultRecord.Attrs.Get("type")
if ty != "NS" {
t.Error("Default record is ignored but should not be")
}
2021-08-09 14:03:04 +00:00
ignoredRecord := remoteResources[2]
2021-05-17 15:03:10 +00:00
ty, _ = ignoredRecord.Attrs.Get("type")
if ty != "A" {
t.Error("Non default record was ignored")
}
}