BIND 10 trac1503, updated. 848be350a53e835929a536d295d0fc3ee12a2376 [1503] Added several new dhcp4 tests (python)
BIND 10 source code commits
bind10-changes at lists.isc.org
Wed May 23 13:08:21 UTC 2012
The branch, trac1503 has been updated
via 848be350a53e835929a536d295d0fc3ee12a2376 (commit)
via 1b5e0412294dcf0bc499c4fd288db0f6d88c4ba5 (commit)
from 9cffd7d9f5dc6c9c2909f9569c2f4c78aa71bdbd (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit 848be350a53e835929a536d295d0fc3ee12a2376
Author: Tomek Mrugalski <tomasz at isc.org>
Date: Wed May 23 13:10:40 2012 +0300
[1503] Added several new dhcp4 tests (python)
commit 1b5e0412294dcf0bc499c4fd288db0f6d88c4ba5
Author: Tomek Mrugalski <tomasz at isc.org>
Date: Wed May 23 12:49:21 2012 +0300
[1503] DHCPv4 Python tests rewritten.
-----------------------------------------------------------------------
Summary of changes:
src/bin/dhcp4/tests/dhcp4_test.py | 128 +++++++++++++++++++++++++++++--------
1 file changed, 102 insertions(+), 26 deletions(-)
-----------------------------------------------------------------------
diff --git a/src/bin/dhcp4/tests/dhcp4_test.py b/src/bin/dhcp4/tests/dhcp4_test.py
index 4071f7e..3f75e69 100644
--- a/src/bin/dhcp4/tests/dhcp4_test.py
+++ b/src/bin/dhcp4/tests/dhcp4_test.py
@@ -26,48 +26,124 @@ import isc
class TestDhcpv4Daemon(unittest.TestCase):
def setUp(self):
- print("Note: Purpose of some of the tests is to check if DHCPv4 server can be started,")
- print(" not that is can bind sockets correctly. Please ignore binding errors.")
+
+ # don't redirect stdout/stderr here as we want to print out things
+ # during the test
+ pass
+
+ def tearDown(self):
+ pass
+
+ def runDhcp4(self, params):
+ """
+ This method runs dhcp4 and returns a touple: (returncode, stdout, stderr)
+ """
+ ## @todo: Convert this into generic method and reuse it in dhcp6
+
+ print("Running command: %s" % (" ".join(params)))
+
# redirect stdout to a pipe so we can check that our
# process spawning is doing the right thing with stdout
- self.old_stdout = os.dup(sys.stdout.fileno())
- self.pipes = os.pipe()
- os.dup2(self.pipes[1], sys.stdout.fileno())
- os.close(self.pipes[1])
+ self.stdout_old = os.dup(sys.stdout.fileno())
+ self.stdout_pipes = os.pipe()
+ os.dup2(self.stdout_pipes[1], sys.stdout.fileno())
+ os.close(self.stdout_pipes[1])
+
+ # do the same trick for stderr:
+ self.stderr_old = os.dup(sys.stderr.fileno())
+ self.stderr_pipes = os.pipe()
+ os.dup2(self.stderr_pipes[1], sys.stderr.fileno())
+ os.close(self.stderr_pipes[1])
+
# note that we use dup2() to restore the original stdout
# to the main program ASAP in each test... this prevents
# hangs reading from the child process (as the pipe is only
# open in the child), and also insures nice pretty output
- def tearDown(self):
- # clean up our stdout munging
- os.dup2(self.old_stdout, sys.stdout.fileno())
- os.close(self.pipes[0])
-
- def test_alive(self):
- """
- Simple test. Checks that b10-dhcp4 can be started and prints out info
- about starting DHCPv4 operation.
- """
- pi = ProcessInfo('Test Process', [ '../b10-dhcp4' , '-v' ])
+ pi = ProcessInfo('Test Process', params)
pi.spawn()
time.sleep(1)
- os.dup2(self.old_stdout, sys.stdout.fileno())
+ os.dup2(self.stdout_old, sys.stdout.fileno())
self.assertNotEqual(pi.process, None)
self.assertTrue(type(pi.pid) is int)
- output = os.read(self.pipes[0], 4096)
- self.assertEqual( str(output).count("[b10-dhcp4] Initiating DHCPv4 server operation."), 1)
- # kill this process
- # XXX: b10-dhcp6 is too dumb to understand 'shutdown' command for now,
- # so let's just kill the bastard
+ # There's potential problem if b10-dhcp4 prints out more
+ # than 4k of text
+ output = os.read(self.stdout_pipes[0], 4096)
+ error = os.read(self.stderr_pipes[0], 4096)
+
+ if (output is None):
+ output = ""
+ if (error is None):
+ error = ""
- # TODO: Ignore errors for now. This test will be more thorough once ticket #1503
- # (passing port number to b10-dhcp6 daemon) is implemented.
try:
- os.kill(pi.pid, signal.SIGTERM)
+ if (not pi.process.poll()):
+ # let's be nice at first...
+ pi.process.send_signal(signal.SIGTERM)
+
+ # give the process time to die out gracefully
+ time.sleep(1)
+ if (pi.process.returncode == None):
+ # If the suspect does not cooperate, use a bigger hammer.
+ os.kill(pi.pid, signal.SIGKILL)
except OSError:
print("Ignoring failed kill attempt. Process is dead already.")
+ # Clean up our stdout/stderr munging.
+ os.dup2(self.stdout_old, sys.stdout.fileno())
+ os.close(self.stdout_pipes[0])
+
+ os.dup2(self.stderr_old, sys.stderr.fileno())
+ os.close(self.stderr_pipes[0])
+
+ print ("Process finished, return code=%d, stdout=%d bytes, stderr=%d bytes"
+ % (pi.process.returncode, len(output), len(error)) )
+
+ return (pi.process.returncode, output, error)
+
+ def test_alive(self):
+ print("Note: Purpose of some of the tests is to check if DHCPv4 server can be started,")
+ print(" not that is can bind sockets correctly. Please ignore binding errors.")
+
+ (returncode, output, error) = self.runDhcp4(["../b10-dhcp4", "-v"])
+
+ self.assertEqual( str(output).count("[b10-dhcp4] Initiating DHCPv4 server operation."), 1)
+
+ def test_portnumber_0(self):
+ print("Check that specifying port number 0 is not allowed.")
+
+ (returncode, output, error) = self.runDhcp4(['../b10-dhcp4', '-p', '0'])
+
+ # When invalid port number is specified, return code must not be success
+ self.assertTrue(returncode != 0)
+
+ # Check that there is an error message about invalid port number printed on stderr
+ self.assertEqual( str(error).count("Failed to parse port number"), 1)
+
+ def test_portnumber_missing(self):
+ print("Check that -p option requires a parameter.")
+
+ (returncode, output, error) = self.runDhcp4(['../b10-dhcp4', '-p'])
+
+ # When invalid port number is specified, return code must not be success
+ self.assertTrue(returncode != 0)
+
+ # Check that there is an error message about invalid port number printed on stderr
+ self.assertEqual( str(error).count("option requires an argument"), 1)
+
+ def test_portnumber_nonroot(self):
+ print("Check that specifying unprivilidged port number will work.")
+
+ (returncode, output, error) = self.runDhcp4(['../b10-dhcp4', '-p', '10057'])
+
+ # When invalid port number is specified, return code must not be success
+ # TODO: Temporarily commented out as socket binding on systems that do not have
+ # interface detection implemented currently fails.
+ # self.assertTrue(returncode == 0)
+
+ # Check that there is an error message about invalid port number printed on stderr
+ self.assertEqual( str(output).count("opening sockets on port 10057"), 1)
+
if __name__ == '__main__':
unittest.main()
More information about the bind10-changes
mailing list