github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_db_instance_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/mitchellh/goamz/rds" 10 ) 11 12 func TestAccAWSDBInstance(t *testing.T) { 13 var v rds.DBInstance 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckAWSDBInstanceDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccAWSDBInstanceConfig, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), 24 testAccCheckAWSDBInstanceAttributes(&v), 25 resource.TestCheckResourceAttr( 26 "aws_db_instance.bar", "identifier", "foobarbaz-test-terraform"), 27 resource.TestCheckResourceAttr( 28 "aws_db_instance.bar", "allocated_storage", "10"), 29 resource.TestCheckResourceAttr( 30 "aws_db_instance.bar", "engine", "mysql"), 31 resource.TestCheckResourceAttr( 32 "aws_db_instance.bar", "engine_version", "5.6.21"), 33 resource.TestCheckResourceAttr( 34 "aws_db_instance.bar", "instance_class", "db.t1.micro"), 35 resource.TestCheckResourceAttr( 36 "aws_db_instance.bar", "name", "baz"), 37 resource.TestCheckResourceAttr( 38 // Shouldn't save password to state 39 "aws_db_instance.bar", "password", ""), 40 resource.TestCheckResourceAttr( 41 "aws_db_instance.bar", "username", "foo"), 42 resource.TestCheckResourceAttr( 43 "aws_db_instance.bar", "skip_final_snapshot", "true"), 44 resource.TestCheckResourceAttr( 45 "aws_db_instance.bar", "security_group_names.3322503515", "secfoobarbaz-test-terraform"), 46 resource.TestCheckResourceAttr( 47 "aws_db_instance.bar", "parameter_group_name", "default.mysql5.6"), 48 ), 49 }, 50 }, 51 }) 52 } 53 54 func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error { 55 conn := testAccProvider.Meta().(*AWSClient).rdsconn 56 57 for _, rs := range s.RootModule().Resources { 58 if rs.Type != "aws_db_instance" { 59 continue 60 } 61 62 // Try to find the Group 63 resp, err := conn.DescribeDBInstances( 64 &rds.DescribeDBInstances{ 65 DBInstanceIdentifier: rs.Primary.ID, 66 }) 67 68 if err == nil { 69 if len(resp.DBInstances) != 0 && 70 resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID { 71 return fmt.Errorf("DB Instance still exists") 72 } 73 } 74 75 // Verify the error 76 newerr, ok := err.(*rds.Error) 77 if !ok { 78 return err 79 } 80 if newerr.Code != "InvalidDBInstance.NotFound" { 81 return err 82 } 83 } 84 85 return nil 86 } 87 88 func testAccCheckAWSDBInstanceAttributes(v *rds.DBInstance) resource.TestCheckFunc { 89 return func(s *terraform.State) error { 90 91 if len(v.DBSecurityGroupNames) == 0 { 92 return fmt.Errorf("no sec names: %#v", v.DBSecurityGroupNames) 93 } 94 95 if v.Engine != "mysql" { 96 return fmt.Errorf("bad engine: %#v", v.Engine) 97 } 98 99 if v.EngineVersion != "5.6.21" { 100 return fmt.Errorf("bad engine_version: %#v", v.EngineVersion) 101 } 102 103 return nil 104 } 105 } 106 107 func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc { 108 return func(s *terraform.State) error { 109 rs, ok := s.RootModule().Resources[n] 110 if !ok { 111 return fmt.Errorf("Not found: %s", n) 112 } 113 114 if rs.Primary.ID == "" { 115 return fmt.Errorf("No DB Instance ID is set") 116 } 117 118 conn := testAccProvider.Meta().(*AWSClient).rdsconn 119 120 opts := rds.DescribeDBInstances{ 121 DBInstanceIdentifier: rs.Primary.ID, 122 } 123 124 resp, err := conn.DescribeDBInstances(&opts) 125 126 if err != nil { 127 return err 128 } 129 130 if len(resp.DBInstances) != 1 || 131 resp.DBInstances[0].DBInstanceIdentifier != rs.Primary.ID { 132 return fmt.Errorf("DB Instance not found") 133 } 134 135 *v = resp.DBInstances[0] 136 137 return nil 138 } 139 } 140 141 const testAccAWSDBInstanceConfig = ` 142 resource "aws_db_security_group" "bar" { 143 name = "secfoobarbaz-test-terraform" 144 description = "just cuz" 145 146 ingress { 147 cidr = "10.0.0.1/24" 148 } 149 } 150 151 resource "aws_db_instance" "bar" { 152 identifier = "foobarbaz-test-terraform" 153 154 allocated_storage = 10 155 engine = "mysql" 156 engine_version = "5.6.21" 157 instance_class = "db.t1.micro" 158 name = "baz" 159 password = "barbarbarbar" 160 username = "foo" 161 162 skip_final_snapshot = true 163 164 security_group_names = ["${aws_db_security_group.bar.name}"] 165 parameter_group_name = "default.mysql5.6" 166 } 167 `