Support version of tosca_simple_yaml_1_1
[parser.git] / tosca2heat / tosca-parser / toscaparser / tests / test_toscatpl.py
index 3fd49bf..fd8ee90 100644 (file)
 
 import os
 import six
-
 from toscaparser.common import exception
 import toscaparser.elements.interfaces as ifaces
 from toscaparser.elements.nodetype import NodeType
+from toscaparser.elements.portspectype import PortSpec
 from toscaparser.functions import GetInput
 from toscaparser.functions import GetProperty
 from toscaparser.nodetemplate import NodeTemplate
@@ -26,16 +26,19 @@ import toscaparser.utils.yamlparser
 
 
 class ToscaTemplateTest(TestCase):
-
     '''TOSCA template.'''
     tosca_tpl = os.path.join(
         os.path.dirname(os.path.abspath(__file__)),
         "data/tosca_single_instance_wordpress.yaml")
-    tosca = ToscaTemplate(tosca_tpl)
-
+    params = {'db_name': 'my_wordpress', 'db_user': 'my_db_user',
+              'db_root_pwd': '12345678'}
+    tosca = ToscaTemplate(tosca_tpl, parsed_params=params)
     tosca_elk_tpl = os.path.join(
         os.path.dirname(os.path.abspath(__file__)),
         "data/tosca_elk.yaml")
+    tosca_repo_tpl = os.path.join(
+        os.path.dirname(os.path.abspath(__file__)),
+        "data/repositories/tosca_repositories_test_definition.yaml")
 
     def test_version(self):
         self.assertEqual(self.tosca.version, "tosca_simple_yaml_1_0")
@@ -134,6 +137,17 @@ class ToscaTemplateTest(TestCase):
                 self.assertEqual('Linux', os_props['type'].value)
                 self.assertEqual('Linux', os_type_prop)
 
+    def test_node_inheritance_type(self):
+        wordpress_node = [
+            node for node in self.tosca.nodetemplates
+            if node.name == 'wordpress'][0]
+        self.assertTrue(
+            wordpress_node.is_derived_from("tosca.nodes.WebApplication"))
+        self.assertTrue(
+            wordpress_node.is_derived_from("tosca.nodes.Root"))
+        self.assertFalse(
+            wordpress_node.is_derived_from("tosca.policies.Root"))
+
     def test_outputs(self):
         self.assertEqual(
             ['website_url'],
@@ -160,14 +174,14 @@ class ToscaTemplateTest(TestCase):
                 self.assertEqual(3, len(interface.inputs))
                 TestCase.skip(self, 'bug #1440247')
                 wp_db_port = interface.inputs['wp_db_port']
-                self.assertTrue(isinstance(wp_db_port, GetProperty))
+                self.assertIsInstance(wp_db_port, GetProperty)
                 self.assertEqual('get_property', wp_db_port.name)
                 self.assertEqual(['SELF',
                                   'database_endpoint',
                                   'port'],
                                  wp_db_port.args)
                 result = wp_db_port.result()
-                self.assertTrue(isinstance(result, GetInput))
+                self.assertIsInstance(result, GetInput)
             else:
                 raise AssertionError(
                     'Unexpected interface: {0}'.format(interface.name))
@@ -186,6 +200,7 @@ class ToscaTemplateTest(TestCase):
             compute_type = NodeType(tpl.type)
             self.assertEqual(
                 sorted(['tosca.capabilities.Container',
+                        'tosca.capabilities.Endpoint.Admin',
                         'tosca.capabilities.Node',
                         'tosca.capabilities.OperatingSystem',
                         'tosca.capabilities.network.Bindable',
@@ -201,6 +216,10 @@ class ToscaTemplateTest(TestCase):
         tosca_tpl = self._load_template('test_no_outputs_in_template.yaml')
         self.assertEqual(0, len(tosca_tpl.outputs))
 
+    def test_template_file_with_suffix_yml(self):
+        tosca_tpl = self._load_template('custom_types/wordpress.yml')
+        self.assertIsNotNone(tosca_tpl)
+
     def test_relationship_interface(self):
         template = ToscaTemplate(self.tosca_elk_tpl)
         for node_tpl in template.nodetemplates:
@@ -211,6 +230,8 @@ class ToscaTemplateTest(TestCase):
                 for key in relation.keys():
                     rel_tpl = relation.get(key).get_relationship_template()
                     if rel_tpl:
+                        self.assertTrue(rel_tpl[0].is_derived_from(
+                            "tosca.relationships.Root"))
                         interfaces = rel_tpl[0].interfaces
                         for interface in interfaces:
                             self.assertEqual(config_interface,
@@ -236,6 +257,18 @@ class ToscaTemplateTest(TestCase):
                     expected_hosts,
                     sorted([v.type for v in node_tpl.relationships.values()]))
 
+    def test_repositories(self):
+        template = ToscaTemplate(self.tosca_repo_tpl)
+        self.assertEqual(
+            ['repo_code0', 'repo_code1', 'repo_code2'],
+            sorted([input.name for input in template.repositories]))
+
+        input_name = "repo_code2"
+        expected_url = "https://github.com/nandinivemula/intern/master"
+        for input in template.repositories:
+            if input.name == input_name:
+                self.assertEqual(input.url, expected_url)
+
     def test_template_macro(self):
         template = ToscaTemplate(self.tosca_elk_tpl)
         for node_tpl in template.nodetemplates:
@@ -258,7 +291,7 @@ class ToscaTemplateTest(TestCase):
         """
         tosca_tpl = os.path.join(
             os.path.dirname(os.path.abspath(__file__)),
-            "data/test_requirements.yaml")
+            "data/requirements/test_requirements.yaml")
         tosca = ToscaTemplate(tosca_tpl)
         for node_tpl in tosca.nodetemplates:
             if node_tpl.name == 'my_app':
@@ -410,21 +443,30 @@ class ToscaTemplateTest(TestCase):
         tosca_tpl = os.path.join(
             os.path.dirname(os.path.abspath(__file__)),
             "data/tosca_single_instance_wordpress.yaml")
-        tosca = ToscaTemplate(tosca_tpl)
+        params = {'db_name': 'my_wordpress', 'db_user': 'my_db_user',
+                  'db_root_pwd': '12345678'}
+        tosca = ToscaTemplate(tosca_tpl, parsed_params=params)
         self.assertTrue(tosca.topology_template.custom_defs)
 
     def test_local_template_with_url_import(self):
         tosca_tpl = os.path.join(
             os.path.dirname(os.path.abspath(__file__)),
             "data/tosca_single_instance_wordpress_with_url_import.yaml")
-        tosca = ToscaTemplate(tosca_tpl)
+        tosca = ToscaTemplate(tosca_tpl,
+                              parsed_params={'db_root_pwd': '123456'})
         self.assertTrue(tosca.topology_template.custom_defs)
 
     def test_url_template_with_local_relpath_import(self):
         tosca_tpl = ('https://raw.githubusercontent.com/openstack/'
                      'tosca-parser/master/toscaparser/tests/data/'
                      'tosca_single_instance_wordpress.yaml')
-        tosca = ToscaTemplate(tosca_tpl, None, False)
+        tosca = ToscaTemplate(tosca_tpl, a_file=False,
+                              parsed_params={"db_name": "mysql",
+                                             "db_user": "mysql",
+                                             "db_root_pwd": "1234",
+                                             "db_pwd": "5678",
+                                             "db_port": 3306,
+                                             "cpus": 4})
         self.assertTrue(tosca.topology_template.custom_defs)
 
     def test_url_template_with_local_abspath_import(self):
@@ -445,26 +487,35 @@ class ToscaTemplateTest(TestCase):
         tosca_tpl = ('https://raw.githubusercontent.com/openstack/'
                      'tosca-parser/master/toscaparser/tests/data/'
                      'tosca_single_instance_wordpress_with_url_import.yaml')
-        tosca = ToscaTemplate(tosca_tpl, None, False)
+        tosca = ToscaTemplate(tosca_tpl, a_file=False,
+                              parsed_params={"db_root_pwd": "1234"})
         self.assertTrue(tosca.topology_template.custom_defs)
 
     def test_csar_parsing_wordpress(self):
         csar_archive = os.path.join(
             os.path.dirname(os.path.abspath(__file__)),
             'data/CSAR/csar_wordpress.zip')
-        self.assertTrue(ToscaTemplate(csar_archive))
+        self.assertTrue(ToscaTemplate(csar_archive,
+                                      parsed_params={"db_name": "mysql",
+                                                     "db_user": "mysql",
+                                                     "db_root_pwd": "1234",
+                                                     "db_pwd": "5678",
+                                                     "db_port": 3306,
+                                                     "cpus": 4}))
 
     def test_csar_parsing_elk_url_based(self):
         csar_archive = ('https://github.com/openstack/tosca-parser/raw/master/'
                         'toscaparser/tests/data/CSAR/csar_elk.zip')
-        self.assertTrue(ToscaTemplate(csar_archive, None, False))
+        self.assertTrue(ToscaTemplate(csar_archive, a_file=False,
+                                      parsed_params={"my_cpus": 4}))
 
     def test_nested_imports_in_templates(self):
         tosca_tpl = os.path.join(
             os.path.dirname(os.path.abspath(__file__)),
             "data/test_instance_nested_imports.yaml")
         tosca = ToscaTemplate(tosca_tpl)
-        expected_custom_types = ['tosca.nodes.WebApplication.WordPress',
+        expected_custom_types = ['tosca.nodes.SoftwareComponent.Kibana',
+                                 'tosca.nodes.WebApplication.WordPress',
                                  'test_namespace_prefix.Rsyslog',
                                  'Test2ndRsyslogType',
                                  'test_2nd_namespace_prefix.Rsyslog',
@@ -525,7 +576,8 @@ class ToscaTemplateTest(TestCase):
         exception.ExceptionCollector.assertExceptionMessage(
             exception.UnknownFieldError, err7_msg)
 
-        err8_msg = _('\'Node template "server1" was not found.\'')
+        err8_msg = _('\'Node template "server1" was not found in '
+                     '"webserver".\'')
         exception.ExceptionCollector.assertExceptionMessage(
             KeyError, err8_msg)
 
@@ -534,6 +586,10 @@ class ToscaTemplateTest(TestCase):
         exception.ExceptionCollector.assertExceptionMessage(
             exception.MissingRequiredFieldError, err9_msg)
 
+        err10_msg = _('Type "tosca.nodes.XYZ" is not a valid type.')
+        exception.ExceptionCollector.assertExceptionMessage(
+            exception.InvalidTypeError, err10_msg)
+
     def test_invalid_section_names(self):
         tosca_tpl = os.path.join(
             os.path.dirname(os.path.abspath(__file__)),
@@ -565,7 +621,7 @@ class ToscaTemplateTest(TestCase):
         tosca_tpl = os.path.join(
             os.path.dirname(os.path.abspath(__file__)),
             "data/CSAR/csar_elk.csar")
-        tosca = ToscaTemplate(tosca_tpl)
+        tosca = ToscaTemplate(tosca_tpl, parsed_params={"my_cpus": 2})
         self.assertTrue(tosca.topology_template.custom_defs)
 
     def test_available_rel_tpls(self):
@@ -610,6 +666,17 @@ class ToscaTemplateTest(TestCase):
 
         self.assertEqual(tosca.version, "tosca_simple_yaml_1_0")
 
+    def test_yaml_dict_tpl_with_version_1_1(self):
+        test_tpl = os.path.join(
+            os.path.dirname(os.path.abspath(__file__)),
+            "data/tosca_helloworld_with_version_1_1.yaml")
+
+        yaml_dict_tpl = toscaparser.utils.yamlparser.load_yaml(test_tpl)
+
+        tosca = ToscaTemplate(yaml_dict_tpl=yaml_dict_tpl)
+
+        self.assertEqual(tosca.version, "tosca_simple_yaml_1_1")
+
     def test_yaml_dict_tpl_with_params_and_url_import(self):
         test_tpl = os.path.join(
             os.path.dirname(os.path.abspath(__file__)),
@@ -631,9 +698,10 @@ class ToscaTemplateTest(TestCase):
             "data/tosca_single_instance_wordpress.yaml")
 
         yaml_dict_tpl = toscaparser.utils.yamlparser.load_yaml(test_tpl)
-
+        params = {'db_name': 'my_wordpress', 'db_user': 'my_db_user',
+                  'db_root_pwd': '12345678'}
         self.assertRaises(exception.ValidationError, ToscaTemplate, None,
-                          None, False, yaml_dict_tpl)
+                          params, False, yaml_dict_tpl)
         err_msg = (_('Relative file name "custom_types/wordpress.yaml" '
                      'cannot be used in a pre-parsed input template.'))
         exception.ExceptionCollector.assertExceptionMessage(ImportError,
@@ -664,6 +732,8 @@ class ToscaTemplateTest(TestCase):
         tosca = ToscaTemplate(tosca_tpl)
 
         for policy in tosca.topology_template.policies:
+            self.assertTrue(
+                policy.is_derived_from("tosca.policies.Root"))
             if policy.name == 'my_compute_placement_policy':
                 self.assertEqual('tosca.policies.Placement', policy.type)
                 self.assertEqual(['my_server_1', 'my_server_2'],
@@ -684,6 +754,8 @@ class ToscaTemplateTest(TestCase):
         tosca = ToscaTemplate(tosca_tpl)
 
         for policy in tosca.topology_template.policies:
+            self.assertTrue(
+                policy.is_derived_from("tosca.policies.Root"))
             if policy.name == 'my_groups_placement':
                 self.assertEqual('mycompany.mytypes.myScalingPolicy',
                                  policy.type)
@@ -701,7 +773,7 @@ class ToscaTemplateTest(TestCase):
     def test_node_filter(self):
         tosca_tpl = os.path.join(
             os.path.dirname(os.path.abspath(__file__)),
-            "data/test_node_filter.yaml")
+            "data/node_filter/test_node_filter.yaml")
         ToscaTemplate(tosca_tpl)
 
     def test_attributes_inheritance(self):
@@ -713,7 +785,7 @@ class ToscaTemplateTest(TestCase):
     def test_repositories_definition(self):
         tosca_tpl = os.path.join(
             os.path.dirname(os.path.abspath(__file__)),
-            "data/test_repositories_definition.yaml")
+            "data/repositories/test_repositories_definition.yaml")
         ToscaTemplate(tosca_tpl)
 
     def test_custom_caps_def(self):
@@ -728,5 +800,71 @@ class ToscaTemplateTest(TestCase):
             "data/test_tosca_custom_rel_with_script.yaml")
         tosca = ToscaTemplate(tosca_tpl)
         rel = tosca.relationship_templates[0]
+        self.assertEqual(rel.type, "tosca.relationships.HostedOn")
+        self.assertTrue(rel.is_derived_from("tosca.relationships.Root"))
         self.assertEqual(len(rel.interfaces), 1)
         self.assertEqual(rel.interfaces[0].type, "Configure")
+
+    def test_various_portspec_errors(self):
+        tosca_tpl = os.path.join(
+            os.path.dirname(os.path.abspath(__file__)),
+            "data/datatypes/test_datatype_portspec_add_req.yaml")
+        self.assertRaises(exception.ValidationError, ToscaTemplate, tosca_tpl,
+                          None)
+
+        # TODO(TBD) find way to reuse error messages from constraints.py
+        msg = (_('The value "%(pvalue)s" of property "%(pname)s" is out of '
+                 'range "(min:%(vmin)s, max:%(vmax)s)".') %
+               dict(pname=PortSpec.SOURCE,
+                    pvalue='0',
+                    vmin='1',
+                    vmax='65535'))
+        exception.ExceptionCollector.assertExceptionMessage(
+            exception.ValidationError, msg)
+
+        # Test value below range min.
+        msg = (_('The value "%(pvalue)s" of property "%(pname)s" is out of '
+                 'range "(min:%(vmin)s, max:%(vmax)s)".') %
+               dict(pname=PortSpec.SOURCE,
+                    pvalue='1',
+                    vmin='2',
+                    vmax='65534'))
+        exception.ExceptionCollector.assertExceptionMessage(
+            exception.RangeValueError, msg)
+
+        # Test value above range max.
+        msg = (_('The value "%(pvalue)s" of property "%(pname)s" is out of '
+                 'range "(min:%(vmin)s, max:%(vmax)s)".') %
+               dict(pname=PortSpec.SOURCE,
+                    pvalue='65535',
+                    vmin='2',
+                    vmax='65534'))
+        exception.ExceptionCollector.assertExceptionMessage(
+            exception.RangeValueError, msg)
+
+    def test_containers(self):
+        tosca_tpl = os.path.join(
+            os.path.dirname(os.path.abspath(__file__)),
+            "data/containers/test_container_docker_mysql.yaml")
+        ToscaTemplate(tosca_tpl, parsed_params={"mysql_root_pwd": "12345678"})
+
+    def test_endpoint_on_compute(self):
+        tosca_tpl = os.path.join(
+            os.path.dirname(os.path.abspath(__file__)),
+            "data/test_endpoint_on_compute.yaml")
+        ToscaTemplate(tosca_tpl)
+
+    def test_nested_dsl_def(self):
+        tosca_tpl = os.path.join(
+            os.path.dirname(os.path.abspath(__file__)),
+            "data/dsl_definitions/test_nested_dsl_def.yaml")
+        self.assertIsNotNone(ToscaTemplate(tosca_tpl))
+
+    def test_multiple_policies(self):
+        tosca_tpl = os.path.join(
+            os.path.dirname(os.path.abspath(__file__)),
+            "data/policies/test_tosca_nfv_multiple_policies.yaml")
+        tosca = ToscaTemplate(tosca_tpl)
+        self.assertEqual(
+            ['ALRM1', 'SP1', 'SP2'],
+            sorted([policy.name for policy in tosca.policies]))