2020-12-09 15:31:34 +00:00
|
|
|
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",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
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",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
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",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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" {
|
2020-12-09 15:31:34 +00:00
|
|
|
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",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
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",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
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",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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" {
|
2021-03-31 12:07:03 +00:00
|
|
|
t.Error("Default record is ignored but should not be")
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 15:03:10 +00:00
|
|
|
ignoredRecord := remoteResources[2].(*resource.AbstractResource)
|
|
|
|
ty, _ = ignoredRecord.Attrs.Get("type")
|
|
|
|
if ty != "A" {
|
2020-12-09 15:31:34 +00:00
|
|
|
t.Error("Non default record was ignored")
|
|
|
|
}
|
|
|
|
}
|