Commit 5dc66e15 authored by Petr Pudlak's avatar Petr Pudlak
Browse files

Update ConfigWriter unit tests



This involves:

- Updating how a mocked configuration object is set up.
- Adding calls to refresh in-memory objects from the configuration
  (because they're not shared any more).
- Adding calls to Update to save modified in-memory objects so that the
  changes are visible by other code.
Signed-off-by: default avatarPetr Pudlak <pudlak@google.com>
Reviewed-by: default avatarKlaus Aehlig <aehlig@google.com>
parent f47b32a8
......@@ -60,14 +60,20 @@ class GanetiContextMock(object):
def GetWConfdContext(self, _ec_id):
return (None, None, None)
def AddNode(self, node, ec_id):
self._test_case.cfg.AddNode(node, ec_id)
def GetConfig(self, _ec_id):
return self._test_case.cfg
def GetRpc(self, _cfg):
return self._test_case.rpc
def AddNode(self, cfg, node, ec_id):
cfg.AddNode(node, ec_id)
def ReaddNode(self, node):
pass
def RemoveNode(self, node):
self._test_case.cfg.RemoveNode(node.uuid)
def RemoveNode(self, cfg, node):
cfg.RemoveNode(node.uuid)
class MockLU(LogicalUnit):
......@@ -207,7 +213,7 @@ class CmdlibTestCase(testutils.GanetiTestCase):
@return: A mock LU
"""
return MockLU(self.mcpu, mock.MagicMock(), self.ctx, self.rpc,
return MockLU(self.mcpu, mock.MagicMock(), self.ctx, self.cfg, self.rpc,
(1234, "/tmp/mock/livelock"), WConfdMock())
def RpcResultsBuilder(self, use_node_names=False):
......
......@@ -52,9 +52,14 @@ class ConfigMock(config.ConfigWriter):
self._cur_nic_id = 1
self._cur_net_id = 1
self._default_os = None
self._mocked_config_store = None
super(ConfigMock, self).__init__(cfg_file="/dev/null",
_getents=_StubGetEntResolver())
_getents=_StubGetEntResolver(),
offline=True)
with self.GetConfigManager():
self._CreateConfig()
def _GetUuid(self):
return str(uuid_module.uuid4())
......@@ -106,7 +111,7 @@ class ConfigMock(config.ConfigWriter):
networks=networks,
members=[])
self.AddNodeGroup(group, None)
self._UnlockedAddNodeGroup(group, None, True)
return group
# pylint: disable=R0913
......@@ -169,7 +174,7 @@ class ConfigMock(config.ConfigWriter):
disk_state=disk_state,
disk_state_static=disk_state_static)
self.AddNode(node, None)
self._UnlockedAddNode(node, None)
return node
def AddNewInstance(self,
......@@ -540,7 +545,7 @@ class ConfigMock(config.ConfigWriter):
cluster.enabled_disk_templates = list(enabled_disk_templates)
cluster.ipolicy[constants.IPOLICY_DTS] = list(enabled_disk_templates)
def _OpenConfig(self, accept_foreign):
def _CreateConfig(self):
self._config_data = objects.ConfigData(
version=constants.CONFIG_VERSION,
cluster=None,
......@@ -593,16 +598,16 @@ class ConfigMock(config.ConfigWriter):
)
self._cluster.ctime = self._cluster.mtime = time.time()
self._cluster.UpgradeConfig()
self._config_data.cluster = self._cluster
self._ConfigData().cluster = self._cluster
self._default_group = self.AddNewNodeGroup(name="default")
self._master_node = self.AddNewNode(uuid=master_node_uuid)
def _WriteConfig(self, destination=None, feedback_fn=None):
pass
def _OpenConfig(self, _accept_foreign):
self._config_data = self._mocked_config_store
def _DistributeConfig(self, feedback_fn):
pass
def _WriteConfig(self, destination=None, feedback_fn=None):
self._mocked_config_store = self._ConfigData()
def _GetRpc(self, address_list):
def _GetRpc(self, _address_list):
raise AssertionError("This should not be used during tests!")
......@@ -111,7 +111,8 @@ class TestConfigRunner(unittest.TestCase):
disks=[], nics=[],
disk_template=constants.DT_DISKLESS,
primary_node=self._get_object().GetMasterNode(),
osparams_private=serializer.PrivateDict())
osparams_private=serializer.PrivateDict(),
beparams={})
return inst
def testEmpty(self):
......@@ -358,6 +359,8 @@ class TestConfigRunner(unittest.TestCase):
set(["node1-uuid", "node2-uuid",
cfg.GetNodeInfoByName(me.name).uuid]))
(grp1, grp2) = [cfg.GetNodeGroup(grp.uuid) for grp in (grp1, grp2)]
def _VerifySerials():
self.assertEqual(cfg.GetClusterInfo().serial_no, cluster_serial)
self.assertEqual(node1.serial_no, node1_serial)
......@@ -404,6 +407,8 @@ class TestConfigRunner(unittest.TestCase):
cfg.AssignGroupNodes([
(node2.uuid, grp1.uuid),
])
(grp1, grp2) = [cfg.GetNodeGroup(grp.uuid) for grp in (grp1, grp2)]
(node1, node2) = [cfg.GetNodeInfo(node.uuid) for node in (node1, node2)]
cluster_serial += 1
node2_serial += 1
grp1_serial += 1
......@@ -421,6 +426,8 @@ class TestConfigRunner(unittest.TestCase):
(node1.uuid, grp2.uuid),
(node2.uuid, grp2.uuid),
])
(grp1, grp2) = [cfg.GetNodeGroup(grp.uuid) for grp in (grp1, grp2)]
(node1, node2) = [cfg.GetNodeInfo(node.uuid) for node in (node1, node2)]
cluster_serial += 1
node1_serial += 1
node2_serial += 1
......@@ -584,11 +591,13 @@ class TestConfigRunner(unittest.TestCase):
node = cfg.GetNodeInfo(cfg.GetNodeList()[0])
key = list(constants.NDC_GLOBALS)[0]
node.ndparams[key] = constants.NDC_DEFAULTS[key]
cfg.Update(node, None)
errs = cfg.VerifyConfig()
self.assertTrue(len(errs) >= 1)
self.assertTrue(_IsErrorInList("has some global parameters set", errs))
del node.ndparams[key]
cfg.Update(node, None)
errs = cfg.VerifyConfig()
self.assertFalse(errs)
......@@ -606,9 +615,9 @@ class TestConfigRunner(unittest.TestCase):
hvparams = {"a": "A", "b": "B", "c": "C"}
hvname = "myhv"
cfg_writer = self._get_object()
cfg_writer._config_data = mock.Mock()
cfg_writer._config_data.cluster = mock.Mock()
cfg_writer._config_data.cluster.hvparams = {hvname: hvparams}
cfg_writer._SetConfigData(mock.Mock())
cfg_writer._ConfigData().cluster = mock.Mock()
cfg_writer._ConfigData().cluster.hvparams = {hvname: hvparams}
result = cfg_writer._UnlockedGetHvparamsString(hvname)
......
......@@ -246,8 +246,8 @@ class TestHooksMaster(unittest.TestCase):
self.context = FakeContext()
# WARNING: here we pass None as RpcRunner instance since we know
# our usage via HooksMaster will not use lu.rpc
self.lu = FakeLU(FakeProc(), self.op, self.context, None, (123, "/foo/bar"),
None)
self.lu = FakeLU(FakeProc(), self.op, self.context, FakeConfig(),
None, (123, "/foo/bar"), None)
def testTotalFalse(self):
"""Test complete rpc failure"""
......@@ -519,7 +519,8 @@ class TestHooksRunnerEnv(unittest.TestCase):
self.op = opcodes.OpTestDummy(result=False, messages=[], fail=False)
self.lu = FakeEnvWithCustomPostHookNodesLU(FakeProc(), self.op,
FakeContext(), None,
FakeContext(), FakeConfig(),
None,
(123, "/foo/bar"),
None)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment