Files
charm-interface-placement/unit_tests/test_provides.py
Hervé Beraud e8a30d9fb4 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: I0e18ed6b0d56c3cb4ffca9faa8fed5c925842a7f
2020-06-09 14:47:07 +02:00

102 lines
3.4 KiB
Python

# Copyright 2019 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 provides
import charms_openstack.test_utils as test_utils
_hook_args = {}
class _relation_mock:
def __init__(self, application_name=None, units=None):
self.to_publish_raw = {}
self.application_name = application_name
self.units = units
class TestRegisteredHooks(test_utils.TestRegisteredHooks):
def test_hooks(self):
defaults = []
hook_set = {
'when': {
'joined': ('endpoint.{endpoint_name}.joined',),
'changed': ('endpoint.{endpoint_name}.changed',),
},
'when_any': {
'broken': ('endpoint.{endpoint_name}.broken',
'endpoint.{endpoint_name}.departed',),
'departed': ('endpoint.{endpoint_name}.broken',
'endpoint.{endpoint_name}.departed',),
},
}
# test that the hooks were registered
self.registered_hooks_test_helper(provides, hook_set, defaults)
class TestPlacementProvides(test_utils.PatchHelper):
def setUp(self):
super().setUp()
self.provides_class = provides.PlacementProvides('some-endpoint')
self._patches = {}
self._patches_start = {}
def tearDown(self):
for k, v in self._patches.items():
v.stop()
setattr(self, k, None)
self._patches = None
self._patches_start = None
def test_joined(self):
self.patch_object(provides, 'set_flag')
self.provides_class.joined()
self.set_flag.assert_called_once_with('some-endpoint.connected')
def test_changed(self):
self.patch_object(provides, 'set_flag')
self.patch_object(provides, 'clear_flag')
self.provides_class.changed()
self.clear_flag.assert_called_once_with(
'endpoint.some-endpoint.changed')
self.set_flag.assert_called_once_with('some-endpoint.available')
def test_departed(self):
self.patch_object(provides, 'clear_flag')
self.provides_class.departed()
self.clear_flag.assert_has_calls([
mock.call('some-endpoint.available'),
mock.call('some-endpoint.connected'),
])
def test_get_nova_placement_disabled(self):
mock_all_joined_units = mock.MagicMock()
mock_all_joined_units.received = {'nova_placement_disabled': True}
self.provides_class._all_joined_units = mock_all_joined_units
self.assertEqual(
self.provides_class.get_nova_placement_disabled(),
True)
def test_set_placement_enabled(self):
mock_rel = _relation_mock()
self.provides_class._relations = [mock_rel]
self.provides_class.set_placement_enabled()
expect = {'placement_enabled': True}
self.assertEqual(mock_rel.to_publish_raw, expect)