driftctl/pkg/remote/aws/repository/route53_repository_test.go

208 lines
5.3 KiB
Go
Raw Normal View History

2021-02-15 20:00:16 +00:00
package repository
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/service/route53"
2021-05-04 16:37:45 +00:00
awstest "github.com/cloudskiff/driftctl/test/aws"
2021-02-15 20:00:16 +00:00
"github.com/aws/aws-sdk-go/aws"
"github.com/stretchr/testify/mock"
"github.com/r3labs/diff/v2"
"github.com/stretchr/testify/assert"
)
func Test_route53Repository_ListAllHealthChecks(t *testing.T) {
tests := []struct {
name string
2021-05-04 16:37:45 +00:00
mocks func(client *awstest.MockFakeRoute53)
2021-02-15 20:00:16 +00:00
want []*route53.HealthCheck
wantErr error
}{
{
name: "List with 2 pages",
2021-05-04 16:37:45 +00:00
mocks: func(client *awstest.MockFakeRoute53) {
2021-02-15 20:00:16 +00:00
client.On("ListHealthChecksPages",
&route53.ListHealthChecksInput{},
mock.MatchedBy(func(callback func(res *route53.ListHealthChecksOutput, lastPage bool) bool) bool {
callback(&route53.ListHealthChecksOutput{
HealthChecks: []*route53.HealthCheck{
{Id: aws.String("1")},
{Id: aws.String("2")},
{Id: aws.String("3")},
},
}, false)
callback(&route53.ListHealthChecksOutput{
HealthChecks: []*route53.HealthCheck{
{Id: aws.String("4")},
{Id: aws.String("5")},
{Id: aws.String("6")},
},
}, true)
return true
})).Return(nil)
},
want: []*route53.HealthCheck{
{Id: aws.String("1")},
{Id: aws.String("2")},
{Id: aws.String("3")},
{Id: aws.String("4")},
{Id: aws.String("5")},
{Id: aws.String("6")},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-05-04 16:37:45 +00:00
client := awstest.MockFakeRoute53{}
tt.mocks(&client)
2021-02-15 20:00:16 +00:00
r := &route53Repository{
2021-05-04 16:37:45 +00:00
client: &client,
2021-02-15 20:00:16 +00:00
}
got, err := r.ListAllHealthChecks()
assert.Equal(t, tt.wantErr, err)
changelog, err := diff.Diff(got, tt.want)
assert.Nil(t, err)
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s: %s -> %s", strings.Join(change.Path, "."), change.From, change.To)
}
t.Fail()
}
})
}
}
func Test_route53Repository_ListAllZones(t *testing.T) {
tests := []struct {
name string
2021-05-04 16:37:45 +00:00
mocks func(client *awstest.MockFakeRoute53)
want []*route53.HostedZone
wantErr error
}{
{name: "Zones with 2 pages",
2021-05-04 16:37:45 +00:00
mocks: func(client *awstest.MockFakeRoute53) {
client.On("ListHostedZonesPages",
&route53.ListHostedZonesInput{},
mock.MatchedBy(func(callback func(res *route53.ListHostedZonesOutput, lastPage bool) bool) bool {
callback(&route53.ListHostedZonesOutput{
HostedZones: []*route53.HostedZone{
{Id: aws.String("1")},
{Id: aws.String("2")},
{Id: aws.String("3")},
},
}, false)
callback(&route53.ListHostedZonesOutput{
HostedZones: []*route53.HostedZone{
{Id: aws.String("4")},
{Id: aws.String("5")},
{Id: aws.String("6")},
},
}, true)
return true
})).Return(nil)
},
want: []*route53.HostedZone{
{Id: aws.String("1")},
{Id: aws.String("2")},
{Id: aws.String("3")},
{Id: aws.String("4")},
{Id: aws.String("5")},
{Id: aws.String("6")},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-05-04 16:37:45 +00:00
client := awstest.MockFakeRoute53{}
tt.mocks(&client)
r := &route53Repository{
2021-05-04 16:37:45 +00:00
client: &client,
}
got, err := r.ListAllZones()
assert.Equal(t, tt.wantErr, err)
changelog, err := diff.Diff(got, tt.want)
assert.Nil(t, err)
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s: %s -> %s", strings.Join(change.Path, "."), change.From, change.To)
}
t.Fail()
}
})
}
}
func Test_route53Repository_ListRecordsForZone(t *testing.T) {
tests := []struct {
name string
zoneIds []string
2021-05-04 16:37:45 +00:00
mocks func(client *awstest.MockFakeRoute53)
want []*route53.ResourceRecordSet
wantErr error
}{
{
name: "records for zone with 2 pages",
zoneIds: []string{
"1",
},
2021-05-04 16:37:45 +00:00
mocks: func(client *awstest.MockFakeRoute53) {
client.On("ListResourceRecordSetsPages",
&route53.ListResourceRecordSetsInput{
HostedZoneId: aws.String("1"),
},
mock.MatchedBy(func(callback func(res *route53.ListResourceRecordSetsOutput, lastPage bool) bool) bool {
callback(&route53.ListResourceRecordSetsOutput{
ResourceRecordSets: []*route53.ResourceRecordSet{
{Name: aws.String("1")},
{Name: aws.String("2")},
{Name: aws.String("3")},
},
}, false)
callback(&route53.ListResourceRecordSetsOutput{
ResourceRecordSets: []*route53.ResourceRecordSet{
{Name: aws.String("4")},
{Name: aws.String("5")},
{Name: aws.String("6")},
},
}, true)
return true
})).Return(nil)
},
want: []*route53.ResourceRecordSet{
{Name: aws.String("1")},
{Name: aws.String("2")},
{Name: aws.String("3")},
{Name: aws.String("4")},
{Name: aws.String("5")},
{Name: aws.String("6")},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-05-04 16:37:45 +00:00
client := awstest.MockFakeRoute53{}
tt.mocks(&client)
r := &route53Repository{
2021-05-04 16:37:45 +00:00
client: &client,
}
for _, id := range tt.zoneIds {
got, err := r.ListRecordsForZone(id)
assert.Equal(t, tt.wantErr, err)
changelog, err := diff.Diff(got, tt.want)
assert.Nil(t, err)
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s: %s -> %s", strings.Join(change.Path, "."), change.From, change.To)
}
t.Fail()
}
}
})
}
}