[PATCH pyparted] Use addCleanup instead of tearDown for removing temp-device.

Chris Lumens clumens at redhat.com
Fri Jul 24 19:58:25 UTC 2015


If something fails in setUp, tearDown will not be called.  However, addCleanup
will always be called.  We should use this to make sure no /tmp/temp-device-*
files stay around.
---
 tests/baseclass.py           | 23 +++++++++++++++--------
 tests/test__ped_partition.py |  4 ++--
 2 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/tests/baseclass.py b/tests/baseclass.py
index 617382e..4f48b87 100644
--- a/tests/baseclass.py
+++ b/tests/baseclass.py
@@ -27,13 +27,16 @@ import unittest
 # Base class for any test case that requires a temp device node
 class RequiresDeviceNode(unittest.TestCase):
     def setUp(self):
+        self.addCleanup(self.removeTempDevice)
+
         (fd, self.path) = tempfile.mkstemp(prefix="temp-device-")
         f = os.fdopen(fd)
         f.seek(140000)
         os.write(fd, b"0")
 
-    def tearDown(self):
-        os.unlink(self.path)
+    def removeTempDevice(self):
+        if self.path and os.path.exists(self.path):
+            os.unlink(self.path)
 
 # Base class for any test case that requires a _ped.Device or parted.Device
 # object first.
@@ -46,6 +49,8 @@ class RequiresDevice(RequiresDeviceNode):
 # Base class for any test case that requires a filesystem on a device.
 class RequiresFileSystem(unittest.TestCase):
     def setUp(self):
+        self.addCleanup(self.removeTempDevice)
+
         self._fileSystemType = {}
         ty = _ped.file_system_type_get_next()
         self._fileSystemType[ty.name] = ty
@@ -68,8 +73,9 @@ class RequiresFileSystem(unittest.TestCase):
         self._device = _ped.device_get(self.path)
         self._geometry = _ped.Geometry(self._device, 0, self._device.length - 1)
 
-    def tearDown(self):
-        os.unlink(self.path)
+    def removeTempDevice(self):
+        if self.path and os.path.exists(self.path):
+            os.unlink(self.path)
 
 # Base class for certain alignment tests that require a _ped.Device
 class RequiresDeviceAlignment(RequiresDevice):
@@ -139,6 +145,7 @@ class RequiresDisk(RequiresDevice):
 # Base class for any test case that requires a filesystem made and mounted.
 class RequiresMount(RequiresDevice):
     def setUp(self):
+        self.addCleanup(self.removeMountpoint)
         RequiresDevice.setUp(self)
         self.mountpoint = None
 
@@ -149,10 +156,10 @@ class RequiresMount(RequiresDevice):
         self.mountpoint = tempfile.mkdtemp()
         os.system("mount -o loop %s %s" % (self.path, self.mountpoint))
 
-    def tearDown(self):
-        os.system("umount %s" % self.mountpoint)
-        os.rmdir(self.mountpoint)
-        RequiresDevice.tearDown(self)
+    def removeMountpoint(self):
+        if self.mountpoint and os.path.exists(self.mountpoint):
+            os.system("umount %s" % self.mountpoint)
+            os.rmdir(self.mountpoint)
 
 # Base class for any test case that requires a _ped.Partition.
 class RequiresPartition(RequiresDisk):
diff --git a/tests/test__ped_partition.py b/tests/test__ped_partition.py
index 1d9b225..7110545 100755
--- a/tests/test__ped_partition.py
+++ b/tests/test__ped_partition.py
@@ -184,6 +184,7 @@ class PartitionGetNameTestCase(RequiresPartition):
 
 class PartitionIsBusyTestCase(RequiresPartition):
     def setUp(self):
+        self.addCleanup(self.removeMountpoint)
         RequiresPartition.setUp(self)
         self.mountpoint = None
 
@@ -194,11 +195,10 @@ class PartitionIsBusyTestCase(RequiresPartition):
     def mkfs(self):
         os.system("mkfs.ext2 -F -q %s" % self.path)
 
-    def tearDown(self):
+    def removeMountpoint(self):
         if self.mountpoint:
             os.system("umount %s" % self.mountpoint)
             os.rmdir(self.mountpoint)
-        RequiresPartition.tearDown(self)
 
     def runTest(self):
         # partitions aren't busy until they're mounted.
-- 
2.4.3



More information about the anaconda-patches mailing list