-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-tests.sh
More file actions
executable file
·325 lines (274 loc) · 12.7 KB
/
run-tests.sh
File metadata and controls
executable file
·325 lines (274 loc) · 12.7 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/bash
# ─────────────────────────────────────────────────────────────────
# UnoSim Test & Build Pipeline (Stability & Resource Guard)
# ─────────────────────────────────────────────────────────────────
# Konfiguration
LOG_FILE="run-tests_output.log"
TOTAL_STEPS=7
STEP=0
SERVER_PID=""
# Policy: Standard Log-Level für die Pipeline ist ERROR (1)
export LOG_LEVEL=1
export NODE_ENV=test
# Docker-Konfiguration (überschreibbar per Umgebungsvariable)
# unix:// + absolute path = 3 slashes total; $HOME already starts with /
DOCKER_HOST="${DOCKER_HOST:-unix://${HOME}/.docker/run/docker.sock}"
DOCKER_SANDBOX_IMAGE="${DOCKER_SANDBOX_IMAGE:-unosim-sandbox:latest}"
# Temp-Verzeichnis unter /Users/… damit Docker Desktop es per default mounten kann
UNOSIM_SHARED_TEMP_DIR="${UNOSIM_SHARED_TEMP_DIR:-$(pwd)/temp}"
export DOCKER_HOST DOCKER_SANDBOX_IMAGE UNOSIM_SHARED_TEMP_DIR
# Farben & Icons
G="\033[32m"; Y="\033[33m"; R="\033[31m"; C="\033[36m"; B="\033[1m"; D="\033[2m"; RS="\033[0m"
OK="${G}✔${RS}"; FAIL="${R}✘${RS}"; RUN="${Y}◌${RS}"; WARN="${Y}⚠${RS}"
div() { printf "${D}────────────────────────────────────────────────${RS}\n"; }
# Helfer: Alle Sandbox-Container finden (Name- UND Kommando-basiert,
# damit auch namenlose Container mit alten Image-IDs erfasst werden).
find_sandbox_containers() {
local filter="${1:---filter status=exited}" # default: nur beendete
{
docker ps -aq $filter --filter "name=unosim-sandbox" 2>/dev/null
docker ps -a $filter --format '{{.ID}} {{.Command}}' 2>/dev/null \
| grep 'g++ /sandbox' | awk '{print $1}'
} | sort -u
}
# Aufräum-Funktion bei Abbruch oder Ende
cleanup() {
if [ -n "$SERVER_PID" ]; then
kill "$SERVER_PID" 2>/dev/null
fi
if docker info > /dev/null 2>&1; then
local containers
containers=$(find_sandbox_containers)
if [ -n "$containers" ]; then
echo "$containers" | xargs docker rm -f > /dev/null 2>&1 || true
fi
fi
}
trap cleanup EXIT
run_task() {
local label=$1 cmd=$2
STEP=$((STEP+1))
local start=$(date +%s)
echo -e "\n${B}▸ [$STEP/$TOTAL_STEPS] $label${RS}"
# Verzeichnisse sicherstellen
mkdir -p temp build
# Policy-Konforme Ausführung
(set -o pipefail; eval "$cmd" >> "$LOG_FILE" 2>&1) &
local pid=$!
while kill -0 "$pid" 2>/dev/null; do
printf "\r %b %-35s Sek.: %d" "$RUN" "$label" $(( $(date +%s) - start ))
sleep 1
done
wait "$pid"
local exit_code=$?
local duration=$(( $(date +%s) - start ))
if [ $exit_code -eq 0 ]; then
printf "\r %b %-35s Sek.: %d\n" "$OK" "$label" "$duration"
return 0
else
printf "\r %b %-35s ${R}FEHLER${RS} (Code: $exit_code)\n" "$FAIL" "$label"
echo -e " ${R}${FAIL} Abbruch: Siehe $LOG_FILE${RS}"
exit 1
fi
}
parse_test_results() {
local pattern=$1
# Prüfe die letzten 30 Zeilen auf Zusammenfassungen
local line=$(tail -n 30 "$LOG_FILE" | grep -E "$pattern" | tail -n 1)
[ -z "$line" ] && return
local p=$(echo "$line" | grep -oE "[0-9]+ passed" | head -n 1 | cut -d' ' -f1 || echo "0")
local f=$(echo "$line" | grep -oE "[0-9]+ failed" | head -n 1 | cut -d' ' -f1 || echo "0")
local s=$(echo "$line" | grep -oE "[0-9]+ skipped" | head -n 1 | cut -d' ' -f1 || echo "0")
local res=" "
[[ $p -gt 0 ]] && res+="${OK} Passed: ${G}$p${RS} "
[[ $f -gt 0 ]] && res+="${FAIL} Failed: ${R}$f${RS} "
[[ $s -gt 0 ]] && res+="${WARN} Skipped: ${Y}$s${RS}"
[[ -n $(echo $res | tr -d ' ') ]] && echo -e "$res"
}
# ─────────────────────────────────────────────────────────────────
# MAIN
# ─────────────────────────────────────────────────────────────────
clear
div
printf " ${B}UnoSim Test & Build Pipeline${RS} ${D}(Log: %s)${RS}\n" "$LOG_FILE"
div
rm -f "$LOG_FILE"
[ -d temp ] && rm -rf temp/*
# ─────── PRE-FLIGHT: Systemvoraussetzungen ───────
echo -e "\n${B}▸ [Pre-Flight] Systemvoraussetzungen${RS}"
# Node.js / npm
if ! command -v npm &>/dev/null; then
echo -e " ${FAIL} npm nicht gefunden – bitte Node.js installieren"
exit 1
fi
echo -e " ${OK} Node.js $(node -v)"
# Docker
DOCKER_AVAILABLE=0
if command -v docker &>/dev/null && docker info >/dev/null 2>&1; then
DOCKER_AVAILABLE=1
echo -e " ${OK} Docker $(docker version --format '{{.Client.Version}}' 2>/dev/null)"
# docker-compose unosim-server prüfen (Port-3000-Konflikt mit dem E2E-Dev-Server)
if docker ps --format '{{.Names}}' 2>/dev/null | grep -q "^unosim-server$"; then
echo -e " ${WARN} docker-compose unosim-server blockiert Port 3000 – wird für Tests gestoppt"
docker stop unosim-server >/dev/null 2>&1 || true
fi
# Stale Sandbox-Container aufräumen (Name + Kommando-basiert → fängt auch alte Image-IDs)
stale=$(find_sandbox_containers)
if [ -n "$stale" ]; then
count=$(echo "$stale" | wc -l | tr -d ' ')
echo "$stale" | xargs docker rm -f >/dev/null 2>&1
echo -e " ${OK} $count alte Sandbox-Container bereinigt"
fi
# Sandbox Image
if docker image inspect "$DOCKER_SANDBOX_IMAGE" >/dev/null 2>&1; then
echo -e " ${OK} Sandbox Image vorhanden"
else
echo -e " ${WARN} Sandbox Image fehlt – wird bei Bedarf gebaut"
fi
else
echo -e " ${WARN} Docker nicht verfügbar – Docker-Tests und Sandbox werden übersprungen"
fi
# Port 3000 freigeben (nach Docker-Stop, damit kein docker-proxy mehr übrig ist)
if lsof -ti:3000 >/dev/null 2>&1; then
lsof -ti:3000 | xargs kill -9 2>/dev/null || true
sleep 1
echo -e " ${OK} Port 3000 freigegeben"
else
echo -e " ${OK} Port 3000 frei"
fi
# SonarQube (optional, informativ)
if [ -n "$SONAR_TOKEN" ] && curl -sf http://localhost:9000/api/system/status >/dev/null 2>&1; then
echo -e " ${OK} SonarQube erreichbar"
else
echo -e " ${D}ℹ SonarQube nicht verfügbar (optional)${RS}"
fi
# ─────── PRE-FLIGHT: Compiler-Prozess-Leaks ───────
echo -e "\n${B}▸ [Pre-Flight] Cleanup leaked compiler processes${RS}"
./check-leaks.sh --cleanup >> "$LOG_FILE" 2>&1 && echo -e " ${OK} Bereinigung abgeschlossen" || true
# 1. Statische Analyse
run_task "Statische Analyse" "npm run check"
# 2. Unit-Tests
run_task "Unit-Tests" "NODE_OPTIONS='--no-warnings' npm run test:fast -- --reporter=default --maxConcurrency=2"
parse_test_results "Tests.*passed"
# 3+4. Sandbox Image Build & Docker-Tests (optional, wenn Docker verfügbar)
if [ "$DOCKER_AVAILABLE" -eq 1 ]; then
# Sandbox Image nur bauen wenn es noch nicht existiert
if ! docker image inspect "$DOCKER_SANDBOX_IMAGE" > /dev/null 2>&1; then
run_task "Sandbox Image Build" "docker build -f Dockerfile.sandbox -t $DOCKER_SANDBOX_IMAGE ."
else
STEP=$((STEP+1))
echo -e "\n${B}▸ [$STEP/$TOTAL_STEPS] Sandbox Image Build${RS}"
echo -e " ${OK} Sandbox Image bereits vorhanden – wird übersprungen"
fi
run_task "Docker-Tests (Timing/Pause/Sandbox/Flow)" \
"FORCE_DOCKER=1 DOCKER_SANDBOX_IMAGE=$DOCKER_SANDBOX_IMAGE SKIP_HEAVY_TESTS=false LOG_LEVEL=warn \
npx vitest run --reporter=default --maxWorkers=1 \
tests/server/timing-delay.test.ts \
tests/server/pause-resume-timing.test.ts \
tests/server/pause-resume-digitalread.test.ts \
tests/integration/serial-flooding.test.ts \
tests/integration/serial-flow.test.ts \
tests/server/services/sandbox-lifecycle.integration.test.ts \
tests/server/services/serial-backpressure.test.ts"
parse_test_results "Tests.*passed"
# Container-Cleanup nach Docker-Tests: entlastet Docker Desktop vor E2E-Phase
stale_after=$(find_sandbox_containers)
if [ -n "$stale_after" ]; then
echo "$stale_after" | xargs docker rm -f >/dev/null 2>&1
fi
else
echo -e " ${WARN} Docker nicht verfügbar – Docker-Tests werden übersprungen (Steps 3+4)"
STEP=$((STEP+2))
fi
# --- VORBEREITUNG SERVER (Kein nummerierter Task) ---
echo -e "\n${B}▸ [Vorbereitung] Server-Start${RS}"
lsof -ti:3000 | xargs kill -9 2>/dev/null || true
# Docker-Gesundheitsprüfung: Docker Desktop auf macOS braucht nach Heavy-Load
# manchmal einige Sekunden bis der Daemon wieder stabil antwortet.
DOCKER_FOR_E2E=0
if [ "$DOCKER_AVAILABLE" -eq 1 ]; then
for _i in {1..10}; do
if docker info > /dev/null 2>&1; then
DOCKER_FOR_E2E=1
[ "$_i" -gt 1 ] && echo -e " ${OK} Docker nach $((_i * 3))s wiederhergestellt"
break
fi
[ "$_i" -eq 1 ] && echo -e " ${RUN} Docker antwortet nicht – warte auf Erholung (max. 30s)..."
sleep 3
done
fi
export PORT=3000
# Server startet im Hintergrund (NODE_ENV=development für Vite-Snapshots)
if [ "$DOCKER_FOR_E2E" -eq 1 ]; then
echo -e " ${OK} Docker verfügbar – E2E mit Sandbox-Unterstützung"
export FORCE_DOCKER=1
DOCKER_SANDBOX_IMAGE=$DOCKER_SANDBOX_IMAGE UNOSIM_SHARED_TEMP_DIR=$UNOSIM_SHARED_TEMP_DIR NODE_ENV=development npm run dev >> "$LOG_FILE" 2>&1 &
else
echo -e " ${WARN} Docker nicht verfügbar – E2E im Lokal-Modus"
NODE_ENV=development npm run dev >> "$LOG_FILE" 2>&1 &
fi
SERVER_PID=$!
for i in {1..15}; do
if curl -s http://localhost:3000 > /dev/null; then
echo -e " ${G}${OK} Server bereit (PID $SERVER_PID)${RS}"
break
fi
[ $i -eq 15 ] && echo -e " ${R}${FAIL} Server-Start Timeout!${RS}" && exit 1
sleep 1
done
# 3. E2E-Tests (Playwright)
run_task "E2E-Tests (Playwright)" "npx playwright test --timeout 60000"
parse_test_results "([0-9]+ passed|[0-9]+ failed|[0-9]+ skipped)"
# 4. Post-Test Integrity Check (Leak-Detection nach allen Tests)
run_task "Post-Test Integrity Check" "./check-leaks.sh --cleanup"
# Server stoppen bevor der Build startet
cleanup
SERVER_PID=""
# 5. Produktions-Build
run_task "Produktions-Build" "npm run build"
# 6. SonarQube Quality Gate Check
if [ -n "$SONAR_TOKEN" ] && curl -sf http://localhost:9000/api/system/status > /dev/null 2>&1; then
STEP=$((STEP+1))
echo -e "\n${B}▸ [$STEP/$TOTAL_STEPS] SonarQube Quality Gate${RS}"
SQ_PROJECT_KEY="unosim"
SQ_URL="http://localhost:9000"
# Fetch quality gate status
QG_JSON=$(curl -sf -H "Authorization: Bearer $SONAR_TOKEN" \
"${SQ_URL}/api/qualitygates/project_status?projectKey=${SQ_PROJECT_KEY}" 2>/dev/null)
if [ -n "$QG_JSON" ]; then
QG_STATUS=$(echo "$QG_JSON" | python3 -c "import sys,json; print(json.load(sys.stdin)['projectStatus']['status'])" 2>/dev/null)
echo -e " Quality Gate: $([ "$QG_STATUS" = "OK" ] && echo "${G}${OK} PASSED${RS}" || echo "${R}${FAIL} $QG_STATUS${RS}")"
# Display individual conditions
echo "$QG_JSON" | python3 -c "
import sys, json
d = json.load(sys.stdin)
for c in d['projectStatus']['conditions']:
status = c['status']
metric = c['metricKey'].replace('new_', '').replace('_', ' ').title()
actual = c['actualValue']
threshold = c['errorThreshold']
comp = c['comparator']
icon = '✔' if status == 'OK' else '✘'
unit = '%' if 'density' in c['metricKey'] or 'coverage' in c['metricKey'] or 'reviewed' in c['metricKey'] else ''
print(f' {icon} {metric}: {actual}{unit} (Threshold: {comp} {threshold}{unit})')
" 2>/dev/null
# Fetch open issues count
ISSUES_JSON=$(curl -sf -H "Authorization: Bearer $SONAR_TOKEN" \
"${SQ_URL}/api/issues/search?componentKeys=${SQ_PROJECT_KEY}&statuses=OPEN&ps=1" 2>/dev/null)
if [ -n "$ISSUES_JSON" ]; then
ISSUE_COUNT=$(echo "$ISSUES_JSON" | python3 -c "import sys,json; print(json.load(sys.stdin)['paging']['total'])" 2>/dev/null)
echo -e " Open Issues: ${ISSUE_COUNT:-?}"
fi
# Quality gate status is informational, not blocking
echo -e " ${D}(informational — does not block pipeline)${RS}"
else
echo -e " ${WARN} Could not fetch quality gate status"
fi
else
STEP=$((STEP+1))
echo -e "\n ${WARN} SonarQube nicht verfügbar – Quality Gate Check übersprungen (Step $STEP)"
fi
echo
div
printf " ${G}${B}${OK} Pipeline erfolgreich abgeschlossen${RS}\n"
div