TOSCA: Handle memory units
Update templates with memory units. Create unit tests for newly added utils module. Change-Id: I6e46b05ff3c25df586c7b7cdac3e85f93185453f
This commit is contained in:
committed by
srinivas_tadepalli
parent
f07a15bec1
commit
c94462ff6f
@@ -11,6 +11,7 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
from translator.common.utils import MemoryUnit
|
||||||
from translator.hot.syntax.hot_resource import HotResource
|
from translator.hot.syntax.hot_resource import HotResource
|
||||||
|
|
||||||
# A design issue to be resolved is how to translate the generic TOSCA server
|
# 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)
|
match_cpu = self._match_flavors(match_all, FLAVORS, 'num_cpus', cpu)
|
||||||
|
|
||||||
# flavors that fit the mem size
|
# 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,
|
match_cpu_mem = self._match_flavors(match_cpu, FLAVORS,
|
||||||
'mem_size', mem)
|
'mem_size', mem)
|
||||||
# flavors that fit the disk size
|
# 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,
|
match_cpu_mem_disk = self._match_flavors(match_cpu_mem, FLAVORS,
|
||||||
'disk_size', disk)
|
'disk_size', disk)
|
||||||
# if multiple match, pick the flavor with the least memory
|
# if multiple match, pick the flavor with the least memory
|
||||||
|
|||||||
@@ -27,9 +27,9 @@ node_templates:
|
|||||||
type: tosca.nodes.Compute
|
type: tosca.nodes.Compute
|
||||||
properties:
|
properties:
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: { get_input: cpus }
|
num_cpus: { get_input: cpus }
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
capabilities:
|
capabilities:
|
||||||
os:
|
os:
|
||||||
properties:
|
properties:
|
||||||
|
|||||||
@@ -27,9 +27,9 @@ node_templates:
|
|||||||
type: tosca.nodes.Compute
|
type: tosca.nodes.Compute
|
||||||
properties:
|
properties:
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: { get_input: cpus }
|
num_cpus: { get_input: cpus }
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
capabilities:
|
capabilities:
|
||||||
os:
|
os:
|
||||||
properties:
|
properties:
|
||||||
@@ -45,9 +45,9 @@ node_templates:
|
|||||||
type: tosca.nodes.Compute
|
type: tosca.nodes.Compute
|
||||||
properties:
|
properties:
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: { get_input: cpus }
|
num_cpus: { get_input: cpus }
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
capabilities:
|
capabilities:
|
||||||
os:
|
os:
|
||||||
properties:
|
properties:
|
||||||
|
|||||||
@@ -27,9 +27,9 @@ node_templates:
|
|||||||
type: tosca.nodes.Compute
|
type: tosca.nodes.Compute
|
||||||
properties:
|
properties:
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: { get_input: cpus }
|
num_cpus: { get_input: cpus }
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
capabilities:
|
capabilities:
|
||||||
os:
|
os:
|
||||||
properties:
|
properties:
|
||||||
@@ -45,9 +45,9 @@ node_templates:
|
|||||||
type: tosca.nodes.Compute
|
type: tosca.nodes.Compute
|
||||||
properties:
|
properties:
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: { get_input: cpus }
|
num_cpus: { get_input: cpus }
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
capabilities:
|
capabilities:
|
||||||
os:
|
os:
|
||||||
properties:
|
properties:
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ imports:
|
|||||||
dsl_definitions:
|
dsl_definitions:
|
||||||
ubuntu_node: &ubuntu_node
|
ubuntu_node: &ubuntu_node
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: 1
|
num_cpus: 1
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
os_capabilities: &os_capabilities
|
os_capabilities: &os_capabilities
|
||||||
architecture: x86_64
|
architecture: x86_64
|
||||||
type: Linux
|
type: Linux
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ node_templates:
|
|||||||
type: tosca.nodes.Compute
|
type: tosca.nodes.Compute
|
||||||
properties:
|
properties:
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: { get_input: cpus }
|
num_cpus: { get_input: cpus }
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
capabilities:
|
capabilities:
|
||||||
os:
|
os:
|
||||||
properties:
|
properties:
|
||||||
@@ -50,9 +50,9 @@ node_templates:
|
|||||||
type: tosca.nodes.Compute
|
type: tosca.nodes.Compute
|
||||||
properties:
|
properties:
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: { get_input: cpus }
|
num_cpus: { get_input: cpus }
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
capabilities:
|
capabilities:
|
||||||
os:
|
os:
|
||||||
properties:
|
properties:
|
||||||
|
|||||||
43
translator/tests/test_utils.py
Normal file
43
translator/tests/test_utils.py
Normal file
@@ -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__())
|
||||||
@@ -91,9 +91,9 @@ node_templates:
|
|||||||
type: tosca.nodes.Compute
|
type: tosca.nodes.Compute
|
||||||
properties:
|
properties:
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: { get_input: cpus }
|
num_cpus: { get_input: cpus }
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
capabilities:
|
capabilities:
|
||||||
os:
|
os:
|
||||||
properties:
|
properties:
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ node_templates:
|
|||||||
type: Compute
|
type: Compute
|
||||||
properties:
|
properties:
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: { get_input: cpus }
|
num_cpus: { get_input: cpus }
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
capabilities:
|
capabilities:
|
||||||
os:
|
os:
|
||||||
properties:
|
properties:
|
||||||
|
|||||||
@@ -13,9 +13,9 @@ node_templates:
|
|||||||
type: tosca.nodes.Compute
|
type: tosca.nodes.Compute
|
||||||
properties:
|
properties:
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: { get_input: cpus }
|
num_cpus: { get_input: cpus }
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
capabilities:
|
capabilities:
|
||||||
os:
|
os:
|
||||||
properties:
|
properties:
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ node_template:
|
|||||||
type: tosca.nodes.Compute
|
type: tosca.nodes.Compute
|
||||||
properties:
|
properties:
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: { get_input: cpus }
|
num_cpus: { get_input: cpus }
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
capabilities:
|
capabilities:
|
||||||
os:
|
os:
|
||||||
properties:
|
properties:
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ imports:
|
|||||||
dsl_definitions:
|
dsl_definitions:
|
||||||
ubuntu_node: &ubuntu_node
|
ubuntu_node: &ubuntu_node
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: { get_input: my_cpus }
|
num_cpus: { get_input: my_cpus }
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
os_capabilities: &os_capabilities
|
os_capabilities: &os_capabilities
|
||||||
architecture: x86_64
|
architecture: x86_64
|
||||||
type: Linux
|
type: Linux
|
||||||
|
|||||||
@@ -97,9 +97,9 @@ node_templates:
|
|||||||
type: tosca.nodes.Compute
|
type: tosca.nodes.Compute
|
||||||
properties:
|
properties:
|
||||||
# compute properties (flavor)
|
# compute properties (flavor)
|
||||||
disk_size: 10
|
disk_size: 10 GB
|
||||||
num_cpus: { get_input: cpus }
|
num_cpus: { get_input: cpus }
|
||||||
mem_size: 4096
|
mem_size: 4096 MB
|
||||||
capabilities:
|
capabilities:
|
||||||
os:
|
os:
|
||||||
properties:
|
properties:
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ class ToscaTemplateTest(TestCase):
|
|||||||
'''Test property value'''
|
'''Test property value'''
|
||||||
for property in tpl.properties:
|
for property in tpl.properties:
|
||||||
if property.name == 'mem_size':
|
if property.name == 'mem_size':
|
||||||
self.assertEqual(property.value, 4096)
|
self.assertEqual(property.value, '4096 MB')
|
||||||
'''Test capability'''
|
'''Test capability'''
|
||||||
self.assertIn('os', [cap.name for cap in tpl.capabilities])
|
self.assertIn('os', [cap.name for cap in tpl.capabilities])
|
||||||
os_props_objs = None
|
os_props_objs = None
|
||||||
|
|||||||
Reference in New Issue
Block a user