dhcp6_test.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_src import ProcessInfo, parse_args, dump_pid, unlink_pid_file, _BASETIME
  16. import unittest
  17. import sys
  18. import os
  19. import signal
  20. import socket
  21. from isc.net.addr import IPAddr
  22. import time
  23. import isc
  24. class TestDhcpv6Daemon(unittest.TestCase):
  25. def setUp(self):
  26. # redirect stdout to a pipe so we can check that our
  27. # process spawning is doing the right thing with stdout
  28. self.old_stdout = os.dup(sys.stdout.fileno())
  29. self.pipes = os.pipe()
  30. os.dup2(self.pipes[1], sys.stdout.fileno())
  31. os.close(self.pipes[1])
  32. # note that we use dup2() to restore the original stdout
  33. # to the main program ASAP in each test... this prevents
  34. # hangs reading from the child process (as the pipe is only
  35. # open in the child), and also insures nice pretty output
  36. def tearDown(self):
  37. # clean up our stdout munging
  38. os.dup2(self.old_stdout, sys.stdout.fileno())
  39. os.close(self.pipes[0])
  40. def test_alive(self):
  41. """
  42. Simple test. Checks that b10-dhcp6 can be started and prints out info
  43. about starting DHCPv6 operation.
  44. """
  45. pi = ProcessInfo('Test Process', [ '../b10-dhcp6' , '-v' ])
  46. pi.spawn()
  47. time.sleep(1)
  48. os.dup2(self.old_stdout, sys.stdout.fileno())
  49. self.assertNotEqual(pi.process, None)
  50. self.assertTrue(type(pi.pid) is int)
  51. output = os.read(self.pipes[0], 4096)
  52. self.assertEqual( str(output).count("[b10-dhcp6] Initiating DHCPv6 operation."), 1)
  53. # kill this process
  54. # XXX: b10-dhcp6 is too dumb to understand 'shutdown' command for now,
  55. # so let's just kill the bastard
  56. # TODO: Ignore errors for now. This test will be more thorough once ticket #1503
  57. # (passing port number to b10-dhcp6 daemon) is implemented.
  58. try:
  59. os.kill(pi.pid, signal.SIGTERM)
  60. except OSError:
  61. print("Ignoring failed kill attempt. Process is dead already.")
  62. if __name__ == '__main__':
  63. unittest.main()