Skip to content

Commit c892c00

Browse files
committed
test: rollback logging on test
1 parent 44ef456 commit c892c00

File tree

3 files changed

+10
-94
lines changed

3 files changed

+10
-94
lines changed

ansible/files/postgres_prestart.sh.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/bin/bash
22

3-
set -e # Exit on error
43
set -x # Print commands
54

65
log() {
@@ -92,7 +91,8 @@ handle_pg_cron_version() {
9291
local version
9392
version=$(get_pg_cron_version)
9493
if [ -n "$version" ]; then
95-
switch_pg_cron_version "$version"
94+
# Don't fail if version switch fails
95+
switch_pg_cron_version "$version" || log "pg_cron: Version switch failed but continuing"
9696
fi
9797
}
9898

ansible/files/postgresql_config/postgresql.service.j2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ TimeoutStopSec=90
1818
TimeoutStartSec=86400
1919
Restart=always
2020
RestartSec=5
21+
StandardOutput=journal
22+
StandardError=journal
2123
OOMScoreAdjust=-1000
2224
EnvironmentFile=-/etc/environment.d/postgresql.env
2325
LimitNOFILE=16384

testinfra/test_ami_nix.py

Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,6 @@ def gzip_then_base64_encode(s: str) -> str:
338338
except Exception as e:
339339
logger.warning(f"Error checking init.sh status: {str(e)}")
340340

341-
# Capture logs during initialization
342-
result = run_ssh_command(ssh, 'sudo journalctl -u postgresql@main -n 100 --no-pager')
343-
logger.info(f"PostgreSQL service logs during initialization:\n{result['stdout']}")
344-
345-
result = run_ssh_command(ssh, 'sudo journalctl -u postgresql@main -n 100 | grep -i "prestart"')
346-
logger.info(f"Prestart script execution logs during initialization:\n{result['stdout']}")
347-
348-
result = run_ssh_command(ssh, 'sudo journalctl -u postgresql@main -n 100 | grep -i "switch_pg_cron_version"')
349-
logger.info(f"Version switcher execution logs during initialization:\n{result['stdout']}")
350-
351341
attempt += 1
352342
logger.warning(f"Waiting for init.sh to complete (attempt {attempt}/{max_attempts})")
353343
sleep(5)
@@ -386,18 +376,6 @@ def is_healthy(ssh) -> bool:
386376
("fail2ban", "sudo fail2ban-client status"),
387377
]
388378

389-
# Capture systemd logs during startup
390-
result = run_ssh_command(ssh, 'sudo journalctl -u postgresql@main -n 100 --no-pager')
391-
logger.info(f"PostgreSQL service logs during startup:\n{result['stdout']}")
392-
393-
# Check prestart script execution
394-
result = run_ssh_command(ssh, 'sudo journalctl -u postgresql@main -n 100 | grep -i "prestart"')
395-
logger.info(f"Prestart script execution logs:\n{result['stdout']}")
396-
397-
# Check version switcher execution
398-
result = run_ssh_command(ssh, 'sudo journalctl -u postgresql@main -n 100 | grep -i "switch_pg_cron_version"')
399-
logger.info(f"Version switcher execution logs:\n{result['stdout']}")
400-
401379
service_status = {}
402380
for service, command in health_checks:
403381
try:
@@ -605,12 +583,12 @@ def test_pg_cron_extension(host):
605583
ssh = host['ssh']
606584

607585
# Check prestart script
608-
result = run_ssh_command(ssh, 'ls -l /usr/local/bin/postgres_prestart.sh')
586+
result = run_ssh_command(ssh, 'ls -l /etc/postgresql/prestart.d/postgres_prestart.sh')
609587
assert result['succeeded'], f"Failed to find prestart script: {result['stderr']}"
610588
logger.info(f"Prestart script details: {result['stdout']}")
611589

612590
# Check if extensions file exists
613-
result = run_ssh_command(ssh, 'sudo cat /root/pg_extensions.json')
591+
result = run_ssh_command(ssh, 'cat /root/pg_extensions.json')
614592
assert result['succeeded'], f"Failed to read extensions file: {result['stderr']}"
615593
logger.info(f"Extensions file contents: {result['stdout']}")
616594

@@ -619,71 +597,6 @@ def test_pg_cron_extension(host):
619597
assert result['succeeded'], f"Failed to find version switcher: {result['stderr']}"
620598
logger.info(f"Version switcher details: {result['stdout']}")
621599

