github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/builtin/providers/aws/resource_aws_kinesis_stream_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "math/rand" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/service/kinesis" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccKinesisStream_basic(t *testing.T) { 17 var stream kinesis.StreamDescription 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckKinesisStreamDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccKinesisStreamConfig, 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), 28 testAccCheckAWSKinesisStreamAttributes(&stream), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func testAccCheckKinesisStreamExists(n string, stream *kinesis.StreamDescription) resource.TestCheckFunc { 36 return func(s *terraform.State) error { 37 rs, ok := s.RootModule().Resources[n] 38 if !ok { 39 return fmt.Errorf("Not found: %s", n) 40 } 41 42 if rs.Primary.ID == "" { 43 return fmt.Errorf("No Kinesis ID is set") 44 } 45 46 conn := testAccProvider.Meta().(*AWSClient).kinesisconn 47 describeOpts := &kinesis.DescribeStreamInput{ 48 StreamName: aws.String(rs.Primary.Attributes["name"]), 49 Limit: aws.Long(1), 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 } 75 return nil 76 } 77 } 78 79 func testAccCheckKinesisStreamDestroy(s *terraform.State) error { 80 for _, rs := range s.RootModule().Resources { 81 if rs.Type != "aws_kinesis_stream" { 82 continue 83 } 84 conn := testAccProvider.Meta().(*AWSClient).kinesisconn 85 describeOpts := &kinesis.DescribeStreamInput{ 86 StreamName: aws.String(rs.Primary.Attributes["name"]), 87 Limit: aws.Long(1), 88 } 89 resp, err := conn.DescribeStream(describeOpts) 90 if err == nil { 91 if resp.StreamDescription != nil && *resp.StreamDescription.StreamStatus != "DELETING" { 92 return fmt.Errorf("Error: Stream still exists") 93 } 94 } 95 96 return nil 97 98 } 99 100 return nil 101 } 102 103 var testAccKinesisStreamConfig = fmt.Sprintf(` 104 resource "aws_kinesis_stream" "test_stream" { 105 name = "terraform-kinesis-test-%d" 106 shard_count = 1 107 } 108 `, rand.New(rand.NewSource(time.Now().UnixNano())).Int())