bind10_test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from bind10 import ProcessInfo, BoB
  2. import unittest
  3. import sys
  4. import os
  5. import signal
  6. class TestProcessInfo(unittest.TestCase):
  7. def setUp(self):
  8. # redirect stdout to a pipe so we can check that our
  9. # process spawning is doing the right thing with stdout
  10. self.old_stdout = os.dup(sys.stdout.fileno())
  11. self.pipes = os.pipe()
  12. os.dup2(self.pipes[1], sys.stdout.fileno())
  13. os.close(self.pipes[1])
  14. # note that we use dup2() to restore the original stdout
  15. # to the main program ASAP in each test... this prevents
  16. # hangs reading from the child process (as the pipe is only
  17. # open in the child), and also insures nice pretty output
  18. def tearDown(self):
  19. # clean up our stdout munging
  20. os.dup2(self.old_stdout, sys.stdout.fileno())
  21. os.close(self.pipes[0])
  22. def test_init(self):
  23. pi = ProcessInfo('Test Process', [ '/bin/echo', 'foo' ])
  24. os.dup2(self.old_stdout, sys.stdout.fileno())
  25. self.assertEqual(pi.name, 'Test Process')
  26. self.assertEqual(pi.args, [ '/bin/echo', 'foo' ])
  27. self.assertEqual(pi.env, { 'PATH': os.environ['PATH'],
  28. 'PYTHON_EXEC': os.environ['PYTHON_EXEC'] })
  29. self.assertEqual(pi.dev_null_stdout, False)
  30. self.assertEqual(os.read(self.pipes[0], 100), b"foo\n")
  31. self.assertNotEqual(pi.process, None)
  32. self.assertTrue(type(pi.pid) is int)
  33. def test_setting_env(self):
  34. pi = ProcessInfo('Test Process', [ '/bin/true' ], env={'FOO': 'BAR'})
  35. os.dup2(self.old_stdout, sys.stdout.fileno())
  36. self.assertEqual(pi.env, { 'PATH': os.environ['PATH'],
  37. 'PYTHON_EXEC': os.environ['PYTHON_EXEC'],
  38. 'FOO': 'BAR' })
  39. def test_setting_null_stdout(self):
  40. pi = ProcessInfo('Test Process', [ '/bin/echo', 'foo' ],
  41. dev_null_stdout=True)
  42. os.dup2(self.old_stdout, sys.stdout.fileno())
  43. self.assertEqual(pi.dev_null_stdout, True)
  44. self.assertEqual(os.read(self.pipes[0], 100), b"")
  45. def test_respawn(self):
  46. pi = ProcessInfo('Test Process', [ '/bin/echo', 'foo' ])
  47. # wait for old process to work...
  48. self.assertEqual(os.read(self.pipes[0], 100), b"foo\n")
  49. # respawn it
  50. old_pid = pi.pid
  51. pi.respawn()
  52. os.dup2(self.old_stdout, sys.stdout.fileno())
  53. # make sure the new one started properly
  54. self.assertEqual(pi.name, 'Test Process')
  55. self.assertEqual(pi.args, [ '/bin/echo', 'foo' ])
  56. self.assertEqual(pi.env, { 'PATH': os.environ['PATH'],
  57. 'PYTHON_EXEC': os.environ['PYTHON_EXEC'] })
  58. self.assertEqual(pi.dev_null_stdout, False)
  59. self.assertEqual(os.read(self.pipes[0], 100), b"foo\n")
  60. self.assertNotEqual(pi.process, None)
  61. self.assertTrue(type(pi.pid) is int)
  62. self.assertNotEqual(pi.pid, old_pid)
  63. class TestBoB(unittest.TestCase):
  64. def test_init(self):
  65. bob = BoB()
  66. self.assertEqual(bob.verbose, False)
  67. self.assertEqual(bob.c_channel_port, 9912)
  68. self.assertEqual(bob.cc_session, None)
  69. self.assertEqual(bob.processes, {})
  70. self.assertEqual(bob.dead_processes, {})
  71. self.assertEqual(bob.runnable, False)
  72. def test_init_alternate_port(self):
  73. bob = BoB(2199)
  74. self.assertEqual(bob.verbose, False)
  75. self.assertEqual(bob.c_channel_port, 2199)
  76. self.assertEqual(bob.cc_session, None)
  77. self.assertEqual(bob.processes, {})
  78. self.assertEqual(bob.dead_processes, {})
  79. self.assertEqual(bob.runnable, False)
  80. # verbose testing...
  81. if __name__ == '__main__':
  82. unittest.main()