driftctl/pkg/middlewares/aws_sns_topic_policy_expand...

252 lines
6.2 KiB
Go
Raw Normal View History

package middlewares
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/r3labs/diff/v2"
"github.com/snyk/driftctl/enumeration/resource"
2022-07-21 08:37:03 +00:00
dctlresource "github.com/snyk/driftctl/pkg/resource"
awsresource "github.com/snyk/driftctl/pkg/resource/aws"
testresource "github.com/snyk/driftctl/test/resource"
"github.com/stretchr/testify/mock"
)
func TestAwsSNSTopicPolicyExpander_Execute(t *testing.T) {
tests := []struct {
name string
2021-08-09 14:03:04 +00:00
resourcesFromState *[]*resource.Resource
expected *[]*resource.Resource
2022-07-21 08:37:03 +00:00
mock func(factory *dctlresource.MockResourceFactory)
wantErr bool
}{
{
name: "Inline policy no attached policy",
2021-08-09 14:03:04 +00:00
resourcesFromState: &[]*resource.Resource{
{
2021-05-03 17:22:36 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
"policy": "{\"policy\":\"coucou\"}",
},
},
},
2021-08-09 14:03:04 +00:00
expected: &[]*resource.Resource{
{
2021-05-03 17:22:36 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
},
},
2021-08-09 14:03:04 +00:00
{
2021-05-03 17:15:14 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicPolicyResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
"policy": "{\"policy\":\"coucou\"}",
},
},
},
2022-07-21 08:37:03 +00:00
mock: func(factory *dctlresource.MockResourceFactory) {
factory.On("CreateAbstractResource", awsresource.AwsSnsTopicPolicyResourceType, "ID", map[string]interface{}{
2021-05-11 10:14:36 +00:00
"arn": "arn",
"id": "ID",
"policy": "{\"policy\":\"coucou\"}",
2021-08-09 14:03:04 +00:00
}).Once().Return(&resource.Resource{
2021-05-03 17:22:36 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicPolicyResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
"policy": "{\"policy\":\"coucou\"}",
},
}, nil)
2021-03-29 16:10:50 +00:00
},
wantErr: false,
},
{
name: "No inline policy, attached policy",
2021-08-09 14:03:04 +00:00
resourcesFromState: &[]*resource.Resource{
{
2021-05-03 17:22:36 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
},
},
2021-08-09 14:03:04 +00:00
{
2021-05-03 17:15:14 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicPolicyResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
"policy": "{\"policy\":\"coucou\"}",
},
},
},
2021-08-09 14:03:04 +00:00
expected: &[]*resource.Resource{
{
2021-05-03 17:22:36 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
},
},
2021-08-09 14:03:04 +00:00
{
2021-05-03 17:22:36 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicPolicyResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
"policy": "{\"policy\":\"coucou\"}",
},
},
},
wantErr: false,
},
{
name: "inline policy and dup attached policy",
2021-08-09 14:03:04 +00:00
resourcesFromState: &[]*resource.Resource{
{
2021-05-03 17:22:36 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
"policy": "{\"policy\":\"coucou\"}",
},
},
2021-08-09 14:03:04 +00:00
{
2021-05-03 17:22:36 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicPolicyResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
"policy": "{\"policy\":\"coucou\"}",
},
},
},
2021-08-09 14:03:04 +00:00
expected: &[]*resource.Resource{
{
2021-05-03 17:22:36 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
},
},
2021-08-09 14:03:04 +00:00
{
2021-05-03 17:15:14 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicPolicyResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
"policy": "{\"policy\":\"coucou\"}",
},
},
},
wantErr: false,
},
{
name: "inline policy and attached policy",
2021-08-09 14:03:04 +00:00
resourcesFromState: &[]*resource.Resource{
{
2021-05-03 17:22:36 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
"policy": "{\"policy\":\"coucou\"}",
},
},
2021-08-09 14:03:04 +00:00
{
2021-05-03 17:22:36 +00:00
Id: "ID2",
Type: awsresource.AwsSnsTopicPolicyResourceType,
Attrs: &resource.Attributes{
"arn": "arn2",
"id": "ID2",
"policy": "{\"policy\":\"coucou2\"}",
},
},
},
2021-08-09 14:03:04 +00:00
expected: &[]*resource.Resource{
{
2021-05-03 17:22:36 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
},
},
2021-08-09 14:03:04 +00:00
{
2021-05-03 17:22:36 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicPolicyResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
"policy": "{\"policy\":\"coucou\"}",
},
},
2021-08-09 14:03:04 +00:00
{
2021-05-03 17:22:36 +00:00
Id: "ID2",
Type: awsresource.AwsSnsTopicPolicyResourceType,
Attrs: &resource.Attributes{
"arn": "arn2",
"id": "ID2",
"policy": "{\"policy\":\"coucou2\"}",
},
},
},
2022-07-21 08:37:03 +00:00
mock: func(factory *dctlresource.MockResourceFactory) {
factory.On("CreateAbstractResource", awsresource.AwsSnsTopicPolicyResourceType, "ID", mock.MatchedBy(func(input map[string]interface{}) bool {
2021-05-03 17:22:36 +00:00
return input["id"] == "ID"
2021-08-09 14:03:04 +00:00
})).Once().Return(&resource.Resource{
2021-05-03 17:22:36 +00:00
Id: "ID",
Type: awsresource.AwsSnsTopicPolicyResourceType,
Attrs: &resource.Attributes{
"arn": "arn",
"id": "ID",
"policy": "{\"policy\":\"coucou\"}",
},
}, nil)
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-03-29 16:10:50 +00:00
2022-07-21 08:37:03 +00:00
factory := &dctlresource.MockResourceFactory{}
2021-03-29 16:10:50 +00:00
if tt.mock != nil {
tt.mock(factory)
}
2021-05-03 17:15:14 +00:00
repo := testresource.InitFakeSchemaRepository("aws", "3.19.0")
awsresource.InitResourcesMetadata(repo)
m := NewAwsSNSTopicPolicyExpander(factory, repo)
2021-08-09 14:03:04 +00:00
if err := m.Execute(&[]*resource.Resource{}, tt.resourcesFromState); (err != nil) != tt.wantErr {
t.Errorf("Execute() error = %v, wantErr %v", err, tt.wantErr)
}
changelog, err := diff.Diff(tt.expected, tt.resourcesFromState)
if err != nil {
t.Fatal(err)
}
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s got = %v, want %v", strings.Join(change.Path, "."), awsutil.Prettify(change.From), awsutil.Prettify(change.To))
}
}
})
}
}