Change in vdsm[master]: tests: split vm tests helpers in a module

fromani at redhat.com fromani at redhat.com
Mon Sep 8 15:44:13 UTC 2014


Francesco Romani has uploaded a new change for review.

Change subject: tests: split vm tests helpers in a module
......................................................................

tests: split vm tests helpers in a module

For the VM tests, we gathered a fair amount of Fake
helper objects. Is time to move them in a module,
in order to make it simpler for the other tests
to use them, and also to avoid to vmTests.py
become another vm.py, before it's too late.

No code changes, just moving code around.

Change-Id: Ied7bb1d38d3814c406a53e1b7dbcac34eb6ffeb7
Signed-off-by: Francesco Romani <fromani at redhat.com>
---
M tests/vmApiTests.py
M tests/vmTests.py
M tests/vmXmlTests.py
A tests/vmtestlib.py
4 files changed, 145 insertions(+), 112 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/50/32650/1

diff --git a/tests/vmApiTests.py b/tests/vmApiTests.py
index 0342743..bf18f98 100644
--- a/tests/vmApiTests.py
+++ b/tests/vmApiTests.py
@@ -28,7 +28,7 @@
 from vdsm import utils
 from rpc import vdsmapi
 
-from vmTests import FakeVM
+from vmtestlib import FakeVM
 
 
 class TestSchemaCompliancyBase(TestCaseBase):
diff --git a/tests/vmTests.py b/tests/vmTests.py
index 7ece7dd..8455097 100644
--- a/tests/vmTests.py
+++ b/tests/vmTests.py
@@ -19,7 +19,6 @@
 # Refer to the README and COPYING files for full details of the license
 #
 
-from contextlib import contextmanager
 from itertools import product
 import re
 import shutil
@@ -39,7 +38,7 @@
 from vdsm import constants
 from vdsm import define
 from testlib import VdsmTestCase as TestCaseBase
-from testlib import permutations, expandPermutations, namedTemporaryDir
+from testlib import permutations, expandPermutations
 import caps
 import hooks
 from vdsm import utils
@@ -48,81 +47,9 @@
 from vmTestsData import CONF_TO_DOMXML_X86_64
 from vmTestsData import CONF_TO_DOMXML_PPC64
 from vmTestsData import CONF_TO_DOMXML_NO_VDSM
+from vmtestlib import ConnectionMock, FakeDomain, FakeVM
 
 from testValidation import slowtest
-
-
-class ConnectionMock:
-    def __init__(self, *args):
-        pass
-
-    def domainEventRegisterAny(self, *arg):
-        pass
-
-    def listAllNetworks(self, *args):
-        return []
-
-
-class FakeClientIF(object):
-    def __init__(self, *args, **kwargs):
-        self.channelListener = None
-        self.vmContainer = {}
-
-
-class FakeDomain(object):
-    def __init__(self, xml='',
-                 virtError=libvirt.VIR_ERR_OK,
-                 domState=libvirt.VIR_DOMAIN_RUNNING,
-                 vmId=''):
-        self._xml = xml
-        self.devXml = ''
-        self._virtError = virtError
-        self._metadata = ""
-        self._io_tune = {}
-        self._domState = domState
-        self._vmId = vmId
-
-    def _failIfRequested(self):
-        if self._virtError != libvirt.VIR_ERR_OK:
-            err = libvirt.libvirtError(defmsg='')
-            err.err = [self._virtError]
-            raise err
-
-    def UUIDString(self):
-        return self._vmId
-
-    def info(self):
-        self._failIfRequested()
-        return (self._domState, )
-
-    def XMLDesc(self, unused):
-        return self._xml
-
-    def updateDeviceFlags(self, devXml, unused):
-        self.devXml = devXml
-
-    def vcpusFlags(self, flags):
-        return -1
-
-    def metadata(self, type, uri, flags):
-        self._failIfRequested()
-
-        if not self._metadata:
-            e = libvirt.libvirtError("No metadata")
-            e.err = [libvirt.VIR_ERR_NO_DOMAIN_METADATA]
-            raise e
-        return self._metadata
-
-    def setMetadata(self, type, xml, prefix, uri, flags):
-        self._metadata = xml
-
-    def schedulerParameters(self):
-        return {'vcpu_quota': vm._NO_CPU_QUOTA,
-                'vcpu_period': vm._NO_CPU_PERIOD}
-
-    def setBlockIoTune(self, name, io_tune, flags):
-        self._io_tune[name] = io_tune
-        return 1
 
 
 class TestVm(TestCaseBase):
@@ -1327,41 +1254,6 @@
             self.assertEqual(graphDev.tlsPort, tlsPort)
             self.assertEqual(graphDev.port, graphConf['port'])
             self.assertEqual(graphDev.tlsPort, graphConf['tlsPort'])
