From b8c2c86333bb610b7353e198f4e523a4022d2994 Mon Sep 17 00:00:00 2001 From: dineshbhor Date: Sun, 20 May 2018 22:45:35 -0700 Subject: [PATCH] Trivial: Fix unnecessary redundant code While translating node_template, heat-translator first checks whether the policy is derived from the policies 'tosca.policies.Scaling' or 'tosca.policies.Monitoring', If yes, then it adds them into TOSCA_TO_HOT_TYPE map. In the subsequent code, it again checks for those policies and if they are not present in TOSCA_TO_HOT_TYPE map then it raises UnsupportedTypeError exception. This patch removes this unnecessary redundant code of checking policies again and ensures that the affected code is getting covered through test case. TrivialFix Change-Id: Ibe435e23e7ac11834e662745d327e6f99280f9f5 --- translator/hot/translate_node_templates.py | 4 +-- translator/tests/data/nfv/tacker_defs.yaml | 3 ++ .../test_tosca_unsupported_policy_type.yaml | 32 +++++++++++++++++++ .../tests/test_tosca_hot_translation.py | 14 ++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 translator/tests/data/nfv/test_tosca_unsupported_policy_type.yaml diff --git a/translator/hot/translate_node_templates.py b/translator/hot/translate_node_templates.py index ac9837f6..1275deef 100644 --- a/translator/hot/translate_node_templates.py +++ b/translator/hot/translate_node_templates.py @@ -276,9 +276,7 @@ class TranslateNodeTemplates(object): if policy.is_derived_from('tosca.policies.Placement'): TOSCA_TO_HOT_TYPE[policy_type.type] = \ TOSCA_TO_HOT_TYPE['tosca.policies.Placement'] - if not policy.is_derived_from('tosca.policies.Monitoring') and \ - not policy.is_derived_from('tosca.policies.Scaling') and \ - policy_type.type not in TOSCA_TO_HOT_TYPE: + if policy_type.type not in TOSCA_TO_HOT_TYPE: raise UnsupportedTypeError(type=_('%s') % policy_type.type) elif policy_type.type == 'tosca.policies.Scaling.Cluster': self.hot_template_version = '2016-04-08' diff --git a/translator/tests/data/nfv/tacker_defs.yaml b/translator/tests/data/nfv/tacker_defs.yaml index f5fac057..60c04302 100644 --- a/translator/tests/data/nfv/tacker_defs.yaml +++ b/translator/tests/data/nfv/tacker_defs.yaml @@ -92,6 +92,9 @@ policy_types: If the policy is not strict, it is allowed to continue even if the nova-scheduler fails to assign hosts under the policy. + tosca.policies.tacker.ABC: + derived_from: tosca.policies.Root + tosca.policies.tacker.Failure: derived_from: tosca.policies.Root action: diff --git a/translator/tests/data/nfv/test_tosca_unsupported_policy_type.yaml b/translator/tests/data/nfv/test_tosca_unsupported_policy_type.yaml new file mode 100644 index 00000000..41b27ce0 --- /dev/null +++ b/translator/tests/data/nfv/test_tosca_unsupported_policy_type.yaml @@ -0,0 +1,32 @@ +tosca_definitions_version: tosca_simple_profile_for_nfv_1_0_0 + +description: > + Template for deploying the nodes based on given policies. + +imports: + - tacker_defs.yaml + - tacker_nfv_defs.yaml +topology_template: + node_templates: + my_server: + type: tosca.nodes.Compute + capabilities: + # Host container properties + host: + properties: + num_cpus: 2 + disk_size: 10 GB + mem_size: 512 MB + # Guest Operating System properties + os: + properties: + # host Operating System image properties + architecture: x86_64 + type: Linux + distribution: RHEL + version: 6.5 + policies: + - my_compute_abc_policy: + type: tosca.policies.tacker.ABC + description: Apply my ABC policy to my application's servers + targets: [ my_server ] diff --git a/translator/tests/test_tosca_hot_translation.py b/translator/tests/test_tosca_hot_translation.py index 1357a10d..b03d73f4 100644 --- a/translator/tests/test_tosca_hot_translation.py +++ b/translator/tests/test_tosca_hot_translation.py @@ -563,6 +563,20 @@ class ToscaHotTranslationTest(TestCase): .translate) self.assertEqual(expected_msg, err.__str__()) + def test_translate_unsupported_tosca_policy_type(self): + tosca_file = ('../tests/data/nfv/' + 'test_tosca_unsupported_policy_type.yaml') + tosca_tpl = os.path.normpath(os.path.join( + os.path.dirname(os.path.abspath(__file__)), tosca_file)) + params = {} + expected_msg = _('Type "tosca.policies.tacker.ABC" is valid TOSCA ' + 'type but translation support is not yet available.') + tosca = ToscaTemplate(tosca_tpl, params, True) + err = self.assertRaises(UnsupportedTypeError, + TOSCATranslator(tosca, params) + .translate) + self.assertEqual(expected_msg, err.__str__()) + def test_hot_translate_cluster_scaling_policy(self): tosca_file = '../tests/data/autoscaling/tosca_cluster_autoscaling.yaml' hot_file = '../tests/data/hot_output/autoscaling/' \