From 068c13547ed0c8bdd55f338fe9d8386fe02e6c0d Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Wed, 14 Apr 2021 13:16:39 +0100 Subject: tests: Add test for config file priority order This was documented in the docstring but we had nothing to assert it in tests. Correct that gap. Change-Id: I4002e7fe18c43eb5f9ba2eae16022d7da87f790d Signed-off-by: Stephen Finucane --- oslo_config/tests/test_cfg.py | 45 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/oslo_config/tests/test_cfg.py b/oslo_config/tests/test_cfg.py index 505cb00..0686eca 100644 --- a/oslo_config/tests/test_cfg.py +++ b/oslo_config/tests/test_cfg.py @@ -264,8 +264,10 @@ class HelpTestCase(BaseTestCase): class FindConfigFilesTestCase(BaseTestCase): def test_find_config_files(self): - config_files = [os.path.expanduser('~/.blaa/blaa.conf'), - '/etc/foo.conf'] + config_files = [ + os.path.expanduser('~/.blaa/blaa.conf'), + '/etc/foo.conf', + ] self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', @@ -273,11 +275,42 @@ class FindConfigFilesTestCase(BaseTestCase): self.assertEqual(cfg.find_config_files(project='blaa'), config_files) + def test_find_config_files_overrides(self): + """Ensure priority of directories is enforced. + + Ensure we will only ever return two files: $project.conf and + $prog.conf. + """ + config_files = [ + os.path.expanduser('~/.foo/foo.conf'), + # no '~/.foo/bar.conf' to make sure we fall through + os.path.expanduser('~/foo.conf'), + os.path.expanduser('~/bar.conf'), + '/etc/foo/foo.conf', + '/etc/foo/bar.conf', + '/etc/foo.conf', + '/etc/bar.conf', + ] + + self.useFixture(fixtures.MonkeyPatch( + 'os.path.exists', lambda p: p in config_files)) + + expected = [ + os.path.expanduser('~/.foo/foo.conf'), + os.path.expanduser('~/bar.conf'), + ] + actual = cfg.find_config_files(project='foo', prog='bar') + self.assertEqual(expected, actual) + def test_find_config_files_snap(self): - config_files = ['/snap/nova/current/etc/blaa/blaa.conf', - '/var/snap/nova/common/etc/blaa/blaa.conf'] - fake_env = {'SNAP': '/snap/nova/current/', - 'SNAP_COMMON': '/var/snap/nova/common/'} + config_files = [ + '/snap/nova/current/etc/blaa/blaa.conf', + '/var/snap/nova/common/etc/blaa/blaa.conf', + ] + fake_env = { + 'SNAP': '/snap/nova/current/', + 'SNAP_COMMON': '/var/snap/nova/common/', + } self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', -- cgit v1.2.1