Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/integration_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ jobs:
- name: Set up environment
timeout-minutes: 15
run: |
# pytest-operator's ops_test fixture requires charmcraft on PATH
sudo snap install charmcraft --classic
sudo snap install go --classic
go install github.com/canonical/spread/cmd/spread@latest
- name: Download packed test charm
Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ lint = [
]
unit = [
"coverage[toml]==7.16.2",
"pytest==9.1.1"
"pytest==9.1.1",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: we should stick to 9.1 until we move away from pytest-operator.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving away from pytest-operator starting on PR #431.

"pytest-asyncio==0.21.2",
]
integration = [
"pytest==9.1.1",
"pytest-operator==0.43.2",
"juju==3.6.1.3",
"jubilant==1.14.0",
"allure-pytest==2.16.2",
"allure-pytest-default-results==0.1.4",
Expand All @@ -101,6 +104,7 @@ show_missing = true
[tool.pytest.ini_options]
minversion = "6.0"
log_cli_level = "INFO"
asyncio_mode = "auto"

# Linting tools configuration
[tool.ruff]
Expand Down
14 changes: 7 additions & 7 deletions tests/charms/postgresql_k8s_test_charm/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ options:
Enables tracking of function call counts and time used. Specify pl to track only procedural-language functions
type: string
default: "none"
# logical-replication-subscription-request:
# description: |
# Set of databases corresponding to list of tables with schema notation in JSON format, which will be requested from
# publiblisher cluster to subscribe on via logical replication.
# Example: {"<database>": ["<schema>.<table>", ...], ...}
# type: string
# default: "{}"
logical-replication-subscription-request:
description: |
Set of databases corresponding to list of tables with schema notation in JSON format, which will be requested from
publiblisher cluster to subscribe on via logical replication.
Example: {"<database>": ["<schema>.<table>", ...], ...}
type: string
default: "{}"
memory-maintenance-work-mem:
description: |
Sets the maximum memory (KB) to be used for maintenance operations.
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/charm/ha_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright 2026 Canonical Ltd.
# See LICENSE file for licensing details.
7 changes: 7 additions & 0 deletions tests/integration/charm/ha_tests/clean-data-dir-k8s.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -Eeuo pipefail
find /var/lib/pg/archive -mindepth 1 -delete
find /var/lib/pg/data/16/main -mindepth 1 -delete
find /var/lib/pg/logs -mindepth 1 -delete
find /var/lib/pg/temp -mindepth 1 -delete
4 changes: 4 additions & 0 deletions tests/integration/charm/ha_tests/clean-data-dir-vm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

set -Eeuo pipefail
rm -rf /var/snap/charmed-postgresql/common/var/lib/postgresql/*
168 changes: 168 additions & 0 deletions tests/integration/charm/ha_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#!/usr/bin/env python3
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.
from asyncio import gather

import pytest as pytest
from pytest_operator.plugin import OpsTest
from tenacity import Retrying, stop_after_delay, wait_fixed

from tests.integration.charm.ha_tests.helpers import (
ORIGINAL_RESTART_CONDITION,
RESTART_CONDITION,
change_patroni_setting,
change_wal_settings,
deploy_chaos_mesh,
destroy_chaos_mesh,
get_patroni_setting,
get_postgresql_parameter,
modify_pebble_restart_delay,
remove_instance_isolation,
update_restart_condition,
)
from tests.integration.charm.pytest_operator_helpers import (
APPLICATION_NAME,
app_name,
get_password,
run_command_on_unit,
)
from tests.integration.types import Substrate


@pytest.fixture()
async def chaos_mesh(ops_test: OpsTest, substrate: Substrate) -> None:
"""Deploys chaos mesh to the namespace and uninstalls it at the end (K8s only)."""
if substrate == Substrate.vm:
yield
return

deploy_chaos_mesh(ops_test.model.info.name)

yield

remove_instance_isolation(ops_test)
destroy_chaos_mesh(ops_test.model.info.name)


@pytest.fixture()
async def continuous_writes(ops_test: OpsTest) -> None:
"""Deploy the charm that makes continuous writes to PostgreSQL."""
yield
# Clear the written data at the end.
for attempt in Retrying(stop=stop_after_delay(60 * 5), wait=wait_fixed(3), reraise=True):
with attempt:
action = (
await ops_test.model
.applications[APPLICATION_NAME]
.units[0]
.run_action("clear-continuous-writes")
)
await action.wait()
assert action.results["result"] == "True", "Unable to clear up continuous_writes table"


@pytest.fixture()
async def loop_wait(ops_test: OpsTest) -> None:
"""Temporary change the loop wait configuration."""
# Change the parameter that makes Patroni wait for some more time before restarting PostgreSQL.
initial_loop_wait = await get_patroni_setting(ops_test, "loop_wait")
yield
# Rollback to the initial configuration.
patroni_password = await get_password(ops_test, "patroni")
await change_patroni_setting(
ops_test, "loop_wait", initial_loop_wait, patroni_password, use_random_unit=True
)


@pytest.fixture(scope="module")
async def primary_start_timeout(ops_test: OpsTest) -> None:
"""Temporary change the primary start timeout configuration."""
# Change the parameter that makes the primary reelection faster.
patroni_password = await get_password(ops_test, "patroni")
initial_primary_start_timeout = await get_patroni_setting(ops_test, "primary_start_timeout")
await change_patroni_setting(ops_test, "primary_start_timeout", 0, patroni_password)
yield
# Rollback to the initial configuration.
await change_patroni_setting(
ops_test,
"primary_start_timeout",
initial_primary_start_timeout,
patroni_password,
use_random_unit=True,
)


@pytest.fixture()
async def reset_restart_condition(ops_test: OpsTest, substrate: Substrate):
"""Resets service file delay on all units (VM only)."""
if substrate == Substrate.k8s:
yield
return

app = await app_name(ops_test)

awaits = []
for unit in ops_test.model.applications[app].units:
awaits.append(update_restart_condition(ops_test, unit, RESTART_CONDITION))
await gather(*awaits)

yield

awaits = []
for unit in ops_test.model.applications[app].units:
awaits.append(update_restart_condition(ops_test, unit, ORIGINAL_RESTART_CONDITION))
await gather(*awaits)


@pytest.fixture()
async def restart_policy(ops_test: OpsTest, substrate: Substrate) -> None:
"""Sets and resets service pebble restart policy on all units (K8s only)."""
if substrate == Substrate.vm:
yield
return

app = await app_name(ops_test)

for unit in ops_test.model.applications[app].units:
await modify_pebble_restart_delay(
ops_test,
unit.name,
"tests/integration/charm/ha_tests/manifests/extend_pebble_restart_delay.yml",
)

async with ops_test.fast_forward():
await ops_test.model.wait_for_idle(
apps=[app],
status="active",
raise_on_blocked=True,
timeout=5 * 60,
idle_period=30,
)

yield


@pytest.fixture()
async def wal_settings(ops_test: OpsTest, substrate: Substrate) -> None:
"""Restore the WAL settings to the initial values."""
# Get the value for each setting.
initial_max_wal_size = await get_postgresql_parameter(ops_test, "max_wal_size")
initial_min_wal_size = await get_postgresql_parameter(ops_test, "min_wal_size")
initial_wal_keep_segments = await get_postgresql_parameter(ops_test, "wal_keep_segments")
yield
app = await app_name(ops_test)
for unit in ops_test.model.applications[app].units:
if substrate == Substrate.vm:
# Start Patroni if it was previously stopped.
await run_command_on_unit(ops_test, unit.name, "snap start charmed-postgresql.patroni")
patroni_password = await get_password(ops_test, "patroni")

# Rollback to the initial settings.
await change_wal_settings(
ops_test,
unit.name,
initial_max_wal_size,
initial_min_wal_size,
initial_wal_keep_segments,
patroni_password,
)
Loading