Commit 1ff86230 authored by Jelte Jansen's avatar Jelte Jansen
Browse files

unittests complete for ModuleCCSession (python version)


git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@1000 e5f2f494-b856-4b98-b285-d166d9295462
parent 718c75b9
......@@ -177,7 +177,7 @@ class ModuleCCSession(ConfigData):
there is."""
msg, env = self._session.group_recvmsg(False)
# should we default to an answer? success-by-default? unhandled error?
if msg:
if msg and not 'result' in msg:
answer = None
try:
cmd, arg = isc.config.ccsession.parse_command(msg)
......@@ -191,7 +191,10 @@ class ModuleCCSession(ConfigData):
else:
answer = self._config_handler(new_config)
else:
answer = self._command_handler(cmd, arg)
if self._command_handler:
answer = self._command_handler(cmd, arg)
else:
answer = create_answer(2, self._module_name + " has no command handler")
except Exception as exc:
answer = create_answer(1, str(exc))
if answer:
......
......@@ -137,6 +137,74 @@ class TestModuleCCSession(unittest.TestCase):
self.assertEqual({'command': ['get_config', {'module_name': 'Spec1'}]},
fake_session.get_message('ConfigManager', None))
def test_start2(self):
fake_session = FakeModuleCCSession()
mccs = self.create_session("spec2.spec", None, None, fake_session)
self.assertEqual(len(fake_session.message_queue), 0)
self.assertRaises(ModuleCCSessionError, mccs.start)
self.assertEqual(len(fake_session.message_queue), 2)
self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
fake_session.get_message('ConfigManager', None))
self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
fake_session.get_message('ConfigManager', None))
self.assertEqual(len(fake_session.message_queue), 0)
fake_session.group_sendmsg({'result': [ 0 ]}, "Spec2")
fake_session.group_sendmsg({'result': [ 0, {} ]}, "Spec2")
mccs.start()
self.assertEqual(len(fake_session.message_queue), 2)
self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
fake_session.get_message('ConfigManager', None))
self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
fake_session.get_message('ConfigManager', None))
def test_start3(self):
fake_session = FakeModuleCCSession()
mccs = self.create_session("spec2.spec", None, None, fake_session)
mccs.set_config_handler(self.my_config_handler_ok)
self.assertEqual(len(fake_session.message_queue), 0)
self.assertRaises(ModuleCCSessionError, mccs.start)
self.assertEqual(len(fake_session.message_queue), 2)
self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
fake_session.get_message('ConfigManager', None))
self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
fake_session.get_message('ConfigManager', None))
self.assertEqual(len(fake_session.message_queue), 0)
fake_session.group_sendmsg({'result': [ 0 ]}, "Spec2")
fake_session.group_sendmsg({'result': [ 0, {} ]}, "Spec2")
mccs.start()
self.assertEqual(len(fake_session.message_queue), 2)
self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
fake_session.get_message('ConfigManager', None))
self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
fake_session.get_message('ConfigManager', None))
def test_start4(self):
fake_session = FakeModuleCCSession()
mccs = self.create_session("spec2.spec", None, None, fake_session)
mccs.set_config_handler(self.my_config_handler_ok)
self.assertEqual(len(fake_session.message_queue), 0)
self.assertRaises(ModuleCCSessionError, mccs.start)
self.assertEqual(len(fake_session.message_queue), 2)
self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
fake_session.get_message('ConfigManager', None))
self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
fake_session.get_message('ConfigManager', None))
self.assertEqual(len(fake_session.message_queue), 0)
fake_session.group_sendmsg({'result': [ 0 ]}, "Spec2")
fake_session.group_sendmsg({'result': [ 1, "just an error" ]}, "Spec2")
mccs.start()
self.assertEqual(len(fake_session.message_queue), 2)
self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
fake_session.get_message('ConfigManager', None))
self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
fake_session.get_message('ConfigManager', None))
def test_get_socket(self):
fake_session = FakeModuleCCSession()
mccs = self.create_session("spec1.spec", None, None, fake_session)
......@@ -153,7 +221,22 @@ class TestModuleCCSession(unittest.TestCase):
mccs.close()
self.assertEqual("closed", fake_session._socket)
def test_check_command(self):
def my_config_handler_ok(self, new_config):
return isc.config.ccsession.create_answer(0)
def my_config_handler_err(self, new_config):
return isc.config.ccsession.create_answer(1, "just an error")
def my_config_handler_exc(self, new_config):
raise Exception("just an exception")
def my_command_handler_ok(self, command, args):
return isc.config.ccsession.create_answer(0)
def my_command_handler_no_answer(self, command, args):
pass
def test_check_command1(self):
fake_session = FakeModuleCCSession()
mccs = self.create_session("spec1.spec", None, None, fake_session)
......@@ -163,6 +246,102 @@ class TestModuleCCSession(unittest.TestCase):
fake_session.group_sendmsg({'result': [ 0 ]}, "Spec1")
mccs.check_command()
self.assertEqual(len(fake_session.message_queue), 0)
cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec1': 'a' })
fake_session.group_sendmsg(cmd, 'Spec1')
mccs.check_command()
self.assertEqual(len(fake_session.message_queue), 1)
self.assertEqual({'result': [2, 'Spec1 has no config handler']},
fake_session.get_message('Spec1', None))
def test_check_command2(self):
fake_session = FakeModuleCCSession()
mccs = self.create_session("spec1.spec", None, None, fake_session)
mccs.set_config_handler(self.my_config_handler_ok)
self.assertEqual(len(fake_session.message_queue), 0)
cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec1': 'a' })
fake_session.group_sendmsg(cmd, 'Spec1')
self.assertEqual(len(fake_session.message_queue), 1)
mccs.check_command()
self.assertEqual(len(fake_session.message_queue), 1)
self.assertEqual({'result': [1, 'No config_data specification']},
fake_session.get_message('Spec1', None))
def test_check_command3(self):
fake_session = FakeModuleCCSession()
mccs = self.create_session("spec2.spec", None, None, fake_session)
mccs.set_config_handler(self.my_config_handler_ok)
self.assertEqual(len(fake_session.message_queue), 0)
cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec2': { 'item1': 2 }})
fake_session.group_sendmsg(cmd, 'Spec2')
self.assertEqual(len(fake_session.message_queue), 1)
mccs.check_command()
self.assertEqual(len(fake_session.message_queue), 1)
self.assertEqual({'result': [0]},
fake_session.get_message('Spec2', None))
def test_check_command4(self):
fake_session = FakeModuleCCSession()
mccs = self.create_session("spec2.spec", None, None, fake_session)
mccs.set_config_handler(self.my_config_handler_err)
self.assertEqual(len(fake_session.message_queue), 0)
cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec2': { 'item1': 'aaa' }})
fake_session.group_sendmsg(cmd, 'Spec2')
self.assertEqual(len(fake_session.message_queue), 1)
mccs.check_command()
self.assertEqual(len(fake_session.message_queue), 1)
self.assertEqual({'result': [1, 'just an error']},
fake_session.get_message('Spec2', None))
def test_check_command5(self):
fake_session = FakeModuleCCSession()
mccs = self.create_session("spec2.spec", None, None, fake_session)
mccs.set_config_handler(self.my_config_handler_exc)
self.assertEqual(len(fake_session.message_queue), 0)
cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec2': { 'item1': 'aaa' }})
fake_session.group_sendmsg(cmd, 'Spec2')
self.assertEqual(len(fake_session.message_queue), 1)
mccs.check_command()
self.assertEqual(len(fake_session.message_queue), 1)
self.assertEqual({'result': [1, 'just an exception']},
fake_session.get_message('Spec2', None))
def test_check_command6(self):
fake_session = FakeModuleCCSession()
mccs = self.create_session("spec2.spec", None, None, fake_session)
self.assertEqual(len(fake_session.message_queue), 0)
cmd = isc.config.ccsession.create_command("print_message", "just a message")
fake_session.group_sendmsg(cmd, 'Spec2')
self.assertEqual(len(fake_session.message_queue), 1)
mccs.check_command()
self.assertEqual(len(fake_session.message_queue), 1)
self.assertEqual({'result': [2, 'Spec2 has no command handler']},
fake_session.get_message('Spec2', None))
def test_check_command7(self):
fake_session = FakeModuleCCSession()
mccs = self.create_session("spec2.spec", None, None, fake_session)
mccs.set_command_handler(self.my_command_handler_ok)
self.assertEqual(len(fake_session.message_queue), 0)
cmd = isc.config.ccsession.create_command("print_message", "just a message")
fake_session.group_sendmsg(cmd, 'Spec2')
self.assertEqual(len(fake_session.message_queue), 1)
mccs.check_command()
self.assertEqual(len(fake_session.message_queue), 1)
self.assertEqual({'result': [0]},
fake_session.get_message('Spec2', None))
def test_check_command8(self):
fake_session = FakeModuleCCSession()
mccs = self.create_session("spec2.spec", None, None, fake_session)
mccs.set_command_handler(self.my_command_handler_no_answer)
self.assertEqual(len(fake_session.message_queue), 0)
cmd = isc.config.ccsession.create_command("print_message", "just a message")
fake_session.group_sendmsg(cmd, 'Spec2')
self.assertEqual(len(fake_session.message_queue), 1)
mccs.check_command()
self.assertEqual(len(fake_session.message_queue), 0)
if __name__ == '__main__':
unittest.main()
......
......@@ -313,11 +313,8 @@ class ConfigManager:
def handle_msg(self, msg):
"""Handle a command from the cc channel to the configuration manager"""
answer = {}
#print("[XX] got msg:")
#print(msg)
cmd, arg = isc.config.ccsession.parse_command(msg)
if cmd:
#print("[XX] cmd: " + cmd)
if cmd == isc.config.ccsession.COMMAND_GET_COMMANDS_SPEC:
answer = isc.config.ccsession.create_answer(0, self.get_commands_spec())
elif cmd == isc.config.ccsession.COMMAND_GET_MODULE_SPEC:
......@@ -347,7 +344,7 @@ class ConfigManager:
self.running = True
while (self.running):
msg, env = self.cc.group_recvmsg(False)
if msg:
if msg and not 'result' in msg:
answer = self.handle_msg(msg);
self.cc.group_reply(env, answer)
else:
......
......@@ -76,7 +76,12 @@ class ModuleSpec:
validate only a part of a configuration tree (like a list of
non-default values)"""
data_def = self.get_config_spec()
return _validate_spec_list(data_def, full, data, errors)
if data_def:
return _validate_spec_list(data_def, full, data, errors)
else:
# no spec, always bad
errors.append("No config_data specification")
return False
def get_module_name(self):
......
......@@ -30,13 +30,14 @@ class FakeModuleCCSession:
self.message_queue.append([ channel, target, msg ])
def group_reply(self, env, msg):
pass
if 'to' in env:
self.message_queue.append([ env['to'], None, msg])
def group_recvmsg(self, blocking):
for qm in self.message_queue:
if qm[0] in self.subscriptions and (qm[1] == None or qm[1] in self.subscriptions[qm[0]]):
self.message_queue.remove(qm)
return qm[2], {}
return qm[2], {'to': qm[0], 'from': qm[1]}
return None, None
def get_message(self, channel, target = 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