bind10_test.py.in 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241
  1. # Copyright (C) 2011 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. # Most of the time, we omit the "bind10_src" for brevity. Sometimes,
  16. # we want to be explicit about what we do, like when hijacking a library
  17. # call used by the bind10_src.
  18. from bind10_src import ProcessInfo, BoB, parse_args, dump_pid, unlink_pid_file, _BASETIME
  19. import bind10_src
  20. # XXX: environment tests are currently disabled, due to the preprocessor
  21. # setup that we have now complicating the environment
  22. import unittest
  23. import sys
  24. import os
  25. import os.path
  26. import copy
  27. import signal
  28. import socket
  29. from isc.net.addr import IPAddr
  30. import time
  31. import isc
  32. import isc.log
  33. import isc.bind10.socket_cache
  34. import errno
  35. import random
  36. from isc.testutils.parse_args import TestOptParser, OptsError
  37. from isc.testutils.ccsession_mock import MockModuleCCSession
  38. class TestProcessInfo(unittest.TestCase):
  39. def setUp(self):
  40. # redirect stdout to a pipe so we can check that our
  41. # process spawning is doing the right thing with stdout
  42. self.old_stdout = os.dup(sys.stdout.fileno())
  43. self.pipes = os.pipe()
  44. os.dup2(self.pipes[1], sys.stdout.fileno())
  45. os.close(self.pipes[1])
  46. # note that we use dup2() to restore the original stdout
  47. # to the main program ASAP in each test... this prevents
  48. # hangs reading from the child process (as the pipe is only
  49. # open in the child), and also insures nice pretty output
  50. def tearDown(self):
  51. # clean up our stdout munging
  52. os.dup2(self.old_stdout, sys.stdout.fileno())
  53. os.close(self.pipes[0])
  54. def test_init(self):
  55. pi = ProcessInfo('Test Process', [ '/bin/echo', 'foo' ])
  56. pi.spawn()
  57. os.dup2(self.old_stdout, sys.stdout.fileno())
  58. self.assertEqual(pi.name, 'Test Process')
  59. self.assertEqual(pi.args, [ '/bin/echo', 'foo' ])
  60. # self.assertEqual(pi.env, { 'PATH': os.environ['PATH'],
  61. # 'PYTHON_EXEC': os.environ['PYTHON_EXEC'] })
  62. self.assertEqual(pi.dev_null_stdout, False)
  63. self.assertEqual(os.read(self.pipes[0], 100), b"foo\n")
  64. self.assertNotEqual(pi.process, None)
  65. self.assertTrue(type(pi.pid) is int)
  66. # def test_setting_env(self):
  67. # pi = ProcessInfo('Test Process', [ '/bin/true' ], env={'FOO': 'BAR'})
  68. # os.dup2(self.old_stdout, sys.stdout.fileno())
  69. # self.assertEqual(pi.env, { 'PATH': os.environ['PATH'],
  70. # 'PYTHON_EXEC': os.environ['PYTHON_EXEC'],
  71. # 'FOO': 'BAR' })
  72. def test_setting_null_stdout(self):
  73. pi = ProcessInfo('Test Process', [ '/bin/echo', 'foo' ],
  74. dev_null_stdout=True)
  75. pi.spawn()
  76. os.dup2(self.old_stdout, sys.stdout.fileno())
  77. self.assertEqual(pi.dev_null_stdout, True)
  78. self.assertEqual(os.read(self.pipes[0], 100), b"")
  79. def test_respawn(self):
  80. pi = ProcessInfo('Test Process', [ '/bin/echo', 'foo' ])
  81. pi.spawn()
  82. # wait for old process to work...
  83. self.assertEqual(os.read(self.pipes[0], 100), b"foo\n")
  84. # respawn it
  85. old_pid = pi.pid
  86. pi.respawn()
  87. os.dup2(self.old_stdout, sys.stdout.fileno())
  88. # make sure the new one started properly
  89. self.assertEqual(pi.name, 'Test Process')
  90. self.assertEqual(pi.args, [ '/bin/echo', 'foo' ])
  91. # self.assertEqual(pi.env, { 'PATH': os.environ['PATH'],
  92. # 'PYTHON_EXEC': os.environ['PYTHON_EXEC'] })
  93. self.assertEqual(pi.dev_null_stdout, False)
  94. self.assertEqual(os.read(self.pipes[0], 100), b"foo\n")
  95. self.assertNotEqual(pi.process, None)
  96. self.assertTrue(type(pi.pid) is int)
  97. self.assertNotEqual(pi.pid, old_pid)
  98. class TestCacheCommands(unittest.TestCase):
  99. """
  100. Test methods of boss related to the socket cache and socket handling.
  101. """
  102. def setUp(self):
  103. """
  104. Prepare the boss for some tests.
  105. Also prepare some variables we need.
  106. """
  107. self.__boss = BoB()
  108. # Fake the cache here so we can pretend it is us and hijack the
  109. # calls to its methods.
  110. self.__boss._socket_cache = self
  111. self.__boss._socket_path = '/socket/path'
  112. self.__raise_exception = None
  113. self.__socket_args = {
  114. "port": 53,
  115. "address": "::",
  116. "protocol": "UDP",
  117. "share_mode": "ANY",
  118. "share_name": "app"
  119. }
  120. # What was and wasn't called.
  121. self.__drop_app_called = None
  122. self.__get_socket_called = None
  123. self.__send_fd_called = None
  124. self.__get_token_called = None
  125. self.__drop_socket_called = None
  126. bind10_src.libutil_io_python.send_fd = self.__send_fd
  127. def __send_fd(self, to, socket):
  128. """
  129. A function to hook the send_fd in the bind10_src.
  130. """
  131. self.__send_fd_called = (to, socket)
  132. class FalseSocket:
  133. """
  134. A socket where we can fake methods we need instead of having a real
  135. socket.
  136. """
  137. def __init__(self):
  138. self.send = b""
  139. def fileno(self):
  140. """
  141. The file number. Used for identifying the remote application.
  142. """
  143. return 42
  144. def sendall(self, data):
  145. """
  146. Adds data to the self.send.
  147. """
  148. self.send += data
  149. def drop_application(self, application):
  150. """
  151. Part of pretending to be the cache. Logs the parameter to
  152. self.__drop_app_called.
  153. In the case self.__raise_exception is set, the exception there
  154. is raised instead.
  155. """
  156. if self.__raise_exception is not None:
  157. raise self.__raise_exception
  158. self.__drop_app_called = application
  159. def test_consumer_dead(self):
  160. """
  161. Test that it calls the drop_application method of the cache.
  162. """
  163. self.__boss.socket_consumer_dead(self.FalseSocket())
  164. self.assertEqual(42, self.__drop_app_called)
  165. def test_consumer_dead_invalid(self):
  166. """
  167. Test that it doesn't crash in case the application is not known to
  168. the cache, the boss doesn't crash, as this actually can happen in
  169. practice.
  170. """
  171. self.__raise_exception = ValueError("This application is unknown")
  172. # This doesn't crash
  173. self.__boss.socket_consumer_dead(self.FalseSocket())
  174. def get_socket(self, token, application):
  175. """
  176. Part of pretending to be the cache. If there's anything in
  177. __raise_exception, it is raised. Otherwise, the call is logged
  178. into __get_socket_called and a number is returned.
  179. """
  180. if self.__raise_exception is not None:
  181. raise self.__raise_exception
  182. self.__get_socket_called = (token, application)
  183. return 13
  184. def test_request_handler(self):
  185. """
  186. Test that a request for socket is forwarded and the socket is sent
  187. back, if it returns a socket.
  188. """
  189. socket = self.FalseSocket()
  190. # An exception from the cache
  191. self.__raise_exception = ValueError("Test value error")
  192. self.__boss.socket_request_handler(b"token", socket)
  193. # It was called, but it threw, so it is not noted here
  194. self.assertIsNone(self.__get_socket_called)
  195. self.assertEqual(b"0\n", socket.send)
  196. # It should not have sent any socket.
  197. self.assertIsNone(self.__send_fd_called)
  198. # Now prepare a valid scenario
  199. self.__raise_exception = None
  200. socket.send = b""
  201. self.__boss.socket_request_handler(b"token", socket)
  202. self.assertEqual(b"1\n", socket.send)
  203. self.assertEqual((42, 13), self.__send_fd_called)
  204. self.assertEqual(("token", 42), self.__get_socket_called)
  205. def get_token(self, protocol, address, port, share_mode, share_name):
  206. """
  207. Part of pretending to be the cache. If there's anything in
  208. __raise_exception, it is raised. Otherwise, the parameters are
  209. logged into __get_token_called and a token is returned.
  210. """
  211. if self.__raise_exception is not None:
  212. raise self.__raise_exception
  213. self.__get_token_called = (protocol, address, port, share_mode,
  214. share_name)
  215. return "token"
  216. def test_get_socket_ok(self):
  217. """
  218. Test the successful scenario of getting a socket.
  219. """
  220. result = self.__boss._get_socket(self.__socket_args)
  221. [code, answer] = result['result']
  222. self.assertEqual(0, code)
  223. self.assertEqual({
  224. 'token': 'token',
  225. 'path': '/socket/path'
  226. }, answer)
  227. addr = self.__get_token_called[1]
  228. self.assertTrue(isinstance(addr, IPAddr))
  229. self.assertEqual("::", str(addr))
  230. self.assertEqual(("UDP", addr, 53, "ANY", "app"),
  231. self.__get_token_called)
  232. def test_get_socket_error(self):
  233. """
  234. Test that bad inputs are handled correctly, etc.
  235. """
  236. def check_code(code, args):
  237. """
  238. Pass the args there and check if it returns success or not.
  239. The rest is not tested, as it is already checked in the
  240. test_get_socket_ok.
  241. """
  242. [rcode, ranswer] = self.__boss._get_socket(args)['result']
  243. self.assertEqual(code, rcode)
  244. if code != 0:
  245. # This should be an error message. The exact formatting
  246. # is unknown, but we check it is string at least
  247. self.assertTrue(isinstance(ranswer, str))
  248. def mod_args(name, value):
  249. """
  250. Override a parameter in the args.
  251. """
  252. result = dict(self.__socket_args)
  253. result[name] = value
  254. return result
  255. # Port too large
  256. check_code(1, mod_args('port', 65536))
  257. # Not numeric address
  258. check_code(1, mod_args('address', 'example.org.'))
  259. # Some bad values of enum-like params
  260. check_code(1, mod_args('protocol', 'BAD PROTO'))
  261. check_code(1, mod_args('share_mode', 'BAD SHARE'))
  262. # Check missing parameters
  263. for param in self.__socket_args.keys():
  264. args = dict(self.__socket_args)
  265. del args[param]
  266. check_code(1, args)
  267. # These are OK values for the enum-like parameters
  268. # The ones from test_get_socket_ok are not tested here
  269. check_code(0, mod_args('protocol', 'TCP'))
  270. check_code(0, mod_args('share_mode', 'SAMEAPP'))
  271. check_code(0, mod_args('share_mode', 'NO'))
  272. # If an exception is raised from within the cache, it is converted
  273. # to an error, not propagated
  274. self.__raise_exception = Exception("Test exception")
  275. check_code(1, self.__socket_args)
  276. # The special "expected" exceptions
  277. self.__raise_exception = \
  278. isc.bind10.socket_cache.ShareError("Not shared")
  279. check_code(3, self.__socket_args)
  280. self.__raise_exception = \
  281. isc.bind10.socket_cache.SocketError("Not shared", 13)
  282. check_code(2, self.__socket_args)
  283. def drop_socket(self, token):
  284. """
  285. Part of pretending to be the cache. If there's anything in
  286. __raise_exception, it is raised. Otherwise, the parameter is stored
  287. in __drop_socket_called.
  288. """
  289. if self.__raise_exception is not None:
  290. raise self.__raise_exception
  291. self.__drop_socket_called = token
  292. def test_drop_socket(self):
  293. """
  294. Check the drop_socket command. It should directly call the method
  295. on the cache. Exceptions should be translated to error messages.
  296. """
  297. # This should be OK and just propagated to the call.
  298. self.assertEqual({"result": [0]},
  299. self.__boss.command_handler("drop_socket",
  300. {"token": "token"}))
  301. self.assertEqual("token", self.__drop_socket_called)
  302. self.__drop_socket_called = None
  303. # Missing parameter
  304. self.assertEqual({"result": [1, "Missing token parameter"]},
  305. self.__boss.command_handler("drop_socket", {}))
  306. self.assertIsNone(self.__drop_socket_called)
  307. # An exception is raised from within the cache
  308. self.__raise_exception = ValueError("Test error")
  309. self.assertEqual({"result": [1, "Test error"]},
  310. self.__boss.command_handler("drop_socket",
  311. {"token": "token"}))
  312. class TestBoB(unittest.TestCase):
  313. def test_init(self):
  314. bob = BoB()
  315. self.assertEqual(bob.verbose, False)
  316. self.assertEqual(bob.msgq_socket_file, None)
  317. self.assertEqual(bob.cc_session, None)
  318. self.assertEqual(bob.ccs, None)
  319. self.assertEqual(bob.components, {})
  320. self.assertEqual(bob.runnable, False)
  321. self.assertEqual(bob.uid, None)
  322. self.assertEqual(bob.username, None)
  323. self.assertIsNone(bob._socket_cache)
  324. def test_set_creator(self):
  325. """
  326. Test the call to set_creator. First time, the cache is created
  327. with the passed creator. The next time, it throws an exception.
  328. """
  329. bob = BoB()
  330. # The cache doesn't use it at start, so just create an empty class
  331. class Creator: pass
  332. creator = Creator()
  333. bob.set_creator(creator)
  334. self.assertTrue(isinstance(bob._socket_cache,
  335. isc.bind10.socket_cache.Cache))
  336. self.assertEqual(creator, bob._socket_cache._creator)
  337. self.assertRaises(ValueError, bob.set_creator, creator)
  338. def test_socket_srv(self):
  339. """Tests init_socket_srv() and remove_socket_srv() work as expected."""
  340. bob = BoB()
  341. self.assertIsNone(bob._srv_socket)
  342. self.assertIsNone(bob._tmpdir)
  343. self.assertIsNone(bob._socket_path)
  344. bob.init_socket_srv()
  345. self.assertIsNotNone(bob._srv_socket)
  346. self.assertNotEqual(-1, bob._srv_socket.fileno())
  347. self.assertEqual(os.path.join(bob._tmpdir, 'sockcreator'),
  348. bob._srv_socket.getsockname())
  349. self.assertIsNotNone(bob._tmpdir)
  350. self.assertTrue(os.path.isdir(bob._tmpdir))
  351. self.assertIsNotNone(bob._socket_path)
  352. self.assertTrue(os.path.exists(bob._socket_path))
  353. # Check that it's possible to connect to the socket file (this
  354. # only works if the socket file exists and the server listens on
  355. # it).
  356. s = socket.socket(socket.AF_UNIX)
  357. try:
  358. s.connect(bob._socket_path)
  359. can_connect = True
  360. s.close()
  361. except socket.error as e:
  362. can_connect = False
  363. self.assertTrue(can_connect)
  364. bob.remove_socket_srv()
  365. self.assertEqual(-1, bob._srv_socket.fileno())
  366. self.assertFalse(os.path.exists(bob._socket_path))
  367. self.assertFalse(os.path.isdir(bob._tmpdir))
  368. # These should not fail either:
  369. # second call
  370. bob.remove_socket_srv()
  371. bob._srv_socket = None
  372. bob.remove_socket_srv()
  373. def test_init_alternate_socket(self):
  374. bob = BoB("alt_socket_file")
  375. self.assertEqual(bob.verbose, False)
  376. self.assertEqual(bob.msgq_socket_file, "alt_socket_file")
  377. self.assertEqual(bob.cc_session, None)
  378. self.assertEqual(bob.ccs, None)
  379. self.assertEqual(bob.components, {})
  380. self.assertEqual(bob.runnable, False)
  381. self.assertEqual(bob.uid, None)
  382. self.assertEqual(bob.username, None)
  383. def test_command_handler(self):
  384. class DummySession():
  385. def group_sendmsg(self, msg, group):
  386. (self.msg, self.group) = (msg, group)
  387. def group_recvmsg(self, nonblock, seq): pass
  388. class DummyModuleCCSession():
  389. module_spec = isc.config.module_spec.ModuleSpec({
  390. "module_name": "Boss",
  391. "statistics": [
  392. {
  393. "item_name": "boot_time",
  394. "item_type": "string",
  395. "item_optional": False,
  396. "item_default": "1970-01-01T00:00:00Z",
  397. "item_title": "Boot time",
  398. "item_description": "A date time when bind10 process starts initially",
  399. "item_format": "date-time"
  400. }
  401. ]
  402. })
  403. def get_module_spec(self):
  404. return self.module_spec
  405. bob = BoB()
  406. bob.verbose = True
  407. bob.cc_session = DummySession()
  408. bob.ccs = DummyModuleCCSession()
  409. # a bad command
  410. self.assertEqual(bob.command_handler(-1, None),
  411. isc.config.ccsession.create_answer(1, "bad command"))
  412. # "shutdown" command
  413. self.assertEqual(bob.command_handler("shutdown", None),
  414. isc.config.ccsession.create_answer(0))
  415. self.assertFalse(bob.runnable)
  416. # "getstats" command
  417. self.assertEqual(bob.command_handler("getstats", None),
  418. isc.config.ccsession.create_answer(0,
  419. { 'boot_time': time.strftime('%Y-%m-%dT%H:%M:%SZ', _BASETIME) }))
  420. # "ping" command
  421. self.assertEqual(bob.command_handler("ping", None),
  422. isc.config.ccsession.create_answer(0, "pong"))
  423. # "show_processes" command
  424. self.assertEqual(bob.command_handler("show_processes", None),
  425. isc.config.ccsession.create_answer(0,
  426. bob.get_processes()))
  427. # an unknown command
  428. self.assertEqual(bob.command_handler("__UNKNOWN__", None),
  429. isc.config.ccsession.create_answer(1, "Unknown command"))
  430. # Fake the get_token of cache and test the command works
  431. bob._socket_path = '/socket/path'
  432. class cache:
  433. def get_token(self, protocol, addr, port, share_mode, share_name):
  434. return str(addr) + ':' + str(port)
  435. bob._socket_cache = cache()
  436. args = {
  437. "port": 53,
  438. "address": "0.0.0.0",
  439. "protocol": "UDP",
  440. "share_mode": "ANY",
  441. "share_name": "app"
  442. }
  443. # at all and this is the easiest way to check.
  444. self.assertEqual({'result': [0, {'token': '0.0.0.0:53',
  445. 'path': '/socket/path'}]},
  446. bob.command_handler("get_socket", args))
  447. # The drop_socket is not tested here, but in TestCacheCommands.
  448. # It needs the cache mocks to be in place and they are there.
  449. def test_stop_process(self):
  450. """
  451. Test checking the stop_process method sends the right message over
  452. the message bus.
  453. """
  454. class DummySession():
  455. def group_sendmsg(self, msg, group, instance="*"):
  456. (self.msg, self.group, self.instance) = (msg, group, instance)
  457. bob = BoB()
  458. bob.cc_session = DummySession()
  459. bob.stop_process('process', 'address', 42)
  460. self.assertEqual('address', bob.cc_session.group)
  461. self.assertEqual('address', bob.cc_session.instance)
  462. self.assertEqual({'command': ['shutdown', {'pid': 42}]},
  463. bob.cc_session.msg)
  464. # Mock class for testing BoB's usage of ProcessInfo
  465. class MockProcessInfo:
  466. def __init__(self, name, args, env={}, dev_null_stdout=False,
  467. dev_null_stderr=False):
  468. self.name = name
  469. self.args = args
  470. self.env = env
  471. self.dev_null_stdout = dev_null_stdout
  472. self.dev_null_stderr = dev_null_stderr
  473. self.process = None
  474. self.pid = None
  475. def spawn(self):
  476. # set some pid (only used for testing that it is not None anymore)
  477. self.pid = 42147
  478. # Class for testing the BoB without actually starting processes.
  479. # This is used for testing the start/stop components routines and
  480. # the BoB commands.
  481. #
  482. # Testing that external processes start is outside the scope
  483. # of the unit test, by overriding the process start methods we can check
  484. # that the right processes are started depending on the configuration
  485. # options.
  486. class MockBob(BoB):
  487. def __init__(self):
  488. BoB.__init__(self)
  489. # Set flags as to which of the overridden methods has been run.
  490. self.msgq = False
  491. self.cfgmgr = False
  492. self.ccsession = False
  493. self.auth = False
  494. self.resolver = False
  495. self.xfrout = False
  496. self.xfrin = False
  497. self.zonemgr = False
  498. self.stats = False
  499. self.stats_httpd = False
  500. self.cmdctl = False
  501. self.dhcp6 = False
  502. self.dhcp4 = False
  503. self.c_channel_env = {}
  504. self.components = { }
  505. self.creator = False
  506. self.get_process_exit_status_called = False
  507. class MockSockCreator(isc.bind10.component.Component):
  508. def __init__(self, process, boss, kind, address=None, params=None):
  509. isc.bind10.component.Component.__init__(self, process, boss,
  510. kind, 'SockCreator')
  511. self._start_func = boss.start_creator
  512. specials = isc.bind10.special_component.get_specials()
  513. specials['sockcreator'] = MockSockCreator
  514. self._component_configurator = \
  515. isc.bind10.component.Configurator(self, specials)
  516. def start_creator(self):
  517. self.creator = True
  518. procinfo = ProcessInfo('b10-sockcreator', ['/bin/false'])
  519. procinfo.pid = 1
  520. return procinfo
  521. def _read_bind10_config(self):
  522. # Configuration options are set directly
  523. pass
  524. def start_msgq(self):
  525. self.msgq = True
  526. procinfo = ProcessInfo('b10-msgq', ['/bin/false'])
  527. procinfo.pid = 2
  528. return procinfo
  529. def start_ccsession(self, c_channel_env):
  530. # this is not a process, don't have to do anything with procinfo
  531. self.ccsession = True
  532. def start_cfgmgr(self):
  533. self.cfgmgr = True
  534. procinfo = ProcessInfo('b10-cfgmgr', ['/bin/false'])
  535. procinfo.pid = 3
  536. return procinfo
  537. def start_auth(self):
  538. self.auth = True
  539. procinfo = ProcessInfo('b10-auth', ['/bin/false'])
  540. procinfo.pid = 5
  541. return procinfo
  542. def start_resolver(self):
  543. self.resolver = True
  544. procinfo = ProcessInfo('b10-resolver', ['/bin/false'])
  545. procinfo.pid = 6
  546. return procinfo
  547. def start_simple(self, name):
  548. procmap = { 'b10-zonemgr': self.start_zonemgr,
  549. 'b10-stats': self.start_stats,
  550. 'b10-stats-httpd': self.start_stats_httpd,
  551. 'b10-cmdctl': self.start_cmdctl,
  552. 'b10-dhcp6': self.start_dhcp6,
  553. 'b10-dhcp4': self.start_dhcp4,
  554. 'b10-xfrin': self.start_xfrin,
  555. 'b10-xfrout': self.start_xfrout }
  556. return procmap[name]()
  557. def start_xfrout(self):
  558. self.xfrout = True
  559. procinfo = ProcessInfo('b10-xfrout', ['/bin/false'])
  560. procinfo.pid = 7
  561. return procinfo
  562. def start_xfrin(self):
  563. self.xfrin = True
  564. procinfo = ProcessInfo('b10-xfrin', ['/bin/false'])
  565. procinfo.pid = 8
  566. return procinfo
  567. def start_zonemgr(self):
  568. self.zonemgr = True
  569. procinfo = ProcessInfo('b10-zonemgr', ['/bin/false'])
  570. procinfo.pid = 9
  571. return procinfo
  572. def start_stats(self):
  573. self.stats = True
  574. procinfo = ProcessInfo('b10-stats', ['/bin/false'])
  575. procinfo.pid = 10
  576. return procinfo
  577. def start_stats_httpd(self):
  578. self.stats_httpd = True
  579. procinfo = ProcessInfo('b10-stats-httpd', ['/bin/false'])
  580. procinfo.pid = 11
  581. return procinfo
  582. def start_cmdctl(self):
  583. self.cmdctl = True
  584. procinfo = ProcessInfo('b10-cmdctl', ['/bin/false'])
  585. procinfo.pid = 12
  586. return procinfo
  587. def start_dhcp6(self):
  588. self.dhcp6 = True
  589. procinfo = ProcessInfo('b10-dhcp6', ['/bin/false'])
  590. procinfo.pid = 13
  591. return procinfo
  592. def start_dhcp4(self):
  593. self.dhcp4 = True
  594. procinfo = ProcessInfo('b10-dhcp4', ['/bin/false'])
  595. procinfo.pid = 14
  596. return procinfo
  597. def stop_process(self, process, recipient, pid):
  598. procmap = { 'b10-auth': self.stop_auth,
  599. 'b10-resolver': self.stop_resolver,
  600. 'b10-xfrout': self.stop_xfrout,
  601. 'b10-xfrin': self.stop_xfrin,
  602. 'b10-zonemgr': self.stop_zonemgr,
  603. 'b10-stats': self.stop_stats,
  604. 'b10-stats-httpd': self.stop_stats_httpd,
  605. 'b10-cmdctl': self.stop_cmdctl }
  606. procmap[process]()
  607. # Some functions to pretend we stop processes, use by stop_process
  608. def stop_msgq(self):
  609. if self.msgq:
  610. del self.components[2]
  611. self.msgq = False
  612. def stop_cfgmgr(self):
  613. if self.cfgmgr:
  614. del self.components[3]
  615. self.cfgmgr = False
  616. def stop_auth(self):
  617. if self.auth:
  618. del self.components[5]
  619. self.auth = False
  620. def stop_resolver(self):
  621. if self.resolver:
  622. del self.components[6]
  623. self.resolver = False
  624. def stop_xfrout(self):
  625. if self.xfrout:
  626. del self.components[7]
  627. self.xfrout = False
  628. def stop_xfrin(self):
  629. if self.xfrin:
  630. del self.components[8]
  631. self.xfrin = False
  632. def stop_zonemgr(self):
  633. if self.zonemgr:
  634. del self.components[9]
  635. self.zonemgr = False
  636. def stop_stats(self):
  637. if self.stats:
  638. del self.components[10]
  639. self.stats = False
  640. def stop_stats_httpd(self):
  641. if self.stats_httpd:
  642. del self.components[11]
  643. self.stats_httpd = False
  644. def stop_cmdctl(self):
  645. if self.cmdctl:
  646. del self.components[12]
  647. self.cmdctl = False
  648. def _get_process_exit_status(self):
  649. if self.get_process_exit_status_called:
  650. return (0, 0)
  651. self.get_process_exit_status_called = True
  652. return (53, 0)
  653. def _get_process_exit_status_unknown_pid(self):
  654. if self.get_process_exit_status_called:
  655. return (0, 0)
  656. self.get_process_exit_status_called = True
  657. return (42, 0)
  658. def _get_process_exit_status_raises_oserror_echild(self):
  659. raise OSError(errno.ECHILD, 'Mock error')
  660. def _get_process_exit_status_raises_oserror_other(self):
  661. raise OSError(0, 'Mock error')
  662. def _get_process_exit_status_raises_other(self):
  663. raise Exception('Mock error')
  664. def _make_mock_process_info(self, name, args, c_channel_env,
  665. dev_null_stdout=False, dev_null_stderr=False):
  666. return MockProcessInfo(name, args, c_channel_env,
  667. dev_null_stdout, dev_null_stderr)
  668. class MockBobSimple(BoB):
  669. def __init__(self):
  670. BoB.__init__(self)
  671. # Set which process has been started
  672. self.started_process_name = None
  673. self.started_process_args = None
  674. self.started_process_env = None
  675. def _make_mock_process_info(self, name, args, c_channel_env,
  676. dev_null_stdout=False, dev_null_stderr=False):
  677. return MockProcessInfo(name, args, c_channel_env,
  678. dev_null_stdout, dev_null_stderr)
  679. def start_process(self, name, args, c_channel_env, port=None,
  680. address=None):
  681. self.started_process_name = name
  682. self.started_process_args = args
  683. self.started_process_env = c_channel_env
  684. return None
  685. class TestStartStopProcessesBob(unittest.TestCase):
  686. """
  687. Check that the start_all_components method starts the right combination
  688. of components and that the right components are started and stopped
  689. according to changes in configuration.
  690. """
  691. def check_environment_unchanged(self):
  692. # Check whether the environment has not been changed
  693. self.assertEqual(original_os_environ, os.environ)
  694. def check_started(self, bob, core, auth, resolver):
  695. """
  696. Check that the right sets of services are started. The ones that
  697. should be running are specified by the core, auth and resolver parameters
  698. (they are groups of processes, eg. auth means b10-auth, -xfrout, -xfrin
  699. and -zonemgr).
  700. """
  701. self.assertEqual(bob.msgq, core)
  702. self.assertEqual(bob.cfgmgr, core)
  703. self.assertEqual(bob.ccsession, core)
  704. self.assertEqual(bob.creator, core)
  705. self.assertEqual(bob.auth, auth)
  706. self.assertEqual(bob.resolver, resolver)
  707. self.assertEqual(bob.xfrout, auth)
  708. self.assertEqual(bob.xfrin, auth)
  709. self.assertEqual(bob.zonemgr, auth)
  710. self.assertEqual(bob.stats, core)
  711. self.assertEqual(bob.stats_httpd, core)
  712. self.assertEqual(bob.cmdctl, core)
  713. self.check_environment_unchanged()
  714. def check_preconditions(self, bob):
  715. self.check_started(bob, False, False, False)
  716. def check_started_none(self, bob):
  717. """
  718. Check that the situation is according to configuration where no servers
  719. should be started. Some components still need to be running.
  720. """
  721. self.check_started(bob, True, False, False)
  722. self.check_environment_unchanged()
  723. def check_started_both(self, bob):
  724. """
  725. Check the situation is according to configuration where both servers
  726. (auth and resolver) are enabled.
  727. """
  728. self.check_started(bob, True, True, True)
  729. self.check_environment_unchanged()
  730. def check_started_auth(self, bob):
  731. """
  732. Check the set of components needed to run auth only is started.
  733. """
  734. self.check_started(bob, True, True, False)
  735. self.check_environment_unchanged()
  736. def check_started_resolver(self, bob):
  737. """
  738. Check the set of components needed to run resolver only is started.
  739. """
  740. self.check_started(bob, True, False, True)
  741. self.check_environment_unchanged()
  742. def check_started_dhcp(self, bob, v4, v6):
  743. """
  744. Check if proper combinations of DHCPv4 and DHCpv6 can be started
  745. """
  746. self.assertEqual(v4, bob.dhcp4)
  747. self.assertEqual(v6, bob.dhcp6)
  748. self.check_environment_unchanged()
  749. def construct_config(self, start_auth, start_resolver):
  750. # The things that are common, not turned on an off
  751. config = {}
  752. config['b10-stats'] = { 'kind': 'dispensable', 'address': 'Stats' }
  753. config['b10-stats-httpd'] = { 'kind': 'dispensable',
  754. 'address': 'StatsHttpd' }
  755. config['b10-cmdctl'] = { 'kind': 'needed', 'special': 'cmdctl' }
  756. if start_auth:
  757. config['b10-auth'] = { 'kind': 'needed', 'special': 'auth' }
  758. config['b10-xfrout'] = { 'kind': 'dispensable',
  759. 'address': 'Xfrout' }
  760. config['b10-xfrin'] = { 'kind': 'dispensable',
  761. 'address': 'Xfrin' }
  762. config['b10-zonemgr'] = { 'kind': 'dispensable',
  763. 'address': 'Zonemgr' }
  764. if start_resolver:
  765. config['b10-resolver'] = { 'kind': 'needed',
  766. 'special': 'resolver' }
  767. return {'components': config}
  768. def config_start_init(self, start_auth, start_resolver):
  769. """
  770. Test the configuration is loaded at the startup.
  771. """
  772. bob = MockBob()
  773. config = self.construct_config(start_auth, start_resolver)
  774. class CC:
  775. def get_full_config(self):
  776. return config
  777. # Provide the fake CC with data
  778. bob.ccs = CC()
  779. # And make sure it's not overwritten
  780. def start_ccsession():
  781. bob.ccsession = True
  782. bob.start_ccsession = lambda _: start_ccsession()
  783. # We need to return the original _read_bind10_config
  784. bob._read_bind10_config = lambda: BoB._read_bind10_config(bob)
  785. bob.start_all_components()
  786. self.check_started(bob, True, start_auth, start_resolver)
  787. self.check_environment_unchanged()
  788. def test_start_none(self):
  789. self.config_start_init(False, False)
  790. def test_start_resolver(self):
  791. self.config_start_init(False, True)
  792. def test_start_auth(self):
  793. self.config_start_init(True, False)
  794. def test_start_both(self):
  795. self.config_start_init(True, True)
  796. def test_config_start(self):
  797. """
  798. Test that the configuration starts and stops components according
  799. to configuration changes.
  800. """
  801. # Create BoB and ensure correct initialization
  802. bob = MockBob()
  803. self.check_preconditions(bob)
  804. bob.start_all_components()
  805. bob.runnable = True
  806. bob.config_handler(self.construct_config(False, False))
  807. self.check_started_none(bob)
  808. # Enable both at once
  809. bob.config_handler(self.construct_config(True, True))
  810. self.check_started_both(bob)
  811. # Not touched by empty change
  812. bob.config_handler({})
  813. self.check_started_both(bob)
  814. # Not touched by change to the same configuration
  815. bob.config_handler(self.construct_config(True, True))
  816. self.check_started_both(bob)
  817. # Turn them both off again
  818. bob.config_handler(self.construct_config(False, False))
  819. self.check_started_none(bob)
  820. # Not touched by empty change
  821. bob.config_handler({})
  822. self.check_started_none(bob)
  823. # Not touched by change to the same configuration
  824. bob.config_handler(self.construct_config(False, False))
  825. self.check_started_none(bob)
  826. # Start and stop auth separately
  827. bob.config_handler(self.construct_config(True, False))
  828. self.check_started_auth(bob)
  829. bob.config_handler(self.construct_config(False, False))
  830. self.check_started_none(bob)
  831. # Start and stop resolver separately
  832. bob.config_handler(self.construct_config(False, True))
  833. self.check_started_resolver(bob)
  834. bob.config_handler(self.construct_config(False, False))
  835. self.check_started_none(bob)
  836. # Alternate
  837. bob.config_handler(self.construct_config(True, False))
  838. self.check_started_auth(bob)
  839. bob.config_handler(self.construct_config(False, True))
  840. self.check_started_resolver(bob)
  841. bob.config_handler(self.construct_config(True, False))
  842. self.check_started_auth(bob)
  843. def test_config_start_once(self):
  844. """
  845. Tests that a component is started only once.
  846. """
  847. # Create BoB and ensure correct initialization
  848. bob = MockBob()
  849. self.check_preconditions(bob)
  850. bob.start_all_components()
  851. bob.runnable = True
  852. bob.config_handler(self.construct_config(True, True))
  853. self.check_started_both(bob)
  854. bob.start_auth = lambda: self.fail("Started auth again")
  855. bob.start_xfrout = lambda: self.fail("Started xfrout again")
  856. bob.start_xfrin = lambda: self.fail("Started xfrin again")
  857. bob.start_zonemgr = lambda: self.fail("Started zonemgr again")
  858. bob.start_resolver = lambda: self.fail("Started resolver again")
  859. # Send again we want to start them. Should not do it, as they are.
  860. bob.config_handler(self.construct_config(True, True))
  861. def test_config_not_started_early(self):
  862. """
  863. Test that components are not started by the config handler before
  864. startup.
  865. """
  866. bob = MockBob()
  867. self.check_preconditions(bob)
  868. bob.start_auth = lambda: self.fail("Started auth again")
  869. bob.start_xfrout = lambda: self.fail("Started xfrout again")
  870. bob.start_xfrin = lambda: self.fail("Started xfrin again")
  871. bob.start_zonemgr = lambda: self.fail("Started zonemgr again")
  872. bob.start_resolver = lambda: self.fail("Started resolver again")
  873. bob.config_handler({'start_auth': True, 'start_resolver': True})
  874. # Checks that DHCP (v4 and v6) components are started when expected
  875. def test_start_dhcp(self):
  876. # Create BoB and ensure correct initialization
  877. bob = MockBob()
  878. self.check_preconditions(bob)
  879. bob.start_all_components()
  880. bob.config_handler(self.construct_config(False, False))
  881. self.check_started_dhcp(bob, False, False)
  882. def test_start_dhcp_v6only(self):
  883. # Create BoB and ensure correct initialization
  884. bob = MockBob()
  885. self.check_preconditions(bob)
  886. # v6 only enabled
  887. bob.start_all_components()
  888. bob.runnable = True
  889. bob._BoB_started = True
  890. config = self.construct_config(False, False)
  891. config['components']['b10-dhcp6'] = { 'kind': 'needed',
  892. 'address': 'Dhcp6' }
  893. bob.config_handler(config)
  894. self.check_started_dhcp(bob, False, True)
  895. # uncomment when dhcpv4 becomes implemented
  896. # v4 only enabled
  897. #bob.cfg_start_dhcp6 = False
  898. #bob.cfg_start_dhcp4 = True
  899. #self.check_started_dhcp(bob, True, False)
  900. # both v4 and v6 enabled
  901. #bob.cfg_start_dhcp6 = True
  902. #bob.cfg_start_dhcp4 = True
  903. #self.check_started_dhcp(bob, True, True)
  904. class MockComponent:
  905. def __init__(self, name, pid, address=None):
  906. self.name = lambda: name
  907. self.pid = lambda: pid
  908. self.address = lambda: address
  909. self.restarted = False
  910. self.forceful = False
  911. self.running = True
  912. self.has_failed = False
  913. def get_restart_time(self):
  914. return 0 # arbitrary dummy value
  915. def restart(self, now):
  916. self.restarted = True
  917. return True
  918. def is_running(self):
  919. return self.running
  920. def failed(self, status):
  921. return self.has_failed
  922. def kill(self, forceful):
  923. self.forceful = forceful
  924. class TestBossCmd(unittest.TestCase):
  925. def test_ping(self):
  926. """
  927. Confirm simple ping command works.
  928. """
  929. bob = MockBob()
  930. answer = bob.command_handler("ping", None)
  931. self.assertEqual(answer, {'result': [0, 'pong']})
  932. def test_show_processes_empty(self):
  933. """
  934. Confirm getting a list of processes works.
  935. """
  936. bob = MockBob()
  937. answer = bob.command_handler("show_processes", None)
  938. self.assertEqual(answer, {'result': [0, []]})
  939. def test_show_processes(self):
  940. """
  941. Confirm getting a list of processes works.
  942. """
  943. bob = MockBob()
  944. bob.register_process(1, MockComponent('first', 1))
  945. bob.register_process(2, MockComponent('second', 2, 'Second'))
  946. answer = bob.command_handler("show_processes", None)
  947. processes = [[1, 'first', None],
  948. [2, 'second', 'Second']]
  949. self.assertEqual(answer, {'result': [0, processes]})
  950. class TestParseArgs(unittest.TestCase):
  951. """
  952. This tests parsing of arguments of the bind10 master process.
  953. """
  954. #TODO: Write tests for the original parsing, bad options, etc.
  955. def test_no_opts(self):
  956. """
  957. Test correct default values when no options are passed.
  958. """
  959. options = parse_args([], TestOptParser)
  960. self.assertEqual(None, options.data_path)
  961. self.assertEqual(None, options.config_file)
  962. self.assertEqual(None, options.cmdctl_port)
  963. def test_data_path(self):
  964. """
  965. Test it can parse the data path.
  966. """
  967. self.assertRaises(OptsError, parse_args, ['-p'], TestOptParser)
  968. self.assertRaises(OptsError, parse_args, ['--data-path'],
  969. TestOptParser)
  970. options = parse_args(['-p', '/data/path'], TestOptParser)
  971. self.assertEqual('/data/path', options.data_path)
  972. options = parse_args(['--data-path=/data/path'], TestOptParser)
  973. self.assertEqual('/data/path', options.data_path)
  974. def test_config_filename(self):
  975. """
  976. Test it can parse the config switch.
  977. """
  978. self.assertRaises(OptsError, parse_args, ['-c'], TestOptParser)
  979. self.assertRaises(OptsError, parse_args, ['--config-file'],
  980. TestOptParser)
  981. options = parse_args(['-c', 'config-file'], TestOptParser)
  982. self.assertEqual('config-file', options.config_file)
  983. options = parse_args(['--config-file=config-file'], TestOptParser)
  984. self.assertEqual('config-file', options.config_file)
  985. def test_clear_config(self):
  986. options = parse_args([], TestOptParser)
  987. self.assertEqual(False, options.clear_config)
  988. options = parse_args(['--clear-config'], TestOptParser)
  989. self.assertEqual(True, options.clear_config)
  990. def test_nokill(self):
  991. options = parse_args([], TestOptParser)
  992. self.assertEqual(False, options.nokill)
  993. options = parse_args(['--no-kill'], TestOptParser)
  994. self.assertEqual(True, options.nokill)
  995. options = parse_args([], TestOptParser)
  996. self.assertEqual(False, options.nokill)
  997. options = parse_args(['-i'], TestOptParser)
  998. self.assertEqual(True, options.nokill)
  999. def test_cmdctl_port(self):
  1000. """
  1001. Test it can parse the command control port.
  1002. """
  1003. self.assertRaises(OptsError, parse_args, ['--cmdctl-port=abc'],
  1004. TestOptParser)
  1005. self.assertRaises(OptsError, parse_args, ['--cmdctl-port=100000000'],
  1006. TestOptParser)
  1007. self.assertRaises(OptsError, parse_args, ['--cmdctl-port'],
  1008. TestOptParser)
  1009. options = parse_args(['--cmdctl-port=1234'], TestOptParser)
  1010. self.assertEqual(1234, options.cmdctl_port)
  1011. class TestPIDFile(unittest.TestCase):
  1012. def setUp(self):
  1013. self.pid_file = '@builddir@' + os.sep + 'bind10.pid'
  1014. if os.path.exists(self.pid_file):
  1015. os.unlink(self.pid_file)
  1016. def tearDown(self):
  1017. if os.path.exists(self.pid_file):
  1018. os.unlink(self.pid_file)
  1019. def check_pid_file(self):
  1020. # dump PID to the file, and confirm the content is correct
  1021. dump_pid(self.pid_file)
  1022. my_pid = os.getpid()
  1023. with open(self.pid_file, "r") as f:
  1024. self.assertEqual(my_pid, int(f.read()))
  1025. def test_dump_pid(self):
  1026. self.check_pid_file()
  1027. # make sure any existing content will be removed
  1028. with open(self.pid_file, "w") as f:
  1029. f.write('dummy data\n')
  1030. self.check_pid_file()
  1031. def test_unlink_pid_file_notexist(self):
  1032. dummy_data = 'dummy_data\n'
  1033. with open(self.pid_file, "w") as f:
  1034. f.write(dummy_data)
  1035. unlink_pid_file("no_such_pid_file")
  1036. # the file specified for unlink_pid_file doesn't exist,
  1037. # and the original content of the file should be intact.
  1038. with open(self.pid_file, "r") as f:
  1039. self.assertEqual(dummy_data, f.read())
  1040. def test_dump_pid_with_none(self):
  1041. # Check the behavior of dump_pid() and unlink_pid_file() with None.
  1042. # This should be no-op.
  1043. dump_pid(None)
  1044. self.assertFalse(os.path.exists(self.pid_file))
  1045. dummy_data = 'dummy_data\n'
  1046. with open(self.pid_file, "w") as f:
  1047. f.write(dummy_data)
  1048. unlink_pid_file(None)
  1049. with open(self.pid_file, "r") as f:
  1050. self.assertEqual(dummy_data, f.read())
  1051. def test_dump_pid_failure(self):
  1052. # the attempt to open file will fail, which should result in exception.
  1053. self.assertRaises(IOError, dump_pid,
  1054. 'nonexistent_dir' + os.sep + 'bind10.pid')
  1055. class TestBossComponents(unittest.TestCase):
  1056. """
  1057. Test the boss propagates component configuration properly to the
  1058. component configurator and acts sane.
  1059. """
  1060. def setUp(self):
  1061. self.__param = None
  1062. self.__called = False
  1063. self.__compconfig = {
  1064. 'comp': {
  1065. 'kind': 'needed',
  1066. 'process': 'cat'
  1067. }
  1068. }
  1069. def __unary_hook(self, param):
  1070. """
  1071. A hook function that stores the parameter for later examination.
  1072. """
  1073. self.__param = param
  1074. def __nullary_hook(self):
  1075. """
  1076. A hook function that notes down it was called.
  1077. """
  1078. self.__called = True
  1079. def __check_core(self, config):
  1080. """
  1081. A function checking that the config contains parts for the valid
  1082. core component configuration.
  1083. """
  1084. self.assertIsNotNone(config)
  1085. for component in ['sockcreator', 'msgq', 'cfgmgr']:
  1086. self.assertTrue(component in config)
  1087. self.assertEqual(component, config[component]['special'])
  1088. self.assertEqual('core', config[component]['kind'])
  1089. def __check_extended(self, config):
  1090. """
  1091. This checks that the config contains the core and one more component.
  1092. """
  1093. self.__check_core(config)
  1094. self.assertTrue('comp' in config)
  1095. self.assertEqual('cat', config['comp']['process'])
  1096. self.assertEqual('needed', config['comp']['kind'])
  1097. self.assertEqual(4, len(config))
  1098. def test_correct_run(self):
  1099. """
  1100. Test the situation when we run in usual scenario, nothing fails,
  1101. we just start, reconfigure and then stop peacefully.
  1102. """
  1103. bob = MockBob()
  1104. # Start it
  1105. orig = bob._component_configurator.startup
  1106. bob._component_configurator.startup = self.__unary_hook
  1107. bob.start_all_components()
  1108. bob._component_configurator.startup = orig
  1109. self.__check_core(self.__param)
  1110. self.assertEqual(3, len(self.__param))
  1111. # Reconfigure it
  1112. self.__param = None
  1113. orig = bob._component_configurator.reconfigure
  1114. bob._component_configurator.reconfigure = self.__unary_hook
  1115. # Otherwise it does not work
  1116. bob.runnable = True
  1117. bob.config_handler({'components': self.__compconfig})
  1118. self.__check_extended(self.__param)
  1119. currconfig = self.__param
  1120. # If we reconfigure it, but it does not contain the components part,
  1121. # nothing is called
  1122. bob.config_handler({})
  1123. self.assertEqual(self.__param, currconfig)
  1124. self.__param = None
  1125. bob._component_configurator.reconfigure = orig
  1126. # Check a configuration that messes up the core components is rejected.
  1127. compconf = dict(self.__compconfig)
  1128. compconf['msgq'] = { 'process': 'echo' }
  1129. result = bob.config_handler({'components': compconf})
  1130. # Check it rejected it
  1131. self.assertEqual(1, result['result'][0])
  1132. # We can't call shutdown, that one relies on the stuff in main
  1133. # We check somewhere else that the shutdown is actually called
  1134. # from there (the test_kills).
  1135. def __real_test_kill(self, nokill=False, ex_on_kill=None):
  1136. """
  1137. Helper function that does the actual kill functionality testing.
  1138. """
  1139. bob = MockBob()
  1140. bob.nokill = nokill
  1141. killed = []
  1142. class ImmortalComponent:
  1143. """
  1144. An immortal component. It does not stop when it is told so
  1145. (anyway it is not told so). It does not die if it is killed
  1146. the first time. It dies only when killed forcefully.
  1147. """
  1148. def __init__(self):
  1149. # number of kill() calls, preventing infinite loop.
  1150. self.__call_count = 0
  1151. def kill(self, forceful=False):
  1152. self.__call_count += 1
  1153. if self.__call_count > 2:
  1154. raise Exception('Too many calls to ImmortalComponent.kill')
  1155. killed.append(forceful)
  1156. if ex_on_kill is not None:
  1157. # If exception is given by the test, raise it here.
  1158. # In the case of ESRCH, the process should have gone
  1159. # somehow, so we clear the components.
  1160. if ex_on_kill.errno == errno.ESRCH:
  1161. bob.components = {}
  1162. raise ex_on_kill
  1163. if forceful:
  1164. bob.components = {}
  1165. def pid(self):
  1166. return 1
  1167. def name(self):
  1168. return "Immortal"
  1169. bob.components = {}
  1170. bob.register_process(1, ImmortalComponent())
  1171. # While at it, we check the configurator shutdown is actually called
  1172. orig = bob._component_configurator.shutdown
  1173. bob._component_configurator.shutdown = self.__nullary_hook
  1174. self.__called = False
  1175. bob.ccs = MockModuleCCSession()
  1176. self.assertFalse(bob.ccs.stopped)
  1177. bob.shutdown()
  1178. self.assertTrue(bob.ccs.stopped)
  1179. # Here, killed is an array where False is added if SIGTERM
  1180. # should be sent, or True if SIGKILL should be sent, in order in
  1181. # which they're sent.
  1182. if nokill:
  1183. self.assertEqual([], killed)
  1184. else:
  1185. if ex_on_kill is not None:
  1186. self.assertEqual([False], killed)
  1187. else:
  1188. self.assertEqual([False, True], killed)
  1189. self.assertTrue(self.__called)
  1190. bob._component_configurator.shutdown = orig
  1191. def test_kills(self):
  1192. """
  1193. Test that the boss kills components which don't want to stop.
  1194. """
  1195. self.__real_test_kill()
  1196. def test_kill_fail(self):
  1197. """Test cases where kill() results in an exception due to OS error.
  1198. The behavior should be different for EPERM, so we test two cases.
  1199. """
  1200. ex = OSError()
  1201. ex.errno, ex.strerror = errno.ESRCH, 'No such process'
  1202. self.__real_test_kill(ex_on_kill=ex)
  1203. ex.errno, ex.strerror = errno.EPERM, 'Operation not permitted'
  1204. self.__real_test_kill(ex_on_kill=ex)
  1205. def test_nokill(self):
  1206. """
  1207. Test that the boss *doesn't* kill components which don't want to
  1208. stop, when asked not to (by passing the --no-kill option which
  1209. sets bob.nokill to True).
  1210. """
  1211. self.__real_test_kill(True)
  1212. def test_component_shutdown(self):
  1213. """
  1214. Test the component_shutdown sets all variables accordingly.
  1215. """
  1216. bob = MockBob()
  1217. self.assertRaises(Exception, bob.component_shutdown, 1)
  1218. self.assertEqual(1, bob.exitcode)
  1219. bob._BoB__started = True
  1220. bob.component_shutdown(2)
  1221. self.assertEqual(2, bob.exitcode)
  1222. self.assertFalse(bob.runnable)
  1223. def test_init_config(self):
  1224. """
  1225. Test initial configuration is loaded.
  1226. """
  1227. bob = MockBob()
  1228. # Start it
  1229. bob._component_configurator.reconfigure = self.__unary_hook
  1230. # We need to return the original read_bind10_config
  1231. bob._read_bind10_config = lambda: BoB._read_bind10_config(bob)
  1232. # And provide a session to read the data from
  1233. class CC:
  1234. pass
  1235. bob.ccs = CC()
  1236. bob.ccs.get_full_config = lambda: {'components': self.__compconfig}
  1237. bob.start_all_components()
  1238. self.__check_extended(self.__param)
  1239. def __setup_restart(self, bob, component):
  1240. '''Common procedure for restarting a component used below.'''
  1241. bob.components_to_restart = { component }
  1242. component.restarted = False
  1243. bob.restart_processes()
  1244. def test_restart_processes(self):
  1245. '''Check some behavior on restarting processes.'''
  1246. bob = MockBob()
  1247. bob.runnable = True
  1248. component = MockComponent('test', 53)
  1249. # A component to be restarted will actually be restarted iff it's
  1250. # in the configurator's configuration.
  1251. # We bruteforce the configurator internal below; ugly, but the easiest
  1252. # way for the test.
  1253. bob._component_configurator._components['test'] = (None, component)
  1254. self.__setup_restart(bob, component)
  1255. self.assertTrue(component.restarted)
  1256. self.assertNotIn(component, bob.components_to_restart)
  1257. # Remove the component from the configuration. It won't be restarted
  1258. # even if scheduled, nor will remain in the to-be-restarted list.
  1259. del bob._component_configurator._components['test']
  1260. self.__setup_restart(bob, component)
  1261. self.assertFalse(component.restarted)
  1262. self.assertNotIn(component, bob.components_to_restart)
  1263. def test_get_processes(self):
  1264. '''Test that procsses are returned correctly, sorted by pid.'''
  1265. bob = MockBob()
  1266. pids = list(range(0, 20))
  1267. random.shuffle(pids)
  1268. for i in range(0, 20):
  1269. pid = pids[i]
  1270. component = MockComponent('test' + str(pid), pid,
  1271. 'Test' + str(pid))
  1272. bob.components[pid] = component
  1273. process_list = bob.get_processes()
  1274. self.assertEqual(20, len(process_list))
  1275. last_pid = -1
  1276. for process in process_list:
  1277. pid = process[0]
  1278. self.assertLessEqual(last_pid, pid)
  1279. last_pid = pid
  1280. self.assertEqual([pid, 'test' + str(pid), 'Test' + str(pid)],
  1281. process)
  1282. def _test_reap_children_helper(self, runnable, is_running, failed):
  1283. '''Construct a BoB instance, set various data in it according to
  1284. passed args and check if the component was added to the list of
  1285. components to restart.'''
  1286. bob = MockBob()
  1287. bob.runnable = runnable
  1288. component = MockComponent('test', 53)
  1289. component.running = is_running
  1290. component.has_failed = failed
  1291. bob.components[53] = component
  1292. self.assertNotIn(component, bob.components_to_restart)
  1293. bob.reap_children()
  1294. if runnable and is_running and not failed:
  1295. self.assertIn(component, bob.components_to_restart)
  1296. else:
  1297. self.assertEqual([], bob.components_to_restart)
  1298. def test_reap_children(self):
  1299. '''Test that children are queued to be restarted when they ask for it.'''
  1300. # test various combinations of 3 booleans
  1301. # (BoB.runnable, component.is_running(), component.failed())
  1302. self._test_reap_children_helper(False, False, False)
  1303. self._test_reap_children_helper(False, False, True)
  1304. self._test_reap_children_helper(False, True, False)
  1305. self._test_reap_children_helper(False, True, True)
  1306. self._test_reap_children_helper(True, False, False)
  1307. self._test_reap_children_helper(True, False, True)
  1308. self._test_reap_children_helper(True, True, False)
  1309. self._test_reap_children_helper(True, True, True)
  1310. # setup for more tests below
  1311. bob = MockBob()
  1312. bob.runnable = True
  1313. component = MockComponent('test', 53)
  1314. bob.components[53] = component
  1315. # case where the returned pid is unknown to us. nothing should
  1316. # happpen then.
  1317. bob.get_process_exit_status_called = False
  1318. bob._get_process_exit_status = bob._get_process_exit_status_unknown_pid
  1319. bob.components_to_restart = []
  1320. # this should do nothing as the pid is unknown
  1321. bob.reap_children()
  1322. self.assertEqual([], bob.components_to_restart)
  1323. # case where bob._get_process_exit_status() raises OSError with
  1324. # errno.ECHILD
  1325. bob._get_process_exit_status = \
  1326. bob._get_process_exit_status_raises_oserror_echild
  1327. bob.components_to_restart = []
  1328. # this should catch and handle the OSError
  1329. bob.reap_children()
  1330. self.assertEqual([], bob.components_to_restart)
  1331. # case where bob._get_process_exit_status() raises OSError with
  1332. # errno other than ECHILD
  1333. bob._get_process_exit_status = \
  1334. bob._get_process_exit_status_raises_oserror_other
  1335. with self.assertRaises(OSError):
  1336. bob.reap_children()
  1337. # case where bob._get_process_exit_status() raises something
  1338. # other than OSError
  1339. bob._get_process_exit_status = \
  1340. bob._get_process_exit_status_raises_other
  1341. with self.assertRaises(Exception):
  1342. bob.reap_children()
  1343. def test_kill_started_components(self):
  1344. '''Test that started components are killed.'''
  1345. bob = MockBob()
  1346. component = MockComponent('test', 53, 'Test')
  1347. bob.components[53] = component
  1348. self.assertEqual([[53, 'test', 'Test']], bob.get_processes())
  1349. bob.kill_started_components()
  1350. self.assertEqual([], bob.get_processes())
  1351. self.assertTrue(component.forceful)
  1352. def test_start_msgq(self):
  1353. '''Test that b10-msgq is started.'''
  1354. bob = MockBobSimple()
  1355. bob.c_channel_env = {'FOO': 'an env string'}
  1356. bob.run_under_unittests = True
  1357. # use the MockProcessInfo creator
  1358. bob._make_process_info = bob._make_mock_process_info
  1359. # non-verbose case
  1360. bob.verbose = False
  1361. pi = bob.start_msgq()
  1362. self.assertEqual('b10-msgq', pi.name)
  1363. self.assertEqual(['b10-msgq'], pi.args)
  1364. self.assertTrue(pi.dev_null_stdout)
  1365. self.assertTrue(pi.dev_null_stderr)
  1366. self.assertEqual({'FOO': 'an env string'}, pi.env)
  1367. # this is set by ProcessInfo.spawn()
  1368. self.assertEqual(42147, pi.pid)
  1369. # verbose case
  1370. bob.verbose = True
  1371. pi = bob.start_msgq()
  1372. self.assertEqual('b10-msgq', pi.name)
  1373. self.assertEqual(['b10-msgq'], pi.args)
  1374. self.assertTrue(pi.dev_null_stdout)
  1375. self.assertFalse(pi.dev_null_stderr)
  1376. self.assertEqual({'FOO': 'an env string'}, pi.env)
  1377. # this is set by ProcessInfo.spawn()
  1378. self.assertEqual(42147, pi.pid)
  1379. def test_start_msgq_timeout(self):
  1380. '''Test that b10-msgq startup attempts connections several times
  1381. and times out eventually.'''
  1382. bob = MockBobSimple()
  1383. bob.c_channel_env = {}
  1384. # keep the timeout small for the test to complete quickly
  1385. bob.msgq_timeout = 1
  1386. # specifically set this to False so that the connect is
  1387. # attempted
  1388. bob.run_under_unittests = False
  1389. # use the MockProcessInfo creator
  1390. bob._make_process_info = bob._make_mock_process_info
  1391. global attempts
  1392. global tsec
  1393. attempts = 0
  1394. tsec = 0
  1395. tmp_time = time.time
  1396. tmp_sleep = time.sleep
  1397. def _my_time():
  1398. global attempts
  1399. global tsec
  1400. attempts += 1
  1401. return tsec
  1402. def _my_sleep(nsec):
  1403. global tsec
  1404. tsec += nsec
  1405. time.time = _my_time
  1406. time.sleep = _my_sleep
  1407. with self.assertRaises(bind10_src.CChannelConnectError):
  1408. # An exception will be thrown here when it eventually times
  1409. # out.
  1410. pi = bob.start_msgq()
  1411. # 1 second of attempts every 0.1 seconds should result in 10
  1412. # attempts. 1 attempt happens at the start. 2 more attempts seem
  1413. # to happen inside start_msgq().
  1414. self.assertEqual(attempts, 13)
  1415. time.time = tmp_time
  1416. time.sleep = tmp_sleep
  1417. def test_start_cfgmgr(self):
  1418. '''Test that b10-cfgmgr is started.'''
  1419. class DummySession():
  1420. def group_recvmsg(self):
  1421. return (None, None)
  1422. bob = MockBobSimple()
  1423. bob.c_channel_env = {}
  1424. bob.cc_session = DummySession()
  1425. bob.run_under_unittests = True
  1426. # use the MockProcessInfo creator
  1427. bob._make_process_info = bob._make_mock_process_info
  1428. # defaults
  1429. pi = bob.start_cfgmgr()
  1430. self.assertEqual('b10-cfgmgr', pi.name)
  1431. self.assertEqual(['b10-cfgmgr'], pi.args)
  1432. self.assertEqual({}, pi.env)
  1433. # this is set by ProcessInfo.spawn()
  1434. self.assertEqual(42147, pi.pid)
  1435. # data_path is specified
  1436. bob.data_path = '/var/lib/test'
  1437. pi = bob.start_cfgmgr()
  1438. self.assertEqual('b10-cfgmgr', pi.name)
  1439. self.assertEqual(['b10-cfgmgr',
  1440. '--data-path=/var/lib/test'],
  1441. pi.args)
  1442. self.assertEqual({}, pi.env)
  1443. # this is set by ProcessInfo.spawn()
  1444. self.assertEqual(42147, pi.pid)
  1445. # config_filename is specified
  1446. bob.config_filename = 'foo.cfg'
  1447. pi = bob.start_cfgmgr()
  1448. self.assertEqual('b10-cfgmgr', pi.name)
  1449. self.assertEqual(['b10-cfgmgr',
  1450. '--data-path=/var/lib/test',
  1451. '--config-filename=foo.cfg'],
  1452. pi.args)
  1453. self.assertEqual({}, pi.env)
  1454. # this is set by ProcessInfo.spawn()
  1455. self.assertEqual(42147, pi.pid)
  1456. # clear_config is specified
  1457. bob.clear_config = True
  1458. pi = bob.start_cfgmgr()
  1459. self.assertEqual('b10-cfgmgr', pi.name)
  1460. self.assertEqual(['b10-cfgmgr',
  1461. '--data-path=/var/lib/test',
  1462. '--config-filename=foo.cfg',
  1463. '--clear-config'],
  1464. pi.args)
  1465. self.assertEqual({}, pi.env)
  1466. # this is set by ProcessInfo.spawn()
  1467. self.assertEqual(42147, pi.pid)
  1468. def test_start_cfgmgr_timeout(self):
  1469. '''Test that b10-cfgmgr startup attempts connections several times
  1470. and times out eventually.'''
  1471. class DummySession():
  1472. def group_recvmsg(self):
  1473. return (None, None)
  1474. bob = MockBobSimple()
  1475. bob.c_channel_env = {}
  1476. bob.cc_session = DummySession()
  1477. # keep the wait time small for the test to complete quickly
  1478. bob.wait_time = 2
  1479. # specifically set this to False so that the process check is
  1480. # attempted
  1481. bob.run_under_unittests = False
  1482. # use the MockProcessInfo creator
  1483. bob._make_process_info = bob._make_mock_process_info
  1484. global attempts
  1485. attempts = 0
  1486. tmp_sleep = time.sleep
  1487. def _my_sleep(nsec):
  1488. global attempts
  1489. attempts += 1
  1490. return tmp_sleep(nsec)
  1491. time.sleep = _my_sleep
  1492. thrown = False
  1493. # An exception will be thrown here when it eventually times out.
  1494. try:
  1495. pi = bob.start_cfgmgr()
  1496. except bind10_src.ProcessStartError as e:
  1497. thrown = True
  1498. # We just check that an exception was thrown, and that several
  1499. # attempts were made to connect.
  1500. self.assertTrue(thrown)
  1501. # 2 seconds of attempts every 1 second should result in 2 attempts
  1502. self.assertEqual(attempts, 2)
  1503. time.sleep = tmp_sleep
  1504. def test_start_ccsession(self):
  1505. '''Test that CC session is started.'''
  1506. class DummySession():
  1507. def __init__(self, specfile, config_handler, command_handler,
  1508. socket_file):
  1509. self.specfile = specfile
  1510. self.config_handler = config_handler
  1511. self.command_handler = command_handler
  1512. self.socket_file = socket_file
  1513. self.started = False
  1514. def start(self):
  1515. self.started = True
  1516. bob = MockBobSimple()
  1517. tmp = isc.config.ModuleCCSession
  1518. isc.config.ModuleCCSession = DummySession
  1519. bob.start_ccsession({})
  1520. self.assertEqual(bind10_src.SPECFILE_LOCATION, bob.ccs.specfile)
  1521. self.assertEqual(bob.config_handler, bob.ccs.config_handler)
  1522. self.assertEqual(bob.command_handler, bob.ccs.command_handler)
  1523. self.assertEqual(bob.msgq_socket_file, bob.ccs.socket_file)
  1524. self.assertTrue(bob.ccs.started)
  1525. isc.config.ModuleCCSession = tmp
  1526. def test_start_process(self):
  1527. '''Test that processes can be started.'''
  1528. bob = MockBob()
  1529. # use the MockProcessInfo creator
  1530. bob._make_process_info = bob._make_mock_process_info
  1531. pi = bob.start_process('Test Process', ['/bin/true'], {})
  1532. self.assertEqual('Test Process', pi.name)
  1533. self.assertEqual(['/bin/true'], pi.args)
  1534. self.assertEqual({}, pi.env)
  1535. # this is set by ProcessInfo.spawn()
  1536. self.assertEqual(42147, pi.pid)
  1537. def test_register_process(self):
  1538. '''Test that processes can be registered with BoB.'''
  1539. bob = MockBob()
  1540. component = MockComponent('test', 53, 'Test')
  1541. self.assertFalse(53 in bob.components)
  1542. bob.register_process(53, component)
  1543. self.assertTrue(53 in bob.components)
  1544. self.assertEqual(bob.components[53].name(), 'test')
  1545. self.assertEqual(bob.components[53].pid(), 53)
  1546. self.assertEqual(bob.components[53].address(), 'Test')
  1547. def test_start_simple(self):
  1548. '''Test simple process startup.'''
  1549. bob = MockBobSimple()
  1550. bob.c_channel_env = {}
  1551. # non-verbose case
  1552. bob.verbose = False
  1553. bob.start_simple('/bin/true')
  1554. self.assertEqual('/bin/true', bob.started_process_name)
  1555. self.assertEqual(['/bin/true'], bob.started_process_args)
  1556. # verbose case
  1557. bob.verbose = True
  1558. bob.start_simple('/bin/true')
  1559. self.assertEqual('/bin/true', bob.started_process_name)
  1560. self.assertEqual(['/bin/true', '-v'], bob.started_process_args)
  1561. def test_start_auth(self):
  1562. '''Test that b10-auth is started.'''
  1563. bob = MockBobSimple()
  1564. bob.c_channel_env = {'FOO': 'an env string'}
  1565. # non-verbose case
  1566. bob.verbose = False
  1567. bob.start_auth()
  1568. self.assertEqual('b10-auth', bob.started_process_name)
  1569. self.assertEqual(['b10-auth'], bob.started_process_args)
  1570. self.assertEqual({'FOO': 'an env string'}, bob.started_process_env)
  1571. # verbose case
  1572. bob.verbose = True
  1573. bob.start_auth()
  1574. self.assertEqual('b10-auth', bob.started_process_name)
  1575. self.assertEqual(['b10-auth', '-v'], bob.started_process_args)
  1576. self.assertEqual({'FOO': 'an env string'}, bob.started_process_env)
  1577. def test_start_resolver(self):
  1578. '''Test that b10-resolver is started.'''
  1579. bob = MockBobSimple()
  1580. bob.c_channel_env = {'BAR': 'an env string'}
  1581. # non-verbose case
  1582. bob.verbose = False
  1583. bob.start_resolver()
  1584. self.assertEqual('b10-resolver', bob.started_process_name)
  1585. self.assertEqual(['b10-resolver'], bob.started_process_args)
  1586. self.assertEqual({'BAR': 'an env string'}, bob.started_process_env)
  1587. # verbose case
  1588. bob.verbose = True
  1589. bob.start_resolver()
  1590. self.assertEqual('b10-resolver', bob.started_process_name)
  1591. self.assertEqual(['b10-resolver', '-v'], bob.started_process_args)
  1592. self.assertEqual({'BAR': 'an env string'}, bob.started_process_env)
  1593. def test_start_cmdctl(self):
  1594. '''Test that b10-cmdctl is started.'''
  1595. bob = MockBobSimple()
  1596. bob.c_channel_env = {'BAZ': 'an env string'}
  1597. # non-verbose case
  1598. bob.verbose = False
  1599. bob.start_cmdctl()
  1600. self.assertEqual('b10-cmdctl', bob.started_process_name)
  1601. self.assertEqual(['b10-cmdctl'], bob.started_process_args)
  1602. self.assertEqual({'BAZ': 'an env string'}, bob.started_process_env)
  1603. # verbose case
  1604. bob.verbose = True
  1605. bob.start_cmdctl()
  1606. self.assertEqual('b10-cmdctl', bob.started_process_name)
  1607. self.assertEqual(['b10-cmdctl', '-v'], bob.started_process_args)
  1608. self.assertEqual({'BAZ': 'an env string'}, bob.started_process_env)
  1609. # with port
  1610. bob.verbose = True
  1611. bob.cmdctl_port = 9353
  1612. bob.start_cmdctl()
  1613. self.assertEqual('b10-cmdctl', bob.started_process_name)
  1614. self.assertEqual(['b10-cmdctl', '--port=9353', '-v'], bob.started_process_args)
  1615. self.assertEqual({'BAZ': 'an env string'}, bob.started_process_env)
  1616. def test_socket_data(self):
  1617. '''Test that BoB._socket_data works as expected.'''
  1618. class MockSock:
  1619. def __init__(self, fd, throw):
  1620. self.fd = fd
  1621. self.throw = throw
  1622. self.buf = b'Hello World.\nYou are so nice today.\nXX'
  1623. self.i = 0
  1624. def recv(self, bufsize, flags = 0):
  1625. if bufsize != 1:
  1626. raise Exception('bufsize != 1')
  1627. if flags != socket.MSG_DONTWAIT:
  1628. raise Exception('flags != socket.MSG_DONTWAIT')
  1629. # after 15 recv()s, throw a socket.error with EAGAIN to
  1630. # get _socket_data() to save back what's been read.
  1631. if self.throw and self.i > 15:
  1632. raise socket.error(errno.EAGAIN, 'Try again')
  1633. if self.i >= len(self.buf):
  1634. return b'';
  1635. t = self.i
  1636. self.i += 1
  1637. return self.buf[t:t+1]
  1638. def close(self):
  1639. return
  1640. class MockBobSocketData(BoB):
  1641. def __init__(self, throw):
  1642. self._unix_sockets = {42: (MockSock(42, throw), b'')}
  1643. self.requests = []
  1644. self.dead = []
  1645. def socket_request_handler(self, previous, sock):
  1646. self.requests.append({sock.fd: previous})
  1647. def socket_consumer_dead(self, sock):
  1648. self.dead.append(sock.fd)
  1649. # Case where we get data every time we call recv()
  1650. bob = MockBobSocketData(False)
  1651. bob._socket_data(42)
  1652. self.assertEqual(bob.requests,
  1653. [{42: b'Hello World.'},
  1654. {42: b'You are so nice today.'}])
  1655. self.assertEqual(bob.dead, [42])
  1656. self.assertEqual({}, bob._unix_sockets)
  1657. # Case where socket.recv() raises EAGAIN. In this case, the
  1658. # routine is supposed to save what it has back to
  1659. # BoB._unix_sockets.
  1660. bob = MockBobSocketData(True)
  1661. bob._socket_data(42)
  1662. self.assertEqual(bob.requests, [{42: b'Hello World.'}])
  1663. self.assertFalse(bob.dead)
  1664. self.assertEqual(len(bob._unix_sockets), 1)
  1665. self.assertEqual(bob._unix_sockets[42][1], b'You')
  1666. def test_startup(self):
  1667. '''Test that BoB.startup() handles failures properly.'''
  1668. class MockBobStartup(BoB):
  1669. def __init__(self, throw):
  1670. self.throw = throw
  1671. self.started = False
  1672. self.killed = False
  1673. self.msgq_socket_file = None
  1674. self.curproc = 'myproc'
  1675. self.runnable = False
  1676. def start_all_components(self):
  1677. self.started = True
  1678. if self.throw:
  1679. raise Exception('Assume starting components has failed.')
  1680. def kill_started_components(self):
  1681. self.killed = True
  1682. # All is well case, where all components are started
  1683. # successfully. We check that the actual call to
  1684. # start_all_components() is made, and BoB.runnable is true.
  1685. bob = MockBobStartup(False)
  1686. r = bob.startup()
  1687. self.assertIsNone(r)
  1688. self.assertTrue(bob.started)
  1689. self.assertFalse(bob.killed)
  1690. self.assertTrue(bob.runnable)
  1691. self.assertEqual({}, bob.c_channel_env)
  1692. # Case where starting components fails. We check that
  1693. # kill_started_components() is called right after, and
  1694. # BoB.runnable is not modified.
  1695. bob = MockBobStartup(True)
  1696. r = bob.startup()
  1697. # r contains an error message
  1698. self.assertEqual(r, 'Unable to start myproc: Assume starting components has failed.')
  1699. self.assertTrue(bob.started)
  1700. self.assertTrue(bob.killed)
  1701. self.assertFalse(bob.runnable)
  1702. self.assertEqual({}, bob.c_channel_env)
  1703. # Check if msgq_socket_file is carried over
  1704. bob = MockBobStartup(False)
  1705. bob.msgq_socket_file = 'foo'
  1706. r = bob.startup()
  1707. self.assertEqual({'BIND10_MSGQ_SOCKET_FILE': 'foo'}, bob.c_channel_env)
  1708. class SocketSrvTest(unittest.TestCase):
  1709. """
  1710. This tests some methods of boss related to the unix domain sockets used
  1711. to transfer other sockets to applications.
  1712. """
  1713. def setUp(self):
  1714. """
  1715. Create the boss to test, testdata and backup some functions.
  1716. """
  1717. self.__boss = BoB()
  1718. self.__select_backup = bind10_src.select.select
  1719. self.__select_called = None
  1720. self.__socket_data_called = None
  1721. self.__consumer_dead_called = None
  1722. self.__socket_request_handler_called = None
  1723. def tearDown(self):
  1724. """
  1725. Restore functions.
  1726. """
  1727. bind10_src.select.select = self.__select_backup
  1728. class __FalseSocket:
  1729. """
  1730. A mock socket for the select and accept and stuff like that.
  1731. """
  1732. def __init__(self, owner, fileno=42):
  1733. self.__owner = owner
  1734. self.__fileno = fileno
  1735. self.data = None
  1736. self.closed = False
  1737. def fileno(self):
  1738. return self.__fileno
  1739. def accept(self):
  1740. return (self.__class__(self.__owner, 13), "/path/to/socket")
  1741. def recv(self, bufsize, flags=0):
  1742. self.__owner.assertEqual(1, bufsize)
  1743. self.__owner.assertEqual(socket.MSG_DONTWAIT, flags)
  1744. if isinstance(self.data, socket.error):
  1745. raise self.data
  1746. elif self.data is not None:
  1747. if len(self.data):
  1748. result = self.data[0:1]
  1749. self.data = self.data[1:]
  1750. return result
  1751. else:
  1752. raise socket.error(errno.EAGAIN, "Would block")
  1753. else:
  1754. return b''
  1755. def close(self):
  1756. self.closed = True
  1757. class __CCS:
  1758. """
  1759. A mock CCS, just to provide the socket file number.
  1760. """
  1761. class __Socket:
  1762. def fileno(self):
  1763. return 1
  1764. def get_socket(self):
  1765. return self.__Socket()
  1766. def __select_accept(self, r, w, x, t):
  1767. self.__select_called = (r, w, x, t)
  1768. return ([42], [], [])
  1769. def __select_data(self, r, w, x, t):
  1770. self.__select_called = (r, w, x, t)
  1771. return ([13], [], [])
  1772. def __accept(self):
  1773. """
  1774. Hijact the accept method of the boss.
  1775. Notes down it was called and stops the boss.
  1776. """
  1777. self.__accept_called = True
  1778. self.__boss.runnable = False
  1779. def test_srv_accept_called(self):
  1780. """
  1781. Test that the _srv_accept method of boss is called when the listening
  1782. socket is readable.
  1783. """
  1784. self.__boss.runnable = True
  1785. self.__boss._srv_socket = self.__FalseSocket(self)
  1786. self.__boss._srv_accept = self.__accept
  1787. self.__boss.ccs = self.__CCS()
  1788. bind10_src.select.select = self.__select_accept
  1789. self.__boss.run(2)
  1790. # It called the accept
  1791. self.assertTrue(self.__accept_called)
  1792. # And the select had the right parameters
  1793. self.assertEqual(([2, 1, 42], [], [], None), self.__select_called)
  1794. def test_srv_accept(self):
  1795. """
  1796. Test how the _srv_accept method works.
  1797. """
  1798. self.__boss._srv_socket = self.__FalseSocket(self)
  1799. self.__boss._srv_accept()
  1800. # After we accepted, a new socket is added there
  1801. socket = self.__boss._unix_sockets[13][0]
  1802. # The socket is properly stored there
  1803. self.assertTrue(isinstance(socket, self.__FalseSocket))
  1804. # And the buffer (yet empty) is there
  1805. self.assertEqual({13: (socket, b'')}, self.__boss._unix_sockets)
  1806. def __socket_data(self, socket):
  1807. self.__boss.runnable = False
  1808. self.__socket_data_called = socket
  1809. def test_socket_data(self):
  1810. """
  1811. Test that a socket that wants attention gets it.
  1812. """
  1813. self.__boss._srv_socket = self.__FalseSocket(self)
  1814. self.__boss._socket_data = self.__socket_data
  1815. self.__boss.ccs = self.__CCS()
  1816. self.__boss._unix_sockets = {13: (self.__FalseSocket(self, 13), b'')}
  1817. self.__boss.runnable = True
  1818. bind10_src.select.select = self.__select_data
  1819. self.__boss.run(2)
  1820. self.assertEqual(13, self.__socket_data_called)
  1821. self.assertEqual(([2, 1, 42, 13], [], [], None), self.__select_called)
  1822. def __prepare_data(self, data):
  1823. socket = self.__FalseSocket(self, 13)
  1824. self.__boss._unix_sockets = {13: (socket, b'')}
  1825. socket.data = data
  1826. self.__boss.socket_consumer_dead = self.__consumer_dead
  1827. self.__boss.socket_request_handler = self.__socket_request_handler
  1828. return socket
  1829. def __consumer_dead(self, socket):
  1830. self.__consumer_dead_called = socket
  1831. def __socket_request_handler(self, token, socket):
  1832. self.__socket_request_handler_called = (token, socket)
  1833. def test_socket_closed(self):
  1834. """
  1835. Test that a socket is removed and the socket_consumer_dead is called
  1836. when it is closed.
  1837. """
  1838. socket = self.__prepare_data(None)
  1839. self.__boss._socket_data(13)
  1840. self.assertEqual(socket, self.__consumer_dead_called)
  1841. self.assertEqual({}, self.__boss._unix_sockets)
  1842. self.assertTrue(socket.closed)
  1843. def test_socket_short(self):
  1844. """
  1845. Test that if there's not enough data to get the whole socket, it is
  1846. kept there, but nothing is called.
  1847. """
  1848. socket = self.__prepare_data(b'tok')
  1849. self.__boss._socket_data(13)
  1850. self.assertEqual({13: (socket, b'tok')}, self.__boss._unix_sockets)
  1851. self.assertFalse(socket.closed)
  1852. self.assertIsNone(self.__consumer_dead_called)
  1853. self.assertIsNone(self.__socket_request_handler_called)
  1854. def test_socket_continue(self):
  1855. """
  1856. Test that we call the token handling function when the whole token
  1857. comes. This test pretends to continue reading where the previous one
  1858. stopped.
  1859. """
  1860. socket = self.__prepare_data(b"en\nanothe")
  1861. # The data to finish
  1862. self.__boss._unix_sockets[13] = (socket, b'tok')
  1863. self.__boss._socket_data(13)
  1864. self.assertEqual({13: (socket, b'anothe')}, self.__boss._unix_sockets)
  1865. self.assertFalse(socket.closed)
  1866. self.assertIsNone(self.__consumer_dead_called)
  1867. self.assertEqual((b'token', socket),
  1868. self.__socket_request_handler_called)
  1869. def test_broken_socket(self):
  1870. """
  1871. If the socket raises an exception during the read other than EAGAIN,
  1872. it is broken and we remove it.
  1873. """
  1874. sock = self.__prepare_data(socket.error(errno.ENOMEM,
  1875. "There's more memory available, but not for you"))
  1876. self.__boss._socket_data(13)
  1877. self.assertEqual(sock, self.__consumer_dead_called)
  1878. self.assertEqual({}, self.__boss._unix_sockets)
  1879. self.assertTrue(sock.closed)
  1880. class TestFunctions(unittest.TestCase):
  1881. def setUp(self):
  1882. self.lockfile_testpath = \
  1883. "@abs_top_builddir@/src/bin/bind10/tests/lockfile_test"
  1884. self.assertFalse(os.path.exists(self.lockfile_testpath))
  1885. os.mkdir(self.lockfile_testpath)
  1886. self.assertTrue(os.path.isdir(self.lockfile_testpath))
  1887. def tearDown(self):
  1888. os.rmdir(self.lockfile_testpath)
  1889. self.assertFalse(os.path.isdir(self.lockfile_testpath))
  1890. os.environ["B10_LOCKFILE_DIR_FROM_BUILD"] = "@abs_top_builddir@"
  1891. def test_remove_lock_files(self):
  1892. os.environ["B10_LOCKFILE_DIR_FROM_BUILD"] = self.lockfile_testpath
  1893. # create lockfiles for the testcase
  1894. lockfiles = ["logger_lockfile"]
  1895. for f in lockfiles:
  1896. fname = os.environ["B10_LOCKFILE_DIR_FROM_BUILD"] + '/' + f
  1897. self.assertFalse(os.path.exists(fname))
  1898. open(fname, "w").close()
  1899. self.assertTrue(os.path.isfile(fname))
  1900. # first call should clear up all the lockfiles
  1901. bind10_src.remove_lock_files()
  1902. # check if the lockfiles exist
  1903. for f in lockfiles:
  1904. fname = os.environ["B10_LOCKFILE_DIR_FROM_BUILD"] + '/' + f
  1905. self.assertFalse(os.path.isfile(fname))
  1906. # second call should not assert anyway
  1907. bind10_src.remove_lock_files()
  1908. def test_get_signame(self):
  1909. # just test with some samples
  1910. signame = bind10_src.get_signame(signal.SIGTERM)
  1911. self.assertEqual('SIGTERM', signame)
  1912. signame = bind10_src.get_signame(signal.SIGKILL)
  1913. self.assertEqual('SIGKILL', signame)
  1914. # 59426 is hopefully an unused signal on most platforms
  1915. signame = bind10_src.get_signame(59426)
  1916. self.assertEqual('Unknown signal 59426', signame)
  1917. def test_fatal_signal(self):
  1918. self.assertIsNone(bind10_src.boss_of_bind)
  1919. bind10_src.boss_of_bind = BoB()
  1920. bind10_src.boss_of_bind.runnable = True
  1921. bind10_src.fatal_signal(signal.SIGTERM, None)
  1922. # Now, runnable must be False
  1923. self.assertFalse(bind10_src.boss_of_bind.runnable)
  1924. bind10_src.boss_of_bind = None
  1925. if __name__ == '__main__':
  1926. # store os.environ for test_unchanged_environment
  1927. original_os_environ = copy.deepcopy(os.environ)
  1928. isc.log.resetUnitTestRootLogger()
  1929. unittest.main()