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:latest 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:latest 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"
117EOF
118
119 echo ""
120 echo "Infrastructure ready!"
121 echo "Config written to: $INFRA_FILE"
122 echo ""
123 cat "$INFRA_FILE"
124}
125
126stop_infra() {
127 echo "Stopping test infrastructure..."
128 $CONTAINER_CMD rm -f "${CONTAINER_PREFIX}-postgres" "${CONTAINER_PREFIX}-minio" "${CONTAINER_PREFIX}-valkey" 2>/dev/null || true
129 rm -f "$INFRA_FILE"
130 echo "Infrastructure stopped."
131}
132
133status_infra() {
134 echo "Test Infrastructure Status:"
135 echo "============================"
136
137 if [[ -f "$INFRA_FILE" ]]; then
138 echo "Config file: $INFRA_FILE"
139 source "$INFRA_FILE"
140 echo "Database URL: $DATABASE_URL"
141 echo "S3 Endpoint: $S3_ENDPOINT"
142 else
143 echo "Config file: NOT FOUND"
144 fi
145
146 echo ""
147 echo "Containers:"
148 $CONTAINER_CMD ps -a --filter "label=bspds_test=true" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null || echo " (none)"
149}
150
151case "${1:-}" in
152 start)
153 start_infra
154 ;;
155 stop)
156 stop_infra
157 ;;
158 restart)
159 stop_infra
160 start_infra
161 ;;
162 status)
163 status_infra
164 ;;
165 env)
166 if [[ -f "$INFRA_FILE" ]]; then
167 cat "$INFRA_FILE"
168 else
169 echo "Infrastructure not running. Run: $0 start" >&2
170 exit 1
171 fi
172 ;;
173 *)
174 echo "Usage: $0 {start|stop|restart|status|env}"
175 echo ""
176 echo "Commands:"
177 echo " start - Start test infrastructure (Postgres, MinIO, Valkey)"
178 echo " stop - Stop and remove test containers"
179 echo " restart - Stop then start infrastructure"
180 echo " status - Show infrastructure status"
181 echo " env - Output environment variables for sourcing"
182 exit 1
183 ;;
184esac