Skip to content

Commit 2e99c83

Browse files
committed
Merge branch 'main' into dpe-4533-flaky-test
2 parents 20ec17e + 2557c2b commit 2e99c83

File tree

5 files changed

+111
-54
lines changed

5 files changed

+111
-54
lines changed

docs/how-to/h-monitor/h-enable-tracing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Integrate `tempo-k8s` with the COS charms as follows:
4848
```shell
4949
juju integrate tempo-k8s:grafana-dashboard grafana:grafana-dashboard
5050
juju integrate tempo-k8s:grafana-source grafana:grafana-source
51-
juju integrate tempo-k8s:ingress traefik:traefik
51+
juju integrate tempo-k8s:ingress traefik:traefik-route
5252
juju integrate tempo-k8s:metrics-endpoint prometheus:metrics-endpoint
5353
juju integrate tempo-k8s:logging loki:logging
5454
```

src/charm.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,7 @@ def get_secret(self, scope: Scopes, key: str) -> Optional[str]:
294294
return None
295295
secret_key = self._translate_field_to_secret_key(key)
296296
# Old translation in databag is to be taken
297-
if key != secret_key and (
298-
result := self.peer_relation_data(scope).fetch_my_relation_field(peers.id, key)
299-
):
297+
if result := self.peer_relation_data(scope).fetch_my_relation_field(peers.id, key):
300298
return result
301299

302300
return self.peer_relation_data(scope).get_secret(peers.id, secret_key)
@@ -312,10 +310,7 @@ def set_secret(self, scope: Scopes, key: str, value: Optional[str]) -> Optional[
312310
peers = self.model.get_relation(PEER)
313311
secret_key = self._translate_field_to_secret_key(key)
314312
# Old translation in databag is to be deleted
315-
if key != secret_key and self.peer_relation_data(scope).fetch_my_relation_field(
316-
peers.id, key
317-
):
318-
self.peer_relation_data(scope).delete_relation_data(peers.id, [key])
313+
self.peer_relation_data(scope).delete_relation_data(peers.id, [key])
319314
self.peer_relation_data(scope).set_secret(peers.id, secret_key, value)
320315

321316
def remove_secret(self, scope: Scopes, key: str) -> None:
@@ -1091,10 +1086,18 @@ def _on_start(self, event: StartEvent) -> None:
10911086
# On replicas, only prepare for starting the instance later.
10921087
if not self.unit.is_leader():
10931088
self._start_replica(event)
1089+
self._restart_services_after_reboot()
10941090
return
10951091

10961092
# Bootstrap the cluster in the leader unit.
10971093
self._start_primary(event)
1094+
self._restart_services_after_reboot()
1095+
1096+
def _restart_services_after_reboot(self):
1097+
"""Restart the Patroni and pgBackRest after a reboot."""
1098+
if self._unit_ip in self.members_ips:
1099+
self._patroni.start_patroni()
1100+
self.backup.start_stop_pgbackrest_service()
10981101

10991102
def _setup_exporter(self) -> None:
11001103
"""Set up postgresql_exporter options."""

src/constants.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
SNAP_PACKAGES = [
3939
(
4040
POSTGRESQL_SNAP_NAME,
41-
{"revision": {"aarch64": "114", "x86_64": "115"}, "channel": "14/stable"},
41+
{
42+
"revision": {"aarch64": "121", "x86_64": "120"},
43+
"channel": "14/stable",
44+
},
4245
)
4346
]
4447

src/dependency.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"dependencies": {},
1010
"name": "charmed-postgresql",
1111
"upgrade_supported": "^14",
12-
"version": "14.11"
12+
"version": "14.12"
1313
}
1414
}

tests/unit/test_charm.py

