浏览代码

Fix test failures when run as root

Conflicts:
	supervisor/options.py
	supervisor/tests/test_options.py
Mike Naberezny 10 年之前
父节点
当前提交
ca807d1361
共有 2 个文件被更改,包括 44 次插入68 次删除
  1. 13 13
      supervisor/options.py
  2. 31 55
      supervisor/tests/test_options.py

+ 13 - 13
supervisor/options.py

@@ -307,7 +307,7 @@ class Options:
 
     def process_config(self, do_usage=True):
         """Process configuration data structure.
-        
+
         This includes reading config file if necessary, setting defaults etc.
         """
         if self.configfile:
@@ -351,6 +351,12 @@ class Options:
                 # if this is called from an RPC method, raise an error
                 raise ValueError(msg)
 
+    def exists(self, path):
+        return os.path.exists(path)
+
+    def open(self, fn, mode='r'):
+        return open(fn, mode)
+
     def get_plugins(self, parser, factory_key, section_prefix):
         factories = []
 
@@ -518,10 +524,10 @@ class ServerOptions(Options):
 
         section = self.configroot.supervisord
         if not hasattr(fp, 'read'):
-            if not os.path.exists(fp):
+            if not self.exists(fp):
                 raise ValueError("could not find config file %s" % fp)
             try:
-                fp = open(fp, 'r')
+                fp = self.open(fp, 'r')
             except (IOError, OSError):
                 raise ValueError("could not read config file %s" % fp)
         parser = UnhosedConfigParser()
@@ -828,7 +834,7 @@ class ServerOptions(Options):
                 raise ValueError(
                     '%(process_num) must be present within process_name when '
                     'numprocs > 1')
-                    
+
         if stopasgroup and not killasgroup:
             raise ValueError("Cannot set stopasgroup=true and killasgroup=false")
 
@@ -1214,7 +1220,7 @@ class ServerOptions(Options):
 
             # always put our primary gid first in this list, otherwise we can
             # lose group info since sometimes the first group in the setgroups
-            # list gets overwritten on the subsequent setgid call (at least on 
+            # list gets overwritten on the subsequent setgid call (at least on
             # freebsd 9 with python 2.7 - this will be safe though for all unix
             # /python version combos)
             groups.insert(0, gid)
@@ -1364,9 +1370,6 @@ class ServerOptions(Options):
     def remove(self, path):
         os.remove(path)
 
-    def exists(self, path):
-        return os.path.exists(path)
-
     def _exit(self, code):
         os._exit(code)
 
@@ -1416,9 +1419,6 @@ class ServerOptions(Options):
     def process_environment(self):
         os.environ.update(self.environment or {})
 
-    def open(self, fn, mode='r'):
-        return open(fn, mode)
-
     def chdir(self, dir):
         os.chdir(dir)
 
@@ -1508,10 +1508,10 @@ class ClientOptions(Options):
         section = self.configroot.supervisorctl
         if not hasattr(fp, 'read'):
             self.here = os.path.dirname(normalize_path(fp))
-            if not os.path.exists(fp):
+            if not self.exists(fp):
                 raise ValueError("could not find config file %s" % fp)
             try:
-                fp = open(fp, 'r')
+                fp = self.open(fp, 'r')
             except (IOError, OSError):
                 raise ValueError("could not read config file %s" % fp)
         config = UnhosedConfigParser()

+ 31 - 55
supervisor/tests/test_options.py

@@ -177,46 +177,34 @@ class ClientOptionsTests(unittest.TestCase):
         self.assertEqual(options.password, '123')
         self.assertEqual(options.history_file, history_file)
 
-    def test_unreadable_config_file(self):
-        # Quick and dirty way of coming up with a decent filename
-        tempf = tempfile.NamedTemporaryFile()
-        fname = tempf.name
-        tempf.close()
-        self.assertFalse(os.path.exists(fname))
-
+    def test_read_config_not_found(self):
         instance = self._makeOne()
