ccsession_test.py 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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, parameter, 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 the
  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 alse 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()
  536. # should incorporate the overridden value, and we should be abel 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 test_logconfig_handler(self):
  678. # test whether default_logconfig_handler reacts nicely to
  679. # bad data. We assume the actual logger output is tested
  680. # elsewhere
  681. self.assertRaises(TypeError, default_logconfig_handler);
  682. self.assertRaises(TypeError, default_logconfig_handler, 1);
  683. spec = isc.config.module_spec_from_file(
  684. path_search('logging.spec', bind10_config.PLUGIN_PATHS))
  685. config_data = ConfigData(spec)
  686. self.assertRaises(TypeError, default_logconfig_handler, 1, config_data)
  687. default_logconfig_handler({}, config_data)
  688. # Wrong data should not raise, but simply not be accepted
  689. # This would log a lot of errors, so we may want to suppress that later
  690. default_logconfig_handler({ "bad_data": "indeed" }, config_data)
  691. default_logconfig_handler({ "bad_data": 1}, config_data)
  692. default_logconfig_handler({ "bad_data": 1123 }, config_data)
  693. default_logconfig_handler({ "bad_data": True }, config_data)
  694. default_logconfig_handler({ "bad_data": False }, config_data)
  695. default_logconfig_handler({ "bad_data": 1.1 }, config_data)
  696. default_logconfig_handler({ "bad_data": [] }, config_data)
  697. default_logconfig_handler({ "bad_data": [[],[],[[1, 3, False, "foo" ]]] },
  698. config_data)
  699. default_logconfig_handler({ "bad_data": [ 1, 2, { "b": { "c": "d" } } ] },
  700. config_data)
  701. # Try a correct config
  702. log_conf = {"loggers":
  703. [{"name": "b10-xfrout", "output_options":
  704. [{"output": "/tmp/bind10.log",
  705. "destination": "file",
  706. "flush": True}]}]}
  707. default_logconfig_handler(log_conf, config_data)
  708. class fakeData:
  709. def decode(self):
  710. return "{}";
  711. class fakeAnswer:
  712. def read(self):
  713. return fakeData();
  714. class fakeUIConn():
  715. def __init__(self):
  716. self.get_answers = {}
  717. self.post_answers = {}
  718. def set_get_answer(self, name, answer):
  719. self.get_answers[name] = answer
  720. def set_post_answer(self, name, answer):
  721. self.post_answers[name] = answer
  722. def send_GET(self, name, arg = None):
  723. if name in self.get_answers:
  724. return self.get_answers[name]
  725. else:
  726. return {}
  727. def send_POST(self, name, arg = None):
  728. if name in self.post_answers:
  729. return self.post_answers[name]
  730. else:
  731. return fakeAnswer()
  732. class TestUIModuleCCSession(unittest.TestCase):
  733. def setUp(self):
  734. if 'CONFIG_TESTDATA_PATH' in os.environ:
  735. self.data_path = os.environ['CONFIG_TESTDATA_PATH']
  736. else:
  737. self.data_path = "../../../testdata"
  738. def spec_file(self, file):
  739. return self.data_path + os.sep + file
  740. def create_uccs2(self, fake_conn):
  741. module_spec = isc.config.module_spec_from_file(self.spec_file("spec2.spec"))
  742. fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
  743. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  744. return UIModuleCCSession(fake_conn)
  745. def create_uccs_named_set(self, fake_conn):
  746. module_spec = isc.config.module_spec_from_file(self.spec_file("spec32.spec"))
  747. fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
  748. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  749. return UIModuleCCSession(fake_conn)
  750. def create_uccs_listtest(self, fake_conn):
  751. module_spec = isc.config.module_spec_from_file(self.spec_file("spec39.spec"))
  752. fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
  753. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  754. return UIModuleCCSession(fake_conn)
  755. def test_init(self):
  756. fake_conn = fakeUIConn()
  757. fake_conn.set_get_answer('/module_spec', {})
  758. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  759. uccs = UIModuleCCSession(fake_conn)
  760. self.assertEqual({}, uccs._specifications)
  761. self.assertEqual({ 'version': BIND10_CONFIG_DATA_VERSION}, uccs._current_config)
  762. module_spec = isc.config.module_spec_from_file(self.spec_file("spec2.spec"))
  763. fake_conn.set_get_answer('/module_spec', { module_spec.get_module_name(): module_spec.get_full_spec()})
  764. fake_conn.set_get_answer('/config_data', { 'version': BIND10_CONFIG_DATA_VERSION })
  765. uccs = UIModuleCCSession(fake_conn)
  766. self.assertEqual(module_spec._module_spec, uccs._specifications['Spec2']._module_spec)
  767. fake_conn.set_get_answer('/config_data', { 'version': 123123 })
  768. self.assertRaises(ModuleCCSessionError, UIModuleCCSession, fake_conn)
  769. def test_request_specifications(self):
  770. module_spec1 = isc.config.module_spec_from_file(
  771. self.spec_file("spec1.spec"))
  772. module_spec_dict1 = { "module_spec": module_spec1.get_full_spec() }
  773. module_spec2 = isc.config.module_spec_from_file(
  774. self.spec_file("spec2.spec"))
  775. module_spec_dict2 = { "module_spec": module_spec2.get_full_spec() }
  776. fake_conn = fakeUIConn()
  777. # Set the first one in the answer
  778. fake_conn.set_get_answer('/module_spec', module_spec_dict1)
  779. fake_conn.set_get_answer('/config_data',
  780. { 'version': BIND10_CONFIG_DATA_VERSION })
  781. uccs = UIModuleCCSession(fake_conn)
  782. # We should now have the first one, but not the second.
  783. self.assertTrue("Spec1" in uccs._specifications)
  784. self.assertEqual(module_spec1.get_full_spec(),
  785. uccs._specifications["Spec1"].get_full_spec())
  786. self.assertFalse("Spec2" in uccs._specifications)
  787. # Now set an answer where only the second one is present
  788. fake_conn.set_get_answer('/module_spec', module_spec_dict2)
  789. uccs.request_specifications()
  790. # Now Spec1 should have been removed, and spec2 should be there
  791. self.assertFalse("Spec1" in uccs._specifications)
  792. self.assertTrue("Spec2" in uccs._specifications)
  793. self.assertEqual(module_spec2.get_full_spec(),
  794. uccs._specifications["Spec2"].get_full_spec())
  795. def test_add_remove_value(self):
  796. fake_conn = fakeUIConn()
  797. uccs = self.create_uccs2(fake_conn)
  798. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.add_value, 1, "a")
  799. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.add_value, "no_such_item", "a")
  800. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.add_value, "Spec2/item1", "a")
  801. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.remove_value, 1, "a")
  802. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.remove_value, "no_such_item", "a")
  803. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.remove_value, "Spec2/item1", "a")
  804. self.assertEqual({}, uccs._local_changes)
  805. uccs.add_value("Spec2/item5", "foo")
  806. self.assertEqual({'Spec2': {'item5': ['a', 'b', 'foo']}}, uccs._local_changes)
  807. uccs.remove_value("Spec2/item5", "foo")
  808. self.assertEqual({'Spec2': {'item5': ['a', 'b']}}, uccs._local_changes)
  809. uccs._local_changes = {'Spec2': {'item5': []}}
  810. uccs.remove_value("Spec2/item5", "foo")
  811. uccs.add_value("Spec2/item5", "foo")
  812. self.assertEqual({'Spec2': {'item5': ['foo']}}, uccs._local_changes)
  813. self.assertRaises(isc.cc.data.DataAlreadyPresentError,
  814. uccs.add_value, "Spec2/item5", "foo")
  815. self.assertEqual({'Spec2': {'item5': ['foo']}}, uccs._local_changes)
  816. self.assertRaises(isc.cc.data.DataNotFoundError,
  817. uccs.remove_value, "Spec2/item5[123]", None)
  818. uccs.remove_value("Spec2/item5[0]", None)
  819. self.assertEqual({'Spec2': {'item5': []}}, uccs._local_changes)
  820. uccs.add_value("Spec2/item5", None);
  821. self.assertEqual({'Spec2': {'item5': ['']}}, uccs._local_changes)
  822. # Intending to empty a list element, but forget specifying the index.
  823. self.assertRaises(isc.cc.data.DataTypeError,
  824. uccs.remove_value, "Spec2/item5", None)
  825. def test_add_dup_value(self):
  826. fake_conn = fakeUIConn()
  827. uccs = self.create_uccs_listtest(fake_conn)
  828. uccs.add_value("Spec39/list")
  829. self.assertRaises(isc.cc.data.DataAlreadyPresentError, uccs.add_value,
  830. "Spec39/list")
  831. def test_add_remove_value_named_set(self):
  832. fake_conn = fakeUIConn()
  833. uccs = self.create_uccs_named_set(fake_conn)
  834. value, status = uccs.get_value("/Spec32/named_set_item")
  835. self.assertEqual({'a': 1, 'b': 2}, value)
  836. # make sure that removing from default actually removes it
  837. uccs.remove_value("/Spec32/named_set_item", "a")
  838. value, status = uccs.get_value("/Spec32/named_set_item")
  839. self.assertEqual({'b': 2}, value)
  840. self.assertEqual(uccs.LOCAL, status)
  841. # ok, put it back now
  842. uccs.add_value("/Spec32/named_set_item", "a")
  843. uccs.set_value("/Spec32/named_set_item/a", 1)
  844. uccs.add_value("/Spec32/named_set_item", "foo")
  845. value, status = uccs.get_value("/Spec32/named_set_item")
  846. self.assertEqual({'a': 1, 'b': 2, 'foo': 3}, value)
  847. uccs.remove_value("/Spec32/named_set_item", "a")
  848. uccs.remove_value("/Spec32/named_set_item", "foo")
  849. value, status = uccs.get_value("/Spec32/named_set_item")
  850. self.assertEqual({'b': 2}, value)
  851. uccs.set_value("/Spec32/named_set_item/c", 5)
  852. value, status = uccs.get_value("/Spec32/named_set_item")
  853. self.assertEqual({"b": 2, "c": 5}, value)
  854. self.assertRaises(isc.cc.data.DataNotFoundError,
  855. uccs.set_value,
  856. "/Spec32/named_set_item/no_such_item/a",
  857. 4)
  858. self.assertRaises(isc.cc.data.DataNotFoundError,
  859. uccs.remove_value, "/Spec32/named_set_item",
  860. "no_such_item")
  861. self.assertRaises(isc.cc.data.DataAlreadyPresentError,
  862. uccs.add_value, "/Spec32/named_set_item", "c")
  863. def test_set_value_named_set(self):
  864. fake_conn = fakeUIConn()
  865. uccs = self.create_uccs_named_set(fake_conn)
  866. value, status = uccs.get_value("/Spec32/named_set_item2")
  867. self.assertEqual({}, value)
  868. self.assertEqual(status, uccs.DEFAULT)
  869. # Try setting a value that is optional but has no default
  870. uccs.add_value("/Spec32/named_set_item2", "new1")
  871. uccs.set_value("/Spec32/named_set_item2/new1/first", 3)
  872. # Different method to add a new element
  873. uccs.set_value("/Spec32/named_set_item2/new2", { "second": 4 })
  874. value, status = uccs.get_value("/Spec32/named_set_item2")
  875. self.assertEqual({ "new1": {"first": 3 }, "new2": {"second": 4}},
  876. value)
  877. self.assertEqual(status, uccs.LOCAL)
  878. uccs.set_value("/Spec32/named_set_item2/new1/second", "foo")
  879. value, status = uccs.get_value("/Spec32/named_set_item2")
  880. self.assertEqual({ "new1": {"first": 3, "second": "foo" },
  881. "new2": {"second": 4}},
  882. value)
  883. self.assertEqual(status, uccs.LOCAL)
  884. # make sure using a bad name still fails
  885. self.assertRaises(isc.cc.data.DataNotFoundError, uccs.set_value,
  886. "/Spec32/named_set_item2/doesnotexist/first", 3)
  887. def test_commit(self):
  888. fake_conn = fakeUIConn()
  889. uccs = self.create_uccs2(fake_conn)
  890. uccs.commit()
  891. uccs._local_changes = {'Spec2': {'item5': [ 'a' ]}}
  892. uccs.commit()
  893. if __name__ == '__main__':
  894. isc.log.init("bind10")
  895. unittest.main()