Update tosca lib to version 0.5
[parser.git] / tosca2heat / tosca-parser / toscaparser / tests / test_properties.py
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from testtools import matchers
+
 from toscaparser.common import exception
 from toscaparser.elements.property_definition import PropertyDef
+from toscaparser.nodetemplate import NodeTemplate
 from toscaparser.properties import Property
 from toscaparser.tests.base import TestCase
 from toscaparser.utils.gettextutils import _
@@ -179,10 +182,12 @@ class PropertyTest(TestCase):
     def test_timestamp_invalid(self):
         test_property_schema = {'type': 'timestamp'}
         # invalid timestamp - day out of range
-        propertyInstance = Property('test_property', '2015-04-115T02:59:43.1Z',
+        value = '2015-04-115T02:59:43.1Z'
+        propertyInstance = Property('test_property', value,
                                     test_property_schema)
         error = self.assertRaises(ValueError, propertyInstance.validate)
-        self.assertEqual(_('day is out of range for month'), str(error))
+        expected_message = (_('"%s" is not a valid timestamp.') % value)
+        self.assertThat(str(error), matchers.StartsWith(expected_message))
 
     def test_required(self):
         test_property_schema = {'type': 'string'}
@@ -191,7 +196,6 @@ class PropertyTest(TestCase):
         self.assertEqual(True, propertyInstance.required)
 
     def test_proprety_inheritance(self):
-        from toscaparser.nodetemplate import NodeTemplate
 
         tosca_custom_def = '''
           tosca.nodes.SoftwareComponent.MySoftware:
@@ -214,11 +218,7 @@ class PropertyTest(TestCase):
         expected_properties = ['component_version',
                                'install_path']
 
-        nodetemplates = yamlparser.\
-            simple_parse(tosca_node_template)['node_templates']
-        custom_def = yamlparser.simple_parse(tosca_custom_def)
-        name = list(nodetemplates.keys())[0]
-        tpl = NodeTemplate(name, nodetemplates, custom_def)
+        tpl = self._get_nodetemplate(tosca_node_template, tosca_custom_def)
         self.assertIsNone(tpl.validate())
         self.assertEqual(expected_properties,
                          sorted(tpl.get_properties().keys()))
@@ -234,3 +234,135 @@ class PropertyTest(TestCase):
                                   'prop', None, schema['properties']['prop'])
         self.assertEqual(_('Schema definition of "prop" must have a "type" '
                            'attribute.'), str(error))
+
+    def test_invalid_required_value(self):
+        tpl_snippet = '''
+         properties:
+           prop:
+             type: tosca.mytesttype.Test
+             required: dunno
+        '''
+        schema = yamlparser.simple_parse(tpl_snippet)
+        error = self.assertRaises(exception.InvalidSchemaError, PropertyDef,
+                                  'prop', None, schema['properties']['prop'])
+
+        valid_values = ', '.join(PropertyDef.VALID_REQUIRED_VALUES)
+        expected_message = (_('Schema definition of "prop" has "required" '
+                              'attribute with invalid value "dunno". The '
+                              'value must be one of "%s".') % valid_values)
+        self.assertEqual(expected_message, str(error))
+
+    def test_invalid_property_status(self):
+        tpl_snippet = '''
+         properties:
+           prop:
+             type: string
+             status: unknown
+        '''
+        schema = yamlparser.simple_parse(tpl_snippet)
+        error = self.assertRaises(exception.InvalidSchemaError, PropertyDef,
+                                  'prop', None, schema['properties']['prop'])
+
+        valid_values = ', '.join(PropertyDef.VALID_STATUS_VALUES)
+        expected_message = (_('Schema definition of "prop" has "status" '
+                              'attribute with invalid value "unknown". The '
+                              'value must be one of "%s".') % valid_values)
+        self.assertEqual(expected_message, str(error))
+
+    def test_capability_proprety_inheritance(self):
+        tosca_custom_def_example1 = '''
+          tosca.capabilities.ScalableNew:
+            derived_from: tosca.capabilities.Scalable
+            properties:
+              max_instances:
+                type: integer
+                default: 0
+                required: no
+
+          tosca.nodes.ComputeNew:
+            derived_from: tosca.nodes.Compute
+            capabilities:
+              scalable:
+                type: tosca.capabilities.ScalableNew
+        '''
+
+        tosca_node_template_example1 = '''
+          node_templates:
+            compute_instance:
+              type: tosca.nodes.ComputeNew
+              capabilities:
+                scalable:
+                  properties:
+                    min_instances: 1
+        '''
+
+        tosca_custom_def_example2 = '''
+          tosca.nodes.ComputeNew:
+            derived_from: tosca.nodes.Compute
+            capabilities:
+              new_cap:
+                type: tosca.capabilities.Scalable
+        '''
+
+        tosca_node_template_example2 = '''
+          node_templates:
+            db_server:
+                type: tosca.nodes.ComputeNew
+                capabilities:
+                  host:
+                   properties:
+                     num_cpus: 1
+        '''
+
+        tpl1 = self._get_nodetemplate(tosca_node_template_example1,
+                                      tosca_custom_def_example1)
+        self.assertIsNone(tpl1.validate())
+
+        tpl2 = self._get_nodetemplate(tosca_node_template_example2,
+                                      tosca_custom_def_example2)
+        self.assertIsNone(tpl2.validate())
+
+    def _get_nodetemplate(self, tpl_snippet,
+                          custom_def_snippet=None):
+        nodetemplates = yamlparser.\
+            simple_parse(tpl_snippet)['node_templates']
+        custom_def = []
+        if custom_def_snippet:
+            custom_def = yamlparser.simple_parse(custom_def_snippet)
+        name = list(nodetemplates.keys())[0]
+        tpl = NodeTemplate(name, nodetemplates, custom_def)
+        return tpl
+
+    def test_explicit_relationship_proprety(self):
+
+        tosca_node_template = '''
+          node_templates:
+
+            client_node:
+              type: tosca.nodes.Compute
+              requirements:
+                - local_storage:
+                    node: my_storage
+                    relationship:
+                      type: AttachesTo
+                      properties:
+                        location: /mnt/disk
+
+            my_storage:
+              type: tosca.nodes.BlockStorage
+              properties:
+                size: 1 GB
+        '''
+
+        expected_properties = ['location']
+
+        nodetemplates = yamlparser.\
+            simple_parse(tosca_node_template)['node_templates']
+        tpl = NodeTemplate('client_node', nodetemplates, [])
+
+        self.assertIsNone(tpl.validate())
+        rel_tpls = []
+        for relationship, trgt in tpl.relationships.items():
+            rel_tpls.extend(trgt.get_relationship_template())
+        self.assertEqual(expected_properties,
+                         sorted(rel_tpls[0].get_properties().keys()))