Move shared methods into shared library
This commit is contained in:
parent
5a5ac25dab
commit
feefb50616
14
.travis.yml
Normal file
14
.travis.yml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
sudo: true
|
||||||
|
dist: xenial
|
||||||
|
language: python
|
||||||
|
install: pip install tox-travis
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- name: "Python 3.6"
|
||||||
|
python: 3.6
|
||||||
|
env: ENV=pep8,py3
|
||||||
|
- name: "Python 3.7"
|
||||||
|
python: 3.7
|
||||||
|
env: ENV=pep8,py3
|
||||||
|
script:
|
||||||
|
- tox -c tox.ini -e $ENV
|
@ -27,6 +27,24 @@ import charms.reactive as reactive
|
|||||||
|
|
||||||
|
|
||||||
class OVSDB(reactive.Endpoint):
|
class OVSDB(reactive.Endpoint):
|
||||||
|
DB_NB_PORT = 6641
|
||||||
|
DB_SB_PORT = 6642
|
||||||
|
|
||||||
|
def _format_addr(self, addr):
|
||||||
|
"""Validate and format IP address
|
||||||
|
|
||||||
|
:param addr: IPv6 or IPv4 address
|
||||||
|
:type addr: str
|
||||||
|
:returns: Address string, optionally encapsulated in brackets ([])
|
||||||
|
:rtype: str
|
||||||
|
:raises: ValueError
|
||||||
|
"""
|
||||||
|
ipaddr = ipaddress.ip_address(addr)
|
||||||
|
if isinstance(ipaddr, ipaddress.IPv6Address):
|
||||||
|
fmt = '[{}]'
|
||||||
|
else:
|
||||||
|
fmt = '{}'
|
||||||
|
return fmt.format(ipaddr)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cluster_local_addr(self):
|
def cluster_local_addr(self):
|
||||||
@ -36,25 +54,59 @@ class OVSDB(reactive.Endpoint):
|
|||||||
relation_id=relation.relation_id)
|
relation_id=relation.relation_id)
|
||||||
for interface in ng_data.get('bind-addresses', []):
|
for interface in ng_data.get('bind-addresses', []):
|
||||||
for addr in interface.get('addresses', []):
|
for addr in interface.get('addresses', []):
|
||||||
return addr['address']
|
return self._format_addr(addr['address'])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cluster_addrs(self):
|
def cluster_remote_addrs(self):
|
||||||
for relation in self.relations:
|
for relation in self.relations:
|
||||||
for unit in relation.units:
|
for unit in relation.units:
|
||||||
try:
|
try:
|
||||||
addr = ipaddress.ip_address(
|
addr = self._format_addr(
|
||||||
unit.received.get('bound-address', ''))
|
unit.received.get('bound-address', ''))
|
||||||
|
yield addr
|
||||||
except ValueError:
|
except ValueError:
|
||||||
continue
|
continue
|
||||||
if isinstance(addr, ipaddress.IPv6Address):
|
|
||||||
yield '[{}]'.format(addr)
|
|
||||||
else:
|
|
||||||
yield '{}'.format(addr)
|
|
||||||
|
|
||||||
def expected_peers_available(self):
|
@property
|
||||||
|
def db_nb_port(self):
|
||||||
|
return self.DB_NB_PORT
|
||||||
|
|
||||||
|
@property
|
||||||
|
def db_sb_port(self):
|
||||||
|
return self.DB_SB_PORT
|
||||||
|
|
||||||
|
def db_connection_strs(self, addrs, port, proto='ssl'):
|
||||||
|
"""Provide connection strings
|
||||||
|
|
||||||
|
:param port: Port number
|
||||||
|
:type port: int
|
||||||
|
:param proto: Protocol
|
||||||
|
:type proto: str
|
||||||
|
:returns: List of connection strings
|
||||||
|
:rtype: Generator[str, None, None]
|
||||||
|
"""
|
||||||
|
for addr in addrs:
|
||||||
|
yield ':'.join((proto, addr, str(port)))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def db_nb_connection_strs(self):
|
||||||
|
return self.db_connection_strs(self.cluster_remote_addrs,
|
||||||
|
self.db_nb_port)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def db_sb_connection_strs(self):
|
||||||
|
return self.db_connection_strs(self.cluster_remote_addrs,
|
||||||
|
self.db_sb_port)
|
||||||
|
|
||||||
|
def expected_units_available(self):
|
||||||
|
"""Whether expected units have joined and published data on a relation
|
||||||
|
|
||||||
|
NOTE: This does not work for the peer relation, see separate method
|
||||||
|
for that in the peer relation implementation.
|
||||||
|
"""
|
||||||
if len(self.all_joined_units) == len(
|
if len(self.all_joined_units) == len(
|
||||||
list(ch_core.hookenv.expected_peer_units())):
|
list(ch_core.hookenv.expected_related_units(
|
||||||
|
self.expand_name('{endpoint_name}')))):
|
||||||
for relation in self.relations:
|
for relation in self.relations:
|
||||||
for unit in relation.units:
|
for unit in relation.units:
|
||||||
if not unit.received.get('bound-address'):
|
if not unit.received.get('bound-address'):
|
||||||
@ -66,14 +118,15 @@ class OVSDB(reactive.Endpoint):
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def publish_cluster_local_addr(self):
|
def publish_cluster_local_addr(self, addr=None):
|
||||||
"""Announce the address we bound our OVSDB Servers to.
|
"""Announce the address we bound our OVSDB Servers to.
|
||||||
|
|
||||||
This will be used by our peers and clients to build a connection
|
This will be used by our peers and clients to build a connection
|
||||||
string to the remote cluster.
|
string to the remote cluster.
|
||||||
"""
|
"""
|
||||||
for relation in self.relations:
|
for relation in self.relations:
|
||||||
relation.to_publish['bound-address'] = self.cluster_local_addr
|
relation.to_publish['bound-address'] = (
|
||||||
|
addr or self.cluster_local_addr)
|
||||||
|
|
||||||
def joined(self):
|
def joined(self):
|
||||||
ch_core.hookenv.log('{}: {} -> {}'
|
ch_core.hookenv.log('{}: {} -> {}'
|
||||||
@ -82,9 +135,6 @@ class OVSDB(reactive.Endpoint):
|
|||||||
inspect.currentframe().f_code.co_name),
|
inspect.currentframe().f_code.co_name),
|
||||||
level=ch_core.hookenv.INFO)
|
level=ch_core.hookenv.INFO)
|
||||||
reactive.set_flag(self.expand_name('{endpoint_name}.connected'))
|
reactive.set_flag(self.expand_name('{endpoint_name}.connected'))
|
||||||
self.publish_cluster_local_addr()
|
|
||||||
if self.expected_peers_available:
|
|
||||||
reactive.set_flag(self.expand_name('{endpoint_name}.available'))
|
|
||||||
|
|
||||||
def broken(self):
|
def broken(self):
|
||||||
reactive.clear_flag(self.expand_name('{endpoint_name}.available'))
|
reactive.clear_flag(self.expand_name('{endpoint_name}.available'))
|
||||||
|
@ -12,6 +12,10 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import charmhelpers.core as ch_core
|
||||||
|
|
||||||
|
import charms.reactive as reactive
|
||||||
|
|
||||||
# the reactive framework unfortunately does not grok `import as` in conjunction
|
# the reactive framework unfortunately does not grok `import as` in conjunction
|
||||||
# with decorators on class instance methods, so we have to revert to `from ...`
|
# with decorators on class instance methods, so we have to revert to `from ...`
|
||||||
# imports
|
# imports
|
||||||
@ -23,10 +27,37 @@ from .lib import ovsdb as ovsdb
|
|||||||
|
|
||||||
|
|
||||||
class OVSDBClusterPeer(ovsdb.OVSDB):
|
class OVSDBClusterPeer(ovsdb.OVSDB):
|
||||||
|
DB_NB_CLUSTER_PORT = 6643
|
||||||
|
DB_SB_CLUSTER_PORT = 6644
|
||||||
|
|
||||||
|
@property
|
||||||
|
def db_nb_cluster_port(self):
|
||||||
|
return self.DB_NB_CLUSTER_PORT
|
||||||
|
|
||||||
|
@property
|
||||||
|
def db_sb_cluster_port(self):
|
||||||
|
return self.DB_SB_CLUSTER_PORT
|
||||||
|
|
||||||
|
def expected_peers_available(self):
|
||||||
|
if len(self.all_joined_units) == len(
|
||||||
|
list(ch_core.hookenv.expected_peer_units())):
|
||||||
|
for relation in self.relations:
|
||||||
|
for unit in relation.units:
|
||||||
|
if not unit.received.get('bound-address'):
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
@when('endpoint.{endpoint_name}.joined')
|
@when('endpoint.{endpoint_name}.joined')
|
||||||
def joined(self):
|
def joined(self):
|
||||||
super().joined()
|
super().joined()
|
||||||
|
self.publish_cluster_local_addr()
|
||||||
|
if self.expected_peers_available:
|
||||||
|
reactive.set_flag(self.expand_name('{endpoint_name}.available'))
|
||||||
|
|
||||||
@when('endpoint.{endpoint_name}.broken')
|
@when('endpoint.{endpoint_name}.broken')
|
||||||
def broken(self):
|
def broken(self):
|
||||||
|
@ -36,6 +36,7 @@ class OVSDBCMSProvides(ovsdb.OVSDB):
|
|||||||
type(self).__name__,
|
type(self).__name__,
|
||||||
inspect.currentframe().f_code.co_name),
|
inspect.currentframe().f_code.co_name),
|
||||||
level=ch_core.hookenv.INFO)
|
level=ch_core.hookenv.INFO)
|
||||||
|
super().joined()
|
||||||
|
|
||||||
@when('endpoint.{endpoint_name}.broken')
|
@when('endpoint.{endpoint_name}.broken')
|
||||||
def broken(self):
|
def broken(self):
|
||||||
|
@ -20,6 +20,8 @@ import inspect
|
|||||||
|
|
||||||
import charmhelpers.core as ch_core
|
import charmhelpers.core as ch_core
|
||||||
|
|
||||||
|
import charms.reactive as reactive
|
||||||
|
|
||||||
from charms.reactive import (
|
from charms.reactive import (
|
||||||
when,
|
when,
|
||||||
)
|
)
|
||||||
@ -36,6 +38,9 @@ class OVSDBCMSRequires(ovsdb.OVSDB):
|
|||||||
type(self).__name__,
|
type(self).__name__,
|
||||||
inspect.currentframe().f_code.co_name),
|
inspect.currentframe().f_code.co_name),
|
||||||
level=ch_core.hookenv.INFO)
|
level=ch_core.hookenv.INFO)
|
||||||
|
super().joined()
|
||||||
|
if self.expected_units_available():
|
||||||
|
reactive.set_flag(self.expand_name('{endpoint_name}.available'))
|
||||||
|
|
||||||
@when('endpoint.{endpoint_name}.broken')
|
@when('endpoint.{endpoint_name}.broken')
|
||||||
def broken(self):
|
def broken(self):
|
||||||
|
@ -36,6 +36,7 @@ class OVSDBProvides(ovsdb.OVSDB):
|
|||||||
type(self).__name__,
|
type(self).__name__,
|
||||||
inspect.currentframe().f_code.co_name),
|
inspect.currentframe().f_code.co_name),
|
||||||
level=ch_core.hookenv.INFO)
|
level=ch_core.hookenv.INFO)
|
||||||
|
super().joined()
|
||||||
|
|
||||||
@when('endpoint.{endpoint_name}.broken')
|
@when('endpoint.{endpoint_name}.broken')
|
||||||
def broken(self):
|
def broken(self):
|
||||||
|
@ -20,6 +20,8 @@ import inspect
|
|||||||
|
|
||||||
import charmhelpers.core as ch_core
|
import charmhelpers.core as ch_core
|
||||||
|
|
||||||
|
import charms.reactive as reactive
|
||||||
|
|
||||||
from charms.reactive import (
|
from charms.reactive import (
|
||||||
when,
|
when,
|
||||||
)
|
)
|
||||||
@ -36,6 +38,9 @@ class OVSDBRequires(ovsdb.OVSDB):
|
|||||||
type(self).__name__,
|
type(self).__name__,
|
||||||
inspect.currentframe().f_code.co_name),
|
inspect.currentframe().f_code.co_name),
|
||||||
level=ch_core.hookenv.INFO)
|
level=ch_core.hookenv.INFO)
|
||||||
|
super().joined()
|
||||||
|
if self.expected_units_available():
|
||||||
|
reactive.set_flag(self.expand_name('{endpoint_name}.available'))
|
||||||
|
|
||||||
@when('endpoint.{endpoint_name}.broken')
|
@when('endpoint.{endpoint_name}.broken')
|
||||||
def broken(self):
|
def broken(self):
|
||||||
|
@ -22,7 +22,7 @@ import charms_openstack.test_utils as test_utils
|
|||||||
_hook_args = {}
|
_hook_args = {}
|
||||||
|
|
||||||
|
|
||||||
class TestNeutronLoadBalancerRequires(test_utils.PatchHelper):
|
class TestOVSDBLib(test_utils.PatchHelper):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
@ -83,44 +83,6 @@ class TestNeutronLoadBalancerRequires(test_utils.PatchHelper):
|
|||||||
self.network_get.assert_called_once_with(
|
self.network_get.assert_called_once_with(
|
||||||
'some-relation', relation_id='some-endpoint:42')
|
'some-relation', relation_id='some-endpoint:42')
|
||||||
|
|
||||||
def test_cluster_addrs(self):
|
|
||||||
|
|
||||||
def _assert_generator(iterable, expect):
|
|
||||||
for value in iterable:
|
|
||||||
self.assertEquals(value, expect)
|
|
||||||
|
|
||||||
relation = mock.MagicMock()
|
|
||||||
relation.relation_id = 'some-endpoint:42'
|
|
||||||
unit = mock.MagicMock()
|
|
||||||
relation.units.__iter__.return_value = [unit]
|
|
||||||
self.patch_target('_relations')
|
|
||||||
self._relations.__iter__.return_value = [relation]
|
|
||||||
unit.received.get.return_value = '127.0.0.1'
|
|
||||||
_assert_generator(self.target.cluster_addrs, '127.0.0.1')
|
|
||||||
unit.received.get.assert_called_once_with('bound-address', '')
|
|
||||||
unit.received.get.return_value = '::1'
|
|
||||||
_assert_generator(self.target.cluster_addrs, '[::1]')
|
|
||||||
|
|
||||||
def test_expected_peers_available(self):
|
|
||||||
# self.patch_target('_all_joined_units')
|
|
||||||
self.patch_object(ovsdb.ch_core.hookenv, 'expected_peer_units')
|
|
||||||
self.patch_target('_relations')
|
|
||||||
self.target._all_joined_units = ['aFakeUnit']
|
|
||||||
self.expected_peer_units.return_value = ['aFakeUnit']
|
|
||||||
relation = mock.MagicMock()
|
|
||||||
unit = mock.MagicMock()
|
|
||||||
relation.units.__iter__.return_value = [unit]
|
|
||||||
self._relations.__iter__.return_value = [relation]
|
|
||||||
unit.received.get.return_value = '127.0.0.1'
|
|
||||||
self.assertTrue(self.target.expected_peers_available())
|
|
||||||
unit.received.get.assert_called_once_with('bound-address')
|
|
||||||
unit.received.get.return_value = ''
|
|
||||||
self.assertFalse(self.target.expected_peers_available())
|
|
||||||
self.expected_peer_units.return_value = ['firstFakeUnit',
|
|
||||||
'secondFakeUnit']
|
|
||||||
unit.received.get.return_value = '127.0.0.1'
|
|
||||||
self.assertFalse(self.target.expected_peers_available())
|
|
||||||
|
|
||||||
def test_publish_cluster_local_addr(self):
|
def test_publish_cluster_local_addr(self):
|
||||||
to_publish = self.patch_topublish()
|
to_publish = self.patch_topublish()
|
||||||
self.target.publish_cluster_local_addr()
|
self.target.publish_cluster_local_addr()
|
||||||
@ -128,20 +90,8 @@ class TestNeutronLoadBalancerRequires(test_utils.PatchHelper):
|
|||||||
|
|
||||||
def test_joined(self):
|
def test_joined(self):
|
||||||
self.patch_object(ovsdb.reactive, 'set_flag')
|
self.patch_object(ovsdb.reactive, 'set_flag')
|
||||||
self.patch_target('publish_cluster_local_addr')
|
|
||||||
self.patch_target('expected_peers_available')
|
|
||||||
self.expected_peers_available.__bool__.return_value = False
|
|
||||||
self.target.joined()
|
self.target.joined()
|
||||||
self.expected_peers_available.__bool__.assert_called_once_with()
|
|
||||||
self.set_flag.assert_called_once_with('some-relation.connected')
|
self.set_flag.assert_called_once_with('some-relation.connected')
|
||||||
self.publish_cluster_local_addr.assert_called_once_with()
|
|
||||||
self.set_flag.reset_mock()
|
|
||||||
self.expected_peers_available.__bool__.return_value = True
|
|
||||||
self.target.joined()
|
|
||||||
self.set_flag.assert_has_calls([
|
|
||||||
mock.call('some-relation.connected'),
|
|
||||||
mock.call('some-relation.available'),
|
|
||||||
])
|
|
||||||
|
|
||||||
def test_broken(self):
|
def test_broken(self):
|
||||||
self.patch_object(ovsdb.reactive, 'clear_flag')
|
self.patch_object(ovsdb.reactive, 'clear_flag')
|
||||||
|
84
unit_tests/test_ovsdb_cluster_peers.py
Normal file
84
unit_tests/test_ovsdb_cluster_peers.py
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
# Copyright 2019 Canonical Ltd
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
# import mock
|
||||||
|
|
||||||
|
# from x import peers
|
||||||
|
|
||||||
|
# import charms_openstack.test_utils as test_utils
|
||||||
|
|
||||||
|
|
||||||
|
# _hook_args = {}
|
||||||
|
|
||||||
|
|
||||||
|
# class TestOVSDBPeers(test_utils.PatchHelper):
|
||||||
|
|
||||||
|
# def setUp(self):
|
||||||
|
# super().setUp()
|
||||||
|
# self.target = ovsdb.OVSDB('some-relation', [])
|
||||||
|
# self._patches = {}
|
||||||
|
# self._patches_start = {}
|
||||||
|
|
||||||
|
# def tearDown(self):
|
||||||
|
# self.target = None
|
||||||
|
# for k, v in self._patches.items():
|
||||||
|
# v.stop()
|
||||||
|
# setattr(self, k, None)
|
||||||
|
# self._patches = None
|
||||||
|
# self._patches_start = None
|
||||||
|
|
||||||
|
# def patch_target(self, attr, return_value=None):
|
||||||
|
# mocked = mock.patch.object(self.target, attr)
|
||||||
|
# self._patches[attr] = mocked
|
||||||
|
# started = mocked.start()
|
||||||
|
# started.return_value = return_value
|
||||||
|
# self._patches_start[attr] = started
|
||||||
|
# setattr(self, attr, started)
|
||||||
|
|
||||||
|
# def test_expected_peers_available(self):
|
||||||
|
# # self.patch_target('_all_joined_units')
|
||||||
|
# self.patch_object(ovsdb.ch_core.hookenv, 'expected_peer_units')
|
||||||
|
# self.patch_target('_relations')
|
||||||
|
# self.target._all_joined_units = ['aFakeUnit']
|
||||||
|
# self.expected_peer_units.return_value = ['aFakeUnit']
|
||||||
|
# relation = mock.MagicMock()
|
||||||
|
# unit = mock.MagicMock()
|
||||||
|
# relation.units.__iter__.return_value = [unit]
|
||||||
|
# self._relations.__iter__.return_value = [relation]
|
||||||
|
# unit.received.get.return_value = '127.0.0.1'
|
||||||
|
# self.assertTrue(self.target.expected_peers_available())
|
||||||
|
# unit.received.get.assert_called_once_with('bound-address')
|
||||||
|
# unit.received.get.return_value = ''
|
||||||
|
# self.assertFalse(self.target.expected_peers_available())
|
||||||
|
# self.expected_peer_units.return_value = ['firstFakeUnit',
|
||||||
|
# 'secondFakeUnit']
|
||||||
|
# unit.received.get.return_value = '127.0.0.1'
|
||||||
|
# self.assertFalse(self.target.expected_peers_available())
|
||||||
|
|
||||||
|
# def test_joined(self):
|
||||||
|
# self.patch_object(ovsdb.reactive, 'set_flag')
|
||||||
|
# self.patch_target('publish_cluster_local_addr')
|
||||||
|
# self.patch_target('expected_peers_available')
|
||||||
|
# self.expected_peers_available.__bool__.return_value = False
|
||||||
|
# self.target.joined()
|
||||||
|
# self.expected_peers_available.__bool__.assert_called_once_with()
|
||||||
|
# self.set_flag.assert_called_once_with('some-relation.connected')
|
||||||
|
# self.publish_cluster_local_addr.assert_called_once_with()
|
||||||
|
# self.set_flag.reset_mock()
|
||||||
|
# self.expected_peers_available.__bool__.return_value = True
|
||||||
|
# self.target.joined()
|
||||||
|
# self.set_flag.assert_has_calls([
|
||||||
|
# mock.call('some-relation.connected'),
|
||||||
|
# mock.call('some-relation.available'),
|
||||||
|
# ])
|
Loading…
x
Reference in New Issue
Block a user