component_test.py 40 KB

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