def test_lookup_fail(self): dom_xml = u'''<vm> <device id='alias0' type='fancydev'> <mode type="int">42</mode> </device> </vm>''' dom = FakeDomain.with_metadata(dom_xml) with metadata.device(dom, id='alias999', type='fancydev') as dev: self.assertEqual(dev, {})
def test_lookup_partial_attributes(self): dom_xml = u'''<vm> <device id='alias0' type='fancydev'> <mode type="int">42</mode> </device> <device id='alias1'> <mode type="int">33</mode> </device> </vm>''' dom = FakeDomain.with_metadata(dom_xml) with metadata.device(dom, type='fancydev') as dev: self.assertEqual(dev, {'mode': 42})
def test_update(self, dom_xml): expected_res = { 'flag': True, 'mode': 42, } dom = FakeDomain.with_metadata(dom_xml) with metadata.device(dom, id='alias0') as dev: dev['mode'] = 42 dev['flag'] = True dev.pop('removeme', None) # troubleshooting helper should the test fail print(dom.metadata( libvirt.VIR_DOMAIN_METADATA_ELEMENT, xmlconstants.METADATA_VM_VDSM_URI, 0 )) # our assertXMLEqual consider the order of XML attributes # significant, while according to XML spec is not. with metadata.device(dom, id='alias0') as dev: self.assertEqual(dev, expected_res)
def test_update(self, dom_xml): expected_res = { 'flag': 'true', 'mode': 42, } dom = FakeDomain.with_metadata(dom_xml) with metadata.device(dom, id='alias0') as dev: dev['mode'] = 42 dev['flag'] = 'true' dev.pop('removeme', None) # troubleshooting helper should the test fail print(dom.metadata( libvirt.VIR_DOMAIN_METADATA_ELEMENT, xmlconstants.METADATA_VM_VDSM_URI, 0 )) # our assertXMLEqual consider the order of XML attributes # significant, while according to XML spec is not. with metadata.device(dom, id='alias0') as dev: self.assertEqual(dev, expected_res)
def test_lookup_ambiguous_raises(self): dom_xml = u'''<vm> <device type='fancydev'> <mode type="int">1</mode> </device> <device type='fancydev'> <mode type="int">2</mode> </device> </vm>''' dom = FakeDomain.with_metadata(dom_xml) with self.assertRaises(metadata.MissingDevice): with metadata.device(dom, type='fancydev'): pass
def test_lookup_multiple_devices(self): dom_xml = u'''<vm> <device id='alias0' type='devA' addr='pci_0000_00_1a_0'> <mode type="int">1200</mode> </device> <device id='alias1' type='devA' addr='pci_0000_00_02_0'> <mode type="int">900</mode> </device> <device id='alias2' type='devC' addr='pci_0000_00_1f_2'> <mode type="int">1440</mode> </device> </vm>''' dom = FakeDomain.with_metadata(dom_xml) with metadata.device(dom, addr='pci_0000_00_02_0') as dev: self.assertEqual(dev, {'mode': 900})
def test_clear(self): dom_xml = u'''<vm> <device id='alias0'> <mode>12</mode> </device> </vm>''' expected_xml = u'''<vm> <device id='alias0' /> </vm>''' dom = FakeDomain.with_metadata(dom_xml) with metadata.device(dom, id='alias0') as dev: dev.clear() produced_xml = dom.metadata( libvirt.VIR_DOMAIN_METADATA_ELEMENT, xmlconstants.METADATA_VM_VDSM_URI, 0 ) self.assertXMLEqual(produced_xml, expected_xml)
def test_get(self, dom_xml, expected_dev): dom = FakeDomain.with_metadata(dom_xml) with metadata.device(dom, id='alias0') as dev: self.assertEqual(dev, expected_dev)