class TestXMLInstance(unittest.TestCase): """Test cases for XML instance methods.""" OVF = "{http://schemas.dmtf.org/ovf/envelope/1}" def setUp(self): """Test case setup function called automatically prior to each test.""" self.xml = XML() self.xml.read_xml(resource_filename(__name__, "input.ovf")) super(TestXMLInstance, self).setUp() def test_find_child(self): """Test corner cases of the find_child() method.""" match = self.xml.find_child(self.xml.root, self.OVF + "References") self.assertEqual(match.tag, self.OVF + "References") # multiple children -> LookupError self.assertRaises(LookupError, self.xml.find_child, match, self.OVF + "File") # no such child -> None unless required match = self.xml.find_child(self.xml.root, self.OVF + "Foobar") self.assertEqual(None, match) # no such child -> KeyError if required self.assertRaises(KeyError, self.xml.find_child, self.xml.root, self.OVF + "Foobar", required=True) def test_set_or_make_child(self): """Call set_or_make_child() in some slightly incorrect ways.""" # Trigger the warning in add_child() logged when # creating a new child that's in a known namespace # but the child isn't in the expected ordering self.xml.set_or_make_child( self.xml.root, self.OVF + "foo", ordering=[self.OVF + "References"], known_namespaces=["http://schemas.dmtf.org/ovf/envelope/1"] ) # Trigger the warning in add_child() logged when # creating a new child in a known namespace # with an expected ordering that includes the child, # but other children in this namespace are left out. self.xml.set_or_make_child( self.xml.root, self.OVF + "bar", ordering=[self.OVF + "DiskSection", self.OVF + "bar"], known_namespaces=["http://schemas.dmtf.org/ovf/envelope/1"] )
class TestXMLInstance(COTTestCase): """Test cases for XML instance methods.""" OVF = "{http://schemas.dmtf.org/ovf/envelope/1}" def setUp(self): """Test case setup function called automatically prior to each test.""" self.xml = XML(self.input_ovf) super(TestXMLInstance, self).setUp() def test_find_child(self): """Test corner cases of the find_child() method.""" match = self.xml.find_child(self.xml.root, self.OVF + "References") self.assertEqual(match.tag, self.OVF + "References") # multiple children -> LookupError self.assertRaises(LookupError, self.xml.find_child, match, self.OVF + "File") # no such child -> None unless required match = self.xml.find_child(self.xml.root, self.OVF + "Foobar") self.assertEqual(None, match) # no such child -> KeyError if required self.assertRaises(KeyError, self.xml.find_child, self.xml.root, self.OVF + "Foobar", required=True) def test_set_or_make_child(self): """Call set_or_make_child() in some slightly incorrect ways.""" # Trigger the warning in add_child() logged when # creating a new child that's in a known namespace # but the child isn't in the expected ordering self.xml.set_or_make_child( self.xml.root, self.OVF + "foo", ordering=[self.OVF + "References"], known_namespaces=["http://schemas.dmtf.org/ovf/envelope/1"]) self.assertLogged(levelname="WARNING", msg="in a known namespace.*not in the list") # Trigger the warning in add_child() logged when # creating a new child in a known namespace # with an expected ordering that includes the child, # but other children in this namespace are left out. self.xml.set_or_make_child( self.xml.root, self.OVF + "bar", ordering=[self.OVF + "DiskSection", self.OVF + "bar"], known_namespaces=["http://schemas.dmtf.org/ovf/envelope/1"]) self.assertLogged(levelname="WARNING", msg="Found unexpected child element")
def generate_items(self): """Get a list of Item XML elements derived from this object's data. Returns: list: Generated list of XML Item elements """ set_string_list = self.get_nonintersecting_set_list() # Now, construct the Items item_tag = self.item_tag_for_namespace(self.namespace) child_ordering = [self.namespace + i for i in self.ITEM_CHILDREN] item_list = [] for set_string in set_string_list: if not set_string: # no config profile item = ET.Element(item_tag) final_set = set([None]) set_string = '<generic>' else: item = ET.Element(item_tag, {self.ITEM_CONFIG: set_string}) final_set = set(set_string.split()) logger.spam("set string: %s; final_set: %s", set_string, final_set) for name in sorted(self.property_names): val = self.get_value(name, final_set) if not val: logger.debug( "No value defined for attribute '%s' " "under profile set '%s' for instance %s", name, set_string, self.get_value(self.INSTANCE_ID)) continue # Convert list of ResourceSubType values to a space-separated # list for output if name == self.RESOURCE_SUB_TYPE: val = " ".join(val) if val else None # Is this an attribute, a child, or a custom element? attrib_match = re.match(r"(.*)" + self.ATTRIB_KEY_SUFFIX, name) if attrib_match: attrib_string = attrib_match.group(1) child_attrib = re.match(r"(.*)_attrib_(.*)", name) custom_elem = re.match(r"(.*)" + self.ELEMENT_KEY_SUFFIX, name) if attrib_match: item.set(attrib_string, val) elif child_attrib: child = XML.set_or_make_child( item, child_attrib.group(1), None, ordering=child_ordering, known_namespaces=self.NSM.values()) child.set(child_attrib.group(2), val) elif custom_elem: # Recreate the element in question and append it item.append(ET.fromstring(val)) else: # Children of Item must be in sorted order XML.set_or_make_child(item, self.namespace + name, val, ordering=child_ordering, known_namespaces=self.NSM.values()) logger.spam("Item is:\n%s", ET.tostring(item)) item_list.append(item) return item_list
def generate_items(self): """Get a list of Item XML elements derived from this object's data. Returns: list: Generated list of XML Item elements """ set_string_list = self.get_nonintersecting_set_list() # Now, construct the Items item_tag = self.item_tag_for_namespace(self.namespace) child_ordering = [self.namespace + i for i in self.ITEM_CHILDREN] item_list = [] for set_string in set_string_list: if not set_string: # no config profile item = ET.Element(item_tag) final_set = set([None]) set_string = '<generic>' else: item = ET.Element(item_tag, {self.ITEM_CONFIG: set_string}) final_set = set(set_string.split()) logger.spam("set string: %s; final_set: %s", set_string, final_set) for name in sorted(self.property_names): val = self.get_value(name, final_set) if not val: logger.debug("No value defined for attribute '%s' " "under profile set '%s' for instance %s", name, set_string, self.get_value(self.INSTANCE_ID)) continue # Convert list of ResourceSubType values to a space-separated # list for output if name == self.RESOURCE_SUB_TYPE: val = " ".join(val) if val else None # Is this an attribute, a child, or a custom element? attrib_match = re.match(r"(.*)" + self.ATTRIB_KEY_SUFFIX, name) if attrib_match: attrib_string = attrib_match.group(1) child_attrib = re.match(r"(.*)_attrib_(.*)", name) custom_elem = re.match(r"(.*)" + self.ELEMENT_KEY_SUFFIX, name) if attrib_match: item.set(attrib_string, val) elif child_attrib: child = XML.set_or_make_child( item, child_attrib.group(1), None, ordering=child_ordering, known_namespaces=self.NSM.values()) child.set(child_attrib.group(2), val) elif custom_elem: # Recreate the element in question and append it item.append(ET.fromstring(val)) else: # Children of Item must be in sorted order XML.set_or_make_child(item, self.namespace + name, val, ordering=child_ordering, known_namespaces=self.NSM.values()) logger.spam("Item is:\n%s", ET.tostring(item)) item_list.append(item) return item_list