From 4d5581438ad216a0de69c0959e05e0b8a76bc9aa Mon Sep 17 00:00:00 2001 From: Felipe Reyes Date: Tue, 12 Dec 2023 11:06:29 -0300 Subject: [PATCH] Run relation_set() in dashboard_relation_changed() on leader The commit 484b7d8260 introduced a new relation that relies on an application databag to exchange data, although only the leader can write to it, and the original patch didn't guard the relation_set() call with a is_leader(), this patch addresses that problem wich produces a hook failure on follower units when openstack-dashboard is deployed in HA. Closes-Bug: #2046257 Related-Bug: #2030094 Change-Id: I1930b0b96f65cb627f896db67dddc6370cf6a413 --- hooks/horizon_hooks.py | 9 +++++++-- unit_tests/test_horizon_hooks.py | 9 ++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/hooks/horizon_hooks.py b/hooks/horizon_hooks.py index edbb13e5..e4f76953 100755 --- a/hooks/horizon_hooks.py +++ b/hooks/horizon_hooks.py @@ -480,8 +480,13 @@ def dashboard_relation_changed(): 'vip': config('vip'), } - for rel_id in relations: - relation_set(rel_id, relation_settings=relation_settings, app=True) + if is_leader(): + log("Setting dashboard access information on 'dashboard' relation", + level="INFO") + for rel_id in relations: + relation_set(rel_id, relation_settings=relation_settings, app=True) + else: + log("Skipping relation_set, because not leader.", level="DEBUG") def main(): diff --git a/unit_tests/test_horizon_hooks.py b/unit_tests/test_horizon_hooks.py index fac20090..9a4f1c98 100644 --- a/unit_tests/test_horizon_hooks.py +++ b/unit_tests/test_horizon_hooks.py @@ -527,13 +527,15 @@ class TestHorizonHooks(CharmTestCase): }) ]) - def test_dashboard_relation_changed(self): + @patch.object(hooks, 'is_leader') + def test_dashboard_relation_changed(self, is_leader): self.relation_ids.return_value = None hooks.dashboard_relation_changed() self.test_config.set('os-public-hostname', 'mydashboard.local') self.test_config.set('vip', '1.2.3.4') self.relation_ids.return_value = ['dashboard:0'] + is_leader.return_value = True hooks.dashboard_relation_changed() self.relation_set.assert_called_with( @@ -542,3 +544,8 @@ class TestHorizonHooks(CharmTestCase): 'vip': '1.2.3.4'}, app=True, ) + + self.relation_set.reset_mock() + is_leader.return_value = False + hooks.dashboard_relation_changed() + self.relation_set.assert_not_called()