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"
99EOF
100
101 echo ""
102 echo "Infrastructure ready!"
103 echo "Config written to: $INFRA_FILE"
104 echo ""
105 cat "$INFRA_FILE"
106}
107
108stop_infra() {
109 echo "Stopping test infrastructure..."
110 $CONTAINER_CMD rm -f "${CONTAINER_PREFIX}-postgres" "${CONTAINER_PREFIX}-minio" 2>/dev/null || true
111 rm -f "$INFRA_FILE"
112 echo "Infrastructure stopped."
113}
114
115status_infra() {
116 echo "Test Infrastructure Status:"
117 echo "============================"
118
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
128 echo ""
129 echo "Containers:"
130 $CONTAINER_CMD ps -a --filter "label=bspds_test=true" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null || echo " (none)"
131}
132
133case "${1:-}" in
134 start)
135 start_infra
136 ;;
137 stop)
138 stop_infra
139 ;;
140 restart)
141 stop_infra
142 start_infra
143 ;;
144 status)
145 status_infra
146 ;;
147 env)
148 if [[ -f "$INFRA_FILE" ]]; then
149 cat "$INFRA_FILE"
150 else
151 echo "Infrastructure not running. Run: $0 start" >&2
152 exit 1
153 fi
154 ;;
155 *)
156 echo "Usage: $0 {start|stop|restart|status|env}"
157 echo ""
158 echo "Commands:"
159 echo " start - Start test infrastructure (Postgres, MinIO)"
160 echo " stop - Stop and remove test containers"
161 echo " restart - Stop then start infrastructure"
162 echo " status - Show infrastructure status"
163 echo " env - Output environment variables for sourcing"
164 exit 1
165 ;;
166esac