diff --git a/.stestr.conf b/.stestr.conf new file mode 100644 index 0000000..ee4526b --- /dev/null +++ b/.stestr.conf @@ -0,0 +1,3 @@ +[DEFAULT] +test_path=./unit_tests +top_dir=./ \ No newline at end of file diff --git a/README.md b/README.md index 2cb4c8c..896adf4 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,19 @@ Basic interface for sending Cinder-Backup external subordinate backend configuration. +# Metadata + +To consume this interface in your charm or layer, add the following to layer.yaml: + +``` +includes: ['interface:cinder-backup'] +``` + +and add a provides interface of type backup-backend to your charm or layers metadata.yaml: + +``` +provides: + backup-backend: + interface: cinder-backup + scope: container +``` diff --git a/interface.yaml b/interface.yaml index a72380a..aeb9d2b 100644 --- a/interface.yaml +++ b/interface.yaml @@ -2,3 +2,10 @@ name: cinder-backup summary: Interface to connect cinder-backup with external backend version: 1 repo: https://github.com/lolwww/interface-cinder-backup +ignore: + - 'unit_tests' + - '.stestr.conf' + - 'test-requirements.txt' + - 'tox.ini' + - '.gitignore' + - '.travis.yml' \ No newline at end of file diff --git a/provides.py b/provides.py index aa4b988..744854b 100644 --- a/provides.py +++ b/provides.py @@ -19,5 +19,5 @@ class CinderBackupProvides(Endpoint): def publish(self, name, configuration): for relation in self.relations: - relation.to_publish_raw['backend_name'] = name + relation.to_publish['backend_name'] = name relation.to_publish['subordinate_configuration'] = configuration diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 0000000..ffc0583 --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1,7 @@ +# Lint and unit test requirements +flake8 +os-testr>=0.4.1 +charms.reactive +mock>=1.2 +coverage>=3.6 +git+https://github.com/openstack/charms.openstack.git#egg=charms.openstack \ No newline at end of file diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..b60a6da --- /dev/null +++ b/tox.ini @@ -0,0 +1,60 @@ +[tox] +skipsdist = True +envlist = pep8,py35 +skip_missing_interpreters = True + +[testenv] +setenv = VIRTUAL_ENV={envdir} + PYTHONHASHSEED=0 + TERM=linux +install_command = + pip install {opts} {packages} + +[testenv:py35] +basepython = python3 +deps = -r{toxinidir}/test-requirements.txt +commands = ostestr {posargs} + +[testenv:py36] +basepython = python3.6 +deps = -r{toxinidir}/test-requirements.txt +commands = ostestr {posargs} + +[testenv:pep8] +basepython = python3 +deps = -r{toxinidir}/test-requirements.txt +commands = flake8 {posargs} + +[testenv:cover] +# Technique based heavily upon +# https://github.com/openstack/nova/blob/master/tox.ini +basepython = python3 +deps = -r{toxinidir}/test-requirements.txt +setenv = + {[testenv]setenv} + PYTHON=coverage run +commands = + coverage erase + ostestr {posargs} + coverage combine + coverage html -d cover + coverage xml -o cover/coverage.xml + coverage report + +[coverage:run] +branch = True +concurrency = multiprocessing +parallel = True +source = + . +omit = + .tox/* + */charmhelpers/* + unit_tests/* + +[testenv:venv] +commands = {posargs} + +[flake8] +# E402 ignore necessary for path append before sys module import in actions +ignore = E402 \ No newline at end of file diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py new file mode 100644 index 0000000..7b5dac4 --- /dev/null +++ b/unit_tests/__init__.py @@ -0,0 +1,22 @@ +# Copyright 2018 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 sys + +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() diff --git a/unit_tests/test_provides.py b/unit_tests/test_provides.py new file mode 100644 index 0000000..0842689 --- /dev/null +++ b/unit_tests/test_provides.py @@ -0,0 +1,110 @@ +# Copyright 2018 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 +import unittest + +import provides + + +_hook_args = {} + + +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 TestCinderBackupProvides(unittest.TestCase): + + @classmethod + def setUpClass(cls): + cls._patched_hook = mock.patch('charms.reactive.hook', mock_hook) + cls._patched_hook_started = cls._patched_hook.start() + # force provides 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 setUp(self): + self.cbr = provides.CinderBackupProvides('some-relation', []) + self._patches = {} + self._patches_start = {} + + def tearDown(self): + self.cbr = None + for k, v in self._patches.items(): + v.stop() + setattr(self, k, None) + self._patches = None + self._patches_start = None + + def patch_cinder_backup_rel(self, attr, return_value=None): + mocked = mock.patch.object(self.cbr, attr) + self._patches[attr] = mocked + started = mocked.start() + started.return_value = return_value + self._patches_start[attr] = started + setattr(self, attr, started) + + def patch_topublish(self): + self.patch_cinder_backup_rel('_relations') + relation = mock.MagicMock() + to_publish = mock.PropertyMock() + type(relation).to_publish = to_publish + self._relations.__iter__.return_value = [relation] + return relation.to_publish + + 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. + hook_patterns = { + 'joined': ('{provides:backup-backend}-relation-joined', ), + 'changed': ('{provides:backup-backend}-relation-changed', ), + 'departed': ( + '{provides:backup-backend}-relation-{broken,departed}', ), + } + for k, v in _hook_args.items(): + self.assertEqual(hook_patterns[k], v['args']) + + def test_publish_backend_info(self): + to_publish = self.patch_topublish() + name = 'FAKENAME' + configuration = {'a': 'A', 'b': 'B'} + self.cbr.publish(name, configuration) + to_publish.__setitem__.assert_has_calls([ + mock.call('backend_name', name), + mock.call('subordinate_configuration', configuration) + ])