622-
# Check if version switching worked correctly
623-
result = run_ssh_command(ssh, 'ls -l /var/lib/postgresql/.nix-profile/lib/pg_cron.so')
624-
logger.info(f"Current pg_cron library symlink: {result['stdout']}")
625-
assert "pg_cron-1.3.1" in result['stdout'], "pg_cron library not pointing to version 1.3.1"
626-
627-
# Check the actual symlink target
628-
result = run_ssh_command(ssh, 'readlink -f /var/lib/postgresql/.nix-profile/lib/pg_cron.so')
629-
logger.info(f"Actual pg_cron library symlink target: {result['stdout']}")
630-
631-
# List all available pg_cron versions
632-
result = run_ssh_command(ssh, 'ls -l /var/lib/postgresql/.nix-profile/lib/pg_cron-*')
633-
logger.info(f"Available pg_cron versions: {result['stdout']}")
634-
635-
# Check if the target version exists
636-
result = run_ssh_command(ssh, 'ls -l /var/lib/postgresql/.nix-profile/lib/pg_cron-1.3.1.so')
637-
logger.info(f"Target version exists: {result['stdout']}")
638-
639-
result = run_ssh_command(ssh, 'cat /var/lib/postgresql/.nix-profile/share/postgresql/extension/pg_cron.control')
640-
logger.info(f"pg_cron control file contents: {result['stdout']}")
641-
assert "default_version = '1.3.1'" in result['stdout'], "pg_cron control file not set to version 1.3.1"
642-
643-
# Check prestart script execution
644-
result = run_ssh_command(ssh, 'sudo journalctl -u postgresql@main -n 50 | grep -i "prestart"')
645-
logger.info(f"Prestart script execution logs: {result['stdout']}")
646-
647-
# Check systemd service status to see if prestart was attempted
648-
result = run_ssh_command(ssh, 'sudo systemctl status postgresql@main')
649-
logger.info(f"PostgreSQL service status: {result['stdout']}")
650-
651-
# Check if prestart script exists and is executable
652-
result = run_ssh_command(ssh, 'ls -l /usr/local/bin/postgres_prestart.sh')
653-
logger.info(f"Prestart script permissions: {result['stdout']}")
654-
655-
# Check prestart script contents
656-
result = run_ssh_command(ssh, 'sudo cat /usr/local/bin/postgres_prestart.sh')
657-
logger.info(f"Prestart script contents: {result['stdout']}")
658-
659-
# Check pg_cron worker process
660-
result = run_ssh_command(ssh, 'ps aux | grep -i "pg_cron"')
661-
logger.info(f"pg_cron worker processes: {result['stdout']}")
662-
663-
# Check PostgreSQL logs for any errors
664-
result = run_ssh_command(ssh, 'sudo tail -n 100 /var/log/postgresql/postgresql.csv')
665-
logger.info(f"PostgreSQL logs: {result['stdout']}")
666-
667-
# Check if version switcher was executed
668-
result = run_ssh_command(ssh, 'sudo ls -l /var/lib/postgresql/.nix-profile/lib/pg_cron-*')
669-
logger.info(f"Available pg_cron versions: {result['stdout']}")
670-
671-
# Check if version switcher has execute permissions
672-
result = run_ssh_command(ssh, 'ls -l /var/lib/postgresql/.nix-profile/bin/switch_pg_cron_version')
673-
logger.info(f"Version switcher permissions: {result['stdout']}")
674-
675-
# Check if version switcher was called by prestart
676-
result = run_ssh_command(ssh, 'sudo grep -r "switch_pg_cron_version" /var/log/postgresql/')
677-
logger.info(f"Version switcher execution logs: {result['stdout']}")
678-
679-
# Check systemd service file to verify prestart configuration
680-
result = run_ssh_command(ssh, 'sudo cat /etc/systemd/system/postgresql@.service')
681-
logger.info(f"PostgreSQL service file: {result['stdout']}")
682-
683-
# Check if prestart script was called by systemd
684-
result = run_ssh_command(ssh, 'sudo journalctl -u postgresql@main -n 100 | grep -i "executing"')
685-
logger.info(f"Systemd execution logs: {result['stdout']}")
686-
687600
# Create the extension
688601
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "CREATE EXTENSION pg_cron WITH SCHEMA pg_catalog VERSION \'1.3.1\';"')
689602
assert result['succeeded'], f"Failed to create pg_cron extension: {result['stderr']}"
@@ -695,7 +608,7 @@ def test_pg_cron_extension(host):
695608
logger.info(f"pg_cron version: {result['stdout']}")
696609

697610
# Check the actual function definition
698-
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "\\sf cron.schedule"')
611+
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "\sf cron.schedule"')
699612
assert result['succeeded'], f"Failed to get cron.schedule function definition: {result['stderr']}"
700613
logger.info(f"cron.schedule function definition: {result['stdout']}")
701614

@@ -709,12 +622,12 @@ def test_pg_cron_extension(host):
709622
assert result['succeeded'], f"Failed to create test table: {result['stderr']}"
710623

711624
# Check the schema of cron.job table
712-
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "\\d cron.job"')
625+
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "\d cron.job"')
713626
assert result['succeeded'], f"Failed to get cron.job schema: {result['stderr']}"
714627
logger.info(f"cron.job schema: {result['stdout']}")
715628

716629
# Check available cron functions
717-
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "\\df cron.*"')
630+
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "\df cron.*"')
718631
assert result['succeeded'], f"Failed to get cron functions: {result['stderr']}"
719632
logger.info(f"Available cron functions: {result['stdout']}")
720633

@@ -730,3 +643,4 @@ def test_pg_cron_extension(host):
730643
assert "INSERT INTO cron_test_log" in result['stdout'], "Expected cron command"
731644
assert "postgres" in result['stdout'], "Expected postgres username"
732645
assert "postgres" in result['stdout'], "Expected postgres database"
646+

0 commit comments

Comments
 (0)