From 44af0d60df24a287bea0f958e902cc5a9fa4b3a7 Mon Sep 17 00:00:00 2001 From: James Nugent Date: Mon, 5 Sep 2016 15:39:57 -0700 Subject: [PATCH] provider/postgres: Fix acceptance tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` › 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 ``` --- builtin/providers/postgresql/provider.go | 8 ++++---- builtin/providers/postgresql/provider_test.go | 13 +++++++------ .../postgresql/resource_postgresql_database.go | 1 - .../postgresql/resource_postgresql_database_test.go | 13 +++++-------- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/builtin/providers/postgresql/provider.go b/builtin/providers/postgresql/provider.go index 6fbd9f84a9..4b73192ada 100644 --- a/builtin/providers/postgresql/provider.go +++ b/builtin/providers/postgresql/provider.go @@ -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", }, }, diff --git a/builtin/providers/postgresql/provider_test.go b/builtin/providers/postgresql/provider_test.go index 19c65cb38b..9596a0089b 100644 --- a/builtin/providers/postgresql/provider_test.go +++ b/builtin/providers/postgresql/provider_test.go @@ -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") } } diff --git a/builtin/providers/postgresql/resource_postgresql_database.go b/builtin/providers/postgresql/resource_postgresql_database.go index c48d314626..0cffcaa988 100644 --- a/builtin/providers/postgresql/resource_postgresql_database.go +++ b/builtin/providers/postgresql/resource_postgresql_database.go @@ -26,7 +26,6 @@ func resourcePostgreSQLDatabase() *schema.Resource { "owner": { Type: schema.TypeString, Optional: true, - ForceNew: false, Computed: true, }, }, diff --git a/builtin/providers/postgresql/resource_postgresql_database_test.go b/builtin/providers/postgresql/resource_postgresql_database_test.go index 97cc15e6a1..4fb20e9186 100644 --- a/builtin/providers/postgresql/resource_postgresql_database_test.go +++ b/builtin/providers/postgresql/resource_postgresql_database_test.go @@ -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)