]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[1175] modify b10-stats-httpd_test.py
authorNaoki Kambe <kambe@jprs.co.jp>
Tue, 6 Sep 2011 11:17:57 +0000 (20:17 +0900)
committerNaoki Kambe <kambe@jprs.co.jp>
Mon, 3 Oct 2011 02:52:52 +0000 (11:52 +0900)
 - The function get_availaddr uses socket.getaddrinfo for getting the
   address family.

 - The function is_ipv6_enabled uses 3 random ports for checking
   whether IPv6 is enabled.

src/bin/stats/tests/b10-stats-httpd_test.py

index ccadeb20fd0365b3009f58874ed7e2dd430327ef..f0a49cf1a5f06c34320f34c24b3c2ca4974a638a 100644 (file)
@@ -33,6 +33,7 @@ import threading
 import http.client
 import xml.etree.ElementTree
 import signal
+import random
 
 import isc
 import stats_httpd
@@ -60,34 +61,36 @@ def get_availaddr(address='127.0.0.1', port=8001):
     """returns tuple of address and port available to listen on the
     platform. Default port range is between 8001 and 65535. If port is
     over flow(greater than 65535), OverflowError is thrown"""
-    sock = None
-    if is_ipv6_enabled(address):
-        sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
-    else:
-        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    try:
-        while True:
+    while True:
+        for addr in socket.getaddrinfo(
+            address, port, 0,
+            socket.SOCK_STREAM, socket.IPPROTO_TCP):
+            sock = socket.socket(addr[0], socket.SOCK_STREAM)
             try:
                 sock.bind((address, port))
                 return (address, port)
             except socket.error:
-                # This address and port number are already in use.
-                # next port number is added
-                port = port + 1
-    finally:
-        if sock: sock.close()
-
-def is_ipv6_enabled(address='::1', port=8000):
-    """checks IPv6 enabled on the platform"""
-    sock = None
-    try:
-        sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
-        sock.bind((address, port))
-        return True
-    except socket.error:
-        return False
-    finally:
-        if sock: sock.close()
+                continue
+            finally:
+                if sock: sock.close()
+        # This address and port number are already in use.
+        # next port number is added
+        port = port + 1
+
+def is_ipv6_enabled(address='::1', port=8001):
+    """checks IPv6 enabled on the platform. address for check is '::1'
+    and port for check is random number between 8001 and
+    65535. Retrying is 3 times even if it fails."""
+    for p in random.sample(range(port, 65535), 3):
+        try:
+            sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
+            sock.bind((address, p))
+            return True
+        except socket.error:
+            continue
+        finally:
+            if sock: sock.close()
+    raise Exception('hoge')
 
 class TestHttpHandler(unittest.TestCase):
     """Tests for HttpHandler class"""