Parcourir la 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 il y a 13 ans
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
     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)
-            else :
-                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-            sock.bind((address, port))
-            sock.close()
-            return (address, port)
-        except socket.error:
-            # This address and port number are already in use.
-            # next port number is added
-            port = port + 1
+    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:
+            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))
-        sock.close()
         return True
     except socket.error:
         return False
+    finally:
+        if sock: sock.close()
 
 class TestHttpHandler(unittest.TestCase):
     """Tests for HttpHandler class"""
@@ -698,4 +703,4 @@ class TestStatsHttpd(unittest.TestCase):
             imp.reload(stats_httpd)
 
 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)
 
 def test_main():
-    unittest.main()
+    unittest.main(verbosity=2)
 
 if __name__ == "__main__":
     test_main()

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

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