github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSSQSQueue_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAWSSQSQueueDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAWSSQSConfigWithDefaults,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.queue-with-defaults"),
    24  				),
    25  			},
    26  			resource.TestStep{
    27  				Config: testAccAWSSQSConfigWithOverrides,
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckAWSSQSExistsWithOverrides("aws_sqs_queue.queue-with-overrides"),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func testAccCheckAWSSQSQueueDestroy(s *terraform.State) error {
    37  	conn := testAccProvider.Meta().(*AWSClient).sqsconn
    38  
    39  	for _, rs := range s.RootModule().Resources {
    40  		if rs.Type != "aws_sqs_queue" {
    41  			continue
    42  		}
    43  
    44  		// Check if queue exists by checking for its attributes
    45  		params := &sqs.GetQueueAttributesInput{
    46  			QueueUrl: aws.String(rs.Primary.ID),
    47  		}
    48  		_, err := conn.GetQueueAttributes(params)
    49  		if err == nil {
    50  			return fmt.Errorf("Queue %s still exists. Failing!", rs.Primary.ID)
    51  		}
    52  
    53  		// Verify the error is what we want
    54  		_, ok := err.(awserr.Error)
    55  		if !ok {
    56  			return err
    57  		}
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  func testAccCheckAWSSQSExistsWithDefaults(n string) resource.TestCheckFunc {
    64  	return func(s *terraform.State) error {
    65  		rs, ok := s.RootModule().Resources[n]
    66  		if !ok {
    67  			return fmt.Errorf("Not found: %s", n)
    68  		}
    69  
    70  		if rs.Primary.ID == "" {
    71  			return fmt.Errorf("No Queue URL specified!")
    72  		}
    73  
    74  		conn := testAccProvider.Meta().(*AWSClient).sqsconn
    75  
    76  		params := &sqs.GetQueueAttributesInput{
    77  			QueueUrl:       aws.String(rs.Primary.ID),
    78  			AttributeNames: []*string{aws.String("All")},
    79  		}
    80  		resp, err := conn.GetQueueAttributes(params)
    81  
    82  		if err != nil {
    83  			return err
    84  		}
    85  
    86  		// checking if attributes are defaults
    87  		for k, v := range resp.Attributes {
    88  			if k == "VisibilityTimeout" && *v != "30" {
    89  				return fmt.Errorf("VisibilityTimeout (%s) was not set to 30", *v)
    90  			}
    91  
    92  			if k == "MessageRetentionPeriod" && *v != "345600" {
    93  				return fmt.Errorf("MessageRetentionPeriod (%s) was not set to 345600", *v)
    94  			}
    95  
    96  			if k == "MaximumMessageSize" && *v != "262144" {
    97  				return fmt.Errorf("MaximumMessageSize (%s) was not set to 262144", *v)
    98  			}
    99  
   100  			if k == "DelaySeconds" && *v != "0" {
   101  				return fmt.Errorf("DelaySeconds (%s) was not set to 0", *v)
   102  			}
   103  
   104  			if k == "ReceiveMessageWaitTimeSeconds" && *v != "0" {
   105  				return fmt.Errorf("ReceiveMessageWaitTimeSeconds (%s) was not set to 0", *v)
   106  			}
   107  		}
   108  
   109  		return nil
   110  	}
   111  }
   112  
   113  func testAccCheckAWSSQSExistsWithOverrides(n string) resource.TestCheckFunc {
   114  	return func(s *terraform.State) error {
   115  		rs, ok := s.RootModule().Resources[n]
   116  		if !ok {
   117  			return fmt.Errorf("Not found: %s", n)
   118  		}
   119  
   120  		if rs.Primary.ID == "" {
   121  			return fmt.Errorf("No Queue URL specified!")
   122  		}
   123  
   124  		conn := testAccProvider.Meta().(*AWSClient).sqsconn
   125  
   126  		params := &sqs.GetQueueAttributesInput{
   127  			QueueUrl:       aws.String(rs.Primary.ID),
   128  			AttributeNames: []*string{aws.String("All")},
   129  		}
   130  		resp, err := conn.GetQueueAttributes(params)
   131  
   132  		if err != nil {
   133  			return err
   134  		}
   135  
   136  		// checking if attributes match our overrides
   137  		for k, v := range resp.Attributes {
   138  			if k == "VisibilityTimeout" && *v != "60" {
   139  				return fmt.Errorf("VisibilityTimeout (%s) was not set to 60", *v)
   140  			}
   141  
   142  			if k == "MessageRetentionPeriod" && *v != "86400" {
   143  				return fmt.Errorf("MessageRetentionPeriod (%s) was not set to 86400", *v)
   144  			}
   145  
   146  			if k == "MaximumMessageSize" && *v != "2048" {
   147  				return fmt.Errorf("MaximumMessageSize (%s) was not set to 2048", *v)
   148  			}
   149  
   150  			if k == "DelaySeconds" && *v != "90" {
   151  				return fmt.Errorf("DelaySeconds (%s) was not set to 90", *v)
   152  			}
   153  
   154  			if k == "ReceiveMessageWaitTimeSeconds" && *v != "10" {
   155  				return fmt.Errorf("ReceiveMessageWaitTimeSeconds (%s) was not set to 10", *v)
   156  			}
   157  		}
   158  
   159  		return nil
   160  	}
   161  }
   162  
   163  const testAccAWSSQSConfigWithDefaults = `
   164  resource "aws_sqs_queue" "queue-with-defaults" {
   165      name = "test-sqs-queue-with-defaults"
   166  }
   167  `
   168  
   169  const testAccAWSSQSConfigWithOverrides = `
   170  resource "aws_sqs_queue" "queue-with-overrides" {
   171  	name = "test-sqs-queue-with-overrides"
   172  	delay_seconds = 90
   173    	max_message_size = 2048
   174    	message_retention_seconds = 86400
   175    	receive_wait_time_seconds = 10
   176    	visibility_timeout_seconds = 60
   177  }
   178  `