github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/builtin/providers/aws/resource_aws_elasticache_cluster_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "math/rand" 6 "testing" 7 "time" 8 9 "github.com/awslabs/aws-sdk-go/aws" 10 "github.com/awslabs/aws-sdk-go/service/elasticache" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSElasticacheCluster(t *testing.T) { 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccAWSElasticacheClusterConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"), 25 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar"), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func TestAccAWSElasticacheCluster_vpc(t *testing.T) { 33 resource.Test(t, resource.TestCase{ 34 PreCheck: func() { testAccPreCheck(t) }, 35 Providers: testAccProviders, 36 CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, 37 Steps: []resource.TestStep{ 38 resource.TestStep{ 39 Config: testAccAWSElasticacheClusterInVPCConfig, 40 Check: resource.ComposeTestCheckFunc( 41 testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar"), 42 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar"), 43 ), 44 }, 45 }, 46 }) 47 } 48 49 func testAccCheckAWSElasticacheClusterDestroy(s *terraform.State) error { 50 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 51 52 for _, rs := range s.RootModule().Resources { 53 if rs.Type != "aws_elasticache_cluster" { 54 continue 55 } 56 res, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{ 57 CacheClusterID: aws.String(rs.Primary.ID), 58 }) 59 if err != nil { 60 return err 61 } 62 if len(res.CacheClusters) > 0 { 63 return fmt.Errorf("still exist.") 64 } 65 } 66 return nil 67 } 68 69 func testAccCheckAWSElasticacheClusterExists(n string) resource.TestCheckFunc { 70 return func(s *terraform.State) error { 71 rs, ok := s.RootModule().Resources[n] 72 if !ok { 73 return fmt.Errorf("Not found: %s", n) 74 } 75 76 if rs.Primary.ID == "" { 77 return fmt.Errorf("No cache cluster ID is set") 78 } 79 80 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 81 _, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{ 82 CacheClusterID: aws.String(rs.Primary.ID), 83 }) 84 if err != nil { 85 return fmt.Errorf("Elasticache error: %v", err) 86 } 87 return nil 88 } 89 } 90 91 func genRandInt() int { 92 return rand.New(rand.NewSource(time.Now().UnixNano())).Int() % 1000 93 } 94 95 var testAccAWSElasticacheClusterConfig = fmt.Sprintf(` 96 resource "aws_security_group" "bar" { 97 name = "tf-test-security-group-%03d" 98 description = "tf-test-security-group-descr" 99 ingress { 100 from_port = -1 101 to_port = -1 102 protocol = "icmp" 103 cidr_blocks = ["0.0.0.0/0"] 104 } 105 } 106 107 resource "aws_elasticache_security_group" "bar" { 108 name = "tf-test-security-group-%03d" 109 description = "tf-test-security-group-descr" 110 security_group_names = ["${aws_security_group.bar.name}"] 111 } 112 113 resource "aws_elasticache_cluster" "bar" { 114 cluster_id = "tf-test-%03d" 115 engine = "memcached" 116 node_type = "cache.m1.small" 117 num_cache_nodes = 1 118 parameter_group_name = "default.memcached1.4" 119 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 120 } 121 `, genRandInt(), genRandInt(), genRandInt()) 122 123 var testAccAWSElasticacheClusterInVPCConfig = fmt.Sprintf(` 124 resource "aws_vpc" "foo" { 125 cidr_block = "192.168.0.0/16" 126 tags { 127 Name = "tf-test" 128 } 129 } 130 131 resource "aws_subnet" "foo" { 132 vpc_id = "${aws_vpc.foo.id}" 133 cidr_block = "192.168.0.0/20" 134 availability_zone = "us-west-2a" 135 tags { 136 Name = "tf-test" 137 } 138 } 139 140 resource "aws_elasticache_subnet_group" "bar" { 141 name = "tf-test-cache-subnet-%03d" 142 description = "tf-test-cache-subnet-group-descr" 143 subnet_ids = ["${aws_subnet.foo.id}"] 144 } 145 146 resource "aws_security_group" "bar" { 147 name = "tf-test-security-group-%03d" 148 description = "tf-test-security-group-descr" 149 vpc_id = "${aws_vpc.foo.id}" 150 ingress { 151 from_port = -1 152 to_port = -1 153 protocol = "icmp" 154 cidr_blocks = ["0.0.0.0/0"] 155 } 156 } 157 158 resource "aws_elasticache_cluster" "bar" { 159 cluster_id = "tf-test-%03d" 160 node_type = "cache.m1.small" 161 num_cache_nodes = 1 162 engine = "redis" 163 engine_version = "2.8.19" 164 port = 6379 165 subnet_group_name = "${aws_elasticache_subnet_group.bar.name}" 166 security_group_ids = ["${aws_security_group.bar.id}"] 167 parameter_group_name = "default.redis2.8" 168 } 169 `, genRandInt(), genRandInt(), genRandInt())