Update charm to use bus to get charm instance
Add ``py3`` target to tox.ini for developer friendliness Update unit tests to use ``charms.openstack`` unit test helers. Change-Id: I4752d8e776491f934cd5c1232166933a9ba17746 Partial-Bug: #1837379
This commit is contained in:
parent
6a392b384f
commit
21cf26b29c
|
@ -51,71 +51,6 @@ PROVIDER_BINDING = 'provider'
|
||||||
charms_openstack.charm.use_defaults('charm.default-select-release')
|
charms_openstack.charm.use_defaults('charm.default-select-release')
|
||||||
|
|
||||||
|
|
||||||
def render_configs(interfaces_list):
|
|
||||||
"""Using a list of interfaces, render the configs and, if they have
|
|
||||||
changes, restart the services on the unit.
|
|
||||||
|
|
||||||
:returns: None
|
|
||||||
"""
|
|
||||||
|
|
||||||
try:
|
|
||||||
[i for i in interfaces_list]
|
|
||||||
except TypeError:
|
|
||||||
interfaces_list = [interfaces_list]
|
|
||||||
# TODO: Add bgp-speaker as an optional interface.
|
|
||||||
# Currently, charms.openstack does not understand endpoints and will need
|
|
||||||
# to be updated for this functionality.
|
|
||||||
DRAgentCharm.singleton.render_with_interfaces(interfaces_list)
|
|
||||||
|
|
||||||
|
|
||||||
def assess_status():
|
|
||||||
"""Just call the DRAgentCharm.singleton.assess_status() command to update
|
|
||||||
status on the unit.
|
|
||||||
|
|
||||||
:returns: None
|
|
||||||
"""
|
|
||||||
|
|
||||||
DRAgentCharm.singleton.assess_status()
|
|
||||||
|
|
||||||
|
|
||||||
def configure_ssl():
|
|
||||||
"""Setup SSL communications calling DRAgentCharm.singleton.configure_ssl()
|
|
||||||
|
|
||||||
:returns: None
|
|
||||||
"""
|
|
||||||
DRAgentCharm.singleton.configure_ssl()
|
|
||||||
|
|
||||||
|
|
||||||
def upgrade_if_available(interfaces_list):
|
|
||||||
"""Just call the DRAgentCharm.singleton.upgrade_if_available() command to
|
|
||||||
update OpenStack package if upgrade is available
|
|
||||||
|
|
||||||
@returns: None
|
|
||||||
"""
|
|
||||||
|
|
||||||
DRAgentCharm.singleton.upgrade_if_available(interfaces_list)
|
|
||||||
|
|
||||||
|
|
||||||
def bgp_speaker_bindings():
|
|
||||||
"""Return BGP speaker bindings for the bgp interface
|
|
||||||
|
|
||||||
:returns: list of bindings
|
|
||||||
"""
|
|
||||||
|
|
||||||
return [SPEAKER_BINDING]
|
|
||||||
|
|
||||||
|
|
||||||
def get_os_codename():
|
|
||||||
"""Return OpenStack Codename for installed application
|
|
||||||
|
|
||||||
:returns: OpenStack Codename
|
|
||||||
:rtype: str
|
|
||||||
"""
|
|
||||||
return DRAgentCharm.singleton.get_os_codename_package(
|
|
||||||
DRAgentCharm.singleton.release_pkg,
|
|
||||||
DRAgentCharm.singleton.package_codenames)
|
|
||||||
|
|
||||||
|
|
||||||
@os_adapters.config_property
|
@os_adapters.config_property
|
||||||
def provider_ip(cls):
|
def provider_ip(cls):
|
||||||
"""Return the provider binding network IP
|
"""Return the provider binding network IP
|
||||||
|
@ -203,6 +138,23 @@ class DRAgentCharm(charms_openstack.charm.OpenStackCharm):
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def bgp_speaker_bindings(self):
|
||||||
|
"""Return BGP speaker bindings for the bgp interface
|
||||||
|
|
||||||
|
:returns: list of bindings
|
||||||
|
"""
|
||||||
|
return [SPEAKER_BINDING]
|
||||||
|
|
||||||
|
def get_os_codename(self):
|
||||||
|
"""Return OpenStack Codename for installed application
|
||||||
|
|
||||||
|
:returns: OpenStack Codename
|
||||||
|
:rtype: str
|
||||||
|
"""
|
||||||
|
return self.get_os_codename_package(
|
||||||
|
self.release_pkg,
|
||||||
|
self.package_codenames)
|
||||||
|
|
||||||
|
|
||||||
class RockyDRAgentCharm(DRAgentCharm):
|
class RockyDRAgentCharm(DRAgentCharm):
|
||||||
|
|
||||||
|
|
|
@ -17,12 +17,11 @@ from __future__ import absolute_import
|
||||||
|
|
||||||
import charms.reactive as reactive
|
import charms.reactive as reactive
|
||||||
|
|
||||||
|
import charms_openstack.bus
|
||||||
import charms_openstack.charm as charm
|
import charms_openstack.charm as charm
|
||||||
|
|
||||||
# This charm's library contains all of the handler code associated with
|
|
||||||
# dragent -- we need to import it to get the definitions for the charm.
|
|
||||||
import charm.openstack.dragent as dragent
|
|
||||||
|
|
||||||
|
charms_openstack.bus.discover()
|
||||||
|
|
||||||
# Use the charms.openstack defaults for common states and hooks
|
# Use the charms.openstack defaults for common states and hooks
|
||||||
charm.use_defaults(
|
charm.use_defaults(
|
||||||
|
@ -36,14 +35,15 @@ charm.use_defaults(
|
||||||
def publish_bgp_info(endpoint):
|
def publish_bgp_info(endpoint):
|
||||||
"""Publish BGP information about this unit to interface-bgp peers
|
"""Publish BGP information about this unit to interface-bgp peers
|
||||||
"""
|
"""
|
||||||
if dragent.get_os_codename() in ['ocata', 'pike']:
|
with charm.provide_charm_instance() as instance:
|
||||||
use_16bit_asn = True
|
if instance.get_os_codename() in ['ocata', 'pike']:
|
||||||
else:
|
use_16bit_asn = True
|
||||||
use_16bit_asn = False
|
else:
|
||||||
endpoint.publish_info(passive=True,
|
use_16bit_asn = False
|
||||||
bindings=dragent.bgp_speaker_bindings(),
|
endpoint.publish_info(passive=True,
|
||||||
use_16bit_asn=use_16bit_asn)
|
bindings=instance.bgp_speaker_bindings(),
|
||||||
dragent.assess_status()
|
use_16bit_asn=use_16bit_asn)
|
||||||
|
instance.assess_status()
|
||||||
|
|
||||||
|
|
||||||
@reactive.when('amqp.connected')
|
@reactive.when('amqp.connected')
|
||||||
|
@ -53,12 +53,14 @@ def setup_amqp_req(amqp):
|
||||||
"""
|
"""
|
||||||
amqp.request_access(username='neutron',
|
amqp.request_access(username='neutron',
|
||||||
vhost='openstack')
|
vhost='openstack')
|
||||||
dragent.assess_status()
|
with charm.provide_charm_instance() as instance:
|
||||||
|
instance.assess_status()
|
||||||
|
|
||||||
|
|
||||||
@reactive.when('amqp.available.ssl')
|
@reactive.when('amqp.available.ssl')
|
||||||
def configure_ssl(amqp):
|
def configure_ssl(amqp):
|
||||||
dragent.configure_ssl()
|
with charm.provide_charm_instance() as instance:
|
||||||
|
instance.configure_ssl()
|
||||||
|
|
||||||
|
|
||||||
@reactive.when('amqp.available')
|
@reactive.when('amqp.available')
|
||||||
|
@ -66,6 +68,7 @@ def render_configs(*args):
|
||||||
"""Render the configuration for dynamic routing when all the interfaces are
|
"""Render the configuration for dynamic routing when all the interfaces are
|
||||||
available.
|
available.
|
||||||
"""
|
"""
|
||||||
dragent.upgrade_if_available(args)
|
with charm.provide_charm_instance() as instance:
|
||||||
dragent.render_configs(args)
|
instance.upgrade_if_available(args)
|
||||||
dragent.assess_status()
|
instance.render_with_interfaces(args)
|
||||||
|
instance.assess_status()
|
||||||
|
|
12
tox.ini
12
tox.ini
|
@ -3,7 +3,7 @@
|
||||||
# within individual charm repos.
|
# within individual charm repos.
|
||||||
[tox]
|
[tox]
|
||||||
skipsdist = True
|
skipsdist = True
|
||||||
envlist = pep8,py35
|
envlist = pep8,py3
|
||||||
skip_missing_interpreters = True
|
skip_missing_interpreters = True
|
||||||
|
|
||||||
[testenv]
|
[testenv]
|
||||||
|
@ -33,6 +33,11 @@ deps =
|
||||||
whitelist_externals = true
|
whitelist_externals = true
|
||||||
commands = true
|
commands = true
|
||||||
|
|
||||||
|
[testenv:py3]
|
||||||
|
basepython = python3
|
||||||
|
deps = -r{toxinidir}/test-requirements.txt
|
||||||
|
commands = stestr run {posargs}
|
||||||
|
|
||||||
[testenv:py35]
|
[testenv:py35]
|
||||||
basepython = python3.5
|
basepython = python3.5
|
||||||
deps = -r{toxinidir}/test-requirements.txt
|
deps = -r{toxinidir}/test-requirements.txt
|
||||||
|
@ -43,6 +48,11 @@ basepython = python3.6
|
||||||
deps = -r{toxinidir}/test-requirements.txt
|
deps = -r{toxinidir}/test-requirements.txt
|
||||||
commands = stestr run {posargs}
|
commands = stestr run {posargs}
|
||||||
|
|
||||||
|
[testenv:py37]
|
||||||
|
basepython = python3.7
|
||||||
|
deps = -r{toxinidir}/test-requirements.txt
|
||||||
|
commands = stestr run {posargs}
|
||||||
|
|
||||||
[testenv:pep8]
|
[testenv:pep8]
|
||||||
basepython = python3
|
basepython = python3
|
||||||
deps = -r{toxinidir}/test-requirements.txt
|
deps = -r{toxinidir}/test-requirements.txt
|
||||||
|
|
|
@ -15,150 +15,81 @@
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
|
import charm.openstack.dragent as dragent
|
||||||
import reactive.dragent_handlers as handlers
|
import reactive.dragent_handlers as handlers
|
||||||
|
|
||||||
|
import charms_openstack.test_utils as test_utils
|
||||||
_when_args = {}
|
|
||||||
_when_not_args = {}
|
|
||||||
|
|
||||||
|
|
||||||
def mock_hook_factory(d):
|
class TestDRAgentHooks(test_utils.TestRegisteredHooks):
|
||||||
|
|
||||||
def mock_hook(*args, **kwargs):
|
|
||||||
|
|
||||||
def inner(f):
|
|
||||||
# remember what we were passed. Note that we can't actually
|
|
||||||
# determine the class we're attached to, as the decorator only gets
|
|
||||||
# the function.
|
|
||||||
try:
|
|
||||||
d[f.__name__].append(dict(args=args, kwargs=kwargs))
|
|
||||||
except KeyError:
|
|
||||||
d[f.__name__] = [dict(args=args, kwargs=kwargs)]
|
|
||||||
return f
|
|
||||||
return inner
|
|
||||||
return mock_hook
|
|
||||||
|
|
||||||
|
|
||||||
class TestDRAgentHandlers(unittest.TestCase):
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setUpClass(cls):
|
|
||||||
cls._patched_when = mock.patch('charms.reactive.when',
|
|
||||||
mock_hook_factory(_when_args))
|
|
||||||
cls._patched_when_started = cls._patched_when.start()
|
|
||||||
cls._patched_when_not = mock.patch('charms.reactive.when_not',
|
|
||||||
mock_hook_factory(_when_not_args))
|
|
||||||
cls._patched_when_not_started = cls._patched_when_not.start()
|
|
||||||
# force requires to rerun the mock_hook decorator:
|
|
||||||
# try except is Python2/Python3 compatibility as Python3 has moved
|
|
||||||
# reload to importlib.
|
|
||||||
try:
|
|
||||||
reload(handlers)
|
|
||||||
except NameError:
|
|
||||||
import importlib
|
|
||||||
importlib.reload(handlers)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def tearDownClass(cls):
|
|
||||||
cls._patched_when.stop()
|
|
||||||
cls._patched_when_started = None
|
|
||||||
cls._patched_when = None
|
|
||||||
cls._patched_when_not.stop()
|
|
||||||
cls._patched_when_not_started = None
|
|
||||||
cls._patched_when_not = None
|
|
||||||
# and fix any breakage we did to the module
|
|
||||||
try:
|
|
||||||
reload(handlers)
|
|
||||||
except NameError:
|
|
||||||
import importlib
|
|
||||||
importlib.reload(handlers)
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self._patches = {}
|
|
||||||
self._patches_start = {}
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
for k, v in self._patches.items():
|
|
||||||
v.stop()
|
|
||||||
setattr(self, k, None)
|
|
||||||
self._patches = None
|
|
||||||
self._patches_start = None
|
|
||||||
|
|
||||||
def patch(self, obj, attr, return_value=None, side_effect=None):
|
|
||||||
mocked = mock.patch.object(obj, attr)
|
|
||||||
self._patches[attr] = mocked
|
|
||||||
started = mocked.start()
|
|
||||||
started.return_value = return_value
|
|
||||||
started.side_effect = side_effect
|
|
||||||
self._patches_start[attr] = started
|
|
||||||
setattr(self, attr, started)
|
|
||||||
|
|
||||||
def test_registered_hooks(self):
|
def test_registered_hooks(self):
|
||||||
# test that the hooks actually registered the relation expressions that
|
# test that the hooks actually registered the relation expressions that
|
||||||
# are meaningful for this interface: this is to handle regressions.
|
# are meaningful for this interface: this is to handle regressions.
|
||||||
# The keys are the function names that the hook attaches to.
|
# The keys are the function names that the hook attaches to.
|
||||||
when_patterns = {
|
defaults = [
|
||||||
'publish_bgp_info': ('endpoint.bgp-speaker.changed',),
|
'charm.installed',
|
||||||
'setup_amqp_req': ('amqp.connected', ),
|
'config.changed',
|
||||||
'render_configs': ('amqp.available', ),
|
'update-status',
|
||||||
'configure_ssl': ('amqp.available.ssl', ),
|
'upgrade-charm',
|
||||||
|
]
|
||||||
|
hook_set = {
|
||||||
|
'when': {
|
||||||
|
'publish_bgp_info': ('endpoint.bgp-speaker.changed',),
|
||||||
|
'setup_amqp_req': ('amqp.connected', ),
|
||||||
|
'render_configs': ('amqp.available', ),
|
||||||
|
'configure_ssl': ('amqp.available.ssl', ),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
when_not_patterns = {}
|
self.registered_hooks_test_helper(handlers, hook_set, defaults)
|
||||||
# check the when hooks are attached to the expected functions
|
|
||||||
for t, p in [(_when_args, when_patterns),
|
|
||||||
(_when_not_args, when_not_patterns)]:
|
class TestDRAgentHandlers(test_utils.PatchHelper):
|
||||||
for f, args in t.items():
|
|
||||||
# check that function is in patterns
|
def setUp(self):
|
||||||
self.assertTrue(f in p.keys(),
|
super().setUp()
|
||||||
"{} not found".format(f))
|
self.patch_release(dragent.DRAgentCharm.release)
|
||||||
# check that the lists are equal
|
self.dragent_charm = mock.MagicMock()
|
||||||
li = []
|
self.patch_object(handlers.charm, 'provide_charm_instance',
|
||||||
for a in args:
|
new=mock.MagicMock())
|
||||||
li += a['args'][:]
|
self.provide_charm_instance().__enter__.return_value = \
|
||||||
self.assertEqual(sorted(li), sorted(p[f]),
|
self.dragent_charm
|
||||||
"{}: incorrect state registration".format(f))
|
self.provide_charm_instance().__exit__.return_value = None
|
||||||
|
|
||||||
def test_publish_bgp_info(self):
|
def test_publish_bgp_info(self):
|
||||||
_bindings = ['bgp-speaker']
|
_bindings = ['bgp-speaker']
|
||||||
self.patch(handlers.dragent, 'assess_status')
|
|
||||||
self.patch(handlers.dragent, 'get_os_codename')
|
|
||||||
bgp = mock.MagicMock()
|
bgp = mock.MagicMock()
|
||||||
self.get_os_codename.return_value = 'queens'
|
self.dragent_charm.bgp_speaker_bindings.return_value = _bindings
|
||||||
handlers.publish_bgp_info(bgp)
|
handlers.publish_bgp_info(bgp)
|
||||||
self.get_os_codename.assert_called()
|
self.dragent_charm.get_os_codename.assert_called()
|
||||||
bgp.publish_info.assert_called_once_with(passive=True,
|
bgp.publish_info.assert_called_once_with(passive=True,
|
||||||
bindings=_bindings,
|
bindings=_bindings,
|
||||||
use_16bit_asn=False)
|
use_16bit_asn=False)
|
||||||
|
|
||||||
def test_publish_bgp_info_pike(self):
|
def test_publish_bgp_info_pike(self):
|
||||||
_bindings = ['bgp-speaker']
|
_bindings = ['bgp-speaker']
|
||||||
self.patch(handlers.dragent, 'assess_status')
|
|
||||||
self.patch(handlers.dragent, 'get_os_codename')
|
|
||||||
bgp = mock.MagicMock()
|
bgp = mock.MagicMock()
|
||||||
self.get_os_codename.return_value = 'pike'
|
self.dragent_charm.get_os_codename.return_value = 'pike'
|
||||||
|
self.dragent_charm.bgp_speaker_bindings.return_value = _bindings
|
||||||
handlers.publish_bgp_info(bgp)
|
handlers.publish_bgp_info(bgp)
|
||||||
self.get_os_codename.assert_called()
|
self.dragent_charm.get_os_codename.assert_called()
|
||||||
bgp.publish_info.assert_called_once_with(passive=True,
|
bgp.publish_info.assert_called_once_with(passive=True,
|
||||||
bindings=_bindings,
|
bindings=_bindings,
|
||||||
use_16bit_asn=True)
|
use_16bit_asn=True)
|
||||||
|
|
||||||
def test_setup_amqp_req(self):
|
def test_setup_amqp_req(self):
|
||||||
self.patch(handlers.dragent, 'assess_status')
|
|
||||||
amqp = mock.MagicMock()
|
amqp = mock.MagicMock()
|
||||||
handlers.setup_amqp_req(amqp)
|
handlers.setup_amqp_req(amqp)
|
||||||
amqp.request_access.assert_called_once_with(
|
amqp.request_access.assert_called_once_with(
|
||||||
username='neutron', vhost='openstack')
|
username='neutron', vhost='openstack')
|
||||||
|
|
||||||
def test_render_configs(self):
|
def test_render_configs(self):
|
||||||
self.patch(handlers.dragent, 'render_configs')
|
self.patch_object(handlers.reactive, 'set_flag')
|
||||||
self.patch(handlers.dragent, 'assess_status')
|
|
||||||
self.patch(handlers.dragent, 'upgrade_if_available')
|
|
||||||
amqp = mock.MagicMock()
|
amqp = mock.MagicMock()
|
||||||
handlers.render_configs(amqp)
|
handlers.render_configs(amqp)
|
||||||
self.upgrade_if_available.assert_called_once_with((amqp,))
|
self.dragent_charm.upgrade_if_available.assert_called_once_with(
|
||||||
self.render_configs.assert_called_once_with((amqp,))
|
(amqp,))
|
||||||
self.assess_status.assert_called_once()
|
self.dragent_charm.render_with_interfaces.assert_called_once_with(
|
||||||
|
(amqp,))
|
||||||
|
self.dragent_charm.assess_status.assert_called_once()
|
||||||
|
|
|
@ -31,17 +31,6 @@ class Helper(test_utils.PatchHelper):
|
||||||
|
|
||||||
class TestOpenStackDRAgent(Helper):
|
class TestOpenStackDRAgent(Helper):
|
||||||
|
|
||||||
def test_render_configs(self):
|
|
||||||
self.patch_object(dragent.DRAgentCharm.singleton,
|
|
||||||
"render_with_interfaces")
|
|
||||||
dragent.render_configs("interfaces-list")
|
|
||||||
self.render_with_interfaces.assert_called_once_with(
|
|
||||||
"interfaces-list")
|
|
||||||
|
|
||||||
def test_bgp_speaker_bindings(self):
|
|
||||||
self.assertEqual(dragent.bgp_speaker_bindings(),
|
|
||||||
[self.SPEAKER_BINDING])
|
|
||||||
|
|
||||||
def get_os_codename(self):
|
def get_os_codename(self):
|
||||||
self.patch_object(dragent.DRAgentCharm.singleton,
|
self.patch_object(dragent.DRAgentCharm.singleton,
|
||||||
"get_os_codename_package")
|
"get_os_codename_package")
|
||||||
|
@ -78,3 +67,8 @@ class TestDRAgentCharm(Helper):
|
||||||
dra.install()
|
dra.install()
|
||||||
self.configure_source.assert_called_once_with()
|
self.configure_source.assert_called_once_with()
|
||||||
self.install.assert_called_once_with()
|
self.install.assert_called_once_with()
|
||||||
|
|
||||||
|
def test_bgp_speaker_bindings(self):
|
||||||
|
dra = dragent.DRAgentCharm()
|
||||||
|
self.assertEqual(dra.bgp_speaker_bindings(),
|
||||||
|
[dragent.SPEAKER_BINDING])
|
||||||
|
|
Loading…
Reference in New Issue