From 355fc831b77d465a3c980731ecfd10b6e84e19c1 Mon Sep 17 00:00:00 2001 From: Alex Kavanagh Date: Thu, 25 May 2017 11:19:58 +0100 Subject: [PATCH] Use charms.openstack charmhelpers test_mocks Every reactive charm that uses charms.openstack and implements unit testing needs to mock charmhelpers, as otherwise the tests blow up. This patch uses the mocking of charmhelpers in charms.openstack so that a charm layer can just import test_mocks and charmhelpers will be mocked out along with setting up a default charmhelpers.c.o.u.OPENSTACK_RELEASES so that when an OpenStackCharm() derived charm class won't fail on definition. Change-Id: I489b15301f64310f7077d0e7246fd5e116877ed9 Related-Bug: #1693017 Depends-On: I5ac40617ee30e5f421ec16fc7592177a5e6aa166 --- .gitignore | 7 +++++ .gitreview | 4 +++ src/lib/charm/openstack/murano.py | 2 +- unit_tests/__init__.py | 28 ++----------------- unit_tests/test_lib_charm_openstack_murano.py | 8 ------ 5 files changed, 14 insertions(+), 35 deletions(-) create mode 100644 .gitignore create mode 100644 .gitreview diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a0b2628 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +build +layers +.tox +interfaces +.testrepository +*__pycache__* +*.pyc diff --git a/.gitreview b/.gitreview new file mode 100644 index 0000000..91969e2 --- /dev/null +++ b/.gitreview @@ -0,0 +1,4 @@ +[gerrit] +host=review.openstack.org +port=29418 +project=openstack/charm-murano.git diff --git a/src/lib/charm/openstack/murano.py b/src/lib/charm/openstack/murano.py index 6704375..af548b1 100644 --- a/src/lib/charm/openstack/murano.py +++ b/src/lib/charm/openstack/murano.py @@ -46,7 +46,7 @@ class MuranoCharm(charms_openstack.charm.HAOpenStackCharm): """ if release is None: release = ch_utils.os_release('python-keystonemiddleware') - super(MuranoCharm, self).__init__(release=release, **kwargs) + super().__init__(release=release, **kwargs) def get_amqp_credentials(self): """Provide the default amqp username and vhost as a tuple. diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py index 12469eb..3a5e9a3 100644 --- a/unit_tests/__init__.py +++ b/unit_tests/__init__.py @@ -13,34 +13,10 @@ # limitations under the License. import sys -import mock sys.path.append('src') sys.path.append('src/lib') # Mock out charmhelpers so that we can test without it. -# also stops sideeffects from occuring. -charmhelpers = mock.MagicMock() -apt_pkg = mock.MagicMock() -sys.modules['apt_pkg'] = apt_pkg -sys.modules['charmhelpers'] = charmhelpers -sys.modules['charmhelpers.core'] = charmhelpers.core -sys.modules['charmhelpers.core.decorators'] = charmhelpers.core.decorators -sys.modules['charmhelpers.core.hookenv'] = charmhelpers.core.hookenv -sys.modules['charmhelpers.core.host'] = charmhelpers.core.host -sys.modules['charmhelpers.core.unitdata'] = charmhelpers.core.unitdata -sys.modules['charmhelpers.core.templating'] = charmhelpers.core.templating -sys.modules['charmhelpers.contrib'] = charmhelpers.contrib -sys.modules['charmhelpers.contrib.openstack'] = charmhelpers.contrib.openstack -sys.modules['charmhelpers.contrib.openstack.utils'] = ( - charmhelpers.contrib.openstack.utils) -sys.modules['charmhelpers.contrib.openstack.templating'] = ( - charmhelpers.contrib.openstack.templating) -sys.modules['charmhelpers.contrib.network'] = charmhelpers.contrib.network -sys.modules['charmhelpers.contrib.network.ip'] = ( - charmhelpers.contrib.network.ip) -sys.modules['charmhelpers.fetch'] = charmhelpers.fetch -sys.modules['charmhelpers.cli'] = charmhelpers.cli -sys.modules['charmhelpers.contrib.hahelpers'] = charmhelpers.contrib.hahelpers -sys.modules['charmhelpers.contrib.hahelpers.cluster'] = ( - charmhelpers.contrib.hahelpers.cluster) +import charms_openstack.test_mocks # noqa +charms_openstack.test_mocks.mock_charmhelpers() diff --git a/unit_tests/test_lib_charm_openstack_murano.py b/unit_tests/test_lib_charm_openstack_murano.py index b200486..692880f 100644 --- a/unit_tests/test_lib_charm_openstack_murano.py +++ b/unit_tests/test_lib_charm_openstack_murano.py @@ -72,15 +72,7 @@ class Helper(unittest.TestCase): class TestMuranoCharm(Helper): - def test_install(self): - self.patch(murano.MuranoCharm, 'configure_source') - a = murano.MuranoCharm(release='mitaka') - a.install() - self.configure_source.assert_called_with() - def test__init__(self): - self.patch(murano.MuranoCharm, - 'set_config_defined_certs_and_keys') self.patch(murano.ch_utils, 'os_release') murano.MuranoCharm() self.os_release.assert_called_once_with('python-keystonemiddleware')