Lines changed: 95 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright 2021 Canonical Ltd.
22
# See LICENSE file for licensing details.
33
import itertools
4+
import json
45
import logging
56
import platform
67
import subprocess
@@ -558,6 +559,9 @@ def test_enable_disable_extensions(harness, caplog):
558559
@patch_network_get(private_address="1.1.1.1")
559560
def test_on_start(harness):
560561
with (
562+
patch(
563+
"charm.PostgresqlOperatorCharm._restart_services_after_reboot"
564+
) as _restart_services_after_reboot,
561565
patch(
562566
"charm.PostgresqlOperatorCharm._set_primary_status_message"
563567
) as _set_primary_status_message,
@@ -617,33 +621,41 @@ def test_on_start(harness):
617621
harness.charm.on.start.emit()
618622
_bootstrap_cluster.assert_called_once()
619623
_oversee_users.assert_not_called()
624+
_restart_services_after_reboot.assert_called_once()
620625
assert isinstance(harness.model.unit.status, BlockedStatus)
621626
# Set an initial waiting status (like after the install hook was triggered).
622627
harness.model.unit.status = WaitingStatus("fake message")
623628

624629
# Test the event of an error happening when trying to create the default postgres user.
630+
_restart_services_after_reboot.reset_mock()
625631
_member_started.return_value = True
626632
harness.charm.on.start.emit()
627633
_postgresql.create_user.assert_called_once()
628634
_oversee_users.assert_not_called()
635+
_restart_services_after_reboot.assert_called_once()
629636
assert isinstance(harness.model.unit.status, BlockedStatus)
630637

631638
# Set an initial waiting status again (like after the install hook was triggered).
632639
harness.model.unit.status = WaitingStatus("fake message")
633640

634641
# Then test the event of a correct cluster bootstrapping.
642+
_restart_services_after_reboot.reset_mock()
635643
harness.charm.on.start.emit()
636644
assert _postgresql.create_user.call_count == 4 # Considering the previous failed call.
637645
_oversee_users.assert_called_once()
638646
_enable_disable_extensions.assert_called_once()
639647
_set_primary_status_message.assert_called_once()
648+
_restart_services_after_reboot.assert_called_once()
640649

641650

642651
@patch_network_get(private_address="1.1.1.1")
643652
def test_on_start_replica(harness):
644653
with (
645654
patch("charm.snap.SnapCache") as _snap_cache,
646655
patch("charm.Patroni.get_postgresql_version") as _get_postgresql_version,
656+
patch(
657+
"charm.PostgresqlOperatorCharm._restart_services_after_reboot"
658+
) as _restart_services_after_reboot,
647659
patch("charm.Patroni.configure_patroni_on_unit") as _configure_patroni_on_unit,
648660
patch(
649661
"charm.Patroni.member_started",
@@ -674,25 +686,30 @@ def test_on_start_replica(harness):
674686
harness.charm._peers.data[harness.charm.app].update({"cluster_initialised": ""})
675687
harness.charm.on.start.emit()
676688
_defer.assert_called_once()
689+
_restart_services_after_reboot.assert_called_once()
677690

678691
# Set an initial waiting status again (like after a machine restart).
679692
harness.model.unit.status = WaitingStatus("fake message")
680693

681694
# Mark the cluster as initialised and with the workload up and running.
695+
_restart_services_after_reboot.reset_mock()
682696
harness.charm._peers.data[harness.charm.app].update({"cluster_initialised": "True"})
683697
_member_started.return_value = True
684698
harness.charm.on.start.emit()
685699
_configure_patroni_on_unit.assert_not_called()
700+
_restart_services_after_reboot.assert_called_once()
686701
assert isinstance(harness.model.unit.status, ActiveStatus)
687702

688703
# Set an initial waiting status (like after the install hook was triggered).
689704
harness.model.unit.status = WaitingStatus("fake message")
690705

691706
# Check that the unit status doesn't change when the workload is not running.
692707
# In that situation only Patroni is configured in the unit (but not started).
708+
_restart_services_after_reboot.reset_mock()
693709
_member_started.return_value = False
694710
harness.charm.on.start.emit()
695711
_configure_patroni_on_unit.assert_called_once()
712+
_restart_services_after_reboot.assert_called_once()
696713
assert isinstance(harness.model.unit.status, WaitingStatus)
697714

698715

@@ -2447,70 +2464,104 @@ def test_set_primary_status_message(harness, is_leader):
24472464
harness.charm._set_primary_status_message()
24482465
tc.assertIsInstance(harness.charm.unit.status, MaintenanceStatus)
24492466

2450-
@patch("charm.Patroni.update_patroni_restart_condition")
2451-
@patch("charm.Patroni.get_patroni_restart_condition")
2452-
@patch("charm.PostgresqlOperatorCharm._unit_ip")
2453-
def test_override_patroni_restart_condition(
2454-
self, _unit_ip, get_restart_condition, update_restart_condition
2467+
2468+
def test_override_patroni_restart_condition(harness):
2469+
with (
2470+
patch("charm.Patroni.update_patroni_restart_condition") as _update_restart_condition,
2471+
patch("charm.Patroni.get_patroni_restart_condition") as _get_restart_condition,
2472+
patch("charm.PostgresqlOperatorCharm._unit_ip") as _unit_ip,
24552473
):
2456-
get_restart_condition.return_value = "always"
2474+
_get_restart_condition.return_value = "always"
24572475

24582476
# Do override without repeat_cause
2459-
assert self.charm.override_patroni_restart_condition("no") is True
2460-
get_restart_condition.assert_called_once()
2461-
update_restart_condition.assert_called_once_with("no")
2462-
get_restart_condition.reset_mock()
2463-
update_restart_condition.reset_mock()
2477+
assert harness.charm.override_patroni_restart_condition("no", None) is True
2478+
_get_restart_condition.assert_called_once()
2479+
_update_restart_condition.assert_called_once_with("no")
2480+
_get_restart_condition.reset_mock()
2481+
_update_restart_condition.reset_mock()
24642482

2465-
get_restart_condition.return_value = "no"
2483+
_get_restart_condition.return_value = "no"
24662484

24672485
# Must not be overridden twice without repeat_cause
2468-
assert self.charm.override_patroni_restart_condition("on-failure") is False
2469-
get_restart_condition.assert_called_once()
2470-
update_restart_condition.assert_not_called()
2471-
get_restart_condition.reset_mock()
2472-
update_restart_condition.reset_mock()
2486+
assert harness.charm.override_patroni_restart_condition("on-failure", None) is False
2487+
_get_restart_condition.assert_called_once()
2488+
_update_restart_condition.assert_not_called()
2489+
_get_restart_condition.reset_mock()
2490+
_update_restart_condition.reset_mock()
24732491

24742492
# Reset override
2475-
self.charm.restore_patroni_restart_condition()
2476-
update_restart_condition.assert_called_once_with("always")
2477-
update_restart_condition.reset_mock()
2493+
harness.charm.restore_patroni_restart_condition()
2494+
_update_restart_condition.assert_called_once_with("always")
2495+
_update_restart_condition.reset_mock()
24782496

24792497
# Must not be reset twice
2480-
self.charm.restore_patroni_restart_condition()
2481-
update_restart_condition.assert_not_called()
2482-
update_restart_condition.reset_mock()
2498+
harness.charm.restore_patroni_restart_condition()
2499+
_update_restart_condition.assert_not_called()
2500+
_update_restart_condition.reset_mock()
24832501

2484-
get_restart_condition.return_value = "always"
2502+
_get_restart_condition.return_value = "always"
24852503

24862504
# Do override with repeat_cause
2487-
assert self.charm.override_patroni_restart_condition("no", "test_charm") is True
2488-
get_restart_condition.assert_called_once()
2489-
update_restart_condition.assert_called_once_with("no")
2490-
get_restart_condition.reset_mock()
2491-
update_restart_condition.reset_mock()
2505+
assert harness.charm.override_patroni_restart_condition("no", "test_charm") is True
2506+
_get_restart_condition.assert_called_once()
2507+
_update_restart_condition.assert_called_once_with("no")
2508+
_get_restart_condition.reset_mock()
2509+
_update_restart_condition.reset_mock()
24922510

2493-
get_restart_condition.return_value = "no"
2511+
_get_restart_condition.return_value = "no"
24942512

24952513
# Do re-override with repeat_cause
2496-
assert self.charm.override_patroni_restart_condition("on-success", "test_charm") is True
2497-
get_restart_condition.assert_called_once()
2498-
update_restart_condition.assert_called_once_with("on-success")
2499-
get_restart_condition.reset_mock()
2500-
update_restart_condition.reset_mock()
2514+
assert harness.charm.override_patroni_restart_condition("on-success", "test_charm") is True
2515+
_get_restart_condition.assert_called_once()
2516+
_update_restart_condition.assert_called_once_with("on-success")
2517+
_get_restart_condition.reset_mock()
2518+
_update_restart_condition.reset_mock()
25012519

2502-
get_restart_condition.return_value = "on-success"
2520+
_get_restart_condition.return_value = "on-success"
25032521

25042522
# Must not be re-overridden with different repeat_cause
25052523
assert (
2506-
self.charm.override_patroni_restart_condition("on-failure", "test_not_charm") is False
2524+
harness.charm.override_patroni_restart_condition("on-failure", "test_not_charm")
2525+
is False
25072526
)
2508-
get_restart_condition.assert_called_once()
2509-
update_restart_condition.assert_not_called()
2510-
get_restart_condition.reset_mock()
2511-
update_restart_condition.reset_mock()
2527+
_get_restart_condition.assert_called_once()
2528+
_update_restart_condition.assert_not_called()
2529+
_get_restart_condition.reset_mock()
2530+
_update_restart_condition.reset_mock()
25122531

25132532
# Reset override
2514-
self.charm.restore_patroni_restart_condition()
2515-
update_restart_condition.assert_called_once_with("always")
2516-
update_restart_condition.reset_mock()
2533+
harness.charm.restore_patroni_restart_condition()
2534+
_update_restart_condition.assert_called_once_with("always")
2535+
_update_restart_condition.reset_mock()
2536+
2537+
2538+
def test_restart_services_after_reboot(harness):
2539+
with (
2540+
patch(
2541+
"backups.PostgreSQLBackups.start_stop_pgbackrest_service"
2542+
) as _start_stop_pgbackrest_service,
2543+
patch("charm.Patroni.start_patroni") as _start_patroni,
2544+
patch(
2545+
"charm.PostgresqlOperatorCharm._unit_ip",
2546+
new_callable=PropertyMock(return_value="1.1.1.1"),
2547+
) as _unit_ip,
2548+
):
2549+
with harness.hooks_disabled():
2550+
harness.update_relation_data(
2551+
harness.model.get_relation(PEER).id,
2552+
harness.charm.app.name,
2553+
{"members_ips": json.dumps([])},
2554+
)
2555+
harness.charm._restart_services_after_reboot()
2556+
_start_patroni.assert_not_called()
2557+
_start_stop_pgbackrest_service.assert_not_called()
2558+
2559+
with harness.hooks_disabled():
2560+
harness.update_relation_data(
2561+
harness.model.get_relation(PEER).id,
2562+
harness.charm.app.name,
2563+
{"members_ips": json.dumps([_unit_ip])},
2564+
)
2565+
harness.charm._restart_services_after_reboot()
2566+
_start_patroni.assert_called_once()
2567+
_start_stop_pgbackrest_service.assert_called_once()

0 commit comments

Comments
 (0)