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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"strconv"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/aws/aws-sdk-go/aws"
    12  	"github.com/aws/aws-sdk-go/service/kinesis"
    13  	"github.com/hashicorp/terraform/helper/resource"
    14  	"github.com/hashicorp/terraform/terraform"
    15  )
    16  
    17  func TestAccAWSKinesisStream_basic(t *testing.T) {
    18  	var stream kinesis.StreamDescription
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckKinesisStreamDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: testAccKinesisStreamConfig,
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream),
    29  					testAccCheckAWSKinesisStreamAttributes(&stream),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func testAccCheckKinesisStreamExists(n string, stream *kinesis.StreamDescription) resource.TestCheckFunc {
    37  	return func(s *terraform.State) error {
    38  		rs, ok := s.RootModule().Resources[n]
    39  		if !ok {
    40  			return fmt.Errorf("Not found: %s", n)
    41  		}
    42  
    43  		if rs.Primary.ID == "" {
    44  			return fmt.Errorf("No Kinesis ID is set")
    45  		}
    46  
    47  		conn := testAccProvider.Meta().(*AWSClient).kinesisconn
    48  		describeOpts := &kinesis.DescribeStreamInput{
    49  			StreamName: aws.String(rs.Primary.Attributes["name"]),
    50  		}
    51  		resp, err := conn.DescribeStream(describeOpts)
    52  		if err != nil {
    53  			return err
    54  		}
    55  
    56  		*stream = *resp.StreamDescription
    57  
    58  		return nil
    59  	}
    60  }
    61  
    62  func testAccCheckAWSKinesisStreamAttributes(stream *kinesis.StreamDescription) resource.TestCheckFunc {
    63  	return func(s *terraform.State) error {
    64  		if !strings.HasPrefix(*stream.StreamName, "terraform-kinesis-test") {
    65  			return fmt.Errorf("Bad Stream name: %s", *stream.StreamName)
    66  		}
    67  		for _, rs := range s.RootModule().Resources {
    68  			if rs.Type != "aws_kinesis_stream" {
    69  				continue
    70  			}
    71  			if *stream.StreamARN != rs.Primary.Attributes["arn"] {
    72  				return fmt.Errorf("Bad Stream ARN\n\t expected: %s\n\tgot: %s\n", rs.Primary.Attributes["arn"], *stream.StreamARN)
    73  			}
    74  			shard_count := strconv.Itoa(len(stream.Shards))
    75  			if shard_count != rs.Primary.Attributes["shard_count"] {
    76  				return fmt.Errorf("Bad Stream Shard Count\n\t expected: %s\n\tgot: %s\n", rs.Primary.Attributes["shard_count"], shard_count)
    77  			}
    78  		}
    79  		return nil
    80  	}
    81  }
    82  
    83  func testAccCheckKinesisStreamDestroy(s *terraform.State) error {
    84  	for _, rs := range s.RootModule().Resources {
    85  		if rs.Type != "aws_kinesis_stream" {
    86  			continue
    87  		}
    88  		conn := testAccProvider.Meta().(*AWSClient).kinesisconn
    89  		describeOpts := &kinesis.DescribeStreamInput{
    90  			StreamName: aws.String(rs.Primary.Attributes["name"]),
    91  		}
    92  		resp, err := conn.DescribeStream(describeOpts)
    93  		if err == nil {
    94  			if resp.StreamDescription != nil && *resp.StreamDescription.StreamStatus != "DELETING" {
    95  				return fmt.Errorf("Error: Stream still exists")
    96  			}
    97  		}
    98  
    99  		return nil
   100  
   101  	}
   102  
   103  	return nil
   104  }
   105  
   106  var testAccKinesisStreamConfig = fmt.Sprintf(`
   107  resource "aws_kinesis_stream" "test_stream" {
   108  	name = "terraform-kinesis-test-%d"
   109  	shard_count = 2
   110  	tags {
   111  		Name = "tf-test"
   112  	}
   113  }
   114  `, rand.New(rand.NewSource(time.Now().UnixNano())).Int())