ccsession_test.py 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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 my_config_handler_ok(self, new_config):
  249. return isc.config.ccsession.create_answer(0)
  250. def my_config_handler_err(self, new_config):
  251. return isc.config.ccsession.create_answer(1, "just an error")
  252. def my_config_handler_exc(self, new_config):
  253. raise Exception("just an exception")
  254. def my_command_handler_ok(self, command, args):
  255. return isc.config.ccsession.create_answer(0)
  256. def my_command_handler_no_answer(self, command, args):
  257. pass
  258. def test_check_command1(self):
  259. fake_session = FakeModuleCCSession()
  260. mccs = self.create_session("spec1.spec", None, None, fake_session)
  261. mccs.check_command()
  262. self.assertEqual(len(fake_session.message_queue), 0)
  263. fake_session.group_sendmsg({'result': [ 0 ]}, "Spec1")
  264. mccs.check_command()
  265. self.assertEqual(len(fake_session.message_queue), 0)
  266. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec1': 'a' })
  267. fake_session.group_sendmsg(cmd, 'Spec1')
  268. mccs.check_command()
  269. self.assertEqual(len(fake_session.message_queue), 1)
  270. self.assertEqual({'result': [2, 'Spec1 has no config handler']},
  271. fake_session.get_message('Spec1', None))
  272. def test_check_command2(self):
  273. fake_session = FakeModuleCCSession()
  274. mccs = self.create_session("spec1.spec", None, None, fake_session)
  275. mccs.set_config_handler(self.my_config_handler_ok)
  276. self.assertEqual(len(fake_session.message_queue), 0)
  277. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec1': 'a' })
  278. fake_session.group_sendmsg(cmd, 'Spec1')
  279. self.assertEqual(len(fake_session.message_queue), 1)
  280. mccs.check_command()
  281. self.assertEqual(len(fake_session.message_queue), 1)
  282. self.assertEqual({'result': [1, 'No config_data specification']},
  283. fake_session.get_message('Spec1', None))
  284. def test_check_command3(self):
  285. fake_session = FakeModuleCCSession()
  286. mccs = self.create_session("spec2.spec", None, None, fake_session)
  287. mccs.set_config_handler(self.my_config_handler_ok)
  288. self.assertEqual(len(fake_session.message_queue), 0)
  289. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'item1': 2 })
  290. fake_session.group_sendmsg(cmd, 'Spec2')
  291. self.assertEqual(len(fake_session.message_queue), 1)
  292. mccs.check_command()
  293. self.assertEqual(len(fake_session.message_queue), 1)
  294. self.assertEqual({'result': [0]},
  295. fake_session.get_message('Spec2', None))
  296. def test_check_command4(self):
  297. fake_session = FakeModuleCCSession()
  298. mccs = self.create_session("spec2.spec", None, None, fake_session)
  299. mccs.set_config_handler(self.my_config_handler_err)
  300. self.assertEqual(len(fake_session.message_queue), 0)
  301. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'item1': 'aaa' })
  302. fake_session.group_sendmsg(cmd, 'Spec2')
  303. self.assertEqual(len(fake_session.message_queue), 1)
  304. mccs.check_command()
  305. self.assertEqual(len(fake_session.message_queue), 1)
  306. self.assertEqual({'result': [1, 'aaa should be an integer']},
  307. fake_session.get_message('Spec2', None))
  308. def test_check_command5(self):
  309. fake_session = FakeModuleCCSession()
  310. mccs = self.create_session("spec2.spec", None, None, fake_session)
  311. mccs.set_config_handler(self.my_config_handler_exc)
  312. self.assertEqual(len(fake_session.message_queue), 0)
  313. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'item1': 'aaa' })
  314. fake_session.group_sendmsg(cmd, 'Spec2')
  315. self.assertEqual(len(fake_session.message_queue), 1)
  316. mccs.check_command()
  317. self.assertEqual(len(fake_session.message_queue), 1)
  318. self.assertEqual({'result': [1, 'aaa should be an integer']},
  319. fake_session.get_message('Spec2', None))
  320. def test_check_command6(self):
  321. fake_session = FakeModuleCCSession()
  322. mccs = self.create_session("spec2.spec", None, None, fake_session)
  323. self.assertEqual(len(fake_session.message_queue), 0)
  324. cmd = isc.config.ccsession.create_command("print_message", "just a message")
  325. fake_session.group_sendmsg(cmd, 'Spec2')
  326. self.assertEqual(len(fake_session.message_queue), 1)
  327. mccs.check_command()
  328. self.assertEqual(len(fake_session.message_queue), 1)
  329. self.assertEqual({'result': [2, 'Spec2 has no command handler']},
  330. fake_session.get_message('Spec2', None))
  331. """Many check_command tests look too similar, this is common body."""
  332. def common_check_command_check(self, cmd_handler,
  333. cmd_check=lambda mccs, _: mccs.check_command()):
  334. fake_session = FakeModuleCCSession()
  335. mccs = self.create_session("spec2.spec", None, None, fake_session)
  336. mccs.set_command_handler(cmd_handler)
  337. self.assertEqual(len(fake_session.message_queue), 0)
  338. cmd = isc.config.ccsession.create_command("print_message", "just a message")
  339. fake_session.group_sendmsg(cmd, 'Spec2')
  340. self.assertEqual(len(fake_session.message_queue), 1)
  341. cmd_check(mccs, fake_session)
  342. return fake_session
  343. def test_check_command7(self):
  344. fake_session = self.common_check_command_check(
  345. self.my_command_handler_ok)
  346. self.assertEqual(len(fake_session.message_queue), 1)
  347. self.assertEqual({'result': [0]},
  348. fake_session.get_message('Spec2', None))
  349. def test_check_command8(self):
  350. fake_session = self.common_check_command_check(
  351. self.my_command_handler_no_answer)
  352. self.assertEqual(len(fake_session.message_queue), 0)
  353. def test_check_command_block(self):
  354. """See if the message gets there even in blocking mode."""
  355. fake_session = self.common_check_command_check(
  356. self.my_command_handler_ok,
  357. lambda mccs, _: mccs.check_command(False))
  358. self.assertEqual(len(fake_session.message_queue), 1)
  359. self.assertEqual({'result': [0]},
  360. fake_session.get_message('Spec2', None))
  361. def test_check_command_block_timeout(self):
  362. """Check it works if session has timeout and it sets it back."""
  363. def cmd_check(mccs, session):
  364. session.set_timeout(1)
  365. mccs.check_command(False)
  366. fake_session = self.common_check_command_check(
  367. self.my_command_handler_ok, cmd_check)
  368. self.assertEqual(len(fake_session.message_queue), 1)
  369. self.assertEqual({'result': [0]},
  370. fake_session.get_message('Spec2', None))
  371. self.assertEqual(fake_session.get_timeout(), 1)
  372. def test_check_command_blocks_forever(self):
  373. """Check it would wait forever checking a command."""
  374. fake_session = FakeModuleCCSession()
  375. mccs = self.create_session("spec2.spec", None, None, fake_session)
  376. mccs.set_command_handler(self.my_command_handler_ok)
  377. self.assertRaises(WouldBlockForever, lambda: mccs.check_command(False))
  378. def test_check_command_blocks_forever_timeout(self):
  379. """Like above, but it should wait forever even with timeout here."""
  380. fake_session = FakeModuleCCSession()
  381. fake_session.set_timeout(1)
  382. mccs = self.create_session("spec2.spec", None, None, fake_session)
  383. mccs.set_command_handler(self.my_command_handler_ok)
  384. self.assertRaises(WouldBlockForever, lambda: mccs.check_command(False))
  385. def test_check_command_without_recvmsg1(self):
  386. "copied from test_check_command2"
  387. fake_session = FakeModuleCCSession()
  388. mccs = self.create_session("spec1.spec", None, None, fake_session)
  389. mccs.set_config_handler(self.my_config_handler_ok)
  390. self.assertEqual(len(fake_session.message_queue), 0)
  391. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec1': 'abcd' })
  392. env = { 'group': 'Spec1', 'from':None };
  393. mccs.check_command_without_recvmsg(cmd, env)
  394. self.assertEqual(len(fake_session.message_queue), 1)
  395. self.assertEqual({'result': [1, 'No config_data specification']},
  396. fake_session.get_message('Spec1', None))
  397. def test_check_command_without_recvmsg2(self):
  398. "copied from test_check_command3"
  399. fake_session = FakeModuleCCSession()
  400. mccs = self.create_session("spec2.spec", None, None, fake_session)
  401. mccs.set_config_handler(self.my_config_handler_ok)
  402. self.assertEqual(len(fake_session.message_queue), 0)
  403. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'item1': 2 })
  404. self.assertEqual(len(fake_session.message_queue), 0)
  405. env = { 'group':'Spec2', 'from':None }
  406. mccs.check_command_without_recvmsg(cmd, env)
  407. self.assertEqual(len(fake_session.message_queue), 1)
  408. self.assertEqual({'result': [0]},
  409. fake_session.get_message('Spec2', None))
  410. def test_check_command_without_recvmsg3(self):
  411. "copied from test_check_command7"
  412. fake_session = FakeModuleCCSession()
  413. mccs = self.create_session("spec2.spec", None, None, fake_session)
  414. mccs.set_command_handler(self.my_command_handler_ok)
  415. self.assertEqual(len(fake_session.message_queue), 0)
  416. cmd = isc.config.ccsession.create_command("print_message", "just a message")
  417. env = { 'group':'Spec2', 'from':None }
  418. self.assertEqual(len(fake_session.message_queue), 0)
  419. mccs.check_command_without_recvmsg(cmd, env)
  420. self.assertEqual({'result': [0]},
  421. fake_session.get_message('Spec2', None))
  422. def test_check_command_block_timeout(self):
  423. """Check it works if session has timeout and it sets it back."""
  424. def cmd_check(mccs, session):
  425. session.set_timeout(1)
  426. mccs.check_command(False)
  427. fake_session = self.common_check_command_check(
  428. self.my_command_handler_ok, cmd_check)
  429. self.assertEqual(len(fake_session.message_queue), 1)
  430. self.assertEqual({'result': [0]},
  431. fake_session.get_message('Spec2', None))
  432. self.assertEqual(fake_session.get_timeout(), 1)
  433. def test_check_command_blocks_forever(self):
  434. """Check it would wait forever checking a command."""
  435. fake_session = FakeModuleCCSession()
  436. mccs = self.create_session("spec2.spec", None, None, fake_session)
  437. mccs.set_command_handler(self.my_command_handler_ok)
  438. self.assertRaises(WouldBlockForever, lambda: mccs.check_command(False))
  439. def test_check_command_blocks_forever_timeout(self):
  440. """Like above, but it should wait forever even with timeout here."""
  441. fake_session = FakeModuleCCSession()
  442. fake_session.set_timeout(1)
  443. mccs = self.create_session("spec2.spec", None, None, fake_session)
  444. mccs.set_command_handler(self.my_command_handler_ok)
  445. self.assertRaises(WouldBlockForever, lambda: mccs.check_command(False))
  446. # Now there's a group of tests testing both add_remote_config and
  447. # add_remote_config_by_name. Since they are almost the same (they differ
  448. # just in the parameter and that the second one asks one more question over
  449. # the bus), the actual test code is shared.
  450. #
  451. # These three functions are helper functions to easy up the writing of them.
  452. # To write a test, there need to be 3 functions. First, the function that
  453. # does the actual test. It looks like:
  454. # def _internal_test(self, function_lambda, param, fill_other_messages):
  455. #
  456. # The function_lambda provides the tested function if called on the
  457. # ccsession. The param is the parameter to pass to the function (either
  458. # the module name or the spec file name. The fill_other_messages fills
  459. # needed messages (the answer containing the module spec in case of add by
  460. # name, no messages in the case of adding by spec file) into the fake bus.
  461. # So, the code would look like:
  462. #
  463. # * Create the fake session and tested ccsession object
  464. # * function = function_lambda(ccsession object)
  465. # * fill_other_messages(fake session)
  466. # * Fill in answer to the get_module_config command
  467. # * Test by calling function(param)
  468. #
  469. # Then you need two wrappers that do launch the tests. There are helpers
  470. # for that, so you can just call:
  471. # def test_by_spec(self)
  472. # self._common_remote_module_test(self._internal_test)
  473. # def test_by_name(self)
  474. # self._common_remote_module_by_name_test(self._internal_test)
  475. def _common_remote_module_test(self, internal_test):
  476. internal_test(lambda ccs: ccs.add_remote_config,
  477. self.spec_file("spec2.spec"),
  478. lambda session: None)
  479. def _prepare_spec_message(self, session, spec_name):
  480. # It could have been one command, but the line would be way too long
  481. # to even split it
  482. spec_file = self.spec_file(spec_name)
  483. spec = isc.config.module_spec_from_file(spec_file)
  484. session.group_sendmsg({'result': [0, spec.get_full_spec()]}, "Spec1")
  485. def _common_remote_module_by_name_test(self, internal_test):
  486. internal_test(lambda ccs: ccs.add_remote_config_by_name, "Spec2",
  487. lambda session: self._prepare_spec_message(session,
  488. "spec2.spec"))
  489. def _internal_remote_module(self, function_lambda, parameter,
  490. fill_other_messages):
  491. fake_session = FakeModuleCCSession()
  492. mccs = self.create_session("spec1.spec", None, None, fake_session)
  493. mccs.remove_remote_config("Spec2")
  494. function = function_lambda(mccs)
  495. self.assertRaises(ModuleCCSessionError, mccs.get_remote_config_value, "Spec2", "item1")
  496. self.assertFalse("Spec2" in fake_session.subscriptions)
  497. fill_other_messages(fake_session)
  498. fake_session.group_sendmsg(None, 'Spec2')
  499. rmodname = function(parameter)
  500. self.assertTrue("Spec2" in fake_session.subscriptions)
  501. self.assertEqual("Spec2", rmodname)
  502. self.assertRaises(isc.cc.data.DataNotFoundError, mccs.get_remote_config_value, rmodname, "asdf")
  503. value, default = mccs.get_remote_config_value(rmodname, "item1")
  504. self.assertEqual(1, value)
  505. self.assertEqual(True, default)
  506. mccs.remove_remote_config(rmodname)
  507. self.assertFalse("Spec2" in fake_session.subscriptions)
  508. self.assertRaises(ModuleCCSessionError, mccs.get_remote_config_value, "Spec2", "item1")
  509. # test if unsubscription is also sent when object is deleted
  510. fill_other_messages(fake_session)
  511. fake_session.group_sendmsg({'result' : [0]}, 'Spec2')
  512. rmodname = function(parameter)
  513. self.assertTrue("Spec2" in fake_session.subscriptions)
  514. mccs = None
  515. function = None
  516. self.assertFalse("Spec2" in fake_session.subscriptions)
  517. def test_remote_module(self):
  518. """
  519. Test we can add a remote config and get the configuration.
  520. Remote module specified by the spec file name.
  521. """
  522. self._common_remote_module_test(self._internal_remote_module)
  523. def test_remote_module_by_name(self):
  524. """
  525. Test we can add a remote config and get the configuration.
  526. Remote module specified its name.
  527. """
  528. self._common_remote_module_by_name_test(self._internal_remote_module)
  529. def _internal_remote_module_with_custom_config(self, function_lambda,
  530. parameter,
  531. fill_other_messages):
  532. fake_session = FakeModuleCCSession()
  533. mccs = self.create_session("spec1.spec", None, None, fake_session)
  534. function = function_lambda(mccs)
  535. # override the default config value for "item1". add_remote_config[_by_name]()
  536. # should incorporate the overridden value, and we should be able to
  537. # get it via get_remote_config_value().
  538. fill_other_messages(fake_session)
  539. fake_session.group_sendmsg({'result': [0, {"item1": 10}]}, 'Spec2')
  540. rmodname = function(parameter)
  541. value, default = mccs.get_remote_config_value(rmodname, "item1")
  542. self.assertEqual(10, value)
  543. self.assertEqual(False, default)
  544. def test_remote_module_with_custom_config(self):
  545. """
  546. Test the config of module will load non-default values on
  547. initialization.
  548. Remote module specified by the spec file name.
  549. """
  550. self._common_remote_module_test(
  551. self._internal_remote_module_with_custom_config)
  552. def test_remote_module_by_name_with_custom_config(self):
  553. """
  554. Test the config of module will load non-default values on
  555. initialization.
  556. Remote module its name.
  557. """
  558. self._common_remote_module_by_name_test(
  559. self._internal_remote_module_with_custom_config)
  560. def _internal_ignore_command_remote_module(self, function_lambda, param,
  561. fill_other_messages):
  562. # Create a Spec1 module and subscribe to remote config for Spec2
  563. fake_session = FakeModuleCCSession()
  564. mccs = self.create_session("spec1.spec", None, None, fake_session)
  565. mccs.set_command_handler(self.my_command_handler_ok)
  566. function = function_lambda(mccs)
  567. fill_other_messages(fake_session)
  568. fake_session.group_sendmsg(None, 'Spec2')
  569. rmodname = function(param)
  570. # remove the commands from queue
  571. while len(fake_session.message_queue) > 0:
  572. fake_session.get_message("ConfigManager")
  573. # check if the command for the module itself is received
  574. cmd = isc.config.ccsession.create_command("just_some_command", { 'foo': 'a' })
  575. fake_session.group_sendmsg(cmd, 'Spec1')
  576. self.assertEqual(len(fake_session.message_queue), 1)
  577. mccs.check_command()
  578. self.assertEqual(len(fake_session.message_queue), 1)
  579. self.assertEqual({'result': [ 0 ]},
  580. fake_session.get_message('Spec1', None))
  581. # check if the command for the other module is ignored
  582. cmd = isc.config.ccsession.create_command("just_some_command", { 'foo': 'a' })
  583. fake_session.group_sendmsg(cmd, 'Spec2')
  584. self.assertEqual(len(fake_session.message_queue), 1)
  585. mccs.check_command()
  586. self.assertEqual(len(fake_session.message_queue), 0)
  587. def test_ignore_commant_remote_module(self):
  588. """
  589. Test that commands for remote modules aren't handled.
  590. Remote module specified by the spec file name.
  591. """
  592. self._common_remote_module_test(
  593. self._internal_ignore_command_remote_module)
  594. def test_ignore_commant_remote_module_by_name(self):
  595. """
  596. Test that commands for remote modules aren't handled.
  597. Remote module specified by its name.
  598. """
  599. self._common_remote_module_by_name_test(
  600. self._internal_ignore_command_remote_module)
  601. def _internal_check_command_without_recvmsg_remote_module(self,
  602. function_lambda,
  603. param,
  604. fill_other_messages):
  605. fake_session = FakeModuleCCSession()
  606. mccs = self.create_session("spec1.spec", None, None, fake_session)
  607. mccs.set_config_handler(self.my_config_handler_ok)
  608. function = function_lambda(mccs)
  609. self.assertEqual(len(fake_session.message_queue), 0)
  610. fill_other_messages(fake_session)
  611. fake_session.group_sendmsg(None, 'Spec2')
  612. rmodname = function(param)
  613. if (len(fake_session.message_queue) == 2):
  614. self.assertEqual({'command': ['get_module_spec',
  615. {'module_name': 'Spec2'}]},
  616. fake_session.get_message('ConfigManager', None))
  617. self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
  618. fake_session.get_message('ConfigManager', None))
  619. self.assertEqual(len(fake_session.message_queue), 0)
  620. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec2': { 'item1': 2 }})
  621. env = { 'group':'Spec2', 'from':None }
  622. self.assertEqual(len(fake_session.message_queue), 0)
  623. mccs.check_command_without_recvmsg(cmd, env)
  624. self.assertEqual(len(fake_session.message_queue), 0)
  625. def test_check_command_without_recvmsg_remote_module(self):
  626. """
  627. Test updates on remote module.
  628. The remote module is specified by the spec file name.
  629. """
  630. self._common_remote_module_test(
  631. self._internal_check_command_without_recvmsg_remote_module)
  632. def test_check_command_without_recvmsg_remote_module_by_name(self):
  633. """
  634. Test updates on remote module.
  635. The remote module is specified by its name.
  636. """
  637. self._common_remote_module_by_name_test(
  638. self._internal_check_command_without_recvmsg_remote_module)
  639. def _internal_check_command_without_recvmsg_remote_module2(self,
  640. function_lambda,
  641. param,
  642. fill_other_messages):
  643. fake_session = FakeModuleCCSession()
  644. mccs = self.create_session("spec1.spec", None, None, fake_session)
  645. mccs.set_config_handler(self.my_config_handler_ok)
  646. function = function_lambda(mccs)
  647. self.assertEqual(len(fake_session.message_queue), 0)
  648. fill_other_messages(fake_session)
  649. fake_session.group_sendmsg(None, 'Spec2')
  650. rmodname = function(param)
  651. if (len(fake_session.message_queue) == 2):
  652. self.assertEqual({'command': ['get_module_spec',
  653. {'module_name': 'Spec2'}]},
  654. fake_session.get_message('ConfigManager', None))
  655. self.assertEqual({'command': ['get_config', {'module_name': 'Spec2'}]},
  656. fake_session.get_message('ConfigManager', None))
  657. self.assertEqual(len(fake_session.message_queue), 0)
  658. cmd = isc.config.ccsession.create_command(isc.config.ccsession.COMMAND_CONFIG_UPDATE, { 'Spec3': { 'item1': 2 }})
  659. env = { 'group':'Spec3', 'from':None }
  660. self.assertEqual(len(fake_session.message_queue), 0)
  661. mccs.check_command_without_recvmsg(cmd, env)
  662. self.assertEqual(len(fake_session.message_queue), 0)
  663. def test_check_command_without_recvmsg_remote_module2(self):
  664. """
  665. Test updates on remote module.
  666. The remote module is specified by the spec file name.
  667. """
  668. self._common_remote_module_test(
  669. self._internal_check_command_without_recvmsg_remote_module2)
  670. def test_check_command_without_recvmsg_remote_module_by_name2(self):
  671. """
  672. Test updates on remote module.
  673. The remote module is specified by its name.
  674. """
  675. self._common_remote_module_by_name_test(
  676. self._internal_check_command_without_recvmsg_remote_module2)
  677. def _internal_remote_module_bad_config(self, function_lambda, parameter,
  678. fill_other_messages):
  679. fake_session = FakeModuleCCSession()
  680. mccs = self.create_session("spec1.spec", None, None, fake_session)
  681. function = function_lambda(mccs)
  682. # Provide wrong config data. It should be rejected.
  683. fill_other_messages(fake_session)
  684. fake_session.group_sendmsg({'result': [0, {"bad_item": -1}]}, 'Spec2')
  685. self.assertRaises(isc.config.ModuleCCSessionError,
  686. function, parameter)
  687. def test_remote_module_bad_config(self):
  688. """
  689. Test the remote module rejects bad config data.
  690. """
  691. self._common_remote_module_test(
  692. self._internal_remote_module_bad_config)
  693. def test_remote_module_by_name_bad_config(self):
  694. """
  695. Test the remote module rejects bad config data.
  696. """
  697. self._common_remote_module_by_name_test(
  698. self._internal_remote_module_bad_config)
  699. def _internal_remote_module_error_response(self, function_lambda,
  700. parameter, fill_other_messages):
  701. fake_session = FakeModuleCCSession()
  702. mccs = self.create_session("spec1.spec", None, None, fake_session)
  703. function = function_lambda(mccs)
  704. # Provide wrong config data. It should be rejected.
  705. fill_other_messages(fake_session)
  706. fake_session.group_sendmsg({'result': [1, "An error, and I mean it!"]},
  707. 'Spec2')
  708. self.assertRaises(isc.config.ModuleCCSessionError,
  709. function, parameter)
  710. def test_remote_module_bad_config(self):
  711. """
  712. Test the remote module complains if there's an error response."
  713. """
  714. self._common_remote_module_test(
  715. self._internal_remote_module_error_response)
  716. def test_remote_module_by_name_bad_config(self):
  717. """
  718. Test the remote module complains if there's an error response."
  719. """
  720. self._common_remote_module_by_name_test(
  721. self._internal_remote_module_error_response)
  722. def test_remote_module_bad_config(self):
  723. """
  724. Test the remote module rejects bad config data.
  725. """
  726. self._common_remote_module_by_name_test(
  727. self._internal_remote_module_bad_config)
  728. def test_module_name_mismatch(self):
  729. fake_session = FakeModuleCCSession()
  730. mccs = self.create_session("spec1.spec", None, None, fake_session)
  731. mccs.set_config_handler(self.my_config_handler_ok)
  732. self._prepare_spec_message(fake_session, 'spec1.spec')
  733. self.assertRaises(isc.config.ModuleCCSessionError,
  734. mccs.add_remote_config_by_name, "Spec2")
  735. def test_logconfig_handler(self):
  736. # test whether default_logconfig_handler reacts nicely to
  737. # bad data. We assume the actual logger output is tested
  738. # elsewhere
  739. self.assertRaises(TypeError, default_logconfig_handler);
  740. self.assertRaises(TypeError, default_logconfig_handler, 1);
  741. spec = isc.config.module_spec_from_file(
  742. path_search('logging.spec', bind10_config.PLUGIN_PATHS))
  743. config_data = ConfigData(spec)
  744. self.assertRaises(TypeError, default_logconfig_handler, 1, config_data)
  745. default_logconfig_handler({}, config_data)
  746. # Wrong data should not raise, but simply not be accepted
  747. # This would log a lot of errors, so we may want to suppress that later
  748. default_logconfig_handler({ "bad_data": "indeed" }, config_data)
  749. default_logconfig_handler({ "bad_data": 1}, config_data)
  750. default_logconfig_handler({ "bad_data": 1123 }, config_data)
  751. default_logconfig_handler({ "bad_data": True }, config_data)
  752. default_logconfig_handler({ "bad_data": False }, config_data)
  753. default_logconfig_handler({ "bad_data": 1.1 }, config_data)
  754. default_logconfig_handler({ "bad_data": [] }, config_data)
  755. default_logconfig_handler({ "bad_data": [[],[],[[1, 3, False, "foo" ]]] },
  756. config_data)
  757. default_logconfig_handler({ "bad_data": [ 1, 2, { "b": { "c": "d" } } ] },
  758. config_data)
  759. # Try a correct config
  760. log_conf = {"loggers":
  761. [{"name": "b10-xfrout", "output_options":
  762. [{"output": "/tmp/bind10.log",
  763. "destination": "file",
  764. "flush": True}]}]}
  765. default_logconfig_handler(log_conf, config_data)
  766. class fakeData:
  767. def decode(self):
  768. return "{}";
  769. class fakeAnswer:
  770. def read(self):
  771. return fakeData();
  772. class fakeUIConn():
  773. def __init__(self):
  774. self.get_answers = {}
  775. self.post_answers = {}
  776. def set_get_answer(self, name, answer):
  777. self.get_answers[name] = answer
  778. def set_post_answer(self, name, answer):
  779. self.post_answers[name] = answer
  780. def send_GET(self, name, arg = None):
  781. if name in self.get_answers:
  782. return self.get_answers[name]
  783. else:
  784. return {}
  785. def send_POST(self, name, arg = None):
  786. if name in self.post_answers:
  787. return self.post_answers[name]
  788. else:
  789. return fakeAnswer()
  790. class TestUIModuleCCSession(unittest.TestCase):
  791. def setUp(self):
  792. if 'CONFIG_TESTDATA_PATH' in os.environ:
  793. self.data_path = os.environ['CONFIG_TESTDATA_PATH']
  794. else:
  795. self.data_path = "../../../testdata"
  796. def spec_file(self, file):
  797. return self.data_path + os.sep + file
  798. def create_uccs2(self, fake_conn):
  799. module_spec = isc.config.module_spec_from_file(self.spec_file("spec2.spec"))
  800. fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
  801. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  802. return UIModuleCCSession(fake_conn)
  803. def create_uccs_named_set(self, fake_conn):
  804. module_spec = isc.config.module_spec_from_file(self.spec_file("spec32.spec"))
  805. fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
  806. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  807. return UIModuleCCSession(fake_conn)
  808. def create_uccs_listtest(self, fake_conn):
  809. module_spec = isc.config.module_spec_from_file(self.spec_file("spec39.spec"))
  810. fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
  811. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  812. return UIModuleCCSession(fake_conn)
  813. def test_init(self):
  814. fake_conn = fakeUIConn()
  815. fake_conn.set_get_answer('/module_spec', {})
  816. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  817. uccs = UIModuleCCSession(fake_conn)
  818. self.assertEqual({}, uccs._specifications)
  819. self.assertEqual({ 'version': BIND10_CONFIG_DATA_VERSION}, uccs._current_config)
  820. module_spec = isc.config.module_spec_from_file(self.spec_file("spec2.spec"))
  821. fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
  822. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  823. uccs = UIModuleCCSession(fake_conn)
  824. self.assertEqual(module_spec._module_spec, uccs._specifications['Spec2']._module_spec)
  825. fake_conn.set_get_answer('/config_data', { 'version': 123123 })
  826. self.assertRaises(ModuleCCSessionError, UIModuleCCSession, fake_conn)
  827. def test_request_specifications(self):
  828. module_spec1 = isc.config.module_spec_from_file(
  829. self.spec_file("spec1.spec"))
  830. module_spec_dict1 = { "module_spec": module_spec1.get_full_spec() }
  831. module_spec2 = isc.config.module_spec_from_file(
  832. self.spec_file("spec2.spec"))
  833. module_spec_dict2 = { "module_spec": module_spec2.get_full_spec() }
  834. fake_conn = fakeUIConn()
  835. # Set the first one in the answer
  836. fake_conn.set_get_answer('/module_spec', module_spec_dict1)
  837. fake_conn.set_get_answer('/config_data',
  838. { 'version': BIND10_CONFIG_DATA_VERSION })
  839. uccs = UIModuleCCSession(fake_conn)
  840. # We should now have the first one, but not the second.
  841. self.assertTrue("Spec1" in uccs._specifications)
  842. self.assertEqual(module_spec1.get_full_spec(),
  843. uccs._specifications["Spec1"].get_full_spec())
  844. self.assertFalse("Spec2" in uccs._specifications)
  845. # Now set an answer where only the second one is present
  846. fake_conn.set_get_answer('/module_spec', module_spec_dict2)
  847. uccs.request_specifications()
  848. # Now Spec1 should have been removed, and spec2 should be there
  849. self.assertFalse("Spec1" in uccs._specifications)
  850. self.assertTrue("Spec2" in uccs._specifications)
  851. self.assertEqual(module_spec2.get_full_spec(),
  852. uccs._specifications["Spec2"].get_full_spec())
  853. def test_add_remove_value(self):
  854. fake_conn = fakeUIConn()
  855. uccs = self.create_uccs2(fake_conn)
  856. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.add_value, 1, "a")
  857. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.add_value, "no_such_item", "a")
  858. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.add_value, "Spec2/item1", "a")
  859. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.remove_value, 1, "a")
  860. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.remove_value, "no_such_item", "a")
  861. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.remove_value, "Spec2/item1", "a")
  862. self.assertEqual({}, uccs._local_changes)
  863. uccs.add_value("Spec2/item5", "foo")
  864. self.assertEqual({'Spec2': {'item5': ['a', 'b', 'foo']}}, uccs._local_changes)
  865. uccs.remove_value("Spec2/item5", "foo")
  866. self.assertEqual({'Spec2': {'item5': ['a', 'b']}}, uccs._local_changes)
  867. uccs._local_changes = {'Spec2': {'item5': []}}
  868. uccs.remove_value("Spec2/item5", "foo")
  869. uccs.add_value("Spec2/item5", "foo")
  870. self.assertEqual({'Spec2': {'item5': ['foo']}}, uccs._local_changes)
  871. self.assertRaises(isc.cc.data.DataAlreadyPresentError,
  872. uccs.add_value, "Spec2/item5", "foo")
  873. self.assertEqual({'Spec2': {'item5': ['foo']}}, uccs._local_changes)
  874. self.assertRaises(isc.cc.data.DataNotFoundError,
  875. uccs.remove_value, "Spec2/item5[123]", None)
  876. uccs.remove_value("Spec2/item5[0]", None)
  877. self.assertEqual({'Spec2': {'item5': []}}, uccs._local_changes)
  878. uccs.add_value("Spec2/item5", None);
  879. self.assertEqual({'Spec2': {'item5': ['']}}, uccs._local_changes)
  880. # Intending to empty a list element, but forget specifying the index.
  881. self.assertRaises(isc.cc.data.DataTypeError,
  882. uccs.remove_value, "Spec2/item5", None)
  883. def test_add_dup_value(self):
  884. fake_conn = fakeUIConn()
  885. uccs = self.create_uccs_listtest(fake_conn)
  886. uccs.add_value("Spec39/list")
  887. self.assertRaises(isc.cc.data.DataAlreadyPresentError, uccs.add_value,
  888. "Spec39/list")
  889. def test_add_remove_value_named_set(self):
  890. fake_conn = fakeUIConn()
  891. uccs = self.create_uccs_named_set(fake_conn)
  892. value, status = uccs.get_value("/Spec32/named_set_item")
  893. self.assertEqual({'a': 1, 'b': 2}, value)
  894. # make sure that removing from default actually removes it
  895. uccs.remove_value("/Spec32/named_set_item", "a")
  896. value, status = uccs.get_value("/Spec32/named_set_item")
  897. self.assertEqual({'b': 2}, value)
  898. self.assertEqual(uccs.LOCAL, status)
  899. # ok, put it back now
  900. uccs.add_value("/Spec32/named_set_item", "a")
  901. uccs.set_value("/Spec32/named_set_item/a", 1)
  902. uccs.add_value("/Spec32/named_set_item", "foo")
  903. value, status = uccs.get_value("/Spec32/named_set_item")
  904. self.assertEqual({'a': 1, 'b': 2, 'foo': 3}, value)
  905. uccs.remove_value("/Spec32/named_set_item", "a")
  906. uccs.remove_value("/Spec32/named_set_item", "foo")
  907. value, status = uccs.get_value("/Spec32/named_set_item")
  908. self.assertEqual({'b': 2}, value)
  909. uccs.set_value("/Spec32/named_set_item/c", 5)
  910. value, status = uccs.get_value("/Spec32/named_set_item")
  911. self.assertEqual({"b": 2, "c": 5}, value)
  912. self.assertRaises(isc.cc.data.DataNotFoundError,
  913. uccs.set_value,
  914. "/Spec32/named_set_item/no_such_item/a",
  915. 4)
  916. self.assertRaises(isc.cc.data.DataNotFoundError,
  917. uccs.remove_value, "/Spec32/named_set_item",
  918. "no_such_item")
  919. self.assertRaises(isc.cc.data.DataAlreadyPresentError,
  920. uccs.add_value, "/Spec32/named_set_item", "c")
  921. def test_set_value_named_set(self):
  922. fake_conn = fakeUIConn()
  923. uccs = self.create_uccs_named_set(fake_conn)
  924. value, status = uccs.get_value("/Spec32/named_set_item2")
  925. self.assertEqual({}, value)
  926. self.assertEqual(status, uccs.DEFAULT)
  927. # Try setting a value that is optional but has no default
  928. uccs.add_value("/Spec32/named_set_item2", "new1")
  929. uccs.set_value("/Spec32/named_set_item2/new1/first", 3)
  930. # Different method to add a new element
  931. uccs.set_value("/Spec32/named_set_item2/new2", { "second": 4 })
  932. value, status = uccs.get_value("/Spec32/named_set_item2")
  933. self.assertEqual({ "new1": {"first": 3 }, "new2": {"second": 4}},
  934. value)
  935. self.assertEqual(status, uccs.LOCAL)
  936. uccs.set_value("/Spec32/named_set_item2/new1/second", "foo")
  937. value, status = uccs.get_value("/Spec32/named_set_item2")
  938. self.assertEqual({ "new1": {"first": 3, "second": "foo" },
  939. "new2": {"second": 4}},
  940. value)
  941. self.assertEqual(status, uccs.LOCAL)
  942. # make sure using a bad name still fails
  943. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.set_value,
  944. "/Spec32/named_set_item2/doesnotexist/first", 3)
  945. def test_commit(self):
  946. fake_conn = fakeUIConn()
  947. uccs = self.create_uccs2(fake_conn)
  948. uccs.commit()
  949. uccs._local_changes = {'Spec2': {'item5': [ 'a' ]}}
  950. uccs.commit()
  951. if __name__ == '__main__':
  952. isc.log.init("bind10")
  953. unittest.main()