-
-
-class FakeGuestAgent(object):
-    def getGuestInfo(self):
-        return {
-            'username': 'Unknown',
-            'session': 'Unknown',
-            'memUsage': 0,
-            'appsList': [],
-            'guestIPs': '',
-            'guestFQDN': '',
-            'disksUsage': [],
-            'netIfaces': [],
-            'memoryStats': {},
-            'guestCPUCount': -1}
-
-
- at contextmanager
-def FakeVM(params=None, devices=None, runCpu=False,
-           arch=caps.Architecture.X86_64, status=None):
-    with namedTemporaryDir() as tmpDir:
-        with MonkeyPatchScope([(constants, 'P_VDSM_RUN', tmpDir + '/'),
-                               (libvirtconnection, 'get', ConnectionMock)]):
-            vmParams = {'vmId': 'TESTING'}
-            vmParams.update({} if params is None else params)
-            cif = FakeClientIF()
-            fake = vm.Vm(cif, vmParams)
-            cif.vmContainer[fake.id] = fake
-            fake.arch = arch
-            fake.guestAgent = FakeGuestAgent()
-            fake.conf['devices'] = [] if devices is None else devices
-            fake._guestCpuRunning = runCpu
-            if status is not None:
-                fake._lastStatus = status
-            yield fake
 
 
 @expandPermutations
diff --git a/tests/vmXmlTests.py b/tests/vmXmlTests.py
index cda1c70..f044a57 100644
--- a/tests/vmXmlTests.py
+++ b/tests/vmXmlTests.py
@@ -25,7 +25,7 @@
 from testlib import VdsmTestCase as TestCaseBase
 from testlib import permutations, expandPermutations
 
-from vmTests import FakeDomain
+from vmtestlib import FakeDomain
 
 from vmTestsData import CONF_TO_DOMXML_X86_64
 from vmTestsData import CONF_TO_DOMXML_PPC64
diff --git a/tests/vmtestlib.py b/tests/vmtestlib.py
new file mode 100644
index 0000000..0df8c80
--- /dev/null
+++ b/tests/vmtestlib.py
@@ -0,0 +1,141 @@
+#
+# Copyright IBM Corp. 2012
+# Copyright 2013-2014 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+
+from contextlib import contextmanager
+
+import libvirt
+
+from vdsm import constants
+from vdsm import libvirtconnection
+
+import caps
+from virt import vm
+
+from testlib import namedTemporaryDir
+from monkeypatch import MonkeyPatchScope
+
+
+class ConnectionMock:
+    def __init__(self, *args):
+        pass
+
+    def domainEventRegisterAny(self, *arg):
+        pass
+
+    def listAllNetworks(self, *args):
+        return []
+
+
+class FakeClientIF(object):
+    def __init__(self, *args, **kwargs):
+        self.channelListener = None
+        self.vmContainer = {}
+
+
+class FakeDomain(object):
+    def __init__(self, xml='',
+                 virtError=libvirt.VIR_ERR_OK,
+                 domState=libvirt.VIR_DOMAIN_RUNNING,
+                 vmId=''):
+        self._xml = xml
+        self.devXml = ''
+        self._virtError = virtError
+        self._metadata = ""
+        self._io_tune = {}
+        self._domState = domState
+        self._vmId = vmId
+
+    def _failIfRequested(self):
+        if self._virtError != libvirt.VIR_ERR_OK:
+            err = libvirt.libvirtError(defmsg='')
+            err.err = [self._virtError]
+            raise err
+
+    def UUIDString(self):
+        return self._vmId
+
+    def info(self):
+        self._failIfRequested()
+        return (self._domState, )
+
+    def XMLDesc(self, unused):
+        return self._xml
+
+    def updateDeviceFlags(self, devXml, unused):
+        self.devXml = devXml
+
+    def vcpusFlags(self, flags):
+        return -1
+
+    def metadata(self, type, uri, flags):
+        self._failIfRequested()
+
+        if not self._metadata:
+            e = libvirt.libvirtError("No metadata")
+            e.err = [libvirt.VIR_ERR_NO_DOMAIN_METADATA]
+            raise e
+        return self._metadata
+
+    def setMetadata(self, type, xml, prefix, uri, flags):
+        self._metadata = xml
+
+    def schedulerParameters(self):
+        return {'vcpu_quota': vm._NO_CPU_QUOTA,
+                'vcpu_period': vm._NO_CPU_PERIOD}
+
+    def setBlockIoTune(self, name, io_tune, flags):
+        self._io_tune[name] = io_tune
+        return 1
+
+
+class FakeGuestAgent(object):
+    def getGuestInfo(self):
+        return {
+            'username': 'Unknown',
+            'session': 'Unknown',
+            'memUsage': 0,
+            'appsList': [],
+            'guestIPs': '',
+            'guestFQDN': '',
+            'disksUsage': [],
+            'netIfaces': [],
+            'memoryStats': {},
+            'guestCPUCount': -1}
+
+
+ at contextmanager
+def FakeVM(params=None, devices=None, runCpu=False,
+           arch=caps.Architecture.X86_64, status=None):
+    with namedTemporaryDir() as tmpDir:
+        with MonkeyPatchScope([(constants, 'P_VDSM_RUN', tmpDir + '/'),
+                               (libvirtconnection, 'get', ConnectionMock)]):
+            vmParams = {'vmId': 'TESTING'}
+            vmParams.update({} if params is None else params)
+            cif = FakeClientIF()
+            fake = vm.Vm(cif, vmParams)
+            cif.vmContainer[fake.id] = fake
+            fake.arch = arch
+            fake.guestAgent = FakeGuestAgent()
+            fake.conf['devices'] = [] if devices is None else devices
+            fake._guestCpuRunning = runCpu
+            if status is not None:
+                fake._lastStatus = status
+            yield fake


-- 
To view, visit http://gerrit.ovirt.org/32650
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ied7bb1d38d3814c406a53e1b7dbcac34eb6ffeb7
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Francesco Romani <fromani at redhat.com>


More information about the vdsm-patches mailing list