this repo has no description
1#!/usr/bin/env bash 2set -euo pipefail 3 4INFRA_FILE="${TMPDIR:-/tmp}/bspds_test_infra.env" 5CONTAINER_PREFIX="bspds-test" 6 7command_exists() { 8 command -v "$1" >/dev/null 2>&1 9} 10 11if command_exists podman; then 12 CONTAINER_CMD="podman" 13 if [[ -z "${DOCKER_HOST:-}" ]]; then 14 RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}" 15 PODMAN_SOCK="$RUNTIME_DIR/podman/podman.sock" 16 if [[ -S "$PODMAN_SOCK" ]]; then 17 export DOCKER_HOST="unix://$PODMAN_SOCK" 18 fi 19 fi 20elif command_exists docker; then 21 CONTAINER_CMD="docker" 22else 23 echo "Error: Neither podman nor docker found" >&2 24 exit 1 25fi 26 27start_infra() { 28 echo "Starting test infrastructure..." 29 30 if [[ -f "$INFRA_FILE" ]]; then 31 source "$INFRA_FILE" 32 if $CONTAINER_CMD ps --format '{{.Names}}' 2>/dev/null | grep -q "^${CONTAINER_PREFIX}-postgres$"; then 33 echo "Infrastructure already running (found $INFRA_FILE)" 34 cat "$INFRA_FILE" 35 return 0 36 fi 37 echo "Stale infra file found, cleaning up..." 38 rm -f "$INFRA_FILE" 39 fi 40 41 $CONTAINER_CMD rm -f "${CONTAINER_PREFIX}-postgres" "${CONTAINER_PREFIX}-minio" 2>/dev/null || true 42 43 echo "Starting PostgreSQL..." 44 $CONTAINER_CMD run -d \ 45 --name "${CONTAINER_PREFIX}-postgres" \ 46 -e POSTGRES_PASSWORD=postgres \ 47 -e POSTGRES_USER=postgres \ 48 -e POSTGRES_DB=postgres \ 49 -P \ 50 --label bspds_test=true \ 51 postgres:18-alpine >/dev/null 52 53 echo "Starting MinIO..." 54 $CONTAINER_CMD run -d \ 55 --name "${CONTAINER_PREFIX}-minio" \ 56 -e MINIO_ROOT_USER=minioadmin \ 57 -e MINIO_ROOT_PASSWORD=minioadmin \ 58 -P \ 59 --label bspds_test=true \ 60 minio/minio:latest server /data >/dev/null 61 62 echo "Waiting for services to be ready..." 63 sleep 2 64 65 PG_PORT=$($CONTAINER_CMD port "${CONTAINER_PREFIX}-postgres" 5432 | head -1 | cut -d: -f2) 66 MINIO_PORT=$($CONTAINER_CMD port "${CONTAINER_PREFIX}-minio" 9000 | head -1 | cut -d: -f2) 67 68 for i in {1..30}; do 69 if $CONTAINER_CMD exec "${CONTAINER_PREFIX}-postgres" pg_isready -U postgres >/dev/null 2>&1; then 70 break 71 fi 72 echo "Waiting for PostgreSQL... ($i/30)" 73 sleep 1 74 done 75 76 for i in {1..30}; do 77 if curl -s "http://127.0.0.1:${MINIO_PORT}/minio/health/live" >/dev/null 2>&1; then 78 break 79 fi 80 echo "Waiting for MinIO... ($i/30)" 81 sleep 1 82 done 83 84 echo "Creating MinIO bucket..." 85 $CONTAINER_CMD run --rm --network host \ 86 -e MC_HOST_minio="http://minioadmin:minioadmin@127.0.0.1:${MINIO_PORT}" \ 87 minio/mc:latest mb minio/test-bucket --ignore-existing >/dev/null 2>&1 || true 88 89 cat > "$INFRA_FILE" << EOF 90export DATABASE_URL="postgres://postgres:postgres@127.0.0.1:${PG_PORT}/postgres" 91export TEST_DB_PORT="${PG_PORT}" 92export S3_ENDPOINT="http://127.0.0.1:${MINIO_PORT}" 93export S3_BUCKET="test-bucket" 94export AWS_ACCESS_KEY_ID="minioadmin" 95export AWS_SECRET_ACCESS_KEY="minioadmin" 96export AWS_REGION="us-east-1" 97export BSPDS_TEST_INFRA_READY="1" 98export BSPDS_ALLOW_INSECURE_SECRETS="1" 99export SKIP_IMPORT_VERIFICATION="true" 100EOF 101 102 echo "" 103 echo "Infrastructure ready!" 104 echo "Config written to: $INFRA_FILE" 105 echo "" 106 cat "$INFRA_FILE" 107} 108 109stop_infra() { 110 echo "Stopping test infrastructure..." 111 $CONTAINER_CMD rm -f "${CONTAINER_PREFIX}-postgres" "${CONTAINER_PREFIX}-minio" 2>/dev/null || true 112 rm -f "$INFRA_FILE" 113 echo "Infrastructure stopped." 114} 115 116status_infra() { 117 echo "Test Infrastructure Status:" 118 echo "============================" 119 120 if [[ -f "$INFRA_FILE" ]]; then 121 echo "Config file: $INFRA_FILE" 122 source "$INFRA_FILE" 123 echo "Database URL: $DATABASE_URL" 124 echo "S3 Endpoint: $S3_ENDPOINT" 125 else 126 echo "Config file: NOT FOUND" 127 fi 128 129 echo "" 130 echo "Containers:" 131 $CONTAINER_CMD ps -a --filter "label=bspds_test=true" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null || echo " (none)" 132} 133 134case "${1:-}" in 135 start) 136 start_infra 137 ;; 138 stop) 139 stop_infra 140 ;; 141 restart) 142 stop_infra 143 start_infra 144 ;; 145 status) 146 status_infra 147 ;; 148 env) 149 if [[ -f "$INFRA_FILE" ]]; then 150 cat "$INFRA_FILE" 151 else 152 echo "Infrastructure not running. Run: $0 start" >&2 153 exit 1 154 fi 155 ;; 156 *) 157 echo "Usage: $0 {start|stop|restart|status|env}" 158 echo "" 159 echo "Commands:" 160 echo " start - Start test infrastructure (Postgres, MinIO)" 161 echo " stop - Stop and remove test containers" 162 echo " restart - Stop then start infrastructure" 163 echo " status - Show infrastructure status" 164 echo " env - Output environment variables for sourcing" 165 exit 1 166 ;; 167esac