-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathtest_docker.sh
More file actions
executable file
·302 lines (265 loc) · 8.91 KB
/
test_docker.sh
File metadata and controls
executable file
·302 lines (265 loc) · 8.91 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
#!/bin/bash
#
# Docker Test Suite for BamBuddy
# Runs build verification, unit tests, and integration tests in Docker
#
set -e
# Configuration
PORT=${PORT:-8000}
# Enable BuildKit for better caching and parallel builds
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Track results
TESTS_PASSED=0
TESTS_FAILED=0
FAILED_TESTS=""
print_header() {
echo ""
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} $1${NC}"
echo -e "${BLUE}========================================${NC}"
}
print_success() {
echo -e "${GREEN}✓ $1${NC}"
TESTS_PASSED=$((TESTS_PASSED + 1))
}
print_failure() {
echo -e "${RED}✗ $1${NC}"
TESTS_FAILED=$((TESTS_FAILED + 1))
FAILED_TESTS="${FAILED_TESTS}\n - $1"
}
print_info() {
echo -e "${YELLOW}→ $1${NC}"
}
cleanup() {
print_info "Cleaning up test containers..."
sudo docker compose -f docker-compose.test.yml down -v --remove-orphans 2>/dev/null || true
sudo docker compose down -v --remove-orphans 2>/dev/null || true
}
# Cleanup on exit
trap cleanup EXIT
# Parse arguments
RUN_BUILD=true
RUN_BACKEND=true
RUN_FRONTEND=true
RUN_INTEGRATION=true
FRESH_BUILD=false
while [[ $# -gt 0 ]]; do
case $1 in
--build-only)
RUN_BACKEND=false
RUN_FRONTEND=false
RUN_INTEGRATION=false
shift
;;
--backend-only)
RUN_BUILD=false
RUN_FRONTEND=false
RUN_INTEGRATION=false
shift
;;
--frontend-only)
RUN_BUILD=false
RUN_BACKEND=false
RUN_INTEGRATION=false
shift
;;
--integration-only)
RUN_BUILD=false
RUN_BACKEND=false
RUN_FRONTEND=false
shift
;;
--skip-build)
RUN_BUILD=false
shift
;;
--skip-integration)
RUN_INTEGRATION=false
shift
;;
--fresh)
FRESH_BUILD=true
shift
;;
-h|--help)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --build-only Only run build test"
echo " --backend-only Only run backend tests"
echo " --frontend-only Only run frontend tests"
echo " --integration-only Only run integration tests"
echo " --skip-build Skip build test"
echo " --skip-integration Skip integration tests"
echo " --fresh Force fresh build (no cache)"
echo " -h, --help Show this help"
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Set cache flag based on --fresh option
CACHE_FLAG=""
if [ "$FRESH_BUILD" = true ]; then
CACHE_FLAG="--no-cache"
print_info "Fresh build enabled (--no-cache)"
fi
print_header "BamBuddy Docker Test Suite"
# ============================================
# Pre-build: Build all test images in parallel
# ============================================
print_header "Pre-building Docker Images"
# Determine which images to build
IMAGES_TO_BUILD=""
if [ "$RUN_BACKEND" = true ]; then
IMAGES_TO_BUILD="$IMAGES_TO_BUILD backend-test"
fi
if [ "$RUN_FRONTEND" = true ]; then
IMAGES_TO_BUILD="$IMAGES_TO_BUILD frontend-test"
fi
if [ "$RUN_INTEGRATION" = true ]; then
IMAGES_TO_BUILD="$IMAGES_TO_BUILD integration integration-test-runner"
fi
if [ -n "$IMAGES_TO_BUILD" ]; then
print_info "Building test images in parallel:$IMAGES_TO_BUILD"
if sudo docker compose -f docker-compose.test.yml build --parallel $CACHE_FLAG $IMAGES_TO_BUILD; then
print_success "Test images built successfully"
else
print_failure "Test image build failed"
exit 1
fi
fi
# ============================================
# Test 1: Docker Build (Production Image)
# ============================================
if [ "$RUN_BUILD" = true ]; then
print_header "Test 1: Docker Build (Production)"
print_info "Building production Docker image..."
if sudo docker build -t bambuddy:test . $CACHE_FLAG --progress=plain; then
print_success "Production image builds successfully"
# Verify image has expected labels/structure
print_info "Verifying image structure..."
if sudo docker run --rm bambuddy:test python -c "import backend.app.main; print('Backend imports OK')"; then
print_success "Backend module imports correctly"
else
print_failure "Backend module import failed"
fi
if sudo docker run --rm bambuddy:test test -d /app/static; then
print_success "Static files directory exists"
else
print_failure "Static files directory missing"
fi
else
print_failure "Production image build failed"
fi
fi
# ============================================
# Test 2: Backend Unit Tests
# ============================================
if [ "$RUN_BACKEND" = true ]; then
print_header "Test 2: Backend Unit Tests"
print_info "Running backend tests..."
if sudo docker compose -f docker-compose.test.yml run --rm backend-test; then
print_success "Backend unit tests passed"
else
print_failure "Backend unit tests failed"
fi
fi
# ============================================
# Test 3: Frontend Unit Tests
# ============================================
if [ "$RUN_FRONTEND" = true ]; then
print_header "Test 3: Frontend Unit Tests"
print_info "Running frontend tests..."
if sudo docker compose -f docker-compose.test.yml run --rm frontend-test; then
print_success "Frontend unit tests passed"
else
print_failure "Frontend unit tests failed"
fi
fi
# ============================================
# Test 4: Integration Tests
# ============================================
if [ "$RUN_INTEGRATION" = true ]; then
print_header "Test 4: Integration Tests"
print_info "Starting application container..."
# Start the integration container
sudo docker compose -f docker-compose.test.yml up --remove-orphans -d integration
# Wait for health check
print_info "Waiting for application to be healthy..."
RETRIES=30
while [ $RETRIES -gt 0 ]; do
if sudo docker compose -f docker-compose.test.yml ps integration | grep -q "healthy"; then
break
fi
sleep 2
((RETRIES--))
done
if [ $RETRIES -eq 0 ]; then
print_failure "Application failed to become healthy"
sudo docker compose -f docker-compose.test.yml logs integration
else
print_success "Application is healthy"
# Run basic health checks
print_info "Running integration tests..."
# Test health endpoint
HEALTH_RESPONSE=$(sudo docker compose -f docker-compose.test.yml exec -T integration curl -s http://localhost:${PORT}/health)
if echo "$HEALTH_RESPONSE" | grep -q "healthy"; then
print_success "Health endpoint responds correctly"
else
print_failure "Health endpoint check failed"
fi
# Test API endpoints
API_RESPONSE=$(sudo docker compose -f docker-compose.test.yml exec -T integration curl -s http://localhost:${PORT}/api/v1/settings)
if echo "$API_RESPONSE" | grep -q "settings"; then
print_success "Settings API endpoint responds"
else
# Settings might return empty, which is OK
print_success "Settings API endpoint accessible"
fi
# Test static files
STATIC_RESPONSE=$(sudo docker compose -f docker-compose.test.yml exec -T integration curl -s -o /dev/null -w "%{http_code}" http://localhost:${PORT}/)
if [ "$STATIC_RESPONSE" = "200" ]; then
print_success "Static files served correctly"
else
print_failure "Static files not served (HTTP $STATIC_RESPONSE)"
fi
# Run pytest integration tests if they exist
if sudo docker compose -f docker-compose.test.yml run --rm integration-test-runner 2>/dev/null; then
print_success "Integration test suite passed"
else
print_info "No Docker-specific integration tests found (this is OK)"
fi
fi
# Cleanup integration containers
sudo docker compose -f docker-compose.test.yml down -v
fi
# ============================================
# Summary
# ============================================
print_header "Test Summary"
echo ""
echo -e "Tests Passed: ${GREEN}${TESTS_PASSED}${NC}"
echo -e "Tests Failed: ${RED}${TESTS_FAILED}${NC}"
if [ $TESTS_FAILED -gt 0 ]; then
echo ""
echo -e "${RED}Failed tests:${NC}"
echo -e "$FAILED_TESTS"
echo ""
exit 1
else
echo ""
echo -e "${GREEN}All tests passed!${NC}"
echo ""
exit 0
fi