|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2026 Canonical Ltd. |
| 3 | +# See LICENSE file for licensing details. |
| 4 | + |
| 5 | +"""Integration tests for PostgreSQL symlinks (pgdata).""" |
| 6 | + |
| 7 | +import logging |
| 8 | + |
| 9 | +import pytest |
| 10 | +from pytest_operator.plugin import OpsTest |
| 11 | + |
| 12 | +from .helpers import ( |
| 13 | + METADATA, |
| 14 | + PGDATA_PATH, |
| 15 | + build_and_deploy, |
| 16 | + run_command_on_unit, |
| 17 | +) |
| 18 | + |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | +APP_NAME = METADATA["name"] |
| 22 | +UNIT_IDS = [0, 1, 2] |
| 23 | +PGDATA_SYMLINK_PATH = "/var/lib/postgresql/16/main" |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.abort_on_fail |
| 27 | +@pytest.mark.skip_if_deployed |
| 28 | +async def test_build_and_deploy(ops_test: OpsTest, charm): |
| 29 | + """Build the charm-under-test and deploy it. |
| 30 | +
|
| 31 | + Assert on the unit status before any relations/configurations take place. |
| 32 | + """ |
| 33 | + async with ops_test.fast_forward(): |
| 34 | + await build_and_deploy(ops_test, charm, len(UNIT_IDS), APP_NAME) |
| 35 | + for unit_id in UNIT_IDS: |
| 36 | + assert ops_test.model.applications[APP_NAME].units[unit_id].workload_status == "active" |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize("unit_id", UNIT_IDS) |
| 40 | +async def test_pgdata_symlinks(ops_test: OpsTest, unit_id: int): |
| 41 | + """Test that symlink for pgdata is correctly created.""" |
| 42 | + unit_name = f"{APP_NAME}/{unit_id}" |
| 43 | + |
| 44 | + # Check pgdata symlink exists and points to correct location |
| 45 | + pgdata_symlink_check = await run_command_on_unit( |
| 46 | + ops_test, unit_name, f"readlink -f {PGDATA_SYMLINK_PATH}" |
| 47 | + ) |
| 48 | + assert pgdata_symlink_check.strip() == PGDATA_PATH, ( |
| 49 | + f"Expected pgdata symlink to point to {PGDATA_PATH}, got {pgdata_symlink_check.strip()}" |
| 50 | + ) |
| 51 | + |
| 52 | + # Verify symlink is owned by postgres:postgres |
| 53 | + pgdata_owner = await run_command_on_unit( |
| 54 | + ops_test, unit_name, f"stat -c '%U:%G' {PGDATA_SYMLINK_PATH}" |
| 55 | + ) |
| 56 | + assert pgdata_owner.strip() == "postgres:postgres", ( |
| 57 | + f"Expected pgdata symlink to be owned by postgres:postgres, got {pgdata_owner.strip()}" |
| 58 | + ) |
0 commit comments