@@ -138,8 +138,8 @@ test_infrastructure_provisioning() {
138138 fi
139139
140140 # 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 "
143143 return 1
144144 fi
145145
@@ -162,6 +162,12 @@ test_application_deployment() {
162162 return 1
163163 fi
164164
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+
165171 local end_time
166172 end_time=$( date +%s)
167173 local duration=$(( end_time - start_time))
@@ -259,7 +265,7 @@ test_smoke_testing() {
259265 # Test 2: Statistics API (through nginx proxy on port 80)
260266 log_info " Testing statistics API through nginx proxy..."
261267 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
263269 if echo " ${stats_response} " | grep -q ' "torrents"' ; then
264270 log_success " ✓ Statistics API working"
265271 else
@@ -296,7 +302,7 @@ test_smoke_testing() {
296302 # Test 5: HTTP tracker through nginx proxy (health check endpoint)
297303 log_info " Testing HTTP tracker through nginx proxy..."
298304 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
300306 if [[ " ${proxy_response} " =~ ^[23][0-9][0-9]$ ]]; then
301307 log_success " ✓ Nginx proxy responding (HTTP ${proxy_response} )"
302308 else
@@ -424,8 +430,8 @@ wait_for_vm_ip() {
424430}
425431
426432# 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 ..."
429435 local max_attempts=60 # 10 minutes total
430436 local attempt=1
431437 local vm_ip=" "
@@ -437,10 +443,10 @@ wait_for_vm_ready() {
437443 return 1
438444 fi
439445
440- log_info " VM IP: ${vm_ip} - checking cloud-init and Docker readiness..."
446+ log_info " VM IP: ${vm_ip} - checking cloud-init readiness..."
441447
442448 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} )..."
444450
445451 # Check if SSH is available
446452 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() {
460466 # Check if Docker is available and working
461467 if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no torrust@" ${vm_ip} " " docker --version && docker compose version" > /dev/null 2>&1 ; then
462468 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"
464470 return 0
465471 else
466472 log_info " Docker not ready yet, waiting 10 seconds..."
@@ -476,10 +482,71 @@ wait_for_vm_ready() {
476482 (( attempt++ ))
477483 done
478484
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
483550 return 1
484551}
485552
0 commit comments