Skip to content

Commit 5ede9b7

Browse files
committed
test: pushing commit to register for nix build and testing
1 parent e801ff4 commit 5ede9b7

File tree

3 files changed

+126
-14
lines changed

3 files changed

+126
-14
lines changed

ansible/files/postgres_prestart.sh.j2

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

3+
set -e # Exit on error
4+
set -x # Print commands
5+
6+
log() {
7+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
8+
}
9+
310
check_orioledb_enabled() {
411
local pg_conf="/etc/postgresql/postgresql.conf"
512
if [ ! -f "$pg_conf" ]; then
@@ -29,7 +36,11 @@ update_orioledb_buffers() {
2936
check_extensions_file() {
3037
local extensions_file="/root/pg_extensions.json"
3138
if [ ! -f "$extensions_file" ]; then
32-
echo "extensions: No extensions file found, skipping extensions versions check"
39+
log "extensions: No extensions file found, skipping extensions versions check"
40+
return 1
41+
fi
42+
if [ ! -r "$extensions_file" ]; then
43+
log "extensions: Cannot read extensions file"
3344
return 1
3445
fi
3546
return 0
@@ -40,18 +51,20 @@ get_pg_cron_version() {
4051
return
4152
fi
4253

54+
# Run jq as postgres user since it's in their nix profile
4355
local version
4456
version=$(sudo -u postgres /var/lib/postgresql/.nix-profile/bin/jq -r '.pg_cron // empty' "/root/pg_extensions.json")
4557
if [ -z "$version" ]; then
46-
echo "pg_cron: Not specified in extensions file"
58+
log "pg_cron: Not specified in extensions file"
4759
return
4860
fi
4961

5062
if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
51-
echo "pg_cron: Invalid version format: $version"
63+
log "pg_cron: Invalid version format: $version"
5264
return
5365
fi
5466

67+
log "pg_cron: Found version $version in extensions file"
5568
echo "$version"
5669
}
5770

@@ -60,13 +73,20 @@ switch_pg_cron_version() {
6073
local switch_script="/var/lib/postgresql/.nix-profile/bin/switch_pg_cron_version"
6174

6275
if [ ! -x "$switch_script" ]; then
63-
echo "pg_cron: No version switch script available"
64-
return
76+
log "pg_cron: No version switch script available at $switch_script"
77+
return 1
6578
fi
6679

67-
echo "pg_cron: Switching to version $version"
80+
log "pg_cron: Switching to version $version"
81+
# Run as postgres user since we're modifying their nix profile
6882
sudo -u postgres "$switch_script" "$version"
69-
echo "pg_cron: Version switch completed"
83+
local exit_code=$?
84+
if [ $exit_code -eq 0 ]; then
85+
log "pg_cron: Version switch completed successfully"
86+
else
87+
log "pg_cron: Version switch failed with exit code $exit_code"
88+
fi
89+
return $exit_code
7090
}
7191

7292
handle_pg_cron_version() {
@@ -78,6 +98,8 @@ handle_pg_cron_version() {
7898
}
7999

80100
main() {
101+
log "Starting prestart script"
102+
81103
# 1. pg_cron version handling
82104
handle_pg_cron_version
83105

@@ -90,6 +112,8 @@ main() {
90112
if [ ! -z "$shared_buffers_value" ]; then
91113
update_orioledb_buffers "$shared_buffers_value"
92114
fi
115+
116+
log "Prestart script completed"
93117
}
94118

95119
# Initial locale setup

nix/ext/pg_cron.nix

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ stdenv.mkDerivation {
114114
fi
115115
116116
VERSION=$1
117-
LIB_DIR=$(dirname "$0")/../lib
118-
EXTENSION_DIR=$(dirname "$0")/../share/postgresql/extension
117+
NIX_PROFILE="/var/lib/postgresql/.nix-profile"
118+
LIB_DIR="$NIX_PROFILE/lib"
119+
EXTENSION_DIR="$NIX_PROFILE/share/postgresql/extension"
119120
120121
# Check if version exists
121122
if [ ! -f "$LIB_DIR/pg_cron-$VERSION${postgresql.dlSuffix}" ]; then

testinfra/test_ami_nix.py

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,16 @@ 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+
341351
attempt += 1
342352
logger.warning(f"Waiting for init.sh to complete (attempt {attempt}/{max_attempts})")
343353
sleep(5)
@@ -376,6 +386,18 @@ def is_healthy(ssh) -> bool:
376386
("fail2ban", "sudo fail2ban-client status"),
377387
]
378388

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+
379401
service_status = {}
380402
for service, command in health_checks:
381403
try:
@@ -583,12 +605,12 @@ def test_pg_cron_extension(host):
583605
ssh = host['ssh']
584606

585607
# 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')
587609
assert result['succeeded'], f"Failed to find prestart script: {result['stderr']}"
588610
logger.info(f"Prestart script details: {result['stdout']}")
589611

590612
# 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')
592614
assert result['succeeded'], f"Failed to read extensions file: {result['stderr']}"
593615
logger.info(f"Extensions file contents: {result['stdout']}")
594616

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

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+
600687
# Create the extension
601688
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "CREATE EXTENSION pg_cron WITH SCHEMA pg_catalog VERSION \'1.3.1\';"')
602689
assert result['succeeded'], f"Failed to create pg_cron extension: {result['stderr']}"
@@ -608,7 +695,7 @@ def test_pg_cron_extension(host):
608695
logger.info(f"pg_cron version: {result['stdout']}")
609696

610697
# 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"')
612699
assert result['succeeded'], f"Failed to get cron.schedule function definition: {result['stderr']}"
613700
logger.info(f"cron.schedule function definition: {result['stdout']}")
614701

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

624711
# 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"')
626713
assert result['succeeded'], f"Failed to get cron.job schema: {result['stderr']}"
627714
logger.info(f"cron.job schema: {result['stdout']}")
628715

629716
# 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.*"')
631718
assert result['succeeded'], f"Failed to get cron functions: {result['stderr']}"
632719
logger.info(f"Available cron functions: {result['stdout']}")
633720

0 commit comments

Comments
 (0)