diff --git a/paunch/tests/test_utils_common.py b/paunch/tests/test_utils_common.py index 9210f1c..1e8a9ef 100644 --- a/paunch/tests/test_utils_common.py +++ b/paunch/tests/test_utils_common.py @@ -39,8 +39,10 @@ class TestUtilsCommonConfig(base.TestCase): def setUp(self): super(TestUtilsCommonConfig, self).setUp() self.config_content = "{'image': 'docker.io/haproxy'}" + self.config_override = {'haproxy': {'image': 'quay.io/haproxy'}} self.open_func = 'paunch.utils.common.open' self.expected_config = {'haproxy': {'image': 'docker.io/haproxy'}} + self.expected_config_over = {'haproxy': {'image': 'quay.io/haproxy'}} self.container = 'haproxy' self.old_config_file = '/var/lib/tripleo-config/' + \ 'hashed-container-startup-config-step_1.json' @@ -104,3 +106,13 @@ class TestUtilsCommonConfig(base.TestCase): self.assertEqual( self.expected_config, common.load_config(self.old_config_file)) + + @mock.patch('os.path.isdir') + def test_load_config_dir_with_name_and_override(self, mock_isdir): + mock_isdir.return_value = True + mock_open = mock.mock_open(read_data=self.config_content) + with mock.patch(self.open_func, mock_open): + self.assertEqual( + self.expected_config_over, + common.load_config('/config_dir', self.container, + self.config_override)) diff --git a/paunch/utils/common.py b/paunch/utils/common.py index 6805a10..a2ebffa 100644 --- a/paunch/utils/common.py +++ b/paunch/utils/common.py @@ -86,8 +86,10 @@ def get_all_cpus(**args): return "0-" + str(psutil.cpu_count() - 1) -def load_config(config, name=None): +def load_config(config, name=None, overrides=None): container_config = {} + if overrides is None: + overrides = {} if os.path.isdir(config): # When the user gives a config directory and specify a container name, # we return the container config for that specific container. @@ -150,4 +152,11 @@ def load_config(config, name=None): with open(os.path.join(config), 'r') as f: container_config[name] = {} container_config[name].update(yaml.safe_load(f)) + + # Overrides + for k in overrides.keys(): + if k in container_config: + for mk, mv in overrides[k].items(): + container_config[k][mk] = mv + return container_config