-        instance.stderr = StringIO()
-
-        class DummyException(Exception):
-            def __init__(self, exitcode):
-                self.exitcode = exitcode
-        def dummy_exit(self, exitcode=2):
-            # Important default exitcode=2 like sys.exit.
-            raise DummyException(exitcode)
-        instance.exit = dummy_exit
-        try:
-            instance.realize(args=['-c', fname])
-        except DummyException, e:
-            self.assertEqual(e.exitcode, 2)
-        else:
-            self.fail("expected exception")
+        def dummy_exists(fn):
+            return False
+        instance.exists = dummy_exists
 
         try:
-            instance.read_config(fname)
+            instance.read_config('filename')
         except ValueError, e:
             self.assertTrue("could not find config file" in str(e))
         else:
             self.fail("expected exception")
 
-        tempf = tempfile.NamedTemporaryFile()
-        os.chmod(tempf.name, 0) # Removing read perms
+    def test_read_config_unreadable(self):
+        instance = self._makeOne()
+        def dummy_exists(fn):
+            return True
+        instance.exists = dummy_exists
+        def dummy_open(fn, mode):
+            raise IOError(errno.EACCES, 'Permission denied: %s' % fn)
+        instance.open = dummy_open
+
         try:
-            instance.read_config(tempf.name)
+            instance.read_config('filename')
         except ValueError, e:
             self.assertTrue("could not read config file" in str(e))
         else:
             self.fail("expected exception")
-        tempf.close()
 
     def test_options_unixsocket_cli(self):
         from StringIO import StringIO
@@ -607,46 +595,34 @@ class ServerOptionsTests(unittest.TestCase):
         instance.realize(args=[])
         self.assertFalse(old_warning in instance.parse_warnings)
 
-    def test_unreadable_config_file(self):
-        # Quick and dirty way of coming up with a decent filename
-        tempf = tempfile.NamedTemporaryFile()
-        fname = tempf.name
-        tempf.close()
-        self.assertFalse(os.path.exists(fname))
-
+    def test_read_config_not_found(self):
         instance = self._makeOne()
-        instance.stderr = StringIO()
+        def dummy_exists(fn):
+            return False
+        instance.exists = dummy_exists
 
-        class DummyException(Exception):
-            def __init__(self, exitcode):
-                self.exitcode = exitcode
-        def dummy_exit(self, exitcode=2):
-            # Important default exitcode=2 like sys.exit.
-            raise DummyException(exitcode)
-        instance.exit = dummy_exit
         try:
-            instance.realize(args=['-c', fname])
-        except DummyException, e:
-            self.assertEqual(e.exitcode, 2)
-        else:
-            self.fail("expected exception")
-
-        try:
-            instance.read_config(fname)
+            instance.read_config('filename')
         except ValueError, e:
             self.assertTrue("could not find config file" in str(e))
         else:
             self.fail("expected exception")
 
-        tempf = tempfile.NamedTemporaryFile()
-        os.chmod(tempf.name, 0) # Removing read perms
+    def test_read_config_unreadable(self):
+        instance = self._makeOne()
+        def dummy_exists(fn):
+            return True
+        instance.exists = dummy_exists
+        def dummy_open(fn, mode):
+            raise IOError(errno.EACCES, 'Permission denied: %s' % fn)
+        instance.open = dummy_open
+
         try:
-            instance.read_config(tempf.name)
+            instance.read_config('filename')
         except ValueError, e:
             self.assertTrue("could not read config file" in str(e))
         else:
             self.fail("expected exception")
-        tempf.close()
 
     def test_readFile_failed(self):
         from supervisor.options import readFile
@@ -978,7 +954,7 @@ class ServerOptionsTests(unittest.TestCase):
         pconfig = pconfigs[0]
         self.assertEqual(pconfig.stopasgroup, True)
         self.assertEqual(pconfig.killasgroup, True)
-    
+
     def test_processes_from_section_killasgroup_mismatch_w_stopasgroup(self):
         instance = self._makeOne()
         text = lstrip("""\