fix(tests): update database connection strings for CI
Integration Tests / integration-test (push) Failing after 57s
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:
@@ -20,7 +20,11 @@ jobs:
|
||||
POSTGRES_USER: user
|
||||
POSTGRES_PASSWORD: password
|
||||
ports:
|
||||
- 5433:5432
|
||||
# No fixed host port: Docker assigns a free one, avoiding
|
||||
# "port is already allocated" collisions with other jobs on a
|
||||
# shared runner. The assigned port is read below via the
|
||||
# `job.services.postgres.ports` context.
|
||||
- 5432/tcp
|
||||
options: >-
|
||||
--health-cmd="pg_isready -U user"
|
||||
--health-interval=5s
|
||||
@@ -38,4 +42,7 @@ jobs:
|
||||
cache: true
|
||||
|
||||
- name: Run all tests
|
||||
run: make test-ci
|
||||
env:
|
||||
TEST_DB_HOST: 127.0.0.1
|
||||
TEST_DB_PORT: ${{ job.services.postgres.ports['5432'] }}
|
||||
run: make test-ci TEST_DB_HOST="$TEST_DB_HOST" TEST_DB_PORT="$TEST_DB_PORT"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
.PHONY: all build clean test test-all test-ci test-integration-go test-unit-go test-connection schema-install broker-start broker-stop install deps docker-up docker-down docker-build release help
|
||||
.PHONY: all build clean test test-all test-ci test-integration-go test-unit-go test-connection generate-test-config schema-install broker-start broker-stop install deps docker-up docker-down docker-build release help
|
||||
|
||||
# Build variables
|
||||
BINARY_NAME=pgsql-broker
|
||||
@@ -30,6 +30,13 @@ COMPOSE_CMD := $(shell \
|
||||
|
||||
|
||||
|
||||
# Test database connection info. Override in CI to point at a dynamically
|
||||
# assigned Postgres (e.g. a services: block port), avoiding a fixed host port
|
||||
# that can collide with other jobs on a shared runner.
|
||||
TEST_DB_HOST ?= 127.0.0.1
|
||||
TEST_DB_PORT ?= 5433
|
||||
TEST_CONFIG := $(BIN_DIR)/broker.test.runtime.yaml
|
||||
|
||||
# Version information
|
||||
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
|
||||
BUILD_TIME=$(shell date -u '+%Y-%m-%d_%H:%M:%S')
|
||||
@@ -68,12 +75,16 @@ test-all: test-teardown test-setup test-connection schema-install broker-start t
|
||||
test-ci: test-connection schema-install broker-start test-local-unit test-integration-go broker-stop ## Run all unit and integration tests against an externally-provided Postgres (CI services: block)
|
||||
|
||||
test-connection: deps ## Test database connection with retry
|
||||
@echo "Testing database connection..."
|
||||
@$(GO) test -v ./tests/integration/connection_test.go
|
||||
@echo "Testing database connection (host=$(TEST_DB_HOST) port=$(TEST_DB_PORT))..."
|
||||
@TEST_DB_HOST=$(TEST_DB_HOST) TEST_DB_PORT=$(TEST_DB_PORT) $(GO) test -v -run '^TestConnection$$' ./tests/integration/...
|
||||
|
||||
schema-install: build ## Install database schema using the broker CLI
|
||||
generate-test-config: ## (internal) render broker.test.yaml with TEST_DB_HOST/TEST_DB_PORT
|
||||
@mkdir -p $(BIN_DIR)
|
||||
@sed -e "s/^ host: .*/ host: $(TEST_DB_HOST)/" -e "s/^ port: .*/ port: $(TEST_DB_PORT)/" broker.test.yaml > $(TEST_CONFIG)
|
||||
|
||||
schema-install: build generate-test-config ## Install database schema using the broker CLI
|
||||
@echo "Installing database schema..."
|
||||
@$(BIN_DIR)/$(BINARY_NAME) install --config broker.test.yaml
|
||||
@$(BIN_DIR)/$(BINARY_NAME) install --config $(TEST_CONFIG)
|
||||
|
||||
test-setup: build ## Start test environment (docker-compose/podman-compose)
|
||||
@echo "Starting test environment (using $(COMPOSE_CMD))..."
|
||||
@@ -102,9 +113,9 @@ test-teardown: ## Stop test environment (docker-compose/podman-compose)
|
||||
fi
|
||||
@sleep 5 # Give the container runtime time to release resources
|
||||
|
||||
broker-start: build ## Start the broker in the background
|
||||
broker-start: build generate-test-config ## Start the broker in the background
|
||||
@echo "Starting broker..."
|
||||
@setsid $(BIN_DIR)/$(BINARY_NAME) start --config broker.test.yaml > broker.log 2>&1 < /dev/null & echo $$! > broker.pid
|
||||
@setsid $(BIN_DIR)/$(BINARY_NAME) start --config $(TEST_CONFIG) > broker.log 2>&1 < /dev/null & echo $$! > broker.pid
|
||||
@sleep 5 # Give the broker a moment to start
|
||||
|
||||
broker-stop: ## Stop the broker
|
||||
@@ -117,8 +128,8 @@ broker-stop: ## Stop the broker
|
||||
fi
|
||||
|
||||
test-integration-go: ## Run Go integration tests
|
||||
@echo "Running Go integration tests..."
|
||||
@$(GO) test -v ./tests/integration/...
|
||||
@echo "Running Go integration tests (host=$(TEST_DB_HOST) port=$(TEST_DB_PORT))..."
|
||||
@TEST_DB_HOST=$(TEST_DB_HOST) TEST_DB_PORT=$(TEST_DB_PORT) $(GO) test -v ./tests/integration/...
|
||||
|
||||
install: build ## Install the binary to GOPATH/bin
|
||||
@echo "Installing to GOPATH/bin..."
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user