github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_sql_database_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/acctest" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 11 "google.golang.org/api/sqladmin/v1beta4" 12 ) 13 14 func TestAccGoogleSqlDatabase_basic(t *testing.T) { 15 var database sqladmin.Database 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testGoogleSqlDatabase_basic, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckGoogleSqlDatabaseExists( 26 "google_sql_database.database", &database), 27 testAccCheckGoogleSqlDatabaseEquals( 28 "google_sql_database.database", &database), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func testAccCheckGoogleSqlDatabaseEquals(n string, 36 database *sqladmin.Database) resource.TestCheckFunc { 37 return func(s *terraform.State) error { 38 rs, ok := s.RootModule().Resources[n] 39 if !ok { 40 return fmt.Errorf("Resource not found: %s", n) 41 } 42 43 database_name := rs.Primary.Attributes["name"] 44 instance_name := rs.Primary.Attributes["instance"] 45 46 if database_name != database.Name { 47 return fmt.Errorf("Error name mismatch, (%s, %s)", database_name, database.Name) 48 } 49 50 if instance_name != database.Instance { 51 return fmt.Errorf("Error instance_name mismatch, (%s, %s)", instance_name, database.Instance) 52 } 53 54 return nil 55 } 56 } 57 58 func testAccCheckGoogleSqlDatabaseExists(n string, 59 database *sqladmin.Database) resource.TestCheckFunc { 60 return func(s *terraform.State) error { 61 config := testAccProvider.Meta().(*Config) 62 rs, ok := s.RootModule().Resources[n] 63 if !ok { 64 return fmt.Errorf("Resource not found: %s", n) 65 } 66 67 database_name := rs.Primary.Attributes["name"] 68 instance_name := rs.Primary.Attributes["instance"] 69 found, err := config.clientSqlAdmin.Databases.Get(config.Project, 70 instance_name, database_name).Do() 71 72 if err != nil { 73 return fmt.Errorf("Not found: %s: %s", n, err) 74 } 75 76 *database = *found 77 78 return nil 79 } 80 } 81 82 func testAccGoogleSqlDatabaseDestroy(s *terraform.State) error { 83 for _, rs := range s.RootModule().Resources { 84 config := testAccProvider.Meta().(*Config) 85 if rs.Type != "google_sql_database" { 86 continue 87 } 88 89 database_name := rs.Primary.Attributes["name"] 90 instance_name := rs.Primary.Attributes["instance"] 91 _, err := config.clientSqlAdmin.Databases.Get(config.Project, 92 instance_name, database_name).Do() 93 94 if err == nil { 95 return fmt.Errorf("Database resource still exists") 96 } 97 } 98 99 return nil 100 } 101 102 var testGoogleSqlDatabase_basic = fmt.Sprintf(` 103 resource "google_sql_database_instance" "instance" { 104 name = "sqldatabasetest%s" 105 region = "us-central" 106 settings { 107 tier = "D0" 108 } 109 } 110 111 resource "google_sql_database" "database" { 112 name = "sqldatabasetest%s" 113 instance = "${google_sql_database_instance.instance.name}" 114 } 115 `, acctest.RandString(10), acctest.RandString(10))