import pytest import textwrap # Add the current cookbook's tests directory to the path so we can import helpers.py. from pathlib import Path import sys sys.path.insert(0, str(Path(__file__).parent)) import helpers # noqa: E402 # Major version of PostgreSQL to install by default on a fresh VM boot. PG_MAJOR_DEFAULT = 18 # Extra files to inject into the FCOS image for the tests in this file. # The config.env is used to configure the PostgreSQL Quadlet. PYTEST_FCOS_EXTRA_FILES = { "/etc/quadlets/postgresql/config.env": ( textwrap.dedent(f""" # This file is generated by conftest.py for testing purposes. POSTGRES_USER=postgres POSTGRES_PASSWORD=postgres POSTGRES_DB=postgres POSTGRES_HOST_AUTH_METHOD=scram-sha-256 POSTGRES_INITDB_ARGS=--auth-host=scram-sha-256 POSTGRES_ARGS=-h 127.0.0.1 PGPORT=5432 PG_MAJOR={PG_MAJOR_DEFAULT} POSTGRES_BACKUP_RETENTION=7 """), 0, 0, 0o600, ), } """ Verify that the postgresql Quadlet correctly restores a database from a backup on a fresh VM with the backup data present in the virtiofs. """ class TestPostgresqlQuadletRestore(helpers.TestPostgresqlQuadlet): expected_pg_major = PG_MAJOR_DEFAULT def test_data_is_still_there_after_restore(self, fcos_host): """Data created before the restore must still be there after the restore.""" # Check that the old data is still there after the restore output = self._run_sql(fcos_host, "SELECT datname FROM pg_database WHERE datname = 'upgrade_path_db'") assert output == "upgrade_path_db", f"Unexpected output from SQL query: {output}" output = self._run_sql(fcos_host, "SELECT datname FROM pg_database WHERE datname = 'testdb'") assert output == "testdb", f"Unexpected output from SQL query: {output}" result = fcos_host.run( "podman exec postgresql-server psql -U test -d testdb --csv -t -c %s", "SELECT 1 AS probe" ) assert result.exit_status == 0, f"SQL query failed with exit code {result.exit_status}: {result.stderr}" # Check that the upgrade_path table contains the initial postgresql version (14) output = self._run_sql(fcos_host, "SELECT LEFT(version, 14) FROM upgrade_path ORDER BY version ASC LIMIT 1", database="upgrade_path_db") assert output.startswith("PostgreSQL 14."), f"Unexpected output from SQL query: {output}"