bind10_test.py.in 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. from bind10 import ProcessInfo, BoB, parse_args, dump_pid, unlink_pid_file
  16. # XXX: environment tests are currently disabled, due to the preprocessor
  17. # setup that we have now complicating the environment
  18. import unittest
  19. import sys
  20. import os
  21. import signal
  22. import socket
  23. from isc.net.addr import IPAddr
  24. from isc.testutils.parse_args import TestOptParser, OptsError
  25. class TestProcessInfo(unittest.TestCase):
  26. def setUp(self):
  27. # redirect stdout to a pipe so we can check that our
  28. # process spawning is doing the right thing with stdout
  29. self.old_stdout = os.dup(sys.stdout.fileno())
  30. self.pipes = os.pipe()
  31. os.dup2(self.pipes[1], sys.stdout.fileno())
  32. os.close(self.pipes[1])
  33. # note that we use dup2() to restore the original stdout
  34. # to the main program ASAP in each test... this prevents
  35. # hangs reading from the child process (as the pipe is only
  36. # open in the child), and also insures nice pretty output
  37. def tearDown(self):
  38. # clean up our stdout munging
  39. os.dup2(self.old_stdout, sys.stdout.fileno())
  40. os.close(self.pipes[0])
  41. def test_init(self):
  42. pi = ProcessInfo('Test Process', [ '/bin/echo', 'foo' ])
  43. pi.spawn()
  44. os.dup2(self.old_stdout, sys.stdout.fileno())
  45. self.assertEqual(pi.name, 'Test Process')
  46. self.assertEqual(pi.args, [ '/bin/echo', 'foo' ])
  47. # self.assertEqual(pi.env, { 'PATH': os.environ['PATH'],
  48. # 'PYTHON_EXEC': os.environ['PYTHON_EXEC'] })
  49. self.assertEqual(pi.dev_null_stdout, False)
  50. self.assertEqual(os.read(self.pipes[0], 100), b"foo\n")
  51. self.assertNotEqual(pi.process, None)
  52. self.assertTrue(type(pi.pid) is int)
  53. # def test_setting_env(self):
  54. # pi = ProcessInfo('Test Process', [ '/bin/true' ], env={'FOO': 'BAR'})
  55. # os.dup2(self.old_stdout, sys.stdout.fileno())
  56. # self.assertEqual(pi.env, { 'PATH': os.environ['PATH'],
  57. # 'PYTHON_EXEC': os.environ['PYTHON_EXEC'],
  58. # 'FOO': 'BAR' })
  59. def test_setting_null_stdout(self):
  60. pi = ProcessInfo('Test Process', [ '/bin/echo', 'foo' ],
  61. dev_null_stdout=True)
  62. pi.spawn()
  63. os.dup2(self.old_stdout, sys.stdout.fileno())
  64. self.assertEqual(pi.dev_null_stdout, True)
  65. self.assertEqual(os.read(self.pipes[0], 100), b"")
  66. def test_respawn(self):
  67. pi = ProcessInfo('Test Process', [ '/bin/echo', 'foo' ])
  68. pi.spawn()
  69. # wait for old process to work...
  70. self.assertEqual(os.read(self.pipes[0], 100), b"foo\n")
  71. # respawn it
  72. old_pid = pi.pid
  73. pi.respawn()
  74. os.dup2(self.old_stdout, sys.stdout.fileno())
  75. # make sure the new one started properly
  76. self.assertEqual(pi.name, 'Test Process')
  77. self.assertEqual(pi.args, [ '/bin/echo', 'foo' ])
  78. # self.assertEqual(pi.env, { 'PATH': os.environ['PATH'],
  79. # 'PYTHON_EXEC': os.environ['PYTHON_EXEC'] })
  80. self.assertEqual(pi.dev_null_stdout, False)
  81. self.assertEqual(os.read(self.pipes[0], 100), b"foo\n")
  82. self.assertNotEqual(pi.process, None)
  83. self.assertTrue(type(pi.pid) is int)
  84. self.assertNotEqual(pi.pid, old_pid)
  85. class TestBoB(unittest.TestCase):
  86. def test_init(self):
  87. bob = BoB()
  88. self.assertEqual(bob.verbose, False)
  89. self.assertEqual(bob.msgq_socket_file, None)
  90. self.assertEqual(bob.cc_session, None)
  91. self.assertEqual(bob.ccs, None)
  92. self.assertEqual(bob.processes, {})
  93. self.assertEqual(bob.dead_processes, {})
  94. self.assertEqual(bob.runnable, False)
  95. self.assertEqual(bob.uid, None)
  96. self.assertEqual(bob.username, None)
  97. self.assertEqual(bob.nocache, False)
  98. self.assertEqual(bob.cfg_start_auth, True)
  99. self.assertEqual(bob.cfg_start_resolver, False)
  100. def test_init_alternate_socket(self):
  101. bob = BoB("alt_socket_file")
  102. self.assertEqual(bob.verbose, False)
  103. self.assertEqual(bob.msgq_socket_file, "alt_socket_file")
  104. self.assertEqual(bob.cc_session, None)
  105. self.assertEqual(bob.ccs, None)
  106. self.assertEqual(bob.processes, {})
  107. self.assertEqual(bob.dead_processes, {})
  108. self.assertEqual(bob.runnable, False)
  109. self.assertEqual(bob.uid, None)
  110. self.assertEqual(bob.username, None)
  111. self.assertEqual(bob.nocache, False)
  112. self.assertEqual(bob.cfg_start_auth, True)
  113. self.assertEqual(bob.cfg_start_resolver, False)
  114. # Class for testing the BoB without actually starting processes.
  115. # This is used for testing the start/stop components routines and
  116. # the BoB commands.
  117. #
  118. # Testing that external processes start is outside the scope
  119. # of the unit test, by overriding the process start methods we can check
  120. # that the right processes are started depending on the configuration
  121. # options.
  122. class MockBob(BoB):
  123. def __init__(self):
  124. BoB.__init__(self)
  125. # Set flags as to which of the overridden methods has been run.
  126. self.msgq = False
  127. self.cfgmgr = False
  128. self.ccsession = False
  129. self.auth = False
  130. self.resolver = False
  131. self.xfrout = False
  132. self.xfrin = False
  133. self.zonemgr = False
  134. self.stats = False
  135. self.cmdctl = False
  136. self.c_channel_env = {}
  137. self.processes = { }
  138. def read_bind10_config(self):
  139. # Configuration options are set directly
  140. pass
  141. def start_msgq(self, c_channel_env):
  142. self.msgq = True
  143. self.processes[2] = ProcessInfo('b10-msgq', ['/bin/false'])
  144. def start_cfgmgr(self, c_channel_env):
  145. self.cfgmgr = True
  146. self.processes[3] = ProcessInfo('b10-cfgmgr', ['/bin/false'])
  147. def start_ccsession(self, c_channel_env):
  148. self.ccsession = True
  149. self.processes[4] = ProcessInfo('b10-ccsession', ['/bin/false'])
  150. def start_auth(self, c_channel_env):
  151. self.auth = True
  152. self.processes[5] = ProcessInfo('b10-auth', ['/bin/false'])
  153. def start_resolver(self, c_channel_env):
  154. self.resolver = True
  155. self.processes[6] = ProcessInfo('b10-resolver', ['/bin/false'])
  156. def start_xfrout(self, c_channel_env):
  157. self.xfrout = True
  158. self.processes[7] = ProcessInfo('b10-xfrout', ['/bin/false'])
  159. def start_xfrin(self, c_channel_env):
  160. self.xfrin = True
  161. self.processes[8] = ProcessInfo('b10-xfrin', ['/bin/false'])
  162. def start_zonemgr(self, c_channel_env):
  163. self.zonemgr = True
  164. self.processes[9] = ProcessInfo('b10-zonemgr', ['/bin/false'])
  165. def start_stats(self, c_channel_env):
  166. self.stats = True
  167. self.processes[10] = ProcessInfo('b10-stats', ['/bin/false'])
  168. def start_cmdctl(self, c_channel_env):
  169. self.cmdctl = True
  170. self.processes[11] = ProcessInfo('b10-cmdctl', ['/bin/false'])
  171. # We don't really use all of these stop_ methods. But it might turn out
  172. # someone would add some stop_ method to BoB and we want that one overriden
  173. # in case he forgets to update the tests.
  174. def stop_msgq(self):
  175. if self.msgq:
  176. del self.processes[2]
  177. self.msgq = False
  178. def stop_cfgmgr(self):
  179. if self.cfgmgr:
  180. del self.processes[3]
  181. self.cfgmgr = False
  182. def stop_ccsession(self):
  183. if self.ccssession:
  184. del self.processes[4]
  185. self.ccsession = False
  186. def stop_auth(self):
  187. if self.auth:
  188. del self.processes[5]
  189. self.auth = False
  190. def stop_resolver(self):
  191. if self.resolver:
  192. del self.processes[6]
  193. self.resolver = False
  194. def stop_xfrout(self):
  195. if self.xfrout:
  196. del self.processes[7]
  197. self.xfrout = False
  198. def stop_xfrin(self):
  199. if self.xfrin:
  200. del self.processes[8]
  201. self.xfrin = False
  202. def stop_zonemgr(self):
  203. if self.zonemgr:
  204. del self.processes[9]
  205. self.zonemgr = False
  206. def stop_stats(self):
  207. if self.stats:
  208. del self.processes[10]
  209. self.stats = False
  210. def stop_cmdctl(self):
  211. if self.cmdctl:
  212. del self.processes[11]
  213. self.cmdctl = False
  214. class TestStartStopProcessesBob(unittest.TestCase):
  215. """
  216. Check that the start_all_processes method starts the right combination
  217. of processes and that the right processes are started and stopped
  218. according to changes in configuration.
  219. """
  220. def check_started(self, bob, core, auth, resolver):
  221. """
  222. Check that the right sets of services are started. The ones that
  223. should be running are specified by the core, auth and resolver parameters
  224. (they are groups of processes, eg. auth means b10-auth, -xfrout, -xfrin
  225. and -zonemgr).
  226. """
  227. self.assertEqual(bob.msgq, core)
  228. self.assertEqual(bob.cfgmgr, core)
  229. self.assertEqual(bob.ccsession, core)
  230. self.assertEqual(bob.auth, auth)
  231. self.assertEqual(bob.resolver, resolver)
  232. self.assertEqual(bob.xfrout, auth)
  233. self.assertEqual(bob.xfrin, auth)
  234. self.assertEqual(bob.zonemgr, auth)
  235. self.assertEqual(bob.stats, core)
  236. self.assertEqual(bob.cmdctl, core)
  237. def check_preconditions(self, bob):
  238. self.check_started(bob, False, False, False)
  239. def check_started_none(self, bob):
  240. """
  241. Check that the situation is according to configuration where no servers
  242. should be started. Some processes still need to be running.
  243. """
  244. self.check_started(bob, True, False, False)
  245. def check_started_both(self, bob):
  246. """
  247. Check the situation is according to configuration where both servers
  248. (auth and resolver) are enabled.
  249. """
  250. self.check_started(bob, True, True, True)
  251. def check_started_auth(self, bob):
  252. """
  253. Check the set of processes needed to run auth only is started.
  254. """
  255. self.check_started(bob, True, True, False)
  256. def check_started_resolver(self, bob):
  257. """
  258. Check the set of processes needed to run resolver only is started.
  259. """
  260. self.check_started(bob, True, False, True)
  261. # Checks the processes started when starting neither auth nor resolver
  262. # is specified.
  263. def test_start_none(self):
  264. # Create BoB and ensure correct initialization
  265. bob = MockBob()
  266. self.check_preconditions(bob)
  267. # Start processes and check what was started
  268. bob.cfg_start_auth = False
  269. bob.cfg_start_resolver = False
  270. bob.start_all_processes()
  271. self.check_started_none(bob)
  272. # Checks the processes started when starting only the auth process
  273. def test_start_auth(self):
  274. # Create BoB and ensure correct initialization
  275. bob = MockBob()
  276. self.check_preconditions(bob)
  277. # Start processes and check what was started
  278. bob.cfg_start_auth = True
  279. bob.cfg_start_resolver = False
  280. bob.start_all_processes()
  281. self.check_started_auth(bob)
  282. # Checks the processes started when starting only the resolver process
  283. def test_start_resolver(self):
  284. # Create BoB and ensure correct initialization
  285. bob = MockBob()
  286. self.check_preconditions(bob)
  287. # Start processes and check what was started
  288. bob.cfg_start_auth = False
  289. bob.cfg_start_resolver = True
  290. bob.start_all_processes()
  291. self.check_started_resolver(bob)
  292. # Checks the processes started when starting both auth and resolver process
  293. def test_start_both(self):
  294. # Create BoB and ensure correct initialization
  295. bob = MockBob()
  296. self.check_preconditions(bob)
  297. # Start processes and check what was started
  298. bob.cfg_start_auth = True
  299. bob.cfg_start_resolver = True
  300. bob.start_all_processes()
  301. self.check_started_both(bob)
  302. def test_config_start(self):
  303. """
  304. Test that the configuration starts and stops processes according
  305. to configuration changes.
  306. """
  307. # Create BoB and ensure correct initialization
  308. bob = MockBob()
  309. self.check_preconditions(bob)
  310. # Start processes (nothing much should be started, as in
  311. # test_start_none)
  312. bob.cfg_start_auth = False
  313. bob.cfg_start_resolver = False
  314. bob.start_all_processes()
  315. bob.runnable = True
  316. self.check_started_none(bob)
  317. # Enable both at once
  318. bob.config_handler({'start_auth': True, 'start_resolver': True})
  319. self.check_started_both(bob)
  320. # Not touched by empty change
  321. bob.config_handler({})
  322. self.check_started_both(bob)
  323. # Not touched by change to the same configuration
  324. bob.config_handler({'start_auth': True, 'start_resolver': True})
  325. self.check_started_both(bob)
  326. # Turn them both off again
  327. bob.config_handler({'start_auth': False, 'start_resolver': False})
  328. self.check_started_none(bob)
  329. # Not touched by empty change
  330. bob.config_handler({})
  331. self.check_started_none(bob)
  332. # Not touched by change to the same configuration
  333. bob.config_handler({'start_auth': False, 'start_resolver': False})
  334. self.check_started_none(bob)
  335. # Start and stop auth separately
  336. bob.config_handler({'start_auth': True})
  337. self.check_started_auth(bob)
  338. bob.config_handler({'start_auth': False})
  339. self.check_started_none(bob)
  340. # Start and stop resolver separately
  341. bob.config_handler({'start_resolver': True})
  342. self.check_started_resolver(bob)
  343. bob.config_handler({'start_resolver': False})
  344. self.check_started_none(bob)
  345. # Alternate
  346. bob.config_handler({'start_auth': True})
  347. self.check_started_auth(bob)
  348. bob.config_handler({'start_auth': False, 'start_resolver': True})
  349. self.check_started_resolver(bob)
  350. bob.config_handler({'start_auth': True, 'start_resolver': False})
  351. self.check_started_auth(bob)
  352. def test_config_start_once(self):
  353. """
  354. Tests that a process is started only once.
  355. """
  356. # Create BoB and ensure correct initialization
  357. bob = MockBob()
  358. self.check_preconditions(bob)
  359. # Start processes (both)
  360. bob.cfg_start_auth = True
  361. bob.cfg_start_resolver = True
  362. bob.start_all_processes()
  363. bob.runnable = True
  364. self.check_started_both(bob)
  365. bob.start_auth = lambda: self.fail("Started auth again")
  366. bob.start_xfrout = lambda: self.fail("Started xfrout again")
  367. bob.start_xfrin = lambda: self.fail("Started xfrin again")
  368. bob.start_zonemgr = lambda: self.fail("Started zonemgr again")
  369. bob.start_resolver = lambda: self.fail("Started resolver again")
  370. # Send again we want to start them. Should not do it, as they are.
  371. bob.config_handler({'start_auth': True})
  372. bob.config_handler({'start_resolver': True})
  373. def test_config_not_started_early(self):
  374. """
  375. Test that processes are not started by the config handler before
  376. startup.
  377. """
  378. bob = MockBob()
  379. self.check_preconditions(bob)
  380. bob.start_auth = lambda: self.fail("Started auth again")
  381. bob.start_xfrout = lambda: self.fail("Started xfrout again")
  382. bob.start_xfrin = lambda: self.fail("Started xfrin again")
  383. bob.start_zonemgr = lambda: self.fail("Started zonemgr again")
  384. bob.start_resolver = lambda: self.fail("Started resolver again")
  385. bob.config_handler({'start_auth': True, 'start_resolver': True})
  386. class TestBossCmd(unittest.TestCase):
  387. def test_ping(self):
  388. """
  389. Confirm simple ping command works.
  390. """
  391. bob = MockBob()
  392. answer = bob.command_handler("ping", None)
  393. self.assertEqual(answer, {'result': [0, 'pong']})
  394. def test_show_processes(self):
  395. """
  396. Confirm getting a list of processes works.
  397. """
  398. bob = MockBob()
  399. answer = bob.command_handler("show_processes", None)
  400. self.assertEqual(answer, {'result': [0, []]})
  401. def test_show_processes_started(self):
  402. """
  403. Confirm getting a list of processes works.
  404. """
  405. bob = MockBob()
  406. bob.start_all_processes()
  407. answer = bob.command_handler("show_processes", None)
  408. processes = [[2, 'b10-msgq'],
  409. [3, 'b10-cfgmgr'],
  410. [4, 'b10-ccsession'],
  411. [5, 'b10-auth'],
  412. [7, 'b10-xfrout'],
  413. [8, 'b10-xfrin'],
  414. [9, 'b10-zonemgr'],
  415. [10, 'b10-stats'],
  416. [11, 'b10-cmdctl']]
  417. self.assertEqual(answer, {'result': [0, processes]})
  418. class TestParseArgs(unittest.TestCase):
  419. """
  420. This tests parsing of arguments of the bind10 master process.
  421. """
  422. #TODO: Write tests for the original parsing, bad options, etc.
  423. def test_no_opts(self):
  424. """
  425. Test correct default values when no options are passed.
  426. """
  427. options = parse_args([], TestOptParser)
  428. self.assertEqual(None, options.data_path)
  429. self.assertEqual(None, options.config_file)
  430. self.assertEqual(None, options.cmdctl_port)
  431. def test_data_path(self):
  432. """
  433. Test it can parse the data path.
  434. """
  435. self.assertRaises(OptsError, parse_args, ['-p'], TestOptParser)
  436. self.assertRaises(OptsError, parse_args, ['--data-path'],
  437. TestOptParser)
  438. options = parse_args(['-p', '/data/path'], TestOptParser)
  439. self.assertEqual('/data/path', options.data_path)
  440. options = parse_args(['--data-path=/data/path'], TestOptParser)
  441. self.assertEqual('/data/path', options.data_path)
  442. def test_config_filename(self):
  443. """
  444. Test it can parse the config switch.
  445. """
  446. self.assertRaises(OptsError, parse_args, ['-c'], TestOptParser)
  447. self.assertRaises(OptsError, parse_args, ['--config-file'],
  448. TestOptParser)
  449. options = parse_args(['-c', 'config-file'], TestOptParser)
  450. self.assertEqual('config-file', options.config_file)
  451. options = parse_args(['--config-file=config-file'], TestOptParser)
  452. self.assertEqual('config-file', options.config_file)
  453. def test_cmdctl_port(self):
  454. """
  455. Test it can parse the command control port.
  456. """
  457. self.assertRaises(OptsError, parse_args, ['--cmdctl-port=abc'],
  458. TestOptParser)
  459. self.assertRaises(OptsError, parse_args, ['--cmdctl-port=100000000'],
  460. TestOptParser)
  461. self.assertRaises(OptsError, parse_args, ['--cmdctl-port'],
  462. TestOptParser)
  463. options = parse_args(['--cmdctl-port=1234'], TestOptParser)
  464. self.assertEqual(1234, options.cmdctl_port)
  465. class TestPIDFile(unittest.TestCase):
  466. def setUp(self):
  467. self.pid_file = '@builddir@' + os.sep + 'bind10.pid'
  468. if os.path.exists(self.pid_file):
  469. os.unlink(self.pid_file)
  470. def tearDown(self):
  471. if os.path.exists(self.pid_file):
  472. os.unlink(self.pid_file)
  473. def check_pid_file(self):
  474. # dump PID to the file, and confirm the content is correct
  475. dump_pid(self.pid_file)
  476. my_pid = os.getpid()
  477. self.assertEqual(my_pid, int(open(self.pid_file, "r").read()))
  478. def test_dump_pid(self):
  479. self.check_pid_file()
  480. # make sure any existing content will be removed
  481. open(self.pid_file, "w").write('dummy data\n')
  482. self.check_pid_file()
  483. def test_unlink_pid_file_notexist(self):
  484. dummy_data = 'dummy_data\n'
  485. open(self.pid_file, "w").write(dummy_data)
  486. unlink_pid_file("no_such_pid_file")
  487. # the file specified for unlink_pid_file doesn't exist,
  488. # and the original content of the file should be intact.
  489. self.assertEqual(dummy_data, open(self.pid_file, "r").read())
  490. def test_dump_pid_with_none(self):
  491. # Check the behavior of dump_pid() and unlink_pid_file() with None.
  492. # This should be no-op.
  493. dump_pid(None)
  494. self.assertFalse(os.path.exists(self.pid_file))
  495. dummy_data = 'dummy_data\n'
  496. open(self.pid_file, "w").write(dummy_data)
  497. unlink_pid_file(None)
  498. self.assertEqual(dummy_data, open(self.pid_file, "r").read())
  499. def test_dump_pid_failure(self):
  500. # the attempt to open file will fail, which should result in exception.
  501. self.assertRaises(IOError, dump_pid,
  502. 'nonexistent_dir' + os.sep + 'bind10.pid')
  503. if __name__ == '__main__':
  504. unittest.main()