def test_roundtrip_empty(self, elem_type): elem_spec = ('' if elem_type is None else 'type="{elem_type}"'.format( elem_type=elem_type)) test_xml = u'''<ovirt-vm:vm xmlns:ovirt-vm="http://ovirt.org/vm/1.0"> <ovirt-vm:param {elem_spec}/> </ovirt-vm:vm>'''.format(elem_spec=elem_spec) expected_xml = u'''<ovirt-vm:vm xmlns:ovirt-vm="http://ovirt.org/vm/1.0"> <ovirt-vm:param /> </ovirt-vm:vm>''' metadata_src = metadata.Metadata('ovirt-vm', 'http://ovirt.org/vm/1.0') metadata_dst = metadata.Metadata('ovirt-vm', 'http://ovirt.org/vm/1.0') data = metadata_src.load(xmlutils.fromstring(test_xml)) out_xml = xmlutils.tostring(metadata_dst.dump('vm', **data)) self.assertXMLEqual(out_xml, expected_xml)
def test_load_ns(self): test_xml = u'''<ovirt-vm:vm xmlns:ovirt-vm="http://ovirt.org/vm/1.0"> <ovirt-vm:version type="float">4.2</ovirt-vm:version> </ovirt-vm:vm>''' metadata_obj = metadata.Metadata('ovirt-vm', 'http://ovirt.org/vm/1.0') self.assertEqual(metadata_obj.load(xmlutils.fromstring(test_xml)), {'version': 4.2})
def test_load_empty_not_string(self, elem_type): test_xml = u'''<ovirt-vm:vm xmlns:ovirt-vm="http://ovirt.org/vm/1.0"> <ovirt-vm:param type="{elem_type}" /> </ovirt-vm:vm>'''.format(elem_type=elem_type) metadata_obj = metadata.Metadata('ovirt-vm', 'http://ovirt.org/vm/1.0') self.assertRaises(ValueError, metadata_obj.load, xmlutils.fromstring(test_xml))
def test_dump_ns(self): expected_xml = u'''<ovirt-vm:vm xmlns:ovirt-vm="http://ovirt.org/vm/1.0"> <ovirt-vm:version type="float">4.2</ovirt-vm:version> </ovirt-vm:vm>''' metadata_obj = metadata.Metadata('ovirt-vm', 'http://ovirt.org/vm/1.0') self.assertXMLEqual( xmlutils.tostring(metadata_obj.dump('vm', version=4.2)), expected_xml)
def test_skip_nested(self, test_xml): metadata_obj = metadata.Metadata( 'ovirt-vm', 'http://ovirt.org/vm/1.0' ) self.assertEqual( metadata_obj.load(vmxml.parse_xml(test_xml)), {} )
def test_create(self, custom): namespace = 'ovirt-vm' namespace_uri = 'http://ovirt.org/vm/1.0' elem = metadata.create( 'vm', namespace, namespace_uri, **custom ) metadata_obj = metadata.Metadata(namespace, namespace_uri) self.assertEqual(metadata_obj.load(elem), custom)
def test_load_empty(self, elem_type): elem_spec = ('' if elem_type is None else 'type="{elem_type}"'.format( elem_type=elem_type)) test_xml = u'''<ovirt-vm:vm xmlns:ovirt-vm="http://ovirt.org/vm/1.0"> <ovirt-vm:param {elem_spec}/> </ovirt-vm:vm>'''.format(elem_spec=elem_spec) metadata_obj = metadata.Metadata('ovirt-vm', 'http://ovirt.org/vm/1.0') self.assertEqual(metadata_obj.load(xmlutils.fromstring(test_xml)), {'param': ''})
def test_unsupported_type_dump_key_in_exception(self): KEY = "versions" data = {KEY: set()} metadata_obj = metadata.Metadata('ovirt-vm', 'http://ovirt.org/vm/1.0') try: metadata_obj.dump('test', **data) except metadata.UnsupportedType as exc: self.assertIn(KEY, str(exc)) else: raise AssertionError('dump() did not raise!')
def drives_map(self): mapping_elem = self._xml_tree.find( './metadata/{%s}drivemap' % ( xmlconstants.METADATA_VM_DRIVE_MAP_URI ) ) if not mapping_elem: return {} md = metadata.Metadata( xmlconstants.METADATA_VM_DRIVE_MAP_PREFIX, xmlconstants.METADATA_VM_DRIVE_MAP_URI ) return md.load(mapping_elem).copy()
def image(self): cont_elem = self._xml_tree.find( './metadata/{%s}container' % ( xmlconstants.METADATA_CONTAINERS_URI ) ) if cont_elem is None: raise ConfigError('missing container configuration') md = metadata.Metadata( xmlconstants.METADATA_CONTAINERS_PREFIX, xmlconstants.METADATA_CONTAINERS_URI ) img = md.load(cont_elem).get('image') if img is None: raise ConfigError('missing image to run') return img
def test_dump_all(self): expected_xml = u'''<ovirt-vm:sequence xmlns:ovirt-vm="http://ovirt.org/vm/1.0"> <ovirt-vm:item>foo</ovirt-vm:item> <ovirt-vm:item>bar</ovirt-vm:item> <ovirt-vm:item>True</ovirt-vm:item> <ovirt-vm:item>42</ovirt-vm:item> <ovirt-vm:item>0.25</ovirt-vm:item> </ovirt-vm:sequence>''' metadata_obj = metadata.Metadata('ovirt-vm', 'http://ovirt.org/vm/1.0') self.assertXMLEqual( xmlutils.tostring( metadata_obj.dump_sequence('sequence', 'item', ( 'foo', 'bar', True, 42, 0.25, ))), expected_xml)
def setUp(self): self.md = metadata.Metadata()
def test_skip_nested(self, test_xml): metadata_obj = metadata.Metadata('ovirt-vm', 'http://ovirt.org/vm/1.0') self.assertEqual(metadata_obj.load(xmlutils.fromstring(test_xml)), {})