Browse Source

[1175] modify stats_httpd.py.in and b10-stats-httpd_test.py

 - A hostname (canonical name of host) is not acceptable in listen_on
   configuration.

 - A default port number(starting number for search) is added in args
   of the function get_availaddr.
Naoki Kambe 13 years ago
parent
commit
9daa2f686b
2 changed files with 17 additions and 15 deletions
  1. 6 2
      src/bin/stats/stats_httpd.py.in
  2. 11 13
      src/bin/stats/tests/b10-stats-httpd_test.py

+ 6 - 2
src/bin/stats/stats_httpd.py.in

@@ -209,8 +209,12 @@ class StatsHttpd:
         httpd = None
         try:
             # get address family for the server_address before
-            # creating HttpServer object
-            address_family = socket.getaddrinfo(*server_address)[0][0]
+            # creating HttpServer object. If a specified address is
+            # not numerical, gaierror may be thrown.
+            address_family = socket.getaddrinfo(
+                server_address[0], server_address[1], 0,
+                socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST
+                )[0][0]
             HttpServer.address_family = address_family
             httpd = HttpServer(
                 server_address, HttpHandler,

+ 11 - 13
src/bin/stats/tests/b10-stats-httpd_test.py

@@ -58,10 +58,11 @@ DUMMY_DATA = {
         }
     }
 
-def get_availaddr(address='127.0.0.1'):
-    """returns tuple of address and port available on the
-    platform. default range of port is from 65535 to 50000"""
-    for port in range(65535, 50000, -1):
+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"""
+    while True:
         try:
             if is_ipv6_enabled(address):
                 sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
@@ -71,7 +72,9 @@ def get_availaddr(address='127.0.0.1'):
             sock.close()
             return (address, port)
         except socket.error:
-            pass
+            # This address and port number are already in use.
+            # next port number is added
+            port = port + 1
 
 def is_ipv6_enabled(address='::1', port=8000):
     """checks IPv6 enabled on the platform"""
@@ -403,14 +406,9 @@ class TestStatsHttpd(unittest.TestCase):
                 self.assertTrue(isinstance(ht.socket, socket.socket))
             self.stats_httpd.stop()
 
-        # hostname
-        server_addresses = get_availaddr(address='localhost')
-        self.stats_httpd = MyStatsHttpd(server_addresses)
-        for ht in self.stats_httpd.httpd:
-            self.assertTrue(isinstance(ht, stats_httpd.HttpServer))
-            self.assertTrue(ht.address_family in set([socket.AF_INET, socket.AF_INET6]))
-            self.assertTrue(isinstance(ht.socket, socket.socket))
-        self.stats_httpd.stop()
+        # existent hostname
+        self.assertRaises(stats_httpd.HttpServerError, MyStatsHttpd,
+                          get_availaddr(address='localhost'))
 
         # nonexistent hostname
         self.assertRaises(stats_httpd.HttpServerError, MyStatsHttpd, ('my.host.domain', 8000))