From 649bdb4683578c7bd5bff235dd1e4551d3b91a45 Mon Sep 17 00:00:00 2001 From: Chris MacNaughton Date: Wed, 12 Feb 2020 11:26:46 +0100 Subject: [PATCH] Ensure that we define `unit` before use When ceph-proxy is configured prior to being related to clients, it is possible for the units to go into a hook error because of an undefined variable. This change ensures that we do correctly define the unit before we use it. Change-Id: Ic6e28783bde4fc342d7c5ef1d733e69a03b702fe Closes-Bug: #1862487 --- hooks/ceph_hooks.py | 6 ++++-- unit_tests/test_ceph_hooks.py | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/hooks/ceph_hooks.py b/hooks/ceph_hooks.py index a637ff6..53568cd 100755 --- a/hooks/ceph_hooks.py +++ b/hooks/ceph_hooks.py @@ -146,7 +146,8 @@ def notify_radosgws(): def notify_client(): for relid in relation_ids('client'): - client_relation_joined(relid) + for unit in related_units(relid): + client_relation_joined(relid=relid, unit=unit) @hooks.hook('radosgw-relation-changed') @@ -195,7 +196,8 @@ def client_relation_joined(relid=None, unit=None): units = related_units(relid) if len(units) > 0: service_name = units[0].split('/')[0] - + if unit is None: + unit = units[0] if service_name is not None: ceph_addrs = config('monitor-hosts') data = {'key': ceph.get_named_key(service_name), diff --git a/unit_tests/test_ceph_hooks.py b/unit_tests/test_ceph_hooks.py index e6b4f08..63dccdb 100644 --- a/unit_tests/test_ceph_hooks.py +++ b/unit_tests/test_ceph_hooks.py @@ -87,7 +87,9 @@ class TestHooks(test_utils.CharmTestCase): def c(k): x = {'radosgw': ['rados:1'], 'client': ['client:1'], - 'rados:1': ['rados/1']} + 'rados:1': ['rados/1'], + 'client:1': ['client/1'], + } return x[k] self.relation_ids.side_effect = c @@ -124,7 +126,7 @@ class TestHooks(test_utils.CharmTestCase): context, owner='ceph-user', perms=0o600) mock_rgw_rel.assert_called_with(relid='rados:1', unit='rados/1') - mock_client_rel.assert_called_with('client:1') + mock_client_rel.assert_called_with(relid='client:1', unit='client/1') @mock.patch.object(hooks.ceph, 'ceph_user') @mock.patch('subprocess.check_output')