github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_kinesis_firehose_delivery_stream_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"math/rand"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/aws/aws-sdk-go/aws"
    12  	"github.com/aws/aws-sdk-go/service/firehose"
    13  	"github.com/hashicorp/terraform/helper/resource"
    14  	"github.com/hashicorp/terraform/terraform"
    15  )
    16  
    17  func TestAccAWSKinesisFirehoseDeliveryStream_basic(t *testing.T) {
    18  	var stream firehose.DeliveryStreamDescription
    19  
    20  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
    21  	config := fmt.Sprintf(testAccKinesisFirehoseDeliveryStreamConfig_basic, ri, ri)
    22  
    23  	resource.Test(t, resource.TestCase{
    24  		PreCheck:     func() { testAccPreCheck(t) },
    25  		Providers:    testAccProviders,
    26  		CheckDestroy: testAccCheckKinesisFirehoseDeliveryStreamDestroy,
    27  		Steps: []resource.TestStep{
    28  			resource.TestStep{
    29  				Config: config,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckKinesisFirehoseDeliveryStreamExists("aws_kinesis_firehose_delivery_stream.test_stream", &stream),
    32  					testAccCheckAWSKinesisFirehoseDeliveryStreamAttributes(&stream),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccAWSKinesisFirehoseDeliveryStream_s3ConfigUpdates(t *testing.T) {
    40  	var stream firehose.DeliveryStreamDescription
    41  
    42  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
    43  	preconfig := fmt.Sprintf(testAccKinesisFirehoseDeliveryStreamConfig_s3, ri, ri)
    44  	postConfig := fmt.Sprintf(testAccKinesisFirehoseDeliveryStreamConfig_s3Updates, ri, ri)
    45  
    46  	resource.Test(t, resource.TestCase{
    47  		PreCheck:     func() { testAccPreCheck(t) },
    48  		Providers:    testAccProviders,
    49  		CheckDestroy: testAccCheckKinesisFirehoseDeliveryStreamDestroy,
    50  		Steps: []resource.TestStep{
    51  			resource.TestStep{
    52  				Config: preconfig,
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testAccCheckKinesisFirehoseDeliveryStreamExists("aws_kinesis_firehose_delivery_stream.test_stream", &stream),
    55  					testAccCheckAWSKinesisFirehoseDeliveryStreamAttributes(&stream),
    56  					resource.TestCheckResourceAttr(
    57  						"aws_kinesis_firehose_delivery_stream.test_stream", "s3_buffer_size", "5"),
    58  					resource.TestCheckResourceAttr(
    59  						"aws_kinesis_firehose_delivery_stream.test_stream", "s3_buffer_interval", "300"),
    60  					resource.TestCheckResourceAttr(
    61  						"aws_kinesis_firehose_delivery_stream.test_stream", "s3_data_compression", "UNCOMPRESSED"),
    62  				),
    63  			},
    64  
    65  			resource.TestStep{
    66  				Config: postConfig,
    67  				Check: resource.ComposeTestCheckFunc(
    68  					testAccCheckKinesisFirehoseDeliveryStreamExists("aws_kinesis_firehose_delivery_stream.test_stream", &stream),
    69  					testAccCheckAWSKinesisFirehoseDeliveryStreamAttributes(&stream),
    70  					resource.TestCheckResourceAttr(
    71  						"aws_kinesis_firehose_delivery_stream.test_stream", "s3_buffer_size", "10"),
    72  					resource.TestCheckResourceAttr(
    73  						"aws_kinesis_firehose_delivery_stream.test_stream", "s3_buffer_interval", "400"),
    74  					resource.TestCheckResourceAttr(
    75  						"aws_kinesis_firehose_delivery_stream.test_stream", "s3_data_compression", "GZIP"),
    76  				),
    77  			},
    78  		},
    79  	})
    80  }
    81  
    82  func testAccCheckKinesisFirehoseDeliveryStreamExists(n string, stream *firehose.DeliveryStreamDescription) resource.TestCheckFunc {
    83  	return func(s *terraform.State) error {
    84  		rs, ok := s.RootModule().Resources[n]
    85  		log.Printf("State: %#v", s.RootModule().Resources)
    86  		if !ok {
    87  			return fmt.Errorf("Not found: %s", n)
    88  		}
    89  
    90  		if rs.Primary.ID == "" {
    91  			return fmt.Errorf("No Kinesis Firehose ID is set")
    92  		}
    93  
    94  		conn := testAccProvider.Meta().(*AWSClient).firehoseconn
    95  		describeOpts := &firehose.DescribeDeliveryStreamInput{
    96  			DeliveryStreamName: aws.String(rs.Primary.Attributes["name"]),
    97  		}
    98  		resp, err := conn.DescribeDeliveryStream(describeOpts)
    99  		if err != nil {
   100  			return err
   101  		}
   102  
   103  		*stream = *resp.DeliveryStreamDescription
   104  
   105  		return nil
   106  	}
   107  }
   108  
   109  func testAccCheckAWSKinesisFirehoseDeliveryStreamAttributes(stream *firehose.DeliveryStreamDescription) resource.TestCheckFunc {
   110  	return func(s *terraform.State) error {
   111  		if !strings.HasPrefix(*stream.DeliveryStreamName, "terraform-kinesis-firehose") {
   112  			return fmt.Errorf("Bad Stream name: %s", *stream.DeliveryStreamName)
   113  		}
   114  		for _, rs := range s.RootModule().Resources {
   115  			if rs.Type != "aws_kinesis_firehose_delivery_stream" {
   116  				continue
   117  			}
   118  			if *stream.DeliveryStreamARN != rs.Primary.Attributes["arn"] {
   119  				return fmt.Errorf("Bad Delivery Stream ARN\n\t expected: %s\n\tgot: %s\n", rs.Primary.Attributes["arn"], *stream.DeliveryStreamARN)
   120  			}
   121  		}
   122  		return nil
   123  	}
   124  }
   125  
   126  func testAccCheckKinesisFirehoseDeliveryStreamDestroy(s *terraform.State) error {
   127  	for _, rs := range s.RootModule().Resources {
   128  		if rs.Type != "aws_kinesis_firehose_delivery_stream" {
   129  			continue
   130  		}
   131  		conn := testAccProvider.Meta().(*AWSClient).firehoseconn
   132  		describeOpts := &firehose.DescribeDeliveryStreamInput{
   133  			DeliveryStreamName: aws.String(rs.Primary.Attributes["name"]),
   134  		}
   135  		resp, err := conn.DescribeDeliveryStream(describeOpts)
   136  		if err == nil {
   137  			if resp.DeliveryStreamDescription != nil && *resp.DeliveryStreamDescription.DeliveryStreamStatus != "DELETING" {
   138  				return fmt.Errorf("Error: Delivery Stream still exists")
   139  			}
   140  		}
   141  
   142  		return nil
   143  
   144  	}
   145  
   146  	return nil
   147  }
   148  
   149  var testAccKinesisFirehoseDeliveryStreamConfig_basic = `
   150  resource "aws_s3_bucket" "bucket" {
   151  	bucket = "tf-test-bucket-%d"
   152  	acl = "private"
   153  }
   154  
   155  resource "aws_kinesis_firehose_delivery_stream" "test_stream" {
   156  	name = "terraform-kinesis-firehose-basictest-%d"
   157  	destination = "s3"
   158  	role_arn = "arn:aws:iam::946579370547:role/firehose_delivery_role"
   159  	s3_bucket_arn = "${aws_s3_bucket.bucket.arn}"
   160  }`
   161  
   162  var testAccKinesisFirehoseDeliveryStreamConfig_s3 = `
   163  resource "aws_s3_bucket" "bucket" {
   164  	bucket = "tf-test-bucket-%d"
   165  	acl = "private"
   166  }
   167  
   168  resource "aws_kinesis_firehose_delivery_stream" "test_stream" {
   169  	name = "terraform-kinesis-firehose-s3test-%d"
   170  	destination = "s3"
   171  	role_arn = "arn:aws:iam::946579370547:role/firehose_delivery_role"
   172  	s3_bucket_arn = "${aws_s3_bucket.bucket.arn}"
   173  }`
   174  
   175  var testAccKinesisFirehoseDeliveryStreamConfig_s3Updates = `
   176  resource "aws_s3_bucket" "bucket" {
   177  	bucket = "tf-test-bucket-01-%d"
   178  	acl = "private"
   179  }
   180  
   181  resource "aws_kinesis_firehose_delivery_stream" "test_stream" {
   182  	name = "terraform-kinesis-firehose-s3test-%d"
   183  	destination = "s3"
   184  	role_arn = "arn:aws:iam::946579370547:role/firehose_delivery_role"
   185  	s3_bucket_arn = "${aws_s3_bucket.bucket.arn}"
   186  	s3_buffer_size = 10
   187  	s3_buffer_interval = 400
   188  	s3_data_compression = "GZIP"
   189  }`