Skip to content

Commit

Permalink
tests/resource/aws_sqs_queue_policy: Add disappears test (#14047)
Browse files Browse the repository at this point in the history
* add disappers test + minor refactor

* use std names
  • Loading branch information
DrFaust92 authored Jul 8, 2020
1 parent 971d9eb commit 10875d1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 24 deletions.
6 changes: 3 additions & 3 deletions aws/resource_aws_sqs_queue_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func resourceAwsSqsQueuePolicyUpsert(d *schema.ResourceData, meta interface{}) e
log.Printf("[DEBUG] SQS attribute %s not found - retrying", sqs.QueueAttributeNamePolicy)
return resource.RetryableError(notUpdatedError)
}
equivalent, err := awspolicy.PoliciesAreEquivalent(*queuePolicy, policy)
equivalent, err := awspolicy.PoliciesAreEquivalent(aws.StringValue(queuePolicy), policy)
if err != nil {
return resource.NonRetryableError(err)
}
Expand All @@ -100,7 +100,7 @@ func resourceAwsSqsQueuePolicyUpsert(d *schema.ResourceData, meta interface{}) e
}

var equivalent bool
equivalent, err = awspolicy.PoliciesAreEquivalent(*queuePolicy, policy)
equivalent, err = awspolicy.PoliciesAreEquivalent(aws.StringValue(queuePolicy), policy)
if !equivalent {
return notUpdatedError
}
Expand All @@ -123,7 +123,7 @@ func resourceAwsSqsQueuePolicyRead(d *schema.ResourceData, meta interface{}) err
AttributeNames: []*string{aws.String(sqs.QueueAttributeNamePolicy)},
})
if err != nil {
if isAWSErr(err, "AWS.SimpleQueueService.NonExistentQueue", "") {
if isAWSErr(err, sqs.ErrCodeQueueDoesNotExist, "") {
log.Printf("[WARN] SQS Queue (%s) not found", d.Id())
d.SetId("")
return nil
Expand Down
93 changes: 72 additions & 21 deletions aws/resource_aws_sqs_queue_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (
func TestAccAWSSQSQueuePolicy_basic(t *testing.T) {
var queueAttributes map[string]*string
resourceName := "aws_sqs_queue_policy.test"
queueName := fmt.Sprintf("sqs-queue-%s", acctest.RandString(5))
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSQSQueueDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSQSPolicyConfig_basic(queueName),
Config: testAccAWSSQSPolicyConfigBasic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSQSQueueExists("aws_sqs_queue.q", &queueAttributes),
testAccCheckAWSSQSQueueExists("aws_sqs_queue.test", &queueAttributes),
testAccCheckAWSSQSQueueDefaultAttributes(&queueAttributes),
resource.TestMatchResourceAttr("aws_sqs_queue_policy.test", "policy",
regexp.MustCompile("^{\"Version\":\"2012-10-17\".+")),
Expand All @@ -33,40 +33,91 @@ func TestAccAWSSQSQueuePolicy_basic(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSSQSPolicyConfigBasic(rName),
PlanOnly: true,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "policy", "aws_sqs_queue.test", "policy"),
),
},
},
})
}

func testAccAWSSQSPolicyConfig_basic(r string) string {
return fmt.Sprintf(testAccAWSSQSPolicyConfig_basic_tpl, r)
func TestAccAWSSQSQueuePolicy_disappears_queue(t *testing.T) {
var queueAttributes map[string]*string
resourceName := "aws_sqs_queue_policy.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSQSQueueDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSQSPolicyConfigBasic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSQSQueueExists("aws_sqs_queue.test", &queueAttributes),
testAccCheckAWSSQSQueueDefaultAttributes(&queueAttributes),
testAccCheckResourceDisappears(testAccProvider, resourceAwsSqsQueue(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

const testAccAWSSQSPolicyConfig_basic_tpl = `
resource "aws_sqs_queue" "q" {
name = "%s"
func TestAccAWSSQSQueuePolicy_disappears(t *testing.T) {
var queueAttributes map[string]*string
resourceName := "aws_sqs_queue_policy.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSQSQueueDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSQSPolicyConfigBasic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSQSQueueExists("aws_sqs_queue.test", &queueAttributes),
testAccCheckAWSSQSQueueDefaultAttributes(&queueAttributes),
testAccCheckResourceDisappears(testAccProvider, resourceAwsSqsQueuePolicy(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccAWSSQSPolicyConfigBasic(rName string) string {
return fmt.Sprintf(`
resource "aws_sqs_queue" "test" {
name = %[1]q
}
resource "aws_sqs_queue_policy" "test" {
queue_url = "${aws_sqs_queue.q.id}"
queue_url = "${aws_sqs_queue.test.id}"
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "sqspolicy",
"Statement": [
"Version":"2012-10-17",
"Id":"sqspolicy",
"Statement":[
{
"Sid": "First",
"Effect": "Allow",
"Principal": "*",
"Action": "sqs:SendMessage",
"Resource": "${aws_sqs_queue.q.arn}",
"Condition": {
"ArnEquals": {
"aws:SourceArn": "${aws_sqs_queue.q.arn}"
"Sid":"First",
"Effect":"Allow",
"Principal":"*",
"Action":"sqs:SendMessage",
"Resource":"${aws_sqs_queue.test.arn}",
"Condition":{
"ArnEquals":{
"aws:SourceArn":"${aws_sqs_queue.test.arn}"
}
}
}
]
}
POLICY
}
`
`, rName)
}

0 comments on commit 10875d1

Please sign in to comment.