github.com/paulmey/terraform@v0.5.2-0.20150519145237-046e9b4c884d/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", "license_model", "general-public-license"), 37 resource.TestCheckResourceAttr( 38 "aws_db_instance.bar", "instance_class", "db.t1.micro"), 39 resource.TestCheckResourceAttr( 40 "aws_db_instance.bar", "name", "baz"), 41 resource.TestCheckResourceAttr( 42 "aws_db_instance.bar", "username", "foo"), 43 resource.TestCheckResourceAttr( 44 "aws_db_instance.bar", "parameter_group_name", "default.mysql5.6"), 45 ), 46 }, 47 }, 48 }) 49 } 50 51 func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error { 52 conn := testAccProvider.Meta().(*AWSClient).rdsconn 53 54 for _, rs := range s.RootModule().Resources { 55 if rs.Type != "aws_db_instance" { 56 continue 57 } 58 59 // Try to find the Group 60 resp, err := conn.DescribeDBInstances( 61 &rds.DescribeDBInstancesInput{ 62 DBInstanceIdentifier: aws.String(rs.Primary.ID), 63 }) 64 65 if err == nil { 66 if len(resp.DBInstances) != 0 && 67 *resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID { 68 return fmt.Errorf("DB Instance still exists") 69 } 70 } 71 72 // Verify the error 73 newerr, ok := err.(*aws.APIError) 74 if !ok { 75 return err 76 } 77 if newerr.Code != "InvalidDBInstance.NotFound" { 78 return err 79 } 80 } 81 82 return nil 83 } 84 85 func testAccCheckAWSDBInstanceAttributes(v *rds.DBInstance) resource.TestCheckFunc { 86 return func(s *terraform.State) error { 87 88 if *v.Engine != "mysql" { 89 return fmt.Errorf("bad engine: %#v", *v.Engine) 90 } 91 92 if *v.EngineVersion != "5.6.21" { 93 return fmt.Errorf("bad engine_version: %#v", *v.EngineVersion) 94 } 95 96 if *v.BackupRetentionPeriod != 0 { 97 return fmt.Errorf("bad backup_retention_period: %#v", *v.BackupRetentionPeriod) 98 } 99 100 return nil 101 } 102 } 103 104 func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc { 105 return func(s *terraform.State) error { 106 rs, ok := s.RootModule().Resources[n] 107 if !ok { 108 return fmt.Errorf("Not found: %s", n) 109 } 110 111 if rs.Primary.ID == "" { 112 return fmt.Errorf("No DB Instance ID is set") 113 } 114 115 conn := testAccProvider.Meta().(*AWSClient).rdsconn 116 117 opts := rds.DescribeDBInstancesInput{ 118 DBInstanceIdentifier: aws.String(rs.Primary.ID), 119 } 120 121 resp, err := conn.DescribeDBInstances(&opts) 122 123 if err != nil { 124 return err 125 } 126 127 if len(resp.DBInstances) != 1 || 128 *resp.DBInstances[0].DBInstanceIdentifier != rs.Primary.ID { 129 return fmt.Errorf("DB Instance not found") 130 } 131 132 *v = *resp.DBInstances[0] 133 134 return nil 135 } 136 } 137 138 // Database names cannot collide, and deletion takes so long, that making the 139 // name a bit random helps so able we can kill a test that's just waiting for a 140 // delete and not be blocked on kicking off another one. 141 var testAccAWSDBInstanceConfig = fmt.Sprintf(` 142 resource "aws_db_instance" "bar" { 143 identifier = "foobarbaz-test-terraform-%d" 144 145 allocated_storage = 10 146 engine = "mysql" 147 engine_version = "5.6.21" 148 instance_class = "db.t1.micro" 149 name = "baz" 150 password = "barbarbarbar" 151 username = "foo" 152 153 backup_retention_period = 0 154 155 parameter_group_name = "default.mysql5.6" 156 }`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())