Skip to content

Commit 96702ad

Browse files
committed
test: restructure checks to avoid race
1 parent f32610f commit 96702ad

File tree

1 file changed

+23
-44
lines changed

1 file changed

+23
-44
lines changed

testinfra/test_ami_nix.py

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -346,31 +346,7 @@ def gzip_then_base64_encode(s: str) -> str:
346346
instance.terminate()
347347
raise TimeoutError("init.sh failed to complete within the timeout period")
348348

349-
<<<<<<< HEAD
350349
def is_healthy(ssh) -> bool:
351-
=======
352-
# Check PostgreSQL logs directory
353-
logger.info("Checking PostgreSQL logs directory:")
354-
result = host.run("sudo ls -la /var/log/postgresql/")
355-
logger.info(f"log directory contents:\n{result.stdout}\n{result.stderr}")
356-
357-
# Check any existing PostgreSQL logs
358-
logger.info("Checking existing PostgreSQL logs:")
359-
result = host.run("sudo cat /var/log/postgresql/*.log")
360-
logger.info(f"postgresql logs:\n{result.stdout}\n{result.stderr}")
361-
362-
# Check the startup log
363-
logger.info("PostgreSQL startup log:")
364-
result = host.run(f"sudo cat {startup_log}")
365-
logger.info(f"startup log contents:\n{result.stdout}\n{result.stderr}")
366-
367-
# Check PostgreSQL environment
368-
logger.info("PostgreSQL environment:")
369-
result = host.run("sudo -u postgres env | grep POSTGRES")
370-
logger.info(f"postgres environment:\n{result.stdout}\n{result.stderr}")
371-
372-
def is_healthy(host, instance_ip, ssh_identity_file) -> bool:
373-
>>>>>>> c2631e8c (test: reorg and print logs while waiting continue on other checks when ready)
374350
health_checks = [
375351
("postgres", "sudo -u postgres /usr/bin/pg_isready -U postgres"),
376352
("adminapi", f"curl -sf -k --connect-timeout 30 --max-time 60 https://localhost:8085/health -H 'apikey: {supabase_admin_key}'"),
@@ -382,22 +358,16 @@ def is_healthy(host, instance_ip, ssh_identity_file) -> bool:
382358

383359
for service, command in health_checks:
384360
try:
385-
<<<<<<< HEAD
386361
result = run_ssh_command(ssh, command)
387362
if not result['succeeded']:
388363
logger.warning(f"{service} not ready")
389364
logger.error(f"{service} command failed with rc={cmd.rc}")
390365
logger.error(f"{service} stdout: {cmd.stdout}")
391366
logger.error(f"{service} stderr: {cmd.stderr}")
392-
=======
393-
if service == "postgres":
394-
pg_isready = check(host)
395-
>>>>>>> 65ef0692 (test: do not unpack result)
396367

397-
# Always read and log the PostgreSQL logs first
368+
# Always read and log the PostgreSQL logs
398369
logger.warning("PostgreSQL status check:")
399370
try:
400-
# Read both .log and .csv files
401371
log_files = [
402372
"/var/log/postgresql/*.log",
403373
"/var/log/postgresql/*.csv"
@@ -415,33 +385,42 @@ def is_healthy(host, instance_ip, ssh_identity_file) -> bool:
415385
except Exception as e:
416386
logger.error(f"Error reading PostgreSQL logs: {str(e)}")
417387

418-
# Then check the status and return
419-
if not pg_isready.failed:
420-
continue
421-
# Wait before next attempt
422-
sleep(5)
423-
return False
388+
service_status[service] = not pg_isready.failed
389+
424390
else:
425391
cmd = check(host)
426-
if cmd.failed is True:
392+
service_status[service] = not cmd.failed
393+
if cmd.failed:
427394
logger.warning(f"{service} not ready")
428395
logger.error(f"{service} command failed with rc={cmd.rc}")
429396
logger.error(f"{service} stdout: {cmd.stdout}")
430397
logger.error(f"{service} stderr: {cmd.stderr}")
431-
return False
398+
432399
except Exception as e:
433-
logger.warning(
434-
f"Connection failed during {service} check, attempting reconnect..."
435-
)
400+
logger.warning(f"Connection failed during {service} check, attempting reconnect...")
436401
logger.error(f"Error details: {str(e)}")
437402
host = get_ssh_connection(instance_ip, ssh_identity_file)
438-
return False
403+
service_status[service] = False
404+
405+
# Log overall status of all services
406+
logger.info("Service health status:")
407+
for service, healthy in service_status.items():
408+
logger.info(f"{service}: {'healthy' if healthy else 'unhealthy'}")
409+
410+
# If any service is unhealthy, wait and return False with status
411+
if not all(service_status.values()):
412+
if service_status.get("postgres", False): # If postgres is healthy but others aren't
413+
sleep(5) # Only wait if postgres is up but other services aren't
414+
logger.warning("Some services are not healthy, will retry...")
415+
return False, service_status
439416

440-
return True
417+
logger.info("All services are healthy, proceeding to tests...")
418+
return True, service_status
441419

442420
while True:
443421
if is_healthy(ssh):
444422
break
423+
logger.warning(f"Health check failed, service status: {status}")
445424
sleep(1)
446425

447426
# Return both the SSH connection and instance IP for use in tests

0 commit comments

Comments
 (0)