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" "${CONTAINER_PREFIX}-valkey" 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:RELEASE.2025-10-15T17-29-55Z server /data >/dev/null
61
62 echo "Starting Valkey..."
63 $CONTAINER_CMD run -d \
64 --name "${CONTAINER_PREFIX}-valkey" \
65 -P \
66 --label bspds_test=true \
67 valkey/valkey:8-alpine >/dev/null
68
69 echo "Waiting for services to be ready..."
70 sleep 2
71
72 PG_PORT=$($CONTAINER_CMD port "${CONTAINER_PREFIX}-postgres" 5432 | head -1 | cut -d: -f2)
73 MINIO_PORT=$($CONTAINER_CMD port "${CONTAINER_PREFIX}-minio" 9000 | head -1 | cut -d: -f2)
74 VALKEY_PORT=$($CONTAINER_CMD port "${CONTAINER_PREFIX}-valkey" 6379 | head -1 | cut -d: -f2)
75
76 for i in {1..30}; do
77 if $CONTAINER_CMD exec "${CONTAINER_PREFIX}-postgres" pg_isready -U postgres >/dev/null 2>&1; then
78 break
79 fi
80 echo "Waiting for PostgreSQL... ($i/30)"
81 sleep 1
82 done
83
84 for i in {1..30}; do
85 if curl -s "http://127.0.0.1:${MINIO_PORT}/minio/health/live" >/dev/null 2>&1; then
86 break
87 fi
88 echo "Waiting for MinIO... ($i/30)"
89 sleep 1
90 done
91
92 for i in {1..30}; do
93 if $CONTAINER_CMD exec "${CONTAINER_PREFIX}-valkey" valkey-cli ping 2>/dev/null | grep -q PONG; then
94 break
95 fi
96 echo "Waiting for Valkey... ($i/30)"
97 sleep 1
98 done
99
100 echo "Creating MinIO bucket..."
101 $CONTAINER_CMD run --rm --network host \
102 -e MC_HOST_minio="http://minioadmin:minioadmin@127.0.0.1:${MINIO_PORT}" \
103 minio/mc:RELEASE.2025-07-16T15-35-03Z mb minio/test-bucket --ignore-existing >/dev/null 2>&1 || true
104
105 cat > "$INFRA_FILE" << EOF
106export DATABASE_URL="postgres://postgres:postgres@127.0.0.1:${PG_PORT}/postgres"
107export TEST_DB_PORT="${PG_PORT}"
108export S3_ENDPOINT="http://127.0.0.1:${MINIO_PORT}"
109export S3_BUCKET="test-bucket"
110export AWS_ACCESS_KEY_ID="minioadmin"
111export AWS_SECRET_ACCESS_KEY="minioadmin"
112export AWS_REGION="us-east-1"
113export VALKEY_URL="redis://127.0.0.1:${VALKEY_PORT}"
114export BSPDS_TEST_INFRA_READY="1"
115export BSPDS_ALLOW_INSECURE_SECRETS="1"
116export SKIP_IMPORT_VERIFICATION="true"
117export DISABLE_RATE_LIMITING="1"
118EOF
119
120 echo ""
121 echo "Infrastructure ready!"
122 echo "Config written to: $INFRA_FILE"
123 echo ""
124 cat "$INFRA_FILE"
125}
126
127stop_infra() {
128 echo "Stopping test infrastructure..."
129 $CONTAINER_CMD rm -f "${CONTAINER_PREFIX}-postgres" "${CONTAINER_PREFIX}-minio" "${CONTAINER_PREFIX}-valkey" 2>/dev/null || true
130 rm -f "$INFRA_FILE"
131 echo "Infrastructure stopped."
132}
133
134status_infra() {
135 echo "Test Infrastructure Status:"
136 echo "============================"
137
138 if [[ -f "$INFRA_FILE" ]]; then
139 echo "Config file: $INFRA_FILE"
140 source "$INFRA_FILE"
141 echo "Database URL: $DATABASE_URL"
142 echo "S3 Endpoint: $S3_ENDPOINT"
143 else
144 echo "Config file: NOT FOUND"
145 fi
146
147 echo ""
148 echo "Containers:"
149 $CONTAINER_CMD ps -a --filter "label=bspds_test=true" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null || echo " (none)"
150}
151
152case "${1:-}" in
153 start)
154 start_infra
155 ;;
156 stop)
157 stop_infra
158 ;;
159 restart)
160 stop_infra
161 start_infra
162 ;;
163 status)
164 status_infra
165 ;;
166 env)
167 if [[ -f "$INFRA_FILE" ]]; then
168 cat "$INFRA_FILE"
169 else
170 echo "Infrastructure not running. Run: $0 start" >&2
171 exit 1
172 fi
173 ;;
174 *)
175 echo "Usage: $0 {start|stop|restart|status|env}"
176 echo ""
177 echo "Commands:"
178 echo " start - Start test infrastructure (Postgres, MinIO, Valkey)"
179 echo " stop - Stop and remove test containers"
180 echo " restart - Stop then start infrastructure"
181 echo " status - Show infrastructure status"
182 echo " env - Output environment variables for sourcing"
183 exit 1
184 ;;
185esac