Skip to content

Commit e801ff4

Browse files
committed
test: new flag and other fixes to testing
1 parent e070474 commit e801ff4

File tree

3 files changed

+78
-22
lines changed

3 files changed

+78
-22
lines changed

flake.nix

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@
772772
773773
show_help() {
774774
cat << EOF
775-
Usage: run-testinfra --ami-name NAME [--aws-vault-profile PROFILE]
775+
Usage: run-testinfra --ami-name NAME [--aws-vault-profile PROFILE] [--postgres-version VERSION]
776776
777777
Run the testinfra tests locally against a specific AMI.
778778
@@ -789,6 +789,7 @@
789789
790790
Optional flags:
791791
--aws-vault-profile PROFILE AWS Vault profile to use (default: staging)
792+
--postgres-version VERSION PostgreSQL major version to test (default: 15)
792793
--help Show this help message and exit
793794
794795
Requirements:
@@ -799,12 +800,14 @@
799800
Examples:
800801
run-testinfra --ami-name supabase-postgres-abc123
801802
run-testinfra --ami-name supabase-postgres-abc123 --aws-vault-profile production
803+
run-testinfra --ami-name supabase-postgres-abc123 --postgres-version 15
802804
EOF
803805
}
804806
805807
# Default values
806808
AWS_VAULT_PROFILE="staging"
807809
AMI_NAME=""
810+
POSTGRES_MAJOR_VERSION="15"
808811
809812
# Parse arguments
810813
while [[ $# -gt 0 ]]; do
@@ -817,6 +820,10 @@
817820
AMI_NAME="$2"
818821
shift 2
819822
;;
823+
--postgres-version)
824+
POSTGRES_MAJOR_VERSION="$2"
825+
shift 2
826+
;;
820827
--help)
821828
show_help
822829
exit 0
@@ -847,6 +854,7 @@
847854
export AWS_DEFAULT_REGION="ap-southeast-1"
848855
export AMI_NAME="$AMI_NAME" # Export AMI_NAME for pytest
849856
export RUN_ID="local-$(date +%s)" # Generate a unique RUN_ID
857+
export POSTGRES_MAJOR_VERSION="$POSTGRES_MAJOR_VERSION" # Export PostgreSQL version for pytest
850858
851859
# Function to terminate EC2 instances
852860
terminate_instances() {

nix/ext/pg_cron.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ stdenv.mkDerivation {
115115
116116
VERSION=$1
117117
LIB_DIR=$(dirname "$0")/../lib
118+
EXTENSION_DIR=$(dirname "$0")/../share/postgresql/extension
118119
119120
# Check if version exists
120121
if [ ! -f "$LIB_DIR/pg_cron-$VERSION${postgresql.dlSuffix}" ]; then
@@ -125,6 +126,10 @@ stdenv.mkDerivation {
125126
# Update library symlink
126127
ln -sfnv "pg_cron-$VERSION${postgresql.dlSuffix}" "$LIB_DIR/pg_cron${postgresql.dlSuffix}"
127128
129+
# Update control file
130+
echo "default_version = '$VERSION'" > "$EXTENSION_DIR/pg_cron.control"
131+
cat "$EXTENSION_DIR/pg_cron--$VERSION.control" >> "$EXTENSION_DIR/pg_cron.control"
132+
128133
echo "Successfully switched pg_cron to version $VERSION"
129134
EOF
130135

testinfra/test_ami_nix.py

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -579,24 +579,67 @@ def test_pg_cron_extension(host):
579579
if postgres_version != "15":
580580
pytest.skip(f"Skipping pg_cron test for PostgreSQL version {postgres_version}")
581581

582-
# Connect as supabase_admin and create the extension
583-
with host.sudo("postgres"):
584-
result = host.run('psql -U supabase_admin -d postgres -c "CREATE EXTENSION pg_cron WITH SCHEMA pg_catalog VERSION \'1.3.1\';"')
585-
assert result.rc == 0, f"Failed to create pg_cron extension: {result.stderr}"
586-
587-
# Create test table
588-
result = host.run('psql -U supabase_admin -d postgres -c "CREATE TABLE cron_test_log (id SERIAL PRIMARY KEY, message TEXT, log_time TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP);"')
589-
assert result.rc == 0, f"Failed to create test table: {result.stderr}"
590-
591-
# Schedule a job
592-
result = host.run('psql -U supabase_admin -d postgres -c "SELECT cron.schedule(\'* * * * *\', \'INSERT INTO cron_test_log (message) VALUES (\\\'Hello from pg_cron!\\\');\');"')
593-
assert result.rc == 0, f"Failed to schedule job: {result.stderr}"
594-
assert "1" in result.stdout, "Expected schedule ID 1"
595-
596-
# Verify job is scheduled
597-
result = host.run('psql -U supabase_admin -d postgres -c "SELECT * FROM cron.job;"')
598-
assert result.rc == 0, f"Failed to query cron.job: {result.stderr}"
599-
assert "* * * * *" in result.stdout, "Expected cron schedule pattern"
600-
assert "INSERT INTO cron_test_log" in result.stdout, "Expected cron command"
601-
assert "postgres" in result.stdout, "Expected postgres username"
602-
assert "postgres" in result.stdout, "Expected postgres database"
582+
# Use the SSH connection to run commands as postgres user
583+
ssh = host['ssh']
584+
585+
# Check prestart script
586+
result = run_ssh_command(ssh, 'ls -l /etc/postgresql/prestart.d/postgres_prestart.sh')
587+
assert result['succeeded'], f"Failed to find prestart script: {result['stderr']}"
588+
logger.info(f"Prestart script details: {result['stdout']}")
589+
590+
# Check if extensions file exists
591+
result = run_ssh_command(ssh, 'cat /root/pg_extensions.json')
592+
assert result['succeeded'], f"Failed to read extensions file: {result['stderr']}"
593+
logger.info(f"Extensions file contents: {result['stdout']}")
594+
595+
# Check if version switcher exists
596+
result = run_ssh_command(ssh, 'ls -l /var/lib/postgresql/.nix-profile/bin/switch_pg_cron_version')
597+
assert result['succeeded'], f"Failed to find version switcher: {result['stderr']}"
598+
logger.info(f"Version switcher details: {result['stdout']}")
599+
600+
# Create the extension
601+
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+
assert result['succeeded'], f"Failed to create pg_cron extension: {result['stderr']}"
603+
604+
# Verify the extension version
605+
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "SELECT extversion FROM pg_extension WHERE extname = \'pg_cron\';"')
606+
assert result['succeeded'], f"Failed to get pg_cron version: {result['stderr']}"
607+
assert "1.3.1" in result['stdout'], f"Expected pg_cron version 1.3.1, but got: {result['stdout']}"
608+
logger.info(f"pg_cron version: {result['stdout']}")
609+
610+
# Check the actual function definition
611+
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "\sf cron.schedule"')
612+
assert result['succeeded'], f"Failed to get cron.schedule function definition: {result['stderr']}"
613+
logger.info(f"cron.schedule function definition: {result['stdout']}")
614+
615+
# Check extension details
616+
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "SELECT * FROM pg_extension WHERE extname = \'pg_cron\';"')
617+
assert result['succeeded'], f"Failed to get pg_cron extension details: {result['stderr']}"
618+
logger.info(f"pg_cron extension details: {result['stdout']}")
619+
620+
# Create test table
621+
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "CREATE TABLE cron_test_log (id SERIAL PRIMARY KEY, message TEXT, log_time TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP);"')
622+
assert result['succeeded'], f"Failed to create test table: {result['stderr']}"
623+
624+
# Check the schema of cron.job table
625+
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "\d cron.job"')
626+
assert result['succeeded'], f"Failed to get cron.job schema: {result['stderr']}"
627+
logger.info(f"cron.job schema: {result['stdout']}")
628+
629+
# Check available cron functions
630+
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "\df cron.*"')
631+
assert result['succeeded'], f"Failed to get cron functions: {result['stderr']}"
632+
logger.info(f"Available cron functions: {result['stdout']}")
633+
634+
# Schedule a job using the basic schedule function
635+
result = run_ssh_command(ssh, '''sudo -u postgres psql -d postgres -c "SELECT cron.schedule('* * * * *'::text, 'INSERT INTO cron_test_log (message) VALUES (''Hello from pg_cron!'');'::text);"''')
636+
assert result['succeeded'], f"Failed to schedule job: {result['stderr']}"
637+
assert "1" in result['stdout'], "Expected schedule ID 1"
638+
639+
# Verify job is scheduled
640+
result = run_ssh_command(ssh, 'sudo -u postgres psql -d postgres -c "SELECT * FROM cron.job;"')
641+
assert result['succeeded'], f"Failed to query cron.job: {result['stderr']}"
642+
assert "* * * * *" in result['stdout'], "Expected cron schedule pattern"
643+
assert "INSERT INTO cron_test_log" in result['stdout'], "Expected cron command"
644+
assert "postgres" in result['stdout'], "Expected postgres username"
645+
assert "postgres" in result['stdout'], "Expected postgres database"

0 commit comments

Comments
 (0)