longshot
This commit is contained in:
130
.github/workflows/integration-tests.yml
vendored
130
.github/workflows/integration-tests.yml
vendored
@@ -11,6 +11,21 @@ jobs:
|
|||||||
name: Integration Tests
|
name: Integration Tests
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:16-alpine
|
||||||
|
env:
|
||||||
|
POSTGRES_USER: relspec
|
||||||
|
POSTGRES_PASSWORD: relspec_test_password
|
||||||
|
POSTGRES_DB: relspec_test
|
||||||
|
options: >-
|
||||||
|
--health-cmd pg_isready
|
||||||
|
--health-interval 10s
|
||||||
|
--health-timeout 5s
|
||||||
|
--health-retries 5
|
||||||
|
ports:
|
||||||
|
- 5432:5432
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -31,68 +46,19 @@ jobs:
|
|||||||
- name: Download dependencies
|
- name: Download dependencies
|
||||||
run: go mod download
|
run: go mod download
|
||||||
|
|
||||||
- name: Create Docker network and connect runner
|
|
||||||
run: |
|
|
||||||
# Create network
|
|
||||||
docker network create relspec-test-network || true
|
|
||||||
|
|
||||||
# Get current container ID (the runner)
|
|
||||||
RUNNER_CONTAINER_ID=$(cat /proc/self/cgroup | grep 'docker\|kubepods' | head -1 | sed 's/^.*\///' | cut -d':' -f3 | sed 's/^.*\///')
|
|
||||||
if [ -z "$RUNNER_CONTAINER_ID" ]; then
|
|
||||||
# Alternative method: use hostname which is often the container ID
|
|
||||||
RUNNER_CONTAINER_ID=$(hostname)
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Runner container ID: $RUNNER_CONTAINER_ID"
|
|
||||||
|
|
||||||
# Connect runner to the network so tests can access PostgreSQL by hostname
|
|
||||||
docker network connect relspec-test-network "$RUNNER_CONTAINER_ID" 2>/dev/null || echo "Already connected or unable to connect"
|
|
||||||
|
|
||||||
- name: Start PostgreSQL container
|
|
||||||
run: |
|
|
||||||
docker run -d \
|
|
||||||
--name relspec-test-postgres \
|
|
||||||
--network relspec-test-network \
|
|
||||||
-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: |
|
|
||||||
# Use container name for DNS resolution on the Docker network
|
|
||||||
# This works because both containers are on the same custom network
|
|
||||||
echo "Using container hostname: relspec-test-postgres:5432"
|
|
||||||
echo "pg_host=relspec-test-postgres" >> $GITHUB_OUTPUT
|
|
||||||
echo "pg_port=5432" >> $GITHUB_OUTPUT
|
|
||||||
|
|
||||||
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
|
- name: Initialize test database
|
||||||
|
env:
|
||||||
|
PGPASSWORD: relspec_test_password
|
||||||
run: |
|
run: |
|
||||||
docker exec -i relspec-test-postgres psql -U relspec -d relspec_test < tests/postgres/init.sql
|
# Services are accessible via hostname matching the service name
|
||||||
|
psql -h postgres -U relspec -d relspec_test -f tests/postgres/init.sql
|
||||||
|
|
||||||
- name: Verify database setup
|
- name: Verify database setup
|
||||||
|
env:
|
||||||
|
PGPASSWORD: relspec_test_password
|
||||||
run: |
|
run: |
|
||||||
echo "Verifying database initialization..."
|
echo "Verifying database initialization..."
|
||||||
docker exec relspec-test-postgres psql -U relspec -d relspec_test -c "
|
psql -h postgres -U relspec -d relspec_test -c "
|
||||||
SELECT
|
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_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_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema')) as tables,
|
||||||
@@ -100,57 +66,7 @@ jobs:
|
|||||||
(SELECT COUNT(*) FROM pg_sequences WHERE schemaname NOT IN ('pg_catalog', 'information_schema')) as sequences;
|
(SELECT COUNT(*) FROM pg_sequences WHERE schemaname NOT IN ('pg_catalog', 'information_schema')) as sequences;
|
||||||
"
|
"
|
||||||
|
|
||||||
- name: Test Go connection to PostgreSQL
|
|
||||||
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"
|
|
||||||
|
|
||||||
# Create test program
|
|
||||||
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
|
|
||||||
|
|
||||||
# Build the test program
|
|
||||||
cd /tmp/testconn && go mod init testconn && go mod tidy && go build -o test_conn
|
|
||||||
|
|
||||||
# Run in a container on the same network
|
|
||||||
docker run --rm \
|
|
||||||
--network relspec-test-network \
|
|
||||||
-e RELSPEC_TEST_PG_CONN="postgres://relspec:relspec_test_password@${{ steps.pg_connection.outputs.pg_host }}:${{ steps.pg_connection.outputs.pg_port }}/relspec_test" \
|
|
||||||
-v /tmp/testconn:/app \
|
|
||||||
golang:1.25-alpine \
|
|
||||||
/app/test_conn
|
|
||||||
|
|
||||||
- name: Run integration tests
|
- name: Run integration tests
|
||||||
env:
|
env:
|
||||||
RELSPEC_TEST_PG_CONN: postgres://relspec:relspec_test_password@${{ steps.pg_connection.outputs.pg_host }}:${{ steps.pg_connection.outputs.pg_port }}/relspec_test
|
RELSPEC_TEST_PG_CONN: postgres://relspec:relspec_test_password@postgres:5432/relspec_test
|
||||||
run: make test-integration
|
run: make test-integration
|
||||||
|
|
||||||
- name: Stop PostgreSQL container
|
|
||||||
if: always()
|
|
||||||
run: |
|
|
||||||
docker stop relspec-test-postgres || true
|
|
||||||
docker rm relspec-test-postgres || true
|
|
||||||
docker network rm relspec-test-network || true
|
|
||||||
|
|||||||
Reference in New Issue
Block a user