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