2015-10-27 11:04:19 +01:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2016-09-05 23:46:40 +02:00
|
|
|
"errors"
|
2015-10-27 11:04:19 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccPostgresqlDatabase_Basic(t *testing.T) {
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckPostgresqlDatabaseDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-09-05 23:46:40 +02:00
|
|
|
{
|
2015-10-27 11:04:19 +01:00
|
|
|
Config: testAccPostgresqlDatabaseConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb", "myrole"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.mydb", "name", "mydb"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.mydb", "owner", "myrole"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccPostgresqlDatabase_DefaultOwner(t *testing.T) {
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckPostgresqlDatabaseDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-09-05 23:46:40 +02:00
|
|
|
{
|
2015-10-27 11:04:19 +01:00
|
|
|
Config: testAccPostgresqlDatabaseConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb_default_owner", ""),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"postgresql_database.mydb_default_owner", "name", "mydb_default_owner"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckPostgresqlDatabaseDestroy(s *terraform.State) error {
|
|
|
|
client := testAccProvider.Meta().(*Client)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "postgresql_database" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := checkDatabaseExists(client, rs.Primary.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error checking db %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
2016-09-05 23:46:40 +02:00
|
|
|
return errors.New("Db still exists after destroy")
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckPostgresqlDatabaseExists(n string, owner string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Resource not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
2016-09-05 23:46:40 +02:00
|
|
|
return errors.New("No ID is set")
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
actualOwner := rs.Primary.Attributes["owner"]
|
|
|
|
if actualOwner != owner {
|
|
|
|
return fmt.Errorf("Wrong owner for db expected %s got %s", owner, actualOwner)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := testAccProvider.Meta().(*Client)
|
|
|
|
exists, err := checkDatabaseExists(client, rs.Primary.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error checking db %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2016-09-05 23:46:40 +02:00
|
|
|
return errors.New("Db not found")
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkDatabaseExists(client *Client, dbName string) (bool, error) {
|
|
|
|
conn, err := client.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
var _rez int
|
|
|
|
err = conn.QueryRow("SELECT 1 from pg_database d WHERE datname=$1", dbName).Scan(&_rez)
|
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
|
|
|
return false, nil
|
|
|
|
case err != nil:
|
|
|
|
return false, fmt.Errorf("Error reading info about database: %s", err)
|
|
|
|
default:
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccPostgresqlDatabaseConfig = `
|
|
|
|
resource "postgresql_role" "myrole" {
|
|
|
|
name = "myrole"
|
|
|
|
login = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "postgresql_database" "mydb" {
|
|
|
|
name = "mydb"
|
|
|
|
owner = "${postgresql_role.myrole.name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "postgresql_database" "mydb2" {
|
|
|
|
name = "mydb2"
|
|
|
|
owner = "${postgresql_role.myrole.name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "postgresql_database" "mydb_default_owner" {
|
|
|
|
name = "mydb_default_owner"
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|