cfgmgr_test.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 configuration manager module
  17. #
  18. import unittest
  19. import os
  20. from isc.config.cfgmgr import *
  21. from isc.config import config_data
  22. from unittest_fakesession import FakeModuleCCSession
  23. class TestConfigManagerData(unittest.TestCase):
  24. def setUp(self):
  25. self.data_path = os.environ['CONFIG_TESTDATA_PATH']
  26. self.writable_data_path = os.environ['CONFIG_WR_TESTDATA_PATH']
  27. self.config_manager_data = ConfigManagerData(self.writable_data_path,
  28. file_name="b10-config.db")
  29. self.assert_(self.config_manager_data)
  30. def test_abs_file(self):
  31. """
  32. Test what happens if we give the config manager an absolute path.
  33. It shouldn't append the data path to it.
  34. """
  35. abs_path = self.data_path + os.sep + "b10-config-imaginary.db"
  36. data = ConfigManagerData(self.data_path, abs_path)
  37. self.assertEqual(abs_path, data.db_filename)
  38. self.assertEqual(self.data_path, data.data_path)
  39. def test_init(self):
  40. self.assertEqual(self.config_manager_data.data['version'],
  41. config_data.BIND10_CONFIG_DATA_VERSION)
  42. self.assertEqual(self.config_manager_data.data_path,
  43. self.writable_data_path)
  44. self.assertEqual(self.config_manager_data.db_filename,
  45. self.writable_data_path + os.sep + "b10-config.db")
  46. def test_read_from_file(self):
  47. ConfigManagerData.read_from_file(self.writable_data_path, "b10-config.db")
  48. self.assertRaises(ConfigManagerDataEmpty,
  49. ConfigManagerData.read_from_file,
  50. "doesnotexist", "b10-config.db")
  51. self.assertRaises(ConfigManagerDataReadError,
  52. ConfigManagerData.read_from_file,
  53. self.data_path, "b10-config-bad1.db")
  54. self.assertRaises(ConfigManagerDataReadError,
  55. ConfigManagerData.read_from_file,
  56. self.data_path, "b10-config-bad2.db")
  57. self.assertRaises(ConfigManagerDataReadError,
  58. ConfigManagerData.read_from_file,
  59. self.data_path, "b10-config-bad3.db")
  60. self.assertRaises(ConfigManagerDataReadError,
  61. ConfigManagerData.read_from_file,
  62. self.data_path, "b10-config-bad4.db")
  63. def test_write_to_file(self):
  64. output_file_name = "b10-config-write-test"
  65. self.config_manager_data.write_to_file(output_file_name)
  66. new_config = ConfigManagerData(self.data_path, output_file_name)
  67. self.assertEqual(self.config_manager_data, new_config)
  68. os.remove(output_file_name)
  69. def test_equality(self):
  70. # tests the __eq__ function. Equality is only defined
  71. # by equality of the .data element. If data_path or db_filename
  72. # are different, but the contents are the same, it's still
  73. # considered equal
  74. cfd1 = ConfigManagerData(self.data_path, file_name="b10-config.db")
  75. cfd2 = ConfigManagerData(self.data_path, file_name="b10-config.db")
  76. self.assertEqual(cfd1, cfd2)
  77. cfd2.data_path = "some/unknown/path"
  78. self.assertEqual(cfd1, cfd2)
  79. cfd2.db_filename = "bad_file.name"
  80. self.assertEqual(cfd1, cfd2)
  81. cfd2.data['test'] = { 'a': [ 1, 2, 3]}
  82. self.assertNotEqual(cfd1, cfd2)
  83. class TestConfigManager(unittest.TestCase):
  84. def setUp(self):
  85. self.data_path = os.environ['CONFIG_TESTDATA_PATH']
  86. self.writable_data_path = os.environ['CONFIG_WR_TESTDATA_PATH']
  87. self.fake_session = FakeModuleCCSession()
  88. self.cm = ConfigManager(self.writable_data_path,
  89. database_filename="b10-config.db",
  90. session=self.fake_session)
  91. self.name = "TestModule"
  92. self.spec = isc.config.module_spec_from_file(self.data_path + os.sep + "/spec2.spec")
  93. def test_paths(self):
  94. """
  95. Test data_path and database filename is passed trough to
  96. underlying ConfigManagerData.
  97. """
  98. cm = ConfigManager("datapath", "filename", self.fake_session)
  99. self.assertEqual("datapath" + os.sep + "filename",
  100. cm.config.db_filename)
  101. # It should preserve it while reading
  102. cm.read_config()
  103. self.assertEqual("datapath" + os.sep + "filename",
  104. cm.config.db_filename)
  105. def test_init(self):
  106. self.assert_(self.cm.module_specs == {})
  107. self.assert_(self.cm.data_path == self.writable_data_path)
  108. self.assert_(self.cm.config != None)
  109. self.assert_(self.fake_session.has_subscription("ConfigManager"))
  110. self.assert_(self.fake_session.has_subscription("Boss", "ConfigManager"))
  111. self.assertFalse(self.cm.running)
  112. def test_notify_boss(self):
  113. self.cm.notify_boss()
  114. msg = self.fake_session.get_message("Boss", None)
  115. self.assert_(msg)
  116. # this one is actually wrong, but 'current status quo'
  117. self.assertEqual(msg, {"running": "ConfigManager"})
  118. def test_set_module_spec(self):
  119. module_spec = isc.config.module_spec.module_spec_from_file(self.data_path + os.sep + "spec1.spec")
  120. self.assert_(module_spec.get_module_name() not in self.cm.module_specs)
  121. self.cm.set_module_spec(module_spec)
  122. self.assert_(module_spec.get_module_name() in self.cm.module_specs)
  123. self.assert_(module_spec.get_module_name() not in
  124. self.cm.virtual_modules)
  125. def test_remove_module_spec(self):
  126. module_spec = isc.config.module_spec.module_spec_from_file(self.data_path + os.sep + "spec1.spec")
  127. self.assert_(module_spec.get_module_name() not in self.cm.module_specs)
  128. self.cm.set_module_spec(module_spec)
  129. self.assert_(module_spec.get_module_name() in self.cm.module_specs)
  130. self.cm.remove_module_spec(module_spec.get_module_name())
  131. self.assert_(module_spec.get_module_name() not in self.cm.module_specs)
  132. self.assert_(module_spec.get_module_name() not in
  133. self.cm.virtual_modules)
  134. def test_add_remove_virtual_module(self):
  135. module_spec = isc.config.module_spec.module_spec_from_file(
  136. self.data_path + os.sep + "spec1.spec")
  137. check_func = lambda: True
  138. # Make sure it's not in there before
  139. self.assert_(module_spec.get_module_name() not in self.cm.module_specs)
  140. self.assert_(module_spec.get_module_name() not in
  141. self.cm.virtual_modules)
  142. # Add it there
  143. self.cm.set_virtual_module(module_spec, check_func)
  144. # Check it's in there
  145. self.assert_(module_spec.get_module_name() in self.cm.module_specs)
  146. self.assertEqual(self.cm.module_specs[module_spec.get_module_name()],
  147. module_spec)
  148. self.assertEqual(self.cm.virtual_modules[module_spec.get_module_name()],
  149. check_func)
  150. # Remove it again
  151. self.cm.remove_module_spec(module_spec.get_module_name())
  152. self.assert_(module_spec.get_module_name() not in self.cm.module_specs)
  153. self.assert_(module_spec.get_module_name() not in
  154. self.cm.virtual_modules)
  155. def test_get_module_spec(self):
  156. module_spec = isc.config.module_spec.module_spec_from_file(self.data_path + os.sep + "spec1.spec")
  157. self.assert_(module_spec.get_module_name() not in self.cm.module_specs)
  158. self.cm.set_module_spec(module_spec)
  159. self.assert_(module_spec.get_module_name() in self.cm.module_specs)
  160. module_spec2 = self.cm.get_module_spec(module_spec.get_module_name())
  161. self.assertEqual(module_spec.get_full_spec(), module_spec2)
  162. self.assertEqual({}, self.cm.get_module_spec("nosuchmodule"))
  163. def test_get_config_spec(self):
  164. config_spec = self.cm.get_config_spec()
  165. self.assertEqual(config_spec, {})
  166. module_spec = isc.config.module_spec.module_spec_from_file(self.data_path + os.sep + "spec1.spec")
  167. self.assert_(module_spec.get_module_name() not in self.cm.module_specs)
  168. self.cm.set_module_spec(module_spec)
  169. self.assert_(module_spec.get_module_name() in self.cm.module_specs)
  170. config_spec = self.cm.get_config_spec()
  171. self.assertEqual(config_spec, { 'Spec1': None })
  172. self.cm.remove_module_spec('Spec1')
  173. module_spec = isc.config.module_spec.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
  174. self.assert_(module_spec.get_module_name() not in self.cm.module_specs)
  175. self.cm.set_module_spec(module_spec)
  176. self.assert_(module_spec.get_module_name() in self.cm.module_specs)
  177. config_spec = self.cm.get_config_spec()
  178. self.assertEqual(config_spec['Spec2'], module_spec.get_config_spec())
  179. config_spec = self.cm.get_config_spec('Spec2')
  180. self.assertEqual(config_spec['Spec2'], module_spec.get_config_spec())
  181. def test_get_commands_spec(self):
  182. commands_spec = self.cm.get_commands_spec()
  183. self.assertEqual(commands_spec, {})
  184. module_spec = isc.config.module_spec.module_spec_from_file(self.data_path + os.sep + "spec1.spec")
  185. self.assert_(module_spec.get_module_name() not in self.cm.module_specs)
  186. self.cm.set_module_spec(module_spec)
  187. self.assert_(module_spec.get_module_name() in self.cm.module_specs)
  188. commands_spec = self.cm.get_commands_spec()
  189. self.assertEqual(commands_spec, { 'Spec1': None })
  190. self.cm.remove_module_spec('Spec1')
  191. module_spec = isc.config.module_spec.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
  192. self.assert_(module_spec.get_module_name() not in self.cm.module_specs)
  193. self.cm.set_module_spec(module_spec)
  194. self.assert_(module_spec.get_module_name() in self.cm.module_specs)
  195. commands_spec = self.cm.get_commands_spec()
  196. self.assertEqual(commands_spec['Spec2'], module_spec.get_commands_spec())
  197. commands_spec = self.cm.get_commands_spec('Spec2')
  198. self.assertEqual(commands_spec['Spec2'], module_spec.get_commands_spec())
  199. def test_get_statistics_spec(self):
  200. statistics_spec = self.cm.get_statistics_spec()
  201. self.assertEqual(statistics_spec, {})
  202. module_spec = isc.config.module_spec.module_spec_from_file(self.data_path + os.sep + "spec1.spec")
  203. self.assert_(module_spec.get_module_name() not in self.cm.module_specs)
  204. self.cm.set_module_spec(module_spec)
  205. self.assert_(module_spec.get_module_name() in self.cm.module_specs)
  206. statistics_spec = self.cm.get_statistics_spec()
  207. self.assertEqual(statistics_spec, { 'Spec1': None })
  208. self.cm.remove_module_spec('Spec1')
  209. module_spec = isc.config.module_spec.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
  210. self.assert_(module_spec.get_module_name() not in self.cm.module_specs)
  211. self.cm.set_module_spec(module_spec)
  212. self.assert_(module_spec.get_module_name() in self.cm.module_specs)
  213. statistics_spec = self.cm.get_statistics_spec()
  214. self.assertEqual(statistics_spec['Spec2'], module_spec.get_statistics_spec())
  215. statistics_spec = self.cm.get_statistics_spec('Spec2')
  216. self.assertEqual(statistics_spec['Spec2'], module_spec.get_statistics_spec())
  217. def test_read_config(self):
  218. self.assertEqual(self.cm.config.data, {'version': config_data.BIND10_CONFIG_DATA_VERSION})
  219. self.cm.read_config()
  220. # due to what get written, the value here is what the last set_config command in test_handle_msg does
  221. self.assertEqual(self.cm.config.data, {'TestModule': {'test': 125}, 'version': config_data.BIND10_CONFIG_DATA_VERSION})
  222. self.cm.data_path = "/no_such_path"
  223. self.cm.read_config()
  224. self.assertEqual(self.cm.config.data, {'version': config_data.BIND10_CONFIG_DATA_VERSION})
  225. def test_write_config(self):
  226. # tested in ConfigManagerData tests
  227. pass
  228. def _handle_msg_helper(self, msg, expected_answer):
  229. answer = self.cm.handle_msg(msg)
  230. self.assertEqual(expected_answer, answer)
  231. def test_handle_msg(self):
  232. self._handle_msg_helper({}, { 'result': [ 1, 'Unknown message format: {}']})
  233. self._handle_msg_helper("", { 'result': [ 1, 'Unknown message format: ']})
  234. self._handle_msg_helper({ "command": [ "badcommand" ] }, { 'result': [ 1, "Unknown command: badcommand"]})
  235. self._handle_msg_helper({ "command": [ "get_commands_spec" ] }, { 'result': [ 0, {} ]})
  236. self._handle_msg_helper({ "command": [ "get_statistics_spec" ] }, { 'result': [ 0, {} ]})
  237. self._handle_msg_helper({ "command": [ "get_module_spec" ] }, { 'result': [ 0, {} ]})
  238. self._handle_msg_helper({ "command": [ "get_module_spec", { "module_name": "Spec2" } ] }, { 'result': [ 0, {} ]})
  239. #self._handle_msg_helper({ "command": [ "get_module_spec", { "module_name": "nosuchmodule" } ] },
  240. # {'result': [1, 'No specification for module nosuchmodule']})
  241. self._handle_msg_helper({ "command": [ "get_module_spec", 1 ] },
  242. {'result': [1, 'Bad get_module_spec command, argument not a dict']})
  243. self._handle_msg_helper({ "command": [ "get_module_spec", { } ] },
  244. {'result': [1, 'Bad module_name in get_module_spec command']})
  245. self._handle_msg_helper({ "command": [ "get_config" ] }, { 'result': [ 0, { 'version': config_data.BIND10_CONFIG_DATA_VERSION } ]})
  246. self._handle_msg_helper({ "command": [ "get_config", { "module_name": "nosuchmodule" } ] },
  247. {'result': [0, { 'version': config_data.BIND10_CONFIG_DATA_VERSION }]})
  248. self._handle_msg_helper({ "command": [ "get_config", 1 ] },
  249. {'result': [1, 'Bad get_config command, argument not a dict']})
  250. self._handle_msg_helper({ "command": [ "get_config", { } ] },
  251. {'result': [1, 'Bad module_name in get_config command']})
  252. self._handle_msg_helper({ "command": [ "set_config" ] },
  253. {'result': [1, 'Wrong number of arguments']})
  254. self._handle_msg_helper({ "command": [ "set_config", [{}]] },
  255. {'result': [0]})
  256. self.assertEqual(len(self.fake_session.message_queue), 0)
  257. # the targets of some of these tests expect specific answers, put
  258. # those in our fake msgq first.
  259. my_ok_answer = { 'result': [ 0 ] }
  260. # Send the 'ok' that cfgmgr expects back to the fake queue first
  261. self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
  262. # then send the command
  263. self._handle_msg_helper({ "command": [ "set_config", [self.name, { "test": 123 }] ] },
  264. my_ok_answer)
  265. # The cfgmgr should have eaten the ok message, and sent out an update again
  266. self.assertEqual(len(self.fake_session.message_queue), 1)
  267. self.assertEqual({'command': [ 'config_update', {'test': 123}]},
  268. self.fake_session.get_message(self.name, None))
  269. # and the queue should now be empty again
  270. self.assertEqual(len(self.fake_session.message_queue), 0)
  271. # below are variations of the theme above
  272. self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
  273. self._handle_msg_helper({ "command": [ "set_config", [self.name, { "test": 124 }] ] },
  274. my_ok_answer)
  275. self.assertEqual(len(self.fake_session.message_queue), 1)
  276. self.assertEqual({'command': [ 'config_update', {'test': 124}]},
  277. self.fake_session.get_message(self.name, None))
  278. self.assertEqual(len(self.fake_session.message_queue), 0)
  279. # This is the last 'succes' one, the value set here is what test_read_config expects
  280. self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
  281. self._handle_msg_helper({ "command": [ "set_config", [ { self.name: { "test": 125 } }] ] },
  282. my_ok_answer )
  283. self.assertEqual(len(self.fake_session.message_queue), 1)
  284. self.assertEqual({'command': [ 'config_update', {'test': 125}]},
  285. self.fake_session.get_message(self.name, None))
  286. self.assertEqual(len(self.fake_session.message_queue), 0)
  287. my_bad_answer = { 'result': [1, "bad_answer"] }
  288. self.fake_session.group_sendmsg(my_bad_answer, "ConfigManager")
  289. self._handle_msg_helper({ "command": [ "set_config", [ self.name, { "test": 125 }] ] },
  290. my_bad_answer )
  291. self.assertEqual(len(self.fake_session.message_queue), 1)
  292. self.assertEqual({'command': [ 'config_update', {'test': 125}]},
  293. self.fake_session.get_message(self.name, None))
  294. self.assertEqual(len(self.fake_session.message_queue), 0)
  295. self.fake_session.group_sendmsg(None, 'ConfigManager')
  296. self._handle_msg_helper({ "command": [ "set_config", [ ] ] },
  297. {'result': [1, 'Wrong number of arguments']} )
  298. self._handle_msg_helper({ "command": [ "set_config", [ self.name, { "test": 125 }] ] },
  299. { 'result': [1, 'No answer message from TestModule']} )
  300. #self.assertEqual(len(self.fake_session.message_queue), 1)
  301. #self.assertEqual({'config_update': {'test': 124}},
  302. # self.fake_session.get_message(self.name, None))
  303. #self.assertEqual({'version': 1, 'TestModule': {'test': 124}}, self.cm.config.data)
  304. #
  305. self._handle_msg_helper({ "command":
  306. ["module_spec", self.spec.get_full_spec()]
  307. },
  308. {'result': [0]})
  309. self._handle_msg_helper({ "command": [ "module_spec", { 'foo': 1 } ] },
  310. {'result': [1, 'Error in data definition: no module_name in module_spec']})
  311. self._handle_msg_helper({ "command": [ "get_module_spec" ] }, { 'result': [ 0, { self.spec.get_module_name(): self.spec.get_full_spec() } ]})
  312. self._handle_msg_helper({ "command": [ "get_module_spec",
  313. { "module_name" : "Spec2" } ] },
  314. { 'result': [ 0, self.spec.get_full_spec() ] })
  315. self._handle_msg_helper({ "command": [ "get_commands_spec" ] }, { 'result': [ 0, { self.spec.get_module_name(): self.spec.get_commands_spec() } ]})
  316. self._handle_msg_helper({ "command": [ "get_statistics_spec" ] }, { 'result': [ 0, { self.spec.get_module_name(): self.spec.get_statistics_spec() } ]})
  317. # re-add this once we have new way to propagate spec changes (1 instead of the current 2 messages)
  318. #self.assertEqual(len(self.fake_session.message_queue), 2)
  319. # the name here is actually wrong (and hardcoded), but needed in the current version
  320. # TODO: fix that
  321. #self.assertEqual({'specification_update': [ self.name, self.spec ] },
  322. # self.fake_session.get_message("Cmdctl", None))
  323. #self.assertEqual({'commands_update': [ self.name, self.commands ] },
  324. # self.fake_session.get_message("Cmdctl", None))
  325. # drop the two messages for now
  326. self.assertEqual(len(self.fake_session.message_queue), 2)
  327. self.fake_session.get_message("Cmdctl", None)
  328. self.fake_session.get_message("TestModule", None)
  329. self.assertEqual(len(self.fake_session.message_queue), 0)
  330. # A stopping message should get no response, but should cause another
  331. # message to be sent, if it is a known module
  332. self._handle_msg_helper({ "command": [ "stopping",
  333. { "module_name": "Spec2"}] },
  334. None)
  335. self.assertEqual(len(self.fake_session.message_queue), 1)
  336. self.assertEqual({'command': [ 'module_specification_update',
  337. ['Spec2', None] ] },
  338. self.fake_session.get_message("Cmdctl", None))
  339. # but not if it is either unknown or not running
  340. self._handle_msg_helper({ "command":
  341. [ "stopping",
  342. { "module_name": "NoSuchModule" } ] },
  343. None)
  344. self.assertEqual(len(self.fake_session.message_queue), 0)
  345. self._handle_msg_helper({ "command":
  346. ["shutdown"]
  347. },
  348. {'result': [0]})
  349. def test_set_config_virtual(self):
  350. """Test that if the module is virtual, we don't send it over the
  351. message bus, but call the checking function.
  352. """
  353. # We run the same three times, with different return values
  354. def single_test(value, returnFunc, expectedResult):
  355. # Because closures can't assign to closed-in variables, we pass
  356. # it trough self
  357. self.called_with = None
  358. def check_test(new_data):
  359. self.called_with = new_data
  360. return returnFunc()
  361. # Register our virtual module
  362. self.cm.set_virtual_module(self.spec, check_test)
  363. # The fake session will throw now if it tries to read a response.
  364. # Handy, we don't need to find a complicated way to check for it.
  365. result = self.cm._handle_set_config_module(self.spec.
  366. get_module_name(),
  367. {'item1': value})
  368. # Check the correct result is passed and our function was called
  369. # With correct data
  370. self.assertEqual(self.called_with['item1'], value)
  371. self.assertEqual(result, {'result': expectedResult})
  372. if expectedResult[0] == 0:
  373. # Check it provided the correct notification
  374. self.assertEqual(len(self.fake_session.message_queue), 1)
  375. self.assertEqual({'command': [ 'config_update',
  376. {'item1': value}]},
  377. self.fake_session.get_message('Spec2', None))
  378. # and the queue should now be empty again
  379. self.assertEqual(len(self.fake_session.message_queue), 0)
  380. else:
  381. # It shouldn't send anything on error
  382. self.assertEqual(len(self.fake_session.message_queue), 0)
  383. # Success
  384. single_test(5, lambda: None, [0])
  385. # Graceful error
  386. single_test(6, lambda: "Just error", [1, "Just error"])
  387. # Exception from the checker
  388. def raiser():
  389. raise Exception("Just exception")
  390. single_test(7, raiser, [1, "Exception: Just exception"])
  391. def test_set_config_all(self):
  392. my_ok_answer = { 'result': [ 0 ] }
  393. self.assertEqual({"version": 2}, self.cm.config.data)
  394. self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
  395. self.cm._handle_set_config_all({"test": { "value1": 123 }})
  396. self.assertEqual({"version": config_data.BIND10_CONFIG_DATA_VERSION,
  397. "test": { "value1": 123 }
  398. }, self.cm.config.data)
  399. self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
  400. self.cm._handle_set_config_all({"test": { "value1": 124 }})
  401. self.assertEqual({"version": config_data.BIND10_CONFIG_DATA_VERSION,
  402. "test": { "value1": 124 }
  403. }, self.cm.config.data)
  404. self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
  405. self.cm._handle_set_config_all({"test": { "value2": True }})
  406. self.assertEqual({"version": config_data.BIND10_CONFIG_DATA_VERSION,
  407. "test": { "value1": 124,
  408. "value2": True
  409. }
  410. }, self.cm.config.data)
  411. self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
  412. self.cm._handle_set_config_all({"test": { "value3": [ 1, 2, 3 ] }})
  413. self.assertEqual({"version": config_data.BIND10_CONFIG_DATA_VERSION,
  414. "test": { "value1": 124,
  415. "value2": True,
  416. "value3": [ 1, 2, 3 ]
  417. }
  418. }, self.cm.config.data)
  419. self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
  420. self.cm._handle_set_config_all({"test": { "value2": False }})
  421. self.assertEqual({"version": config_data.BIND10_CONFIG_DATA_VERSION,
  422. "test": { "value1": 124,
  423. "value2": False,
  424. "value3": [ 1, 2, 3 ]
  425. }
  426. }, self.cm.config.data)
  427. self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
  428. self.cm._handle_set_config_all({"test": { "value1": None }})
  429. self.assertEqual({"version": config_data.BIND10_CONFIG_DATA_VERSION,
  430. "test": { "value2": False,
  431. "value3": [ 1, 2, 3 ]
  432. }
  433. }, self.cm.config.data)
  434. self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
  435. self.cm._handle_set_config_all({"test": { "value3": [ 1 ] }})
  436. self.assertEqual({"version": config_data.BIND10_CONFIG_DATA_VERSION,
  437. "test": { "value2": False,
  438. "value3": [ 1 ]
  439. }
  440. }, self.cm.config.data)
  441. def test_run(self):
  442. self.fake_session.group_sendmsg({ "command": [ "get_commands_spec" ] }, "ConfigManager")
  443. self.fake_session.group_sendmsg({ "command": [ "get_statistics_spec" ] }, "ConfigManager")
  444. self.fake_session.group_sendmsg({ "command": [ "stopping", { "module_name": "FooModule" } ] }, "ConfigManager")
  445. self.fake_session.group_sendmsg({ "command": [ "shutdown" ] }, "ConfigManager")
  446. self.assertEqual(len(self.fake_session.message_queue), 4)
  447. self.cm.run()
  448. # All commands should have been read out by run()
  449. # Three of the commands should have been responded to, so the queue
  450. # should now contain three answers
  451. self.assertEqual(len(self.fake_session.message_queue), 3)
  452. if __name__ == '__main__':
  453. if not 'CONFIG_TESTDATA_PATH' in os.environ or not 'CONFIG_WR_TESTDATA_PATH' in os.environ:
  454. print("You need to set the environment variable CONFIG_TESTDATA_PATH and CONFIG_WR_TESTDATA_PATH to point to the directory containing the test data files")
  455. exit(1)
  456. unittest.main()