From 10875d1e9ca4a09493c53388a9bd477bdae9f956 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Wed, 8 Jul 2020 16:09:11 +0300 Subject: [PATCH] tests/resource/aws_sqs_queue_policy: Add disappears test (#14047) * add disappers test + minor refactor * use std names --- aws/resource_aws_sqs_queue_policy.go | 6 +- aws/resource_aws_sqs_queue_policy_test.go | 93 ++++++++++++++++++----- 2 files changed, 75 insertions(+), 24 deletions(-) diff --git a/aws/resource_aws_sqs_queue_policy.go b/aws/resource_aws_sqs_queue_policy.go index 4288d8afe46..3e269896b2c 100644 --- a/aws/resource_aws_sqs_queue_policy.go +++ b/aws/resource_aws_sqs_queue_policy.go @@ -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) } @@ -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 } @@ -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 diff --git a/aws/resource_aws_sqs_queue_policy_test.go b/aws/resource_aws_sqs_queue_policy_test.go index ce18e4a01fe..a71c31c9820 100644 --- a/aws/resource_aws_sqs_queue_policy_test.go +++ b/aws/resource_aws_sqs_queue_policy_test.go @@ -12,7 +12,7 @@ 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) }, @@ -20,9 +20,9 @@ func TestAccAWSSQSQueuePolicy_basic(t *testing.T) { 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\".+")), @@ -33,35 +33,85 @@ 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 = <