def test_get_disks(self, mock_communicate, mock_popen): ''' Test virt.get_discs() ''' xml = '''<domain type='kvm' id='7'> <name>test-vm</name> <devices> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/disks/test.qcow2'/> <target dev='vda' bus='virtio'/> </disk> <disk type='file' device='cdrom'> <driver name='qemu' type='raw'/> <source file='/disks/test-cdrom.iso'/> <target dev='hda' bus='ide'/> <readonly/> </disk> </devices> </domain> ''' self.set_mock_vm("test-vm", xml) disks = virt.get_disks('test-vm') disk = disks[list(disks)[0]] self.assertEqual('/disks/test.qcow2', disk['file']) self.assertEqual('disk', disk['type']) cdrom = disks[list(disks)[1]] self.assertEqual('/disks/test-cdrom.iso', cdrom['file']) self.assertEqual('cdrom', cdrom['type'])
def test_get_disks(self, mock_communicate, mock_popen): get_xml_mock = MagicMock(return_value='''<domain type='kvm' id='7'> <name>test-vm</name> <devices> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/disks/test.qcow2'/> <target dev='vda' bus='virtio'/> </disk> <disk type='file' device='cdrom'> <driver name='qemu' type='raw'/> <source file='/disks/test-cdrom.iso'/> <target dev='hda' bus='ide'/> <readonly/> </disk> </devices> </domain> ''') with patch.object(virt, 'get_xml', get_xml_mock): disks = virt.get_disks('test-vm') disk = disks[list(disks)[0]] self.assertEqual('/disks/test.qcow2', disk['file']) self.assertEqual('disk', disk['type']) cdrom = disks[list(disks)[1]] self.assertEqual('/disks/test-cdrom.iso', cdrom['file']) self.assertEqual('cdrom', cdrom['type'])
def test_get_disks(make_mock_vm, make_mock_storage_pool): # test with volumes vm_def = """<domain type='kvm' id='3'> <name>srv01</name> <devices> <disk type='volume' device='disk'> <driver name='qemu' type='qcow2' cache='none' io='native'/> <source pool='default' volume='srv01_system'/> <backingStore/> <target dev='vda' bus='virtio'/> <alias name='virtio-disk0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </disk> <disk type='volume' device='disk'> <driver name='qemu' type='qcow2' cache='none' io='native'/> <source pool='default' volume='srv01_data'/> <backingStore type='file' index='1'> <format type='qcow2'/> <source file='/var/lib/libvirt/images/vol01'/> <backingStore/> </backingStore> <target dev='vdb' bus='virtio'/> <alias name='virtio-disk1'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </disk> <disk type='volume' device='disk'> <driver name='qemu' type='qcow2' cache='none' io='native'/> <source pool='default' volume='vm05_system'/> <backingStore type='file' index='1'> <format type='qcow2'/> <source file='/var/lib/libvirt/images/vm04_system.qcow2'/> <backingStore type='file' index='2'> <format type='raw'/> <source file='/var/testsuite-data/disk-image-template.raw'/> <backingStore/> </backingStore> </backingStore> <target dev='vdc' bus='virtio'/> <alias name='virtio-disk0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </disk> <disk type='network' device='cdrom'> <driver name='qemu' type='raw' cache='none' io='native'/> <source protocol='http' name='/pub/iso/myimage.iso' query='foo=bar&baz=flurb' index='1'> <host name='dev-srv.tf.local' port='80'/> </source> <target dev='hda' bus='ide'/> <readonly/> <alias name='ide0-0-0'/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk> </devices> </domain> """ domain_mock = make_mock_vm(vm_def) pool_mock = make_mock_storage_pool( "default", "dir", ["srv01_system", "srv01_data", "vm05_system"] ) # Append backing store to srv01_data volume XML description srv1data_mock = pool_mock.storageVolLookupByName("srv01_data") append_to_XMLDesc( srv1data_mock, """ <backingStore> <path>/var/lib/libvirt/images/vol01</path> <format type="qcow2"/> </backingStore>""", ) assert virt.get_disks("srv01") == { "vda": { "type": "disk", "file": "default/srv01_system", "file format": "qcow2", "disk size": 12345, "virtual size": 1234567, }, "vdb": { "type": "disk", "file": "default/srv01_data", "file format": "qcow2", "disk size": 12345, "virtual size": 1234567, "backing file": { "file": "/var/lib/libvirt/images/vol01", "file format": "qcow2", }, }, "vdc": { "type": "disk", "file": "default/vm05_system", "file format": "qcow2", "disk size": 12345, "virtual size": 1234567, "backing file": { "file": "/var/lib/libvirt/images/vm04_system.qcow2", "file format": "qcow2", "backing file": { "file": "/var/testsuite-data/disk-image-template.raw", "file format": "raw", }, }, }, "hda": { "type": "cdrom", "file format": "raw", "file": "http://dev-srv.tf.local:80/pub/iso/myimage.iso?foo=bar&baz=flurb", }, }
def test_get_disk_convert_volumes(make_mock_vm, make_mock_storage_pool): vm_def = """<domain type='kvm' id='3'> <name>srv01</name> <devices> <disk type='file' device='disk'> <driver name='qemu' type='qcow2' cache='none' io='native'/> <source file='/path/to/default/srv01_system'/> <target dev='vda' bus='virtio'/> <alias name='virtio-disk0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </disk> <disk type='block' device='disk'> <driver name='qemu' type='raw'/> <source dev='/path/to/default/srv01_data'/> <target dev='vdb' bus='virtio'/> <alias name='virtio-disk1'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </disk> <disk type='file' device='disk'> <driver name='qemu' type='qcow2' cache='none' io='native'/> <source file='/path/to/srv01_extra'/> <target dev='vdc' bus='virtio'/> <alias name='virtio-disk1'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </disk> </devices> </domain> """ domain_mock = make_mock_vm(vm_def) pool_mock = make_mock_storage_pool("default", "dir", ["srv01_system", "srv01_data"]) subprocess_mock = MagicMock() popen_mock = MagicMock(spec=virt.subprocess.Popen) popen_mock.return_value.communicate.return_value = [ """[ { "virtual-size": 214748364800, "filename": "/path/to/srv01_extra", "cluster-size": 65536, "format": "qcow2", "actual-size": 340525056, "format-specific": { "type": "qcow2", "data": { "compat": "1.1", "lazy-refcounts": false, "refcount-bits": 16, "corrupt": false } }, "dirty-flag": false } ] """ ] subprocess_mock.Popen = popen_mock with patch.dict(virt.__dict__, {"subprocess": subprocess_mock}): assert { "vda": { "type": "disk", "file": "default/srv01_system", "file format": "qcow2", "disk size": 12345, "virtual size": 1234567, }, "vdb": { "type": "disk", "file": "default/srv01_data", "file format": "raw", "disk size": 12345, "virtual size": 1234567, }, "vdc": { "type": "disk", "file": "/path/to/srv01_extra", "file format": "qcow2", "cluster size": 65536, "disk size": 340525056, "virtual size": 214748364800, }, } == virt.get_disks("srv01")