ccsession_test.py 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. # Copyright (C) 2010 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. #
  16. # Tests for the ConfigData and MultiConfigData classes
  17. #
  18. import unittest
  19. import os
  20. from isc.config.ccsession import *
  21. from isc.config.config_data import BIND10_CONFIG_DATA_VERSION
  22. from unittest_fakesession import FakeModuleCCSession, WouldBlockForever
  23. import bind10_config
  24. import isc.log
  25. class TestHelperFunctions(unittest.TestCase):
  26. def test_parse_answer(self):
  27. self.assertRaises(ModuleCCSessionError, parse_answer, 1)
  28. self.assertRaises(ModuleCCSessionError, parse_answer, { 'just a dict': 1 })
  29. self.assertRaises(ModuleCCSessionError, parse_answer, { 'result': 1 })
  30. self.assertRaises(ModuleCCSessionError, parse_answer, { 'result': [] })
  31. self.assertRaises(ModuleCCSessionError, parse_answer, { 'result': [ 'not_an_rcode' ] })
  32. self.assertRaises(ModuleCCSessionError, parse_answer, { 'result': [ 1, 2 ] })
  33. rcode, val = parse_answer({ 'result': [ 0 ] })
  34. self.assertEqual(0, rcode)
  35. self.assertEqual(None, val)
  36. rcode, val = parse_answer({ 'result': [ 0, "something" ] })
  37. self.assertEqual(0, rcode)
  38. self.assertEqual("something", val)
  39. rcode, val = parse_answer({ 'result': [ 1, "some error" ] })
  40. self.assertEqual(1, rcode)
  41. self.assertEqual("some error", val)
  42. def test_create_answer(self):
  43. self.assertRaises(ModuleCCSessionError, create_answer, 'not_an_int')
  44. self.assertRaises(ModuleCCSessionError, create_answer, 1, 2)
  45. self.assertRaises(ModuleCCSessionError, create_answer, 1)
  46. self.assertEqual({ 'result': [ 0 ] }, create_answer(0))
  47. self.assertEqual({ 'result': [ 1, 'something bad' ] }, create_answer(1, 'something bad'))
  48. self.assertEqual({ 'result': [ 0, 'something good' ] }, create_answer(0, 'something good'))
  49. self.assertEqual({ 'result': [ 0, ['some', 'list' ] ] }, create_answer(0, ['some', 'list']))
  50. self.assertEqual({ 'result': [ 0, {'some': 'map' } ] }, create_answer(0, {'some': 'map'}))
  51. def test_parse_command(self):
  52. cmd, arg = parse_command(1)
  53. self.assertEqual(None, cmd)
  54. self.assertEqual(None, arg)
  55. cmd, arg = parse_command({})
  56. self.assertEqual(None, cmd)
  57. self.assertEqual(None, arg)
  58. cmd, arg = parse_command({ 'not a command': 1})
  59. self.assertEqual(None, cmd)
  60. self.assertEqual(None, arg)
  61. cmd, arg = parse_command({ 'command': 1})
  62. self.assertEqual(None, cmd)
  63. self.assertEqual(None, arg)
  64. cmd, arg = parse_command({ 'command': []})
  65. self.assertEqual(None, cmd)
  66. self.assertEqual(None, arg)
  67. cmd, arg = parse_command({ 'command': [ 1 ]})
  68. self.assertEqual(None, cmd)
  69. self.assertEqual(None, arg)
  70. cmd, arg = parse_command({ 'command': [ 'command' ]})
  71. self.assertEqual('command', cmd)
  72. self.assertEqual(None, arg)
  73. cmd, arg = parse_command({ 'command': [ 'command', 1 ]})
  74. self.assertEqual('command', cmd)
  75. self.assertEqual(1, arg)
  76. cmd, arg = parse_command({ 'command': [ 'command', ['some', 'argument', 'list'] ]})
  77. self.assertEqual('command', cmd)
  78. self.assertEqual(['some', 'argument', 'list'], arg)
  79. def test_create_command(self):
  80. self.assertRaises(ModuleCCSessionError, create_command, 1)
  81. self.assertEqual({'command': [ 'my_command' ]}, create_command('my_command'))
  82. self.assertEqual({'command': [ 'my_command', 1 ]}, create_command('my_command', 1))
  83. self.assertEqual({'command': [ 'my_command', [ 'some', 'list' ] ]}, create_command('my_command', [ 'some', 'list' ]))
  84. self.assertEqual({'command': [ 'my_command', { 'some': 'map' } ]}, create_command('my_command', { 'some': 'map' }))
  85. class TestModuleCCSession(unittest.TestCase):
  86. def setUp(self):
  87. if 'CONFIG_TESTDATA_PATH' in os.environ:
  88. self.data_path = os.environ['CONFIG_TESTDATA_PATH']
  89. else:
  90. self.data_path = "../../../testdata"
  91. def spec_file(self, file):
  92. return self.data_path + os.sep + file
  93. def create_session(self, spec_file_name, config_handler = None,
  94. command_handler = None, cc_session = None):
  95. return ModuleCCSession(self.spec_file(spec_file_name),
  96. config_handler, command_handler,
  97. cc_session, False)
  98. def test_init(self):
  99. fake_session = FakeModuleCCSession()
  100. mccs = self.create_session("spec1.spec", None, None, fake_session)
  101. self.assertEqual(isc.config.module_spec_from_file(self.spec_file("spec1.spec"))._module_spec, mccs.specification._module_spec)
  102. self.assertEqual(None, mccs._config_handler)
  103. self.assertEqual(None, mccs._command_handler)
  104. def test_start1(self):
  105. fake_session = FakeModuleCCSession()
  106. self.assertFalse("Spec1" in fake_session.subscriptions)
  107. mccs = self.create_session("spec1.spec", None, None, fake_session)
  108. self.assertTrue("Spec1" in fake_session.subscriptions)
  109. self.assertEqual(len(fake_session.message_queue), 0)
  110. fake_session.group_sendmsg(None, 'Spec1')
  111. fake_session.group_sendmsg(None, 'Spec1')
  112. self.assertRaises(ModuleCCSessionError, mccs.start)
  113. self.assertEqual(len(fake_session.message_queue), 2)
  114. self.assertEqual({'command': ['module_spec', {'module_name': 'Spec1'}]},
  115. fake_session.get_message('ConfigManager', None))
  116. self.assertEqual({'command': ['get_config', {'module_name': 'Spec1'}]},
  117. fake_session.get_message('ConfigManager', None))
  118. self.assertEqual(len(fake_session.message_queue), 0)
  119. fake_session.group_sendmsg({'result': [ 0 ]}, "Spec1")
  120. fake_session.group_sendmsg({'result': [ 0 ]}, "Spec1")
  121. mccs.start()
  122. self.assertEqual(len(fake_session.message_queue), 2)
  123. self.assertEqual({'command': ['module_spec', {'module_name': 'Spec1'}]},
  124. fake_session.get_message('ConfigManager', None))
  125. self.assertEqual({'command': ['get_config', {'module_name': 'Spec1'}]},
  126. fake_session.get_message('ConfigManager', None))
  127. mccs = None
  128. self.assertFalse("Spec1" in fake_session.subscriptions)
  129. def test_start2(self):
  130. fake_session = FakeModuleCCSession()
  131. mccs = self.create_session("spec2.spec", None, None, fake_session)
  132. self.assertEqual(len(fake_session.message_queue), 0)
  133. fake_session.group_sendmsg(None, 'Spec2')
  134. fake_session.group_sendmsg(None, 'Spec2')
  135. self.assertRaises(ModuleCCSessionError, mccs.start)
  136. self.assertEqual(len(fake_session.message_queue), 2)
  137. self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
  138. fake_session.get_message('ConfigManager', None))
  139. self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
  140. fake_session.get_message('ConfigManager', None))
  141. self.assertEqual(len(fake_session.message_queue), 0)
  142. fake_session.group_sendmsg({'result': [ 0 ]}, "Spec2")
  143. fake_session.group_sendmsg({'result': [ 0, {} ]}, "Spec2")
  144. mccs.start()
  145. self.assertEqual(len(fake_session.message_queue), 2)
  146. self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
  147. fake_session.get_message('ConfigManager', None))
  148. self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
  149. fake_session.get_message('ConfigManager', None))
  150. def test_start3(self):
  151. fake_session = FakeModuleCCSession()
  152. mccs = self.create_session("spec2.spec", None, None, fake_session)
  153. mccs.set_config_handler(self.my_config_handler_ok)
  154. self.assertEqual(len(fake_session.message_queue), 0)
  155. fake_session.group_sendmsg(None, 'Spec2')
  156. fake_session.group_sendmsg(None, 'Spec2')
  157. self.assertRaises(ModuleCCSessionError, mccs.start)
  158. self.assertEqual(len(fake_session.message_queue), 2)
  159. self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
  160. fake_session.get_message('ConfigManager', None))
  161. self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
  162. fake_session.get_message('ConfigManager', None))
  163. self.assertEqual(len(fake_session.message_queue), 0)
  164. fake_session.group_sendmsg({'result': [ 0 ]}, "Spec2")
  165. fake_session.group_sendmsg({'result': [ 0, {} ]}, "Spec2")
  166. mccs.start()
  167. self.assertEqual(len(fake_session.message_queue), 2)
  168. self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
  169. fake_session.get_message('ConfigManager', None))
  170. self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
  171. fake_session.get_message('ConfigManager', None))
  172. def test_start4(self):
  173. fake_session = FakeModuleCCSession()
  174. mccs = self.create_session("spec2.spec", None, None, fake_session)
  175. mccs.set_config_handler(self.my_config_handler_ok)
  176. self.assertEqual(len(fake_session.message_queue), 0)
  177. fake_session.group_sendmsg(None, 'Spec2')
  178. fake_session.group_sendmsg(None, 'Spec2')
  179. self.assertRaises(ModuleCCSessionError, mccs.start)
  180. self.assertEqual(len(fake_session.message_queue), 2)
  181. self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
  182. fake_session.get_message('ConfigManager', None))
  183. self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
  184. fake_session.get_message('ConfigManager', None))
  185. self.assertEqual(len(fake_session.message_queue), 0)
  186. fake_session.group_sendmsg({'result': [ 0 ]}, "Spec2")
  187. fake_session.group_sendmsg({'result': [ 1, "just an error" ]}, "Spec2")
  188. mccs.start()
  189. self.assertEqual(len(fake_session.message_queue), 2)
  190. self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
  191. fake_session.get_message('ConfigManager', None))
  192. self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
  193. fake_session.get_message('ConfigManager', None))
  194. def test_start5(self):
  195. fake_session = FakeModuleCCSession()
  196. mccs = self.create_session("spec2.spec", None, None, fake_session)
  197. mccs.set_config_handler(self.my_config_handler_ok)
  198. self.assertEqual(len(fake_session.message_queue), 0)
  199. fake_session.group_sendmsg(None, 'Spec2')
  200. fake_session.group_sendmsg(None, 'Spec2')
  201. self.assertRaises(ModuleCCSessionError, mccs.start)
  202. self.assertEqual(len(fake_session.message_queue), 2)
  203. self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
  204. fake_session.get_message('ConfigManager', None))
  205. self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
  206. fake_session.get_message('ConfigManager', None))
  207. self.assertEqual(len(fake_session.message_queue), 0)
  208. fake_session.group_sendmsg({'result': [ 0 ]}, "Spec2")
  209. fake_session.group_sendmsg({'result': [ 0, {"Wrong": True} ]}, "Spec2")
  210. self.assertRaises(ModuleCCSessionError, mccs.start)
  211. self.assertEqual(len(fake_session.message_queue), 2)
  212. self.assertEqual({'command': ['module_spec', mccs.specification._module_spec]},
  213. fake_session.get_message('ConfigManager', None))
  214. self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
  215. fake_session.get_message('ConfigManager', None))
  216. def test_stop(self):
  217. fake_session = FakeModuleCCSession()
  218. self.assertFalse("Spec1" in fake_session.subscriptions)
  219. mccs = self.create_session("spec1.spec", None, None, fake_session)
  220. self.assertTrue("Spec1" in fake_session.subscriptions)
  221. self.assertEqual(len(fake_session.message_queue), 0)
  222. mccs.send_stopping()
  223. self.assertEqual(len(fake_session.message_queue), 1)
  224. self.assertEqual({'command': ['stopping', {'module_name': 'Spec1'}]},
  225. fake_session.get_message('ConfigManager', None))
  226. def test_get_socket(self):
  227. fake_session = FakeModuleCCSession()
  228. mccs = self.create_session("spec1.spec", None, None, fake_session)
  229. self.assertNotEqual(None, mccs.get_socket())
  230. def test_get_session(self):
  231. fake_session = FakeModuleCCSession()
  232. mccs = self.create_session("spec1.spec", None, None, fake_session)
  233. self.assertEqual(fake_session, mccs._session)
  234. def test_close(self):
  235. fake_session = FakeModuleCCSession()
  236. mccs = self.create_session("spec1.spec", None, None, fake_session)
  237. mccs.close()
  238. self.assertEqual(None, fake_session._socket)
  239. def test_del_opened(self):
  240. fake_session = FakeModuleCCSession()
  241. mccs = self.create_session("spec1.spec", None, None, fake_session)
  242. mccs.__del__() # with opened fake_session
  243. def test_del_closed(self):
  244. fake_session = FakeModuleCCSession()
  245. mccs = self.create_session("spec1.spec", None, None, fake_session)
  246. fake_session.close()
  247. mccs.__del__() # with closed fake_session
  248. def rpc_check(self, reply):
  249. fake_session = FakeModuleCCSession()
  250. mccs = self.create_session("spec1.spec", None, None, fake_session)
  251. fake_session.message_queue = [
  252. ["Spec1", None, reply, False]
  253. ]
  254. exception = None
  255. try:
  256. result = mccs.rpc_call("test", "Spec2", param1="Param 1",
  257. param2="Param 2")
  258. except Exception as e:
  259. # We first want to check the value sent, raise the exception
  260. # afterwards. So store it for a short while.
  261. exception = e
  262. self.assertEqual([
  263. ["Spec2", "*", {"command": ["test", {
  264. "param1": "Param 1",
  265. "param2": "Param 2"
  266. }]}, True]
  267. ], fake_session.message_queue)
  268. if exception is not None:
  269. raise exception
  270. return result
  271. def test_rpc_call_success(self):
  272. """
  273. Test we can send an RPC (command) and get an answer. The answer is
  274. success in this case.
  275. """
  276. result = self.rpc_check({"result": [0, {"Hello": "a"}]})
  277. self.assertEqual({"Hello": "a"}, result)
  278. def test_rpc_call_success_none(self):
  279. """
  280. Test the success case of RPC command, but the answer is empty
  281. (eg. a "void" function on the remote side).
  282. """
  283. self.assertIsNone(self.rpc_check({"result": [0]}))
  284. def test_rpc_call_malformed_answer(self):
  285. """
  286. Test it successfully raises ModuleCCSessionError when a malformed
  287. reply is sent.
  288. """
  289. self.assertRaises(ModuleCCSessionError, self.rpc_check, ["Nonsense"])
  290. def my_config_handler_ok(self, new_config):
  291. return isc.config.ccsession.create_answer(0)
  292. def my_config_handler_err(self, new_config):
  293. return isc.config.ccsession.create_answer(1, "just an error")
  294. def my_config_handler_exc(self, new_config):
  295. raise Exception("just an exception")
  296. def my_command_handler_ok(self, command, args):
  297. return isc.config.ccsession.create_answer(0)
  298. def my_command_handler_no_answer(self, command, args):
  299. pass
  300. def test_check_command1(self):
  301. fake_session = FakeModuleCCSession()
  302. mccs = self.create_session("spec1.spec", None, None, fake_session)
  303. mccs.check_command()
  304. self.assertEqual(len(fake_session.message_queue), 0)
  305. fake_session.group_sendmsg({'result': [ 0 ]}, "Spec1")
  306. mccs.check_command()
  307. self.assertEqual(len(fake_session.message_queue), 0)
  308. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec1': 'a' })
  309. fake_session.group_sendmsg(cmd, 'Spec1')
  310. mccs.check_command()
  311. self.assertEqual(len(fake_session.message_queue), 1)
  312. self.assertEqual({'result': [2, 'Spec1 has no config handler']},
  313. fake_session.get_message('Spec1', None))
  314. def test_check_command2(self):
  315. fake_session = FakeModuleCCSession()
  316. mccs = self.create_session("spec1.spec", None, None, fake_session)
  317. mccs.set_config_handler(self.my_config_handler_ok)
  318. self.assertEqual(len(fake_session.message_queue), 0)
  319. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec1': 'a' })
  320. fake_session.group_sendmsg(cmd, 'Spec1')
  321. self.assertEqual(len(fake_session.message_queue), 1)
  322. mccs.check_command()
  323. self.assertEqual(len(fake_session.message_queue), 1)
  324. self.assertEqual({'result': [1, 'No config_data specification']},
  325. fake_session.get_message('Spec1', None))
  326. def test_check_command3(self):
  327. fake_session = FakeModuleCCSession()
  328. mccs = self.create_session("spec2.spec", None, None, fake_session)
  329. mccs.set_config_handler(self.my_config_handler_ok)
  330. self.assertEqual(len(fake_session.message_queue), 0)
  331. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'item1': 2 })
  332. fake_session.group_sendmsg(cmd, 'Spec2')
  333. self.assertEqual(len(fake_session.message_queue), 1)
  334. mccs.check_command()
  335. self.assertEqual(len(fake_session.message_queue), 1)
  336. self.assertEqual({'result': [0]},
  337. fake_session.get_message('Spec2', None))
  338. def test_check_command4(self):
  339. fake_session = FakeModuleCCSession()
  340. mccs = self.create_session("spec2.spec", None, None, fake_session)
  341. mccs.set_config_handler(self.my_config_handler_err)
  342. self.assertEqual(len(fake_session.message_queue), 0)
  343. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'item1': 'aaa' })
  344. fake_session.group_sendmsg(cmd, 'Spec2')
  345. self.assertEqual(len(fake_session.message_queue), 1)
  346. mccs.check_command()
  347. self.assertEqual(len(fake_session.message_queue), 1)
  348. self.assertEqual({'result': [1, 'aaa should be an integer']},
  349. fake_session.get_message('Spec2', None))
  350. def test_check_command5(self):
  351. fake_session = FakeModuleCCSession()
  352. mccs = self.create_session("spec2.spec", None, None, fake_session)
  353. mccs.set_config_handler(self.my_config_handler_exc)
  354. self.assertEqual(len(fake_session.message_queue), 0)
  355. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'item1': 'aaa' })
  356. fake_session.group_sendmsg(cmd, 'Spec2')
  357. self.assertEqual(len(fake_session.message_queue), 1)
  358. mccs.check_command()
  359. self.assertEqual(len(fake_session.message_queue), 1)
  360. self.assertEqual({'result': [1, 'aaa should be an integer']},
  361. fake_session.get_message('Spec2', None))
  362. def test_check_command6(self):
  363. fake_session = FakeModuleCCSession()
  364. mccs = self.create_session("spec2.spec", None, None, fake_session)
  365. self.assertEqual(len(fake_session.message_queue), 0)
  366. cmd = isc.config.ccsession.create_command("print_message", "just a message")
  367. fake_session.group_sendmsg(cmd, 'Spec2')
  368. self.assertEqual(len(fake_session.message_queue), 1)
  369. mccs.check_command()
  370. self.assertEqual(len(fake_session.message_queue), 1)
  371. self.assertEqual({'result': [2, 'Spec2 has no command handler']},
  372. fake_session.get_message('Spec2', None))
  373. """Many check_command tests look too similar, this is common body."""
  374. def common_check_command_check(self, cmd_handler,
  375. cmd_check=lambda mccs, _: mccs.check_command()):
  376. fake_session = FakeModuleCCSession()
  377. mccs = self.create_session("spec2.spec", None, None, fake_session)
  378. mccs.set_command_handler(cmd_handler)
  379. self.assertEqual(len(fake_session.message_queue), 0)
  380. cmd = isc.config.ccsession.create_command("print_message", "just a message")
  381. fake_session.group_sendmsg(cmd, 'Spec2')
  382. self.assertEqual(len(fake_session.message_queue), 1)
  383. cmd_check(mccs, fake_session)
  384. return fake_session
  385. def test_check_command7(self):
  386. fake_session = self.common_check_command_check(
  387. self.my_command_handler_ok)
  388. self.assertEqual(len(fake_session.message_queue), 1)
  389. self.assertEqual({'result': [0]},
  390. fake_session.get_message('Spec2', None))
  391. def test_check_command8(self):
  392. fake_session = self.common_check_command_check(
  393. self.my_command_handler_no_answer)
  394. self.assertEqual(len(fake_session.message_queue), 0)
  395. def test_check_command_block(self):
  396. """See if the message gets there even in blocking mode."""
  397. fake_session = self.common_check_command_check(
  398. self.my_command_handler_ok,
  399. lambda mccs, _: mccs.check_command(False))
  400. self.assertEqual(len(fake_session.message_queue), 1)
  401. self.assertEqual({'result': [0]},
  402. fake_session.get_message('Spec2', None))
  403. def test_check_command_block_timeout(self):
  404. """Check it works if session has timeout and it sets it back."""
  405. def cmd_check(mccs, session):
  406. session.set_timeout(1)
  407. mccs.check_command(False)
  408. fake_session = self.common_check_command_check(
  409. self.my_command_handler_ok, cmd_check)
  410. self.assertEqual(len(fake_session.message_queue), 1)
  411. self.assertEqual({'result': [0]},
  412. fake_session.get_message('Spec2', None))
  413. self.assertEqual(fake_session.get_timeout(), 1)
  414. def test_check_command_blocks_forever(self):
  415. """Check it would wait forever checking a command."""
  416. fake_session = FakeModuleCCSession()
  417. mccs = self.create_session("spec2.spec", None, None, fake_session)
  418. mccs.set_command_handler(self.my_command_handler_ok)
  419. self.assertRaises(WouldBlockForever, lambda: mccs.check_command(False))
  420. def test_check_command_blocks_forever_timeout(self):
  421. """Like above, but it should wait forever even with timeout here."""
  422. fake_session = FakeModuleCCSession()
  423. fake_session.set_timeout(1)
  424. mccs = self.create_session("spec2.spec", None, None, fake_session)
  425. mccs.set_command_handler(self.my_command_handler_ok)
  426. self.assertRaises(WouldBlockForever, lambda: mccs.check_command(False))
  427. def test_check_command_without_recvmsg1(self):
  428. "copied from test_check_command2"
  429. fake_session = FakeModuleCCSession()
  430. mccs = self.create_session("spec1.spec", None, None, fake_session)
  431. mccs.set_config_handler(self.my_config_handler_ok)
  432. self.assertEqual(len(fake_session.message_queue), 0)
  433. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec1': 'abcd' })
  434. env = { 'group': 'Spec1', 'from':None };
  435. mccs.check_command_without_recvmsg(cmd, env)
  436. self.assertEqual(len(fake_session.message_queue), 1)
  437. self.assertEqual({'result': [1, 'No config_data specification']},
  438. fake_session.get_message('Spec1', None))
  439. def test_check_command_without_recvmsg2(self):
  440. "copied from test_check_command3"
  441. fake_session = FakeModuleCCSession()
  442. mccs = self.create_session("spec2.spec", None, None, fake_session)
  443. mccs.set_config_handler(self.my_config_handler_ok)
  444. self.assertEqual(len(fake_session.message_queue), 0)
  445. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'item1': 2 })
  446. self.assertEqual(len(fake_session.message_queue), 0)
  447. env = { 'group':'Spec2', 'from':None }
  448. mccs.check_command_without_recvmsg(cmd, env)
  449. self.assertEqual(len(fake_session.message_queue), 1)
  450. self.assertEqual({'result': [0]},
  451. fake_session.get_message('Spec2', None))
  452. def test_check_command_without_recvmsg3(self):
  453. "copied from test_check_command7"
  454. fake_session = FakeModuleCCSession()
  455. mccs = self.create_session("spec2.spec", None, None, fake_session)
  456. mccs.set_command_handler(self.my_command_handler_ok)
  457. self.assertEqual(len(fake_session.message_queue), 0)
  458. cmd = isc.config.ccsession.create_command("print_message", "just a message")
  459. env = { 'group':'Spec2', 'from':None }
  460. self.assertEqual(len(fake_session.message_queue), 0)
  461. mccs.check_command_without_recvmsg(cmd, env)
  462. self.assertEqual({'result': [0]},
  463. fake_session.get_message('Spec2', None))
  464. def test_check_command_block_timeout(self):
  465. """Check it works if session has timeout and it sets it back."""
  466. def cmd_check(mccs, session):
  467. session.set_timeout(1)
  468. mccs.check_command(False)
  469. fake_session = self.common_check_command_check(
  470. self.my_command_handler_ok, cmd_check)
  471. self.assertEqual(len(fake_session.message_queue), 1)
  472. self.assertEqual({'result': [0]},
  473. fake_session.get_message('Spec2', None))
  474. self.assertEqual(fake_session.get_timeout(), 1)
  475. def test_check_command_blocks_forever(self):
  476. """Check it would wait forever checking a command."""
  477. fake_session = FakeModuleCCSession()
  478. mccs = self.create_session("spec2.spec", None, None, fake_session)
  479. mccs.set_command_handler(self.my_command_handler_ok)
  480. self.assertRaises(WouldBlockForever, lambda: mccs.check_command(False))
  481. def test_check_command_blocks_forever_timeout(self):
  482. """Like above, but it should wait forever even with timeout here."""
  483. fake_session = FakeModuleCCSession()
  484. fake_session.set_timeout(1)
  485. mccs = self.create_session("spec2.spec", None, None, fake_session)
  486. mccs.set_command_handler(self.my_command_handler_ok)
  487. self.assertRaises(WouldBlockForever, lambda: mccs.check_command(False))
  488. # Now there's a group of tests testing both add_remote_config and
  489. # add_remote_config_by_name. Since they are almost the same (they differ
  490. # just in the parameter and that the second one asks one more question over
  491. # the bus), the actual test code is shared.
  492. #
  493. # These three functions are helper functions to easy up the writing of them.
  494. # To write a test, there need to be 3 functions. First, the function that
  495. # does the actual test. It looks like:
  496. # def _internal_test(self, function_lambda, param, fill_other_messages):
  497. #
  498. # The function_lambda provides the tested function if called on the
  499. # ccsession. The param is the parameter to pass to the function (either
  500. # the module name or the spec file name. The fill_other_messages fills
  501. # needed messages (the answer containing the module spec in case of add by
  502. # name, no messages in the case of adding by spec file) into the fake bus.
  503. # So, the code would look like:
  504. #
  505. # * Create the fake session and tested ccsession object
  506. # * function = function_lambda(ccsession object)
  507. # * fill_other_messages(fake session)
  508. # * Fill in answer to the get_module_config command
  509. # * Test by calling function(param)
  510. #
  511. # Then you need two wrappers that do launch the tests. There are helpers
  512. # for that, so you can just call:
  513. # def test_by_spec(self)
  514. # self._common_remote_module_test(self._internal_test)
  515. # def test_by_name(self)
  516. # self._common_remote_module_by_name_test(self._internal_test)
  517. def _common_remote_module_test(self, internal_test):
  518. internal_test(lambda ccs: ccs.add_remote_config,
  519. self.spec_file("spec2.spec"),
  520. lambda session: None)
  521. def _prepare_spec_message(self, session, spec_name):
  522. # It could have been one command, but the line would be way too long
  523. # to even split it
  524. spec_file = self.spec_file(spec_name)
  525. spec = isc.config.module_spec_from_file(spec_file)
  526. session.group_sendmsg({'result': [0, spec.get_full_spec()]}, "Spec1")
  527. def _common_remote_module_by_name_test(self, internal_test):
  528. internal_test(lambda ccs: ccs.add_remote_config_by_name, "Spec2",
  529. lambda session: self._prepare_spec_message(session,
  530. "spec2.spec"))
  531. def _internal_remote_module(self, function_lambda, parameter,
  532. fill_other_messages):
  533. fake_session = FakeModuleCCSession()
  534. mccs = self.create_session("spec1.spec", None, None, fake_session)
  535. mccs.remove_remote_config("Spec2")
  536. function = function_lambda(mccs)
  537. self.assertRaises(ModuleCCSessionError, mccs.get_remote_config_value, "Spec2", "item1")
  538. self.assertFalse("Spec2" in fake_session.subscriptions)
  539. fill_other_messages(fake_session)
  540. fake_session.group_sendmsg(None, 'Spec2')
  541. rmodname = function(parameter)
  542. self.assertTrue("Spec2" in fake_session.subscriptions)
  543. self.assertEqual("Spec2", rmodname)
  544. self.assertRaises(isc.cc.data.DataNotFoundError, mccs.get_remote_config_value, rmodname, "asdf")
  545. value, default = mccs.get_remote_config_value(rmodname, "item1")
  546. self.assertEqual(1, value)
  547. self.assertEqual(True, default)
  548. mccs.remove_remote_config(rmodname)
  549. self.assertFalse("Spec2" in fake_session.subscriptions)
  550. self.assertRaises(ModuleCCSessionError, mccs.get_remote_config_value, "Spec2", "item1")
  551. # test if unsubscription is also sent when object is deleted
  552. fill_other_messages(fake_session)
  553. fake_session.group_sendmsg({'result' : [0]}, 'Spec2')
  554. rmodname = function(parameter)
  555. self.assertTrue("Spec2" in fake_session.subscriptions)
  556. mccs = None
  557. function = None
  558. self.assertFalse("Spec2" in fake_session.subscriptions)
  559. def test_remote_module(self):
  560. """
  561. Test we can add a remote config and get the configuration.
  562. Remote module specified by the spec file name.
  563. """
  564. self._common_remote_module_test(self._internal_remote_module)
  565. def test_remote_module_by_name(self):
  566. """
  567. Test we can add a remote config and get the configuration.
  568. Remote module specified its name.
  569. """
  570. self._common_remote_module_by_name_test(self._internal_remote_module)
  571. def _internal_remote_module_with_custom_config(self, function_lambda,
  572. parameter,
  573. fill_other_messages):
  574. fake_session = FakeModuleCCSession()
  575. mccs = self.create_session("spec1.spec", None, None, fake_session)
  576. function = function_lambda(mccs)
  577. # override the default config value for "item1". add_remote_config[_by_name]()
  578. # should incorporate the overridden value, and we should be able to
  579. # get it via get_remote_config_value().
  580. fill_other_messages(fake_session)
  581. fake_session.group_sendmsg({'result': [0, {"item1": 10}]}, 'Spec2')
  582. rmodname = function(parameter)
  583. value, default = mccs.get_remote_config_value(rmodname, "item1")
  584. self.assertEqual(10, value)
  585. self.assertEqual(False, default)
  586. def test_remote_module_with_custom_config(self):
  587. """
  588. Test the config of module will load non-default values on
  589. initialization.
  590. Remote module specified by the spec file name.
  591. """
  592. self._common_remote_module_test(
  593. self._internal_remote_module_with_custom_config)
  594. def test_remote_module_by_name_with_custom_config(self):
  595. """
  596. Test the config of module will load non-default values on
  597. initialization.
  598. Remote module its name.
  599. """
  600. self._common_remote_module_by_name_test(
  601. self._internal_remote_module_with_custom_config)
  602. def _internal_ignore_command_remote_module(self, function_lambda, param,
  603. fill_other_messages):
  604. # Create a Spec1 module and subscribe to remote config for Spec2
  605. fake_session = FakeModuleCCSession()
  606. mccs = self.create_session("spec1.spec", None, None, fake_session)
  607. mccs.set_command_handler(self.my_command_handler_ok)
  608. function = function_lambda(mccs)
  609. fill_other_messages(fake_session)
  610. fake_session.group_sendmsg(None, 'Spec2')
  611. rmodname = function(param)
  612. # remove the commands from queue
  613. while len(fake_session.message_queue) > 0:
  614. fake_session.get_message("ConfigManager")
  615. # check if the command for the module itself is received
  616. cmd = isc.config.ccsession.create_command("just_some_command", { 'foo': 'a' })
  617. fake_session.group_sendmsg(cmd, 'Spec1')
  618. self.assertEqual(len(fake_session.message_queue), 1)
  619. mccs.check_command()
  620. self.assertEqual(len(fake_session.message_queue), 1)
  621. self.assertEqual({'result': [ 0 ]},
  622. fake_session.get_message('Spec1', None))
  623. # check if the command for the other module is ignored
  624. cmd = isc.config.ccsession.create_command("just_some_command", { 'foo': 'a' })
  625. fake_session.group_sendmsg(cmd, 'Spec2')
  626. self.assertEqual(len(fake_session.message_queue), 1)
  627. mccs.check_command()
  628. self.assertEqual(len(fake_session.message_queue), 0)
  629. def test_ignore_commant_remote_module(self):
  630. """
  631. Test that commands for remote modules aren't handled.
  632. Remote module specified by the spec file name.
  633. """
  634. self._common_remote_module_test(
  635. self._internal_ignore_command_remote_module)
  636. def test_ignore_commant_remote_module_by_name(self):
  637. """
  638. Test that commands for remote modules aren't handled.
  639. Remote module specified by its name.
  640. """
  641. self._common_remote_module_by_name_test(
  642. self._internal_ignore_command_remote_module)
  643. def _internal_check_command_without_recvmsg_remote_module(self,
  644. function_lambda,
  645. param,
  646. fill_other_messages):
  647. fake_session = FakeModuleCCSession()
  648. mccs = self.create_session("spec1.spec", None, None, fake_session)
  649. mccs.set_config_handler(self.my_config_handler_ok)
  650. function = function_lambda(mccs)
  651. self.assertEqual(len(fake_session.message_queue), 0)
  652. fill_other_messages(fake_session)
  653. fake_session.group_sendmsg(None, 'Spec2')
  654. rmodname = function(param)
  655. if (len(fake_session.message_queue) == 2):
  656. self.assertEqual({'command': ['get_module_spec',
  657. {'module_name': 'Spec2'}]},
  658. fake_session.get_message('ConfigManager', None))
  659. self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
  660. fake_session.get_message('ConfigManager', None))
  661. self.assertEqual(len(fake_session.message_queue), 0)
  662. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec2': { 'item1': 2 }})
  663. env = { 'group':'Spec2', 'from':None }
  664. self.assertEqual(len(fake_session.message_queue), 0)
  665. mccs.check_command_without_recvmsg(cmd, env)
  666. self.assertEqual(len(fake_session.message_queue), 0)
  667. def test_check_command_without_recvmsg_remote_module(self):
  668. """
  669. Test updates on remote module.
  670. The remote module is specified by the spec file name.
  671. """
  672. self._common_remote_module_test(
  673. self._internal_check_command_without_recvmsg_remote_module)
  674. def test_check_command_without_recvmsg_remote_module_by_name(self):
  675. """
  676. Test updates on remote module.
  677. The remote module is specified by its name.
  678. """
  679. self._common_remote_module_by_name_test(
  680. self._internal_check_command_without_recvmsg_remote_module)
  681. def _internal_check_command_without_recvmsg_remote_module2(self,
  682. function_lambda,
  683. param,
  684. fill_other_messages):
  685. fake_session = FakeModuleCCSession()
  686. mccs = self.create_session("spec1.spec", None, None, fake_session)
  687. mccs.set_config_handler(self.my_config_handler_ok)
  688. function = function_lambda(mccs)
  689. self.assertEqual(len(fake_session.message_queue), 0)
  690. fill_other_messages(fake_session)
  691. fake_session.group_sendmsg(None, 'Spec2')
  692. rmodname = function(param)
  693. if (len(fake_session.message_queue) == 2):
  694. self.assertEqual({'command': ['get_module_spec',
  695. {'module_name': 'Spec2'}]},
  696. fake_session.get_message('ConfigManager', None))
  697. self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
  698. fake_session.get_message('ConfigManager', None))
  699. self.assertEqual(len(fake_session.message_queue), 0)
  700. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec3': { 'item1': 2 }})
  701. env = { 'group':'Spec3', 'from':None }
  702. self.assertEqual(len(fake_session.message_queue), 0)
  703. mccs.check_command_without_recvmsg(cmd, env)
  704. self.assertEqual(len(fake_session.message_queue), 0)
  705. def test_check_command_without_recvmsg_remote_module2(self):
  706. """
  707. Test updates on remote module.
  708. The remote module is specified by the spec file name.
  709. """
  710. self._common_remote_module_test(
  711. self._internal_check_command_without_recvmsg_remote_module2)
  712. def test_check_command_without_recvmsg_remote_module_by_name2(self):
  713. """
  714. Test updates on remote module.
  715. The remote module is specified by its name.
  716. """
  717. self._common_remote_module_by_name_test(
  718. self._internal_check_command_without_recvmsg_remote_module2)
  719. def _internal_remote_module_bad_config(self, function_lambda, parameter,
  720. fill_other_messages):
  721. fake_session = FakeModuleCCSession()
  722. mccs = self.create_session("spec1.spec", None, None, fake_session)
  723. function = function_lambda(mccs)
  724. # Provide wrong config data. It should be rejected.
  725. fill_other_messages(fake_session)
  726. fake_session.group_sendmsg({'result': [0, {"bad_item": -1}]}, 'Spec2')
  727. self.assertRaises(isc.config.ModuleCCSessionError,
  728. function, parameter)
  729. def test_remote_module_bad_config(self):
  730. """
  731. Test the remote module rejects bad config data.
  732. """
  733. self._common_remote_module_test(
  734. self._internal_remote_module_bad_config)
  735. def test_remote_module_by_name_bad_config(self):
  736. """
  737. Test the remote module rejects bad config data.
  738. """
  739. self._common_remote_module_by_name_test(
  740. self._internal_remote_module_bad_config)
  741. def _internal_remote_module_error_response(self, function_lambda,
  742. parameter, fill_other_messages):
  743. fake_session = FakeModuleCCSession()
  744. mccs = self.create_session("spec1.spec", None, None, fake_session)
  745. function = function_lambda(mccs)
  746. # Provide wrong config data. It should be rejected.
  747. fill_other_messages(fake_session)
  748. fake_session.group_sendmsg({'result': [1, "An error, and I mean it!"]},
  749. 'Spec2')
  750. self.assertRaises(isc.config.ModuleCCSessionError,
  751. function, parameter)
  752. def test_remote_module_bad_config(self):
  753. """
  754. Test the remote module complains if there's an error response."
  755. """
  756. self._common_remote_module_test(
  757. self._internal_remote_module_error_response)
  758. def test_remote_module_by_name_bad_config(self):
  759. """
  760. Test the remote module complains if there's an error response."
  761. """
  762. self._common_remote_module_by_name_test(
  763. self._internal_remote_module_error_response)
  764. def test_remote_module_bad_config(self):
  765. """
  766. Test the remote module rejects bad config data.
  767. """
  768. self._common_remote_module_by_name_test(
  769. self._internal_remote_module_bad_config)
  770. def test_module_name_mismatch(self):
  771. fake_session = FakeModuleCCSession()
  772. mccs = self.create_session("spec1.spec", None, None, fake_session)
  773. mccs.set_config_handler(self.my_config_handler_ok)
  774. self._prepare_spec_message(fake_session, 'spec1.spec')
  775. self.assertRaises(isc.config.ModuleCCSessionError,
  776. mccs.add_remote_config_by_name, "Spec2")
  777. def test_logconfig_handler(self):
  778. # test whether default_logconfig_handler reacts nicely to
  779. # bad data. We assume the actual logger output is tested
  780. # elsewhere
  781. self.assertRaises(TypeError, default_logconfig_handler);
  782. self.assertRaises(TypeError, default_logconfig_handler, 1);
  783. spec = isc.config.module_spec_from_file(
  784. path_search('logging.spec', bind10_config.PLUGIN_PATHS))
  785. config_data = ConfigData(spec)
  786. self.assertRaises(TypeError, default_logconfig_handler, 1, config_data)
  787. default_logconfig_handler({}, config_data)
  788. # Wrong data should not raise, but simply not be accepted
  789. # This would log a lot of errors, so we may want to suppress that later
  790. default_logconfig_handler({ "bad_data": "indeed" }, config_data)
  791. default_logconfig_handler({ "bad_data": 1}, config_data)
  792. default_logconfig_handler({ "bad_data": 1123 }, config_data)
  793. default_logconfig_handler({ "bad_data": True }, config_data)
  794. default_logconfig_handler({ "bad_data": False }, config_data)
  795. default_logconfig_handler({ "bad_data": 1.1 }, config_data)
  796. default_logconfig_handler({ "bad_data": [] }, config_data)
  797. default_logconfig_handler({ "bad_data": [[],[],[[1, 3, False, "foo" ]]] },
  798. config_data)
  799. default_logconfig_handler({ "bad_data": [ 1, 2, { "b": { "c": "d" } } ] },
  800. config_data)
  801. # Try a correct config
  802. log_conf = {"loggers":
  803. [{"name": "b10-xfrout", "output_options":
  804. [{"output": "/tmp/bind10.log",
  805. "destination": "file",
  806. "flush": True}]}]}
  807. default_logconfig_handler(log_conf, config_data)
  808. class fakeData:
  809. def decode(self):
  810. return "{}";
  811. class fakeAnswer:
  812. def read(self):
  813. return fakeData();
  814. class fakeUIConn():
  815. def __init__(self):
  816. self.get_answers = {}
  817. self.post_answers = {}
  818. def set_get_answer(self, name, answer):
  819. self.get_answers[name] = answer
  820. def set_post_answer(self, name, answer):
  821. self.post_answers[name] = answer
  822. def send_GET(self, name, arg = None):
  823. if name in self.get_answers:
  824. return self.get_answers[name]
  825. else:
  826. return {}
  827. def send_POST(self, name, arg = None):
  828. if name in self.post_answers:
  829. return self.post_answers[name]
  830. else:
  831. return fakeAnswer()
  832. class TestUIModuleCCSession(unittest.TestCase):
  833. def setUp(self):
  834. if 'CONFIG_TESTDATA_PATH' in os.environ:
  835. self.data_path = os.environ['CONFIG_TESTDATA_PATH']
  836. else:
  837. self.data_path = "../../../testdata"
  838. def spec_file(self, file):
  839. return self.data_path + os.sep + file
  840. def create_uccs(self, fake_conn, specfile="spec2.spec"):
  841. module_spec = isc.config.module_spec_from_file(self.spec_file(specfile))
  842. fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
  843. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  844. return UIModuleCCSession(fake_conn)
  845. def create_uccs_named_set(self, fake_conn):
  846. module_spec = isc.config.module_spec_from_file(self.spec_file("spec32.spec"))
  847. fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
  848. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  849. return UIModuleCCSession(fake_conn)
  850. def create_uccs_listtest(self, fake_conn):
  851. module_spec = isc.config.module_spec_from_file(self.spec_file("spec39.spec"))
  852. fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
  853. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  854. return UIModuleCCSession(fake_conn)
  855. def test_init(self):
  856. fake_conn = fakeUIConn()
  857. fake_conn.set_get_answer('/module_spec', {})
  858. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  859. uccs = UIModuleCCSession(fake_conn)
  860. self.assertEqual({}, uccs._specifications)
  861. self.assertEqual({ 'version': BIND10_CONFIG_DATA_VERSION}, uccs._current_config)
  862. module_spec = isc.config.module_spec_from_file(self.spec_file("spec2.spec"))
  863. fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
  864. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  865. uccs = UIModuleCCSession(fake_conn)
  866. self.assertEqual(module_spec._module_spec, uccs._specifications['Spec2']._module_spec)
  867. fake_conn.set_get_answer('/config_data', { 'version': 123123 })
  868. self.assertRaises(ModuleCCSessionError, UIModuleCCSession, fake_conn)
  869. def test_request_specifications(self):
  870. module_spec1 = isc.config.module_spec_from_file(
  871. self.spec_file("spec1.spec"))
  872. module_spec_dict1 = { "module_spec": module_spec1.get_full_spec() }
  873. module_spec2 = isc.config.module_spec_from_file(
  874. self.spec_file("spec2.spec"))
  875. module_spec_dict2 = { "module_spec": module_spec2.get_full_spec() }
  876. fake_conn = fakeUIConn()
  877. # Set the first one in the answer
  878. fake_conn.set_get_answer('/module_spec', module_spec_dict1)
  879. fake_conn.set_get_answer('/config_data',
  880. { 'version': BIND10_CONFIG_DATA_VERSION })
  881. uccs = UIModuleCCSession(fake_conn)
  882. # We should now have the first one, but not the second.
  883. self.assertTrue("Spec1" in uccs._specifications)
  884. self.assertEqual(module_spec1.get_full_spec(),
  885. uccs._specifications["Spec1"].get_full_spec())
  886. self.assertFalse("Spec2" in uccs._specifications)
  887. # Now set an answer where only the second one is present
  888. fake_conn.set_get_answer('/module_spec', module_spec_dict2)
  889. uccs.request_specifications()
  890. # Now Spec1 should have been removed, and spec2 should be there
  891. self.assertFalse("Spec1" in uccs._specifications)
  892. self.assertTrue("Spec2" in uccs._specifications)
  893. self.assertEqual(module_spec2.get_full_spec(),
  894. uccs._specifications["Spec2"].get_full_spec())
  895. def test_add_remove_value(self):
  896. fake_conn = fakeUIConn()
  897. uccs = self.create_uccs(fake_conn)
  898. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.add_value, 1, "a")
  899. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.add_value, "no_such_item", "a")
  900. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.remove_value, 1, "a")
  901. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.remove_value, "no_such_item", "a")
  902. # add and remove should raise DataNotFoundError when used with items
  903. # that are not a list or named_set (more importantly, they should
  904. # not raise TypeError)
  905. self.assertRaises(isc.cc.data.DataTypeError, uccs.add_value, "Spec2/item1", "a")
  906. self.assertRaises(isc.cc.data.DataTypeError, uccs.remove_value, "Spec2/item1", "a")
  907. self.assertRaises(isc.cc.data.DataTypeError, uccs.add_value, "Spec2", "")
  908. self.assertRaises(isc.cc.data.DataTypeError, uccs.remove_value, "Spec2", "")
  909. self.assertEqual({}, uccs._local_changes)
  910. uccs.add_value("Spec2/item5", "foo")
  911. self.assertEqual({'Spec2': {'item5': ['a', 'b', 'foo']}}, uccs._local_changes)
  912. uccs.remove_value("Spec2/item5", "foo")
  913. self.assertEqual({'Spec2': {'item5': ['a', 'b']}}, uccs._local_changes)
  914. uccs._local_changes = {'Spec2': {'item5': []}}
  915. uccs.remove_value("Spec2/item5", "foo")
  916. uccs.add_value("Spec2/item5", "foo")
  917. self.assertEqual({'Spec2': {'item5': ['foo']}}, uccs._local_changes)
  918. self.assertRaises(isc.cc.data.DataAlreadyPresentError,
  919. uccs.add_value, "Spec2/item5", "foo")
  920. self.assertEqual({'Spec2': {'item5': ['foo']}}, uccs._local_changes)
  921. self.assertRaises(isc.cc.data.DataNotFoundError,
  922. uccs.remove_value, "Spec2/item5[123]", None)
  923. uccs.remove_value("Spec2/item5[0]", None)
  924. self.assertEqual({'Spec2': {'item5': []}}, uccs._local_changes)
  925. uccs.add_value("Spec2/item5", None);
  926. self.assertEqual({'Spec2': {'item5': ['']}}, uccs._local_changes)
  927. # Intending to empty a list element, but forget specifying the index.
  928. self.assertRaises(isc.cc.data.DataTypeError,
  929. uccs.remove_value, "Spec2/item5", None)
  930. # Check that the difference between no default and default = null
  931. # is recognized
  932. def test_default_null(self):
  933. fake_conn = fakeUIConn()
  934. uccs = self.create_uccs(fake_conn, "spec40.spec")
  935. (value, status) = uccs.get_value("/Spec40/item2")
  936. self.assertIsNone(value)
  937. self.assertEqual(uccs.NONE, status)
  938. (value, status) = uccs.get_value("/Spec40/item3")
  939. self.assertIsNone(value)
  940. self.assertEqual(uccs.DEFAULT, status)
  941. # Test adding and removing values for type = any
  942. def test_add_remove_value_any(self):
  943. fake_conn = fakeUIConn()
  944. uccs = self.create_uccs(fake_conn, "spec40.spec")
  945. # Test item set of basic types
  946. items = [ 1234, "foo", True, False ]
  947. items_as_str = [ '1234', 'foo', 'true', 'false' ]
  948. def test_fails():
  949. self.assertRaises(isc.cc.data.DataTypeError, uccs.add_value, "Spec40/item1", "foo")
  950. self.assertRaises(isc.cc.data.DataTypeError, uccs.add_value, "Spec40/item1", "foo", "bar")
  951. self.assertRaises(isc.cc.data.DataTypeError, uccs.remove_value, "Spec40/item1", "foo")
  952. self.assertRaises(isc.cc.data.DataTypeError, uccs.remove_value, "Spec40/item1[0]", None)
  953. # A few helper functions to perform a number of tests
  954. # (to repeat the same test for nested data)
  955. def check_list(identifier):
  956. for item in items_as_str:
  957. uccs.add_value(identifier, item)
  958. self.assertEqual((items, 1), uccs.get_value(identifier))
  959. # Removing from list should work in both ways
  960. uccs.remove_value(identifier, "foo")
  961. uccs.remove_value(identifier + "[1]", None)
  962. self.assertEqual(([1234, False], 1), uccs.get_value(identifier))
  963. # As should item indexing
  964. self.assertEqual((1234, 1), uccs.get_value(identifier + "[0]"))
  965. self.assertEqual((False, 1), uccs.get_value(identifier + "[1]"))
  966. def check_named_set(identifier):
  967. for item in items_as_str:
  968. # use string version as key as well
  969. uccs.add_value(identifier, item, item)
  970. self.assertEqual((1234, 1), uccs.get_value(identifier + "/1234"))
  971. self.assertEqual((True, 1), uccs.get_value(identifier + "/true"))
  972. for item in items_as_str:
  973. # use string version as key as well
  974. uccs.remove_value(identifier, item)
  975. # should fail when set to value of primitive type
  976. for item in items:
  977. uccs.set_value("Spec40/item1", item)
  978. test_fails()
  979. # When set to list, add and remove should work, and its elements
  980. # should be considered of type 'any' themselves.
  981. uccs.set_value("Spec40/item1", [])
  982. check_list("Spec40/item1")
  983. # When set to dict, it should have the behaviour of a named set
  984. uccs.set_value("Spec40/item1", {})
  985. check_named_set("Spec40/item1")
  986. # And, or course, we may need nesting.
  987. uccs.set_value("Spec40/item1", { "foo": {}, "bar": [] })
  988. check_named_set("Spec40/item1/foo")
  989. check_list("Spec40/item1/bar")
  990. uccs.set_value("Spec40/item1", [ {}, [] ] )
  991. check_named_set("Spec40/item1[0]")
  992. check_list("Spec40/item1[1]")
  993. uccs.set_value("Spec40/item1", [[[[[[]]]]]] )
  994. check_list("Spec40/item1[0][0][0][0][0]")
  995. uccs.set_value("Spec40/item1", { 'a': { 'a': { 'a': {} } } } )
  996. check_named_set("Spec40/item1/a/a/a")
  997. def test_add_dup_value(self):
  998. fake_conn = fakeUIConn()
  999. uccs = self.create_uccs_listtest(fake_conn)
  1000. uccs.add_value("Spec39/list")
  1001. self.assertRaises(isc.cc.data.DataAlreadyPresentError, uccs.add_value,
  1002. "Spec39/list")
  1003. def test_add_remove_value_named_set(self):
  1004. fake_conn = fakeUIConn()
  1005. uccs = self.create_uccs_named_set(fake_conn)
  1006. value, status = uccs.get_value("/Spec32/named_set_item")
  1007. self.assertEqual({'a': 1, 'b': 2}, value)
  1008. # make sure that removing from default actually removes it
  1009. uccs.remove_value("/Spec32/named_set_item", "a")
  1010. value, status = uccs.get_value("/Spec32/named_set_item")
  1011. self.assertEqual({'b': 2}, value)
  1012. self.assertEqual(uccs.LOCAL, status)
  1013. # ok, put it back now
  1014. uccs.add_value("/Spec32/named_set_item", "a")
  1015. uccs.set_value("/Spec32/named_set_item/a", 1)
  1016. uccs.add_value("/Spec32/named_set_item", "foo")
  1017. value, status = uccs.get_value("/Spec32/named_set_item")
  1018. self.assertEqual({'a': 1, 'b': 2, 'foo': 3}, value)
  1019. uccs.remove_value("/Spec32/named_set_item", "a")
  1020. uccs.remove_value("/Spec32/named_set_item", "foo")
  1021. value, status = uccs.get_value("/Spec32/named_set_item")
  1022. self.assertEqual({'b': 2}, value)
  1023. uccs.set_value("/Spec32/named_set_item/c", 5)
  1024. value, status = uccs.get_value("/Spec32/named_set_item")
  1025. self.assertEqual({"b": 2, "c": 5}, value)
  1026. self.assertRaises(isc.cc.data.DataNotFoundError,
  1027. uccs.set_value,
  1028. "/Spec32/named_set_item/no_such_item/a",
  1029. 4)
  1030. self.assertRaises(isc.cc.data.DataNotFoundError,
  1031. uccs.remove_value, "/Spec32/named_set_item",
  1032. "no_such_item")
  1033. self.assertRaises(isc.cc.data.DataAlreadyPresentError,
  1034. uccs.add_value, "/Spec32/named_set_item", "c")
  1035. def test_set_value_named_set(self):
  1036. fake_conn = fakeUIConn()
  1037. uccs = self.create_uccs_named_set(fake_conn)
  1038. value, status = uccs.get_value("/Spec32/named_set_item2")
  1039. self.assertEqual({}, value)
  1040. self.assertEqual(status, uccs.DEFAULT)
  1041. # Try setting a value that is optional but has no default
  1042. uccs.add_value("/Spec32/named_set_item2", "new1")
  1043. uccs.set_value("/Spec32/named_set_item2/new1/first", 3)
  1044. # Different method to add a new element
  1045. uccs.set_value("/Spec32/named_set_item2/new2", { "second": 4 })
  1046. value, status = uccs.get_value("/Spec32/named_set_item2")
  1047. self.assertEqual({ "new1": {"first": 3 }, "new2": {"second": 4}},
  1048. value)
  1049. self.assertEqual(status, uccs.LOCAL)
  1050. uccs.set_value("/Spec32/named_set_item2/new1/second", "foo")
  1051. value, status = uccs.get_value("/Spec32/named_set_item2")
  1052. self.assertEqual({ "new1": {"first": 3, "second": "foo" },
  1053. "new2": {"second": 4}},
  1054. value)
  1055. self.assertEqual(status, uccs.LOCAL)
  1056. # make sure using a bad name still fails
  1057. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.set_value,
  1058. "/Spec32/named_set_item2/doesnotexist/first", 3)
  1059. def test_commit(self):
  1060. fake_conn = fakeUIConn()
  1061. uccs = self.create_uccs(fake_conn)
  1062. uccs.commit()
  1063. uccs._local_changes = {'Spec2': {'item5': [ 'a' ]}}
  1064. uccs.commit()
  1065. if __name__ == '__main__':
  1066. isc.log.init("bind10")
  1067. unittest.main()