def _get_data_from_csar(tosca, context, id): for tp in tosca.nested_tosca_templates_with_topology: policies = tp.tpl.get("policies") if policies: levels = _get_instantiation_levels_from_policy(policies) for policy_tpl in policies: _validate_instantiation_levels(policy_tpl, levels) _validate_sw_image_data_for_artifacts(tosca) vnf_data = _get_vnf_data(tosca.nodetemplates) if not vnf_data: error_msg = "VNF properties are mandatory" raise exceptions.InvalidCSAR(error_msg) flavours = _populate_flavour_data(tosca) if not flavours: error_msg = "No VNF flavours are available" raise exceptions.InvalidCSAR(error_msg) csar = CSAR(tosca.input_path, tosca.a_file) vnf_artifacts = [] if csar.validate(): vnf_artifacts = _get_vnf_artifacts(csar) return vnf_data, flavours, vnf_artifacts
def test_csar_main_template(self): path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip") csar = CSAR(path) yaml_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data/tosca_helloworld.yaml") expected_yaml = toscaparser.utils.yamlparser.load_yaml(yaml_file) self.assertEqual(expected_yaml, csar.get_main_template_yaml())
def test_valid_csar_with_url_import_and_script(self): path = os.path.join(self.base_path, "data/CSAR/csar_wordpress_with_url" "_import_and_script.zip") csar = CSAR(path) self.assertIsNone(csar.validate()) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_csar_valid_artifact(self): path = os.path.join(self.base_path, "data/CSAR/csar_wordpress_valid_artifact.zip") csar = CSAR(path) self.assertTrue(csar.validate()) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_valid_csar_with_url_import_and_script(self): path = os.path.join( self.base_path, "data/CSAR/csar_wordpress_with_url" "_import_and_script.zip") csar = CSAR(path) self.assertTrue(csar.validate()) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_decompress(self): path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip") csar = CSAR(path) tmp_dir = csar.decompress() zf = zipfile.ZipFile(path) for name in zf.namelist(): tmp_path = os.path.join(tmp_dir, name) self.assertTrue(os.path.isdir(tmp_path) or os.path.isfile(tmp_path))
def test_csar_main_template(self): path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip") csar = CSAR(path) yaml_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data/tosca_helloworld.yaml") expected_yaml = toscaparser.utils.yamlparser.load_yaml(yaml_file) self.assertEqual(expected_yaml, csar.get_main_template_yaml()) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_csar_with_root_level_yaml_and_tosca_metadata(self): path = os.path.join( self.base_path, "data/CSAR/csar_root_level_" "yaml_and_tosca_metadata.zip") csar = CSAR(path) yaml_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data/CSAR/tosca_meta_file.yaml") expected_yaml = toscaparser.utils.yamlparser.load_yaml(yaml_file) self.assertEqual(expected_yaml, csar.get_main_template_yaml()) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_csar_with_multilevel_imports_valid(self): path = os.path.join( self.base_path, "data/CSAR/csar_valid_multilevel_imports_validation.zip") csar = CSAR(path) yaml_file = os.path.join( os.path.dirname(os.path.abspath(__file__)), "data/CSAR/multi_level_imports_response.yaml") expected_yaml = toscaparser.utils.yamlparser.load_yaml(yaml_file) self.assertEqual(expected_yaml, csar.get_main_template_yaml()) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_decompress(self): path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip") csar = CSAR(path) csar.decompress() zf = zipfile.ZipFile(path) for name in zf.namelist(): tmp_path = os.path.join(csar.temp_dir, name) self.assertTrue(os.path.isdir(tmp_path) or os.path.isfile(tmp_path)) shutil.rmtree(csar.temp_dir) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_decompress(self): path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip") csar = CSAR(path) csar.decompress() zf = zipfile.ZipFile(path) for name in zf.namelist(): tmp_path = os.path.join(csar.temp_dir, name) self.assertTrue( os.path.isdir(tmp_path) or os.path.isfile(tmp_path)) shutil.rmtree(csar.temp_dir) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def _get_path(self, path): if path.lower().endswith('.yaml'): return path elif path.lower().endswith('.zip'): # a CSAR archive csar = CSAR(path, self.a_file) csar.validate() folder = csar.decompress() self.a_file = True # the file has been decompressed locally return os.path.join(folder, csar.get_main_template()) else: raise ValueError(_("%(path)s is not a valid file.") % {'path': path})
def _get_path(self, path): if path.lower().endswith('.yaml'): return path elif path.lower().endswith(('.zip', '.csar')): # a CSAR archive csar = CSAR(path, self.a_file) if csar.validate(): csar.decompress() self.a_file = True # the file has been decompressed locally return os.path.join(csar.temp_dir, csar.get_main_template()) else: ExceptionCollector.appendException( ValueError(_('"%(path)s" is not a valid file.') % {'path': path}))
def test_file_exists(self): path = os.path.join(self.base_path, "data/CSAR/csar_not_there.zip") csar = CSAR(path) error = self.assertRaises(ValidationError, csar.validate) self.assertEqual(_('"%s" does not exist.') % path, str(error)) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_url_is_zip(self): path = "https://github.com/openstack/tosca-parser/raw/master/" \ "toscaparser/tests/data/CSAR/csar_not_zip.zip" csar = CSAR(path, False) error = self.assertRaises(ValidationError, csar.validate) self.assertEqual(_('"%s" is not a valid zip file.') % path, str(error)) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_csar_invalid_import_path(self): path = os.path.join( self.base_path, "data/CSAR/csar_wordpress_invalid_import_path.zip") csar = CSAR(path) error = self.assertRaises(URLException, csar.validate) assert str(error).endswith( '"Invalid_import_path/wordpress.yaml" is not valid.') self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_entry_def_exists(self): path = os.path.join(self.base_path, "data/CSAR/csar_invalid_entry_def.zip") csar = CSAR(path) error = self.assertRaises(ValidationError, csar.validate) self.assertEqual( _('The "Entry-Definitions" file defined in the CSAR ' '"%s" does not exist.') % path, str(error)) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_csar_invalid_import_path(self): path = os.path.join( self.base_path, "data/CSAR/csar_wordpress_invalid_import_path.zip") csar = CSAR(path) error = self.assertRaises(ImportError, csar.validate) self.assertEqual( _('Import "Invalid_import_path/wordpress.yaml" is' ' not valid.'), str(error)) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_csar_with_multiple_root_level_yaml_files_error(self): path = os.path.join(self.base_path, "data/CSAR/csar_two_root_level_yaml.zip") csar = CSAR(path) error = self.assertRaises(ValidationError, csar.validate) self.assertEqual( _('CSAR file should contain only one root level' ' yaml file. Found "2" yaml file(s).'), str(error)) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_metadata_is_yaml(self): path = os.path.join(self.base_path, "data/CSAR/csar_metadata_not_yaml.zip") csar = CSAR(path) error = self.assertRaises(ValidationError, csar.validate) self.assertEqual( _('The file "TOSCA-Metadata/TOSCA.meta" in the CSAR ' '"%s" does not contain valid YAML content.') % path, str(error)) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_csar_invalid_artifact(self): path = os.path.join(self.base_path, "data/CSAR/csar_wordpress_invalid_artifact.zip") csar = CSAR(path) error = self.assertRaises(ValueError, csar.validate) self.assertTrue( str(error) == _('The resource "Scripts/WordPress/configure.sh" ' 'does not exist.')) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_metadata_exists(self): path = os.path.join(self.base_path, "data/CSAR/csar_missing_metadata.zip") csar = CSAR(path) error = self.assertRaises(ValidationError, csar.validate) self.assertEqual( _('The CSAR "%s" is missing the required metadata ' '"Entry-Definitions" in ' '"TOSCA-Metadata/TOSCA.meta".') % path, str(error)) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_valid_metadata_file_exists(self): path = os.path.join(self.base_path, "data/CSAR/csar_wrong_metadata_file.zip") csar = CSAR(path) error = self.assertRaises(ValidationError, csar.validate) self.assertEqual( _('"%s" is not a valid CSAR as it does not contain ' 'the required file "TOSCA.meta" in the folder ' '"TOSCA-Metadata".') % path, str(error)) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_csar_with_multilevel_imports_invalid(self): path = os.path.join( self.base_path, "data/CSAR/csar_invalid_multilevel" "_imports_validation.zip") csar = CSAR(path) error = self.assertRaises(ValueError, csar.validate) self.assertEqual( _('The resource "%s" does ' 'not exist.') % 'Files/images/' 'cirros-0.4.0-x86_64-disk.img', str(error)) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_csar_root_yaml_with_tosca_definition_1_0_error(self): path = os.path.join( self.base_path, "data/CSAR/csar_root_yaml" "_with_tosca_definition1_0.zip") csar = CSAR(path) error = self.assertRaises(ValidationError, csar.validate) self.assertEqual( _('"%s" is not a valid CSAR as it does not contain' ' the required file "TOSCA.meta" in the folder ' '"TOSCA-Metadata".') % path, str(error)) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_csar_invalid_import_url(self): path = os.path.join(self.base_path, "data/CSAR/csar_wordpress_invalid_import_url.zip") csar = CSAR(path) error = self.assertRaises(URLException, csar.validate) self.assertEqual( _('Import ' '"https://raw.githubusercontent.com/openstack/' 'tosca-parser/master/toscaparser/tests/data/CSAR/' 'tosca_single_instance_wordpress/Definitions/' 'wordpress1.yaml" is not valid.'), str(error)) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_csar_invalid_script_url(self): path = os.path.join(self.base_path, "data/CSAR/csar_wordpress_invalid_script_url.zip") csar = CSAR(path) error = self.assertRaises(URLException, csar.validate) self.assertEqual( _('The resource at ' '"https://raw.githubusercontent.com/openstack/' 'tosca-parser/master/toscaparser/tests/data/CSAR/' 'tosca_single_instance_wordpress/Scripts/WordPress/' 'install1.sh" cannot be accessed.'), str(error)) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def _get_path(self, path): if path.lower().endswith('.yaml'): return path elif path.lower().endswith(('.zip', '.csar')): # a CSAR archive csar = CSAR(path, self.a_file) if csar.validate(): csar.decompress() self.a_file = True # the file has been decompressed locally return os.path.join(csar.temp_dir, csar.get_main_template()) else: ExceptionCollector.appendException( ValueError( _('"%(path)s" is not a valid file.') % {'path': path}))
def test_csar_invalid_import_url(self): path = os.path.join(self.base_path, "data/CSAR/csar_wordpress_invalid_import_url.zip") invalid_file = ( "https://raw.githubusercontent.com/openstack/tosca-parser/" "master/toscaparser/tests/data/CSAR/" "tosca_single_instance_wordpress/Definitions/wordpress1.yaml") csar = CSAR(path) error = self.assertRaises(URLException, csar.validate) # self.assertEqual(_('Failed to reach server ' # '"https://raw.githubusercontent.com/openstack/' # 'tosca-parser/master/toscaparser/tests/data/CSAR/' # 'tosca_single_instance_wordpress/Definitions/' # 'wordpress1.yaml". Reason is: Not Found.'), # str(error)) self.assertIn( _('Request error "{path}": Reason is 404 Client Error'.format( path=invalid_file)), str(error)) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_metadata_valid_csar(self): path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip") csar = CSAR(path) expected_meta = {'TOSCA-Meta-File-Version': 1.0, 'CSAR-Version': 1.1, 'Created-By': 'OASIS TOSCA TC', 'Entry-Definitions': 'tosca_helloworld.yaml'} self.assertEqual(expected_meta, csar.get_metadata(), 'The extracted metadata of the CSAR file %(csar)s ' 'does not match the expected metadata %(meta)s' % {'csar': path, 'meta': expected_meta.__str__()}) self.assertEqual(1.1, csar.get_version()) self.assertEqual('OASIS TOSCA TC', csar.get_author()) self.assertEqual('tosca_helloworld.yaml', csar.get_main_template()) self.assertEqual('Template for deploying a single server with ' 'predefined properties.', csar.get_description())
def test_metadata_valid_csar(self): path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip") csar = CSAR(path) expected_meta = { 'TOSCA-Meta-File-Version': 1.0, 'CSAR-Version': 1.1, 'Created-By': 'OASIS TOSCA TC', 'Entry-Definitions': 'tosca_helloworld.yaml' } self.assertEqual( expected_meta, csar.get_metadata(), 'The extracted metadata of the CSAR %(csar)s does ' 'not match the expected metadata %(meta)s' % { 'csar': path, 'meta': expected_meta.__str__() }) self.assertEqual(1.1, csar.get_version()) self.assertEqual('OASIS TOSCA TC', csar.get_author()) self.assertEqual('tosca_helloworld.yaml', csar.get_main_template()) self.assertEqual( 'Template for deploying a single server with ' 'predefined properties.', csar.get_description()) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_valid_csar(self): path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip") csar = CSAR(path) self.assertIsNone(csar.validate())
def test_valid_csar(self): path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip") csar = CSAR(path) self.assertTrue(csar.validate()) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_alternate_csar_extension(self): path = os.path.join(self.base_path, "data/CSAR/csar_elk.csar") csar = CSAR(path) self.assertIsNone(csar.validate()) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_valid_csar(self): path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip") csar = CSAR(path) self.assertIsNone(csar.validate()) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))
def test_file_is_zip(self): path = os.path.join(self.base_path, "data/CSAR/csar_not_zip.zip") csar = CSAR(path) error = self.assertRaises(ValidationError, csar.validate) self.assertEqual(_('"%s" is not a valid zip file.') % path, str(error))
def get_tosca_template(file_path, inputs=None): if inputs is None: inputs = {} global _log _log = Logger.get(__name__) # Work around bug validation csar of toscaparser if file_path.endswith(('.zip', '.csar')): csar = CSAR(file_path) try: csar.validate() except ValueError as e: _log.debug(e) if not str(e).startswith("The resource") or \ not str(e).endswith("does not exist."): raise e csar.decompress() file_path = path.join(csar.temp_dir, csar.get_main_template()) tosca = ToscaTemplate(file_path, inputs) base_path = '/'.join(tosca.path.split('/')[:-1]) _log.debug('base_path: %s', base_path) _parse_functions(tosca, inputs, base_path) # print(helper.print_TOSCA(tosca)) repositories = tosca.tpl.get('repositories', None) tosca_name = tosca.input_path.split('/')[-1][:-5] tpl = Template(tosca_name) if hasattr(tosca, 'topology_template'): if hasattr(tosca, 'outputs'): tpl.outputs = tosca.outputs if hasattr(tosca, 'nodetemplates'): for node in tosca.nodetemplates: tpl.push(_parse_conf(tpl, node, repositories, base_path)) _add_pointer(tpl) _add_back_links(tpl) _add_extension(tpl) if hasattr(tosca, 'policies'): for policy in tosca.policies: if not policy.is_derived_from(PROTOCOL_POLICY): raise ValueError( 'policy of type "{}" not supported.'.format( policy.type)) # policy.name # policy.properties # policy.targets _validate_protocol(policy.properties) protocol = _parse_protocol(policy.properties) _log.debug(protocol) for target in policy.targets: comp = tpl[target] if not isinstance(comp, Software): raise ValueError( 'Only software support custom protocol') comp.protocol = protocol return tpl
def test_alternate_csar_extension(self): path = os.path.join(self.base_path, "data/CSAR/csar_elk.csar") csar = CSAR(path) self.assertTrue(csar.validate()) self.assertTrue(csar.temp_dir is None or not os.path.exists(csar.temp_dir))