provider/postgres: Fix acceptance tests

```
› PGSSLMODE=disable PGHOST=localhost PGUSER=postgres make testacc \
	TEST=./builtin/providers/postgresql
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2016/09/05 15:39:23 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/postgresql -v  -timeout 120m
=== RUN   TestProvider
--- PASS: TestProvider (0.00s)
=== RUN   TestProvider_impl
--- PASS: TestProvider_impl (0.00s)
=== RUN   TestAccPostgresqlDatabase_Basic
--- PASS: TestAccPostgresqlDatabase_Basic (0.53s)
=== RUN   TestAccPostgresqlDatabase_DefaultOwner
--- PASS: TestAccPostgresqlDatabase_DefaultOwner (0.51s)
=== RUN   TestAccPostgresqlRole_Basic
--- PASS: TestAccPostgresqlRole_Basic (0.11s)
PASS
ok     	github.com/hashicorp/terraform/builtin/providers/postgresql    	1.160s
```
This commit is contained in:
James Nugent 2016-09-05 15:39:57 -07:00
parent 34a17d3b46
commit 44af0d60df
4 changed files with 16 additions and 19 deletions

View File

@ -13,7 +13,7 @@ func Provider() terraform.ResourceProvider {
"host": { "host": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"PGUSER", "POSTGRESQL_HOST"}, nil), DefaultFunc: schema.MultiEnvDefaultFunc([]string{"PGHOST", "POSTGRESQL_HOST"}, nil),
Description: "The PostgreSQL server address", Description: "The PostgreSQL server address",
}, },
"port": { "port": {
@ -25,19 +25,19 @@ func Provider() terraform.ResourceProvider {
"username": { "username": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"PGHOST", "POSTGRESQL_HOST"}, nil), DefaultFunc: schema.MultiEnvDefaultFunc([]string{"PGUSER", "POSTGRESQL_USER"}, nil),
Description: "Username for PostgreSQL server connection", Description: "Username for PostgreSQL server connection",
}, },
"password": { "password": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"PGPASSWORD", "POSTGRESQL_PASSWORD"}, nil), DefaultFunc: schema.MultiEnvDefaultFunc([]string{"PGPASSWORD", "POSTGRESQL_PASSWORD"}, nil),
Description: "Password for PostgreSQL server connection", Description: "Password for PostgreSQL server connection",
}, },
"ssl_mode": { "ssl_mode": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Default: "prefer", DefaultFunc: schema.EnvDefaultFunc("PGSSLMODE", "require"),
Description: "Connection mode for PostgreSQL server", Description: "Connection mode for PostgreSQL server",
}, },
}, },

View File

@ -29,13 +29,14 @@ func TestProvider_impl(t *testing.T) {
} }
func testAccPreCheck(t *testing.T) { func testAccPreCheck(t *testing.T) {
if v := os.Getenv("POSTGRESQL_HOST"); v == "" { var host string
t.Fatal("POSTGRESQL_HOST must be set for acceptance tests") if host = os.Getenv("PGHOST"); host == "" {
t.Fatal("PGHOST must be set for acceptance tests")
} }
if v := os.Getenv("POSTGRESQL_USERNAME"); v == "" { if v := os.Getenv("PGUSER"); v == "" {
t.Fatal("POSTGRESQL_USERNAME must be set for acceptance tests") t.Fatal("PGUSER must be set for acceptance tests")
} }
if v := os.Getenv("POSTGRESQL_PASSWORD"); v == "" { if v := os.Getenv("PGPASSWORD"); v == "" && host != "localhost" {
t.Fatal("POSTGRESQL_PASSWORD must be set for acceptance tests") t.Fatal("PGPASSWORD must be set for acceptance tests if PGHOST is not localhost")
} }
} }

View File

@ -26,7 +26,6 @@ func resourcePostgreSQLDatabase() *schema.Resource {
"owner": { "owner": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: false,
Computed: true, Computed: true,
}, },
}, },

View File

@ -20,7 +20,7 @@ func TestAccPostgresqlDatabase_Basic(t *testing.T) {
{ {
Config: testAccPostgresqlDatabaseConfig, Config: testAccPostgresqlDatabaseConfig,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb", "myrole"), testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"postgresql_database.mydb", "name", "mydb"), "postgresql_database.mydb", "name", "mydb"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -41,9 +41,11 @@ func TestAccPostgresqlDatabase_DefaultOwner(t *testing.T) {
{ {
Config: testAccPostgresqlDatabaseConfig, Config: testAccPostgresqlDatabaseConfig,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb_default_owner", ""), testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb_default_owner"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"postgresql_database.mydb_default_owner", "name", "mydb_default_owner"), "postgresql_database.mydb_default_owner", "name", "mydb_default_owner"),
resource.TestCheckResourceAttrSet(
"postgresql_database.mydb_default_owner", "owner"),
), ),
}, },
}, },
@ -72,7 +74,7 @@ func testAccCheckPostgresqlDatabaseDestroy(s *terraform.State) error {
return nil return nil
} }
func testAccCheckPostgresqlDatabaseExists(n string, owner string) resource.TestCheckFunc { func testAccCheckPostgresqlDatabaseExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n] rs, ok := s.RootModule().Resources[n]
if !ok { if !ok {
@ -83,11 +85,6 @@ func testAccCheckPostgresqlDatabaseExists(n string, owner string) resource.TestC
return errors.New("No ID is set") return errors.New("No ID is set")
} }
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) client := testAccProvider.Meta().(*Client)
exists, err := checkDatabaseExists(client, rs.Primary.ID) exists, err := checkDatabaseExists(client, rs.Primary.ID)