github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/builtin/providers/aws/resource_aws_db_instance_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "math/rand" 6 "testing" 7 "time" 8 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 12 "github.com/awslabs/aws-sdk-go/aws" 13 "github.com/awslabs/aws-sdk-go/service/rds" 14 ) 15 16 func TestAccAWSDBInstance(t *testing.T) { 17 var v rds.DBInstance 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckAWSDBInstanceDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccAWSDBInstanceConfig, 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), 28 testAccCheckAWSDBInstanceAttributes(&v), 29 resource.TestCheckResourceAttr( 30 "aws_db_instance.bar", "allocated_storage", "10"), 31 resource.TestCheckResourceAttr( 32 "aws_db_instance.bar", "engine", "mysql"), 33 resource.TestCheckResourceAttr( 34 "aws_db_instance.bar", "engine_version", "5.6.21"), 35 resource.TestCheckResourceAttr( 36 "aws_db_instance.bar", "instance_class", "db.t1.micro"), 37 resource.TestCheckResourceAttr( 38 "aws_db_instance.bar", "name", "baz"), 39 resource.TestCheckResourceAttr( 40 "aws_db_instance.bar", "username", "foo"), 41 resource.TestCheckResourceAttr( 42 "aws_db_instance.bar", "parameter_group_name", "default.mysql5.6"), 43 ), 44 }, 45 }, 46 }) 47 } 48 49 func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error { 50 conn := testAccProvider.Meta().(*AWSClient).rdsconn 51 52 for _, rs := range s.RootModule().Resources { 53 if rs.Type != "aws_db_instance" { 54 continue 55 } 56 57 // Try to find the Group 58 resp, err := conn.DescribeDBInstances( 59 &rds.DescribeDBInstancesInput{ 60 DBInstanceIdentifier: aws.String(rs.Primary.ID), 61 }) 62 63 if err == nil { 64 if len(resp.DBInstances) != 0 && 65 *resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID { 66 return fmt.Errorf("DB Instance still exists") 67 } 68 } 69 70 // Verify the error 71 newerr, ok := err.(*aws.APIError) 72 if !ok { 73 return err 74 } 75 if newerr.Code != "InvalidDBInstance.NotFound" { 76 return err 77 } 78 } 79 80 return nil 81 } 82 83 func testAccCheckAWSDBInstanceAttributes(v *rds.DBInstance) resource.TestCheckFunc { 84 return func(s *terraform.State) error { 85 86 if *v.Engine != "mysql" { 87 return fmt.Errorf("bad engine: %#v", *v.Engine) 88 } 89 90 if *v.EngineVersion != "5.6.21" { 91 return fmt.Errorf("bad engine_version: %#v", *v.EngineVersion) 92 } 93 94 if *v.BackupRetentionPeriod != 0 { 95 return fmt.Errorf("bad backup_retention_period: %#v", *v.BackupRetentionPeriod) 96 } 97 98 return nil 99 } 100 } 101 102 func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc { 103 return func(s *terraform.State) error { 104 rs, ok := s.RootModule().Resources[n] 105 if !ok { 106 return fmt.Errorf("Not found: %s", n) 107 } 108 109 if rs.Primary.ID == "" { 110 return fmt.Errorf("No DB Instance ID is set") 111 } 112 113 conn := testAccProvider.Meta().(*AWSClient).rdsconn 114 115 opts := rds.DescribeDBInstancesInput{ 116 DBInstanceIdentifier: aws.String(rs.Primary.ID), 117 } 118 119 resp, err := conn.DescribeDBInstances(&opts) 120 121 if err != nil { 122 return err 123 } 124 125 if len(resp.DBInstances) != 1 || 126 *resp.DBInstances[0].DBInstanceIdentifier != rs.Primary.ID { 127 return fmt.Errorf("DB Instance not found") 128 } 129 130 *v = *resp.DBInstances[0] 131 132 return nil 133 } 134 } 135 136 // Database names cannot collide, and deletion takes so long, that making the 137 // name a bit random helps so able we can kill a test that's just waiting for a 138 // delete and not be blocked on kicking off another one. 139 var testAccAWSDBInstanceConfig = fmt.Sprintf(` 140 resource "aws_db_instance" "bar" { 141 identifier = "foobarbaz-test-terraform-%d" 142 143 allocated_storage = 10 144 engine = "mysql" 145 engine_version = "5.6.21" 146 instance_class = "db.t1.micro" 147 name = "baz" 148 password = "barbarbarbar" 149 username = "foo" 150 151 backup_retention_period = 0 152 153 parameter_group_name = "default.mysql5.6" 154 }`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())