On Wed, Jul 7, 2010 at 3:23 PM, Michael Hanselmann <[email protected]> wrote:
Hi!
> Don't change other modules' internals in unittests as doing so can
> cause bugs very, very difficult to debug. Instead, do something like
> this:
>
> def Mlockall(_ctypes=ctypes):
> if _ctypes:
> _ctypes.….mlockall()
>
> Then you can call Mlockall(_ctypes=None) and
> Mlockall(_ctypes=MockCtypes()) in the unittests.
Ok, inline interdiff below,
Thank you!
Luca
diff --git a/lib/utils.py b/lib/utils.py
index 8d83220..887a74e 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -1772,7 +1772,7 @@ def CloseFDs(noclose_fds=None):
_CloseFDNoErr(fd)
-def Mlockall():
+def Mlockall(_ctypes=ctypes):
"""Lock current process' virtual address space into RAM.
This is equivalent to the C call mlockall(MCL_CURRENT|MCL_FUTURE),
@@ -1781,10 +1781,10 @@ def Mlockall():
@raises errors.NoCtypesError: if ctypes module is not found
"""
- if ctypes is None:
+ if _ctypes is None:
raise errors.NoCtypesError()
- libc = ctypes.cdll.LoadLibrary("libc.so.6")
+ libc = _ctypes.cdll.LoadLibrary("libc.so.6")
if libc is None:
logging.error("Cannot set memory lock, ctypes cannot load libc")
return
@@ -1795,7 +1795,7 @@ def Mlockall():
# its value correctly, should the mlockall call fail, in order to see what
# the actual error code was.
# pylint: disable-msg=W0212
- libc.__errno_location.restype = ctypes.POINTER(ctypes.c_int)
+ libc.__errno_location.restype = _ctypes.POINTER(_ctypes.c_int)
if libc.mlockall(_MCL_CURRENT | _MCL_FUTURE):
# pylint: disable-msg=W0212
diff --git a/test/ganeti.utils_mlockall_unittest.py
b/test/ganeti.utils_mlockall_unittest.py
index 55ea6b1..17726b7 100755
--- a/test/ganeti.utils_mlockall_unittest.py
+++ b/test/ganeti.utils_mlockall_unittest.py
@@ -48,15 +48,8 @@ class TestMlockallWithNoCtypes(unittest.TestCase):
"""
- def setUp(self):
- self.old_ctypes = utils.ctypes
- utils.ctypes = None
-
- def tearDown(self):
- utils.ctypes = self.old_ctypes
-
def test(self):
- self.assertRaises(errors.NoCtypesError, utils.Mlockall)
+ self.assertRaises(errors.NoCtypesError, utils.Mlockall, _ctypes=None)
if __name__ == "__main__":