Files
charm-masakari/unit_tests/test_lib_charm_openstack_masakari.py
Hervé Beraud d9a643d8e3 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: I55ccb5548917c8431b0080c956bec5543d31a9a3
2021-12-15 11:19:29 +00:00

84 lines
2.8 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 charmhelpers
import charm.openstack.masakari as masakari
import charms_openstack.test_utils as test_utils
import charms_openstack.charm
class Helper(test_utils.PatchHelper):
def setUp(self):
super().setUp()
self.patch_release(masakari.MasakariCharm.release)
class TestMasakariCharm(Helper):
def _patch_config_and_charm(self, config):
self.patch_object(charmhelpers.core.hookenv, 'config')
def cf(key=None):
if key is not None:
return config[key]
return config
self.config.side_effect = cf
c = masakari.MasakariCharm()
return c
def test_get_amqp_credentials(self):
c = self._patch_config_and_charm({})
self.assertEqual(
c.get_amqp_credentials(),
('masakari', 'masakari'))
def test_get_database_setup(self):
c = self._patch_config_and_charm({})
self.assertEqual(
c.get_database_setup(),
[{'database': 'masakari', 'username': 'masakari'}])
def test_public_url(self):
self.patch_object(charms_openstack.charm.HAOpenStackCharm,
'public_url', new_callable=mock.PropertyMock)
c = self._patch_config_and_charm({})
self.public_url.return_value = 'http://masakari-public'
self.assertEqual(
c.public_url,
'http://masakari-public/v1/%(tenant_id)s')
def test_admin_url(self):
self.patch_object(charms_openstack.charm.HAOpenStackCharm,
'admin_url', new_callable=mock.PropertyMock)
c = self._patch_config_and_charm({})
self.admin_url.return_value = 'http://masakari-admin'
self.assertEqual(
c.admin_url,
'http://masakari-admin/v1/%(tenant_id)s')
def test_internal_url(self):
self.patch_object(charms_openstack.charm.HAOpenStackCharm,
'internal_url', new_callable=mock.PropertyMock)
c = self._patch_config_and_charm({})
self.internal_url.return_value = 'http://masakari-internal'
self.assertEqual(
c.internal_url,
'http://masakari-internal/v1/%(tenant_id)s')