Files
charm-manila-generic/unit_tests/test_manila_generic_handlers.py
Hervé Beraud ab1d7e94a1 Use unittest.mock instead of mock
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: I609298d09de3806636b8a157f4ef81c1ab567bf7
2021-12-15 09:35:05 +00:00

82 lines
2.9 KiB
Python

# Copyright 2016 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.
from unittest import mock
import reactive.manila_generic_handlers as handlers
import charms_openstack.test_utils as test_utils
class TestRegisteredHooks(test_utils.TestRegisteredHooks):
def test_hooks(self):
defaults = [
'charm.installed',
'update-status']
hook_set = {
'when': {
'send_config': ('manila-plugin.changed', ),
'update_config': ('manila-plugin.available',
'config.changed', ),
},
'when_not': {
'send_config': ('config.changed', 'update-status'),
'update_config': ('update-status', ),
},
}
# test that the hooks were registered via the
# reactive.barbican_handlers
self.registered_hooks_test_helper(handlers, hook_set, defaults)
class TestHandlerFunctions(test_utils.PatchHelper):
def _patch_provide_charm_instance(self):
manila_generic_charm = mock.MagicMock()
self.patch('charms_openstack.charm.provide_charm_instance',
name='provide_charm_instance',
new=mock.MagicMock())
self.provide_charm_instance().__enter__.return_value = \
manila_generic_charm
self.provide_charm_instance().__exit__.return_value = None
return manila_generic_charm
def test_send_config(self):
generic = self._patch_provide_charm_instance()
class FakeManilaPlugin(object):
name = None
configuration_data = None
authentication_data = 'auth data'
_clear_changed = 0
def clear_changed(self):
self._clear_changed += 1
generic.get_config_for_principal.return_value = "some data"
manila_plugin = FakeManilaPlugin()
handlers.send_config(manila_plugin)
# test for expecations
self.assertEqual(manila_plugin.name,
generic.options.share_backend_name)
self.assertEqual(manila_plugin.configuration_data, "some data")
generic.get_config_for_principal.assert_called_once_with('auth data')
generic.assess_status.assert_called_once_with()
generic.maybe_write_ssh_keys.assert_called_once_with()
self.assertEqual(manila_plugin._clear_changed, 1)