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()))
def test_proprety_inheritance(self): from toscaparser.nodetemplate import NodeTemplate tosca_custom_def = ''' tosca.nodes.SoftwareComponent.MySoftware: derived_from: SoftwareComponent properties: install_path: required: false type: string default: /opt/mysoftware ''' tosca_node_template = ''' node_templates: mysoftware_instance: type: tosca.nodes.SoftwareComponent.MySoftware properties: component_version: 3.1 ''' 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) self.assertIsNone(tpl.validate()) self.assertEqual(expected_properties, sorted(tpl.get_properties().keys()))
def test_constraint_for_scalar_unit(self): tpl_snippet = ''' server: type: tosca.my.nodes.Compute properties: cpu_frequency: 0.05 GHz disk_size: 500 MB mem_size: 1 MB ''' nodetemplates = yamlparser.simple_parse(tpl_snippet) nodetemplate = NodeTemplate('server', nodetemplates, self.custom_def) props = nodetemplate.get_properties() if 'cpu_frequency' in props.keys(): error = self.assertRaises(exception.ValidationError, props['cpu_frequency'].validate) self.assertEqual(_('The value "0.05 GHz" of property ' '"cpu_frequency" must be greater than or equal ' 'to "0.1 GHz".'), error.__str__()) if 'disk_size' in props.keys(): error = self.assertRaises(exception.ValidationError, props['disk_size'].validate) self.assertEqual(_('The value "500 MB" of property "disk_size" ' 'must be greater than or equal to "1 GB".'), error.__str__()) if 'mem_size' in props.keys(): error = self.assertRaises(exception.ValidationError, props['mem_size'].validate) self.assertEqual(_('The value "1 MB" of property "mem_size" is ' 'out of range "(min:1 MiB, max:1 GiB)".'), error.__str__())
def node_type_2_node_template(node_type, all_custom_def): node_template_dict = {} type_name = next(iter(node_type)) node_type_array = type_name.split(".") name = node_type_array[len(node_type_array) - 1].lower() node_template_dict[name] = node_type[next(iter(node_type))].copy() node_template_dict[name]['type'] = type_name for name_to_remove in node_type_key_names_to_remove: if name_to_remove in node_template_dict[name]: node_template_dict[name].pop(name_to_remove) if 'type' in node_type[next(iter(node_type))]: node_type[next(iter(node_type))].pop('type') node_template = NodeTemplate(name, node_template_dict, node_type) # For some reason the tosca.nodes.ARTICONF.docker.Orchestrator doesn't have all definitions so we need to add them # manually. We get 'toscaparser.common.exception.InvalidTypeError: Type "tosca.nodes.ARTICONF.docker.Orchestrator" # is not a valid type.' if len(node_template.custom_def) < len(all_custom_def): for def_key in all_custom_def: if isinstance(def_key, dict): node_template.custom_def.update(def_key) else: node_template.custom_def[def_key] = all_custom_def[def_key] return node_template
def test_constraint_for_scalar_unit(self): tpl_snippet = ''' server: type: tosca.my.nodes.Compute properties: cpu_frequency: 0.05 GHz disk_size: 500 MB mem_size: 1 MB ''' nodetemplates = yamlparser.simple_parse(tpl_snippet) nodetemplate = NodeTemplate('server', nodetemplates, self.custom_def) props = nodetemplate.get_properties() if 'cpu_frequency' in props.keys(): error = self.assertRaises(exception.ValidationError, props['cpu_frequency'].validate) self.assertEqual( _('The value "0.05 GHz" of property ' '"cpu_frequency" must be greater than or equal ' 'to "0.1 GHz".'), error.__str__()) if 'disk_size' in props.keys(): error = self.assertRaises(exception.ValidationError, props['disk_size'].validate) self.assertEqual( _('The value "500 MB" of property "disk_size" ' 'must be greater than or equal to "1 GB".'), error.__str__()) if 'mem_size' in props.keys(): error = self.assertRaises(exception.ValidationError, props['mem_size'].validate) self.assertEqual( _('The value "1 MB" of property "mem_size" is ' 'out of range "(min:1 MiB, max:1 GiB)".'), error.__str__())
def test_constraint_for_scalar_unit(self): tpl_snippet = ''' server: type: tosca.my.nodes.Compute properties: cpu_frequency: 0.05 GHz disk_size: 500 MB mem_size: 1 MB ''' nodetemplates = yamlparser.simple_parse(tpl_snippet) nodetemplate = NodeTemplate('server', nodetemplates, self.custom_def) props = nodetemplate.get_properties() if 'cpu_frequency' in props.keys(): error = self.assertRaises(exception.ValidationError, props['cpu_frequency'].validate) self.assertEqual('cpu_frequency: 0.05 GHz must be greater or ' 'equal to "0.1 GHz".', error.__str__()) if 'disk_size' in props.keys(): error = self.assertRaises(exception.ValidationError, props['disk_size'].validate) self.assertEqual('disk_size: 500 MB must be greater or ' 'equal to "1 GB".', error.__str__()) if 'mem_size' in props.keys(): error = self.assertRaises(exception.ValidationError, props['mem_size'].validate) self.assertEqual('mem_size: 1 MB is out of range ' '(min:1 MiB, ' 'max:1 GiB).', error.__str__())
def _tosca_floatingip_test(self, tpl_snippet, expectedprops, name=None): nodetemplates = (toscaparser.utils.yamlparser. simple_parse(tpl_snippet)['node_templates']) if not name: name = list(nodetemplates.keys())[0] nodetemplate = NodeTemplate(name, nodetemplates, custom_def=[]) nodetemplate.validate() tosca_floatingip = ToscaFloatingIP(nodetemplate) tosca_floatingip.handle_properties() self.assertEqual(expectedprops, tosca_floatingip.properties)
def _nodetemplates(self): nodetemplates = [] tpls = self._tpl_nodetemplates() for name in tpls: tpl = NodeTemplate(name, tpls, self.custom_defs, self.relationship_templates, self.rel_types) tpl.validate(self) nodetemplates.append(tpl) return nodetemplates
def _tosca_floatingip_test(self, tpl_snippet, expectedprops, name=None): nodetemplates = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet) ['node_templates']) if not name: name = list(nodetemplates.keys())[0] nodetemplate = NodeTemplate(name, nodetemplates, custom_def=[]) nodetemplate.validate() tosca_floatingip = ToscaFloatingIP(nodetemplate) tosca_floatingip.handle_properties() self.assertEqual(expectedprops, tosca_floatingip.properties)
def _tosca_compute_test(self, tpl_snippet, expectedprops): nodetemplates = (toscaparser.utils.yamlparser. simple_parse(tpl_snippet)['node_templates']) name = list(nodetemplates.keys())[0] nodetemplate = NodeTemplate(name, nodetemplates) nodetemplate.validate() toscacompute = ToscaCompute(nodetemplate) toscacompute.handle_properties() self.assertEqual(expectedprops, toscacompute.properties)
def _tosca_compute_test(self, tpl_snippet, expectedprops): nodetemplates = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet) ['node_templates']) name = list(nodetemplates.keys())[0] nodetemplate = NodeTemplate(name, nodetemplates) nodetemplate.validate() toscacompute = ToscaCompute(nodetemplate) toscacompute.handle_properties() self.assertEqual(expectedprops, toscacompute.properties)
def test_scenario_scalar_unit_positive(self): tpl = self.tpl_snippet nodetemplates = yamlparser.simple_parse(tpl) nodetemplate = NodeTemplate('server', nodetemplates) props = nodetemplate.get_capability('host').get_properties() prop_name = self.property if props and prop_name in props.keys(): prop = props[prop_name] self.assertIsNone(prop.validate()) resolved = prop.value self.assertEqual(resolved, self.expected)
def test_custom_capability_type_definition(self): tpl_snippet = ''' node_templates: test_app: type: tosca.nodes.WebApplication.TestApp capabilities: test_cap: properties: test: 1 ''' # custom node type definition with custom capability type definition custom_def = ''' tosca.nodes.WebApplication.TestApp: derived_from: tosca.nodes.WebApplication capabilities: test_cap: type: tosca.capabilities.TestCapability tosca.capabilities.TestCapability: derived_from: tosca.capabilities.Root properties: test: type: integer required: false ''' expected_capabilities = ['app_endpoint', 'feature', 'test_cap'] nodetemplates = (toscaparser.utils.yamlparser. simple_parse(tpl_snippet))['node_templates'] custom_def = (toscaparser.utils.yamlparser. simple_parse(custom_def)) name = list(nodetemplates.keys())[0] tpl = NodeTemplate(name, nodetemplates, custom_def) self.assertEqual( expected_capabilities, sorted(tpl.get_capabilities().keys())) # custom definition without valid capability type definition custom_def = ''' tosca.nodes.WebApplication.TestApp: derived_from: tosca.nodes.WebApplication capabilities: test_cap: type: tosca.capabilities.TestCapability ''' custom_def = (toscaparser.utils.yamlparser. simple_parse(custom_def)) tpl = NodeTemplate(name, nodetemplates, custom_def) err = self.assertRaises( exception.InvalidTypeError, lambda: NodeTemplate(name, nodetemplates, custom_def).get_capabilities_objects()) self.assertEqual('Type "tosca.capabilities.TestCapability" is not ' 'a valid type.', six.text_type(err))
def test_invalid_scalar_unit(self): tpl_snippet = ''' server: type: tosca.my.nodes.Compute properties: cpu_frequency: 50.3.6 GHZ disk_size: MB mem_size: 1 QB ''' nodetemplates = yamlparser.simple_parse(tpl_snippet) nodetemplate = NodeTemplate('server', nodetemplates, self.custom_def) for p in nodetemplate.get_properties_objects(): self.assertRaises(ValueError, p.validate)
def _nodetemplates(self): nodetemplates = [] tpls = self._tpl_nodetemplates() if tpls: for name in tpls: tpl = NodeTemplate(name, tpls, self.custom_defs, self.relationship_templates, self.rel_types) if (tpl.type_definition and (tpl.type in tpl.type_definition.TOSCA_DEF or (tpl.type not in tpl.type_definition.TOSCA_DEF and bool(tpl.custom_def)))): tpl.validate(self) nodetemplates.append(tpl) return nodetemplates
def _tosca_scaling_test(self, tpl_snippet, expectedprops, hot_template_parameters=None): nodetemplates = (toscaparser.utils.yamlparser. simple_parse(tpl_snippet)['node_templates']) policies = (toscaparser.utils.yamlparser. simple_parse(tpl_snippet)['policies']) name = list(nodetemplates.keys())[0] policy_name = list(policies[0].keys())[0] for policy in policies: tpl = policy[policy_name] targets = tpl["targets"] properties = tpl["properties"] try: nodetemplate = NodeTemplate(name, nodetemplates) toscacompute = ToscaCompute(nodetemplate) toscacompute.handle_properties() policy = Policy(policy_name, tpl, targets, properties, "node_templates") toscascaling = ToscaAutoscaling( policy, hot_template_parameters=hot_template_parameters) parameters = toscascaling.handle_properties([toscacompute]) if hot_template_parameters: substack_template = toscascaling.extract_substack_templates( "output.yaml", HOT_TEMPLATE_VERSION) actual_nested_resource = yaml.load( substack_template['SP1_res.yaml']) self.assertEqual(expectedprops, actual_nested_resource) else: self.assertEqual(parameters[0].properties, expectedprops) except Exception: raise
def _get_nodetemplate(self, tpl_snippet, custom_def_snippet=None): nodetemplates = yamlparser.\ simple_parse(tpl_snippet)['node_templates'] custom_def = yamlparser.simple_parse(custom_def_snippet) name = list(nodetemplates.keys())[0] tpl = NodeTemplate(name, nodetemplates, custom_def) return tpl
def _tosca_policy_test(self, tpl_snippet, expectedprops): nodetemplates = (toscaparser.utils.yamlparser. simple_parse(tpl_snippet)['node_templates']) policies = (toscaparser.utils.yamlparser. simple_parse(tpl_snippet)['policies']) name = list(nodetemplates.keys())[0] policy_name = list(policies[0].keys())[0] for policy in policies: tpl = policy[policy_name] targets = tpl["targets"] try: nodetemplate = NodeTemplate(name, nodetemplates) toscacompute = ToscaCompute(nodetemplate) toscacompute.handle_properties() # adding a property to test that # ToscaPolicies.handle_properties does not overwrite this. toscacompute.properties['scheduler_hints'] = { 'target_cell': 'cell0'} policy = Policy(policy_name, tpl, targets, "node_templates") toscapolicy = ToscaPolicies(policy) nodetemplate = [toscacompute] toscapolicy.handle_properties(nodetemplate) self.assertEqual(toscacompute.properties, expectedprops) except Exception: raise
def _tosca_compute_test(self, tpl_snippet, expectedprops): nodetemplates = (toscaparser.utils.yamlparser. simple_parse(tpl_snippet)['node_templates']) name = list(nodetemplates.keys())[0] try: nodetemplate = NodeTemplate(name, nodetemplates) nodetemplate.validate() toscacompute = ToscaCompute(nodetemplate) toscacompute.handle_properties() if not self._compare_properties(toscacompute.properties, expectedprops): raise Exception(_("Hot Properties are not" " same as expected properties")) except Exception: # for time being rethrowing. Will be handled future based # on new development in Glance and Graffiti raise
def add_template(self, name, tpl): # if name in self.node_templates: # exception.ExceptionCollector.appendException( # exception.ValidationError(message= # 'Node template already defined "%s"' % name)) # return None self.tpl.setdefault(NODE_TEMPLATES, {})[name] = tpl node = NodeTemplate( name, self, self.custom_defs, self.relationship_templates) node.validate(self) node.relationships # this will update the relationship_tpl of the target node self.node_templates[name] = node return node
def _single_node_template_content_test(self, tpl_snippet): nodetemplates = (toscaparser.utils.yamlparser.simple_ordered_parse( tpl_snippet))['node_templates'] name = list(nodetemplates.keys())[0] nodetemplate = NodeTemplate(name, nodetemplates, self._custom_types()) nodetemplate.validate() nodetemplate.requirements nodetemplate.get_capabilities_objects() nodetemplate.get_properties_objects() nodetemplate.interfaces
def _nodetemplates(self): nodetemplates = {} tpls = self._tpl_nodetemplates() if tpls: for name in tpls: tpl = NodeTemplate( name, self, self.custom_defs, self.relationship_templates ) # why these tests? defeats validation # if (tpl.type_definition and # (tpl.type in tpl.type_definition.TOSCA_DEF or # (tpl.type not in tpl.type_definition.TOSCA_DEF and # bool(tpl.custom_def)))): tpl.validate(self) nodetemplates[name] = tpl return nodetemplates
def _get_nodetemplate(tpl_snippet, name, custom_def_snippet=None): tpl = toscaparser.utils.yamlparser.simple_parse(tpl_snippet) nodetemplates = tpl['node_templates'] custom_def = [] if custom_def_snippet: custom_def = toscaparser.utils.yamlparser.simple_parse( custom_def_snippet) topology = TopologyTemplate(tpl, custom_def) if not name: name = list(nodetemplates.keys())[0] return NodeTemplate(name, topology, custom_def)
def node_dict_2_node_template(node_name, node_dict, all_custom_def): node_dict = {node_name: node_dict} # node_type = node_dict[node_name]['type'] # for name_to_remove in node_type_key_names_to_remove: # if name_to_remove in node_dict[node_name][node_name]: # node_dict[node_name].pop(name_to_remove) node_template = NodeTemplate(node_name, node_templates=copy.deepcopy(node_dict), custom_def=all_custom_def) # For some reason the tosca.nodes.ARTICONF.Orchestrator doesn't have all definitions so we need to add them # manually. We get 'toscaparser.common.exception.InvalidTypeError: Type "tosca.nodes.ARTICONF.Orchestrator" # is not a valid type.' if len(node_template.custom_def) < len(all_custom_def): for def_key in all_custom_def: if isinstance(def_key, dict): node_template.custom_def.update(def_key) else: node_template.custom_def[def_key] = all_custom_def[def_key] return node_template
def build_node_fact(node_tpl: NodeTemplate) -> str: def build_node_property(prop: Property) -> str: if type(prop.value) in [int, float]: prop_val_str = str(prop.value) elif type(prop.value) is str: prop_val_str = '"' + prop.value + '"' # TODO: escaping elif type(prop.value) is bool: prop_val_str = "true" if prop.value else "false" elif type(prop.value) is GetInput: prop_val_str = f"get_input({prop.value.args[0]})" else: raise ValueError(f"Property type {type(prop.value)} not handled.") return f"property({prop.name}, {prop_val_str})" properties = "[" \ + ", ".join([build_node_property(prop) for prop in node_tpl.get_properties_objects()]) \ + "]" def build_node_capability(cap: Capability): properties = "[" \ + ", ".join([build_node_property(prop) for prop in cap.get_properties_objects()]) \ + "]" return f"capability({cap.name}, {properties})" capabilities = "[" \ + ", ".join([build_node_capability(cap) for cap in node_tpl.get_capabilities_objects()]) \ + "]" def build_node_requirement(req): req_name = list(req)[0] return f"requirement({req_name}, {req[req_name]})" requirements = "[" \ + ", ".join([build_node_requirement(req) for req in node_tpl.requirements]) \ + "]" return textwrap.dedent(f""" node( {node_tpl.name}, '{node_tpl.type}', {properties}, {capabilities}, {requirements} )""")
def _single_node_template_content_test(self, tpl_snippet): nodetemplates = (toscaparser.utils.yamlparser. simple_ordered_parse(tpl_snippet))['node_templates'] name = list(nodetemplates.keys())[0] nodetemplate = NodeTemplate(name, nodetemplates, self._custom_types()) nodetemplate.validate() nodetemplate.requirements nodetemplate.get_capabilities_objects() nodetemplate.get_properties_objects() nodetemplate.interfaces
def _get_nodetemplate(self, tpl_snippet, custom_def_snippet=None, name=None): tpl = yamlparser.simple_parse(tpl_snippet) nodetemplates = tpl['node_templates'] custom_def = [] if custom_def_snippet: custom_def = yamlparser.simple_parse(custom_def_snippet) if not name: name = list(nodetemplates.keys())[0] topology = TopologyTemplate(tpl, custom_def) tpl = NodeTemplate(name, topology, custom_def) return tpl
def _tosca_scaling_test(self, tpl_snippet, expectedprops): nodetemplates = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet) ['node_templates']) policies = ( toscaparser.utils.yamlparser.simple_parse(tpl_snippet)['policies']) name = list(nodetemplates.keys())[0] policy_name = list(policies[0].keys())[0] for policy in policies: tpl = policy[policy_name] targets = tpl["targets"] properties = tpl["properties"] try: nodetemplate = NodeTemplate(name, nodetemplates) toscacompute = ToscaCompute(nodetemplate) toscacompute.handle_properties() policy = Policy(policy_name, tpl, targets, properties, "node_templates") toscascaling = ToscaAutoscaling(policy) parameters = toscascaling.handle_properties([toscacompute]) self.assertEqual(parameters[0].properties, expectedprops) except Exception: raise
def _requirements_not_implemented(self, tpl_snippet, tpl_name): nodetemplates = (toscaparser.utils.yamlparser. simple_parse(tpl_snippet))['node_templates'] self.assertRaises( NotImplementedError, lambda: NodeTemplate(tpl_name, nodetemplates).relationships)