From 27da24f5750e30a44896f4c9c9cff88182cdb44b Mon Sep 17 00:00:00 2001 From: Hein Date: Sun, 28 Dec 2025 15:37:21 +0200 Subject: [PATCH] Better integration test. --- .github/workflows/ci.yml | 2 +- .github/workflows/integration-tests.yml | 66 +++++++++---------------- .github/workflows/release.yml | 2 +- Makefile | 10 ++-- docker-compose.yml | 2 +- tests/postgres/run_tests.sh | 2 +- tests/postgres/run_tests_podman.sh | 2 +- 7 files changed, 33 insertions(+), 53 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8c10433..601e779 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,5 @@ name: CI - +run-name: "Test on master branch" on: push: branches: [ master ] diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index 6a12bd5..8e4977c 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -1,5 +1,5 @@ name: Integration Tests - +run-name: "Integration Tests" on: push: branches: [ master ] @@ -11,6 +11,21 @@ jobs: name: Integration Tests runs-on: ubuntu-latest + services: + postgres: + image: postgres:16-alpine + env: + POSTGRES_USER: relspec + POSTGRES_PASSWORD: relspec_test_password + POSTGRES_DB: relspec_test + ports: + - 5439:5432 + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + steps: - name: Checkout code uses: actions/checkout@v4 @@ -31,41 +46,18 @@ jobs: - name: Download dependencies run: go mod download - - name: Start PostgreSQL container - run: | - docker run -d \ - --name relspec-test-postgres \ - --network host \ - -e POSTGRES_USER=relspec \ - -e POSTGRES_PASSWORD=relspec_test_password \ - -e POSTGRES_DB=relspec_test \ - postgres:16-alpine - - - name: Wait for PostgreSQL to be ready - run: | - echo "Waiting for PostgreSQL to start..." - for i in {1..30}; do - if docker exec relspec-test-postgres pg_isready -U relspec -d relspec_test > /dev/null 2>&1; then - echo "PostgreSQL is ready!" - break - fi - echo "Waiting... ($i/30)" - sleep 1 - done - sleep 2 - - - name: Copy init script into container - run: | - docker cp tests/postgres/init.sql relspec-test-postgres:/tmp/init.sql - - name: Initialize test database + env: + PGPASSWORD: relspec_test_password run: | - docker exec relspec-test-postgres psql -U relspec -d relspec_test -f /tmp/init.sql + psql -h localhost -U relspec -d relspec_test -f tests/postgres/init.sql - name: Verify database setup + env: + PGPASSWORD: relspec_test_password run: | echo "Verifying database initialization..." - docker exec relspec-test-postgres psql -U relspec -d relspec_test -c " + psql -h localhost -U relspec -d relspec_test -c " SELECT (SELECT COUNT(*) FROM pg_namespace WHERE nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast') AND nspname NOT LIKE 'pg_%') as schemas, (SELECT COUNT(*) FROM pg_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema')) as tables, @@ -75,17 +67,5 @@ jobs: - name: Run integration tests env: - RELSPEC_TEST_PG_CONN: postgres://relspec:relspec_test_password@localhost:5432/relspec_test + RELSPEC_TEST_PG_CONN: postgres://relspec:relspec_test_password@localhost:5439/relspec_test run: make test-integration - - - name: Stop PostgreSQL container - if: always() - run: | - docker stop relspec-test-postgres || true - docker rm relspec-test-postgres || true - - - name: Summary - if: always() - run: | - echo "Integration tests completed." - echo "PostgreSQL container has been cleaned up." diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d583494..be4d878 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,5 +1,5 @@ name: Release - +run-name: "Making Release" on: push: tags: diff --git a/Makefile b/Makefile index 80541d8..b82eb20 100644 --- a/Makefile +++ b/Makefile @@ -52,7 +52,7 @@ test-integration: ## Run integration tests (requires RELSPEC_TEST_PG_CONN enviro @echo "Running integration tests..." @if [ -z "$$RELSPEC_TEST_PG_CONN" ]; then \ echo "Error: RELSPEC_TEST_PG_CONN environment variable is not set"; \ - echo "Example: export RELSPEC_TEST_PG_CONN='postgres://relspec:relspec_test_password@localhost:5432/relspec_test'"; \ + echo "Example: export RELSPEC_TEST_PG_CONN='postgres://relspec:relspec_test_password@localhost:5439/relspec_test'"; \ exit 1; \ fi @echo "Running PostgreSQL reader tests..." @@ -116,7 +116,7 @@ docker-up: ## Start PostgreSQL test database -e POSTGRES_USER=relspec \ -e POSTGRES_PASSWORD=relspec_test_password \ -e POSTGRES_DB=relspec_test \ - -p 5433:5432 \ + -p 5439:5432 \ -v ./tests/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql:Z \ postgres:16-alpine 2>/dev/null || echo "Container already running"; \ else \ @@ -124,8 +124,8 @@ docker-up: ## Start PostgreSQL test database fi @echo "Waiting for PostgreSQL to be ready..." @sleep 3 - @echo "PostgreSQL is running on port 5433" - @echo "Connection: postgres://relspec:relspec_test_password@localhost:5433/relspec_test" + @echo "PostgreSQL is running on port 5439" + @echo "Connection: postgres://relspec:relspec_test_password@localhost:5439/relspec_test" docker-down: ## Stop PostgreSQL test database @echo "Stopping PostgreSQL test database (using $(CONTAINER_RUNTIME))..." @@ -147,7 +147,7 @@ docker-test: ## Run PostgreSQL integration tests with Docker/Podman docker-test-integration: docker-up ## Start DB and run integration tests @echo "Running integration tests..." @sleep 2 - @RELSPEC_TEST_PG_CONN="postgres://relspec:relspec_test_password@localhost:5433/relspec_test" \ + @RELSPEC_TEST_PG_CONN="postgres://relspec:relspec_test_password@localhost:5439/relspec_test" \ $(GOTEST) -v ./pkg/readers/pgsql/ -count=1 || (make docker-down && exit 1) @make docker-down diff --git a/docker-compose.yml b/docker-compose.yml index cf45a04..083f976 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,7 +9,7 @@ services: POSTGRES_PASSWORD: relspec_test_password POSTGRES_DB: relspec_test ports: - - "5433:5432" # Using 5433 to avoid conflicts with local PostgreSQL + - "5439:5432" # Using 5439 to avoid conflicts with local PostgreSQL volumes: - ./tests/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql - postgres_data:/var/lib/postgresql/data diff --git a/tests/postgres/run_tests.sh b/tests/postgres/run_tests.sh index 688a99c..92d672e 100755 --- a/tests/postgres/run_tests.sh +++ b/tests/postgres/run_tests.sh @@ -79,7 +79,7 @@ SELECT " # Set environment variable for tests -export RELSPEC_TEST_PG_CONN="postgres://relspec:relspec_test_password@localhost:5433/relspec_test" +export RELSPEC_TEST_PG_CONN="postgres://relspec:relspec_test_password@localhost:5439/relspec_test" echo -e "\n${YELLOW}Running PostgreSQL reader tests...${NC}" echo "Connection string: $RELSPEC_TEST_PG_CONN" diff --git a/tests/postgres/run_tests_podman.sh b/tests/postgres/run_tests_podman.sh index e7b89d0..2b34343 100755 --- a/tests/postgres/run_tests_podman.sh +++ b/tests/postgres/run_tests_podman.sh @@ -21,7 +21,7 @@ CONTAINER_NAME="relspec-test-postgres" POSTGRES_USER="relspec" POSTGRES_PASSWORD="relspec_test_password" POSTGRES_DB="relspec_test" -POSTGRES_PORT="5433" +POSTGRES_PORT="5439" # Check if podman is available if ! command -v podman &> /dev/null; then