From c94462ff6fad95083282cb620ca21e5ce7ce7d1c Mon Sep 17 00:00:00 2001 From: spzala Date: Mon, 6 Apr 2015 12:17:48 -0700 Subject: [PATCH] TOSCA: Handle memory units Update templates with memory units. Create unit tests for newly added utils module. Change-Id: I6e46b05ff3c25df586c7b7cdac3e85f93185453f --- translator/common/__init.py__ | 0 translator/hot/tosca/tosca_compute.py | 7 ++- .../tosca_blockstorage_with_attachment.yaml | 4 +- ...lockstorage_with_attachment_notation1.yaml | 8 ++-- ...lockstorage_with_attachment_notation2.yaml | 8 ++-- translator/tests/data/tosca_elk.yaml | 4 +- ...multiple_blockstorage_with_attachment.yaml | 8 ++-- translator/tests/test_utils.py | 43 +++++++++++++++++++ .../tosca_single_instance_wordpress.yaml | 4 +- ...est_tosca_normative_type_by_shortname.yaml | 4 +- .../data/test_tosca_top_level_error1.yaml | 4 +- .../data/test_tosca_top_level_error2.yaml | 4 +- translator/toscalib/tests/data/tosca_elk.yaml | 4 +- .../data/tosca_single_instance_wordpress.yaml | 4 +- translator/toscalib/tests/test_toscatpl.py | 2 +- 15 files changed, 77 insertions(+), 31 deletions(-) delete mode 100644 translator/common/__init.py__ create mode 100644 translator/tests/test_utils.py diff --git a/translator/common/__init.py__ b/translator/common/__init.py__ deleted file mode 100644 index e69de29b..00000000 diff --git a/translator/hot/tosca/tosca_compute.py b/translator/hot/tosca/tosca_compute.py index 769ceb21..57d45011 100755 --- a/translator/hot/tosca/tosca_compute.py +++ b/translator/hot/tosca/tosca_compute.py @@ -11,6 +11,7 @@ # License for the specific language governing permissions and limitations # under the License. +from translator.common.utils import MemoryUnit from translator.hot.syntax.hot_resource import HotResource # A design issue to be resolved is how to translate the generic TOSCA server @@ -101,11 +102,13 @@ class ToscaCompute(HotResource): match_cpu = self._match_flavors(match_all, FLAVORS, 'num_cpus', cpu) # flavors that fit the mem size - mem = properties.get('mem_size') + mem = MemoryUnit.convert_unit_size_to_num(properties.get('mem_size'), + 'MB') match_cpu_mem = self._match_flavors(match_cpu, FLAVORS, 'mem_size', mem) # flavors that fit the disk size - disk = properties.get('disk_size') + disk = MemoryUnit.convert_unit_size_to_num(properties.get('disk_size'), + 'GB') match_cpu_mem_disk = self._match_flavors(match_cpu_mem, FLAVORS, 'disk_size', disk) # if multiple match, pick the flavor with the least memory diff --git a/translator/tests/data/tosca_blockstorage_with_attachment.yaml b/translator/tests/data/tosca_blockstorage_with_attachment.yaml index a02ae62f..51255d38 100644 --- a/translator/tests/data/tosca_blockstorage_with_attachment.yaml +++ b/translator/tests/data/tosca_blockstorage_with_attachment.yaml @@ -27,9 +27,9 @@ node_templates: type: tosca.nodes.Compute properties: # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: { get_input: cpus } - mem_size: 4096 + mem_size: 4096 MB capabilities: os: properties: diff --git a/translator/tests/data/tosca_blockstorage_with_attachment_notation1.yaml b/translator/tests/data/tosca_blockstorage_with_attachment_notation1.yaml index 9e8ccab4..e65dc0f6 100644 --- a/translator/tests/data/tosca_blockstorage_with_attachment_notation1.yaml +++ b/translator/tests/data/tosca_blockstorage_with_attachment_notation1.yaml @@ -27,9 +27,9 @@ node_templates: type: tosca.nodes.Compute properties: # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: { get_input: cpus } - mem_size: 4096 + mem_size: 4096 MB capabilities: os: properties: @@ -45,9 +45,9 @@ node_templates: type: tosca.nodes.Compute properties: # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: { get_input: cpus } - mem_size: 4096 + mem_size: 4096 MB capabilities: os: properties: diff --git a/translator/tests/data/tosca_blockstorage_with_attachment_notation2.yaml b/translator/tests/data/tosca_blockstorage_with_attachment_notation2.yaml index c1f103e3..418818d1 100644 --- a/translator/tests/data/tosca_blockstorage_with_attachment_notation2.yaml +++ b/translator/tests/data/tosca_blockstorage_with_attachment_notation2.yaml @@ -27,9 +27,9 @@ node_templates: type: tosca.nodes.Compute properties: # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: { get_input: cpus } - mem_size: 4096 + mem_size: 4096 MB capabilities: os: properties: @@ -45,9 +45,9 @@ node_templates: type: tosca.nodes.Compute properties: # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: { get_input: cpus } - mem_size: 4096 + mem_size: 4096 MB capabilities: os: properties: diff --git a/translator/tests/data/tosca_elk.yaml b/translator/tests/data/tosca_elk.yaml index 40e6ed92..596658f2 100644 --- a/translator/tests/data/tosca_elk.yaml +++ b/translator/tests/data/tosca_elk.yaml @@ -9,9 +9,9 @@ imports: dsl_definitions: ubuntu_node: &ubuntu_node # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: 1 - mem_size: 4096 + mem_size: 4096 MB os_capabilities: &os_capabilities architecture: x86_64 type: Linux diff --git a/translator/tests/data/tosca_multiple_blockstorage_with_attachment.yaml b/translator/tests/data/tosca_multiple_blockstorage_with_attachment.yaml index d6df1812..19073faa 100644 --- a/translator/tests/data/tosca_multiple_blockstorage_with_attachment.yaml +++ b/translator/tests/data/tosca_multiple_blockstorage_with_attachment.yaml @@ -25,9 +25,9 @@ node_templates: type: tosca.nodes.Compute properties: # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: { get_input: cpus } - mem_size: 4096 + mem_size: 4096 MB capabilities: os: properties: @@ -50,9 +50,9 @@ node_templates: type: tosca.nodes.Compute properties: # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: { get_input: cpus } - mem_size: 4096 + mem_size: 4096 MB capabilities: os: properties: diff --git a/translator/tests/test_utils.py b/translator/tests/test_utils.py new file mode 100644 index 00000000..42763c48 --- /dev/null +++ b/translator/tests/test_utils.py @@ -0,0 +1,43 @@ +# 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. + +import translator.common.utils +from translator.toscalib.tests.base import TestCase + + +class CommonUtilsTest(TestCase): + + MemoryUnit = translator.common.utils.MemoryUnit + + def test_convert_unit_size_to_num(self): + size = '1 TB' + num_to_convert = 'GB' + expected_output = 1000 + output = self.MemoryUnit.convert_unit_size_to_num(size, num_to_convert) + self.assertEqual(output, expected_output) + + size = '40 GB' + num_to_convert = 'MB' + expected_output = 40000 + output = self.MemoryUnit.convert_unit_size_to_num(size, num_to_convert) + self.assertEqual(output, expected_output) + + def test_validate_unit(self): + unit = 'AB' + exp_msg = ('Provided unit "{0}" is not valid. The valid units are ' + '{1}').format(unit, self.MemoryUnit.UNIT_SIZE_DICT.keys()) + try: + self.MemoryUnit.validate_unit(unit) + except Exception as err: + self.assertTrue( + isinstance(err, ValueError)) + self.assertEqual(exp_msg, err.__str__()) diff --git a/translator/toscalib/tests/data/CSAR/tosca_single_instance_wordpress/Definitions/tosca_single_instance_wordpress.yaml b/translator/toscalib/tests/data/CSAR/tosca_single_instance_wordpress/Definitions/tosca_single_instance_wordpress.yaml index a72fd078..fc822edc 100644 --- a/translator/toscalib/tests/data/CSAR/tosca_single_instance_wordpress/Definitions/tosca_single_instance_wordpress.yaml +++ b/translator/toscalib/tests/data/CSAR/tosca_single_instance_wordpress/Definitions/tosca_single_instance_wordpress.yaml @@ -91,9 +91,9 @@ node_templates: type: tosca.nodes.Compute properties: # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: { get_input: cpus } - mem_size: 4096 + mem_size: 4096 MB capabilities: os: properties: diff --git a/translator/toscalib/tests/data/test_tosca_normative_type_by_shortname.yaml b/translator/toscalib/tests/data/test_tosca_normative_type_by_shortname.yaml index aee9bd9b..e4675f69 100644 --- a/translator/toscalib/tests/data/test_tosca_normative_type_by_shortname.yaml +++ b/translator/toscalib/tests/data/test_tosca_normative_type_by_shortname.yaml @@ -15,9 +15,9 @@ node_templates: type: Compute properties: # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: { get_input: cpus } - mem_size: 4096 + mem_size: 4096 MB capabilities: os: properties: diff --git a/translator/toscalib/tests/data/test_tosca_top_level_error1.yaml b/translator/toscalib/tests/data/test_tosca_top_level_error1.yaml index e7f98b63..51ba8643 100644 --- a/translator/toscalib/tests/data/test_tosca_top_level_error1.yaml +++ b/translator/toscalib/tests/data/test_tosca_top_level_error1.yaml @@ -13,9 +13,9 @@ node_templates: type: tosca.nodes.Compute properties: # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: { get_input: cpus } - mem_size: 4096 + mem_size: 4096 MB capabilities: os: properties: diff --git a/translator/toscalib/tests/data/test_tosca_top_level_error2.yaml b/translator/toscalib/tests/data/test_tosca_top_level_error2.yaml index ad72b5c7..104e7cab 100644 --- a/translator/toscalib/tests/data/test_tosca_top_level_error2.yaml +++ b/translator/toscalib/tests/data/test_tosca_top_level_error2.yaml @@ -15,9 +15,9 @@ node_template: type: tosca.nodes.Compute properties: # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: { get_input: cpus } - mem_size: 4096 + mem_size: 4096 MB capabilities: os: properties: diff --git a/translator/toscalib/tests/data/tosca_elk.yaml b/translator/toscalib/tests/data/tosca_elk.yaml index f2650a5b..23df46ca 100644 --- a/translator/toscalib/tests/data/tosca_elk.yaml +++ b/translator/toscalib/tests/data/tosca_elk.yaml @@ -14,9 +14,9 @@ imports: dsl_definitions: ubuntu_node: &ubuntu_node # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: { get_input: my_cpus } - mem_size: 4096 + mem_size: 4096 MB os_capabilities: &os_capabilities architecture: x86_64 type: Linux diff --git a/translator/toscalib/tests/data/tosca_single_instance_wordpress.yaml b/translator/toscalib/tests/data/tosca_single_instance_wordpress.yaml index de0148a8..b5485da0 100644 --- a/translator/toscalib/tests/data/tosca_single_instance_wordpress.yaml +++ b/translator/toscalib/tests/data/tosca_single_instance_wordpress.yaml @@ -97,9 +97,9 @@ node_templates: type: tosca.nodes.Compute properties: # compute properties (flavor) - disk_size: 10 + disk_size: 10 GB num_cpus: { get_input: cpus } - mem_size: 4096 + mem_size: 4096 MB capabilities: os: properties: diff --git a/translator/toscalib/tests/test_toscatpl.py b/translator/toscalib/tests/test_toscatpl.py index 800360fc..f9dd76a3 100644 --- a/translator/toscalib/tests/test_toscatpl.py +++ b/translator/toscalib/tests/test_toscatpl.py @@ -108,7 +108,7 @@ class ToscaTemplateTest(TestCase): '''Test property value''' for property in tpl.properties: if property.name == 'mem_size': - self.assertEqual(property.value, 4096) + self.assertEqual(property.value, '4096 MB') '''Test capability''' self.assertIn('os', [cap.name for cap in tpl.capabilities]) os_props_objs = None