714ef09e17
The mock third party library was needed for mock support in py2 runtimes. Since we now only support py36 and later, we can use the standard lib unittest.mock module instead. Note that https://github.com/openstack/charms.openstack is used during tests and he need `mock`, unfortunatelly it doesn't declare `mock` in its requirements so it retrieve mock from other charm project (cross dependency). So we depend on charms.openstack first and when Ib1ed5b598a52375e29e247db9ab4786df5b6d142 will be merged then CI will pass without errors. Depends-On: Ib1ed5b598a52375e29e247db9ab4786df5b6d142 Change-Id: Ic1a687f95e5fd16ffa7f63102d6f8f2de383780e
103 lines
3.3 KiB
Python
103 lines
3.3 KiB
Python
# 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
|
|
from 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')
|