Skip to content

Commit

Permalink
chore: use sequential ports to avoid 5432 conflicts (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
enocom authored Jan 10, 2024
1 parent 1724277 commit 19d7ad9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
42 changes: 28 additions & 14 deletions tests/alloydb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func TestPostgresTCP(t *testing.T) {
requirePostgresVars(t)

dsn := fmt.Sprintf(
"host=127.0.0.1 user=%v password=%v database=%v sslmode=disable",
"host=127.0.0.1 port=10000 user=%v password=%v database=%v sslmode=disable",
*alloydbUser, *alloydbPass, *alloydbDB,
)
proxyConnTest(t, []string{*alloydbInstanceName}, "pgx", dsn)
proxyConnTest(t, []string{*alloydbInstanceName, "--port=10000"}, "pgx", dsn)
}

func TestPostgresAutoIAMAuthN(t *testing.T) {
Expand All @@ -87,9 +87,11 @@ func TestPostgresAutoIAMAuthN(t *testing.T) {
}
requirePostgresVars(t)

dsn := fmt.Sprintf("host=127.0.0.1 user=%v database=%v sslmode=disable",
dsn := fmt.Sprintf("host=127.0.0.1 port=10001 user=%v database=%v sslmode=disable",
*alloydbIAMUser, *alloydbDB)
proxyConnTest(t, []string{*alloydbInstanceName, "--auto-iam-authn"}, "pgx", dsn)
proxyConnTest(t, []string{
*alloydbInstanceName, "--auto-iam-authn", "--port=10001",
}, "pgx", dsn)
}

func createTempDir(t *testing.T) (string, func()) {
Expand Down Expand Up @@ -132,10 +134,14 @@ func TestPostgresAuthWithToken(t *testing.T) {
tok, _, cleanup2 := removeAuthEnvVar(t, true)
defer cleanup2()

dsn := fmt.Sprintf("host=localhost user=%v password=%v database=%v sslmode=disable",
*alloydbUser, *alloydbPass, *alloydbDB)
dsn := fmt.Sprintf(
"host=localhost port=10002 user=%v password=%v database=%v sslmode=disable",
*alloydbUser, *alloydbPass, *alloydbDB,
)
proxyConnTest(t,
[]string{"--token", tok.AccessToken, *alloydbInstanceName},
[]string{"--token", tok.AccessToken, *alloydbInstanceName,
"--port=10002",
},
"pgx", dsn)
}

Expand All @@ -147,10 +153,13 @@ func TestPostgresAuthWithCredentialsFile(t *testing.T) {
_, path, cleanup2 := removeAuthEnvVar(t, false)
defer cleanup2()

dsn := fmt.Sprintf("host=localhost user=%v password=%v database=%v sslmode=disable",
dsn := fmt.Sprintf(
"host=localhost port=10003 user=%v password=%v database=%v sslmode=disable",
*alloydbUser, *alloydbPass, *alloydbDB)
proxyConnTest(t,
[]string{"--credentials-file", path, *alloydbInstanceName},
[]string{"--credentials-file", path, *alloydbInstanceName,
"--port=10003",
},
"pgx", dsn)
}

Expand All @@ -163,10 +172,13 @@ func TestPostgresAuthWithCredentialsJSON(t *testing.T) {
_, _, cleanup := removeAuthEnvVar(t, false)
defer cleanup()

dsn := fmt.Sprintf("host=localhost user=%s password=%s database=%s sslmode=disable",
dsn := fmt.Sprintf(
"host=localhost port=10004 user=%s password=%s database=%s sslmode=disable",
*alloydbUser, *alloydbPass, *alloydbDB)
proxyConnTest(t,
[]string{"--json-credentials", string(creds), *alloydbInstanceName},
[]string{"--json-credentials", string(creds), *alloydbInstanceName,
"--port=10004",
},
"pgx", dsn)
}

Expand All @@ -176,9 +188,11 @@ func TestAuthWithGcloudAuth(t *testing.T) {
}
requirePostgresVars(t)

dsn := fmt.Sprintf("host=localhost user=%s password=%s database=%s sslmode=disable",
*alloydbUser, *alloydbPass, *alloydbDB)
dsn := fmt.Sprintf(
"host=localhost port=10005 user=%s password=%s database=%s sslmode=disable",
*alloydbUser, *alloydbPass, *alloydbDB,
)
proxyConnTest(t,
[]string{"--gcloud-auth", *alloydbInstanceName},
[]string{"--gcloud-auth", *alloydbInstanceName, "--port=10005"},
"pgx", dsn)
}
4 changes: 4 additions & 0 deletions tests/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const connTestTimeout = time.Minute
// and then unsets GOOGLE_APPLICATION_CREDENTIALS. It returns a cleanup function
// that restores the original setup.
func removeAuthEnvVar(t *testing.T, wantToken bool) (*oauth2.Token, string, func()) {
t.Helper()
var tok *oauth2.Token
if wantToken {
ts, err := google.DefaultTokenSource(context.Background(),
Expand All @@ -57,6 +58,7 @@ func removeAuthEnvVar(t *testing.T, wantToken bool) (*oauth2.Token, string, func
}

func keyfile(t *testing.T) string {
t.Helper()
path := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
if path == "" {
t.Fatal("GOOGLE_APPLICATION_CREDENTIALS not set")
Expand All @@ -70,10 +72,12 @@ func keyfile(t *testing.T) string {

// proxyConnTest is a test helper to verify the proxy works with a basic connectivity test.
func proxyConnTest(t *testing.T, args []string, driver, dsn string) {
t.Helper()
proxyConnTestWithReady(t, args, driver, dsn, func() error { return nil })
}

func proxyConnTestWithReady(t *testing.T, args []string, driver, dsn string, ready func() error) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), connTestTimeout)
defer cancel()
// Start the proxy
Expand Down

0 comments on commit 19d7ad9

Please sign in to comment.