@@ -338,6 +338,16 @@ def gzip_then_base64_encode(s: str) -> str:
338
338
except Exception as e :
339
339
logger .warning (f"Error checking init.sh status: { str (e )} " )
340
340
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
+
341
351
attempt += 1
342
352
logger .warning (f"Waiting for init.sh to complete (attempt { attempt } /{ max_attempts } )" )
343
353
sleep (5 )
@@ -376,6 +386,18 @@ def is_healthy(ssh) -> bool:
376
386
("fail2ban" , "sudo fail2ban-client status" ),
377
387
]
378
388
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
+
379
401
service_status = {}
380
402
for service , command in health_checks :
381
403
try :
@@ -583,12 +605,12 @@ def test_pg_cron_extension(host):
583
605
ssh = host ['ssh' ]
584
606
585
607
# Check prestart script
586
- result = run_ssh_command (ssh , 'ls -l /etc/postgresql/prestart.d /postgres_prestart.sh' )
608
+ result = run_ssh_command (ssh , 'ls -l /usr/local/bin /postgres_prestart.sh' )
587
609
assert result ['succeeded' ], f"Failed to find prestart script: { result ['stderr' ]} "
588
610
logger .info (f"Prestart script details: { result ['stdout' ]} " )
589
611
590
612
# Check if extensions file exists
591
- result = run_ssh_command (ssh , 'cat /root/pg_extensions.json' )
613
+ result = run_ssh_command (ssh , 'sudo cat /root/pg_extensions.json' )
592
614
assert result ['succeeded' ], f"Failed to read extensions file: { result ['stderr' ]} "
593
615
logger .info (f"Extensions file contents: { result ['stdout' ]} " )
594
616
@@ -597,6 +619,71 @@ def test_pg_cron_extension(host):
597
619
assert result ['succeeded' ], f"Failed to find version switcher: { result ['stderr' ]} "
598
620
logger .info (f"Version switcher details: { result ['stdout' ]} " )
599
621
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/[email protected] ' )
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
+
600
687
# Create the extension
601
688
result = run_ssh_command (ssh , 'sudo -u postgres psql -d postgres -c "CREATE EXTENSION pg_cron WITH SCHEMA pg_catalog VERSION \' 1.3.1\' ;"' )
602
689
assert result ['succeeded' ], f"Failed to create pg_cron extension: { result ['stderr' ]} "
@@ -608,7 +695,7 @@ def test_pg_cron_extension(host):
608
695
logger .info (f"pg_cron version: { result ['stdout' ]} " )
609
696
610
697
# Check the actual function definition
611
- result = run_ssh_command (ssh , 'sudo -u postgres psql -d postgres -c "\sf cron.schedule"' )
698
+ result = run_ssh_command (ssh , 'sudo -u postgres psql -d postgres -c "\\ sf cron.schedule"' )
612
699
assert result ['succeeded' ], f"Failed to get cron.schedule function definition: { result ['stderr' ]} "
613
700
logger .info (f"cron.schedule function definition: { result ['stdout' ]} " )
614
701
@@ -622,12 +709,12 @@ def test_pg_cron_extension(host):
622
709
assert result ['succeeded' ], f"Failed to create test table: { result ['stderr' ]} "
623
710
624
711
# Check the schema of cron.job table
625
- result = run_ssh_command (ssh , 'sudo -u postgres psql -d postgres -c "\d cron.job"' )
712
+ result = run_ssh_command (ssh , 'sudo -u postgres psql -d postgres -c "\\ d cron.job"' )
626
713
assert result ['succeeded' ], f"Failed to get cron.job schema: { result ['stderr' ]} "
627
714
logger .info (f"cron.job schema: { result ['stdout' ]} " )
628
715
629
716
# Check available cron functions
630
- result = run_ssh_command (ssh , 'sudo -u postgres psql -d postgres -c "\df cron.*"' )
717
+ result = run_ssh_command (ssh , 'sudo -u postgres psql -d postgres -c "\\ df cron.*"' )
631
718
assert result ['succeeded' ], f"Failed to get cron functions: { result ['stderr' ]} "
632
719
logger .info (f"Available cron functions: { result ['stdout' ]} " )
633
720
0 commit comments