]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Handle incorrect '--config-dir' param
authorYuriy Nesenenko <ynesenenko@mirantis.com>
Thu, 4 Jun 2015 15:50:14 +0000 (18:50 +0300)
committerYuriy Nesenenko <ynesenenko@mirantis.com>
Fri, 19 Jun 2015 09:44:51 +0000 (12:44 +0300)
This patch fixes the error that occurs if the --config-dir directory
doesn't exist. The exception ConfigDirNotFoundError should be caught
in this case.

Change-Id: Iee3fbd2b01de11124788e2c1df5c456c4010d234
Closes-Bug: #1255354

cinder/cmd/manage.py
cinder/tests/unit/test_cmd.py

index e087e606a9db337f9620ae0fce1c8c6a5e85f0ee..65121221162e818f798fcb54c65011011626965f 100644 (file)
@@ -547,6 +547,9 @@ def main():
         CONF(sys.argv[1:], project='cinder',
              version=version.version_string())
         logging.setup(CONF, "cinder")
+    except cfg.ConfigDirNotFoundError as details:
+        print(_("Invalid directory: %s") % details)
+        sys.exit(2)
     except cfg.ConfigFilesNotFoundError:
         cfgfile = CONF.config_file[-1] if CONF.config_file else None
         if cfgfile and not os.access(cfgfile, os.R_OK):
index 7d62805e0ecc9d9a958a164e76d74e45ab417e6d..a1929e108508ccf615448f0a5149ad8e7352df0e 100644 (file)
@@ -701,6 +701,28 @@ class TestCinderManageCmd(test.TestCase):
             sys.argv[1:], project='cinder', version=version.version_string())
         self.assertTrue(action_fn.called)
 
+    @mock.patch('oslo_config.cfg.ConfigOpts.__call__')
+    @mock.patch('oslo_log.log.setup')
+    @mock.patch('oslo_config.cfg.ConfigOpts.register_cli_opt')
+    def test_main_invalid_dir(self, register_cli_opt, log_setup,
+                              config_opts_call):
+        script_name = 'cinder-manage'
+        fake_dir = 'fake-dir'
+        invalid_dir = 'Invalid directory:'
+        sys.argv = [script_name, '--config-dir', fake_dir]
+        config_opts_call.side_effect = cfg.ConfigDirNotFoundError(fake_dir)
+
+        with mock.patch('sys.stdout', new=six.StringIO()) as fake_out:
+            exit = self.assertRaises(SystemExit, cinder_manage.main)
+            self.assertTrue(register_cli_opt.called)
+            config_opts_call.assert_called_once_with(
+                sys.argv[1:], project='cinder',
+                version=version.version_string())
+            self.assertIn(invalid_dir, fake_out.getvalue())
+            self.assertIn(fake_dir, fake_out.getvalue())
+            self.assertFalse(log_setup.called)
+            self.assertEqual(2, exit.code)
+
 
 class TestCinderRtstoolCmd(test.TestCase):