Fix up to work with 0.6.0 of charms.reactive

charms.reactive deprecates the RelationBase.from_state() class
method.  This is an internal method in charms.reactive and shouldn't
have been used.

This patch changes charms.openstack to use
charms.relations.endpoint_from_flag(...) which essentially does the
same thing and will be a more stable API moving forwards.

Note that from this point on, the library will ONLY work with
charms.reactive 0.6.0 onwards.

Change-Id: I20b8a6978b83d9ec075817e929fa86d3a3ae828a
This commit is contained in:
Alex Kavanagh 2017-12-12 11:02:26 +00:00
parent 389d928ba5
commit 536333a47d
7 changed files with 39 additions and 27 deletions

View File

@ -20,7 +20,7 @@ import itertools
import re
import weakref
import charms.reactive as reactive
import charms.reactive.relations as relations
import charms.reactive.bus
import charmhelpers.contrib.hahelpers.cluster as ch_cluster
import charmhelpers.contrib.network.ip as ch_ip
@ -1165,7 +1165,7 @@ class OpenStackAPIRelationAdapters(OpenStackRelationAdapters):
else:
# LY: Automatically add the cluster relation if it exists and
# has not been passed through.
cluster_rel = reactive.RelationBase.from_state('cluster.connected')
cluster_rel = relations.endpoint_from_flag('cluster.connected')
if cluster_rel:
return PeerHARelationAdapter(relation=cluster_rel)
return None

View File

