Переглянути джерело

Remove extra except clause from cleanup() and add tests

Mike Naberezny 10 роки тому
батько
коміт
19a7d9d536
2 змінених файлів з 52 додано та 18 видалено
  1. 9 12
      supervisor/options.py
  2. 43 6
      supervisor/tests/test_options.py

+ 9 - 12
supervisor/options.py

@@ -1134,19 +1134,16 @@ class ServerOptions(Options):
             self.logger.info('supervisord started with pid %s' % pid)
 
     def cleanup(self):
+        for config, server in self.httpservers:
+            if config['family'] == socket.AF_UNIX:
+                if self.unlink_socketfiles:
+                    socketname = config['file']
+                    self._try_unlink(socketname)
+        self._try_unlink(self.pidfile)
+
+    def _try_unlink(self, path):
         try:
-            for config, server in self.httpservers:
-                if config['family'] == socket.AF_UNIX:
-                    if self.unlink_socketfiles:
-                        socketname = config['file']
-                        try:
-                            os.unlink(socketname)
-                        except OSError:
-                            pass
-        except OSError:
-            pass
-        try:
-            os.unlink(self.pidfile)
+            os.unlink(path)
         except OSError:
             pass
 

+ 43 - 6
supervisor/tests/test_options.py

@@ -910,9 +910,6 @@ class ServerOptionsTests(unittest.TestCase):
         with open(fn, 'w') as f:
             f.write('foo')
         instance = self._makeOne()
-        class Port:
-            family = socket.AF_UNIX
-            address = fn
         class Server:
             pass
         instance.httpservers = [({'family':socket.AF_UNIX, 'file':fn},
@@ -927,9 +924,6 @@ class ServerOptionsTests(unittest.TestCase):
             with open(fn, 'w') as f:
                 f.write('foo')
             instance = self._makeOne()
-            class Port:
-                family = socket.AF_UNIX
-                address = fn
             class Server:
                 pass
             instance.httpservers = [({'family':socket.AF_UNIX, 'file':fn},
@@ -944,6 +938,49 @@ class ServerOptionsTests(unittest.TestCase):
             except OSError:
                 pass
 
+    def test_cleanup_afunix_ignores_oserror_enoent(self):
+        notfound = os.path.join(os.path.dirname(__file__), 'notfound')
+        socketname = tempfile.mktemp()
+        try:
+            with open(socketname, 'w') as f:
+                f.write('foo')
+            instance = self._makeOne()
+            class Server:
+                pass
+            instance.httpservers = [
+                ({'family': socket.AF_UNIX, 'file': notfound}, Server()),
+                ({'family': socket.AF_UNIX, 'file': socketname}, Server()),
+            ]
+            instance.pidfile = ''
+            instance.cleanup()
+            self.assertFalse(os.path.exists(socketname))
+        finally:
+            try:
+                os.unlink(socketname)
+            except OSError:
+                pass
+
+    def test_cleanup_removes_pidfile(self):
+        pidfile = tempfile.mktemp()
+        try:
+            with open(pidfile, 'w') as f:
+                f.write('2')
+            instance = self._makeOne()
+            instance.pidfile = pidfile
+            instance.cleanup()
+            self.assertFalse(os.path.exists(pidfile))
+        finally:
+            try:
+                os.unlink(pidfile)
+            except OSError:
+                pass
+
+    def test_cleanup_pidfile_ignores_oserror_enoent(self):
+        notfound = os.path.join(os.path.dirname(__file__), 'notfound')
+        instance = self._makeOne()
+        instance.pidfile = notfound
+        instance.cleanup() # shouldn't raise
+
     def test_close_httpservers(self):
         instance = self._makeOne()
         class Server: