Browse Source

[1175] modify b10-stats-httpd_test.py, b10-stats_test.py and
test_utils.py

- more strictly close the io object whether it's successfully opened
or not

- add verbosity=2 in unittest.main for debugging the failure in the
buildbot

- don't redict sys.stderr in MockMsgq

- rename the function name to create_specfile

- switch the verbose in Msgq into True

Naoki Kambe 13 years ago
parent
commit
433381e5ca

+ 20 - 15
src/bin/stats/tests/b10-stats-httpd_test.py

@@ -63,29 +63,34 @@ def get_availaddr(address='127.0.0.1', port=8001):
     """returns tuple of address and port available to listen on the
     """returns tuple of address and port available to listen on the
     platform. Default port range is between 8001 and 65535. If port is
     platform. Default port range is between 8001 and 65535. If port is
     over flow(greater than 65535), OverflowError is thrown"""
     over flow(greater than 65535), OverflowError is thrown"""
-    while True:
+    sock = None
-        try:
+    if is_ipv6_enabled(address):
-            if is_ipv6_enabled(address):
+        sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
-                sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
+    else:
-            else :
+        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    try:
-            sock.bind((address, port))
+        while True:
-            sock.close()
+            try:
-            return (address, port)
+                sock.bind((address, port))
-        except socket.error:
+                return (address, port)
-            # This address and port number are already in use.
+            except socket.error:
-            # next port number is added
+                # This address and port number are already in use.
-            port = port + 1
+                # next port number is added
+                port = port + 1
+    finally:
+        if sock: sock.close()
 
 
 def is_ipv6_enabled(address='::1', port=8000):
 def is_ipv6_enabled(address='::1', port=8000):
     """checks IPv6 enabled on the platform"""
     """checks IPv6 enabled on the platform"""
+    sock = None
     try:
     try:
         sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
         sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
         sock.bind((address, port))
         sock.bind((address, port))
-        sock.close()
         return True
         return True
     except socket.error:
     except socket.error:
         return False
         return False
+    finally:
+        if sock: sock.close()
 
 
 class TestHttpHandler(unittest.TestCase):
 class TestHttpHandler(unittest.TestCase):
     """Tests for HttpHandler class"""
     """Tests for HttpHandler class"""
@@ -698,4 +703,4 @@ class TestStatsHttpd(unittest.TestCase):
             imp.reload(stats_httpd)
             imp.reload(stats_httpd)
 
 
 if __name__ == "__main__":
 if __name__ == "__main__":
-    unittest.main()
+    unittest.main(verbosity=2)

+ 1 - 1
src/bin/stats/tests/b10-stats_test.py

@@ -603,7 +603,7 @@ class TestOSEnv(unittest.TestCase):
         imp.reload(stats)
         imp.reload(stats)
 
 
 def test_main():
 def test_main():
-    unittest.main()
+    unittest.main(verbosity=2)
 
 
 if __name__ == "__main__":
 if __name__ == "__main__":
     test_main()
     test_main()

+ 18 - 15
src/bin/stats/tests/test_utils.py

@@ -60,7 +60,7 @@ class ThreadingServerManager:
 class MockMsgq:
 class MockMsgq:
     def __init__(self):
     def __init__(self):
         self._started = threading.Event()
         self._started = threading.Event()
-        self.msgq = msgq.MsgQ(verbose=False)
+        self.msgq = msgq.MsgQ(verbose=True)
         result = self.msgq.setup()
         result = self.msgq.setup()
         if result:
         if result:
             sys.exit("Error on Msgq startup: %s" % result)
             sys.exit("Error on Msgq startup: %s" % result)
@@ -68,10 +68,7 @@ class MockMsgq:
     def run(self):
     def run(self):
         self._started.set()
         self._started.set()
         try:
         try:
-            # any message is written to /dev/null
-            sys.stderr = open(os.devnull, "w")
             self.msgq.run()
             self.msgq.run()
-            sys.stderr.close()
         except Exception:
         except Exception:
             pass
             pass
         finally:
         finally:
@@ -254,24 +251,30 @@ class MyStatsHttpd(stats_httpd.StatsHttpd):
     def __init__(self, *server_address):
     def __init__(self, *server_address):
         self._started = threading.Event()
         self._started = threading.Event()
         if server_address:
         if server_address:
-            stats_httpd.SPECFILE_LOCATION = self.get_specfile(*server_address)
+            stats_httpd.SPECFILE_LOCATION = self.create_specfile(*server_address)
             try:
             try:
                 stats_httpd.StatsHttpd.__init__(self)
                 stats_httpd.StatsHttpd.__init__(self)
             finally:
             finally:
-                stats_httpd.SPECFILE_LOCATION.close()
+                if hasattr(stats_httpd.SPECFILE_LOCATION, "close"):
+                    stats_httpd.SPECFILE_LOCATION.close()
                 stats_httpd.SPECFILE_LOCATION = self.ORIG_SPECFILE_LOCATION
                 stats_httpd.SPECFILE_LOCATION = self.ORIG_SPECFILE_LOCATION
         else:
         else:
             stats_httpd.StatsHttpd.__init__(self)
             stats_httpd.StatsHttpd.__init__(self)
 
 
-    def get_specfile(self, *server_address):
+    def create_specfile(self, *server_address):
-        spec = json.load(open(self.ORIG_SPECFILE_LOCATION))
+        spec_io = open(self.ORIG_SPECFILE_LOCATION)
-        config = spec['module_spec']['config_data']
+        try:
-        for i in range(len(config)):
+            spec = json.load(spec_io)
-            if config[i]['item_name'] == 'listen_on':
+            spec_io.close()
-                config[i]['item_default'] = \
+            config = spec['module_spec']['config_data']
-                    [ dict(address=a[0], port=a[1]) for a in server_address ]
+            for i in range(len(config)):
-                break
+                if config[i]['item_name'] == 'listen_on':
-        return io.StringIO(json.dumps(spec))
+                    config[i]['item_default'] = \
+                        [ dict(address=a[0], port=a[1]) for a in server_address ]
+                    break
+            return io.StringIO(json.dumps(spec))
+        finally:
+            spec_io.close()
 
 
     def run(self):
     def run(self):
         self._started.set()
         self._started.set()