github.com/shvar/terraform@v0.6.9-0.20151215234924-3365cd2231df/builtin/providers/aws/resource_aws_elasticache_cluster_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/elasticache" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSElasticacheCluster_basic(t *testing.T) { 17 var ec elasticache.CacheCluster 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccAWSElasticacheClusterConfig, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"), 27 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec), 28 resource.TestCheckResourceAttr( 29 "aws_elasticache_cluster.bar", "cache_nodes.0.id", "0001"), 30 ), 31 }, 32 }, 33 }) 34 } 35 36 func TestAccAWSElasticacheCluster_snapshotsWithUpdates(t *testing.T) { 37 var ec elasticache.CacheCluster 38 39 ri := genRandInt() 40 preConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfig_snapshots, ri, ri, ri) 41 postConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfig_snapshotsUpdated, ri, ri, ri) 42 43 resource.Test(t, resource.TestCase{ 44 PreCheck: func() { testAccPreCheck(t) }, 45 Providers: testAccProviders, 46 CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, 47 Steps: []resource.TestStep{ 48 resource.TestStep{ 49 Config: preConfig, 50 Check: resource.ComposeTestCheckFunc( 51 testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"), 52 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec), 53 resource.TestCheckResourceAttr( 54 "aws_elasticache_cluster.bar", "snapshot_window", "05:00-09:00"), 55 resource.TestCheckResourceAttr( 56 "aws_elasticache_cluster.bar", "snapshot_retention_limit", "3"), 57 ), 58 }, 59 60 resource.TestStep{ 61 Config: postConfig, 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"), 64 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec), 65 resource.TestCheckResourceAttr( 66 "aws_elasticache_cluster.bar", "snapshot_window", "07:00-09:00"), 67 resource.TestCheckResourceAttr( 68 "aws_elasticache_cluster.bar", "snapshot_retention_limit", "7"), 69 ), 70 }, 71 }, 72 }) 73 } 74 75 func TestAccAWSElasticacheCluster_decreasingCacheNodes(t *testing.T) { 76 var ec elasticache.CacheCluster 77 78 ri := genRandInt() 79 preConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfigDecreasingNodes, ri, ri, ri) 80 postConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfigDecreasingNodes_update, ri, ri, ri) 81 82 resource.Test(t, resource.TestCase{ 83 PreCheck: func() { testAccPreCheck(t) }, 84 Providers: testAccProviders, 85 CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, 86 Steps: []resource.TestStep{ 87 resource.TestStep{ 88 Config: preConfig, 89 Check: resource.ComposeTestCheckFunc( 90 testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"), 91 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec), 92 resource.TestCheckResourceAttr( 93 "aws_elasticache_cluster.bar", "num_cache_nodes", "3"), 94 ), 95 }, 96 97 resource.TestStep{ 98 Config: postConfig, 99 Check: resource.ComposeTestCheckFunc( 100 testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"), 101 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec), 102 resource.TestCheckResourceAttr( 103 "aws_elasticache_cluster.bar", "num_cache_nodes", "1"), 104 ), 105 }, 106 }, 107 }) 108 } 109 110 func TestAccAWSElasticacheCluster_vpc(t *testing.T) { 111 var csg elasticache.CacheSubnetGroup 112 var ec elasticache.CacheCluster 113 resource.Test(t, resource.TestCase{ 114 PreCheck: func() { testAccPreCheck(t) }, 115 Providers: testAccProviders, 116 CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, 117 Steps: []resource.TestStep{ 118 resource.TestStep{ 119 Config: testAccAWSElasticacheClusterInVPCConfig, 120 Check: resource.ComposeTestCheckFunc( 121 testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg), 122 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec), 123 testAccCheckAWSElasticacheClusterAttributes(&ec), 124 ), 125 }, 126 }, 127 }) 128 } 129 130 func testAccCheckAWSElasticacheClusterAttributes(v *elasticache.CacheCluster) resource.TestCheckFunc { 131 return func(s *terraform.State) error { 132 if v.NotificationConfiguration == nil { 133 return fmt.Errorf("Expected NotificationConfiguration for ElastiCache Cluster (%s)", *v.CacheClusterId) 134 } 135 136 if strings.ToLower(*v.NotificationConfiguration.TopicStatus) != "active" { 137 return fmt.Errorf("Expected NotificationConfiguration status to be 'active', got (%s)", *v.NotificationConfiguration.TopicStatus) 138 } 139 140 return nil 141 } 142 } 143 144 func testAccCheckAWSElasticacheClusterDestroy(s *terraform.State) error { 145 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 146 147 for _, rs := range s.RootModule().Resources { 148 if rs.Type != "aws_elasticache_cluster" { 149 continue 150 } 151 res, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{ 152 CacheClusterId: aws.String(rs.Primary.ID), 153 }) 154 if err != nil { 155 return err 156 } 157 if len(res.CacheClusters) > 0 { 158 return fmt.Errorf("still exist.") 159 } 160 } 161 return nil 162 } 163 164 func testAccCheckAWSElasticacheClusterExists(n string, v *elasticache.CacheCluster) resource.TestCheckFunc { 165 return func(s *terraform.State) error { 166 rs, ok := s.RootModule().Resources[n] 167 if !ok { 168 return fmt.Errorf("Not found: %s", n) 169 } 170 171 if rs.Primary.ID == "" { 172 return fmt.Errorf("No cache cluster ID is set") 173 } 174 175 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 176 resp, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{ 177 CacheClusterId: aws.String(rs.Primary.ID), 178 }) 179 if err != nil { 180 return fmt.Errorf("Elasticache error: %v", err) 181 } 182 183 for _, c := range resp.CacheClusters { 184 if *c.CacheClusterId == rs.Primary.ID { 185 *v = *c 186 } 187 } 188 189 return nil 190 } 191 } 192 193 func genRandInt() int { 194 return rand.New(rand.NewSource(time.Now().UnixNano())).Int() % 1000 195 } 196 197 var testAccAWSElasticacheClusterConfig = fmt.Sprintf(` 198 provider "aws" { 199 region = "us-east-1" 200 } 201 resource "aws_security_group" "bar" { 202 name = "tf-test-security-group-%03d" 203 description = "tf-test-security-group-descr" 204 ingress { 205 from_port = -1 206 to_port = -1 207 protocol = "icmp" 208 cidr_blocks = ["0.0.0.0/0"] 209 } 210 } 211 212 resource "aws_elasticache_security_group" "bar" { 213 name = "tf-test-security-group-%03d" 214 description = "tf-test-security-group-descr" 215 security_group_names = ["${aws_security_group.bar.name}"] 216 } 217 218 resource "aws_elasticache_cluster" "bar" { 219 cluster_id = "tf-test-%03d" 220 engine = "memcached" 221 node_type = "cache.m1.small" 222 num_cache_nodes = 1 223 port = 11211 224 parameter_group_name = "default.memcached1.4" 225 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 226 } 227 `, genRandInt(), genRandInt(), genRandInt()) 228 229 var testAccAWSElasticacheClusterConfig_snapshots = ` 230 provider "aws" { 231 region = "us-east-1" 232 } 233 resource "aws_security_group" "bar" { 234 name = "tf-test-security-group-%03d" 235 description = "tf-test-security-group-descr" 236 ingress { 237 from_port = -1 238 to_port = -1 239 protocol = "icmp" 240 cidr_blocks = ["0.0.0.0/0"] 241 } 242 } 243 244 resource "aws_elasticache_security_group" "bar" { 245 name = "tf-test-security-group-%03d" 246 description = "tf-test-security-group-descr" 247 security_group_names = ["${aws_security_group.bar.name}"] 248 } 249 250 resource "aws_elasticache_cluster" "bar" { 251 cluster_id = "tf-test-%03d" 252 engine = "redis" 253 node_type = "cache.m1.small" 254 num_cache_nodes = 1 255 port = 6379 256 parameter_group_name = "default.redis2.8" 257 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 258 snapshot_window = "05:00-09:00" 259 snapshot_retention_limit = 3 260 } 261 ` 262 263 var testAccAWSElasticacheClusterConfig_snapshotsUpdated = ` 264 provider "aws" { 265 region = "us-east-1" 266 } 267 resource "aws_security_group" "bar" { 268 name = "tf-test-security-group-%03d" 269 description = "tf-test-security-group-descr" 270 ingress { 271 from_port = -1 272 to_port = -1 273 protocol = "icmp" 274 cidr_blocks = ["0.0.0.0/0"] 275 } 276 } 277 278 resource "aws_elasticache_security_group" "bar" { 279 name = "tf-test-security-group-%03d" 280 description = "tf-test-security-group-descr" 281 security_group_names = ["${aws_security_group.bar.name}"] 282 } 283 284 resource "aws_elasticache_cluster" "bar" { 285 cluster_id = "tf-test-%03d" 286 engine = "redis" 287 node_type = "cache.m1.small" 288 num_cache_nodes = 1 289 port = 6379 290 parameter_group_name = "default.redis2.8" 291 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 292 snapshot_window = "07:00-09:00" 293 snapshot_retention_limit = 7 294 apply_immediately = true 295 } 296 ` 297 298 var testAccAWSElasticacheClusterConfigDecreasingNodes = ` 299 provider "aws" { 300 region = "us-east-1" 301 } 302 resource "aws_security_group" "bar" { 303 name = "tf-test-security-group-%03d" 304 description = "tf-test-security-group-descr" 305 ingress { 306 from_port = -1 307 to_port = -1 308 protocol = "icmp" 309 cidr_blocks = ["0.0.0.0/0"] 310 } 311 } 312 313 resource "aws_elasticache_security_group" "bar" { 314 name = "tf-test-security-group-%03d" 315 description = "tf-test-security-group-descr" 316 security_group_names = ["${aws_security_group.bar.name}"] 317 } 318 319 resource "aws_elasticache_cluster" "bar" { 320 cluster_id = "tf-test-%03d" 321 engine = "memcached" 322 node_type = "cache.m1.small" 323 num_cache_nodes = 3 324 port = 11211 325 parameter_group_name = "default.memcached1.4" 326 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 327 } 328 ` 329 330 var testAccAWSElasticacheClusterConfigDecreasingNodes_update = ` 331 provider "aws" { 332 region = "us-east-1" 333 } 334 resource "aws_security_group" "bar" { 335 name = "tf-test-security-group-%03d" 336 description = "tf-test-security-group-descr" 337 ingress { 338 from_port = -1 339 to_port = -1 340 protocol = "icmp" 341 cidr_blocks = ["0.0.0.0/0"] 342 } 343 } 344 345 resource "aws_elasticache_security_group" "bar" { 346 name = "tf-test-security-group-%03d" 347 description = "tf-test-security-group-descr" 348 security_group_names = ["${aws_security_group.bar.name}"] 349 } 350 351 resource "aws_elasticache_cluster" "bar" { 352 cluster_id = "tf-test-%03d" 353 engine = "memcached" 354 node_type = "cache.m1.small" 355 num_cache_nodes = 1 356 port = 11211 357 parameter_group_name = "default.memcached1.4" 358 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 359 apply_immediately = true 360 } 361 ` 362 363 var testAccAWSElasticacheClusterInVPCConfig = fmt.Sprintf(` 364 resource "aws_vpc" "foo" { 365 cidr_block = "192.168.0.0/16" 366 tags { 367 Name = "tf-test" 368 } 369 } 370 371 resource "aws_subnet" "foo" { 372 vpc_id = "${aws_vpc.foo.id}" 373 cidr_block = "192.168.0.0/20" 374 availability_zone = "us-west-2a" 375 tags { 376 Name = "tf-test" 377 } 378 } 379 380 resource "aws_elasticache_subnet_group" "bar" { 381 name = "tf-test-cache-subnet-%03d" 382 description = "tf-test-cache-subnet-group-descr" 383 subnet_ids = ["${aws_subnet.foo.id}"] 384 } 385 386 resource "aws_security_group" "bar" { 387 name = "tf-test-security-group-%03d" 388 description = "tf-test-security-group-descr" 389 vpc_id = "${aws_vpc.foo.id}" 390 ingress { 391 from_port = -1 392 to_port = -1 393 protocol = "icmp" 394 cidr_blocks = ["0.0.0.0/0"] 395 } 396 } 397 398 resource "aws_elasticache_cluster" "bar" { 399 // Including uppercase letters in this name to ensure 400 // that we correctly handle the fact that the API 401 // normalizes names to lowercase. 402 cluster_id = "tf-TEST-%03d" 403 node_type = "cache.m1.small" 404 num_cache_nodes = 1 405 engine = "redis" 406 engine_version = "2.8.19" 407 port = 6379 408 subnet_group_name = "${aws_elasticache_subnet_group.bar.name}" 409 security_group_ids = ["${aws_security_group.bar.id}"] 410 parameter_group_name = "default.redis2.8" 411 notification_topic_arn = "${aws_sns_topic.topic_example.arn}" 412 } 413 414 resource "aws_sns_topic" "topic_example" { 415 name = "tf-ecache-cluster-test" 416 } 417 `, genRandInt(), genRandInt(), genRandInt())