#!/bin/bash set -e # RelSpec PostgreSQL Integration Tests Runner (Podman version) # This script starts a PostgreSQL Podman container and runs integration tests SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" echo "=== RelSpec PostgreSQL Integration Tests (Podman) ===" echo "" # Colors for output GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # No Color # Container configuration CONTAINER_NAME="relspec-test-postgres" POSTGRES_USER="relspec" POSTGRES_PASSWORD="relspec_test_password" POSTGRES_DB="relspec_test" POSTGRES_PORT="5439" # Check if podman is available if ! command -v podman &> /dev/null; then echo -e "${RED}Error: podman is not installed${NC}" exit 1 fi # Change to project root cd "$PROJECT_ROOT" # Function to cleanup cleanup() { echo -e "\n${YELLOW}Cleaning up...${NC}" podman stop "$CONTAINER_NAME" 2>/dev/null || true podman rm "$CONTAINER_NAME" 2>/dev/null || true } # Trap exit to cleanup trap cleanup EXIT # Stop and remove existing container if it exists echo -e "${YELLOW}Cleaning up any existing containers...${NC}" podman stop "$CONTAINER_NAME" 2>/dev/null || true podman rm "$CONTAINER_NAME" 2>/dev/null || true # Start PostgreSQL container echo -e "${YELLOW}Starting PostgreSQL container...${NC}" podman run -d \ --name "$CONTAINER_NAME" \ -e POSTGRES_USER="$POSTGRES_USER" \ -e POSTGRES_PASSWORD="$POSTGRES_PASSWORD" \ -e POSTGRES_DB="$POSTGRES_DB" \ -p "$POSTGRES_PORT:5432" \ -v "$SCRIPT_DIR/init.sql:/docker-entrypoint-initdb.d/init.sql:Z" \ docker.io/library/postgres:16-alpine # Wait for PostgreSQL to be ready echo -e "${YELLOW}Waiting for PostgreSQL to be ready...${NC}" max_attempts=30 attempt=0 while [ $attempt -lt $max_attempts ]; do if podman exec "$CONTAINER_NAME" pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB" &> /dev/null; then echo -e "${GREEN}PostgreSQL is ready!${NC}" break fi attempt=$((attempt + 1)) echo -n "." sleep 1 done if [ $attempt -eq $max_attempts ]; then echo -e "\n${RED}Error: PostgreSQL failed to start${NC}" podman logs "$CONTAINER_NAME" exit 1 fi # Give it one more second to fully initialize sleep 2 # Show database stats echo -e "\n${YELLOW}Database Information:${NC}" podman exec "$CONTAINER_NAME" psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -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; " # Set environment variable for tests export RELSPEC_TEST_PG_CONN="postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:$POSTGRES_PORT/$POSTGRES_DB" echo -e "\n${YELLOW}Running PostgreSQL reader tests...${NC}" echo "Connection string: $RELSPEC_TEST_PG_CONN" echo "" # Run the tests cd "$PROJECT_ROOT" if go test -v ./pkg/readers/pgsql/ -count=1; then echo -e "\n${GREEN}✓ All tests passed!${NC}" exit 0 else echo -e "\n${RED}✗ Tests failed${NC}" exit 1 fi