diff options
author | Mark McLoughlin <markmc@redhat.com> | 2012-05-01 08:59:18 +0100 |
---|---|---|
committer | Mark McLoughlin <markmc@redhat.com> | 2012-05-01 16:33:57 +0100 |
commit | 3f4934226a3abfa55605a2766174c96c8926d795 (patch) | |
tree | 0e85fe6f3ce5cedbb266cf35c759c2b8e256dc0d /tests/unit/test_cfg.py | |
parent | fd8c2e7c22de79b37fdbc3e6235e9e50fb1f8e2b (diff) | |
download | oslo-3f4934226a3abfa55605a2766174c96c8926d795.tar.gz oslo-3f4934226a3abfa55605a2766174c96c8926d795.tar.xz oslo-3f4934226a3abfa55605a2766174c96c8926d795.zip |
New ConfigOpts.find_file() for locating conf files
Most services have the need to locate files like api-paste.ini or
policy.json.
This new method attempts to find these files by looking alongside
the config files already parsed by ConfigOpts and, failing that,
falls back to a standard set of directories.
Change-Id: I95897816485b88f78854df194cab7872d7c5452a
Diffstat (limited to 'tests/unit/test_cfg.py')
-rw-r--r-- | tests/unit/test_cfg.py | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/tests/unit/test_cfg.py b/tests/unit/test_cfg.py index 1d8bdca..785a324 100644 --- a/tests/unit/test_cfg.py +++ b/tests/unit/test_cfg.py @@ -1,6 +1,6 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 -# Copyright 2011 Red Hat, Inc. +# Copyright 2012 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -80,9 +80,13 @@ class BaseTestCase(unittest.TestCase): self.remove_tempfiles() self.stubs.UnsetAll() - def create_tempfiles(self, files): + def create_tempfiles(self, files, ext='.conf'): for (basename, contents) in files: - (fd, path) = tempfile.mkstemp(prefix=basename, suffix='.conf') + if not os.path.isabs(basename): + (fd, path) = tempfile.mkstemp(prefix=basename, suffix=ext) + else: + path = basename + ext + fd = os.open(path, os.O_CREAT|os.O_WRONLY) self.tempfiles.append(path) try: os.write(fd, contents) @@ -1002,6 +1006,43 @@ class SadPathTestCase(BaseTestCase): self.conf.set_override, 'foo', 'bar', group='blaa') +class FindFileTestCase(BaseTestCase): + + def test_find_policy_file(self): + policy_file = '/etc/policy.json' + + self.stubs.Set(os.path, 'exists', lambda p: p == policy_file) + + self.assertEquals(self.conf.find_file('foo.json'), None) + self.assertEquals(self.conf.find_file('policy.json'), policy_file) + + def test_find_policy_file_with_config_file(self): + dir = tempfile.mkdtemp() + self.tempdirs.append(dir) + + paths = self.create_tempfiles([(os.path.join(dir, 'test.conf'), + '[DEFAULT]'), + (os.path.join(dir, 'policy.json'), + '{}')], + ext='') + + self.conf(['--config-file', paths[0]]) + + self.assertEquals(self.conf.find_file('policy.json'), paths[1]) + + def test_find_policy_file_with_config_dir(self): + dir = tempfile.mkdtemp() + self.tempdirs.append(dir) + + path = self.create_tempfiles([(os.path.join(dir, 'policy.json'), + '{}')], + ext='')[0] + + self.conf(['--config-dir', dir]) + + self.assertEquals(self.conf.find_file('policy.json'), path) + + class OptDumpingTestCase(BaseTestCase): class FakeLogger: |