black unit test definitions

This commit is contained in:
James Page 2020-03-06 10:52:41 +00:00
parent ea123a8227
commit 08f890fa7b
3 changed files with 56 additions and 49 deletions

View File

@ -1,8 +1,9 @@
import sys
sys.path.append('src')
sys.path.append('src/lib')
sys.path.append("src")
sys.path.append("src/lib")
# Mock out charmhelpers so that we can test without it.
import charms_openstack.test_mocks # noqa
charms_openstack.test_mocks.mock_charmhelpers()

View File

@ -16,53 +16,53 @@ import charms_openstack.test_utils as test_utils
class Helper(test_utils.PatchHelper):
def setUp(self):
super().setUp()
self.patch_release(dmapi.DmapiCharm.release)
class TestDmapiDBAdapter(Helper):
def fake_get_uri(self, prefix):
return 'mysql://uri/{}-database'.format(prefix)
return "mysql://uri/{}-database".format(prefix)
def test_dmapi_uri(self):
relation = mock.MagicMock()
a = dmapi.DmapiDBAdapter(relation)
self.patch_object(dmapi.DmapiDBAdapter, 'get_uri')
self.patch_object(dmapi.DmapiDBAdapter, "get_uri")
self.get_uri.side_effect = self.fake_get_uri
self.assertEqual(a.dmapi_nova_uri, 'mysql://uri/dmapinova-database')
self.assertEqual(a.dmapi_nova_api_uri, 'mysql://uri/dmapinovaapi-database')
self.assertEqual(a.dmapi_nova_uri, "mysql://uri/dmapinova-database")
self.assertEqual(
a.dmapi_nova_api_uri, "mysql://uri/dmapinovaapi-database"
)
class TestDmapiAdapters(Helper):
@mock.patch.object(dmapi, 'hookenv')
@mock.patch.object(dmapi, "hookenv")
def test_dmapi_adapters(self, hookenv):
reply = {
'keystone-api-version': '3',
}
reply = {"keystone-api-version": "3"}
hookenv.config.side_effect = lambda: reply
self.patch_object(
dmapi.adapters.APIConfigurationAdapter,
'get_network_addresses')
dmapi.adapters.APIConfigurationAdapter, "get_network_addresses"
)
cluster_relation = mock.MagicMock()
cluster_relation.endpoint_name = 'cluster'
cluster_relation.endpoint_name = "cluster"
amqp_relation = mock.MagicMock()
amqp_relation.endpoint_name = 'amqp'
amqp_relation.endpoint_name = "amqp"
shared_db_relation = mock.MagicMock()
shared_db_relation.endpoint_name = 'shared_db'
shared_db_relation.endpoint_name = "shared_db"
other_relation = mock.MagicMock()
other_relation.endpoint_name = 'other'
other_relation.thingy = 'help'
other_relation.endpoint_name = "other"
other_relation.thingy = "help"
# verify that the class is created with a DmapiConfigurationAdapter
b = dmapi.DmapiAdapters([amqp_relation,
cluster_relation,
shared_db_relation,
other_relation])
b = dmapi.DmapiAdapters(
[
amqp_relation,
cluster_relation,
shared_db_relation,
other_relation,
]
)
# ensure that the relevant things got put on.
self.assertTrue(
isinstance(
b.other,
dmapi.adapters.OpenStackRelationAdapter))
isinstance(b.other, dmapi.adapters.OpenStackRelationAdapter)
)

View File

@ -20,9 +20,7 @@ _when_not_args = {}
def mock_hook_factory(d):
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
@ -32,19 +30,22 @@ def mock_hook_factory(d):
except KeyError:
d[f.__name__] = [dict(args=args, kwargs=kwargs)]
return f
return inner
return mock_hook
class TestDmapiHandlers(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls._patched_when = mock.patch('charms.reactive.when',
mock_hook_factory(_when_args))
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 = 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
@ -53,6 +54,7 @@ class TestDmapiHandlers(unittest.TestCase):
reload(handlers)
except NameError:
import importlib
importlib.reload(handlers)
@classmethod
@ -68,6 +70,7 @@ class TestDmapiHandlers(unittest.TestCase):
reload(handlers)
except NameError:
import importlib
importlib.reload(handlers)
def setUp(self):
@ -90,35 +93,38 @@ class TestDmapiHandlers(unittest.TestCase):
self._patches_start[attr] = started
setattr(self, attr, started)
def test_registered_hooks(self):
# test that the hooks actually registered the relation expressions that
# are meaningful for this interface: this is to handle regressions.
# The keys are the function names that the hook attaches to.
when_patterns = {
'render_config': (
'shared-db.available',
'identity-service.available',
'amqp.available'
"render_config": (
"shared-db.available",
"identity-service.available",
"amqp.available",
),
'init_db': ('config.rendered', ),
'cluster_connected': ('ha.connected', ),
"init_db": ("config.rendered",),
"cluster_connected": ("ha.connected",),
}
when_not_patterns = {}
# check the when hooks are attached to the expected functions
for t, p in [(_when_args, when_patterns),
(_when_not_args, when_not_patterns)]:
for t, p in [
(_when_args, when_patterns),
(_when_not_args, when_not_patterns),
]:
for f, args in t.items():
# check that function is in patterns
self.assertTrue(f in p.keys(),
"{} not found".format(f))
self.assertTrue(f in p.keys(), "{} not found".format(f))
# check that the lists are equal
l = []
for a in args:
l += a['args'][:]
self.assertEqual(sorted(l), sorted(p[f]),
"{}: incorrect state registration".format(f))
l += a["args"][:]
self.assertEqual(
sorted(l),
sorted(p[f]),
"{}: incorrect state registration".format(f),
)
def test_render(self):
pass
#handlers.render_config('args')
# handlers.render_config('args')