|
3 | 3 | import itertools
|
4 | 4 | import json
|
5 | 5 | import logging
|
| 6 | +import os |
6 | 7 | import pathlib
|
7 | 8 | import platform
|
8 | 9 | import subprocess
|
| 10 | +from datetime import UTC, datetime |
9 | 11 | from unittest.mock import MagicMock, Mock, PropertyMock, call, mock_open, patch, sentinel
|
10 | 12 |
|
11 | 13 | import charm_refresh
|
|
43 | 45 | SwitchoverFailedError,
|
44 | 46 | SwitchoverNotSyncError,
|
45 | 47 | )
|
46 |
| -from constants import PEER, SECRET_INTERNAL_LABEL, UPDATE_CERTS_BIN_PATH |
| 48 | +from constants import PEER, POSTGRESQL_DATA_PATH, SECRET_INTERNAL_LABEL, UPDATE_CERTS_BIN_PATH |
47 | 49 |
|
48 | 50 | CREATE_CLUSTER_CONF_PATH = "/etc/postgresql-common/createcluster.d/pgcharm.conf"
|
49 | 51 |
|
@@ -2642,3 +2644,55 @@ def test_get_ldap_parameters(harness):
|
2642 | 2644 | harness.charm.get_ldap_parameters()
|
2643 | 2645 | _get_relation_data.assert_called_once()
|
2644 | 2646 | _get_relation_data.reset_mock()
|
| 2647 | + |
| 2648 | + |
| 2649 | +def test_handle_processes_failures(harness): |
| 2650 | + _now = datetime.now(UTC) |
| 2651 | + with ( |
| 2652 | + patch( |
| 2653 | + "charm.Patroni.member_inactive", |
| 2654 | + new_callable=PropertyMock, |
| 2655 | + return_value=False, |
| 2656 | + ) as _member_inactive, |
| 2657 | + patch( |
| 2658 | + "charm.Patroni.restart_patroni", |
| 2659 | + ) as _restart_patroni, |
| 2660 | + patch("charm.os.listdir", return_value=["other_dirs", "pg_wal"]) as _listdir, |
| 2661 | + patch("charm.os.rename") as _rename, |
| 2662 | + patch("charm.datetime") as _datetime, |
| 2663 | + ): |
| 2664 | + _datetime.now.return_value = _now |
| 2665 | + rel_id = harness.model.get_relation(PEER).id |
| 2666 | + with harness.hooks_disabled(): |
| 2667 | + harness.update_relation_data( |
| 2668 | + rel_id, |
| 2669 | + harness.charm.app.name, |
| 2670 | + {"cluster_initialised": "True", "members_ips": '["192.0.2.0"]'}, |
| 2671 | + ) |
| 2672 | + |
| 2673 | + # Does nothing if member is inactive |
| 2674 | + assert not harness.charm._handle_processes_failures() |
| 2675 | + assert not _restart_patroni.called |
| 2676 | + |
| 2677 | + # Will not remove pg_wal if dir look right |
| 2678 | + _member_inactive.return_value = True |
| 2679 | + assert harness.charm._handle_processes_failures() |
| 2680 | + _restart_patroni.assert_called_once_with() |
| 2681 | + _restart_patroni.reset_mock() |
| 2682 | + |
| 2683 | + # Will move pg_wal if there's only a pg_wal dir |
| 2684 | + _listdir.return_value = ["pg_wal"] |
| 2685 | + assert harness.charm._handle_processes_failures() |
| 2686 | + assert not _restart_patroni.called |
| 2687 | + _rename.assert_called_once_with( |
| 2688 | + os.path.join(POSTGRESQL_DATA_PATH, "pg_wal"), |
| 2689 | + os.path.join(POSTGRESQL_DATA_PATH, f"pg_wal-{_now.isoformat()}"), |
| 2690 | + ) |
| 2691 | + _rename.reset_mock() |
| 2692 | + |
| 2693 | + # Will not move pg_wal if there's only a pg_wal dir or other moved dirs |
| 2694 | + _listdir.return_value = ["pg_wal", f"pg_wal-{_now.isoformat()}"] |
| 2695 | + assert harness.charm._handle_processes_failures() |
| 2696 | + _restart_patroni.assert_called_once_with() |
| 2697 | + assert not _rename.called |
| 2698 | + _restart_patroni.reset_mock() |
0 commit comments