github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/postgresql/resource_postgresql_database_test.go (about) 1 package postgresql 2 3 import ( 4 "database/sql" 5 "fmt" 6 "testing" 7 8 "errors" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccPostgresqlDatabase_Basic(t *testing.T) { 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckPostgresqlDatabaseDestroy, 19 Steps: []resource.TestStep{ 20 { 21 Config: testAccPostgresqlDatabaseConfig, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb"), 24 resource.TestCheckResourceAttr( 25 "postgresql_database.mydb", "name", "mydb"), 26 resource.TestCheckResourceAttr( 27 "postgresql_database.mydb", "owner", "myrole"), 28 ), 29 }, 30 }, 31 }) 32 } 33 34 func TestAccPostgresqlDatabase_DefaultOwner(t *testing.T) { 35 36 resource.Test(t, resource.TestCase{ 37 PreCheck: func() { testAccPreCheck(t) }, 38 Providers: testAccProviders, 39 CheckDestroy: testAccCheckPostgresqlDatabaseDestroy, 40 Steps: []resource.TestStep{ 41 { 42 Config: testAccPostgresqlDatabaseConfig, 43 Check: resource.ComposeTestCheckFunc( 44 testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb_default_owner"), 45 resource.TestCheckResourceAttr( 46 "postgresql_database.mydb_default_owner", "name", "mydb_default_owner"), 47 resource.TestCheckResourceAttrSet( 48 "postgresql_database.mydb_default_owner", "owner"), 49 ), 50 }, 51 }, 52 }) 53 } 54 55 func testAccCheckPostgresqlDatabaseDestroy(s *terraform.State) error { 56 client := testAccProvider.Meta().(*Client) 57 58 for _, rs := range s.RootModule().Resources { 59 if rs.Type != "postgresql_database" { 60 continue 61 } 62 63 exists, err := checkDatabaseExists(client, rs.Primary.ID) 64 65 if err != nil { 66 return fmt.Errorf("Error checking db %s", err) 67 } 68 69 if exists { 70 return errors.New("Db still exists after destroy") 71 } 72 } 73 74 return nil 75 } 76 77 func testAccCheckPostgresqlDatabaseExists(n string) resource.TestCheckFunc { 78 return func(s *terraform.State) error { 79 rs, ok := s.RootModule().Resources[n] 80 if !ok { 81 return fmt.Errorf("Resource not found: %s", n) 82 } 83 84 if rs.Primary.ID == "" { 85 return errors.New("No ID is set") 86 } 87 88 client := testAccProvider.Meta().(*Client) 89 exists, err := checkDatabaseExists(client, rs.Primary.ID) 90 91 if err != nil { 92 return fmt.Errorf("Error checking db %s", err) 93 } 94 95 if !exists { 96 return errors.New("Db not found") 97 } 98 99 return nil 100 } 101 } 102 103 func checkDatabaseExists(client *Client, dbName string) (bool, error) { 104 conn, err := client.Connect() 105 if err != nil { 106 return false, err 107 } 108 defer conn.Close() 109 110 var _rez int 111 err = conn.QueryRow("SELECT 1 from pg_database d WHERE datname=$1", dbName).Scan(&_rez) 112 switch { 113 case err == sql.ErrNoRows: 114 return false, nil 115 case err != nil: 116 return false, fmt.Errorf("Error reading info about database: %s", err) 117 default: 118 return true, nil 119 } 120 } 121 122 var testAccPostgresqlDatabaseConfig = ` 123 resource "postgresql_role" "myrole" { 124 name = "myrole" 125 login = true 126 } 127 128 resource "postgresql_database" "mydb" { 129 name = "mydb" 130 owner = "${postgresql_role.myrole.name}" 131 } 132 133 resource "postgresql_database" "mydb2" { 134 name = "mydb2" 135 owner = "${postgresql_role.myrole.name}" 136 } 137 138 resource "postgresql_database" "mydb_default_owner" { 139 name = "mydb_default_owner" 140 } 141 142 `