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

View File

@ -29,13 +29,14 @@ func TestProvider_impl(t *testing.T) {
}
func testAccPreCheck(t *testing.T) {
if v := os.Getenv("POSTGRESQL_HOST"); v == "" {
t.Fatal("POSTGRESQL_HOST must be set for acceptance tests")
var host string
if host = os.Getenv("PGHOST"); host == "" {
t.Fatal("PGHOST must be set for acceptance tests")
}
if v := os.Getenv("POSTGRESQL_USERNAME"); v == "" {
t.Fatal("POSTGRESQL_USERNAME must be set for acceptance tests")
if v := os.Getenv("PGUSER"); v == "" {
t.Fatal("PGUSER must be set for acceptance tests")
}
if v := os.Getenv("POSTGRESQL_PASSWORD"); v == "" {
t.Fatal("POSTGRESQL_PASSWORD must be set for acceptance tests")
if v := os.Getenv("PGPASSWORD"); v == "" && host != "localhost" {
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": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
Computed: true,
},
},

View File

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