Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 2 files changed
  • 0 comments
  • 2 contributors
May 11, 2012
Fix libvirt Connection.get_disks method
The get_disks() method is not used anywhere yet, and is not tested properly,
but it does not work as reported in bug #998089.

This patch add unit test for it and fixes it so it's now possible to
retrieve the list of device name associated with a libvirt domain.

Change-Id: If09fb683364174fe02cbcbec4c6bbcda991287d6
Signed-off-by: Julien Danjou <julien.danjou@enovance.com>
a345ee5
May 13, 2012
Merge "Fix libvirt Connection.get_disks method" 2c7e0d1
60  nova/tests/test_libvirt.py
@@ -693,6 +693,66 @@ def fake_lookup(id):
693 693
         devices = conn.get_all_block_devices()
694 694
         self.assertEqual(devices, ['/path/to/dev/1', '/path/to/dev/3'])
695 695
 
  696
+    def test_get_disks(self):
  697
+        xml = [
  698
+            # NOTE(vish): id 0 is skipped
  699
+            None,
  700
+            """
  701
+                <domain type='kvm'>
  702
+                    <devices>
  703
+                        <disk type='file'>
  704
+                            <source file='filename'/>
  705
+                            <target dev='vda' bus='virtio'/>
  706
+                        </disk>
  707
+                        <disk type='block'>
  708
+                            <source dev='/path/to/dev/1'/>
  709
+                            <target dev='vdb' bus='virtio'/>
  710
+                        </disk>
  711
+                    </devices>
  712
+                </domain>
  713
+            """,
  714
+            """
  715
+                <domain type='kvm'>
  716
+                    <devices>
  717
+                        <disk type='file'>
  718
+                            <source file='filename'/>
  719
+                            <target dev='vda' bus='virtio'/>
  720
+                        </disk>
  721
+                    </devices>
  722
+                </domain>
  723
+            """,
  724
+            """
  725
+                <domain type='kvm'>
  726
+                    <devices>
  727
+                        <disk type='file'>
  728
+                            <source file='filename'/>
  729
+                            <target dev='vda' bus='virtio'/>
  730
+                        </disk>
  731
+                        <disk type='block'>
  732
+                            <source dev='/path/to/dev/3'/>
  733
+                            <target dev='vdb' bus='virtio'/>
  734
+                        </disk>
  735
+                    </devices>
  736
+                </domain>
  737
+            """,
  738
+        ]
  739
+
  740
+        def fake_lookup(id):
  741
+            return FakeVirtDomain(xml[id])
  742
+
  743
+        def fake_lookup_name(name):
  744
+            return FakeVirtDomain(xml[1])
  745
+
  746
+        self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn')
  747
+        connection.LibvirtConnection._conn.listDomainsID = lambda: range(4)
  748
+        connection.LibvirtConnection._conn.lookupByID = fake_lookup
  749
+        connection.LibvirtConnection._conn.lookupByName = fake_lookup_name
  750
+
  751
+        self.mox.ReplayAll()
  752
+        conn = connection.LibvirtConnection(False)
  753
+        devices = conn.get_disks(conn.list_instances()[0])
  754
+        self.assertEqual(devices, ['vda', 'vdb'])
  755
+
696 756
     @test.skip_if(missing_libvirt(), "Test requires libvirt")
697 757
     def test_snapshot_in_ami_format(self):
698 758
         self.flags(image_service='nova.image.fake.FakeImageService')
21  nova/virt/libvirt/connection.py
@@ -1773,30 +1773,15 @@ def get_disks(self, instance_name):
1773 1773
         """
1774 1774
         domain = self._lookup_by_name(instance_name)
1775 1775
         xml = domain.XMLDesc(0)
1776  
-        doc = None
1777 1776
 
1778 1777
         try:
1779 1778
             doc = etree.fromstring(xml)
1780 1779
         except Exception:
1781 1780
             return []
1782 1781
 
1783  
-        disks = []
1784  
-
1785  
-        ret = doc.findall('./devices/disk')
1786  
-
1787  
-        for node in ret:
1788  
-            devdst = None
1789  
-
1790  
-            for child in node.children:
1791  
-                if child.name == 'target':
1792  
-                    devdst = child.prop('dev')
1793  
-
1794  
-            if devdst is None:
1795  
-                continue
1796  
-
1797  
-            disks.append(devdst)
1798  
-
1799  
-        return disks
  1782
+        return filter(bool,
  1783
+                      [target.get("dev") \
  1784
+                           for target in doc.findall('devices/disk/target')])
1800 1785
 
1801 1786
     def get_interfaces(self, instance_name):
1802 1787
         """

No commit comments for this range

Something went wrong with that request. Please try again.