From 323c1c3b14562d7db05648be57418a2871ce26dd Mon Sep 17 00:00:00 2001 From: Frode Nordahl Date: Wed, 9 Jan 2019 07:47:47 +0100 Subject: [PATCH] Fixup repo config Change-Id: I91d545198a8570d7cacf06576c71f18b06c14df4 --- .gitignore | 5 ++ .gitreview | 4 ++ .stestr.conf | 3 ++ .zuul.yaml | 3 ++ interface.yaml | 7 +++ test-requirements.txt | 7 +++ tox.ini | 38 ++++++++++++++ unit_tests/__init__.py | 0 unit_tests/test_requires.py | 102 ++++++++++++++++++++++++++++++++++++ 9 files changed, 169 insertions(+) create mode 100644 .gitignore create mode 100644 .gitreview create mode 100644 .stestr.conf create mode 100644 .zuul.yaml create mode 100644 test-requirements.txt create mode 100644 tox.ini create mode 100644 unit_tests/__init__.py create mode 100644 unit_tests/test_requires.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ee6755 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.tox +.stestr +*__pycache__* +*.pyc +.unit-state.db diff --git a/.gitreview b/.gitreview new file mode 100644 index 0000000..ff4358f --- /dev/null +++ b/.gitreview @@ -0,0 +1,4 @@ +[gerrit] +host=review.openstack.org +port=29418 +project=openstack/charm-interface-service-control.git diff --git a/.stestr.conf b/.stestr.conf new file mode 100644 index 0000000..5fcccac --- /dev/null +++ b/.stestr.conf @@ -0,0 +1,3 @@ +[DEFAULT] +test_path=./unit_tests +top_dir=./ diff --git a/.zuul.yaml b/.zuul.yaml new file mode 100644 index 0000000..7051aee --- /dev/null +++ b/.zuul.yaml @@ -0,0 +1,3 @@ +- project: + templates: + - python35-charm-jobs diff --git a/interface.yaml b/interface.yaml index 7a66e8b..71a532e 100644 --- a/interface.yaml +++ b/interface.yaml @@ -1,3 +1,10 @@ name: service-control summary: Interface for requesting restarts of services maintainer: OpenStack Charmers +ignore: + - 'unit_tests' + - '.stestr.conf' + - 'test-requirements.txt' + - 'tox.ini' + - '.gitignore' + - '.zuul.yaml' diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 0000000..ac58161 --- /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 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..bf2661b --- /dev/null +++ b/tox.ini @@ -0,0 +1,38 @@ +[tox] +skipsdist = True +envlist = pep8,py3 +skip_missing_interpreters = True + +[testenv] +setenv = VIRTUAL_ENV={envdir} + PYTHONHASHSEED=0 + TERM=linux +install_command = + pip install {opts} {packages} + +[testenv:py3] +basepython = python3 +deps = -r{toxinidir}/test-requirements.txt +commands = ostestr {posargs} + +[testenv:py35] +basepython = python3.5 +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:venv] +commands = {posargs} + +[flake8] +# E402 ignore necessary for path append before sys module import in actions +ignore = E402 diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/unit_tests/test_requires.py b/unit_tests/test_requires.py new file mode 100644 index 0000000..baa4066 --- /dev/null +++ b/unit_tests/test_requires.py @@ -0,0 +1,102 @@ +# 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 + +import requires + + +_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 TestServiceControlRequires(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 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 setUp(self): + self.sr = requires.ServiceControlRequires('some-relation', []) + self._patches = {} + self._patches_start = {} + + def tearDown(self): + self.sr = None + for k, v in self._patches.items(): + v.stop() + setattr(self, k, None) + self._patches = None + self._patches_start = None + + def patch_sr(self, attr, return_value=None): + mocked = mock.patch.object(self.sr, 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 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 = { + 'broken': ( + '{requires:service-control}-relation-{broken,departed}', ), + 'changed': ( + '{requires:service-control}-relation-{joined,changed}', ), + } + for k, v in _hook_args.items(): + self.assertEqual(hook_patterns[k], v['args']) + + def test_broken(self): + self.patch_sr('remove_state') + self.sr.broken() + self.remove_state.assert_called_once_with('{relation_name}.connected') + + def test_changed(self): + self.patch_sr('set_state') + self.sr.changed() + self.set_state.assert_called_once_with('{relation_name}.connected')