diff --git a/unit_tests/test_hacluster_hooks.py b/unit_tests/test_hacluster_hooks.py index a7c6a48..8d9754e 100644 --- a/unit_tests/test_hacluster_hooks.py +++ b/unit_tests/test_hacluster_hooks.py @@ -19,6 +19,7 @@ import sys import tempfile import unittest import test_utils +import pcmk mock_apt = mock.MagicMock() sys.modules['apt_pkg'] = mock_apt @@ -40,6 +41,7 @@ class TestCorosyncConf(unittest.TestCase): shutil.rmtree(self.tmpdir) os.remove(self.tmpfile.name) + @mock.patch.object(pcmk.unitdata, 'kv') @mock.patch.object(hooks, 'is_stonith_configured') @mock.patch.object(hooks, 'configure_peer_stonith_resource') @mock.patch.object(hooks, 'get_member_ready_nodes') @@ -76,13 +78,15 @@ class TestCorosyncConf(unittest.TestCase): configure_resources_on_remotes, get_member_ready_nodes, configure_peer_stonith_resource, - is_stonith_configured): + is_stonith_configured, mock_kv): def fake_crm_opt_exists(res_name): # res_ubuntu will take the "update resource" route # res_nova_eth0_vip will take the delete resource route return res_name in ["res_ubuntu", "res_nova_eth0_vip"] + db = test_utils.FakeKvStore() + mock_kv.return_value = db crm_opt_exists.side_effect = fake_crm_opt_exists commit.return_value = 0 is_stonith_configured.return_value = False diff --git a/unit_tests/test_pcmk.py b/unit_tests/test_pcmk.py index ea66b62..b9691fc 100644 --- a/unit_tests/test_pcmk.py +++ b/unit_tests/test_pcmk.py @@ -16,9 +16,9 @@ import mock import pcmk import os import tempfile +import test_utils import unittest from distutils.version import StrictVersion -from charmhelpers.core import unitdata CRM_CONFIGURE_SHOW_XML = ''' @@ -229,9 +229,11 @@ class TestPcmk(unittest.TestCase): 'maintenance-mode=false'], universal_newlines=True) + @mock.patch.object(pcmk.unitdata, 'kv') @mock.patch('subprocess.call') - def test_crm_update_resource(self, mock_call): - db = unitdata.kv() + def test_crm_update_resource(self, mock_call, mock_kv): + db = test_utils.FakeKvStore() + mock_kv.return_value = db db.set('res_test-IPaddr2', '') mock_call.return_value = 0 @@ -248,9 +250,11 @@ class TestPcmk(unittest.TestCase): ('primitive res_test IPaddr2 \\\n' '\tparams ip=1.2.3.4 cidr_netmask=255.255.0.0')) + @mock.patch.object(pcmk.unitdata, 'kv') @mock.patch('subprocess.call') - def test_crm_update_resource_exists_in_kv(self, mock_call): - db = unitdata.kv() + def test_crm_update_resource_exists_in_kv(self, mock_call, mock_kv): + db = test_utils.FakeKvStore() + mock_kv.return_value = db db.set('res_test-IPaddr2', 'ef395293b1b7c29c5bf1c99774f75cf4') pcmk.crm_update_resource('res_test', 'IPaddr2', @@ -261,9 +265,12 @@ class TestPcmk(unittest.TestCase): "Resource res_test already defined and parameters haven't changed" ]) + @mock.patch.object(pcmk.unitdata, 'kv') @mock.patch('subprocess.call') - def test_crm_update_resource_exists_in_kv_force_true(self, mock_call): - db = unitdata.kv() + def test_crm_update_resource_exists_in_kv_force_true(self, mock_call, + mock_kv): + db = test_utils.FakeKvStore() + mock_kv.return_value = db db.set('res_test-IPaddr2', 'ef395293b1b7c29c5bf1c99774f75cf4') with mock.patch.object(tempfile, "NamedTemporaryFile", diff --git a/unit_tests/test_utils.py b/unit_tests/test_utils.py index a2d7f4a..00a14c7 100644 --- a/unit_tests/test_utils.py +++ b/unit_tests/test_utils.py @@ -15,11 +15,13 @@ import os import logging import unittest +import sys import yaml from contextlib import contextmanager from mock import patch, MagicMock +from charmhelpers.core.unitdata import Record def load_config(): @@ -150,3 +152,55 @@ def patch_open(): with patch('builtins.open', stub_open): yield mock_open, mock_file + + +class FakeKvStore(): + + def __init__(self): + self._store = {} + self._closed = False + self._flushed = False + + def close(self): + self._closed = True + self._flushed = True + + def get(self, key, default=None, record=False): + if key not in self._store: + return default + if record: + return Record(self._store[key]) + return self._store[key] + + def getrange(self, *args, **kwargs): + raise NotImplementedError + + def update(self, mapping, prefix=""): + for k, v in mapping.items(): + self.set("%s%s" % (prefix, k), v) + + def unset(self, key): + if key in self._store: + del self._store[key] + + def unsetrange(self, keys=None, prefix=""): + raise NotImplementedError + + def set(self, key, value): + self._store[key] = value + return value + + def delta(self, mapping, prefix): + raise NotImplementedError + + def hook_scope(self, name=""): + raise NotImplementedError + + def flush(self, save=True): + self._flushed = True + + def gethistory(self, key, deserialize=False): + raise NotImplementedError + + def debug(self, fh=sys.stderr): + raise NotImplementedError