Add unit tests
Interface can now have unit tests so I have added them. Change-Id: I6fba1ae6cb428c7f73446d051bf05657aba57ba2
This commit is contained in:
parent
7cfed26220
commit
e43d82e1f8
8
.testr.conf
Normal file
8
.testr.conf
Normal file
@ -0,0 +1,8 @@
|
||||
[DEFAULT]
|
||||
test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \
|
||||
OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \
|
||||
OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-60} \
|
||||
${PYTHON:-python} -m subunit.run discover -t ./ ./unit_tests $LISTOPT $IDOPTION
|
||||
|
||||
test_id_option=--load-list $IDFILE
|
||||
test_list_option=--list
|
@ -1,3 +1,4 @@
|
||||
- project:
|
||||
templates:
|
||||
- python-charm-interface-jobs
|
||||
|
||||
|
@ -1,2 +1,7 @@
|
||||
flake8>=2.2.4,<=2.4.1
|
||||
os-testr>=0.4.1
|
||||
# Lint and unit test requirements
|
||||
flake8
|
||||
os-testr>=0.4.1
|
||||
charms.reactive
|
||||
mock>=1.2
|
||||
coverage>=3.6
|
||||
netifaces
|
||||
|
21
tox.ini
21
tox.ini
@ -1,5 +1,5 @@
|
||||
[tox]
|
||||
envlist = pep8,py27,py34,py35
|
||||
envlist = pep8,py35
|
||||
skipsdist = True
|
||||
skip_missing_interpreters = True
|
||||
|
||||
@ -10,20 +10,6 @@ install_command =
|
||||
pip install {opts} {packages}
|
||||
commands = ostestr {posargs}
|
||||
|
||||
[testenv:py27]
|
||||
basepython = python2.7
|
||||
deps = -r{toxinidir}/test-requirements.txt
|
||||
# TODO: Need to write unit tests then remove the following command.
|
||||
# https://github.com/juju/charm-tools/issues/249
|
||||
commands = /bin/true
|
||||
|
||||
[testenv:py34]
|
||||
basepython = python3.4
|
||||
deps = -r{toxinidir}/test-requirements.txt
|
||||
# TODO: Need to write unit tests then remove the following command.
|
||||
# https://github.com/juju/charm-tools/issues/249
|
||||
commands = /bin/true
|
||||
|
||||
[testenv:py35]
|
||||
basepython = python3.5
|
||||
deps = -r{toxinidir}/test-requirements.txt
|
||||
@ -31,6 +17,11 @@ deps = -r{toxinidir}/test-requirements.txt
|
||||
# https://github.com/juju/charm-tools/issues/249
|
||||
commands = /bin/true
|
||||
|
||||
[testenv:py36]
|
||||
basepython = python3.6
|
||||
deps = -r{toxinidir}/test-requirements.txt
|
||||
commands = ostestr {posargs}
|
||||
|
||||
[testenv:pep8]
|
||||
basepython = python3
|
||||
deps = -r{toxinidir}/test-requirements.txt
|
||||
|
0
unit_tests/__init__.py
Normal file
0
unit_tests/__init__.py
Normal file
196
unit_tests/test_provides.py
Normal file
196
unit_tests/test_provides.py
Normal file
@ -0,0 +1,196 @@
|
||||
# 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 unittest
|
||||
import mock
|
||||
|
||||
|
||||
with mock.patch('charmhelpers.core.hookenv.metadata') as _meta:
|
||||
_meta.return_Value = 'ss'
|
||||
import provides
|
||||
|
||||
_hook_args = {}
|
||||
|
||||
TO_PATCH = [
|
||||
'relation_set',
|
||||
]
|
||||
|
||||
|
||||
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.
|
||||
_hook_args[f.__name__] = dict(args=args, kwargs=kwargs)
|
||||
return f
|
||||
return inner
|
||||
|
||||
|
||||
class TestCephClientProvider(unittest.TestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls._patched_hook = mock.patch('charms.reactive.when', mock_hook)
|
||||
cls._patched_hook_started = cls._patched_hook.start()
|
||||
# force requires to rerun the mock_hook decorator:
|
||||
# try except is Python2/Python3 compatibility as Python3 has moved
|
||||
# reload to importlib.
|
||||
try:
|
||||
reload(provides)
|
||||
except NameError:
|
||||
import importlib
|
||||
importlib.reload(provides)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls._patched_hook.stop()
|
||||
cls._patched_hook_started = None
|
||||
cls._patched_hook = None
|
||||
# and fix any breakage we did to the module
|
||||
try:
|
||||
reload(provides)
|
||||
except NameError:
|
||||
import importlib
|
||||
importlib.reload(provides)
|
||||
|
||||
def patch(self, method):
|
||||
_m = mock.patch.object(self.obj, method)
|
||||
_mock = _m.start()
|
||||
self.addCleanup(_m.stop)
|
||||
return _mock
|
||||
|
||||
def setUp(self):
|
||||
self.cr = provides.CephClientProvider('some-relation', [])
|
||||
self._patches = {}
|
||||
self._patches_start = {}
|
||||
self.obj = provides
|
||||
for method in TO_PATCH:
|
||||
setattr(self, method, self.patch(method))
|
||||
|
||||
def tearDown(self):
|
||||
self.cr = None
|
||||
for k, v in self._patches.items():
|
||||
v.stop()
|
||||
setattr(self, k, None)
|
||||
self._patches = None
|
||||
self._patches_start = None
|
||||
|
||||
def patch_kr(self, attr, return_value=None):
|
||||
mocked = mock.patch.object(self.cr, attr)
|
||||
self._patches[attr] = mocked
|
||||
started = mocked.start()
|
||||
started.return_value = return_value
|
||||
self._patches_start[attr] = started
|
||||
setattr(self, attr, started)
|
||||
|
||||
def test_registered_hooks(self):
|
||||
# test that the decorators 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.
|
||||
hook_patterns = {
|
||||
'data_changed': ('endpoint.{endpoint_name}.changed', ),
|
||||
'joined': ('endpoint.{endpoint_name}.joined', ),
|
||||
'broken': ('endpoint.{endpoint_name}.joined', ),
|
||||
}
|
||||
for k, v in _hook_args.items():
|
||||
self.assertEqual(hook_patterns[k], v['args'])
|
||||
|
||||
def test_changed(self):
|
||||
conv1 = mock.MagicMock()
|
||||
conv1.get_remote.return_value = True
|
||||
self.patch_kr('conversation', conv1)
|
||||
self.patch_kr('set_state')
|
||||
self.cr.changed()
|
||||
self.set_state.assert_has_calls([
|
||||
mock.call('{relation_name}.connected'),
|
||||
mock.call('{relation_name}.broker_requested')])
|
||||
|
||||
def test_changed_no_request(self):
|
||||
conv1 = mock.MagicMock()
|
||||
conv1.get_remote.return_value = None
|
||||
self.patch_kr('conversation', conv1)
|
||||
self.patch_kr('set_state')
|
||||
self.cr.changed()
|
||||
self.set_state.assert_called_once_with('{relation_name}.connected')
|
||||
|
||||
def test_provide_auth(self):
|
||||
conv1 = mock.MagicMock()
|
||||
conv1.get_remote.return_value = None
|
||||
conv1.namespace = 'ns1'
|
||||
self.patch_kr('conversation', conv1)
|
||||
self.cr.provide_auth(
|
||||
'svc1',
|
||||
'secret',
|
||||
'ssl1.0',
|
||||
'10.0.0.10')
|
||||
expect = {
|
||||
'auth': 'ssl1.0',
|
||||
'ceph-public-address': '10.0.0.10'}
|
||||
conv1.set_remote.assert_called_once_with(**expect)
|
||||
self.relation_set.assert_called_once_with(
|
||||
relation_id='ns1',
|
||||
relation_settings={'key': 'secret'})
|
||||
|
||||
def test_requested_keys(self):
|
||||
conv1 = mock.MagicMock()
|
||||
conv1.scope = 'svc1'
|
||||
conv2 = mock.MagicMock()
|
||||
conv2.scope = 'svc2'
|
||||
self.patch_kr('conversations', [conv1, conv2])
|
||||
self.patch_kr('requested_key')
|
||||
keys = {
|
||||
'svc1': 'key'}
|
||||
self.requested_key.side_effect = lambda x: keys.get(x)
|
||||
self.assertEqual(
|
||||
list(self.cr.requested_keys()),
|
||||
['svc2'])
|
||||
|
||||
def test_requested_key(self):
|
||||
conv1 = mock.MagicMock()
|
||||
self.patch_kr('conversation', conv1)
|
||||
self.cr.requested_key('svc1')
|
||||
self.conversation.assert_called_once_with(scope='svc1')
|
||||
self.conversation(scope='svc1').get_remote.assert_called_once_with(
|
||||
'key')
|
||||
|
||||
def test_provide_broker_token(self):
|
||||
conv1 = mock.MagicMock()
|
||||
self.patch_kr('conversation', conv1)
|
||||
self.cr.provide_broker_token('svc1', 'urkey', 'token1')
|
||||
conv1.set_remote.assert_called_once_with(
|
||||
broker_rsp='token1',
|
||||
urkey='token1')
|
||||
|
||||
def test_requested_tokens(self):
|
||||
conv1 = mock.MagicMock()
|
||||
conv1.scope = 'svc1'
|
||||
conv2 = mock.MagicMock()
|
||||
conv2.scope = 'svc2'
|
||||
self.patch_kr('conversations', [conv1, conv2])
|
||||
tokens = {
|
||||
'svc1': 'token1',
|
||||
'svc2': 'token2'}
|
||||
self.patch_kr('requested_token')
|
||||
self.requested_token.side_effect = lambda x: tokens.get(x)
|
||||
self.assertEqual(
|
||||
list(self.cr.requested_tokens()),
|
||||
[('svc1', 'token1'), ('svc2', 'token2')])
|
||||
|
||||
def test_requested_token(self):
|
||||
conv1 = mock.MagicMock()
|
||||
self.patch_kr('conversation', conv1)
|
||||
self.cr.requested_token('svc1')
|
||||
self.conversation.assert_called_once_with(scope='svc1')
|
||||
self.conversation(scope='svc1').get_remote.assert_called_once_with(
|
||||
'broker_req')
|
269
unit_tests/test_requires.py
Normal file
269
unit_tests/test_requires.py
Normal file
@ -0,0 +1,269 @@
|
||||
# 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 unittest
|
||||
import mock
|
||||
|
||||
|
||||
with mock.patch('charmhelpers.core.hookenv.metadata') as _meta:
|
||||
_meta.return_Value = 'ss'
|
||||
import requires
|
||||
|
||||
import charmhelpers
|
||||
|
||||
|
||||
_hook_args = {}
|
||||
|
||||
TO_PATCH = [
|
||||
'is_request_complete',
|
||||
'send_request_if_needed',
|
||||
]
|
||||
|
||||
|
||||
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.
|
||||
_hook_args[f.__name__] = dict(args=args, kwargs=kwargs)
|
||||
return f
|
||||
return inner
|
||||
|
||||
|
||||
class TestCephClientRequires(unittest.TestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls._patched_hook = mock.patch('charms.reactive.when', mock_hook)
|
||||
cls._patched_hook_started = cls._patched_hook.start()
|
||||
# force requires to rerun the mock_hook decorator:
|
||||
# try except is Python2/Python3 compatibility as Python3 has moved
|
||||
# reload to importlib.
|
||||
try:
|
||||
reload(requires)
|
||||
except NameError:
|
||||
import importlib
|
||||
importlib.reload(requires)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls._patched_hook.stop()
|
||||
cls._patched_hook_started = None
|
||||
cls._patched_hook = None
|
||||
# and fix any breakage we did to the module
|
||||
try:
|
||||
reload(requires)
|
||||
except NameError:
|
||||
import importlib
|
||||
importlib.reload(requires)
|
||||
|
||||
def patch(self, method):
|
||||
_m = mock.patch.object(self.obj, method)
|
||||
_mock = _m.start()
|
||||
self.addCleanup(_m.stop)
|
||||
return _mock
|
||||
|
||||
def setUp(self):
|
||||
self.cr = requires.CephClientRequires('some-relation', [])
|
||||
self._patches = {}
|
||||
self._patches_start = {}
|
||||
self.obj = requires
|
||||
for method in TO_PATCH:
|
||||
setattr(self, method, self.patch(method))
|
||||
|
||||
def tearDown(self):
|
||||
self.cr = None
|
||||
for k, v in self._patches.items():
|
||||
v.stop()
|
||||
setattr(self, k, None)
|
||||
self._patches = None
|
||||
self._patches_start = None
|
||||
|
||||
def patch_kr(self, attr, return_value=None):
|
||||
mocked = mock.patch.object(self.cr, attr)
|
||||
self._patches[attr] = mocked
|
||||
started = mocked.start()
|
||||
started.return_value = return_value
|
||||
self._patches_start[attr] = started
|
||||
setattr(self, attr, started)
|
||||
|
||||
def test_registered_hooks(self):
|
||||
# test that the decorators 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.
|
||||
hook_patterns = {
|
||||
'data_changed': ('endpoint.{endpoint_name}.changed', ),
|
||||
'joined': ('endpoint.{endpoint_name}.joined', ),
|
||||
'broken': ('endpoint.{endpoint_name}.joined', ),
|
||||
}
|
||||
for k, v in _hook_args.items():
|
||||
self.assertEqual(hook_patterns[k], v['args'])
|
||||
|
||||
def test_date_changed(self):
|
||||
self.patch_kr('key', 'key1')
|
||||
self.patch_kr('auth', 'auth1')
|
||||
self.patch_kr('mon_hosts', 'host1')
|
||||
self.patch_kr('get_local', None)
|
||||
self.patch_kr('set_state')
|
||||
self.cr.changed()
|
||||
self.set_state.assert_called_once_with('{relation_name}.available')
|
||||
|
||||
def test_date_changed_incomplete(self):
|
||||
self.patch_kr('key', 'key1')
|
||||
self.patch_kr('auth', None)
|
||||
self.patch_kr('mon_hosts', 'host1')
|
||||
self.patch_kr('get_local', None)
|
||||
self.patch_kr('set_state')
|
||||
self.cr.changed()
|
||||
self.assertFalse(self.set_state.called)
|
||||
|
||||
def test_date_changed_existing_broker_rq(self):
|
||||
broker_req = (
|
||||
'{"api-version": 1, '
|
||||
'"request-id": "4f7e247d-f953-11e8-a4f3-fa163e55565e",'
|
||||
'"ops": [{"group": "volumes", "name": "cinder-ceph", '
|
||||
'"weight": 40, "replicas": 3, "pg_num": null, '
|
||||
'"group-namespace": null, "op": "create-pool"}]}')
|
||||
self.patch_kr('key', 'key1')
|
||||
self.patch_kr('auth', 'auth1')
|
||||
self.patch_kr('mon_hosts', 'host1')
|
||||
self.patch_kr('get_local', broker_req)
|
||||
self.patch_kr('set_state')
|
||||
self.is_request_complete.return_value = True
|
||||
self.cr.changed()
|
||||
self.set_state.assert_has_calls([
|
||||
mock.call('{relation_name}.available'),
|
||||
mock.call('{relation_name}.pools.available')])
|
||||
|
||||
def test_date_changed_existing_broker_rq_incomplete(self):
|
||||
broker_req = (
|
||||
'{"api-version": 1, '
|
||||
'"request-id": "4f7e247d-f953-11e8-a4f3-fa163e55565e",'
|
||||
'"ops": [{"group": "volumes", "name": "cinder-ceph", '
|
||||
'"weight": 40, "replicas": 3, "pg_num": null, '
|
||||
'"group-namespace": null, "op": "create-pool"}]}')
|
||||
self.patch_kr('key', 'key1')
|
||||
self.patch_kr('auth', 'auth1')
|
||||
self.patch_kr('mon_hosts', 'host1')
|
||||
self.patch_kr('get_local', broker_req)
|
||||
self.patch_kr('set_state')
|
||||
self.is_request_complete.return_value = False
|
||||
self.cr.changed()
|
||||
# Side effect of asserting pools.available was not set.
|
||||
self.set_state.assert_called_once_with('{relation_name}.available')
|
||||
|
||||
def test_broken(self):
|
||||
self.patch_kr('remove_state')
|
||||
self.cr.broken()
|
||||
self.remove_state.assert_has_calls([
|
||||
mock.call('{relation_name}.available'),
|
||||
mock.call('{relation_name}.connected'),
|
||||
mock.call('{relation_name}.pools.available')])
|
||||
|
||||
@mock.patch.object(charmhelpers.contrib.storage.linux.ceph.uuid, 'uuid1')
|
||||
def test_create_pool_new_request(self, _uuid1):
|
||||
_uuid1.return_value = '9e34123e-fa0c-11e8-ad9c-fa163ed1cc55'
|
||||
req = (
|
||||
'{"api-version": 1, '
|
||||
'"ops": [{"op": "create-pool", "name": "bob", "replicas": 3, '
|
||||
'"pg_num": null, "weight": null, "group": null, '
|
||||
'"group-namespace": null}], '
|
||||
'"request-id": "9e34123e-fa0c-11e8-ad9c-fa163ed1cc55"}')
|
||||
self.patch_kr('get_local', None)
|
||||
self.patch_kr('set_local')
|
||||
self.cr.create_pool('bob')
|
||||
self.set_local.assert_called_once_with(key='broker_req', value=req)
|
||||
ceph_broker_rq = self.send_request_if_needed.mock_calls[0][1][0]
|
||||
self.assertEqual(
|
||||
ceph_broker_rq.ops,
|
||||
[{
|
||||
'op': 'create-pool',
|
||||
'name': 'bob',
|
||||
'replicas': 3,
|
||||
'group': None,
|
||||
'group-namespace': None,
|
||||
'pg_num': None,
|
||||
'weight': None}])
|
||||
|
||||
@mock.patch.object(charmhelpers.contrib.storage.linux.ceph.uuid, 'uuid1')
|
||||
def test_create_pool_existing_request(self, _uuid1):
|
||||
_uuid1.return_value = '9e34123e-fa0c-11e8-ad9c-fa163ed1cc55'
|
||||
req = (
|
||||
'{"api-version": 1, '
|
||||
'"ops": [{"op": "create-pool", "name": "bob", "replicas": 3, '
|
||||
'"pg_num": null, "weight": null, "group": null, '
|
||||
'"group-namespace": null}], '
|
||||
'"request-id": "9e34123e-fa0c-11e8-ad9c-fa163ed1cc55"}')
|
||||
self.patch_kr('get_local', req)
|
||||
self.cr.create_pool('bob')
|
||||
ceph_broker_rq = self.send_request_if_needed.mock_calls[0][1][0]
|
||||
self.assertEqual(
|
||||
ceph_broker_rq.ops,
|
||||
[{
|
||||
'op': 'create-pool',
|
||||
'name': 'bob',
|
||||
'replicas': 3,
|
||||
'group': None,
|
||||
'group-namespace': None,
|
||||
'pg_num': None,
|
||||
'weight': None}])
|
||||
|
||||
@mock.patch.object(requires.hookenv, 'related_units')
|
||||
@mock.patch.object(requires.hookenv, 'relation_get')
|
||||
def test_get_remote_all(self, relation_get, related_units):
|
||||
unit_data = {
|
||||
'rid:1': {
|
||||
'app1/0': {
|
||||
'key1': 'value1',
|
||||
'key2': 'value2'},
|
||||
'app1/1': {
|
||||
'key1': 'value1',
|
||||
'key2': 'value3'}},
|
||||
'rid:2': {
|
||||
'app2/0': {
|
||||
'key1': 'value1',
|
||||
'key2': 'value3'}},
|
||||
'rid:3': {}}
|
||||
|
||||
def get_unit_data(key, unit, relation_id):
|
||||
return unit_data[relation_id].get(unit, {}).get(key, {})
|
||||
conv1 = mock.MagicMock()
|
||||
conv1.relation_ids = ['rid:1', 'rid:2']
|
||||
conv2 = mock.MagicMock()
|
||||
conv2.relation_ids = ['rid:3']
|
||||
self.patch_kr('conversations', [conv1, conv2])
|
||||
related_units.side_effect = lambda x: unit_data[x].keys()
|
||||
relation_get.side_effect = get_unit_data
|
||||
# Check de-duplication:
|
||||
self.assertEqual(
|
||||
self.cr.get_remote_all('key1'),
|
||||
['value1'])
|
||||
# Check multiple values:
|
||||
self.assertEqual(
|
||||
self.cr.get_remote_all('key2'),
|
||||
['value2', 'value3'])
|
||||
# Check missing key
|
||||
self.assertEqual(
|
||||
self.cr.get_remote_all('key100'),
|
||||
[])
|
||||
# Check missing key with default
|
||||
self.assertEqual(
|
||||
self.cr.get_remote_all('key100', default='defaultvalue'),
|
||||
['defaultvalue'])
|
||||
|
||||
def test_mon_hosts(self):
|
||||
self.patch_kr('get_remote_all', ['10.0.0.10 10.0.0.12', '10.0.0.23'])
|
||||
self.assertEqual(
|
||||
self.cr.mon_hosts(),
|
||||
['10.0.0.10', '10.0.0.12', '10.0.0.23'])
|
Loading…
x
Reference in New Issue
Block a user