From 36a0ad29e53e30528a03577f71b2f6fc396daa05 Mon Sep 17 00:00:00 2001 From: rabi Date: Thu, 4 Aug 2016 09:32:51 +0530 Subject: [PATCH] Add valid merge strategies This adds three merge startegies(overwrite, merge, deep-merge). There can be a default merge startegy for an env or parameter specific merge strategy. if none of the above is specified then it defaults to 'overwrite'. Change-Id: I05f56887d762342981e3cb9885872ef58ecd0ad8 Blueprint: environment-merging --- heat/common/environment_util.py | 29 +++++++++++++++++ heat/tests/test_common_env_util.py | 50 ++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 heat/common/environment_util.py create mode 100644 heat/tests/test_common_env_util.py diff --git a/heat/common/environment_util.py b/heat/common/environment_util.py new file mode 100644 index 0000000000..5ee4f49849 --- /dev/null +++ b/heat/common/environment_util.py @@ -0,0 +1,29 @@ +# +# 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 +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +ALLOWED_PARAM_MERGE_STRATEGIES = (OVERWRITE, MERGE, DEEP_MERGE) = ( + 'overwrite', 'merge', 'deep_merge') + + +def get_param_merge_strategy(merge_strategies, param_key): + + if merge_strategies is None: + return OVERWRITE + + env_default = merge_strategies.get('default', OVERWRITE) + + merge_strategy = merge_strategies.get(param_key, env_default) + if merge_strategy in ALLOWED_PARAM_MERGE_STRATEGIES: + return merge_strategy + + return env_default diff --git a/heat/tests/test_common_env_util.py b/heat/tests/test_common_env_util.py new file mode 100644 index 0000000000..bfcb8665ea --- /dev/null +++ b/heat/tests/test_common_env_util.py @@ -0,0 +1,50 @@ +# +# 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 +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from heat.common import environment_util as env_util +from heat.tests import common + + +class TestEnvironmentUtil(common.HeatTestCase): + + def test_empty_merge_strategies(self): + merge_strategies = {} + param_strategy = env_util.get_param_merge_strategy(merge_strategies, + 'param1') + self.assertEqual(env_util.OVERWRITE, param_strategy) + + def test_default_merge_strategy(self): + merge_strategies = {'default': 'deep_merge'} + param_strategy = env_util.get_param_merge_strategy(merge_strategies, + 'param1') + self.assertEqual(env_util.DEEP_MERGE, param_strategy) + + def test_param_sepcific_merge_strategy(self): + merge_strategies = {'default': 'merge', + 'param1': 'deep_merge'} + param_strategy = env_util.get_param_merge_strategy(merge_strategies, + 'param1') + self.assertEqual(env_util.DEEP_MERGE, param_strategy) + + def test_wrong_param_strategy(self): + merge_strategies = {'default': 'merge', + 'param1': 'unknown'} + param_strategy = env_util.get_param_merge_strategy(merge_strategies, + 'param1') + self.assertEqual(env_util.MERGE, param_strategy) + + def test_merge_startegies_none(self): + merge_strategies = None + param_strategy = env_util.get_param_merge_strategy(merge_strategies, + 'param1') + self.assertEqual(env_util.OVERWRITE, param_strategy)