github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/resource_aws_kinesis_stream_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 "testing" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/service/kinesis" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSKinesisStream_basic(t *testing.T) { 17 var stream kinesis.StreamDescription 18 19 rInt := acctest.RandInt() 20 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { testAccPreCheck(t) }, 23 Providers: testAccProviders, 24 CheckDestroy: testAccCheckKinesisStreamDestroy, 25 Steps: []resource.TestStep{ 26 { 27 Config: testAccKinesisStreamConfig(rInt), 28 Check: resource.ComposeTestCheckFunc( 29 testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), 30 testAccCheckAWSKinesisStreamAttributes(&stream), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccAWSKinesisStream_importBasic(t *testing.T) { 38 rInt := acctest.RandInt() 39 resourceName := "aws_kinesis_stream.test_stream" 40 streamName := fmt.Sprintf("terraform-kinesis-test-%d", rInt) 41 42 resource.Test(t, resource.TestCase{ 43 PreCheck: func() { testAccPreCheck(t) }, 44 Providers: testAccProviders, 45 CheckDestroy: testAccCheckKinesisStreamDestroy, 46 Steps: []resource.TestStep{ 47 { 48 Config: testAccKinesisStreamConfig(rInt), 49 }, 50 51 { 52 ResourceName: resourceName, 53 ImportState: true, 54 ImportStateVerify: true, 55 ImportStateId: streamName, 56 }, 57 }, 58 }) 59 } 60 61 func TestAccAWSKinesisStream_shardCount(t *testing.T) { 62 var stream kinesis.StreamDescription 63 64 rInt := acctest.RandInt() 65 66 resource.Test(t, resource.TestCase{ 67 PreCheck: func() { testAccPreCheck(t) }, 68 Providers: testAccProviders, 69 CheckDestroy: testAccCheckKinesisStreamDestroy, 70 Steps: []resource.TestStep{ 71 { 72 Config: testAccKinesisStreamConfig(rInt), 73 Check: resource.ComposeTestCheckFunc( 74 testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), 75 testAccCheckAWSKinesisStreamAttributes(&stream), 76 resource.TestCheckResourceAttr( 77 "aws_kinesis_stream.test_stream", "shard_count", "2"), 78 ), 79 }, 80 81 { 82 Config: testAccKinesisStreamConfigUpdateShardCount(rInt), 83 Check: resource.ComposeTestCheckFunc( 84 testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), 85 testAccCheckAWSKinesisStreamAttributes(&stream), 86 resource.TestCheckResourceAttr( 87 "aws_kinesis_stream.test_stream", "shard_count", "4"), 88 ), 89 }, 90 }, 91 }) 92 } 93 94 func TestAccAWSKinesisStream_retentionPeriod(t *testing.T) { 95 var stream kinesis.StreamDescription 96 97 rInt := acctest.RandInt() 98 99 resource.Test(t, resource.TestCase{ 100 PreCheck: func() { testAccPreCheck(t) }, 101 Providers: testAccProviders, 102 CheckDestroy: testAccCheckKinesisStreamDestroy, 103 Steps: []resource.TestStep{ 104 { 105 Config: testAccKinesisStreamConfig(rInt), 106 Check: resource.ComposeTestCheckFunc( 107 testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), 108 testAccCheckAWSKinesisStreamAttributes(&stream), 109 resource.TestCheckResourceAttr( 110 "aws_kinesis_stream.test_stream", "retention_period", "24"), 111 ), 112 }, 113 114 { 115 Config: testAccKinesisStreamConfigUpdateRetentionPeriod(rInt), 116 Check: resource.ComposeTestCheckFunc( 117 testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), 118 testAccCheckAWSKinesisStreamAttributes(&stream), 119 resource.TestCheckResourceAttr( 120 "aws_kinesis_stream.test_stream", "retention_period", "100"), 121 ), 122 }, 123 124 { 125 Config: testAccKinesisStreamConfigDecreaseRetentionPeriod(rInt), 126 Check: resource.ComposeTestCheckFunc( 127 testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), 128 testAccCheckAWSKinesisStreamAttributes(&stream), 129 resource.TestCheckResourceAttr( 130 "aws_kinesis_stream.test_stream", "retention_period", "28"), 131 ), 132 }, 133 }, 134 }) 135 } 136 137 func TestAccAWSKinesisStream_shardLevelMetrics(t *testing.T) { 138 var stream kinesis.StreamDescription 139 140 rInt := acctest.RandInt() 141 142 resource.Test(t, resource.TestCase{ 143 PreCheck: func() { testAccPreCheck(t) }, 144 Providers: testAccProviders, 145 CheckDestroy: testAccCheckKinesisStreamDestroy, 146 Steps: []resource.TestStep{ 147 { 148 Config: testAccKinesisStreamConfig(rInt), 149 Check: resource.ComposeTestCheckFunc( 150 testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), 151 testAccCheckAWSKinesisStreamAttributes(&stream), 152 resource.TestCheckNoResourceAttr( 153 "aws_kinesis_stream.test_stream", "shard_level_metrics"), 154 ), 155 }, 156 157 { 158 Config: testAccKinesisStreamConfigAllShardLevelMetrics(rInt), 159 Check: resource.ComposeTestCheckFunc( 160 testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), 161 testAccCheckAWSKinesisStreamAttributes(&stream), 162 resource.TestCheckResourceAttr( 163 "aws_kinesis_stream.test_stream", "shard_level_metrics.#", "7"), 164 ), 165 }, 166 167 { 168 Config: testAccKinesisStreamConfigSingleShardLevelMetric(rInt), 169 Check: resource.ComposeTestCheckFunc( 170 testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), 171 testAccCheckAWSKinesisStreamAttributes(&stream), 172 resource.TestCheckResourceAttr( 173 "aws_kinesis_stream.test_stream", "shard_level_metrics.#", "1"), 174 ), 175 }, 176 }, 177 }) 178 } 179 180 func testAccCheckKinesisStreamExists(n string, stream *kinesis.StreamDescription) resource.TestCheckFunc { 181 return func(s *terraform.State) error { 182 rs, ok := s.RootModule().Resources[n] 183 if !ok { 184 return fmt.Errorf("Not found: %s", n) 185 } 186 187 if rs.Primary.ID == "" { 188 return fmt.Errorf("No Kinesis ID is set") 189 } 190 191 conn := testAccProvider.Meta().(*AWSClient).kinesisconn 192 describeOpts := &kinesis.DescribeStreamInput{ 193 StreamName: aws.String(rs.Primary.Attributes["name"]), 194 } 195 resp, err := conn.DescribeStream(describeOpts) 196 if err != nil { 197 return err 198 } 199 200 *stream = *resp.StreamDescription 201 202 return nil 203 } 204 } 205 206 func testAccCheckAWSKinesisStreamAttributes(stream *kinesis.StreamDescription) resource.TestCheckFunc { 207 return func(s *terraform.State) error { 208 if !strings.HasPrefix(*stream.StreamName, "terraform-kinesis-test") { 209 return fmt.Errorf("Bad Stream name: %s", *stream.StreamName) 210 } 211 for _, rs := range s.RootModule().Resources { 212 if rs.Type != "aws_kinesis_stream" { 213 continue 214 } 215 if *stream.StreamARN != rs.Primary.Attributes["arn"] { 216 return fmt.Errorf("Bad Stream ARN\n\t expected: %s\n\tgot: %s\n", rs.Primary.Attributes["arn"], *stream.StreamARN) 217 } 218 shard_count := strconv.Itoa(len(stream.Shards)) 219 if shard_count != rs.Primary.Attributes["shard_count"] { 220 return fmt.Errorf("Bad Stream Shard Count\n\t expected: %s\n\tgot: %s\n", rs.Primary.Attributes["shard_count"], shard_count) 221 } 222 } 223 return nil 224 } 225 } 226 227 func testAccCheckKinesisStreamDestroy(s *terraform.State) error { 228 for _, rs := range s.RootModule().Resources { 229 if rs.Type != "aws_kinesis_stream" { 230 continue 231 } 232 conn := testAccProvider.Meta().(*AWSClient).kinesisconn 233 describeOpts := &kinesis.DescribeStreamInput{ 234 StreamName: aws.String(rs.Primary.Attributes["name"]), 235 } 236 resp, err := conn.DescribeStream(describeOpts) 237 if err == nil { 238 if resp.StreamDescription != nil && *resp.StreamDescription.StreamStatus != "DELETING" { 239 return fmt.Errorf("Error: Stream still exists") 240 } 241 } 242 243 return nil 244 245 } 246 247 return nil 248 } 249 250 func testAccKinesisStreamConfig(rInt int) string { 251 return fmt.Sprintf(` 252 resource "aws_kinesis_stream" "test_stream" { 253 name = "terraform-kinesis-test-%d" 254 shard_count = 2 255 tags { 256 Name = "tf-test" 257 } 258 }`, rInt) 259 } 260 261 func testAccKinesisStreamConfigUpdateShardCount(rInt int) string { 262 return fmt.Sprintf(` 263 resource "aws_kinesis_stream" "test_stream" { 264 name = "terraform-kinesis-test-%d" 265 shard_count = 4 266 tags { 267 Name = "tf-test" 268 } 269 }`, rInt) 270 } 271 272 func testAccKinesisStreamConfigUpdateRetentionPeriod(rInt int) string { 273 return fmt.Sprintf(` 274 resource "aws_kinesis_stream" "test_stream" { 275 name = "terraform-kinesis-test-%d" 276 shard_count = 2 277 retention_period = 100 278 tags { 279 Name = "tf-test" 280 } 281 }`, rInt) 282 } 283 284 func testAccKinesisStreamConfigDecreaseRetentionPeriod(rInt int) string { 285 return fmt.Sprintf(` 286 resource "aws_kinesis_stream" "test_stream" { 287 name = "terraform-kinesis-test-%d" 288 shard_count = 2 289 retention_period = 28 290 tags { 291 Name = "tf-test" 292 } 293 }`, rInt) 294 } 295 296 func testAccKinesisStreamConfigAllShardLevelMetrics(rInt int) string { 297 return fmt.Sprintf(` 298 resource "aws_kinesis_stream" "test_stream" { 299 name = "terraform-kinesis-test-%d" 300 shard_count = 2 301 tags { 302 Name = "tf-test" 303 } 304 shard_level_metrics = [ 305 "IncomingBytes", 306 "IncomingRecords", 307 "OutgoingBytes", 308 "OutgoingRecords", 309 "WriteProvisionedThroughputExceeded", 310 "ReadProvisionedThroughputExceeded", 311 "IteratorAgeMilliseconds" 312 ] 313 }`, rInt) 314 } 315 316 func testAccKinesisStreamConfigSingleShardLevelMetric(rInt int) string { 317 return fmt.Sprintf(` 318 resource "aws_kinesis_stream" "test_stream" { 319 name = "terraform-kinesis-test-%d" 320 shard_count = 2 321 tags { 322 Name = "tf-test" 323 } 324 shard_level_metrics = [ 325 "IncomingBytes" 326 ] 327 }`, rInt) 328 }