github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 "regexp" 15 ) 16 17 func TestAccAWSSQSQueue_basic(t *testing.T) { 18 queueName := fmt.Sprintf("sqs-queue-%s", acctest.RandString(10)) 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckAWSSQSQueueDestroy, 23 Steps: []resource.TestStep{ 24 { 25 Config: testAccAWSSQSConfigWithDefaults(queueName), 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.queue"), 28 ), 29 }, 30 { 31 Config: testAccAWSSQSConfigWithOverrides(queueName), 32 Check: resource.ComposeTestCheckFunc( 33 testAccCheckAWSSQSExistsWithOverrides("aws_sqs_queue.queue"), 34 ), 35 }, 36 { 37 Config: testAccAWSSQSConfigWithDefaults(queueName), 38 Check: resource.ComposeTestCheckFunc( 39 testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.queue"), 40 ), 41 }, 42 }, 43 }) 44 } 45 46 func TestAccAWSSQSQueue_policy(t *testing.T) { 47 queueName := fmt.Sprintf("sqs-queue-%s", acctest.RandString(10)) 48 topicName := fmt.Sprintf("sns-topic-%s", acctest.RandString(10)) 49 50 expectedPolicyText := fmt.Sprintf( 51 `{"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"}}}]}`, 52 topicName, queueName) 53 54 resource.Test(t, resource.TestCase{ 55 PreCheck: func() { testAccPreCheck(t) }, 56 Providers: testAccProviders, 57 CheckDestroy: testAccCheckAWSSQSQueueDestroy, 58 Steps: []resource.TestStep{ 59 { 60 Config: testAccAWSSQSConfig_PolicyFormat(topicName, queueName), 61 Check: resource.ComposeTestCheckFunc( 62 testAccCheckAWSQSHasPolicy("aws_sqs_queue.test-email-events", expectedPolicyText), 63 ), 64 }, 65 }, 66 }) 67 } 68 69 func TestAccAWSSQSQueue_redrivePolicy(t *testing.T) { 70 resource.Test(t, resource.TestCase{ 71 PreCheck: func() { testAccPreCheck(t) }, 72 Providers: testAccProviders, 73 CheckDestroy: testAccCheckAWSSQSQueueDestroy, 74 Steps: []resource.TestStep{ 75 { 76 Config: testAccAWSSQSConfigWithRedrive(acctest.RandString(10)), 77 Check: resource.ComposeTestCheckFunc( 78 testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.my_dead_letter_queue"), 79 ), 80 }, 81 }, 82 }) 83 } 84 85 // Tests formatting and compacting of Policy, Redrive json 86 func TestAccAWSSQSQueue_Policybasic(t *testing.T) { 87 queueName := fmt.Sprintf("sqs-queue-%s", acctest.RandString(10)) 88 topicName := fmt.Sprintf("sns-topic-%s", acctest.RandString(10)) 89 resource.Test(t, resource.TestCase{ 90 PreCheck: func() { testAccPreCheck(t) }, 91 Providers: testAccProviders, 92 CheckDestroy: testAccCheckAWSSQSQueueDestroy, 93 Steps: []resource.TestStep{ 94 { 95 Config: testAccAWSSQSConfig_PolicyFormat(topicName, queueName), 96 Check: resource.ComposeTestCheckFunc( 97 testAccCheckAWSSQSExistsWithOverrides("aws_sqs_queue.test-email-events"), 98 ), 99 }, 100 }, 101 }) 102 } 103 104 func TestAccAWSSQSQueue_FIFO(t *testing.T) { 105 106 resource.Test(t, resource.TestCase{ 107 PreCheck: func() { testAccPreCheck(t) }, 108 Providers: testAccProviders, 109 CheckDestroy: testAccCheckAWSSQSQueueDestroy, 110 Steps: []resource.TestStep{ 111 { 112 Config: testAccAWSSQSConfigWithFIFO(acctest.RandString(10)), 113 Check: resource.ComposeTestCheckFunc( 114 testAccCheckAWSSQSExists("aws_sqs_queue.queue"), 115 resource.TestCheckResourceAttr("aws_sqs_queue.queue", "fifo_queue", "true"), 116 ), 117 }, 118 }, 119 }) 120 } 121 122 func TestAccAWSSQSQueue_FIFOExpectNameError(t *testing.T) { 123 resource.Test(t, resource.TestCase{ 124 PreCheck: func() { testAccPreCheck(t) }, 125 Providers: testAccProviders, 126 CheckDestroy: testAccCheckAWSSQSQueueDestroy, 127 Steps: []resource.TestStep{ 128 { 129 Config: testAccAWSSQSConfigWithFIFOExpectError(acctest.RandString(10)), 130 ExpectError: regexp.MustCompile(`Error validating the FIFO queue name`), 131 }, 132 }, 133 }) 134 } 135 136 func TestAccAWSSQSQueue_FIFOWithContentBasedDeduplication(t *testing.T) { 137 resource.Test(t, resource.TestCase{ 138 PreCheck: func() { testAccPreCheck(t) }, 139 Providers: testAccProviders, 140 CheckDestroy: testAccCheckAWSSQSQueueDestroy, 141 Steps: []resource.TestStep{ 142 { 143 Config: testAccAWSSQSConfigWithFIFOContentBasedDeduplication(acctest.RandString(10)), 144 Check: resource.ComposeTestCheckFunc( 145 testAccCheckAWSSQSExists("aws_sqs_queue.queue"), 146 resource.TestCheckResourceAttr("aws_sqs_queue.queue", "fifo_queue", "true"), 147 resource.TestCheckResourceAttr("aws_sqs_queue.queue", "content_based_deduplication", "true"), 148 ), 149 }, 150 }, 151 }) 152 } 153 154 func TestAccAWSSQSQueue_ExpectContentBasedDeduplicationError(t *testing.T) { 155 resource.Test(t, resource.TestCase{ 156 PreCheck: func() { testAccPreCheck(t) }, 157 Providers: testAccProviders, 158 CheckDestroy: testAccCheckAWSSQSQueueDestroy, 159 Steps: []resource.TestStep{ 160 { 161 Config: testAccExpectContentBasedDeduplicationError(acctest.RandString(10)), 162 ExpectError: regexp.MustCompile(`Content based deduplication can only be set with FIFO queues`), 163 }, 164 }, 165 }) 166 } 167 168 func testAccCheckAWSSQSQueueDestroy(s *terraform.State) error { 169 conn := testAccProvider.Meta().(*AWSClient).sqsconn 170 171 for _, rs := range s.RootModule().Resources { 172 if rs.Type != "aws_sqs_queue" { 173 continue 174 } 175 176 // Check if queue exists by checking for its attributes 177 params := &sqs.GetQueueAttributesInput{ 178 QueueUrl: aws.String(rs.Primary.ID), 179 } 180 _, err := conn.GetQueueAttributes(params) 181 if err == nil { 182 return fmt.Errorf("Queue %s still exists. Failing!", rs.Primary.ID) 183 } 184 185 // Verify the error is what we want 186 _, ok := err.(awserr.Error) 187 if !ok { 188 return err 189 } 190 } 191 192 return nil 193 } 194 func testAccCheckAWSQSHasPolicy(n string, expectedPolicyText string) resource.TestCheckFunc { 195 return func(s *terraform.State) error { 196 rs, ok := s.RootModule().Resources[n] 197 if !ok { 198 return fmt.Errorf("Not found: %s", n) 199 } 200 201 if rs.Primary.ID == "" { 202 return fmt.Errorf("No Queue URL specified!") 203 } 204 205 conn := testAccProvider.Meta().(*AWSClient).sqsconn 206 207 params := &sqs.GetQueueAttributesInput{ 208 QueueUrl: aws.String(rs.Primary.ID), 209 AttributeNames: []*string{aws.String("Policy")}, 210 } 211 resp, err := conn.GetQueueAttributes(params) 212 if err != nil { 213 return err 214 } 215 216 var actualPolicyText string 217 for k, v := range resp.Attributes { 218 if k == "Policy" { 219 actualPolicyText = *v 220 break 221 } 222 } 223 224 equivalent, err := awspolicy.PoliciesAreEquivalent(actualPolicyText, expectedPolicyText) 225 if err != nil { 226 return fmt.Errorf("Error testing policy equivalence: %s", err) 227 } 228 if !equivalent { 229 return fmt.Errorf("Non-equivalent policy error:\n\nexpected: %s\n\n got: %s\n", 230 expectedPolicyText, actualPolicyText) 231 } 232 233 return nil 234 } 235 } 236 237 func testAccCheckAWSSQSExists(n string) resource.TestCheckFunc { 238 return func(s *terraform.State) error { 239 rs, ok := s.RootModule().Resources[n] 240 if !ok { 241 return fmt.Errorf("Not found: %s", n) 242 } 243 244 if rs.Primary.ID == "" { 245 return fmt.Errorf("No Queue URL specified!") 246 } 247 248 return nil 249 } 250 } 251 252 func testAccCheckAWSSQSExistsWithDefaults(n string) resource.TestCheckFunc { 253 return func(s *terraform.State) error { 254 rs, ok := s.RootModule().Resources[n] 255 if !ok { 256 return fmt.Errorf("Not found: %s", n) 257 } 258 259 if rs.Primary.ID == "" { 260 return fmt.Errorf("No Queue URL specified!") 261 } 262 263 conn := testAccProvider.Meta().(*AWSClient).sqsconn 264 265 params := &sqs.GetQueueAttributesInput{ 266 QueueUrl: aws.String(rs.Primary.ID), 267 AttributeNames: []*string{aws.String("All")}, 268 } 269 resp, err := conn.GetQueueAttributes(params) 270 271 if err != nil { 272 return err 273 } 274 275 // checking if attributes are defaults 276 for k, v := range resp.Attributes { 277 if k == "VisibilityTimeout" && *v != "30" { 278 return fmt.Errorf("VisibilityTimeout (%s) was not set to 30", *v) 279 } 280 281 if k == "MessageRetentionPeriod" && *v != "345600" { 282 return fmt.Errorf("MessageRetentionPeriod (%s) was not set to 345600", *v) 283 } 284 285 if k == "MaximumMessageSize" && *v != "262144" { 286 return fmt.Errorf("MaximumMessageSize (%s) was not set to 262144", *v) 287 } 288 289 if k == "DelaySeconds" && *v != "0" { 290 return fmt.Errorf("DelaySeconds (%s) was not set to 0", *v) 291 } 292 293 if k == "ReceiveMessageWaitTimeSeconds" && *v != "0" { 294 return fmt.Errorf("ReceiveMessageWaitTimeSeconds (%s) was not set to 0", *v) 295 } 296 } 297 298 return nil 299 } 300 } 301 302 func testAccCheckAWSSQSExistsWithOverrides(n string) resource.TestCheckFunc { 303 return func(s *terraform.State) error { 304 rs, ok := s.RootModule().Resources[n] 305 if !ok { 306 return fmt.Errorf("Not found: %s", n) 307 } 308 309 if rs.Primary.ID == "" { 310 return fmt.Errorf("No Queue URL specified!") 311 } 312 313 conn := testAccProvider.Meta().(*AWSClient).sqsconn 314 315 params := &sqs.GetQueueAttributesInput{ 316 QueueUrl: aws.String(rs.Primary.ID), 317 AttributeNames: []*string{aws.String("All")}, 318 } 319 resp, err := conn.GetQueueAttributes(params) 320 321 if err != nil { 322 return err 323 } 324 325 // checking if attributes match our overrides 326 for k, v := range resp.Attributes { 327 if k == "VisibilityTimeout" && *v != "60" { 328 return fmt.Errorf("VisibilityTimeout (%s) was not set to 60", *v) 329 } 330 331 if k == "MessageRetentionPeriod" && *v != "86400" { 332 return fmt.Errorf("MessageRetentionPeriod (%s) was not set to 86400", *v) 333 } 334 335 if k == "MaximumMessageSize" && *v != "2048" { 336 return fmt.Errorf("MaximumMessageSize (%s) was not set to 2048", *v) 337 } 338 339 if k == "DelaySeconds" && *v != "90" { 340 return fmt.Errorf("DelaySeconds (%s) was not set to 90", *v) 341 } 342 343 if k == "ReceiveMessageWaitTimeSeconds" && *v != "10" { 344 return fmt.Errorf("ReceiveMessageWaitTimeSeconds (%s) was not set to 10", *v) 345 } 346 } 347 348 return nil 349 } 350 } 351 352 func testAccAWSSQSConfigWithDefaults(r string) string { 353 return fmt.Sprintf(` 354 resource "aws_sqs_queue" "queue" { 355 name = "%s" 356 } 357 `, r) 358 } 359 360 func testAccAWSSQSFifoConfigWithDefaults(r string) string { 361 return fmt.Sprintf(` 362 resource "aws_sqs_queue" "queue" { 363 name = "%s.fifo" 364 fifo_queue = true 365 } 366 `, r) 367 } 368 369 func testAccAWSSQSConfigWithOverrides(r string) string { 370 return fmt.Sprintf(` 371 resource "aws_sqs_queue" "queue" { 372 name = "%s" 373 delay_seconds = 90 374 max_message_size = 2048 375 message_retention_seconds = 86400 376 receive_wait_time_seconds = 10 377 visibility_timeout_seconds = 60 378 }`, r) 379 } 380 381 func testAccAWSSQSConfigWithRedrive(name string) string { 382 return fmt.Sprintf(` 383 resource "aws_sqs_queue" "my_queue" { 384 name = "tftestqueuq-%s" 385 delay_seconds = 0 386 visibility_timeout_seconds = 300 387 388 redrive_policy = <<EOF 389 { 390 "maxReceiveCount": 3, 391 "deadLetterTargetArn": "${aws_sqs_queue.my_dead_letter_queue.arn}" 392 } 393 EOF 394 } 395 396 resource "aws_sqs_queue" "my_dead_letter_queue" { 397 name = "tfotherqueuq-%s" 398 } 399 `, name, name) 400 } 401 402 func testAccAWSSQSConfig_PolicyFormat(queue, topic string) string { 403 return fmt.Sprintf(` 404 variable "sns_name" { 405 default = "%s" 406 } 407 408 variable "sqs_name" { 409 default = "%s" 410 } 411 412 resource "aws_sns_topic" "test_topic" { 413 name = "${var.sns_name}" 414 } 415 416 resource "aws_sqs_queue" "test-email-events" { 417 name = "${var.sqs_name}" 418 depends_on = ["aws_sns_topic.test_topic"] 419 delay_seconds = 90 420 max_message_size = 2048 421 message_retention_seconds = 86400 422 receive_wait_time_seconds = 10 423 visibility_timeout_seconds = 60 424 425 policy = <<EOF 426 { 427 "Version": "2012-10-17", 428 "Id": "sqspolicy", 429 "Statement": [ 430 { 431 "Sid": "Stmt1451501026839", 432 "Effect": "Allow", 433 "Principal": "*", 434 "Action": "sqs:SendMessage", 435 "Resource": "arn:aws:sqs:us-west-2:470663696735:${var.sqs_name}", 436 "Condition": { 437 "ArnEquals": { 438 "aws:SourceArn": "arn:aws:sns:us-west-2:470663696735:${var.sns_name}" 439 } 440 } 441 } 442 ] 443 } 444 EOF 445 } 446 447 resource "aws_sns_topic_subscription" "test_queue_target" { 448 topic_arn = "${aws_sns_topic.test_topic.arn}" 449 protocol = "sqs" 450 endpoint = "${aws_sqs_queue.test-email-events.arn}" 451 } 452 `, topic, queue) 453 } 454 455 func testAccAWSSQSConfigWithFIFO(queue string) string { 456 return fmt.Sprintf(` 457 resource "aws_sqs_queue" "queue" { 458 name = "%s.fifo" 459 fifo_queue = true 460 } 461 `, queue) 462 } 463 464 func testAccAWSSQSConfigWithFIFOContentBasedDeduplication(queue string) string { 465 return fmt.Sprintf(` 466 resource "aws_sqs_queue" "queue" { 467 name = "%s.fifo" 468 fifo_queue = true 469 content_based_deduplication = true 470 } 471 `, queue) 472 } 473 474 func testAccAWSSQSConfigWithFIFOExpectError(queue string) string { 475 return fmt.Sprintf(` 476 resource "aws_sqs_queue" "queue" { 477 name = "%s" 478 fifo_queue = true 479 } 480 `, queue) 481 } 482 483 func testAccExpectContentBasedDeduplicationError(queue string) string { 484 return fmt.Sprintf(` 485 resource "aws_sqs_queue" "queue" { 486 name = "%s" 487 content_based_deduplication = true 488 } 489 `, queue) 490 }