component_test.py 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. # Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  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 isc.bind10.component module and the
  17. isc.bind10.special_component module.
  18. """
  19. import unittest
  20. import isc.log
  21. import time
  22. import copy
  23. from isc.bind10.component import Component, Configurator, BaseComponent
  24. import isc.bind10.special_component
  25. class TestError(Exception):
  26. """
  27. Just a private exception not known to anybody we use for our tests.
  28. """
  29. pass
  30. class BossUtils:
  31. """
  32. A class that brings some utilities for pretending we're Boss.
  33. This is expected to be inherited by the testcases themselves.
  34. """
  35. def setUp(self):
  36. """
  37. Part of setup. Should be called by descendant's setUp.
  38. """
  39. self._shutdown = False
  40. self._exitcode = None
  41. # Back up the time function, we may want to replace it with something
  42. self.__orig_time = isc.bind10.component.time.time
  43. def tearDown(self):
  44. """
  45. Clean up after tests. If the descendant implements a tearDown, it
  46. should call this method internally.
  47. """
  48. # Return the original time function
  49. isc.bind10.component.time.time = self.__orig_time
  50. def component_shutdown(self, exitcode=0):
  51. """
  52. Mock function to shut down. We just note we were asked to do so.
  53. """
  54. self._shutdown = True
  55. self._exitcode = exitcode
  56. def _timeskip(self):
  57. """
  58. Skip in time to future some 30s. Implemented by replacing the
  59. time.time function in the tested module with function that returns
  60. current time increased by 30.
  61. """
  62. tm = time.time()
  63. isc.bind10.component.time.time = lambda: tm + 30
  64. # Few functions that pretend to start something. Part of pretending of
  65. # being boss.
  66. def start_msgq(self):
  67. pass
  68. def start_cfgmgr(self):
  69. pass
  70. def start_auth(self):
  71. pass
  72. def start_resolver(self):
  73. pass
  74. def start_cmdctl(self):
  75. pass
  76. def start_xfrin(self):
  77. pass
  78. class ComponentTests(BossUtils, unittest.TestCase):
  79. """
  80. Tests for the bind10.component.Component class
  81. """
  82. def setUp(self):
  83. """
  84. Pretend a newly started system.
  85. """
  86. BossUtils.setUp(self)
  87. self._shutdown = False
  88. self._exitcode = None
  89. self.__start_called = False
  90. self.__stop_called = False
  91. self.__failed_called = False
  92. self.__registered_processes = {}
  93. self.__stop_process_params = None
  94. self.__start_simple_params = None
  95. # Pretending to be boss
  96. self.uid = None
  97. self.__uid_set = None
  98. def __start(self):
  99. """
  100. Mock function, installed into the component into _start_internal.
  101. This only notes the component was "started".
  102. """
  103. self.__start_called = True
  104. def __stop(self):
  105. """
  106. Mock function, installed into the component into _stop_internal.
  107. This only notes the component was "stopped".
  108. """
  109. self.__stop_called = True
  110. def __fail(self):
  111. """
  112. Mock function, installed into the component into _failed_internal.
  113. This only notes the component called the method.
  114. """
  115. self.__failed_called = True
  116. def __fail_to_start(self):
  117. """
  118. Mock function. It can be installed into the component's _start_internal
  119. to simulate a component that fails to start by raising an exception.
  120. """
  121. orig_started = self.__start_called
  122. self.__start_called = True
  123. if not orig_started:
  124. # This one is from restart. Avoid infinite recursion for now.
  125. # FIXME: We should use the restart scheduler to avoid it, not this.
  126. raise TestError("Test error")
  127. def __create_component(self, kind):
  128. """
  129. Convenience function that creates a component of given kind
  130. and installs the mock functions into it so we can hook up into
  131. its behaviour.
  132. The process used is some nonsense, as this isn't used in this
  133. kind of tests and we pretend to be the boss.
  134. """
  135. component = Component('No process', self, kind, 'homeless', [])
  136. component._start_internal = self.__start
  137. component._stop_internal = self.__stop
  138. component._failed_internal = self.__fail
  139. return component
  140. def test_name(self):
  141. """
  142. Test the name provides whatever we passed to the constructor as process.
  143. """
  144. component = self.__create_component('core')
  145. self.assertEqual('No process', component.name())
  146. def test_guts(self):
  147. """
  148. Test the correct data are stored inside the component.
  149. """
  150. component = self.__create_component('core')
  151. self.assertEqual(self, component._boss)
  152. self.assertEqual("No process", component._process)
  153. self.assertEqual(None, component._start_func)
  154. self.assertEqual("homeless", component._address)
  155. self.assertEqual([], component._params)
  156. def __check_startup(self, component):
  157. """
  158. Check that nothing was called yet. A newly created component should
  159. not get started right away, so this should pass after the creation.
  160. """
  161. self.assertFalse(self._shutdown)
  162. self.assertFalse(self.__start_called)
  163. self.assertFalse(self.__stop_called)
  164. self.assertFalse(self.__failed_called)
  165. self.assertFalse(component.running())
  166. # We can't stop or fail the component yet
  167. self.assertRaises(ValueError, component.stop)
  168. self.assertRaises(ValueError, component.failed, 1)
  169. def __check_started(self, component):
  170. """
  171. Check the component was started, but not stopped anyhow yet.
  172. """
  173. self.assertFalse(self._shutdown)
  174. self.assertTrue(self.__start_called)
  175. self.assertFalse(self.__stop_called)
  176. self.assertFalse(self.__failed_called)
  177. self.assertTrue(component.running())
  178. def __check_dead(self, component):
  179. """
  180. Check the component is completely dead, and the server too.
  181. """
  182. self.assertTrue(self._shutdown)
  183. self.assertTrue(self.__start_called)
  184. self.assertFalse(self.__stop_called)
  185. self.assertTrue(self.__failed_called)
  186. self.assertEqual(1, self._exitcode)
  187. self.assertFalse(component.running())
  188. # Surely it can't be stopped when already dead
  189. self.assertRaises(ValueError, component.stop)
  190. # Nor started
  191. self.assertRaises(ValueError, component.start)
  192. # Nor it can fail again
  193. self.assertRaises(ValueError, component.failed, 1)
  194. def __check_restarted(self, component):
  195. """
  196. Check the component restarted successfully.
  197. Reset the self.__start_called to False before calling the function when
  198. the component should fail.
  199. """
  200. self.assertFalse(self._shutdown)
  201. self.assertTrue(self.__start_called)
  202. self.assertFalse(self.__stop_called)
  203. self.assertTrue(self.__failed_called)
  204. self.assertTrue(component.running())
  205. # Check it can't be started again
  206. self.assertRaises(ValueError, component.start)
  207. def __check_not_restarted(self, component):
  208. """
  209. Check the component has not (yet) restarted successfully.
  210. """
  211. self.assertFalse(self._shutdown)
  212. self.assertTrue(self.__start_called)
  213. self.assertFalse(self.__stop_called)
  214. self.assertTrue(self.__failed_called)
  215. self.assertFalse(component.running())
  216. def __do_start_stop(self, kind):
  217. """
  218. This is a body of a test. It creates a component of given kind,
  219. then starts it and stops it. It checks correct functions are called
  220. and the component's status is correct.
  221. It also checks the component can't be started/stopped twice.
  222. """
  223. # Create it and check it did not do any funny stuff yet
  224. component = self.__create_component(kind)
  225. self.__check_startup(component)
  226. # Start it and check it called the correct starting functions
  227. component.start()
  228. self.__check_started(component)
  229. # Check it can't be started twice
  230. self.assertRaises(ValueError, component.start)
  231. # Stop it again and check
  232. component.stop()
  233. self.assertFalse(self._shutdown)
  234. self.assertTrue(self.__start_called)
  235. self.assertTrue(self.__stop_called)
  236. self.assertFalse(self.__failed_called)
  237. self.assertFalse(component.running())
  238. # Check it can't be stopped twice
  239. self.assertRaises(ValueError, component.stop)
  240. # Or failed
  241. self.assertRaises(ValueError, component.failed, 1)
  242. # But it can be started again if it is stopped
  243. # (no more checking here, just it doesn't crash)
  244. component.start()
  245. def test_start_stop_core(self):
  246. """
  247. A start-stop test for core component. See do_start_stop.
  248. """
  249. self.__do_start_stop('core')
  250. def test_start_stop_needed(self):
  251. """
  252. A start-stop test for needed component. See do_start_stop.
  253. """
  254. self.__do_start_stop('needed')
  255. def test_start_stop_dispensable(self):
  256. """
  257. A start-stop test for dispensable component. See do_start_stop.
  258. """
  259. self.__do_start_stop('dispensable')
  260. def test_start_fail_core(self):
  261. """
  262. Start and then fail a core component. It should stop the whole server.
  263. """
  264. # Just ordinary startup
  265. component = self.__create_component('core')
  266. self.__check_startup(component)
  267. component.start()
  268. self.__check_started(component)
  269. # Pretend the component died
  270. restarted = component.failed(1)
  271. # Since it is a core component, it should not be restarted
  272. self.assertFalse(restarted)
  273. # It should bring down the whole server
  274. self.__check_dead(component)
  275. def test_start_fail_core_later(self):
  276. """
  277. Start and then fail a core component, but let it be running for longer time.
  278. It should still stop the whole server.
  279. """
  280. # Just ordinary startup
  281. component = self.__create_component('core')
  282. self.__check_startup(component)
  283. component.start()
  284. self.__check_started(component)
  285. self._timeskip()
  286. # Pretend the component died some time later
  287. restarted = component.failed(1)
  288. # Should not be restarted
  289. self.assertFalse(restarted)
  290. # Check the component is still dead
  291. self.__check_dead(component)
  292. def test_start_fail_needed(self):
  293. """
  294. Start and then fail a needed component. As this happens really soon after
  295. being started, it is considered failure to start and should bring down the
  296. whole server.
  297. """
  298. # Just ordinary startup
  299. component = self.__create_component('needed')
  300. self.__check_startup(component)
  301. component.start()
  302. self.__check_started(component)
  303. # Make it fail right away.
  304. restarted = component.failed(1)
  305. # Should not have restarted
  306. self.assertFalse(restarted)
  307. self.__check_dead(component)
  308. def test_start_fail_needed_later(self):
  309. """
  310. Start and then fail a needed component. But the failure is later on, so
  311. we just restart it and will be happy.
  312. """
  313. # Just ordinary startup
  314. component = self.__create_component('needed')
  315. self.__check_startup(component)
  316. component.start()
  317. self.__check_started(component)
  318. # Make it fail later on
  319. self.__start_called = False
  320. self._timeskip()
  321. restarted = component.failed(1)
  322. # Should have restarted
  323. self.assertTrue(restarted)
  324. self.__check_restarted(component)
  325. def test_start_fail_dispensable(self):
  326. """
  327. Start and then fail a dispensable component. Should not get restarted.
  328. """
  329. # Just ordinary startup
  330. component = self.__create_component('dispensable')
  331. self.__check_startup(component)
  332. component.start()
  333. self.__check_started(component)
  334. # Make it fail right away
  335. restarted = component.failed(1)
  336. # Should signal that it did not restart
  337. self.assertFalse(restarted)
  338. self.__check_not_restarted(component)
  339. def test_start_fail_dispensable_later(self):
  340. """
  341. Start and then later on fail a dispensable component. Should just get
  342. restarted.
  343. """
  344. # Just ordinary startup
  345. component = self.__create_component('dispensable')
  346. self.__check_startup(component)
  347. component.start()
  348. self.__check_started(component)
  349. # Make it fail later on
  350. self._timeskip()
  351. restarted = component.failed(1)
  352. # should signal that it restarted
  353. self.assertTrue(restarted)
  354. # and check if it really did
  355. self.__check_restarted(component)
  356. def test_start_fail_dispensable_restart_later(self):
  357. """
  358. Start and then fail a dispensable component, wait a bit and try to
  359. restart. Should get restarted after the wait.
  360. """
  361. # Just ordinary startup
  362. component = self.__create_component('dispensable')
  363. self.__check_startup(component)
  364. component.start()
  365. self.__check_started(component)
  366. # Make it fail immediately
  367. restarted = component.failed(1)
  368. # should signal that it did not restart
  369. self.assertFalse(restarted)
  370. self.__check_not_restarted(component)
  371. self._timeskip()
  372. # try to restart again
  373. restarted = component.restart()
  374. # should signal that it restarted
  375. self.assertTrue(restarted)
  376. # and check if it really did
  377. self.__check_restarted(component)
  378. def test_fail_core(self):
  379. """
  380. Failure to start a core component. Should bring the system down
  381. and the exception should get through.
  382. """
  383. component = self.__create_component('core')
  384. self.__check_startup(component)
  385. component._start_internal = self.__fail_to_start
  386. self.assertRaises(TestError, component.start)
  387. self.__check_dead(component)
  388. def test_fail_needed(self):
  389. """
  390. Failure to start a needed component. Should bring the system down
  391. and the exception should get through.
  392. """
  393. component = self.__create_component('needed')
  394. self.__check_startup(component)
  395. component._start_internal = self.__fail_to_start
  396. self.assertRaises(TestError, component.start)
  397. self.__check_dead(component)
  398. def test_fail_dispensable(self):
  399. """
  400. Failure to start a dispensable component. The exception should get
  401. through, but it should be restarted after a time skip.
  402. """
  403. component = self.__create_component('dispensable')
  404. self.__check_startup(component)
  405. component._start_internal = self.__fail_to_start
  406. self.assertRaises(TestError, component.start)
  407. # tell it to see if it must restart
  408. restarted = component.restart()
  409. # should not have restarted yet
  410. self.assertFalse(restarted)
  411. self.__check_not_restarted(component)
  412. self._timeskip()
  413. # tell it to see if it must restart and do so, with our vision of time
  414. restarted = component.restart()
  415. # should have restarted now
  416. self.assertTrue(restarted)
  417. self.__check_restarted(component)
  418. def test_component_start_time(self):
  419. """
  420. Check that original start time is set initially, and remains the same
  421. after a restart, while the internal __start_time does change
  422. """
  423. # Just ordinary startup
  424. component = self.__create_component('dispensable')
  425. self.__check_startup(component)
  426. self.assertIsNone(component._original_start_time)
  427. component.start()
  428. self.__check_started(component)
  429. self.assertIsNotNone(component._original_start_time)
  430. self.assertIsNotNone(component._BaseComponent__start_time)
  431. original_start_time = component._original_start_time
  432. start_time = component._BaseComponent__start_time
  433. # Not restarted yet, so they should be the same
  434. self.assertEqual(original_start_time, start_time)
  435. self._timeskip()
  436. # Make it fail
  437. restarted = component.failed(1)
  438. # should signal that it restarted
  439. self.assertTrue(restarted)
  440. # and check if it really did
  441. self.__check_restarted(component)
  442. # original start time should not have changed
  443. self.assertEqual(original_start_time, component._original_start_time)
  444. # but actual start time should
  445. self.assertNotEqual(start_time, component._BaseComponent__start_time)
  446. def test_bad_kind(self):
  447. """
  448. Test the component rejects nonsensical kinds. This includes bad
  449. capitalization.
  450. """
  451. for kind in ['Core', 'CORE', 'nonsense', 'need ed', 'required']:
  452. self.assertRaises(ValueError, Component, 'No process', self, kind)
  453. def test_pid_not_running(self):
  454. """
  455. Test that a componet that is not yet started doesn't have a PID.
  456. But it won't fail if asked for and return None.
  457. """
  458. for component_type in [Component,
  459. isc.bind10.special_component.SockCreator,
  460. isc.bind10.special_component.Msgq,
  461. isc.bind10.special_component.CfgMgr,
  462. isc.bind10.special_component.Auth,
  463. isc.bind10.special_component.Resolver,
  464. isc.bind10.special_component.CmdCtl,
  465. isc.bind10.special_component.XfrIn,
  466. isc.bind10.special_component.SetUID]:
  467. component = component_type('none', self, 'needed')
  468. self.assertIsNone(component.pid())
  469. def test_kill_unstarted(self):
  470. """
  471. Try to kill the component if it's not started. Should not fail.
  472. We do not try to kill a running component, as we should not start
  473. it during unit tests.
  474. """
  475. component = Component('component', self, 'needed')
  476. component.kill()
  477. component.kill(True)
  478. def register_process(self, pid, process):
  479. """
  480. Part of pretending to be a boss
  481. """
  482. self.__registered_processes[pid] = process
  483. def test_component_attributes(self):
  484. """
  485. Test the default attributes of Component (not BaseComponent) and
  486. some of the methods we might be allowed to call.
  487. """
  488. class TestProcInfo:
  489. def __init__(self):
  490. self.pid = 42
  491. component = Component('component', self, 'needed', 'Address',
  492. ['hello'], TestProcInfo)
  493. self.assertEqual('component', component._process)
  494. self.assertEqual('component', component.name())
  495. self.assertIsNone(component._procinfo)
  496. self.assertIsNone(component.pid())
  497. self.assertEqual(['hello'], component._params)
  498. self.assertEqual('Address', component._address)
  499. self.assertFalse(component.running())
  500. self.assertEqual({}, self.__registered_processes)
  501. component.start()
  502. self.assertTrue(component.running())
  503. # Some versions of unittest miss assertIsInstance
  504. self.assertTrue(isinstance(component._procinfo, TestProcInfo))
  505. self.assertEqual(42, component.pid())
  506. self.assertEqual(component, self.__registered_processes.get(42))
  507. def stop_process(self, process, address):
  508. """
  509. Part of pretending to be boss.
  510. """
  511. self.__stop_process_params = (process, address)
  512. def start_simple(self, process):
  513. """
  514. Part of pretending to be boss.
  515. """
  516. self.__start_simple_params = process
  517. def test_component_start_stop_internal(self):
  518. """
  519. Test the behavior of _stop_internal and _start_internal.
  520. """
  521. component = Component('component', self, 'needed', 'Address')
  522. component.start()
  523. self.assertTrue(component.running())
  524. self.assertEqual('component', self.__start_simple_params)
  525. component.stop()
  526. self.assertFalse(component.running())
  527. self.assertEqual(('component', 'Address'), self.__stop_process_params)
  528. def test_component_kill(self):
  529. """
  530. Check the kill is propagated. The case when component wasn't started
  531. yet is already tested elsewhere.
  532. """
  533. class Process:
  534. def __init__(self):
  535. self.killed = False
  536. self.terminated = False
  537. def kill(self):
  538. self.killed = True
  539. def terminate(self):
  540. self.terminated = True
  541. process = Process()
  542. class ProcInfo:
  543. def __init__(self):
  544. self.process = process
  545. self.pid = 42
  546. component = Component('component', self, 'needed', 'Address',
  547. [], ProcInfo)
  548. component.start()
  549. self.assertTrue(component.running())
  550. component.kill()
  551. self.assertTrue(process.terminated)
  552. self.assertFalse(process.killed)
  553. process.terminated = False
  554. component.kill(True)
  555. self.assertTrue(process.killed)
  556. self.assertFalse(process.terminated)
  557. def setuid(self, uid):
  558. self.__uid_set = uid
  559. def test_setuid(self):
  560. """
  561. Some tests around the SetUID pseudo-component.
  562. """
  563. component = isc.bind10.special_component.SetUID(None, self, 'needed',
  564. None)
  565. orig_setuid = isc.bind10.special_component.posix.setuid
  566. isc.bind10.special_component.posix.setuid = self.setuid
  567. component.start()
  568. # No uid set in boss, nothing called.
  569. self.assertIsNone(self.__uid_set)
  570. # Doesn't do anything, but doesn't crash
  571. component.stop()
  572. component.kill()
  573. component.kill(True)
  574. self.uid = 42
  575. component = isc.bind10.special_component.SetUID(None, self, 'needed',
  576. None)
  577. component.start()
  578. # This time, it get's called
  579. self.assertEqual(42, self.__uid_set)
  580. class TestComponent(BaseComponent):
  581. """
  582. A test component. It does not start any processes or so, it just logs
  583. information about what happens.
  584. """
  585. def __init__(self, owner, name, kind, address=None, params=None):
  586. """
  587. Initializes the component. The owner is the test that started the
  588. component. The logging will happen into it.
  589. The process is used as a name for the logging.
  590. """
  591. BaseComponent.__init__(self, owner, kind)
  592. self.__owner = owner
  593. self.__name = name
  594. self.log('init')
  595. self.log(kind)
  596. self._address = address
  597. self._params = params
  598. def log(self, event):
  599. """
  600. Log an event into the owner. The owner can then check the correct
  601. order of events that happened.
  602. """
  603. self.__owner.log.append((self.__name, event))
  604. def _start_internal(self):
  605. self.log('start')
  606. def _stop_internal(self):
  607. self.log('stop')
  608. def _failed_internal(self):
  609. self.log('failed')
  610. def kill(self, forcefull=False):
  611. self.log('killed')
  612. class FailComponent(BaseComponent):
  613. """
  614. A mock component that fails whenever it is started.
  615. """
  616. def __init__(self, name, boss, kind, address=None, params=None):
  617. BaseComponent.__init__(self, boss, kind)
  618. def _start_internal(self):
  619. raise TestError("test error")
  620. class ConfiguratorTest(BossUtils, unittest.TestCase):
  621. """
  622. Tests for the configurator.
  623. """
  624. def setUp(self):
  625. """
  626. Prepare some test data for the tests.
  627. """
  628. BossUtils.setUp(self)
  629. self.log = []
  630. # The core "hardcoded" configuration
  631. self.__core = {
  632. 'core1': {
  633. 'priority': 5,
  634. 'process': 'core1',
  635. 'special': 'test',
  636. 'kind': 'core'
  637. },
  638. 'core2': {
  639. 'process': 'core2',
  640. 'special': 'test',
  641. 'kind': 'core'
  642. },
  643. 'core3': {
  644. 'process': 'core3',
  645. 'priority': 3,
  646. 'special': 'test',
  647. 'kind': 'core'
  648. }
  649. }
  650. # How they should be started. They are created in the order they are
  651. # found in the dict, but then they should be started by priority.
  652. # This expects that the same dict returns its keys in the same order
  653. # every time
  654. self.__core_log_create = []
  655. for core in self.__core.keys():
  656. self.__core_log_create.append((core, 'init'))
  657. self.__core_log_create.append((core, 'core'))
  658. self.__core_log_start = [('core1', 'start'), ('core3', 'start'),
  659. ('core2', 'start')]
  660. self.__core_log = self.__core_log_create + self.__core_log_start
  661. self.__specials = { 'test': self.__component_test }
  662. def __component_test(self, process, boss, kind, address=None, params=None):
  663. """
  664. Create a test component. It will log events to us.
  665. """
  666. self.assertEqual(self, boss)
  667. return TestComponent(self, process, kind, address, params)
  668. def test_init(self):
  669. """
  670. Tests the configurator can be created and it does not create
  671. any components yet, nor does it remember anything.
  672. """
  673. configurator = Configurator(self, self.__specials)
  674. self.assertEqual([], self.log)
  675. self.assertEqual({}, configurator._components)
  676. self.assertFalse(configurator.running())
  677. def test_run_plan(self):
  678. """
  679. Test the internal function of running plans. Just see it can handle
  680. the commands in the given order. We see that by the log.
  681. Also includes one that raises, so we see it just stops there.
  682. """
  683. # Prepare the configurator and the plan
  684. configurator = Configurator(self, self.__specials)
  685. started = self.__component_test('second', self, 'dispensable')
  686. started.start()
  687. stopped = self.__component_test('first', self, 'core')
  688. configurator._components = {'second': started}
  689. plan = [
  690. {
  691. 'component': stopped,
  692. 'command': 'start',
  693. 'name': 'first',
  694. 'config': {'a': 1}
  695. },
  696. {
  697. 'component': started,
  698. 'command': 'stop',
  699. 'name': 'second',
  700. 'config': {}
  701. },
  702. {
  703. 'component': FailComponent('third', self, 'needed'),
  704. 'command': 'start',
  705. 'name': 'third',
  706. 'config': {}
  707. },
  708. {
  709. 'component': self.__component_test('fourth', self, 'core'),
  710. 'command': 'start',
  711. 'name': 'fourth',
  712. 'config': {}
  713. }
  714. ]
  715. # Don't include the preparation into the log
  716. self.log = []
  717. # The error from the third component is propagated
  718. self.assertRaises(TestError, configurator._run_plan, plan)
  719. # The first two were handled, the rest not, due to the exception
  720. self.assertEqual([('first', 'start'), ('second', 'stop')], self.log)
  721. self.assertEqual({'first': ({'a': 1}, stopped)},
  722. configurator._components)
  723. def __build_components(self, config):
  724. """
  725. Insert the components into the configuration to specify possible
  726. Configurator._components.
  727. Actually, the components are None, but we need something to be there.
  728. """
  729. result = {}
  730. for name in config.keys():
  731. result[name] = (config[name], None)
  732. return result
  733. def test_build_plan(self):
  734. """
  735. Test building the plan correctly. Not complete yet, this grows as we
  736. add more ways of changing the plan.
  737. """
  738. configurator = Configurator(self, self.__specials)
  739. plan = configurator._build_plan({}, self.__core)
  740. # This should have created the components
  741. self.assertEqual(self.__core_log_create, self.log)
  742. self.assertEqual(3, len(plan))
  743. for (task, name) in zip(plan, ['core1', 'core3', 'core2']):
  744. self.assertTrue('component' in task)
  745. self.assertEqual('start', task['command'])
  746. self.assertEqual(name, task['name'])
  747. component = task['component']
  748. self.assertIsNone(component._address)
  749. self.assertIsNone(component._params)
  750. # A plan to go from older state to newer one containing more components
  751. bigger = copy.copy(self.__core)
  752. bigger['additional'] = {
  753. 'priority': 6,
  754. 'special': 'test',
  755. 'process': 'additional',
  756. 'kind': 'needed'
  757. }
  758. self.log = []
  759. plan = configurator._build_plan(self.__build_components(self.__core),
  760. bigger)
  761. self.assertEqual([('additional', 'init'), ('additional', 'needed')],
  762. self.log)
  763. self.assertEqual(1, len(plan))
  764. self.assertTrue('component' in plan[0])
  765. component = plan[0]['component']
  766. self.assertEqual('start', plan[0]['command'])
  767. self.assertEqual('additional', plan[0]['name'])
  768. # Now remove the one component again
  769. # We run the plan so the component is wired into internal structures
  770. configurator._run_plan(plan)
  771. self.log = []
  772. plan = configurator._build_plan(self.__build_components(bigger),
  773. self.__core)
  774. self.assertEqual([], self.log)
  775. self.assertEqual([{
  776. 'command': 'stop',
  777. 'name': 'additional',
  778. 'component': component
  779. }], plan)
  780. # We want to switch a component. So, prepare the configurator so it
  781. # holds one
  782. configurator._run_plan(configurator._build_plan(
  783. self.__build_components(self.__core), bigger))
  784. # Get a different configuration with a different component
  785. different = copy.copy(self.__core)
  786. different['another'] = {
  787. 'special': 'test',
  788. 'process': 'another',
  789. 'kind': 'dispensable'
  790. }
  791. self.log = []
  792. plan = configurator._build_plan(self.__build_components(bigger),
  793. different)
  794. self.assertEqual([('another', 'init'), ('another', 'dispensable')],
  795. self.log)
  796. self.assertEqual(2, len(plan))
  797. self.assertEqual('stop', plan[0]['command'])
  798. self.assertEqual('additional', plan[0]['name'])
  799. self.assertTrue('component' in plan[0])
  800. self.assertEqual('start', plan[1]['command'])
  801. self.assertEqual('another', plan[1]['name'])
  802. self.assertTrue('component' in plan[1])
  803. # Some slightly insane plans, like missing process, having parameters,
  804. # no special, etc
  805. plan = configurator._build_plan({}, {
  806. 'component': {
  807. 'kind': 'needed',
  808. 'params': ["1", "2"],
  809. 'address': 'address'
  810. }
  811. })
  812. self.assertEqual(1, len(plan))
  813. self.assertEqual('start', plan[0]['command'])
  814. self.assertEqual('component', plan[0]['name'])
  815. component = plan[0]['component']
  816. self.assertEqual('component', component.name())
  817. self.assertEqual(["1", "2"], component._params)
  818. self.assertEqual('address', component._address)
  819. self.assertEqual('needed', component._kind)
  820. # We don't use isinstance on purpose, it would allow a descendant
  821. self.assertTrue(type(component) is Component)
  822. plan = configurator._build_plan({}, {
  823. 'component': { 'kind': 'dispensable' }
  824. })
  825. self.assertEqual(1, len(plan))
  826. self.assertEqual('start', plan[0]['command'])
  827. self.assertEqual('component', plan[0]['name'])
  828. component = plan[0]['component']
  829. self.assertEqual('component', component.name())
  830. self.assertIsNone(component._params)
  831. self.assertIsNone(component._address)
  832. self.assertEqual('dispensable', component._kind)
  833. def __do_switch(self, option, value):
  834. """
  835. Start it with some component and then switch the configuration of the
  836. component. This will probably raise, as it is not yet supported.
  837. """
  838. configurator = Configurator(self, self.__specials)
  839. compconfig = {
  840. 'special': 'test',
  841. 'process': 'process',
  842. 'priority': 13,
  843. 'kind': 'core'
  844. }
  845. modifiedconfig = copy.copy(compconfig)
  846. modifiedconfig[option] = value
  847. return configurator._build_plan({'comp': (compconfig, None)},
  848. {'comp': modifiedconfig})
  849. def test_change_config_plan(self):
  850. """
  851. Test changing a configuration of one component. This is not yet
  852. implemented and should therefore throw.
  853. """
  854. self.assertRaises(NotImplementedError, self.__do_switch, 'kind',
  855. 'dispensable')
  856. self.assertRaises(NotImplementedError, self.__do_switch, 'special',
  857. 'not_a_test')
  858. self.assertRaises(NotImplementedError, self.__do_switch, 'process',
  859. 'different')
  860. self.assertRaises(NotImplementedError, self.__do_switch, 'address',
  861. 'different')
  862. self.assertRaises(NotImplementedError, self.__do_switch, 'params',
  863. ['different'])
  864. # This does not change anything on running component, so no need to
  865. # raise
  866. self.assertEqual([], self.__do_switch('priority', 5))
  867. # Check against false positive, if the data are the same, but different
  868. # instance
  869. self.assertEqual([], self.__do_switch('special', 'test'))
  870. def __check_shutdown_log(self):
  871. """
  872. Checks the log for shutting down from the core configuration.
  873. """
  874. # We know everything must be stopped, we know what it is.
  875. # But we don't know the order, so we check everything is exactly
  876. # once in the log
  877. components = set(self.__core.keys())
  878. for (name, command) in self.log:
  879. self.assertEqual('stop', command)
  880. self.assertTrue(name in components)
  881. components.remove(name)
  882. self.assertEqual(set([]), components, "Some component wasn't stopped")
  883. def test_run(self):
  884. """
  885. Passes some configuration to the startup method and sees if
  886. the components are started up. Then it reconfigures it with
  887. empty configuration, the original configuration again and shuts
  888. down.
  889. It also checks the components are kept inside the configurator.
  890. """
  891. configurator = Configurator(self, self.__specials)
  892. # Can't reconfigure nor stop yet
  893. self.assertRaises(ValueError, configurator.reconfigure, self.__core)
  894. self.assertRaises(ValueError, configurator.shutdown)
  895. self.assertFalse(configurator.running())
  896. # Start it
  897. configurator.startup(self.__core)
  898. self.assertEqual(self.__core_log, self.log)
  899. for core in self.__core.keys():
  900. self.assertTrue(core in configurator._components)
  901. self.assertEqual(self.__core[core],
  902. configurator._components[core][0])
  903. self.assertEqual(set(self.__core), set(configurator._components))
  904. self.assertTrue(configurator.running())
  905. # It can't be started twice
  906. self.assertRaises(ValueError, configurator.startup, self.__core)
  907. self.log = []
  908. # Reconfigure - stop everything
  909. configurator.reconfigure({})
  910. self.assertEqual({}, configurator._components)
  911. self.assertTrue(configurator.running())
  912. self.__check_shutdown_log()
  913. # Start it again
  914. self.log = []
  915. configurator.reconfigure(self.__core)
  916. self.assertEqual(self.__core_log, self.log)
  917. for core in self.__core.keys():
  918. self.assertTrue(core in configurator._components)
  919. self.assertEqual(self.__core[core],
  920. configurator._components[core][0])
  921. self.assertEqual(set(self.__core), set(configurator._components))
  922. self.assertTrue(configurator.running())
  923. # Do a shutdown
  924. self.log = []
  925. configurator.shutdown()
  926. self.assertEqual({}, configurator._components)
  927. self.assertFalse(configurator.running())
  928. self.__check_shutdown_log()
  929. # It can't be stopped twice
  930. self.assertRaises(ValueError, configurator.shutdown)
  931. def test_sort_no_prio(self):
  932. """
  933. There was a bug if there were two things with the same priority
  934. (or without priority), it failed as it couldn't compare the dicts
  935. there. This tests it doesn't crash.
  936. """
  937. configurator = Configurator(self, self.__specials)
  938. configurator._build_plan({}, {
  939. "c1": { 'kind': 'dispensable'},
  940. "c2": { 'kind': 'dispensable'}
  941. })
  942. if __name__ == '__main__':
  943. isc.log.init("bind10") # FIXME Should this be needed?
  944. isc.log.resetUnitTestRootLogger()
  945. unittest.main()