driftctl/pkg/middlewares/route53_records_test.go

124 lines
2.9 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()
remoteResources := []resource.Resource{
2021-05-17 15:03:10 +00:00
&resource.AbstractResource{
Type: aws.AwsRoute53ZoneResourceType,
Attrs: &resource.Attributes{},
},
&resource.AbstractResource{
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-05-17 15:03:10 +00:00
&resource.AbstractResource{
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-05-17 15:03:10 +00:00
&resource.AbstractResource{
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",
},
},
}
stateResources := []resource.Resource{
2021-05-17 15:03:10 +00:00
&resource.AbstractResource{
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-05-17 15:03:10 +00:00
remainingResource := remoteResources[1].(*resource.AbstractResource)
ty, _ := remainingResource.Attrs.Get("type")
if ty != "A" {
t.Error("Default record is invalid")
}
}
func TestDefaultRoute53RecordShouldNotBeIgnoredWhenManaged(t *testing.T) {
middleware := NewRoute53DefaultZoneRecordSanitizer()
remoteResources := []resource.Resource{
2021-05-17 15:03:10 +00:00
&resource.AbstractResource{
Type: aws.AwsRoute53ZoneResourceType,
Attrs: &resource.Attributes{},
},
&resource.AbstractResource{
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-05-17 15:03:10 +00:00
&resource.AbstractResource{
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-05-17 15:03:10 +00:00
&resource.AbstractResource{
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",
},
},
}
stateResources := []resource.Resource{
2021-05-17 15:03:10 +00:00
&resource.AbstractResource{
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-05-17 15:03:10 +00:00
managedDefaultRecord := remoteResources[1].(*resource.AbstractResource)
ty, _ := managedDefaultRecord.Attrs.Get("type")
if ty != "NS" {
t.Error("Default record is ignored but should not be")
}
2021-05-17 15:03:10 +00:00
ignoredRecord := remoteResources[2].(*resource.AbstractResource)
ty, _ = ignoredRecord.Attrs.Get("type")
if ty != "A" {
t.Error("Non default record was ignored")
}
}