1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246 |
- import unittest
- import os
- from isc.config.ccsession import *
- from isc.config.config_data import BIND10_CONFIG_DATA_VERSION
- from unittest_fakesession import FakeModuleCCSession, WouldBlockForever
- import bind10_config
- import isc.log
- class TestHelperFunctions(unittest.TestCase):
- def test_parse_answer(self):
- self.assertRaises(ModuleCCSessionError, parse_answer, 1)
- self.assertRaises(ModuleCCSessionError, parse_answer, { 'just a dict': 1 })
- self.assertRaises(ModuleCCSessionError, parse_answer, { 'result': 1 })
- self.assertRaises(ModuleCCSessionError, parse_answer, { 'result': [] })
- self.assertRaises(ModuleCCSessionError, parse_answer, { 'result': [ 'not_an_rcode' ] })
- self.assertRaises(ModuleCCSessionError, parse_answer, { 'result': [ 1, 2 ] })
- rcode, val = parse_answer({ 'result': [ 0 ] })
- self.assertEqual(0, rcode)
- self.assertEqual(None, val)
- rcode, val = parse_answer({ 'result': [ 0, "something" ] })
- self.assertEqual(0, rcode)
- self.assertEqual("something", val)
- rcode, val = parse_answer({ 'result': [ 1, "some error" ] })
- self.assertEqual(1, rcode)
- self.assertEqual("some error", val)
- def test_create_answer(self):
- self.assertRaises(ModuleCCSessionError, create_answer, 'not_an_int')
- self.assertRaises(ModuleCCSessionError, create_answer, 1, 2)
- self.assertRaises(ModuleCCSessionError, create_answer, 1)
- self.assertEqual({ 'result': [ 0 ] }, create_answer(0))
- self.assertEqual({ 'result': [ 1, 'something bad' ] }, create_answer(1, 'something bad'))
- self.assertEqual({ 'result': [ 0, 'something good' ] }, create_answer(0, 'something good'))
- self.assertEqual({ 'result': [ 0, ['some', 'list' ] ] }, create_answer(0, ['some', 'list']))
- self.assertEqual({ 'result': [ 0, {'some': 'map' } ] }, create_answer(0, {'some': 'map'}))
- def test_parse_command(self):
- cmd, arg = parse_command(1)
- self.assertEqual(None, cmd)
- self.assertEqual(None, arg)
- cmd, arg = parse_command({})
- self.assertEqual(None, cmd)
- self.assertEqual(None, arg)
- cmd, arg = parse_command({ 'not a command': 1})
- self.assertEqual(None, cmd)
- self.assertEqual(None, arg)
- cmd, arg = parse_command({ 'command': 1})
- self.assertEqual(None, cmd)
- self.assertEqual(None, arg)
- cmd, arg = parse_command({ 'command': []})
- self.assertEqual(None, cmd)
- self.assertEqual(None, arg)
- cmd, arg = parse_command({ 'command': [ 1 ]})
- self.assertEqual(None, cmd)
- self.assertEqual(None, arg)
- cmd, arg = parse_command({ 'command': [ 'command' ]})
- self.assertEqual('command', cmd)
- self.assertEqual(None, arg)
- cmd, arg = parse_command({ 'command': [ 'command', 1 ]})
- self.assertEqual('command', cmd)
- self.assertEqual(1, arg)
- cmd, arg = parse_command({ 'command': [ 'command', ['some', 'argument', 'list'] ]})
- self.assertEqual('command', cmd)
- self.assertEqual(['some', 'argument', 'list'], arg)
- def test_create_command(self):
- self.assertRaises(ModuleCCSessionError, create_command, 1)
- self.assertEqual({'command': [ 'my_command' ]}, create_command('my_command'))
- self.assertEqual({'command': [ 'my_command', 1 ]}, create_command('my_command', 1))
- self.assertEqual({'command': [ 'my_command', [ 'some', 'list' ] ]}, create_command('my_command', [ 'some', 'list' ]))
- self.assertEqual({'command': [ 'my_command', { 'some': 'map' } ]}, create_command('my_command', { 'some': 'map' }))
- class TestModuleCCSession(unittest.TestCase):
- def setUp(self):
- if 'CONFIG_TESTDATA_PATH' in os.environ:
- self.data_path = os.environ['CONFIG_TESTDATA_PATH']
- else:
- self.data_path = "../../../testdata"
- def spec_file(self, file):
- return self.data_path + os.sep + file
- def create_session(self, spec_file_name, config_handler = None,
- command_handler = None, cc_session = None):
- return ModuleCCSession(self.spec_file(spec_file_name),
- config_handler, command_handler,
- cc_session, False)
- def test_init(self):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- self.assertEqual(isc.config.module_spec_from_file(self.spec_file("spec1.spec"))._module_spec, mccs.specification._module_spec)
- self.assertEqual(None, mccs._config_handler)
- self.assertEqual(None, mccs._command_handler)
- def test_start1(self):
- fake_session = FakeModuleCCSession()
- self.assertFalse("Spec1" in fake_session.subscriptions)
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- self.assertTrue("Spec1" in fake_session.subscriptions)
- self.assertEqual(len(fake_session.message_queue), 0)
- fake_session.group_sendmsg(None, 'Spec1')
- fake_session.group_sendmsg(None, 'Spec1')
- self.assertRaises(ModuleCCSessionError, mccs.start)
- self.assertEqual(len(fake_session.message_queue), 2)
- self.assertEqual({'command': ['module_spec', {'module_name': 'Spec1'}]},
- fake_session.get_message('ConfigManager', None))
- self.assertEqual({'command': ['get_config', {'module_name': 'Spec1'}]},
- fake_session.get_message('ConfigManager', None))
- self.assertEqual(len(fake_session.message_queue), 0)
- fake_session.group_sendmsg({'result': [ 0 ]}, "Spec1")
- fake_session.group_sendmsg({'result': [ 0 ]}, "Spec1")
- mccs.start()
- self.assertEqual(len(fake_session.message_queue), 2)
- self.assertEqual({'command': ['module_spec', {'module_name': 'Spec1'}]},
- fake_session.get_message('ConfigManager', None))
- self.assertEqual({'command': ['get_config', {'module_name': 'Spec1'}]},
- fake_session.get_message('ConfigManager', None))
- mccs = None
- self.assertFalse("Spec1" in fake_session.subscriptions)
- 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)
- fake_session.group_sendmsg(None, 'Spec2')
- fake_session.group_sendmsg(None, 'Spec2')
- 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)
- fake_session.group_sendmsg(None, 'Spec2')
- fake_session.group_sendmsg(None, 'Spec2')
- 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)
- fake_session.group_sendmsg(None, 'Spec2')
- fake_session.group_sendmsg(None, 'Spec2')
- 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_start5(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)
- fake_session.group_sendmsg(None, 'Spec2')
- fake_session.group_sendmsg(None, 'Spec2')
- 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, {"Wrong": True} ]}, "Spec2")
- 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))
- def test_stop(self):
- fake_session = FakeModuleCCSession()
- self.assertFalse("Spec1" in fake_session.subscriptions)
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- self.assertTrue("Spec1" in fake_session.subscriptions)
- self.assertEqual(len(fake_session.message_queue), 0)
- mccs.send_stopping()
- self.assertEqual(len(fake_session.message_queue), 1)
- self.assertEqual({'command': ['stopping', {'module_name': 'Spec1'}]},
- fake_session.get_message('ConfigManager', None))
- def test_get_socket(self):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- self.assertNotEqual(None, mccs.get_socket())
- def test_get_session(self):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- self.assertEqual(fake_session, mccs._session)
- def test_close(self):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- mccs.close()
- self.assertEqual(None, fake_session._socket)
- def test_del_opened(self):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- mccs.__del__()
- def test_del_closed(self):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- fake_session.close()
- mccs.__del__()
- def rpc_check(self, reply):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- fake_session.message_queue = [
- ["Spec1", None, reply, False]
- ]
- exception = None
- try:
- result = mccs.rpc_call("test", "Spec2", param1="Param 1",
- param2="Param 2")
- except Exception as e:
-
-
- exception = e
- self.assertEqual([
- ["Spec2", "*", {"command": ["test", {
- "param1": "Param 1",
- "param2": "Param 2"
- }]}, True]
- ], fake_session.message_queue)
- if exception is not None:
- raise exception
- return result
- def test_rpc_call_success(self):
- """
- Test we can send an RPC (command) and get an answer. The answer is
- success in this case.
- """
- result = self.rpc_check({"result": [0, {"Hello": "a"}]})
- self.assertEqual({"Hello": "a"}, result)
- def test_rpc_call_success_none(self):
- """
- Test the success case of RPC command, but the answer is empty
- (eg. a "void" function on the remote side).
- """
- self.assertIsNone(self.rpc_check({"result": [0]}))
- def test_rpc_call_malformed_answer(self):
- """
- Test it successfully raises ModuleCCSessionError when a malformed
- reply is sent.
- """
- self.assertRaises(ModuleCCSessionError, self.rpc_check, ["Nonsense"])
- 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)
- mccs.check_command()
- self.assertEqual(len(fake_session.message_queue), 0)
- 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, { '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, { '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, 'aaa should be an integer']},
- 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, { '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, 'aaa should be an integer']},
- 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))
- """Many check_command tests look too similar, this is common body."""
- def common_check_command_check(self, cmd_handler,
- cmd_check=lambda mccs, _: mccs.check_command()):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec2.spec", None, None, fake_session)
- mccs.set_command_handler(cmd_handler)
- 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)
- cmd_check(mccs, fake_session)
- return fake_session
- def test_check_command7(self):
- fake_session = self.common_check_command_check(
- self.my_command_handler_ok)
- 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 = self.common_check_command_check(
- self.my_command_handler_no_answer)
- self.assertEqual(len(fake_session.message_queue), 0)
- def test_check_command_block(self):
- """See if the message gets there even in blocking mode."""
- fake_session = self.common_check_command_check(
- self.my_command_handler_ok,
- lambda mccs, _: mccs.check_command(False))
- self.assertEqual(len(fake_session.message_queue), 1)
- self.assertEqual({'result': [0]},
- fake_session.get_message('Spec2', None))
- def test_check_command_block_timeout(self):
- """Check it works if session has timeout and it sets it back."""
- def cmd_check(mccs, session):
- session.set_timeout(1)
- mccs.check_command(False)
- fake_session = self.common_check_command_check(
- self.my_command_handler_ok, cmd_check)
- self.assertEqual(len(fake_session.message_queue), 1)
- self.assertEqual({'result': [0]},
- fake_session.get_message('Spec2', None))
- self.assertEqual(fake_session.get_timeout(), 1)
- def test_check_command_blocks_forever(self):
- """Check it would wait forever checking a command."""
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec2.spec", None, None, fake_session)
- mccs.set_command_handler(self.my_command_handler_ok)
- self.assertRaises(WouldBlockForever, lambda: mccs.check_command(False))
- def test_check_command_blocks_forever_timeout(self):
- """Like above, but it should wait forever even with timeout here."""
- fake_session = FakeModuleCCSession()
- fake_session.set_timeout(1)
- mccs = self.create_session("spec2.spec", None, None, fake_session)
- mccs.set_command_handler(self.my_command_handler_ok)
- self.assertRaises(WouldBlockForever, lambda: mccs.check_command(False))
- def test_check_command_without_recvmsg1(self):
- "copied from test_check_command2"
- 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': 'abcd' })
- env = { 'group': 'Spec1', 'from':None };
- mccs.check_command_without_recvmsg(cmd, env)
- 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_command_without_recvmsg2(self):
- "copied from test_check_command3"
- 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, { 'item1': 2 })
- self.assertEqual(len(fake_session.message_queue), 0)
- env = { 'group':'Spec2', 'from':None }
- mccs.check_command_without_recvmsg(cmd, env)
- self.assertEqual(len(fake_session.message_queue), 1)
- self.assertEqual({'result': [0]},
- fake_session.get_message('Spec2', None))
- def test_check_command_without_recvmsg3(self):
- "copied from test_check_command7"
- 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")
- env = { 'group':'Spec2', 'from':None }
- self.assertEqual(len(fake_session.message_queue), 0)
- mccs.check_command_without_recvmsg(cmd, env)
- self.assertEqual({'result': [0]},
- fake_session.get_message('Spec2', None))
- def test_check_command_block_timeout(self):
- """Check it works if session has timeout and it sets it back."""
- def cmd_check(mccs, session):
- session.set_timeout(1)
- mccs.check_command(False)
- fake_session = self.common_check_command_check(
- self.my_command_handler_ok, cmd_check)
- self.assertEqual(len(fake_session.message_queue), 1)
- self.assertEqual({'result': [0]},
- fake_session.get_message('Spec2', None))
- self.assertEqual(fake_session.get_timeout(), 1)
- def test_check_command_blocks_forever(self):
- """Check it would wait forever checking a command."""
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec2.spec", None, None, fake_session)
- mccs.set_command_handler(self.my_command_handler_ok)
- self.assertRaises(WouldBlockForever, lambda: mccs.check_command(False))
- def test_check_command_blocks_forever_timeout(self):
- """Like above, but it should wait forever even with timeout here."""
- fake_session = FakeModuleCCSession()
- fake_session.set_timeout(1)
- mccs = self.create_session("spec2.spec", None, None, fake_session)
- mccs.set_command_handler(self.my_command_handler_ok)
- self.assertRaises(WouldBlockForever, lambda: mccs.check_command(False))
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- def _common_remote_module_test(self, internal_test):
- internal_test(lambda ccs: ccs.add_remote_config,
- self.spec_file("spec2.spec"),
- lambda session: None)
- def _prepare_spec_message(self, session, spec_name):
-
-
- spec_file = self.spec_file(spec_name)
- spec = isc.config.module_spec_from_file(spec_file)
- session.group_sendmsg({'result': [0, spec.get_full_spec()]}, "Spec1")
- def _common_remote_module_by_name_test(self, internal_test):
- internal_test(lambda ccs: ccs.add_remote_config_by_name, "Spec2",
- lambda session: self._prepare_spec_message(session,
- "spec2.spec"))
- def _internal_remote_module(self, function_lambda, parameter,
- fill_other_messages):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- mccs.remove_remote_config("Spec2")
- function = function_lambda(mccs)
- self.assertRaises(ModuleCCSessionError, mccs.get_remote_config_value, "Spec2", "item1")
- self.assertFalse("Spec2" in fake_session.subscriptions)
- fill_other_messages(fake_session)
- fake_session.group_sendmsg(None, 'Spec2')
- rmodname = function(parameter)
- self.assertTrue("Spec2" in fake_session.subscriptions)
- self.assertEqual("Spec2", rmodname)
- self.assertRaises(isc.cc.data.DataNotFoundError, mccs.get_remote_config_value, rmodname, "asdf")
- value, default = mccs.get_remote_config_value(rmodname, "item1")
- self.assertEqual(1, value)
- self.assertEqual(True, default)
- mccs.remove_remote_config(rmodname)
- self.assertFalse("Spec2" in fake_session.subscriptions)
- self.assertRaises(ModuleCCSessionError, mccs.get_remote_config_value, "Spec2", "item1")
-
- fill_other_messages(fake_session)
- fake_session.group_sendmsg({'result' : [0]}, 'Spec2')
- rmodname = function(parameter)
- self.assertTrue("Spec2" in fake_session.subscriptions)
- mccs = None
- function = None
- self.assertFalse("Spec2" in fake_session.subscriptions)
- def test_remote_module(self):
- """
- Test we can add a remote config and get the configuration.
- Remote module specified by the spec file name.
- """
- self._common_remote_module_test(self._internal_remote_module)
- def test_remote_module_by_name(self):
- """
- Test we can add a remote config and get the configuration.
- Remote module specified its name.
- """
- self._common_remote_module_by_name_test(self._internal_remote_module)
- def _internal_remote_module_with_custom_config(self, function_lambda,
- parameter,
- fill_other_messages):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- function = function_lambda(mccs)
-
-
-
- fill_other_messages(fake_session)
- fake_session.group_sendmsg({'result': [0, {"item1": 10}]}, 'Spec2')
- rmodname = function(parameter)
- value, default = mccs.get_remote_config_value(rmodname, "item1")
- self.assertEqual(10, value)
- self.assertEqual(False, default)
- def test_remote_module_with_custom_config(self):
- """
- Test the config of module will load non-default values on
- initialization.
- Remote module specified by the spec file name.
- """
- self._common_remote_module_test(
- self._internal_remote_module_with_custom_config)
- def test_remote_module_by_name_with_custom_config(self):
- """
- Test the config of module will load non-default values on
- initialization.
- Remote module its name.
- """
- self._common_remote_module_by_name_test(
- self._internal_remote_module_with_custom_config)
- def _internal_ignore_command_remote_module(self, function_lambda, param,
- fill_other_messages):
-
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- mccs.set_command_handler(self.my_command_handler_ok)
- function = function_lambda(mccs)
- fill_other_messages(fake_session)
- fake_session.group_sendmsg(None, 'Spec2')
- rmodname = function(param)
-
- while len(fake_session.message_queue) > 0:
- fake_session.get_message("ConfigManager")
-
- cmd = isc.config.ccsession.create_command("just_some_command", { 'foo': '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': [ 0 ]},
- fake_session.get_message('Spec1', None))
-
- cmd = isc.config.ccsession.create_command("just_some_command", { 'foo': 'a' })
- 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)
- def test_ignore_commant_remote_module(self):
- """
- Test that commands for remote modules aren't handled.
- Remote module specified by the spec file name.
- """
- self._common_remote_module_test(
- self._internal_ignore_command_remote_module)
- def test_ignore_commant_remote_module_by_name(self):
- """
- Test that commands for remote modules aren't handled.
- Remote module specified by its name.
- """
- self._common_remote_module_by_name_test(
- self._internal_ignore_command_remote_module)
- def _internal_check_command_without_recvmsg_remote_module(self,
- function_lambda,
- param,
- fill_other_messages):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- mccs.set_config_handler(self.my_config_handler_ok)
- function = function_lambda(mccs)
- self.assertEqual(len(fake_session.message_queue), 0)
- fill_other_messages(fake_session)
- fake_session.group_sendmsg(None, 'Spec2')
- rmodname = function(param)
- if (len(fake_session.message_queue) == 2):
- self.assertEqual({'command': ['get_module_spec',
- {'module_name': 'Spec2'}]},
- 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)
- cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec2': { 'item1': 2 }})
- env = { 'group':'Spec2', 'from':None }
- self.assertEqual(len(fake_session.message_queue), 0)
- mccs.check_command_without_recvmsg(cmd, env)
- self.assertEqual(len(fake_session.message_queue), 0)
- def test_check_command_without_recvmsg_remote_module(self):
- """
- Test updates on remote module.
- The remote module is specified by the spec file name.
- """
- self._common_remote_module_test(
- self._internal_check_command_without_recvmsg_remote_module)
- def test_check_command_without_recvmsg_remote_module_by_name(self):
- """
- Test updates on remote module.
- The remote module is specified by its name.
- """
- self._common_remote_module_by_name_test(
- self._internal_check_command_without_recvmsg_remote_module)
- def _internal_check_command_without_recvmsg_remote_module2(self,
- function_lambda,
- param,
- fill_other_messages):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- mccs.set_config_handler(self.my_config_handler_ok)
- function = function_lambda(mccs)
- self.assertEqual(len(fake_session.message_queue), 0)
- fill_other_messages(fake_session)
- fake_session.group_sendmsg(None, 'Spec2')
- rmodname = function(param)
- if (len(fake_session.message_queue) == 2):
- self.assertEqual({'command': ['get_module_spec',
- {'module_name': 'Spec2'}]},
- 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)
- cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec3': { 'item1': 2 }})
- env = { 'group':'Spec3', 'from':None }
- self.assertEqual(len(fake_session.message_queue), 0)
- mccs.check_command_without_recvmsg(cmd, env)
- self.assertEqual(len(fake_session.message_queue), 0)
- def test_check_command_without_recvmsg_remote_module2(self):
- """
- Test updates on remote module.
- The remote module is specified by the spec file name.
- """
- self._common_remote_module_test(
- self._internal_check_command_without_recvmsg_remote_module2)
- def test_check_command_without_recvmsg_remote_module_by_name2(self):
- """
- Test updates on remote module.
- The remote module is specified by its name.
- """
- self._common_remote_module_by_name_test(
- self._internal_check_command_without_recvmsg_remote_module2)
- def _internal_remote_module_bad_config(self, function_lambda, parameter,
- fill_other_messages):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- function = function_lambda(mccs)
-
- fill_other_messages(fake_session)
- fake_session.group_sendmsg({'result': [0, {"bad_item": -1}]}, 'Spec2')
- self.assertRaises(isc.config.ModuleCCSessionError,
- function, parameter)
- def test_remote_module_bad_config(self):
- """
- Test the remote module rejects bad config data.
- """
- self._common_remote_module_test(
- self._internal_remote_module_bad_config)
- def test_remote_module_by_name_bad_config(self):
- """
- Test the remote module rejects bad config data.
- """
- self._common_remote_module_by_name_test(
- self._internal_remote_module_bad_config)
- def _internal_remote_module_error_response(self, function_lambda,
- parameter, fill_other_messages):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- function = function_lambda(mccs)
-
- fill_other_messages(fake_session)
- fake_session.group_sendmsg({'result': [1, "An error, and I mean it!"]},
- 'Spec2')
- self.assertRaises(isc.config.ModuleCCSessionError,
- function, parameter)
- def test_remote_module_bad_config(self):
- """
- Test the remote module complains if there's an error response."
- """
- self._common_remote_module_test(
- self._internal_remote_module_error_response)
- def test_remote_module_by_name_bad_config(self):
- """
- Test the remote module complains if there's an error response."
- """
- self._common_remote_module_by_name_test(
- self._internal_remote_module_error_response)
- def test_remote_module_bad_config(self):
- """
- Test the remote module rejects bad config data.
- """
- self._common_remote_module_by_name_test(
- self._internal_remote_module_bad_config)
- def test_module_name_mismatch(self):
- fake_session = FakeModuleCCSession()
- mccs = self.create_session("spec1.spec", None, None, fake_session)
- mccs.set_config_handler(self.my_config_handler_ok)
- self._prepare_spec_message(fake_session, 'spec1.spec')
- self.assertRaises(isc.config.ModuleCCSessionError,
- mccs.add_remote_config_by_name, "Spec2")
- def test_logconfig_handler(self):
-
-
-
- self.assertRaises(TypeError, default_logconfig_handler);
- self.assertRaises(TypeError, default_logconfig_handler, 1);
- spec = isc.config.module_spec_from_file(
- path_search('logging.spec', bind10_config.PLUGIN_PATHS))
- config_data = ConfigData(spec)
- self.assertRaises(TypeError, default_logconfig_handler, 1, config_data)
- default_logconfig_handler({}, config_data)
-
-
- default_logconfig_handler({ "bad_data": "indeed" }, config_data)
- default_logconfig_handler({ "bad_data": 1}, config_data)
- default_logconfig_handler({ "bad_data": 1123 }, config_data)
- default_logconfig_handler({ "bad_data": True }, config_data)
- default_logconfig_handler({ "bad_data": False }, config_data)
- default_logconfig_handler({ "bad_data": 1.1 }, config_data)
- default_logconfig_handler({ "bad_data": [] }, config_data)
- default_logconfig_handler({ "bad_data": [[],[],[[1, 3, False, "foo" ]]] },
- config_data)
- default_logconfig_handler({ "bad_data": [ 1, 2, { "b": { "c": "d" } } ] },
- config_data)
-
- log_conf = {"loggers":
- [{"name": "b10-xfrout", "output_options":
- [{"output": "/tmp/bind10.log",
- "destination": "file",
- "flush": True}]}]}
- default_logconfig_handler(log_conf, config_data)
- class fakeData:
- def decode(self):
- return "{}";
- class fakeAnswer:
- def read(self):
- return fakeData();
- class fakeUIConn():
- def __init__(self):
- self.get_answers = {}
- self.post_answers = {}
- def set_get_answer(self, name, answer):
- self.get_answers[name] = answer
- def set_post_answer(self, name, answer):
- self.post_answers[name] = answer
- def send_GET(self, name, arg = None):
- if name in self.get_answers:
- return self.get_answers[name]
- else:
- return {}
- def send_POST(self, name, arg = None):
- if name in self.post_answers:
- return self.post_answers[name]
- else:
- return fakeAnswer()
- class TestUIModuleCCSession(unittest.TestCase):
- def setUp(self):
- if 'CONFIG_TESTDATA_PATH' in os.environ:
- self.data_path = os.environ['CONFIG_TESTDATA_PATH']
- else:
- self.data_path = "../../../testdata"
- def spec_file(self, file):
- return self.data_path + os.sep + file
- def create_uccs(self, fake_conn, specfile="spec2.spec"):
- module_spec = isc.config.module_spec_from_file(self.spec_file(specfile))
- fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
- fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
- return UIModuleCCSession(fake_conn)
- def create_uccs_named_set(self, fake_conn):
- module_spec = isc.config.module_spec_from_file(self.spec_file("spec32.spec"))
- fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
- fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
- return UIModuleCCSession(fake_conn)
- def create_uccs_listtest(self, fake_conn):
- module_spec = isc.config.module_spec_from_file(self.spec_file("spec39.spec"))
- fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
- fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
- return UIModuleCCSession(fake_conn)
- def test_init(self):
- fake_conn = fakeUIConn()
- fake_conn.set_get_answer('/module_spec', {})
- fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
- uccs = UIModuleCCSession(fake_conn)
- self.assertEqual({}, uccs._specifications)
- self.assertEqual({ 'version': BIND10_CONFIG_DATA_VERSION}, uccs._current_config)
- module_spec = isc.config.module_spec_from_file(self.spec_file("spec2.spec"))
- fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
- fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
- uccs = UIModuleCCSession(fake_conn)
- self.assertEqual(module_spec._module_spec, uccs._specifications['Spec2']._module_spec)
- fake_conn.set_get_answer('/config_data', { 'version': 123123 })
- self.assertRaises(ModuleCCSessionError, UIModuleCCSession, fake_conn)
- def test_request_specifications(self):
- module_spec1 = isc.config.module_spec_from_file(
- self.spec_file("spec1.spec"))
- module_spec_dict1 = { "module_spec": module_spec1.get_full_spec() }
- module_spec2 = isc.config.module_spec_from_file(
- self.spec_file("spec2.spec"))
- module_spec_dict2 = { "module_spec": module_spec2.get_full_spec() }
- fake_conn = fakeUIConn()
-
- fake_conn.set_get_answer('/module_spec', module_spec_dict1)
- fake_conn.set_get_answer('/config_data',
- { 'version': BIND10_CONFIG_DATA_VERSION })
- uccs = UIModuleCCSession(fake_conn)
-
- self.assertTrue("Spec1" in uccs._specifications)
- self.assertEqual(module_spec1.get_full_spec(),
- uccs._specifications["Spec1"].get_full_spec())
- self.assertFalse("Spec2" in uccs._specifications)
-
- fake_conn.set_get_answer('/module_spec', module_spec_dict2)
- uccs.request_specifications()
-
- self.assertFalse("Spec1" in uccs._specifications)
- self.assertTrue("Spec2" in uccs._specifications)
- self.assertEqual(module_spec2.get_full_spec(),
- uccs._specifications["Spec2"].get_full_spec())
- def test_add_remove_value(self):
- fake_conn = fakeUIConn()
- uccs = self.create_uccs(fake_conn)
- self.assertRaises(isc.cc.data.DataNotFoundError, uccs.add_value, 1, "a")
- self.assertRaises(isc.cc.data.DataNotFoundError, uccs.add_value, "no_such_item", "a")
- self.assertRaises(isc.cc.data.DataNotFoundError, uccs.remove_value, 1, "a")
- self.assertRaises(isc.cc.data.DataNotFoundError, uccs.remove_value, "no_such_item", "a")
-
-
-
- self.assertRaises(isc.cc.data.DataTypeError, uccs.add_value, "Spec2/item1", "a")
- self.assertRaises(isc.cc.data.DataTypeError, uccs.remove_value, "Spec2/item1", "a")
- self.assertRaises(isc.cc.data.DataTypeError, uccs.add_value, "Spec2", "")
- self.assertRaises(isc.cc.data.DataTypeError, uccs.remove_value, "Spec2", "")
- self.assertEqual({}, uccs._local_changes)
- uccs.add_value("Spec2/item5", "foo")
- self.assertEqual({'Spec2': {'item5': ['a', 'b', 'foo']}}, uccs._local_changes)
- uccs.remove_value("Spec2/item5", "foo")
- self.assertEqual({'Spec2': {'item5': ['a', 'b']}}, uccs._local_changes)
- uccs._local_changes = {'Spec2': {'item5': []}}
- uccs.remove_value("Spec2/item5", "foo")
- uccs.add_value("Spec2/item5", "foo")
- self.assertEqual({'Spec2': {'item5': ['foo']}}, uccs._local_changes)
- self.assertRaises(isc.cc.data.DataAlreadyPresentError,
- uccs.add_value, "Spec2/item5", "foo")
- self.assertEqual({'Spec2': {'item5': ['foo']}}, uccs._local_changes)
- self.assertRaises(isc.cc.data.DataNotFoundError,
- uccs.remove_value, "Spec2/item5[123]", None)
- uccs.remove_value("Spec2/item5[0]", None)
- self.assertEqual({'Spec2': {'item5': []}}, uccs._local_changes)
- uccs.add_value("Spec2/item5", None);
- self.assertEqual({'Spec2': {'item5': ['']}}, uccs._local_changes)
-
- self.assertRaises(isc.cc.data.DataTypeError,
- uccs.remove_value, "Spec2/item5", None)
-
-
- def test_default_null(self):
- fake_conn = fakeUIConn()
- uccs = self.create_uccs(fake_conn, "spec40.spec")
- (value, status) = uccs.get_value("/Spec40/item2")
- self.assertIsNone(value)
- self.assertEqual(uccs.NONE, status)
- (value, status) = uccs.get_value("/Spec40/item3")
- self.assertIsNone(value)
- self.assertEqual(uccs.DEFAULT, status)
-
- def test_add_remove_value_any(self):
- fake_conn = fakeUIConn()
- uccs = self.create_uccs(fake_conn, "spec40.spec")
-
- items = [ 1234, "foo", True, False ]
- items_as_str = [ '1234', 'foo', 'true', 'false' ]
- def test_fails():
- self.assertRaises(isc.cc.data.DataTypeError, uccs.add_value, "Spec40/item1", "foo")
- self.assertRaises(isc.cc.data.DataTypeError, uccs.add_value, "Spec40/item1", "foo", "bar")
- self.assertRaises(isc.cc.data.DataTypeError, uccs.remove_value, "Spec40/item1", "foo")
- self.assertRaises(isc.cc.data.DataTypeError, uccs.remove_value, "Spec40/item1[0]", None)
-
-
- def check_list(identifier):
- for item in items_as_str:
- uccs.add_value(identifier, item)
- self.assertEqual((items, 1), uccs.get_value(identifier))
-
- uccs.remove_value(identifier, "foo")
- uccs.remove_value(identifier + "[1]", None)
- self.assertEqual(([1234, False], 1), uccs.get_value(identifier))
-
- self.assertEqual((1234, 1), uccs.get_value(identifier + "[0]"))
- self.assertEqual((False, 1), uccs.get_value(identifier + "[1]"))
- def check_named_set(identifier):
- for item in items_as_str:
-
- uccs.add_value(identifier, item, item)
- self.assertEqual((1234, 1), uccs.get_value(identifier + "/1234"))
- self.assertEqual((True, 1), uccs.get_value(identifier + "/true"))
- for item in items_as_str:
-
- uccs.remove_value(identifier, item)
-
- for item in items:
- uccs.set_value("Spec40/item1", item)
- test_fails()
-
-
- uccs.set_value("Spec40/item1", [])
- check_list("Spec40/item1")
-
- uccs.set_value("Spec40/item1", {})
- check_named_set("Spec40/item1")
-
- uccs.set_value("Spec40/item1", { "foo": {}, "bar": [] })
- check_named_set("Spec40/item1/foo")
- check_list("Spec40/item1/bar")
- uccs.set_value("Spec40/item1", [ {}, [] ] )
- check_named_set("Spec40/item1[0]")
- check_list("Spec40/item1[1]")
- uccs.set_value("Spec40/item1", [[[[[[]]]]]] )
- check_list("Spec40/item1[0][0][0][0][0]")
- uccs.set_value("Spec40/item1", { 'a': { 'a': { 'a': {} } } } )
- check_named_set("Spec40/item1/a/a/a")
- def test_add_dup_value(self):
- fake_conn = fakeUIConn()
- uccs = self.create_uccs_listtest(fake_conn)
- uccs.add_value("Spec39/list")
- self.assertRaises(isc.cc.data.DataAlreadyPresentError, uccs.add_value,
- "Spec39/list")
- def test_add_remove_value_named_set(self):
- fake_conn = fakeUIConn()
- uccs = self.create_uccs_named_set(fake_conn)
- value, status = uccs.get_value("/Spec32/named_set_item")
- self.assertEqual({'a': 1, 'b': 2}, value)
-
- uccs.remove_value("/Spec32/named_set_item", "a")
- value, status = uccs.get_value("/Spec32/named_set_item")
- self.assertEqual({'b': 2}, value)
- self.assertEqual(uccs.LOCAL, status)
-
- uccs.add_value("/Spec32/named_set_item", "a")
- uccs.set_value("/Spec32/named_set_item/a", 1)
- uccs.add_value("/Spec32/named_set_item", "foo")
- value, status = uccs.get_value("/Spec32/named_set_item")
- self.assertEqual({'a': 1, 'b': 2, 'foo': 3}, value)
- uccs.remove_value("/Spec32/named_set_item", "a")
- uccs.remove_value("/Spec32/named_set_item", "foo")
- value, status = uccs.get_value("/Spec32/named_set_item")
- self.assertEqual({'b': 2}, value)
- uccs.set_value("/Spec32/named_set_item/c", 5)
- value, status = uccs.get_value("/Spec32/named_set_item")
- self.assertEqual({"b": 2, "c": 5}, value)
- self.assertRaises(isc.cc.data.DataNotFoundError,
- uccs.set_value,
- "/Spec32/named_set_item/no_such_item/a",
- 4)
- self.assertRaises(isc.cc.data.DataNotFoundError,
- uccs.remove_value, "/Spec32/named_set_item",
- "no_such_item")
- self.assertRaises(isc.cc.data.DataAlreadyPresentError,
- uccs.add_value, "/Spec32/named_set_item", "c")
- def test_set_value_named_set(self):
- fake_conn = fakeUIConn()
- uccs = self.create_uccs_named_set(fake_conn)
- value, status = uccs.get_value("/Spec32/named_set_item2")
- self.assertEqual({}, value)
- self.assertEqual(status, uccs.DEFAULT)
-
- uccs.add_value("/Spec32/named_set_item2", "new1")
- uccs.set_value("/Spec32/named_set_item2/new1/first", 3)
-
- uccs.set_value("/Spec32/named_set_item2/new2", { "second": 4 })
- value, status = uccs.get_value("/Spec32/named_set_item2")
- self.assertEqual({ "new1": {"first": 3 }, "new2": {"second": 4}},
- value)
- self.assertEqual(status, uccs.LOCAL)
- uccs.set_value("/Spec32/named_set_item2/new1/second", "foo")
- value, status = uccs.get_value("/Spec32/named_set_item2")
- self.assertEqual({ "new1": {"first": 3, "second": "foo" },
- "new2": {"second": 4}},
- value)
- self.assertEqual(status, uccs.LOCAL)
-
- self.assertRaises(isc.cc.data.DataNotFoundError, uccs.set_value,
- "/Spec32/named_set_item2/doesnotexist/first", 3)
- def test_commit(self):
- fake_conn = fakeUIConn()
- uccs = self.create_uccs(fake_conn)
- uccs.commit()
- uccs._local_changes = {'Spec2': {'item5': [ 'a' ]}}
- uccs.commit()
- if __name__ == '__main__':
- isc.log.init("bind10")
- unittest.main()
|