Jelajahi Sumber

Merge branch 'issue_61_pending' of git://github.com/JensRantil/supervisor into JensRantil-issue_61_pending

Conflicts:
	supervisor/tests/test_options.py
Mike Naberezny 12 tahun lalu
induk
melakukan
fcb17f1c0c
2 mengubah file dengan 84 tambahan dan 2 penghapusan
  1. 6 2
      supervisor/options.py
  2. 78 0
      supervisor/tests/test_options.py

+ 6 - 2
supervisor/options.py

@@ -516,10 +516,12 @@ class ServerOptions(Options):
 
 
         section = self.configroot.supervisord
         section = self.configroot.supervisord
         if not hasattr(fp, 'read'):
         if not hasattr(fp, 'read'):
+            if not os.path.exists(fp):
+                raise ValueError("could not find config file %s" % fp)
             try:
             try:
                 fp = open(fp, 'r')
                 fp = open(fp, 'r')
             except (IOError, OSError):
             except (IOError, OSError):
-                raise ValueError("could not find config file %s" % fp)
+                raise ValueError("could not read config file %s" % fp)
         parser = UnhosedConfigParser()
         parser = UnhosedConfigParser()
         try:
         try:
             parser.readfp(fp)
             parser.readfp(fp)
@@ -1501,10 +1503,12 @@ class ClientOptions(Options):
         section = self.configroot.supervisorctl
         section = self.configroot.supervisorctl
         if not hasattr(fp, 'read'):
         if not hasattr(fp, 'read'):
             self.here = os.path.dirname(normalize_path(fp))
             self.here = os.path.dirname(normalize_path(fp))
+            if not os.path.exists(fp):
+                raise ValueError("could not find config file %s" % fp)
             try:
             try:
                 fp = open(fp, 'r')
                 fp = open(fp, 'r')
             except (IOError, OSError):
             except (IOError, OSError):
-                raise ValueError("could not find config file %s" % fp)
+                raise ValueError("could not read config file %s" % fp)
         config = UnhosedConfigParser()
         config = UnhosedConfigParser()
         config.mysection = 'supervisorctl'
         config.mysection = 'supervisorctl'
         config.readfp(fp)
         config.readfp(fp)

+ 78 - 0
supervisor/tests/test_options.py

@@ -178,6 +178,45 @@ class ClientOptionsTests(unittest.TestCase):
         self.assertEqual(options.password, '123')
         self.assertEqual(options.password, '123')
         self.assertEqual(options.history_file, history_file)
         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))
+
+        instance = self._makeOne()
+        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.assertEquals(e.exitcode, 2)
+        else:
+            self.fail("expected exception")
+
+        try:
+            instance.read_config(fname)
+        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
+        try:
+            instance.read_config(tempf.name)
+        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):
     def test_options_unixsocket_cli(self):
         from StringIO import StringIO
         from StringIO import StringIO
         fp = StringIO('[supervisorctl]')
         fp = StringIO('[supervisorctl]')
@@ -544,6 +583,45 @@ class ServerOptionsTests(unittest.TestCase):
         instance.realize(args=[])
         instance.realize(args=[])
         self.assertFalse(old_warning in instance.parse_warnings)
         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))
+
+        instance = self._makeOne()
+        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.assertEquals(e.exitcode, 2)
+        else:
+            self.fail("expected exception")
+
+        try:
+            instance.read_config(fname)
+        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
+        try:
+            instance.read_config(tempf.name)
+        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):
     def test_readFile_failed(self):
         from supervisor.options import readFile
         from supervisor.options import readFile
         try:
         try: