2021-02-03 11:53:28 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2021-06-04 14:33:58 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
2021-05-04 16:37:45 +00:00
|
|
|
awstest "github.com/cloudskiff/driftctl/test/aws"
|
2021-02-03 11:53:28 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
|
|
|
|
"github.com/r3labs/diff/v2"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/sns"
|
|
|
|
)
|
|
|
|
|
2021-02-03 13:18:17 +00:00
|
|
|
func Test_snsRepository_ListAllTopics(t *testing.T) {
|
2021-02-03 11:53:28 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2021-05-04 16:37:45 +00:00
|
|
|
mocks func(client *awstest.MockFakeSNS)
|
2021-02-03 11:53:28 +00:00
|
|
|
want []*sns.Topic
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "List with 2 pages",
|
2021-05-04 16:37:45 +00:00
|
|
|
mocks: func(client *awstest.MockFakeSNS) {
|
2021-02-03 11:53:28 +00:00
|
|
|
client.On("ListTopicsPages",
|
|
|
|
&sns.ListTopicsInput{},
|
|
|
|
mock.MatchedBy(func(callback func(res *sns.ListTopicsOutput, lastPage bool) bool) bool {
|
|
|
|
callback(&sns.ListTopicsOutput{
|
|
|
|
Topics: []*sns.Topic{
|
|
|
|
{TopicArn: aws.String("arn1")},
|
|
|
|
{TopicArn: aws.String("arn2")},
|
|
|
|
{TopicArn: aws.String("arn3")},
|
|
|
|
},
|
|
|
|
}, false)
|
|
|
|
callback(&sns.ListTopicsOutput{
|
|
|
|
Topics: []*sns.Topic{
|
|
|
|
{TopicArn: aws.String("arn4")},
|
|
|
|
{TopicArn: aws.String("arn5")},
|
|
|
|
{TopicArn: aws.String("arn6")},
|
|
|
|
},
|
|
|
|
}, true)
|
|
|
|
return true
|
2021-06-04 14:33:58 +00:00
|
|
|
})).Return(nil).Once()
|
2021-02-03 11:53:28 +00:00
|
|
|
},
|
|
|
|
want: []*sns.Topic{
|
|
|
|
{TopicArn: aws.String("arn1")},
|
|
|
|
{TopicArn: aws.String("arn2")},
|
|
|
|
{TopicArn: aws.String("arn3")},
|
|
|
|
{TopicArn: aws.String("arn4")},
|
|
|
|
{TopicArn: aws.String("arn5")},
|
|
|
|
{TopicArn: aws.String("arn6")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-06-04 14:33:58 +00:00
|
|
|
store := cache.New(1)
|
2021-05-04 16:37:45 +00:00
|
|
|
client := &awstest.MockFakeSNS{}
|
2021-02-03 11:53:28 +00:00
|
|
|
tt.mocks(client)
|
2021-02-03 13:18:17 +00:00
|
|
|
r := &snsRepository{
|
2021-02-03 11:53:28 +00:00
|
|
|
client: client,
|
2021-06-04 14:33:58 +00:00
|
|
|
cache: store,
|
2021-02-03 11:53:28 +00:00
|
|
|
}
|
|
|
|
got, err := r.ListAllTopics()
|
|
|
|
assert.Equal(t, tt.wantErr, err)
|
2021-06-04 14:33:58 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
// Check that results were cached
|
|
|
|
cachedData, err := r.ListAllTopics()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, got, cachedData)
|
|
|
|
assert.IsType(t, []*sns.Topic{}, store.Get("snsListAllTopics"))
|
|
|
|
}
|
|
|
|
|
2021-02-03 11:53:28 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 10:03:45 +00:00
|
|
|
|
|
|
|
func Test_snsRepository_ListAllSubscriptions(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2021-05-04 16:37:45 +00:00
|
|
|
mocks func(client *awstest.MockFakeSNS)
|
2021-02-05 10:03:45 +00:00
|
|
|
want []*sns.Subscription
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "List with 2 pages",
|
2021-05-04 16:37:45 +00:00
|
|
|
mocks: func(client *awstest.MockFakeSNS) {
|
2021-02-05 10:03:45 +00:00
|
|
|
client.On("ListSubscriptionsPages",
|
|
|
|
&sns.ListSubscriptionsInput{},
|
|
|
|
mock.MatchedBy(func(callback func(res *sns.ListSubscriptionsOutput, lastPage bool) bool) bool {
|
|
|
|
callback(&sns.ListSubscriptionsOutput{
|
|
|
|
Subscriptions: []*sns.Subscription{
|
|
|
|
{TopicArn: aws.String("arn1"), SubscriptionArn: aws.String("SubArn1")},
|
|
|
|
{TopicArn: aws.String("arn2"), SubscriptionArn: aws.String("SubArn2")},
|
|
|
|
{TopicArn: aws.String("arn3"), SubscriptionArn: aws.String("SubArn3")},
|
|
|
|
},
|
|
|
|
}, false)
|
|
|
|
callback(&sns.ListSubscriptionsOutput{
|
|
|
|
Subscriptions: []*sns.Subscription{
|
|
|
|
{TopicArn: aws.String("arn4"), SubscriptionArn: aws.String("SubArn4")},
|
|
|
|
{TopicArn: aws.String("arn5"), SubscriptionArn: aws.String("SubArn5")},
|
|
|
|
{TopicArn: aws.String("arn6"), SubscriptionArn: aws.String("SubArn6")},
|
|
|
|
},
|
|
|
|
}, true)
|
|
|
|
return true
|
2021-06-04 14:33:58 +00:00
|
|
|
})).Return(nil).Once()
|
2021-02-05 10:03:45 +00:00
|
|
|
},
|
|
|
|
want: []*sns.Subscription{
|
|
|
|
{TopicArn: aws.String("arn1"), SubscriptionArn: aws.String("SubArn1")},
|
|
|
|
{TopicArn: aws.String("arn2"), SubscriptionArn: aws.String("SubArn2")},
|
|
|
|
{TopicArn: aws.String("arn3"), SubscriptionArn: aws.String("SubArn3")},
|
|
|
|
{TopicArn: aws.String("arn4"), SubscriptionArn: aws.String("SubArn4")},
|
|
|
|
{TopicArn: aws.String("arn5"), SubscriptionArn: aws.String("SubArn5")},
|
|
|
|
{TopicArn: aws.String("arn6"), SubscriptionArn: aws.String("SubArn6")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-06-04 14:33:58 +00:00
|
|
|
store := cache.New(1)
|
2021-05-04 16:37:45 +00:00
|
|
|
client := &awstest.MockFakeSNS{}
|
2021-02-05 10:03:45 +00:00
|
|
|
tt.mocks(client)
|
|
|
|
r := &snsRepository{
|
|
|
|
client: client,
|
2021-06-04 14:33:58 +00:00
|
|
|
cache: store,
|
2021-02-05 10:03:45 +00:00
|
|
|
}
|
|
|
|
got, err := r.ListAllSubscriptions()
|
|
|
|
assert.Equal(t, tt.wantErr, err)
|
2021-06-04 14:33:58 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
// Check that results were cached
|
|
|
|
cachedData, err := r.ListAllSubscriptions()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, got, cachedData)
|
|
|
|
assert.IsType(t, []*sns.Subscription{}, store.Get("snsListAllSubscriptions"))
|
|
|
|
}
|
|
|
|
|
2021-02-05 10:03:45 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|