fix(tests): update database connection strings for CI
Integration Tests / integration-test (push) Failing after 57s

* Use dynamic host and port for Postgres in tests
* Add helper functions for test database host and port
This commit is contained in:
2026-09-18 21:07:28 +02:00
parent 21ce966d82
commit 63a7494982
7 changed files with 71 additions and 21 deletions
+2 -1
View File
@@ -2,6 +2,7 @@ package integration
import (
"database/sql"
"fmt"
"testing"
"time"
@@ -10,7 +11,7 @@ import (
)
func TestConnection(t *testing.T) {
connStr := "user=user password=password dbname=broker_test host=127.0.0.1 port=5433 sslmode=disable"
connStr := fmt.Sprintf("user=user password=password dbname=broker_test host=%s port=%d sslmode=disable", testDBHost(), testDBPort())
var db *sql.DB
var err error
+2 -1
View File
@@ -3,6 +3,7 @@ package integration
import (
"context"
"database/sql"
"fmt"
"testing"
_ "github.com/lib/pq"
@@ -47,7 +48,7 @@ func TestRLSTenantIsolation(t *testing.T) {
}
runtimeDB, err := sql.Open("postgres",
"user=test_broker_runtime password=test-pass dbname=broker_test host=127.0.0.1 port=5433 sslmode=disable options='-c search_path=broker,public'")
fmt.Sprintf("user=test_broker_runtime password=test-pass dbname=broker_test host=%s port=%d sslmode=disable options='-c search_path=broker,public'", testDBHost(), testDBPort()))
require.NoError(t, err)
defer runtimeDB.Close()
require.NoError(t, runtimeDB.Ping())
+6 -3
View File
@@ -3,6 +3,7 @@ package integration
import (
"context"
"database/sql"
"fmt"
"log/slog"
"testing"
"time"
@@ -14,11 +15,13 @@ import (
"git.warky.dev/wdevs/pgsql-broker/pkg/broker/install"
)
const stage5ConnStr = "user=user password=password dbname=broker_test host=127.0.0.1 port=5433 sslmode=disable"
func stage5ConnStr() string {
return fmt.Sprintf("user=user password=password dbname=broker_test host=%s port=%d sslmode=disable", testDBHost(), testDBPort())
}
func newStage5Adapter(logger adapter.Logger) *adapter.PostgresAdapter {
return adapter.NewPostgresAdapter(adapter.PostgresConfig{
Host: "127.0.0.1", Port: 5433, Database: "broker_test",
Host: testDBHost(), Port: testDBPort(), Database: "broker_test",
User: "user", Password: "password", SSLMode: "disable",
MaxOpenConns: 10, MaxIdleConns: 2,
ConnMaxLifetime: 5 * time.Minute, ConnMaxIdleTime: 10 * time.Minute,
@@ -30,7 +33,7 @@ func newStage5Adapter(logger adapter.Logger) *adapter.PostgresAdapter {
func setupStage5Schema(t *testing.T) *sql.DB {
t.Helper()
db, err := connectWithRetry(stage5ConnStr, 10, 2*time.Second)
db, err := connectWithRetry(stage5ConnStr(), 10, 2*time.Second)
require.NoError(t, err)
cleanupSchema(t, db)
+26
View File
@@ -0,0 +1,26 @@
package integration
import (
"os"
"strconv"
)
// testDBHost and testDBPort let CI point the integration suite at a
// dynamically-assigned Postgres (TEST_DB_HOST/TEST_DB_PORT), avoiding a fixed
// host port that can collide with other jobs on a shared runner. Local dev
// keeps working unset, defaulting to the docker-compose test stack.
func testDBHost() string {
if h := os.Getenv("TEST_DB_HOST"); h != "" {
return h
}
return "127.0.0.1"
}
func testDBPort() int {
if p := os.Getenv("TEST_DB_PORT"); p != "" {
if n, err := strconv.Atoi(p); err == nil {
return n
}
}
return 5433
}
+6 -5
View File
@@ -3,6 +3,7 @@ package integration
import (
"context"
"database/sql"
"fmt"
"log/slog"
"testing"
"time"
@@ -27,7 +28,7 @@ func TestBrokerWorkflow(t *testing.T) {
ctx := context.Background()
// Database connection string
connStr := "user=user password=password dbname=broker_test host=127.0.0.1 port=5433 sslmode=disable"
connStr := fmt.Sprintf("user=user password=password dbname=broker_test host=%s port=%d sslmode=disable", testDBHost(), testDBPort())
// Connect to database with retry logic
db, err := connectWithRetry(connStr, 10, 2*time.Second)
@@ -43,8 +44,8 @@ func TestBrokerWorkflow(t *testing.T) {
// Create database adapter
postgresConfig := adapter.PostgresConfig{
Host: "127.0.0.1",
Port: 5433,
Host: testDBHost(),
Port: testDBPort(),
Database: "broker_test",
User: "user",
Password: "password",
@@ -78,8 +79,8 @@ func TestBrokerWorkflow(t *testing.T) {
Databases: []config.DatabaseConfig{
{
Name: "test_db",
Host: "127.0.0.1",
Port: 5433,
Host: testDBHost(),
Port: testDBPort(),
Database: "broker_test",
User: "user",
Password: "password",