-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-smoke-test.sh
More file actions
executable file
·258 lines (227 loc) · 8 KB
/
docker-smoke-test.sh
File metadata and controls
executable file
·258 lines (227 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env bash
# Docker deployment smoke test.
#
# Validates the real docker-compose shape by:
# 1. Building and starting services (postgres + app)
# 2. Waiting for /health to respond
# 3. Verifying /memories/health returns valid config
# 4. Posting a minimal ingest and confirming 200
# 5. Running a search and confirming results
# 6. Tearing down
#
# Catches the class of bug where the app builds and starts but fails at
# runtime due to provider misconfiguration (e.g., ollama on localhost
# inside a container, missing env vars, broken DB connection).
#
# Usage:
# ./scripts/docker-smoke-test.sh # full build + test
# SKIP_BUILD=1 ./scripts/docker-smoke-test.sh # reuse existing image
#
# Requirements: docker, docker compose, curl, jq
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
COMPOSE_PROJECT="${COMPOSE_PROJECT:-}"
APP_PORT="${APP_PORT:-}"
POSTGRES_PORT="${POSTGRES_PORT:-}"
HEALTH_TIMEOUT=90
HEALTH_INTERVAL=2
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
passed=0
failed=0
total=0
log() { echo -e "${GREEN}[smoke]${NC} $*"; }
warn() { echo -e "${YELLOW}[smoke]${NC} $*"; }
fail() { echo -e "${RED}[FAIL]${NC} $*"; }
port_in_use() {
local port="$1"
lsof -n -P -iTCP:"$port" -sTCP:LISTEN >/dev/null 2>&1
}
find_free_port() {
local port="$1"
while port_in_use "$port"; do
port=$((port + 1))
done
echo "$port"
}
resolve_port() {
local requested="$1"
local fallback="$2"
if [[ -n "$requested" ]]; then
if port_in_use "$requested"; then
fail "Requested port is already in use: $requested"
exit 1
fi
echo "$requested"
return
fi
find_free_port "$fallback"
}
assert_ok() {
local name="$1"
total=$((total + 1))
if eval "$2"; then
log " PASS: $name"
passed=$((passed + 1))
else
fail " FAIL: $name"
failed=$((failed + 1))
fi
}
cleanup() {
log "Tearing down compose stack..."
cd "$PROJECT_DIR"
docker compose -p "$COMPOSE_PROJECT" down -v --remove-orphans 2>/dev/null || true
}
trap cleanup EXIT
# --- Pre-flight checks ---
for cmd in docker curl jq lsof; do
if ! command -v "$cmd" &>/dev/null; then
fail "Required command not found: $cmd"
exit 1
fi
done
if ! docker info &>/dev/null; then
fail "Docker daemon is not running"
exit 1
fi
cd "$PROJECT_DIR"
APP_PORT="$(resolve_port "$APP_PORT" 3060)"
POSTGRES_PORT="$(resolve_port "$POSTGRES_PORT" 5444)"
if [[ -z "$COMPOSE_PROJECT" ]]; then
COMPOSE_PROJECT="supermem-smoke-test-${APP_PORT}-${POSTGRES_PORT}"
fi
# --- Build + Start ---
log "Starting compose stack (project=$COMPOSE_PROJECT, app_port=$APP_PORT, postgres_port=$POSTGRES_PORT)..."
# Override published ports to avoid conflicts with local dev stacks
export APP_PORT POSTGRES_PORT
if [[ "${SKIP_BUILD:-}" == "1" ]]; then
docker compose -p "$COMPOSE_PROJECT" \
-f docker-compose.yml -f docker-compose.smoke.yml \
up -d
else
docker compose -p "$COMPOSE_PROJECT" \
-f docker-compose.yml -f docker-compose.smoke.yml \
up -d --build
fi
# --- Wait for health ---
log "Waiting for app health (timeout=${HEALTH_TIMEOUT}s)..."
elapsed=0
while true; do
if curl -sf "http://localhost:${APP_PORT}/health" >/dev/null 2>&1; then
log "App is healthy after ${elapsed}s"
break
fi
if [[ $elapsed -ge $HEALTH_TIMEOUT ]]; then
fail "App did not become healthy within ${HEALTH_TIMEOUT}s"
echo ""
warn "--- app container logs ---"
docker compose -p "$COMPOSE_PROJECT" logs app 2>&1 | tail -40
exit 1
fi
sleep "$HEALTH_INTERVAL"
elapsed=$((elapsed + HEALTH_INTERVAL))
done
BASE="http://localhost:${APP_PORT}"
# --- Test 1: Root health ---
log "Test: root /health endpoint"
health_body=$(curl -sf "$BASE/health")
assert_ok "/health returns status=ok" \
'[ "$(echo "$health_body" | jq -r .status)" = "ok" ]'
# --- Test 2: Memory router health with config ---
log "Test: /memories/health endpoint"
mem_health=$(curl -sf "$BASE/memories/health")
assert_ok "/memories/health returns status=ok" \
'[ "$(echo "$mem_health" | jq -r .status)" = "ok" ]'
assert_ok "/memories/health includes embedding_provider" \
'[ "$(echo "$mem_health" | jq -r .config.embedding_provider)" != "null" ]'
assert_ok "/memories/health includes llm_provider" \
'[ "$(echo "$mem_health" | jq -r .config.llm_provider)" != "null" ]'
# --- Test 3: Provider reachability ---
# Extract configured providers and verify they are reachable from inside the container
log "Test: provider reachability from inside container"
embedding_provider=$(echo "$mem_health" | jq -r .config.embedding_provider)
llm_provider=$(echo "$mem_health" | jq -r .config.llm_provider)
if [[ "$embedding_provider" == "ollama" || "$llm_provider" == "ollama" ]]; then
# The exact bug we're catching: ollama on localhost inside Docker fails
# The container should use host.docker.internal, not localhost
ollama_url=$(docker compose -p "$COMPOSE_PROJECT" exec -T app \
sh -c 'echo $OLLAMA_BASE_URL' 2>/dev/null | tr -d '\r')
assert_ok "OLLAMA_BASE_URL does not point to localhost (would fail inside container)" \
'! echo "$ollama_url" | grep -q "localhost"'
# Try reaching ollama from inside the container
ollama_reachable=$(docker compose -p "$COMPOSE_PROJECT" exec -T app \
sh -c "curl -sf \${OLLAMA_BASE_URL}/api/tags >/dev/null 2>&1 && echo yes || echo no" \
2>/dev/null | tr -d '\r')
assert_ok "Ollama is reachable from inside the container" \
'[ "$ollama_reachable" = "yes" ]'
fi
# --- Test 4: Database connectivity (via stats endpoint) ---
log "Test: database connectivity"
stats_status=$(curl -sf -o /dev/null -w '%{http_code}' \
-G "$BASE/memories/stats" \
--data-urlencode "user_id=smoke-test-user")
assert_ok "GET /memories/stats returns 200 (DB connected)" \
'[ "$stats_status" = "200" ]'
# --- Test 5: Quick ingest endpoint (no LLM required — embedding-only dedup) ---
log "Test: quick ingest endpoint"
ingest_response=$(curl -sf -w '\n%{http_code}' \
-X POST "$BASE/memories/ingest/quick" \
-H "Content-Type: application/json" \
-d '{
"user_id": "smoke-test-user",
"conversation": "User: I am testing the Docker deployment. The project uses PostgreSQL and Next.js.",
"source_site": "docker-smoke-test"
}')
ingest_status=$(echo "$ingest_response" | tail -1)
ingest_body=$(echo "$ingest_response" | sed '$d')
assert_ok "POST /memories/ingest/quick returns 200" \
'[ "$ingest_status" = "200" ]'
assert_ok "Ingest stored at least 1 memory" \
'[ "$(echo "$ingest_body" | jq -r .memoriesStored)" -ge 1 ]'
# --- Test 6: Search endpoint ---
log "Test: search endpoint"
search_response=$(curl -sf -w '\n%{http_code}' \
-X POST "$BASE/memories/search" \
-H "Content-Type: application/json" \
-d '{
"user_id": "smoke-test-user",
"query": "What database is the project using?",
"source_site": "docker-smoke-test"
}')
search_status=$(echo "$search_response" | tail -1)
search_body=$(echo "$search_response" | sed '$d')
assert_ok "POST /memories/search returns 200" \
'[ "$search_status" = "200" ]'
assert_ok "Search returns at least 1 result" \
'[ "$(echo "$search_body" | jq -r .count)" -ge 1 ]'
# --- Test 7: Cleanup via reset-source ---
log "Test: reset-source cleanup"
reset_status=$(curl -sf -o /dev/null -w '%{http_code}' \
-X POST "$BASE/memories/reset-source" \
-H "Content-Type: application/json" \
-d '{"user_id":"smoke-test-user","source_site":"docker-smoke-test"}')
assert_ok "POST /memories/reset-source returns 200" \
'[ "$reset_status" = "200" ]'
# --- Test 8: Input validation ---
log "Test: input validation"
bad_ingest_status=$(curl -s -o /dev/null -w '%{http_code}' \
-X POST "$BASE/memories/ingest" \
-H "Content-Type: application/json" \
-d '{"user_id":"x"}')
assert_ok "Missing required fields returns 400" \
'[ "$bad_ingest_status" = "400" ]'
# --- Summary ---
echo ""
log "========================================="
if [[ $failed -eq 0 ]]; then
log " ALL PASSED: $passed/$total tests"
else
fail " FAILED: $failed/$total tests"
fi
log "========================================="
exit "$failed"