github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_sqs_queue_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/sqs" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 "github.com/jen20/awspolicyequivalence" 14 ) 15 16 func TestAccAWSSQSQueue_basic(t *testing.T) { 17 queueName := fmt.Sprintf("sqs-queue-%s", acctest.RandString(5)) 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckAWSSQSQueueDestroy, 22 Steps: []resource.TestStep{ 23 { 24 Config: testAccAWSSQSConfigWithDefaults(queueName), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.queue"), 27 ), 28 }, 29 { 30 Config: testAccAWSSQSConfigWithOverrides(queueName), 31 Check: resource.ComposeTestCheckFunc( 32 testAccCheckAWSSQSExistsWithOverrides("aws_sqs_queue.queue"), 33 ), 34 }, 35 { 36 Config: testAccAWSSQSConfigWithDefaults(queueName), 37 Check: resource.ComposeTestCheckFunc( 38 testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.queue"), 39 ), 40 }, 41 }, 42 }) 43 } 44 45 func TestAccAWSSQSQueue_policy(t *testing.T) { 46 queueName := fmt.Sprintf("sqs-queue-%s", acctest.RandString(5)) 47 topicName := fmt.Sprintf("sns-topic-%s", acctest.RandString(5)) 48 49 expectedPolicyText := fmt.Sprintf( 50 `{"Version": "2012-10-17","Id": "sqspolicy","Statement":[{"Sid": "Stmt1451501026839","Effect": "Allow","Principal":"*","Action":"sqs:SendMessage","Resource":"arn:aws:sqs:us-west-2:470663696735:%s","Condition":{"ArnEquals":{"aws:SourceArn":"arn:aws:sns:us-west-2:470663696735:%s"}}}]}`, 51 topicName, queueName) 52 53 resource.Test(t, resource.TestCase{ 54 PreCheck: func() { testAccPreCheck(t) }, 55 Providers: testAccProviders, 56 CheckDestroy: testAccCheckAWSSQSQueueDestroy, 57 Steps: []resource.TestStep{ 58 { 59 Config: testAccAWSSQSConfig_PolicyFormat(topicName, queueName), 60 Check: resource.ComposeTestCheckFunc( 61 testAccCheckAWSQSHasPolicy("aws_sqs_queue.test-email-events", expectedPolicyText), 62 ), 63 }, 64 }, 65 }) 66 } 67 68 func TestAccAWSSQSQueue_redrivePolicy(t *testing.T) { 69 resource.Test(t, resource.TestCase{ 70 PreCheck: func() { testAccPreCheck(t) }, 71 Providers: testAccProviders, 72 CheckDestroy: testAccCheckAWSSQSQueueDestroy, 73 Steps: []resource.TestStep{ 74 { 75 Config: testAccAWSSQSConfigWithRedrive(acctest.RandStringFromCharSet(5, acctest.CharSetAlpha)), 76 Check: resource.ComposeTestCheckFunc( 77 testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.my_dead_letter_queue"), 78 ), 79 }, 80 }, 81 }) 82 } 83 84 // Tests formatting and compacting of Policy, Redrive json 85 func TestAccAWSSQSQueue_Policybasic(t *testing.T) { 86 queueName := fmt.Sprintf("sqs-queue-%s", acctest.RandString(5)) 87 topicName := fmt.Sprintf("sns-topic-%s", acctest.RandString(5)) 88 resource.Test(t, resource.TestCase{ 89 PreCheck: func() { testAccPreCheck(t) }, 90 Providers: testAccProviders, 91 CheckDestroy: testAccCheckAWSSQSQueueDestroy, 92 Steps: []resource.TestStep{ 93 { 94 Config: testAccAWSSQSConfig_PolicyFormat(topicName, queueName), 95 Check: resource.ComposeTestCheckFunc( 96 testAccCheckAWSSQSExistsWithOverrides("aws_sqs_queue.test-email-events"), 97 ), 98 }, 99 }, 100 }) 101 } 102 103 func testAccCheckAWSSQSQueueDestroy(s *terraform.State) error { 104 conn := testAccProvider.Meta().(*AWSClient).sqsconn 105 106 for _, rs := range s.RootModule().Resources { 107 if rs.Type != "aws_sqs_queue" { 108 continue 109 } 110 111 // Check if queue exists by checking for its attributes 112 params := &sqs.GetQueueAttributesInput{ 113 QueueUrl: aws.String(rs.Primary.ID), 114 } 115 _, err := conn.GetQueueAttributes(params) 116 if err == nil { 117 return fmt.Errorf("Queue %s still exists. Failing!", rs.Primary.ID) 118 } 119 120 // Verify the error is what we want 121 _, ok := err.(awserr.Error) 122 if !ok { 123 return err 124 } 125 } 126 127 return nil 128 } 129 func testAccCheckAWSQSHasPolicy(n string, expectedPolicyText string) resource.TestCheckFunc { 130 return func(s *terraform.State) error { 131 rs, ok := s.RootModule().Resources[n] 132 if !ok { 133 return fmt.Errorf("Not found: %s", n) 134 } 135 136 if rs.Primary.ID == "" { 137 return fmt.Errorf("No Queue URL specified!") 138 } 139 140 conn := testAccProvider.Meta().(*AWSClient).sqsconn 141 142 params := &sqs.GetQueueAttributesInput{ 143 QueueUrl: aws.String(rs.Primary.ID), 144 AttributeNames: []*string{aws.String("Policy")}, 145 } 146 resp, err := conn.GetQueueAttributes(params) 147 if err != nil { 148 return err 149 } 150 151 var actualPolicyText string 152 for k, v := range resp.Attributes { 153 if k == "Policy" { 154 actualPolicyText = *v 155 break 156 } 157 } 158 159 equivalent, err := awspolicy.PoliciesAreEquivalent(actualPolicyText, expectedPolicyText) 160 if err != nil { 161 return fmt.Errorf("Error testing policy equivalence: %s", err) 162 } 163 if !equivalent { 164 return fmt.Errorf("Non-equivalent policy error:\n\nexpected: %s\n\n got: %s\n", 165 expectedPolicyText, actualPolicyText) 166 } 167 168 return nil 169 } 170 } 171 172 func testAccCheckAWSSQSExistsWithDefaults(n string) resource.TestCheckFunc { 173 return func(s *terraform.State) error { 174 rs, ok := s.RootModule().Resources[n] 175 if !ok { 176 return fmt.Errorf("Not found: %s", n) 177 } 178 179 if rs.Primary.ID == "" { 180 return fmt.Errorf("No Queue URL specified!") 181 } 182 183 conn := testAccProvider.Meta().(*AWSClient).sqsconn 184 185 params := &sqs.GetQueueAttributesInput{ 186 QueueUrl: aws.String(rs.Primary.ID), 187 AttributeNames: []*string{aws.String("All")}, 188 } 189 resp, err := conn.GetQueueAttributes(params) 190 191 if err != nil { 192 return err 193 } 194 195 // checking if attributes are defaults 196 for k, v := range resp.Attributes { 197 if k == "VisibilityTimeout" && *v != "30" { 198 return fmt.Errorf("VisibilityTimeout (%s) was not set to 30", *v) 199 } 200 201 if k == "MessageRetentionPeriod" && *v != "345600" { 202 return fmt.Errorf("MessageRetentionPeriod (%s) was not set to 345600", *v) 203 } 204 205 if k == "MaximumMessageSize" && *v != "262144" { 206 return fmt.Errorf("MaximumMessageSize (%s) was not set to 262144", *v) 207 } 208 209 if k == "DelaySeconds" && *v != "0" { 210 return fmt.Errorf("DelaySeconds (%s) was not set to 0", *v) 211 } 212 213 if k == "ReceiveMessageWaitTimeSeconds" && *v != "0" { 214 return fmt.Errorf("ReceiveMessageWaitTimeSeconds (%s) was not set to 0", *v) 215 } 216 } 217 218 return nil 219 } 220 } 221 222 func testAccCheckAWSSQSExistsWithOverrides(n string) resource.TestCheckFunc { 223 return func(s *terraform.State) error { 224 rs, ok := s.RootModule().Resources[n] 225 if !ok { 226 return fmt.Errorf("Not found: %s", n) 227 } 228 229 if rs.Primary.ID == "" { 230 return fmt.Errorf("No Queue URL specified!") 231 } 232 233 conn := testAccProvider.Meta().(*AWSClient).sqsconn 234 235 params := &sqs.GetQueueAttributesInput{ 236 QueueUrl: aws.String(rs.Primary.ID), 237 AttributeNames: []*string{aws.String("All")}, 238 } 239 resp, err := conn.GetQueueAttributes(params) 240 241 if err != nil { 242 return err 243 } 244 245 // checking if attributes match our overrides 246 for k, v := range resp.Attributes { 247 if k == "VisibilityTimeout" && *v != "60" { 248 return fmt.Errorf("VisibilityTimeout (%s) was not set to 60", *v) 249 } 250 251 if k == "MessageRetentionPeriod" && *v != "86400" { 252 return fmt.Errorf("MessageRetentionPeriod (%s) was not set to 86400", *v) 253 } 254 255 if k == "MaximumMessageSize" && *v != "2048" { 256 return fmt.Errorf("MaximumMessageSize (%s) was not set to 2048", *v) 257 } 258 259 if k == "DelaySeconds" && *v != "90" { 260 return fmt.Errorf("DelaySeconds (%s) was not set to 90", *v) 261 } 262 263 if k == "ReceiveMessageWaitTimeSeconds" && *v != "10" { 264 return fmt.Errorf("ReceiveMessageWaitTimeSeconds (%s) was not set to 10", *v) 265 } 266 } 267 268 return nil 269 } 270 } 271 272 func testAccAWSSQSConfigWithDefaults(r string) string { 273 return fmt.Sprintf(` 274 resource "aws_sqs_queue" "queue" { 275 name = "%s" 276 } 277 `, r) 278 } 279 280 func testAccAWSSQSConfigWithOverrides(r string) string { 281 return fmt.Sprintf(` 282 resource "aws_sqs_queue" "queue" { 283 name = "%s" 284 delay_seconds = 90 285 max_message_size = 2048 286 message_retention_seconds = 86400 287 receive_wait_time_seconds = 10 288 visibility_timeout_seconds = 60 289 }`, r) 290 } 291 292 func testAccAWSSQSConfigWithRedrive(name string) string { 293 return fmt.Sprintf(` 294 resource "aws_sqs_queue" "my_queue" { 295 name = "tftestqueuq-%s" 296 delay_seconds = 0 297 visibility_timeout_seconds = 300 298 299 redrive_policy = <<EOF 300 { 301 "maxReceiveCount": 3, 302 "deadLetterTargetArn": "${aws_sqs_queue.my_dead_letter_queue.arn}" 303 } 304 EOF 305 } 306 307 resource "aws_sqs_queue" "my_dead_letter_queue" { 308 name = "tfotherqueuq-%s" 309 } 310 `, name, name) 311 } 312 313 func testAccAWSSQSConfig_PolicyFormat(queue, topic string) string { 314 return fmt.Sprintf(` 315 variable "sns_name" { 316 default = "%s" 317 } 318 319 variable "sqs_name" { 320 default = "%s" 321 } 322 323 resource "aws_sns_topic" "test_topic" { 324 name = "${var.sns_name}" 325 } 326 327 resource "aws_sqs_queue" "test-email-events" { 328 name = "${var.sqs_name}" 329 depends_on = ["aws_sns_topic.test_topic"] 330 delay_seconds = 90 331 max_message_size = 2048 332 message_retention_seconds = 86400 333 receive_wait_time_seconds = 10 334 visibility_timeout_seconds = 60 335 336 policy = <<EOF 337 { 338 "Version": "2012-10-17", 339 "Id": "sqspolicy", 340 "Statement": [ 341 { 342 "Sid": "Stmt1451501026839", 343 "Effect": "Allow", 344 "Principal": "*", 345 "Action": "sqs:SendMessage", 346 "Resource": "arn:aws:sqs:us-west-2:470663696735:${var.sqs_name}", 347 "Condition": { 348 "ArnEquals": { 349 "aws:SourceArn": "arn:aws:sns:us-west-2:470663696735:${var.sns_name}" 350 } 351 } 352 } 353 ] 354 } 355 EOF 356 } 357 358 resource "aws_sns_topic_subscription" "test_queue_target" { 359 topic_arn = "${aws_sns_topic.test_topic.arn}" 360 protocol = "sqs" 361 endpoint = "${aws_sqs_queue.test-email-events.arn}" 362 } 363 `, topic, queue) 364 }