driftctl/pkg/middlewares/aws_default_sqs_queue_polic...

151 lines
4.1 KiB
Go
Raw Normal View History

2021-01-29 11:35:11 +00:00
package middlewares
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/r3labs/diff/v2"
2021-01-29 11:35:11 +00:00
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
)
func TestAwsDefaultSqsQueuePolicy_Execute(t *testing.T) {
tests := []struct {
name string
remoteResources []resource.Resource
resourcesFromState []resource.Resource
expected []resource.Resource
}{
{
"test default sqs queue policy managed by IaC",
[]resource.Resource{
&resource.AbstractResource{
Id: "non-default-sqs-queue-policy",
Type: aws.AwsSqsQueuePolicyResourceType,
Attrs: &resource.Attributes{
"queue_url": "non-default-sqs-queue-policy",
"id": "non-default-sqs-queue-policy",
"policy": "foo",
},
2021-01-29 11:35:11 +00:00
},
&resource.AbstractResource{
Id: "default-sqs-queue-policy",
Type: aws.AwsSqsQueuePolicyResourceType,
Attrs: &resource.Attributes{
"queue_url": "default-sqs-queue-policy",
"id": "default-sqs-queue-policy",
"policy": "",
},
2021-01-29 11:35:11 +00:00
},
},
[]resource.Resource{
&resource.AbstractResource{
Id: "non-default-sqs-queue-policy",
Type: aws.AwsSqsQueuePolicyResourceType,
Attrs: &resource.Attributes{
"queue_url": "non-default-sqs-queue-policy",
"id": "non-default-sqs-queue-policy",
"policy": "foo",
},
2021-01-29 11:35:11 +00:00
},
&resource.AbstractResource{
Id: "default-sqs-queue-policy",
Type: aws.AwsSqsQueuePolicyResourceType,
Attrs: &resource.Attributes{
"queue_url": "default-sqs-queue-policy",
"id": "default-sqs-queue-policy",
"policy": "",
},
2021-01-29 11:35:11 +00:00
},
},
[]resource.Resource{
&resource.AbstractResource{
Id: "non-default-sqs-queue-policy",
Type: aws.AwsSqsQueuePolicyResourceType,
Attrs: &resource.Attributes{
"queue_url": "non-default-sqs-queue-policy",
"id": "non-default-sqs-queue-policy",
"policy": "foo",
},
2021-01-29 11:35:11 +00:00
},
&resource.AbstractResource{
Id: "default-sqs-queue-policy",
Type: aws.AwsSqsQueuePolicyResourceType,
Attrs: &resource.Attributes{
"queue_url": "default-sqs-queue-policy",
"id": "default-sqs-queue-policy",
"policy": "",
},
2021-01-29 11:35:11 +00:00
},
},
},
{
"test default sqs queue policy not managed by IaC",
[]resource.Resource{
&resource.AbstractResource{
Id: "non-default-sqs-queue-policy",
Type: aws.AwsSqsQueuePolicyResourceType,
Attrs: &resource.Attributes{
"queue_url": "non-default-sqs-queue-policy",
"id": "non-default-sqs-queue-policy",
"policy": "foo",
},
2021-01-29 11:35:11 +00:00
},
&resource.AbstractResource{
Id: "default-sqs-queue-policy",
Type: aws.AwsSqsQueuePolicyResourceType,
Attrs: &resource.Attributes{
"queue_url": "default-sqs-queue-policy",
"id": "default-sqs-queue-policy",
"policy": "",
},
2021-01-29 11:35:11 +00:00
},
},
[]resource.Resource{
&resource.AbstractResource{
Id: "non-default-sqs-queue-policy",
Type: aws.AwsSqsQueuePolicyResourceType,
Attrs: &resource.Attributes{
"queue_url": "non-default-sqs-queue-policy",
"id": "non-default-sqs-queue-policy",
"policy": "foo",
},
2021-01-29 11:35:11 +00:00
},
},
[]resource.Resource{
&resource.AbstractResource{
Id: "non-default-sqs-queue-policy",
Type: aws.AwsSqsQueuePolicyResourceType,
Attrs: &resource.Attributes{
"queue_url": "non-default-sqs-queue-policy",
"id": "non-default-sqs-queue-policy",
"policy": "foo",
},
2021-01-29 11:35:11 +00:00
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := NewAwsDefaultSqsQueuePolicy()
err := m.Execute(&tt.remoteResources, &tt.resourcesFromState)
if err != nil {
t.Fatal(err)
}
changelog, err := diff.Diff(tt.remoteResources, tt.expected)
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))
}
}
})
}
}