Change in vdsm[master]: vm: Simplify filter using list comprehension

nsoffer at redhat.com nsoffer at redhat.com
Sun Feb 14 21:33:14 UTC 2016


Nir Soffer has uploaded a new change for review.

Change subject: vm: Simplify filter using list comprehension
......................................................................

vm: Simplify filter using list comprehension

Replace unclear filter() with list comprehension and add the missing
test.

Change-Id: I38968cced1053592279fc8477525d8f4b76899af
Signed-off-by: Nir Soffer <nsoffer at redhat.com>
---
M tests/vmTests.py
M vdsm/virt/vm.py
2 files changed, 19 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/03/53503/1

diff --git a/tests/vmTests.py b/tests/vmTests.py
index ff841c8..71f3f85 100644
--- a/tests/vmTests.py
+++ b/tests/vmTests.py
@@ -27,6 +27,7 @@
 import time
 import uuid
 import xml.etree.cElementTree as etree
+from xml.dom.minidom import parseString
 
 import libvirt
 
@@ -1588,6 +1589,22 @@
             vm._syncGuestTime()
 
 
+class TestFilterSnappableDiskDevices(TestCaseBase):
+
+    def test_filter(self):
+        def parse(xml):
+            return parseString(xml).documentElement
+        devices = [
+            parse('<disk/>'),
+            parse('<disk device="lun"/>'),
+            parse('<disk device="disk"/>'),
+            parse('<disk device="cdrom"/>'),
+        ]
+
+        filtered = vm._filterSnappableDiskDevices(devices)
+        self.assertEqual(devices[:-1], filtered)
+
+
 def _load_xml(name):
     test_path = os.path.realpath(__file__)
     data_path = os.path.join(os.path.split(test_path)[0], 'devices', 'data')
diff --git a/vdsm/virt/vm.py b/vdsm/virt/vm.py
index 0839006..df58d1b 100644
--- a/vdsm/virt/vm.py
+++ b/vdsm/virt/vm.py
@@ -95,9 +95,8 @@
 
 
 def _filterSnappableDiskDevices(diskDeviceXmlElements):
-    return filter(lambda x: not(x.getAttribute('device')) or
-                  x.getAttribute('device') in ['disk', 'lun'],
-                  diskDeviceXmlElements)
+    return [x for x in diskDeviceXmlElements
+            if x.getAttribute('device') in ('disk', 'lun', '')]
 
 
 class VolumeError(RuntimeError):


-- 
To view, visit https://gerrit.ovirt.org/53503
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I38968cced1053592279fc8477525d8f4b76899af
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <nsoffer at redhat.com>


More information about the vdsm-patches mailing list