@ -13,7 +13,7 @@ import charmhelpers.contrib.openstack.ha as os_ha
import charmhelpers.core.hookenv as hookenv
import charmhelpers.core.host as ch_host
import charmhelpers.fetch as fetch
import charms.reactive as reactive
import charms.reactive.relations as relations
from charms_openstack.charm.core import (
BaseOpenStackCharm,
@ -631,10 +631,9 @@ class HAOpenStackCharm(OpenStackAPICharm):
@param keystone_interface KeystoneRequires class
"""
keystone_interface = (
reactive.RelationBase
.from_state('identity-service.available.ssl') or
reactive.RelationBase
.from_state('identity-service.available.ssl_legacy'))
relations.endpoint_from_flag('identity-service.available.ssl') or
relations
.endpoint_from_flag('identity-service.available.ssl_legacy'))
ssl_objects = self.get_certs_and_keys(
keystone_interface=keystone_interface)
with is_data_changed('configure_ssl.ssl_objects',
@ -654,7 +653,7 @@ class HAOpenStackCharm(OpenStackAPICharm):
self.set_state('ssl.enabled', True)
else:
self.set_state('ssl.enabled', False)
amqp_ssl = reactive.RelationBase.from_state('amqp.available.ssl')
amqp_ssl = relations.endpoint_from_flag('amqp.available.ssl')
if amqp_ssl:
self.configure_rabbit_cert(amqp_ssl)

View File

@ -15,6 +15,7 @@ import charmhelpers.core.host as ch_host
import charmhelpers.core.templating
import charmhelpers.fetch as fetch
import charms.reactive as reactive
import charms.reactive.relations as relations
import charms_openstack.adapters as os_adapters
import charms_openstack.ip as os_ip
@ -51,7 +52,7 @@ def optional_interfaces(args, *interfaces):
interfaces.
:returns: [list of reactive interfaces]
"""
return args + tuple(ri for ri in (reactive.RelationBase.from_state(i)
return args + tuple(ri for ri in (relations.endpoint_from_flag(i)
for i in interfaces)
if ri is not None)
@ -374,7 +375,7 @@ class BaseOpenStackCharm(object, metaclass=BaseOpenStackCharmMeta):
:param adapters_instance: Class which has make_adapter() method
:returns: None if the state doesn't exist, or the adapter
"""
interface = reactive.RelationBase.from_state(state)
interface = relations.endpoint_from_flag(state)
if interface is None:
return None
adapters_instance = adapters_instance or self.adapters_instance

View File

@ -83,7 +83,8 @@ class TestOpenStackCharm(BaseOpenStackCharmTest):
def test_set_state(self):
# tests that OpenStackCharm.set_state() calls set_state() global
self.patch_object(chm.reactive.bus, 'set_state')
# self.patch_object(chm.reactive.bus, 'set_state')
self.patch('charms.reactive.bus.set_state', name='set_state')
self.target.set_state('hello')
self.set_state.assert_called_once_with('hello', None)
self.set_state.reset_mock()
@ -92,7 +93,7 @@ class TestOpenStackCharm(BaseOpenStackCharmTest):
def test_remove_state(self):
# tests that OpenStackCharm.remove_state() calls remove_state() global
self.patch_object(chm.reactive.bus, 'remove_state')
self.patch('charms.reactive.bus.remove_state', name='remove_state')
self.target.remove_state('hello')
self.remove_state.assert_called_once_with('hello')
@ -571,8 +572,8 @@ class TestHAOpenStackCharm(BaseOpenStackCharmTest):
self.assertFalse(interface_mock.add_dnsha.called)
def test_set_haproxy_stat_password(self):
self.patch_object(chm.reactive.bus, 'get_state')
self.patch_object(chm.reactive.bus, 'set_state')
self.patch('charms.reactive.bus.get_state', name='get_state')
self.patch('charms.reactive.bus.set_state', name='set_state')
self.get_state.return_value = None
self.target.set_haproxy_stat_password()
self.set_state.assert_called_once_with('haproxy.stat.password',
@ -767,8 +768,8 @@ class TestHAOpenStackCharm(BaseOpenStackCharmTest):
self.patch_target('configure_apache')
self.patch_target('configure_cert')
self.patch_target('configure_ca')
self.patch_object(chm.reactive.bus, 'set_state')
self.patch_object(chm.reactive.RelationBase, 'from_state',
self.patch('charms.reactive.bus.set_state', name='set_state')
self.patch_object(chm.relations, 'endpoint_from_flag',
return_value=None)
self.patch_object(chm_core.charmhelpers.fetch,
'filter_installed_packages',
@ -796,8 +797,8 @@ class TestHAOpenStackCharm(BaseOpenStackCharmTest):
def test_configure_ssl_off(self):
self.patch_target('get_certs_and_keys', return_value=[])
self.patch_object(chm.reactive.bus, 'set_state')
self.patch_object(chm.reactive.RelationBase, 'from_state',
self.patch('charms.reactive.bus.set_state', name='set_state')
self.patch_object(chm.relations, 'endpoint_from_flag',
return_value=None)
self.patch_object(chm.os_utils, 'snap_install_requested',
return_value=False)
@ -807,8 +808,8 @@ class TestHAOpenStackCharm(BaseOpenStackCharmTest):
def test_configure_ssl_rabbit(self):
self.patch_target('get_certs_and_keys', return_value=[])
self.patch_target('configure_rabbit_cert')
self.patch_object(chm.reactive.bus, 'set_state')
self.patch_object(chm.reactive.RelationBase, 'from_state',
self.patch('charms.reactive.bus.set_state', name='set_state')
self.patch_object(chm.relations, 'endpoint_from_flag',
return_value='ssl_int')
self.patch_object(chm.os_utils, 'snap_install_requested',
return_value=False)

View File

@ -132,14 +132,12 @@ class TestFunctions(BaseOpenStackCharmTest):
self.assertIsInstance(chm_core.get_charm_instance(), self.C3)
def test_optional_interfaces(self):
self.patch_object(chm_core.reactive,
'RelationBase',
name='relation_base')
self.relation_base.from_state.side_effect = ['x', None, 'z']
self.patch_object(chm_core.relations, 'endpoint_from_flag')
self.endpoint_from_flag.side_effect = ['x', None, 'z']
r = chm_core.optional_interfaces(
('a', 'b', 'c'), 'any', 'old', 'thing')
self.assertEqual(r, ('a', 'b', 'c', 'x', 'z'))
self.relation_base.from_state.assert_has_calls(
self.endpoint_from_flag.assert_has_calls(
[mock.call('any'), mock.call('old'), mock.call('thing')])

View File

@ -1183,8 +1183,7 @@ class TestCustomOpenStackAPIRelationAdapters(unittest.TestCase):
mock.patch.object(adapters.hookenv,
'config',
new=lambda: test_config), \
mock.patch.object(adapters.reactive.RelationBase,
'from_state',
mock.patch.object(adapters.relations, 'endpoint_from_flag',
new=FakePeerHARelationAdapter), \
mock.patch.object(adapters, 'PeerHARelationAdapter',
new=FakePeerHARelationAdapter2):

View File

@ -66,3 +66,17 @@ class BaseTestCase(unittest.TestCase):
started.return_value = return_value
self._patches_start[name] = started
setattr(self, name, started)
def patch(self, item, return_value=None, name=None, new=None, **kwargs):
if name is None:
raise RuntimeError("Must pass 'name' to .patch()")
if new is not None:
mocked = mock.patch(item, new=new, **kwargs)
else:
mocked = mock.patch(item, **kwargs)
self._patches[name] = mocked
started = mocked.start()
if new is None:
started.return_value = return_value
self._patches_start[name] = started
setattr(self, name, started)