Mock the sqlite3 kvstore

This patch replaces the sqlite3 kvstore implementation provided by charmhelpers. Patches the kvstore used in pcmk for both the test_pcmk and the test_hacluster_hooks tests.

closes-bug: #1908282

Change-Id: I3320735314f0b03aecec6635ef82ddd44eecaff1
This commit is contained in:
Jarred Wilson 2021-01-13 13:56:46 -05:00
parent 355bbabe65
commit 33c10cff26
3 changed files with 73 additions and 8 deletions

View File

@ -19,6 +19,7 @@ import sys
import tempfile import tempfile
import unittest import unittest
import test_utils import test_utils
import pcmk
mock_apt = mock.MagicMock() mock_apt = mock.MagicMock()
sys.modules['apt_pkg'] = mock_apt sys.modules['apt_pkg'] = mock_apt
@ -40,6 +41,7 @@ class TestCorosyncConf(unittest.TestCase):
shutil.rmtree(self.tmpdir) shutil.rmtree(self.tmpdir)
os.remove(self.tmpfile.name) os.remove(self.tmpfile.name)
@mock.patch.object(pcmk.unitdata, 'kv')
@mock.patch.object(hooks, 'is_stonith_configured') @mock.patch.object(hooks, 'is_stonith_configured')
@mock.patch.object(hooks, 'configure_peer_stonith_resource') @mock.patch.object(hooks, 'configure_peer_stonith_resource')
@mock.patch.object(hooks, 'get_member_ready_nodes') @mock.patch.object(hooks, 'get_member_ready_nodes')
@ -76,13 +78,15 @@ class TestCorosyncConf(unittest.TestCase):
configure_resources_on_remotes, configure_resources_on_remotes,
get_member_ready_nodes, get_member_ready_nodes,
configure_peer_stonith_resource, configure_peer_stonith_resource,
is_stonith_configured): is_stonith_configured, mock_kv):
def fake_crm_opt_exists(res_name): def fake_crm_opt_exists(res_name):
# res_ubuntu will take the "update resource" route # res_ubuntu will take the "update resource" route
# res_nova_eth0_vip will take the delete resource route # res_nova_eth0_vip will take the delete resource route
return res_name in ["res_ubuntu", "res_nova_eth0_vip"] 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 crm_opt_exists.side_effect = fake_crm_opt_exists
commit.return_value = 0 commit.return_value = 0
is_stonith_configured.return_value = False is_stonith_configured.return_value = False

View File

@ -16,9 +16,9 @@ import mock
import pcmk import pcmk
import os import os
import tempfile import tempfile
import test_utils
import unittest import unittest
from distutils.version import StrictVersion from distutils.version import StrictVersion
from charmhelpers.core import unitdata
CRM_CONFIGURE_SHOW_XML = '''<?xml version="1.0" ?> CRM_CONFIGURE_SHOW_XML = '''<?xml version="1.0" ?>
@ -229,9 +229,11 @@ class TestPcmk(unittest.TestCase):
'maintenance-mode=false'], 'maintenance-mode=false'],
universal_newlines=True) universal_newlines=True)
@mock.patch.object(pcmk.unitdata, 'kv')
@mock.patch('subprocess.call') @mock.patch('subprocess.call')
def test_crm_update_resource(self, mock_call): def test_crm_update_resource(self, mock_call, mock_kv):
db = unitdata.kv() db = test_utils.FakeKvStore()
mock_kv.return_value = db
db.set('res_test-IPaddr2', '') db.set('res_test-IPaddr2', '')
mock_call.return_value = 0 mock_call.return_value = 0
@ -248,9 +250,11 @@ class TestPcmk(unittest.TestCase):
('primitive res_test IPaddr2 \\\n' ('primitive res_test IPaddr2 \\\n'
'\tparams ip=1.2.3.4 cidr_netmask=255.255.0.0')) '\tparams ip=1.2.3.4 cidr_netmask=255.255.0.0'))
@mock.patch.object(pcmk.unitdata, 'kv')
@mock.patch('subprocess.call') @mock.patch('subprocess.call')
def test_crm_update_resource_exists_in_kv(self, mock_call): def test_crm_update_resource_exists_in_kv(self, mock_call, mock_kv):
db = unitdata.kv() db = test_utils.FakeKvStore()
mock_kv.return_value = db
db.set('res_test-IPaddr2', 'ef395293b1b7c29c5bf1c99774f75cf4') db.set('res_test-IPaddr2', 'ef395293b1b7c29c5bf1c99774f75cf4')
pcmk.crm_update_resource('res_test', 'IPaddr2', 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" "Resource res_test already defined and parameters haven't changed"
]) ])
@mock.patch.object(pcmk.unitdata, 'kv')
@mock.patch('subprocess.call') @mock.patch('subprocess.call')
def test_crm_update_resource_exists_in_kv_force_true(self, mock_call): def test_crm_update_resource_exists_in_kv_force_true(self, mock_call,
db = unitdata.kv() mock_kv):
db = test_utils.FakeKvStore()
mock_kv.return_value = db
db.set('res_test-IPaddr2', 'ef395293b1b7c29c5bf1c99774f75cf4') db.set('res_test-IPaddr2', 'ef395293b1b7c29c5bf1c99774f75cf4')
with mock.patch.object(tempfile, "NamedTemporaryFile", with mock.patch.object(tempfile, "NamedTemporaryFile",

View File

@ -15,11 +15,13 @@
import os import os
import logging import logging
import unittest import unittest
import sys
import yaml import yaml
from contextlib import contextmanager from contextlib import contextmanager
from mock import patch, MagicMock from mock import patch, MagicMock
from charmhelpers.core.unitdata import Record
def load_config(): def load_config():
@ -150,3 +152,55 @@ def patch_open():
with patch('builtins.open', stub_open): with patch('builtins.open', stub_open):
yield mock_open, mock_file 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