From c51e73b08cbdda096e8c5bda5c917743f280a578 Mon Sep 17 00:00:00 2001 From: Yuriy Nesenenko Date: Thu, 4 Jun 2015 18:50:14 +0300 Subject: [PATCH] Handle incorrect '--config-dir' param 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 | 3 +++ cinder/tests/unit/test_cmd.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/cinder/cmd/manage.py b/cinder/cmd/manage.py index e087e606a..651212211 100644 --- a/cinder/cmd/manage.py +++ b/cinder/cmd/manage.py @@ -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): diff --git a/cinder/tests/unit/test_cmd.py b/cinder/tests/unit/test_cmd.py index 7d62805e0..a1929e108 100644 --- a/cinder/tests/unit/test_cmd.py +++ b/cinder/tests/unit/test_cmd.py @@ -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): -- 2.45.2