Skip to content

Commit

Permalink
add get query
Browse files Browse the repository at this point in the history
  • Loading branch information
fairclothjm committed Nov 11, 2024
1 parent e9eb2b1 commit c576424
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
27 changes: 27 additions & 0 deletions testutil/postgresqlhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@ func defaultRunOpts(t *testing.T) docker.RunOptions {
}
}

func GetTestPGUser(t *testing.T, connURL string, username, password, query string) {
t.Helper()
t.Logf("[TRACE] Creating test user")

db, err := sql.Open("pgx", connURL)
defer db.Close()
if err != nil {
t.Fatal(err)
}

ctx := context.Background()
_, err = db.ExecContext(ctx, `SELECT set_config('log_statement', 'all', false);`)
if err != nil {
t.Fatal(err)
}

var exists bool
err = db.QueryRowContext(ctx, "SELECT exists (SELECT rolname FROM pg_roles WHERE rolname=$1);", username).Scan(&exists)
if err != nil && err != sql.ErrNoRows {
t.Fatalf("user does not appear to exist: %w", err)

Check failure on line 63 in testutil/postgresqlhelper.go

View workflow job for this annotation

GitHub Actions / acceptance (vault-enterprise:1.18.0-ent)

(*testing.common).Fatalf does not support error-wrapping directive %w
}
if !exists {
t.Fatalf("!exists: %w", err)

Check failure on line 66 in testutil/postgresqlhelper.go

View workflow job for this annotation

GitHub Actions / acceptance (vault-enterprise:1.18.0-ent)

(*testing.common).Fatalf does not support error-wrapping directive %w
}
t.Logf("exists: %b", exists)

Check failure on line 68 in testutil/postgresqlhelper.go

View workflow job for this annotation

GitHub Actions / acceptance (vault-enterprise:1.18.0-ent)

(*testing.common).Logf format %b has arg exists of wrong type bool
}

func CreateTestPGUser(t *testing.T, connURL string, username, password, query string) {
t.Helper()
t.Logf("[TRACE] Creating test user")
Expand Down
4 changes: 2 additions & 2 deletions vault/resource_database_secret_backend_static_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/hashicorp/terraform-provider-vault/internal/consts"

Expand Down Expand Up @@ -181,7 +180,7 @@ CREATE ROLE "{{name}}" WITH

// create static database user
testutil.CreateTestPGUser(t, pgxURL.String(), username, "testpassword", testRoleStaticCreate)
time.Sleep(5)
testutil.GetTestPGUser(t, pgxURL.String(), username, "testpassword", testRoleStaticCreate)

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Expand Down Expand Up @@ -209,6 +208,7 @@ CREATE ROLE "{{name}}" WITH
},
},
})
testutil.GetTestPGUser(t, pgxURL.String(), username, "testpassword", testRoleStaticCreate)
}

func testAccDatabaseSecretBackendStaticRoleCheckDestroy(s *terraform.State) error {
Expand Down

0 comments on commit c576424

Please sign in to comment.