diff --git a/pkg/bdd/testserver/server.go b/pkg/bdd/testserver/server.go index c60986e..5512eb2 100644 --- a/pkg/bdd/testserver/server.go +++ b/pkg/bdd/testserver/server.go @@ -5,6 +5,7 @@ import ( "database/sql" "fmt" "net/http" + "os" "strings" "time" @@ -15,6 +16,17 @@ import ( "github.com/rs/zerolog/log" ) +// getPostgresHost returns the appropriate PostgreSQL host based on environment +// Uses DLC_DATABASE_HOST environment variable or defaults to container name +func getPostgresHost() string { + host := os.Getenv("DLC_DATABASE_HOST") + if host != "" { + return host + } + // Default to container name for Docker environments + return "dance-lessons-coach-postgres" +} + type Server struct { httpServer *http.Server port int @@ -260,7 +272,7 @@ func createTestConfig(port int) *config.Config { AdminMasterPassword: "admin123", }, Database: config.DatabaseConfig{ - Host: "localhost", + Host: getPostgresHost(), // Use container name in Docker, localhost otherwise Port: 5432, User: "postgres", Password: "postgres", diff --git a/scripts/test-local-ci-cd.sh b/scripts/test-local-ci-cd.sh index 13dc5c3..110e8de 100755 --- a/scripts/test-local-ci-cd.sh +++ b/scripts/test-local-ci-cd.sh @@ -3,7 +3,7 @@ # Simulates the CI/CD pipeline but builds Docker image locally # Use this for local development and testing without Gitea -set -e +set -eu echo "🚀 Local CI/CD Testing" echo "======================" @@ -34,7 +34,7 @@ echo "" # 2. Calculate dependency hash (match CI workflow) echo "2. Calculating dependency hash..." -DEPS_HASH=$(sha256sum go.mod go.sum | sha256sum | cut -d' ' -f1 | head -c 12) +export DEPS_HASH=$(sha256sum go.mod go.sum | sha256sum | cut -d' ' -f1 | head -c 12) echo "Dependency hash: $DEPS_HASH" echo "✅ Dependency hash calculated" echo "" @@ -74,12 +74,13 @@ if [ "$HAS_DOCKER" = true ]; then sleep 2 done - # Set PostgreSQL environment variables - export PGHOST="dance-lessons-coach-postgres" - export PGPORT=5432 - export PGUSER=postgres - export PGPASSWORD=postgres - export PGDATABASE=dance_lessons_coach_bdd_test + # Set PostgreSQL environment variables for BDD tests + export DLC_DATABASE_HOST="dance-lessons-coach-postgres" + export DLC_DATABASE_PORT=5432 + export DLC_DATABASE_USER=postgres + export DLC_DATABASE_PASSWORD=postgres + export DLC_DATABASE_NAME=dance_lessons_coach_bdd_test + export DLC_DATABASE_SSL_MODE=disable else echo "⚠️ Docker not available - PostgreSQL tests will be skipped" fi @@ -101,7 +102,7 @@ echo "" echo "6. Generating Swagger documentation..." if [ "$USE_DOCKER_CACHE" = true ]; then echo "Running in Docker Compose container..." - docker compose -f docker-compose.build.yml exec -w /workspace/pkg/server build-cache sh -c "go generate" + docker compose -f docker-compose.build.yml run -w /workspace/pkg/server build-cache sh -c "go generate" else echo "Running natively..." cd pkg/server && go generate @@ -114,7 +115,7 @@ echo "" echo "7. Building and testing..." if [ "$USE_DOCKER_CACHE" = true ]; then echo "Running in Docker Compose container..." - docker compose -f docker-compose.build.yml exec -w /workspace build-cache sh -c "go build ./..." + docker compose -f docker-compose.build.yml run -w /workspace build-cache sh -c "go build ./..." else echo "Running natively..." go build ./... @@ -123,18 +124,26 @@ echo "✅ Code compiled successfully" if [ "$USE_DOCKER_CACHE" = true ]; then echo "Running in Docker Compose container with PostgreSQL..." - docker compose -f docker-compose.build.yml exec \ - -e PGHOST=dance-lessons-coach-postgres \ - -e PGPORT=5432 \ - -e PGUSER=postgres \ - -e PGPASSWORD=postgres \ - -e PGDATABASE=dance_lessons_coach_bdd_test \ + docker compose -f docker-compose.build.yml run \ + -e DLC_DATABASE_HOST=dance-lessons-coach-postgres \ + -e DLC_DATABASE_PORT=5432 \ + -e DLC_DATABASE_USER=postgres \ + -e DLC_DATABASE_PASSWORD=postgres \ + -e DLC_DATABASE_NAME=dance_lessons_coach_bdd_test \ + -e DLC_DATABASE_SSL_MODE=disable \ -w /workspace \ build-cache \ sh -c "go test ./... -coverprofile=coverage.out -v && go tool cover -func=coverage.out > coverage.txt" else - echo "Running natively..." - go test ./... -cover -v + echo "Running natively with Docker Compose PostgreSQL..." + export DLC_DATABASE_HOST=dance-lessons-coach-postgres + export DLC_DATABASE_PORT=5432 + export DLC_DATABASE_USER=postgres + export DLC_DATABASE_PASSWORD=postgres + export DLC_DATABASE_NAME=dance_lessons_coach_bdd_test + export DLC_DATABASE_SSL_MODE=disable + go test ./... -coverprofile=coverage.out -v + go tool cover -func=coverage.out > coverage.txt fi echo "✅ Tests passed" echo "" @@ -143,7 +152,7 @@ echo "" echo "8. Building binaries..." if [ "$USE_DOCKER_CACHE" = true ]; then echo "Running in Docker Compose container..." - docker compose -f docker-compose.build.yml exec -w /workspace build-cache sh -c "./scripts/build.sh" + docker compose -f docker-compose.build.yml run -w /workspace build-cache sh -c "./scripts/build.sh" else echo "Running natively..." ./scripts/build.sh