@@ -138,8 +138,8 @@ test_infrastructure_provisioning() {
138
138
fi
139
139
140
140
# Wait for VM to be fully ready (cloud-init completion and Docker availability)
141
- if ! wait_for_vm_ready ; then
142
- log_error " VM not ready - cannot proceed with application deployment "
141
+ if ! wait_for_cloud_init_to_finish ; then
142
+ log_error " VM not ready for application deployment - cloud-init failed or timed out "
143
143
return 1
144
144
fi
145
145
@@ -162,6 +162,12 @@ test_application_deployment() {
162
162
return 1
163
163
fi
164
164
165
+ # Wait for application services to be healthy
166
+ if ! wait_for_app_deployment_to_finish; then
167
+ log_error " Application services not healthy after deployment"
168
+ return 1
169
+ fi
170
+
165
171
local end_time
166
172
end_time=$( date +%s)
167
173
local duration=$(( end_time - start_time))
@@ -259,7 +265,7 @@ test_smoke_testing() {
259
265
# Test 2: Statistics API (through nginx proxy on port 80)
260
266
log_info " Testing statistics API through nginx proxy..."
261
267
local stats_response
262
- stats_response=$( curl -f -s " http://${vm_ip} :80/api/v1/stats?token=local-dev-admin-token-12345" 2> /dev/null || echo " " )
268
+ stats_response=$( curl -f -s " http://${vm_ip} :80/api/v1/stats?token=local-dev-admin-token-12345" 2> /dev/null || echo " " ) # DevSkim: ignore DS137138
263
269
if echo " ${stats_response} " | grep -q ' "torrents"' ; then
264
270
log_success " ✓ Statistics API working"
265
271
else
@@ -296,7 +302,7 @@ test_smoke_testing() {
296
302
# Test 5: HTTP tracker through nginx proxy (health check endpoint)
297
303
log_info " Testing HTTP tracker through nginx proxy..."
298
304
local proxy_response
299
- proxy_response=$( curl -s -w " %{http_code}" -o /dev/null " http://${vm_ip} :80/health_check" 2> /dev/null || echo " 000" )
305
+ proxy_response=$( curl -s -w " %{http_code}" -o /dev/null " http://${vm_ip} :80/health_check" 2> /dev/null || echo " 000" ) # DevSkim: ignore DS137138
300
306
if [[ " ${proxy_response} " =~ ^[23][0-9][0-9]$ ]]; then
301
307
log_success " ✓ Nginx proxy responding (HTTP ${proxy_response} )"
302
308
else
@@ -424,8 +430,8 @@ wait_for_vm_ip() {
424
430
}
425
431
426
432
# Wait for VM to be fully ready (cloud-init completion and Docker availability)
427
- wait_for_vm_ready () {
428
- log_info " Waiting for VM to be fully ready ( cloud-init + Docker) ..."
433
+ wait_for_cloud_init_to_finish () {
434
+ log_info " Waiting for cloud-init to finish ..."
429
435
local max_attempts=60 # 10 minutes total
430
436
local attempt=1
431
437
local vm_ip=" "
@@ -437,10 +443,10 @@ wait_for_vm_ready() {
437
443
return 1
438
444
fi
439
445
440
- log_info " VM IP: ${vm_ip} - checking cloud-init and Docker readiness..."
446
+ log_info " VM IP: ${vm_ip} - checking cloud-init readiness..."
441
447
442
448
while [[ ${attempt} -le ${max_attempts} ]]; do
443
- log_info " Checking VM readiness (attempt ${attempt} /${max_attempts} )..."
449
+ log_info " Checking cloud-init status (attempt ${attempt} /${max_attempts} )..."
444
450
445
451
# Check if SSH is available
446
452
if ! ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no torrust@" ${vm_ip} " " echo 'SSH OK'" > /dev/null 2>&1 ; then
@@ -460,7 +466,7 @@ wait_for_vm_ready() {
460
466
# Check if Docker is available and working
461
467
if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no torrust@" ${vm_ip} " " docker --version && docker compose version" > /dev/null 2>&1 ; then
462
468
log_success " Docker is ready and available"
463
- log_success " VM is fully ready for application deployment"
469
+ log_success " VM is ready for application deployment"
464
470
return 0
465
471
else
466
472
log_info " Docker not ready yet, waiting 10 seconds..."
@@ -476,10 +482,71 @@ wait_for_vm_ready() {
476
482
(( attempt++ ))
477
483
done
478
484
479
- log_error " Timeout waiting for VM to be ready after $(( max_attempts * 10 )) seconds"
480
- log_error " You can check manually with:"
481
- log_error " ssh torrust@${vm_ip} 'cloud-init status'"
482
- log_error " ssh torrust@${vm_ip} 'docker --version'"
485
+ log_error " Timeout waiting for cloud-init to finish after $(( max_attempts * 10 )) seconds"
486
+ log_error " You can check manually with: ssh torrust@${vm_ip} 'cloud-init status'"
487
+ return 1
488
+ }
489
+
490
+ # Wait for application deployment to finish (healthy containers)
491
+ wait_for_app_deployment_to_finish () {
492
+ log_info " Waiting for application services to become healthy..."
493
+ local max_attempts=15 # 2.5 minutes total
494
+ local attempt=1
495
+ local vm_ip=" "
496
+
497
+ # First get the VM IP
498
+ vm_ip=$( virsh domifaddr torrust-tracker-demo 2> /dev/null | grep ipv4 | awk ' {print $4}' | cut -d' /' -f1 || echo " " )
499
+ if [[ -z " ${vm_ip} " ]]; then
500
+ log_error " VM IP not available - cannot check application health"
501
+ return 1
502
+ fi
503
+
504
+ log_info " VM IP: ${vm_ip} - checking Docker container health..."
505
+
506
+ while [[ ${attempt} -le ${max_attempts} ]]; do
507
+ log_info " Checking container health (attempt ${attempt} /${max_attempts} )..."
508
+
509
+ local ps_output
510
+ if ! ps_output=$( ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no torrust@" ${vm_ip} " " cd /home/torrust/github/torrust/torrust-tracker-demo/application && docker compose ps --filter status=running" 2>&1 ) ; then
511
+ log_warning " Could not get container status via ssh. Retrying..."
512
+ sleep 10
513
+ (( attempt++ ))
514
+ continue
515
+ fi
516
+
517
+ log_info " Current container status:"
518
+ echo " ${ps_output} "
519
+
520
+ if echo " ${ps_output} " | grep -q ' (unhealthy)' ; then
521
+ log_info " Unhealthy containers found, waiting 10 seconds..."
522
+ log_info " Unhealthy details:"
523
+ echo " ${ps_output} " | grep ' (unhealthy)'
524
+ else
525
+ # No unhealthy containers, check if required ones are healthy
526
+ local healthy_count=0
527
+ if echo " ${ps_output} " | grep ' mysql' | grep -q ' (healthy)' ; then
528
+ (( healthy_count++ ))
529
+ fi
530
+ if echo " ${ps_output} " | grep ' tracker' | grep -q ' (healthy)' ; then
531
+ (( healthy_count++ ))
532
+ fi
533
+
534
+ if [[ ${healthy_count} -ge 2 ]]; then
535
+ log_success " All services with healthchecks (mysql, tracker) are healthy"
536
+ log_success " Application deployment finished successfully"
537
+ return 0
538
+ else
539
+ log_info " Waiting for mysql and tracker to become healthy (${healthy_count} /2)..."
540
+ fi
541
+ fi
542
+
543
+ sleep 10
544
+ (( attempt++ ))
545
+ done
546
+
547
+ log_error " Timeout waiting for application services to be healthy after $(( max_attempts * 10 )) seconds"
548
+ log_info " Final container status:"
549
+ ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no torrust@" ${vm_ip} " " cd /home/torrust/github/torrust/torrust-tracker-demo/application && docker compose ps" || true
483
550
return 1
484
551
}
485
552
0 commit comments