From 8949ab2f4e06ff885a940b59e39a960d9a98c219 Mon Sep 17 00:00:00 2001 From: Alex Kavanagh Date: Wed, 2 Jun 2021 10:13:42 +0100 Subject: [PATCH] Switch Helper to use c.openstack PatchHelper This charms.openstack PatchHelper class is a better version of the Helper class in test_lib_charm_openstack_designate_bind.py as it allows patching by fully qualified class string. This simplifies the patching in the tests. Change-Id: I9a997550d58bd06c8a8af9752c0d418b4acbbbdd --- ...test_lib_charm_openstack_designate_bind.py | 182 ++++++++---------- 1 file changed, 80 insertions(+), 102 deletions(-) diff --git a/unit_tests/test_lib_charm_openstack_designate_bind.py b/unit_tests/test_lib_charm_openstack_designate_bind.py index 83bd727..643d61d 100644 --- a/unit_tests/test_lib_charm_openstack_designate_bind.py +++ b/unit_tests/test_lib_charm_openstack_designate_bind.py @@ -13,8 +13,8 @@ # limitations under the License. import mock -import unittest +import charms_openstack.test_utils as test_utils import charm.openstack.designate_bind as designate_bind @@ -27,107 +27,79 @@ def FakeConfig(init_dict): return _config -class Helper(unittest.TestCase): +class Helper(test_utils.PatchHelper): def setUp(self): - self._patches = {} - self._patches_start = {} - self.ch_config_patch = mock.patch('charmhelpers.core.hookenv.config') - self.ch_config = self.ch_config_patch.start() + super().setUp() + self.patch('charmhelpers.core.hookenv.config', name='ch_config') self.ch_config.side_effect = lambda: {'ssl_param': None} - - def tearDown(self): - for k, v in self._patches.items(): - v.stop() - setattr(self, k, None) - self._patches = None - self._patches_start = None - self.ch_config_patch.stop() - - def patch(self, obj, attr, return_value=None, **kwargs): - mocked = mock.patch.object(obj, attr, **kwargs) - self._patches[attr] = mocked - started = mocked.start() - started.return_value = return_value - self._patches_start[attr] = started - setattr(self, attr, started) - - def patch_object(self, obj, attr, return_value=None, name=None, new=None): - if name is None: - name = attr - if new is not None: - mocked = mock.patch.object(obj, attr, new=new) - else: - mocked = mock.patch.object(obj, attr) - self._patches[name] = mocked - started = mocked.start() - if new is None: - started.return_value = return_value - self._patches_start[name] = started - setattr(self, name, started) + self.patch('charms_openstack.charm.core._singleton', new=None) class TestOpenStackDesignateBind(Helper): def test_install(self): - self.patch(designate_bind.DesignateBindCharm.singleton, 'install') + charm = designate_bind.DesignateBindCharm.singleton + self.patch_object(charm, 'install') designate_bind.install() self.install.assert_called_once_with() def test_init_rndckey(self): - self.patch(designate_bind.DesignateBindCharm.singleton, 'init_rndckey') + self.patch_object( + designate_bind.DesignateBindCharm.singleton, 'init_rndckey') designate_bind.init_rndckey() self.init_rndckey.assert_called_once_with() def test_get_rndc_secret(self): - self.patch( + self.patch_object( designate_bind.DesignateBindCharm.singleton, 'get_rndc_secret') designate_bind.get_rndc_secret() self.get_rndc_secret.assert_called_once_with() def test_get_rndc_algorithm(self): - self.patch( + self.patch_object( designate_bind.DesignateBindCharm.singleton, 'get_rndc_algorithm') designate_bind.get_rndc_algorithm() self.get_rndc_algorithm.assert_called_once_with() def test_get_sync_time(self): - self.patch( + self.patch_object( designate_bind.DesignateBindCharm.singleton, 'get_sync_time') designate_bind.get_sync_time() self.get_sync_time.assert_called_once_with() def test_setup_sync(self): - self.patch(designate_bind.DesignateBindCharm.singleton, 'setup_sync') + self.patch_object( + designate_bind.DesignateBindCharm.singleton, 'setup_sync') designate_bind.setup_sync() self.setup_sync.assert_called_once_with() def test_retrieve_zones(self): - self.patch( + self.patch_object( designate_bind.DesignateBindCharm.singleton, 'retrieve_zones') designate_bind.retrieve_zones('hacluster') self.retrieve_zones.assert_called_once_with('hacluster') def test_request_sync(self): - self.patch( + self.patch_object( designate_bind.DesignateBindCharm.singleton, 'request_sync') designate_bind.request_sync('hacluster') self.request_sync.assert_called_once_with('hacluster') def test_process_requests(self): - self.patch( + self.patch_object( designate_bind.DesignateBindCharm.singleton, 'process_requests') designate_bind.process_requests('hacluster') self.process_requests.assert_called_once_with('hacluster') def test_render_all_configs(self): - self.patch( + self.patch_object( designate_bind.DesignateBindCharm.singleton, 'render_with_interfaces') designate_bind.render_all_configs('interface_list') @@ -153,8 +125,8 @@ class TestDNSAdapter(Helper): def test_control_listen_ip(self): relation = mock.MagicMock() - self.patch(designate_bind.ch_ip, 'get_relation_ip') - self.patch(designate_bind.hookenv, 'unit_private_ip') + self.patch_object(designate_bind.ch_ip, 'get_relation_ip') + self.patch_object(designate_bind.hookenv, 'unit_private_ip') self.get_relation_ip.return_value = 'ip1' a = designate_bind.DNSAdapter(relation) self.assertEqual(a.control_listen_ip, 'ip1') @@ -167,14 +139,15 @@ class TestDNSAdapter(Helper): def test_algorithm(self): relation = mock.MagicMock() - self.patch(designate_bind.DesignateBindCharm, 'get_rndc_algorithm') + self.patch_object( + designate_bind.DesignateBindCharm, 'get_rndc_algorithm') self.get_rndc_algorithm.return_value = 'algo1' a = designate_bind.DNSAdapter(relation) self.assertEqual(a.algorithm, 'algo1') def test_secret(self): relation = mock.MagicMock() - self.patch(designate_bind.DesignateBindCharm, 'get_rndc_secret') + self.patch_object(designate_bind.DesignateBindCharm, 'get_rndc_secret') self.get_rndc_secret.return_value = 'secret1' a = designate_bind.DNSAdapter(relation) self.assertEqual(a.secret, 'secret1') @@ -202,7 +175,7 @@ class TestDesignateBindCharm(Helper): ) def test_get_rndc_secret(self): - self.patch(designate_bind.hookenv, 'leader_get') + self.patch_object(designate_bind.hookenv, 'leader_get') self.leader_get.return_value = 'secret1' self.assertEqual( designate_bind.DesignateBindCharm.get_rndc_secret(), @@ -210,7 +183,7 @@ class TestDesignateBindCharm(Helper): ) def test_get_sync_src(self): - self.patch(designate_bind.hookenv, 'leader_get') + self.patch_object(designate_bind.hookenv, 'leader_get') self.leader_get.return_value = 'http://ip1/my.tar' self.assertEqual( designate_bind.DesignateBindCharm.get_sync_src(), @@ -218,7 +191,7 @@ class TestDesignateBindCharm(Helper): ) def test_get_sync_time(self): - self.patch(designate_bind.hookenv, 'leader_get') + self.patch_object(designate_bind.hookenv, 'leader_get') self.leader_get.return_value = '100' self.assertEqual( designate_bind.DesignateBindCharm.get_sync_time(), @@ -227,9 +200,9 @@ class TestDesignateBindCharm(Helper): def test_process_requests(self): hacluster = mock.MagicMock() - self.patch(designate_bind.hookenv, 'log') - self.patch(designate_bind.DesignateBindCharm, 'setup_sync') - self.patch(designate_bind.DesignateBindCharm, 'get_sync_time') + self.patch_object(designate_bind.hookenv, 'log') + self.patch_object(designate_bind.DesignateBindCharm, 'setup_sync') + self.patch_object(designate_bind.DesignateBindCharm, 'get_sync_time') a = designate_bind.DesignateBindCharm() # No queued requests hacluster.retrieve_remote.return_value = [] @@ -250,8 +223,8 @@ class TestDesignateBindCharm(Helper): self.assertTrue(self.setup_sync.called) def test_set_sync_info(self): - self.patch(designate_bind.hookenv, 'leader_set') - self.patch(designate_bind.hookenv, 'unit_private_ip') + self.patch_object(designate_bind.hookenv, 'leader_set') + self.patch_object(designate_bind.hookenv, 'unit_private_ip') self.unit_private_ip.return_value = 'ip1' a = designate_bind.DesignateBindCharm() a.set_sync_info('20', '/tmp/tarball.tar') @@ -261,9 +234,10 @@ class TestDesignateBindCharm(Helper): def test_generate_rndc_key(self): hmac_mock = mock.MagicMock() - self.patch(designate_bind.os, 'urandom', return_value='seed') - self.patch(designate_bind.hmac, 'new', return_value=hmac_mock) - self.patch(designate_bind.base64, 'b64encode', return_value=hmac_mock) + self.patch_object(designate_bind.os, 'urandom', return_value='seed') + self.patch_object(designate_bind.hmac, 'new', return_value=hmac_mock) + self.patch_object( + designate_bind.base64, 'b64encode', return_value=hmac_mock) self.patch_object(designate_bind.hashlib, 'md5', new='md5lib') a = designate_bind.DesignateBindCharm() a.generate_rndc_key() @@ -273,11 +247,12 @@ class TestDesignateBindCharm(Helper): msg=b'RNDC Secret') def test_init_rndckey(self): - self.patch(designate_bind.hookenv, 'log') - self.patch(designate_bind.DesignateBindCharm, 'get_rndc_secret') - self.patch(designate_bind.DesignateBindCharm, 'generate_rndc_key') - self.patch(designate_bind.hookenv, 'leader_set') - self.patch(designate_bind.hookenv, 'is_leader') + self.patch_object(designate_bind.hookenv, 'log') + self.patch_object(designate_bind.DesignateBindCharm, 'get_rndc_secret') + self.patch_object( + designate_bind.DesignateBindCharm, 'generate_rndc_key') + self.patch_object(designate_bind.hookenv, 'leader_set') + self.patch_object(designate_bind.hookenv, 'is_leader') a = designate_bind.DesignateBindCharm() # Test secret already stored self.get_rndc_secret.return_value = 'mysecret' @@ -294,8 +269,8 @@ class TestDesignateBindCharm(Helper): self.assertEqual(a.init_rndckey(), None) def test_create_zone_tarball(self): - self.patch(designate_bind.glob, 'glob') - self.patch(designate_bind.subprocess, 'check_call') + self.patch_object(designate_bind.glob, 'glob') + self.patch_object(designate_bind.subprocess, 'check_call') _files = { '/var/cache/bind/juju*': ['jujufile1'], '/var/cache/bind/slave*': ['slavefile1'], @@ -309,8 +284,8 @@ class TestDesignateBindCharm(Helper): 'nsffile', 'nsdfile'], cwd='/var/cache/bind') def test_setup_sync_dir(self): - self.patch(designate_bind.os, 'mkdir') - self.patch(designate_bind.os, 'chmod') + self.patch_object(designate_bind.os, 'mkdir') + self.patch_object(designate_bind.os, 'chmod') a = designate_bind.DesignateBindCharm() a.setup_sync_dir('100') self.mkdir.assert_called_once_with('/var/www/html/zone-syncs', 493) @@ -321,7 +296,8 @@ class TestDesignateBindCharm(Helper): self.chmod.assert_called_once_with('/var/www/html/zone-syncs', 493) def test_create_sync_src_info_file(self): - self.patch(designate_bind.hookenv, 'local_unit', return_value='unit/1') + self.patch_object( + designate_bind.hookenv, 'local_unit', return_value='unit/1') a = designate_bind.DesignateBindCharm() with mock.patch('builtins.open') as bob: a.create_sync_src_info_file() @@ -330,15 +306,17 @@ class TestDesignateBindCharm(Helper): 'w+') def test_setup_sync(self): - self.patch(designate_bind.hookenv, 'log') - self.patch(designate_bind.DesignateBindCharm, 'setup_sync_dir') - self.patch(designate_bind.time, 'time') - self.patch( + self.patch_object(designate_bind.hookenv, 'log') + self.patch_object(designate_bind.DesignateBindCharm, 'setup_sync_dir') + self.patch_object(designate_bind.time, 'time') + self.patch_object( designate_bind.DesignateBindCharm, 'create_sync_src_info_file') - self.patch(designate_bind.DesignateBindCharm, 'service_control') - self.patch(designate_bind.DesignateBindCharm, 'create_zone_tarball') - self.patch(designate_bind.DesignateBindCharm, 'set_sync_info') + self.patch_object(designate_bind.DesignateBindCharm, 'service_control') + self.patch_object( + designate_bind.DesignateBindCharm, 'create_zone_tarball') + self.patch_object( + designate_bind.DesignateBindCharm, 'set_sync_info') self.setup_sync_dir.return_value = '/tmp/zonefiles' self.time.return_value = 100 a = designate_bind.DesignateBindCharm() @@ -354,7 +332,7 @@ class TestDesignateBindCharm(Helper): self.set_sync_info.assert_called_once_with('100', '100.tar.gz') def test_service_control(self): - self.patch(designate_bind.host, 'service_stop') + self.patch_object(designate_bind.host, 'service_stop') a = designate_bind.DesignateBindCharm() a.service_control('stop', ['svc1', 'svc2']) ctrl_calls = [ @@ -363,9 +341,9 @@ class TestDesignateBindCharm(Helper): self.service_stop.assert_has_calls(ctrl_calls) def test_request_sync(self): - self.patch(designate_bind.time, 'time') + self.patch_object(designate_bind.time, 'time') relation = mock.MagicMock() - self.patch(designate_bind.reactive, 'set_state') + self.patch_object(designate_bind.reactive, 'set_state') self.time.return_value = 100 a = designate_bind.DesignateBindCharm() a.request_sync(relation) @@ -376,7 +354,7 @@ class TestDesignateBindCharm(Helper): def test_wget_file(self): # retry_on_exception patched out in __init__.py - self.patch(designate_bind.subprocess, 'check_call') + self.patch_object(designate_bind.subprocess, 'check_call') a = designate_bind.DesignateBindCharm() a.wget_file('http://ip1/tarfile.tar', '/tmp') self.check_call.assert_called_once_with( @@ -387,14 +365,14 @@ class TestDesignateBindCharm(Helper): def test_retrieve_zones_cluster_relation(self): relation = mock.MagicMock() - self.patch(designate_bind.DesignateBindCharm, 'get_sync_time') - self.patch(designate_bind.DesignateBindCharm, 'get_sync_src') - self.patch(designate_bind.DesignateBindCharm, 'service_control') - self.patch(designate_bind.hookenv, 'log') - self.patch(designate_bind.reactive, 'set_state') - self.patch(designate_bind.reactive, 'remove_state') - self.patch(designate_bind.os, 'remove') - self.patch(designate_bind.subprocess, 'check_call') + self.patch_object(designate_bind.DesignateBindCharm, 'get_sync_time') + self.patch_object(designate_bind.DesignateBindCharm, 'get_sync_src') + self.patch_object(designate_bind.DesignateBindCharm, 'service_control') + self.patch_object(designate_bind.hookenv, 'log') + self.patch_object(designate_bind.reactive, 'set_state') + self.patch_object(designate_bind.reactive, 'remove_state') + self.patch_object(designate_bind.os, 'remove') + self.patch_object(designate_bind.subprocess, 'check_call') self.patch_object(designate_bind.DesignateBindCharm, 'wget_file') self.get_sync_src.return_value = 'http://ip1/tarfile.tar' ctrl_calls = [ @@ -420,10 +398,10 @@ class TestDesignateBindCharm(Helper): def test_retrieve_zones_cluster_relation_nourl(self): relation = mock.MagicMock() - self.patch(designate_bind.DesignateBindCharm, 'get_sync_time') - self.patch(designate_bind.DesignateBindCharm, 'get_sync_src') + self.patch_object(designate_bind.DesignateBindCharm, 'get_sync_time') + self.patch_object(designate_bind.DesignateBindCharm, 'get_sync_src') self.patch_object(designate_bind.DesignateBindCharm, 'wget_file') - self.patch(designate_bind.hookenv, 'log') + self.patch_object(designate_bind.hookenv, 'log') self.get_sync_src.return_value = None relation.retrieve_local.return_value = ['10'] self.get_sync_time.return_value = '20' @@ -432,14 +410,14 @@ class TestDesignateBindCharm(Helper): self.assertFalse(self.wget_file.called) def test_retrieve_zones_no_cluster_relation(self): - self.patch(designate_bind.DesignateBindCharm, 'get_sync_time') - self.patch(designate_bind.DesignateBindCharm, 'get_sync_src') - self.patch(designate_bind.DesignateBindCharm, 'service_control') - self.patch(designate_bind.hookenv, 'log') - self.patch(designate_bind.reactive, 'set_state') - self.patch(designate_bind.reactive, 'remove_state') - self.patch(designate_bind.os, 'remove') - self.patch(designate_bind.subprocess, 'check_call') + self.patch_object(designate_bind.DesignateBindCharm, 'get_sync_time') + self.patch_object(designate_bind.DesignateBindCharm, 'get_sync_src') + self.patch_object(designate_bind.DesignateBindCharm, 'service_control') + self.patch_object(designate_bind.hookenv, 'log') + self.patch_object(designate_bind.reactive, 'set_state') + self.patch_object(designate_bind.reactive, 'remove_state') + self.patch_object(designate_bind.os, 'remove') + self.patch_object(designate_bind.subprocess, 'check_call') self.patch_object(designate_bind.DesignateBindCharm, 'wget_file') self.get_sync_src.return_value = 'http://ip1/tarfile.tar' ctrl_calls = [ @@ -456,7 +434,7 @@ class TestDesignateBindCharm(Helper): '/var/cache/bind') def test_set_apparmor(self): - self.patch(designate_bind.os.path, 'isfile') + self.patch_object(designate_bind.os.path, 'isfile') a = designate_bind.DesignateBindCharm() self.isfile.return_value = True with mock.patch('builtins.open') as bob: