toscaparser: Avoid exception when creating our own 37/65437/4
authorLianhao Lu <lianhao.lu@intel.com>
Mon, 19 Nov 2018 08:28:05 +0000 (16:28 +0800)
committerLianhao Lu <lianhao.lu@intel.com>
Wed, 28 Nov 2018 04:36:45 +0000 (12:36 +0800)
Fixed the bug that new exception is thrown when we're creating our own
exceptions.

JIRA: PARSER-187

Change-Id: If4c445eb9b5c6ff26d40cf6b3fa7f19cd31fd425
Signed-off-by: Lianhao Lu <lianhao.lu@intel.com>
tosca2heat/tosca-parser/toscaparser/elements/tosca_type_validation.py
tosca2heat/tosca-parser/toscaparser/tests/data/custom_types/invalid_type.yaml [new file with mode: 0644]
tosca2heat/tosca-parser/toscaparser/tests/data/test_import_invalid_type.yaml [new file with mode: 0644]
tosca2heat/tosca-parser/toscaparser/tests/test_toscatplvalidation.py

index 89a6a03..8b49f48 100644 (file)
@@ -49,12 +49,12 @@ class TypeValidation(object):
         for name in custom_type:
             if name not in self.ALLOWED_TYPE_SECTIONS:
                 ExceptionCollector.appendException(
-                    UnknownFieldError(what='Template ' + (self.import_def),
+                    UnknownFieldError(what='Template ' + str(self.import_def),
                                       field=name))
 
     def _validate_type_version(self, version):
         if version not in self.VALID_TEMPLATE_VERSIONS:
             ExceptionCollector.appendException(
                 InvalidTemplateVersion(
-                    what=version + ' in ' + self.import_def,
+                    what=version + ' in ' + str(self.import_def),
                     valid_versions=', '. join(self.VALID_TEMPLATE_VERSIONS)))
diff --git a/tosca2heat/tosca-parser/toscaparser/tests/data/custom_types/invalid_type.yaml b/tosca2heat/tosca-parser/toscaparser/tests/data/custom_types/invalid_type.yaml
new file mode 100644 (file)
index 0000000..4d3a0b0
--- /dev/null
@@ -0,0 +1,8 @@
+tosca_definitions_version: tosca_simple_yaml_1_1
+
+annotation_types:
+  org.openecomp.annotations.Source:
+    description: Indicates the origin source of an input
+    properties:
+      source_type:
+        type: string
diff --git a/tosca2heat/tosca-parser/toscaparser/tests/data/test_import_invalid_type.yaml b/tosca2heat/tosca-parser/toscaparser/tests/data/test_import_invalid_type.yaml
new file mode 100644 (file)
index 0000000..f2c1876
--- /dev/null
@@ -0,0 +1,11 @@
+tosca_definitions_version: tosca_simple_yaml_1_1
+
+imports:
+  - invalid: custom_types/invalid_type.yaml
+
+description: Test to import a template with an invalid type.
+
+topology_template:
+  node_templates:
+    test:
+      type: tosca.nodes.Root
index a8b1590..ea27bcb 100644 (file)
@@ -1442,6 +1442,17 @@ heat-translator/master/translator/tests/data/custom_types/wordpress.yaml
             (_('The template version "tosca_xyz" is invalid. Valid versions '
                'are "%s".') % valid_versions))
 
+    def test_import_invalid_type(self):
+        tosca_tpl = os.path.join(
+            os.path.dirname(os.path.abspath(__file__)),
+            "data/test_import_invalid_type.yaml")
+        self.assertRaises(exception.ValidationError, ToscaTemplate, tosca_tpl)
+        exception.ExceptionCollector.assertExceptionMessage(
+            exception.UnknownFieldError,
+            (_("Template {'invalid': 'custom_types/invalid_type.yaml'} "
+               'contains unknown field "annotation_types". Refer to the '
+               'definition to verify valid values.')))
+
     def test_node_template_capabilities_properties(self):
         # validating capability property values
         tpl_snippet = '''