Merge "Do not set gnocchi URL until clustering complete"

This commit is contained in:
Zuul 2018-03-02 09:54:56 +00:00 committed by Gerrit Code Review
commit 2d581d2f1b
3 changed files with 48 additions and 1 deletions

2
.gitignore vendored
View File

@ -8,3 +8,5 @@ test-charm/
.project
.pydevproject
**/__pycache__
interfaces
layers

View File

@ -55,17 +55,22 @@ def render_config(*args):
charm_class.render_with_interfaces(args)
charm_class.enable_webserver_site()
charm_class.assess_status()
hookenv.log("Configuration rendered", hookenv.DEBUG)
reactive.set_state('config.rendered')
# db_sync checks if sync has been done so rerunning is a noop
@reactive.when('config.rendered')
@reactive.when_not('db.synced')
def init_db():
with charm.provide_charm_instance() as charm_class:
charm_class.db_sync()
hookenv.log("Database synced", hookenv.DEBUG)
reactive.set_state('db.synced')
@reactive.when('ha.connected')
@reactive.when_not('ha.available')
def cluster_connected(hacluster):
"""Configure HA resources in corosync"""
with charm.provide_charm_instance() as charm_class:
@ -100,6 +105,19 @@ def storage_ceph_disconnected():
@reactive.when('metric-service.connected')
@reactive.when('config.rendered')
@reactive.when('db.synced')
def provide_gnocchi_url(metric_service):
# Reactive endpoints are only fully functional in their respective relation
# hooks. So we cannot rely on ha.is_clustered() which would return
# False when not in a relation hook. Use flags instead.
# Check if the optional relation, ha is connected. If it is,
# check if it is available.
if (reactive.flags.is_flag_set('ha.connected') and
not reactive.flags.is_flag_set('ha.available')):
hookenv.log("Hacluster is related but not yet clustered",
hookenv.DEBUG)
return
with charm.provide_charm_instance() as charm_class:
hookenv.log("Providing gnocchi URL: {}"
.format(charm_class.public_url), hookenv.DEBUG)
metric_service.set_gnocchi_url(charm_class.public_url)

View File

@ -28,6 +28,7 @@ class TestRegisteredHooks(test_utils.TestRegisteredHooks):
defaults = [
'charm.installed',
'shared-db.connected',
'ha.connected',
'identity-service.connected',
'identity-service.available',
'config.changed',
@ -50,6 +51,7 @@ class TestRegisteredHooks(test_utils.TestRegisteredHooks):
'provide_gnocchi_url': (
'metric-service.connected',
'config.rendered',
'db.synced',
),
'configure_ceph': (
'storage-ceph.available',
@ -65,6 +67,12 @@ class TestRegisteredHooks(test_utils.TestRegisteredHooks):
'disable_services': (
'config.rendered',
),
'cluster_connected': (
'ha.available',
),
'init_db': (
'db.synced',
),
},
}
# test that the hooks were registered via the
@ -129,7 +137,26 @@ class TestHandlers(test_utils.PatchHelper):
handlers.storage_ceph_disconnected()
mock_ceph_helper.delete_keyring.assert_called_once_with('gnocchi')
def test_provide_gnocchi_url(self):
@mock.patch.object(handlers.reactive.flags, 'is_flag_set')
def test_provide_gnocchi_url(self, mock_is_flag_set):
mock_is_flag_set.return_value = False
mock_gnocchi = mock.MagicMock()
self.gnocchi_charm.public_url = "http://gnocchi:8041"
handlers.provide_gnocchi_url(mock_gnocchi)
mock_gnocchi.set_gnocchi_url.assert_called_once_with(
"http://gnocchi:8041"
)
@mock.patch.object(handlers.reactive.flags, 'is_flag_set')
def test_provide_gnocchi_url_ha_connected(self, mock_is_flag_set):
mock_is_flag_set.side_effect = [True, False]
mock_gnocchi = mock.MagicMock()
handlers.provide_gnocchi_url(mock_gnocchi)
mock_gnocchi.set_gnocchi_url.assert_not_called()
@mock.patch.object(handlers.reactive.flags, 'is_flag_set')
def test_provide_gnocchi_url_ha_available(self, mock_is_flag_set):
mock_is_flag_set.side_effect = [True, True]
mock_gnocchi = mock.MagicMock()
self.gnocchi_charm.public_url = "http://gnocchi:8041"
handlers.provide_gnocchi_url(mock_gnocchi)