Skip to content

Commit c2631e8

Browse files
committed
test: reorg and print logs while waiting continue on other checks when ready
1 parent 65ef069 commit c2631e8

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

testinfra/test_ami_nix.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,6 @@ def run_detailed_checks(host):
313313
result = host.run("sudo cat /var/log/postgresql/*.log")
314314
logger.info(f"postgresql logs:\n{result.stdout}\n{result.stderr}")
315315

316-
# Try starting PostgreSQL directly with pg_ctl and capture output
317-
logger.info("Attempting to start PostgreSQL directly with pg_ctl:")
318-
startup_log = "/tmp/postgres-start.log"
319-
result = host.run(f"sudo -u postgres /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data start -l {startup_log}")
320-
logger.info(f"pg_ctl start attempt:\n{result.stdout}\n{result.stderr}")
321-
322316
# Check the startup log
323317
logger.info("PostgreSQL startup log:")
324318
result = host.run(f"sudo cat {startup_log}")
@@ -362,17 +356,35 @@ def is_healthy(host, instance_ip, ssh_identity_file) -> bool:
362356
for service, check in health_checks:
363357
try:
364358
if service == "postgres":
365-
# For PostgreSQL, we need to check multiple things
366359
pg_isready = check(host)
367360

368-
if pg_isready.failed:
369-
logger.error("PostgreSQL is not ready")
370-
logger.error(f"pg_isready stdout: {pg_isready.stdout}")
371-
logger.error(f"pg_isready stderr: {pg_isready.stderr}")
361+
# Always read and log the PostgreSQL logs first
362+
logger.warning("PostgreSQL status check:")
363+
try:
364+
# Read both .log and .csv files
365+
log_files = [
366+
"/var/log/postgresql/*.log",
367+
"/var/log/postgresql/*.csv"
368+
]
372369

373-
# Run detailed checks since we know we have a working connection
374-
run_detailed_checks(host)
375-
return False
370+
for log_pattern in log_files:
371+
log_result = host.run(f"sudo cat {log_pattern}")
372+
if not log_result.failed:
373+
logger.error(f"PostgreSQL logs from {log_pattern}:")
374+
logger.error(log_result.stdout)
375+
if log_result.stderr:
376+
logger.error(f"Log read errors: {log_result.stderr}")
377+
else:
378+
logger.error(f"Failed to read PostgreSQL logs from {log_pattern}: {log_result.stderr}")
379+
except Exception as e:
380+
logger.error(f"Error reading PostgreSQL logs: {str(e)}")
381+
382+
# Then check the status and return
383+
if not pg_isready.failed:
384+
continue
385+
# Wait before next attempt
386+
sleep(5)
387+
return False
376388
else:
377389
cmd = check(host)
378390
if cmd.failed is True:

0 commit comments

Comments
 (0)