diff options
author | Jay S. Bryant <jsbryant@us.ibm.com> | 2013-12-17 19:22:06 -0600 |
---|---|---|
committer | Jay S. Bryant <jsbryant@us.ibm.com> | 2014-01-27 09:30:55 -0600 |
commit | bd16059492b74f1711e873bf26b0c7491a974c6d (patch) | |
tree | a546c4d5ea95e2dbdc00ff7646765599c1112883 /tests/test_cfg.py | |
parent | e6bcb78a70ddde28b4b26156546e680077d8cee5 (diff) | |
download | oslo-config-bd16059492b74f1711e873bf26b0c7491a974c6d.tar.gz |
Throw exception if --config-dir doesn't exist
Currently an invalid --config-dir option is silently ignored.
This means that a user could accidentally enter the wrong
directory or have a typo which would cause the desired
config files to not be used and no indication of the error would be
produced.
This change adds a ConfigDirNotFoundError exception that can be
raised when the requested --config-dir isn't found.
While working out the unit tests for this it was discovered that
the test_config_dir_tilde test case was not correct. It could still
pass when it should fail. This commit includes a fix for that issue
as well.
Closes-bug: 1255354
Change-Id: I386b2419d04572b14ccf11b527f871a237304e53
Diffstat (limited to 'tests/test_cfg.py')
-rw-r--r-- | tests/test_cfg.py | 54 |
1 files changed, 34 insertions, 20 deletions
diff --git a/tests/test_cfg.py b/tests/test_cfg.py index 249f219..ed354f6 100644 --- a/tests/test_cfg.py +++ b/tests/test_cfg.py @@ -75,6 +75,10 @@ class ExceptionsTestCase(utils.BaseTestCase): msg = str(cfg.ConfigFilesNotFoundError(['foo', 'bar'])) self.assertEqual(msg, 'Failed to read some config files: foo,bar') + def test_config_dir_not_found_error(self): + msg = str(cfg.ConfigDirNotFoundError('foobar')) + self.assertEqual(msg, 'Failed to read config file directory: foobar') + def test_config_file_parse_error(self): msg = str(cfg.ConfigFileParseError('foo', 'foobar')) self.assertEqual(msg, 'Failed to parse foo: foobar') @@ -2011,6 +2015,14 @@ class ConfigDirTestCase(BaseTestCase): self.assertTrue(hasattr(self.conf.snafu, 'bell')) self.assertEqual(self.conf.snafu.bell, 'whistle-11') + def test_config_dir_doesnt_exist(self): + tmpdir = '/tmp/foo' + + self.assertRaises(cfg.ConfigDirNotFoundError, + self.conf, + ['--config-dir', tmpdir] + ) + class ReparseTestCase(BaseTestCase): @@ -2762,27 +2774,29 @@ class TildeExpansionTestCase(BaseTestCase): def test_config_dir_tilde(self): homedir = os.path.expanduser('~') - tmpdir = tempfile.mktemp(dir=homedir, - prefix='cfg-', - suffix='.d') - tmpfile = os.path.join(tmpdir, 'foo.conf') - tmpbase = os.path.basename(tmpfile) - - self.useFixture(fixtures.MonkeyPatch( - 'glob.glob', - lambda p: [tmpfile])) - try: - self.conf(['--config-dir', - os.path.join('~', os.path.basename(tmpdir))]) - except cfg.ConfigFilesNotFoundError as cfnfe: - self.assertTrue(os.path.expanduser('~') in str(cfnfe)) - - self.useFixture(fixtures.MonkeyPatch( - 'os.path.exists', - lambda p: p == tmpfile)) - - self.assertEqual(self.conf.find_file(tmpbase), tmpfile) + tmpdir = tempfile.mkdtemp(dir=homedir, + prefix='cfg-', + suffix='.d') + tmpfile = os.path.join(tmpdir, 'foo.conf') + + self.useFixture(fixtures.MonkeyPatch( + 'glob.glob', + lambda p: [tmpfile])) + + e = self.assertRaises(cfg.ConfigFilesNotFoundError, + self.conf, + ['--config-dir', + os.path.join('~', + os.path.basename(tmpdir))] + ) + self.assertIn(tmpdir, str(e)) + finally: + try: + shutil.rmtree(tmpdir) + except OSError as exc: + if exc.errno != 2: + raise class SubCommandTestCase(BaseTestCase): |