Change in vdsm[master]: virt: remove duplicate connection in hostdevTests

mpolednik at redhat.com mpolednik at redhat.com
Thu May 14 21:57:18 UTC 2015


Martin Polednik has uploaded a new change for review.

Change subject: virt: remove duplicate connection in hostdevTests
......................................................................

virt: remove duplicate connection in hostdevTests

hostdevTests included a fake Connection, similar to the one in
vmfakelib. This patch creates new decorator, that is able to inject
devices without needing another Connection and without having to hack
the vmfakelib connection.

Change-Id: I936766c8fd4c87175dcc3987d7ae5c64ff5675ea
Signed-off-by: Martin Polednik <mpolednik at redhat.com>
---
M tests/hostdevTests.py
M tests/vmfakelib.py
2 files changed, 33 insertions(+), 17 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/46/40946/1

diff --git a/tests/hostdevTests.py b/tests/hostdevTests.py
index 6cf7b78..94e06cd 100644
--- a/tests/hostdevTests.py
+++ b/tests/hostdevTests.py
@@ -204,22 +204,6 @@
                                   {'params': DEVICES_PARSED['usb_1_1']}}}
 
 
-class Connection(fake.Connection):
-
-    def __init__(self, *args):
-        self._virNodeDevices = []
-
-        for device in _PCI_DEVICES + _USB_DEVICES + _SCSI_DEVICES:
-            self._virNodeDevices.append(
-                fake.Connection.nodeDeviceLookupByName(device))
-
-    def listAllDevices(self, flags=0):
-        return self._virNodeDevices
-
-    def nodeDeviceLookupByName(self, device_xml_filename):
-        return fake.Connection.nodeDeviceLookupByName(device_xml_filename)
-
-
 def _fake_totalvfs(device_name):
     if device_name == 'pci_0000_05_00_1':
         return 7
@@ -228,7 +212,7 @@
 
 
 @expandPermutations
- at MonkeyClass(libvirtconnection, 'get', Connection)
+ at MonkeyClass(libvirtconnection, 'get', fake.Connection)
 @MonkeyClass(hostdev, '_sriov_totalvfs', _fake_totalvfs)
 @MonkeyClass(hooks, 'after_hostdev_list_by_caps', lambda json: json)
 class HostdevTests(TestCaseBase):
@@ -265,6 +249,7 @@
 
         self.assertEquals(_NET_DEVICE_PARSED, deviceXML)
 
+    @fake.set_device_pool(_PCI_DEVICES + _USB_DEVICES + _SCSI_DEVICES)
     def testGetDevicesFromLibvirt(self):
         libvirt_devices = hostdev._get_devices_from_libvirt()
 
@@ -274,6 +259,7 @@
                          len(_USB_DEVICES) +
                          len(_SCSI_DEVICES))
 
+    @fake.set_device_pool(_PCI_DEVICES + _USB_DEVICES + _SCSI_DEVICES)
     @permutations([[''], [('pci',)], [('usb_device',)],
                    [('pci', 'usb_device')]])
     def testListByCaps(self, caps):
diff --git a/tests/vmfakelib.py b/tests/vmfakelib.py
index a77d15e..9c15325 100644
--- a/tests/vmfakelib.py
+++ b/tests/vmfakelib.py
@@ -20,6 +20,7 @@
 #
 
 from contextlib import contextmanager
+from functools import wraps
 import logging
 import os
 import threading
@@ -49,6 +50,9 @@
     def listAllNetworks(self, *args):
         return []
 
+    def listAllDevices(self, flags=0):
+        pass
+
     @staticmethod
     def nodeDeviceLookupByName(name):
         """
@@ -71,6 +75,32 @@
         return VirNodeDeviceStub(device_xml)
 
 
+def set_device_pool(devices):
+    """
+    Decorator to inject new listAllDevices method, that will return
+    VirNodeDeviceStubs from device names specified in the argument.
+    """
+    def decorator(func):
+        @wraps(func)
+        def wrapper(*args, **kwargs):
+            vir_node_devices = []
+
+            for device in devices:
+                vir_node_devices.append(
+                    Connection.nodeDeviceLookupByName(device)
+                )
+
+            def listAllDevices(_, flags=0):
+                return vir_node_devices
+
+            with MonkeyPatchScope(
+                [(libvirtconnection.get, 'listAllDevices', listAllDevices)]
+            ):
+                return func(*args, **kwargs)
+        return wrapper
+    return decorator
+
+
 class ClientIF(clientIF.clientIF):
     def __init__(self):
         # the bare minimum initialization for our test needs.


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I936766c8fd4c87175dcc3987d7ae5c64ff5675ea
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Martin Polednik <mpolednik at redhat.com>


More information about the vdsm-patches mailing list