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