139 lines
5.0 KiB
YAML
139 lines
5.0 KiB
YAML
name: Integration Tests
|
|
run-name: "Integration Tests"
|
|
on:
|
|
push:
|
|
branches: [ master ]
|
|
pull_request:
|
|
branches: [ master ]
|
|
|
|
jobs:
|
|
integration-tests:
|
|
name: Integration Tests
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Go
|
|
uses: actions/setup-go@v5
|
|
with:
|
|
go-version: '1.25'
|
|
|
|
- name: Cache Go modules
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: ~/go/pkg/mod
|
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
restore-keys: |
|
|
${{ runner.os }}-go-
|
|
|
|
- name: Download dependencies
|
|
run: go mod download
|
|
|
|
- name: Start PostgreSQL container
|
|
run: |
|
|
docker run -d \
|
|
--name relspec-test-postgres \
|
|
-p 5439:5432 \
|
|
-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 be ready..."
|
|
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 inside container!"
|
|
break
|
|
fi
|
|
echo "Attempt $i/30: Waiting..."
|
|
sleep 2
|
|
done
|
|
# Give it one more second to fully initialize
|
|
sleep 2
|
|
|
|
- name: Get PostgreSQL connection details
|
|
id: pg_connection
|
|
run: |
|
|
# Get container IP address
|
|
CONTAINER_IP=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' relspec-test-postgres)
|
|
echo "Container IP: $CONTAINER_IP"
|
|
|
|
# If container IP exists, use it with port 5432 (internal port)
|
|
# Otherwise fall back to localhost:5439
|
|
if [ -n "$CONTAINER_IP" ]; then
|
|
echo "Using container IP: $CONTAINER_IP:5432"
|
|
echo "pg_host=$CONTAINER_IP" >> $GITHUB_OUTPUT
|
|
echo "pg_port=5432" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "Using localhost:5439"
|
|
echo "pg_host=localhost" >> $GITHUB_OUTPUT
|
|
echo "pg_port=5439" >> $GITHUB_OUTPUT
|
|
fi
|
|
|
|
echo ""
|
|
echo "=== Testing connection from inside container ==="
|
|
docker exec relspec-test-postgres psql -U relspec -d relspec_test -c "SELECT version();"
|
|
|
|
- name: Initialize test database
|
|
run: |
|
|
docker exec -i relspec-test-postgres psql -U relspec -d relspec_test < tests/postgres/init.sql
|
|
|
|
- name: Verify database setup
|
|
run: |
|
|
echo "Verifying database initialization..."
|
|
docker exec relspec-test-postgres psql -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,
|
|
(SELECT COUNT(*) FROM pg_views WHERE schemaname NOT IN ('pg_catalog', 'information_schema')) as views,
|
|
(SELECT COUNT(*) FROM pg_sequences WHERE schemaname NOT IN ('pg_catalog', 'information_schema')) as sequences;
|
|
"
|
|
|
|
- name: Test Go connection to PostgreSQL
|
|
env:
|
|
RELSPEC_TEST_PG_CONN: postgres://relspec:relspec_test_password@${{ steps.pg_connection.outputs.pg_host }}:${{ steps.pg_connection.outputs.pg_port }}/relspec_test
|
|
run: |
|
|
echo "Testing connection using Go (same as integration tests will use)..."
|
|
echo "Connection string: postgres://relspec:***@${{ steps.pg_connection.outputs.pg_host }}:${{ steps.pg_connection.outputs.pg_port }}/relspec_test"
|
|
mkdir -p /tmp/testconn
|
|
cat > /tmp/testconn/test_conn.go << 'EOF'
|
|
package main
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
func main() {
|
|
conn, err := pgx.Connect(context.Background(), os.Getenv("RELSPEC_TEST_PG_CONN"))
|
|
if err != nil {
|
|
fmt.Printf("Connection failed: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
defer conn.Close(context.Background())
|
|
var version string
|
|
err = conn.QueryRow(context.Background(), "SELECT version()").Scan(&version)
|
|
if err != nil {
|
|
fmt.Printf("Query failed: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Printf("SUCCESS! Connected to: %s\n", version)
|
|
}
|
|
EOF
|
|
cd /tmp/testconn && go mod init testconn && go mod tidy && go run test_conn.go
|
|
|
|
- name: Run integration tests
|
|
env:
|
|
RELSPEC_TEST_PG_CONN: postgres://relspec:relspec_test_password@${{ steps.pg_connection.outputs.pg_host }}:${{ steps.pg_connection.outputs.pg_port }}/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
|