[blivet:master 07/11] Make DevicelibsTestCase devices configurable.

mulhern amulhern at redhat.com
Tue Jun 17 15:07:58 UTC 2014


Allow choosing size of backing file. Do not use the path of the backing
file in all subclasses in previous strange way, since it is irrelevant
to all the subclasses, and only relevant to DevicelibsTestCase which has
to do the necessary cleanup. Use a new attribute, loopDevices, to store
the actual loopDevices, which are what is required by the subclasses.
Fix up the subclasses to use this attribute appropriately.

Signed-off-by: mulhern <amulhern at redhat.com>
---
 tests/devicelibs_test/baseclass.py   | 30 +++++++++++++++++++++------
 tests/devicelibs_test/btrfs_test.py  | 20 +++++++++---------
 tests/devicelibs_test/crypto_test.py | 17 ++++++++--------
 tests/devicelibs_test/lvm_test.py    | 14 ++++++-------
 tests/devicelibs_test/mdraid_test.py | 11 ++++------
 tests/devicelibs_test/swap_test.py   |  4 ++--
 tests/formats_test/fslabeling.py     | 39 +++++++++++-------------------------
 tests/formats_test/labeling_test.py  | 12 +++--------
 tests/formats_test/selinux_test.py   | 11 +++++-----
 9 files changed, 75 insertions(+), 83 deletions(-)

diff --git a/tests/devicelibs_test/baseclass.py b/tests/devicelibs_test/baseclass.py
index 9b3d991..5e6d51e 100644
--- a/tests/devicelibs_test/baseclass.py
+++ b/tests/devicelibs_test/baseclass.py
@@ -67,18 +67,36 @@ def getFreeLoopDev():
 @unittest.skipUnless(os.geteuid() == 0, "requires root privileges")
 class DevicelibsTestCase(unittest.TestCase):
 
-    _LOOP_DEVICES = ["/tmp/test-virtdev0", "/tmp/test-virtdev1"]
+    DEFAULT_STORE_SIZE = 102400
+    _DEFAULT_DEVICE_SPEC = [DEFAULT_STORE_SIZE, DEFAULT_STORE_SIZE]
+    _STORE_FILE_BASE_NAME = 'test-virtdev'
+    _STORE_FILE_PATH = '/tmp'
 
-    def __init__(self, methodName='runTest'):
+    def __init__(self, methodName='runTest', deviceSpec=None):
+        """ DevicelibsTestCase manages loop devices.
+
+            It constructs loop devices according to loopDeviceSpec,
+            sets them up, and tears them down again.
+
+            :param deviceSpec: Specification for the loop devices.
+            :type deviceSpec: list of int
+
+            deviceSpec is currently just a list of ints corresponding
+            to the number of blocks for each backing store.
+        """
         unittest.TestCase.__init__(self, methodName=methodName)
+        self._deviceSpec = deviceSpec or self._DEFAULT_DEVICE_SPEC
         self._loopMap = {}
+        self.loopDevices = []
 
     def setUp(self):
-        for store in self._LOOP_DEVICES:
+        for index, size in enumerate(self._deviceSpec):
+            store = os.path.join(self._STORE_FILE_PATH, (self._STORE_FILE_BASE_NAME + "%d") % index)
             dev = getFreeLoopDev()
-            makeLoopDev(dev, store)
-            self._loopMap[store] = dev
+            makeLoopDev(dev, store, num_blocks=size)
+            self._loopMap[dev] = store
+            self.loopDevices.append(dev)
 
     def tearDown(self):
-        for (store, dev) in self._loopMap.iteritems():
+        for (dev, store) in self._loopMap.iteritems():
             removeLoopDev(dev, store)
diff --git a/tests/devicelibs_test/btrfs_test.py b/tests/devicelibs_test/btrfs_test.py
index b01720b..740bb1c 100755
--- a/tests/devicelibs_test/btrfs_test.py
+++ b/tests/devicelibs_test/btrfs_test.py
@@ -28,8 +28,8 @@ class BTRFSMountDevice(baseclass.DevicelibsTestCase):
         """
         baseclass.DevicelibsTestCase.setUp(self)
 
-        btrfs.create_volume(self._loopMap.values())
-        self.device = self._loopMap.values()[0]
+        btrfs.create_volume(self.loopDevices)
+        self.device = self.loopDevices[0]
 
         self.mountpoint = tempfile.mkdtemp()
         rc = subprocess.call(["mount", self.device, self.mountpoint])
@@ -59,8 +59,8 @@ class BTRFSAsRootTestCase1(baseclass.DevicelibsTestCase):
 
            These tests are limited to simple creating and scanning.
         """
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-        _LOOP_DEV1 = self._loopMap[self._LOOP_DEVICES[1]]
+        _LOOP_DEV0 = self.loopDevices[0]
+        _LOOP_DEV1 = self.loopDevices[1]
 
         ##
         ## create_volume
@@ -89,7 +89,7 @@ class BTRFSAsRootTestCase1(baseclass.DevicelibsTestCase):
            [_LOOP_DEV0], metadata="RaID7")
 
         # pass
-        self.assertEqual(btrfs.create_volume(self._loopMap.values()), 0)
+        self.assertEqual(btrfs.create_volume(self.loopDevices), 0)
 
         # already created
         self.assertRaisesRegexp(BTRFSError,
@@ -98,10 +98,10 @@ class BTRFSAsRootTestCase1(baseclass.DevicelibsTestCase):
            [_LOOP_DEV0], metadata="RaID7")
 
     def testMkfsDefaults(self):
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-        _LOOP_DEV1 = self._loopMap[self._LOOP_DEVICES[1]]
+        _LOOP_DEV0 = self.loopDevices[0]
+        _LOOP_DEV1 = self.loopDevices[1]
 
-        btrfs.create_volume(self._loopMap.values())
+        btrfs.create_volume(self.loopDevices)
 
         self.assertEqual(btrfs.summarize_filesystem(_LOOP_DEV0)["label"], "none")
         self.assertEqual(btrfs.summarize_filesystem(_LOOP_DEV0)["num_devices"], "2")
@@ -117,8 +117,8 @@ class BTRFSAsRootTestCase2(BTRFSMountDevice):
 
     def testSubvolume(self):
         """Tests which focus on subvolumes."""
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-        _LOOP_DEV1 = self._loopMap[self._LOOP_DEVICES[1]]
+        _LOOP_DEV0 = self.loopDevices[0]
+        _LOOP_DEV1 = self.loopDevices[1]
 
         # no subvolumes yet
         self.assertEqual(btrfs.list_subvolumes(self.mountpoint), [])
diff --git a/tests/devicelibs_test/crypto_test.py b/tests/devicelibs_test/crypto_test.py
index e37df15..8316a21 100755
--- a/tests/devicelibs_test/crypto_test.py
+++ b/tests/devicelibs_test/crypto_test.py
@@ -30,8 +30,8 @@ from tests.devicelibs_test import baseclass
 class CryptoTestCase(baseclass.DevicelibsTestCase):
 
     def testCryptoMisc(self):
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-        _LOOP_DEV1 = self._loopMap[self._LOOP_DEVICES[1]]
+        _LOOP_DEV0 = self.loopDevices[0]
+        _LOOP_DEV1 = self.loopDevices[1]
 
 
         ##
@@ -134,12 +134,11 @@ class CryptoTestCase2(baseclass.DevicelibsTestCase):
     def __init__(self, methodName='runTest'):
         """Set up the names by which luks knows these devices."""
         super(CryptoTestCase2, self).__init__(methodName=methodName)
-        self._names = { self._LOOP_DEVICES[0]: "crypted",
-           self._LOOP_DEVICES[1]: "encrypted" }
+        self._names = ["crypted", "encrypted"]
 
     def tearDown(self):
         """Close all devices just in case they are still open."""
-        for name in self._names.values():
+        for name in self._names:
             try:
                 crypto.luks_close(name)
             except (IOError, CryptoError):
@@ -147,11 +146,11 @@ class CryptoTestCase2(baseclass.DevicelibsTestCase):
         super(CryptoTestCase2, self).tearDown()
 
     def testCryptoOpen(self):
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-        _LOOP_DEV1 = self._loopMap[self._LOOP_DEVICES[1]]
+        _LOOP_DEV0 = self.loopDevices[0]
+        _LOOP_DEV1 = self.loopDevices[1]
 
-        _name0 = self._names[self._LOOP_DEVICES[0]]
-        _name1 = self._names[self._LOOP_DEVICES[1]]
+        _name0 = self._names[0]
+        _name1 = self._names[1]
 
         ##
         ## luks_format
diff --git a/tests/devicelibs_test/lvm_test.py b/tests/devicelibs_test/lvm_test.py
index 1c941ea..a66c756 100755
--- a/tests/devicelibs_test/lvm_test.py
+++ b/tests/devicelibs_test/lvm_test.py
@@ -59,7 +59,7 @@ class LVMAsRootTestCase(baseclass.DevicelibsTestCase):
             pass
 
         try:
-            for dev in self._loopMap.values():
+            for dev in self.loopDevices:
                 lvm.pvremove(dev)
         except LVMError:
             pass
@@ -67,14 +67,14 @@ class LVMAsRootTestCase(baseclass.DevicelibsTestCase):
         super(LVMAsRootTestCase, self).tearDown()
 
     def testLVM(self):
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-        _LOOP_DEV1 = self._loopMap[self._LOOP_DEVICES[1]]
+        _LOOP_DEV0 = self.loopDevices[0]
+        _LOOP_DEV1 = self.loopDevices[1]
 
         ##
         ## pvcreate
         ##
         # pass
-        for dev in self._loopMap.values():
+        for dev in self.loopDevices:
             self.assertEqual(lvm.pvcreate(dev), None)
 
         # fail
@@ -84,7 +84,7 @@ class LVMAsRootTestCase(baseclass.DevicelibsTestCase):
         ## pvresize
         ##
         # pass
-        for dev in self._loopMap.values():
+        for dev in self.loopDevices:
             self.assertEqual(lvm.pvresize(dev, Size("50MiB")), None)
             self.assertEqual(lvm.pvresize(dev, Size("100MiB")), None)
 
@@ -98,7 +98,7 @@ class LVMAsRootTestCase(baseclass.DevicelibsTestCase):
         ## vgcreate
         ##
         # pass
-        self.assertEqual(lvm.vgcreate(self._vg_name, [_LOOP_DEV0, _LOOP_DEV1], Size("4MiB")), None)
+        self.assertEqual(lvm.vgcreate(self._vg_name, self.loopDevices, Size("4MiB")), None)
 
         # fail
         self.assertRaisesRegexp(LVMError,
@@ -256,7 +256,7 @@ class LVMAsRootTestCase(baseclass.DevicelibsTestCase):
         ## pvremove
         ##
         # pass
-        for dev in self._loopMap.values():
+        for dev in self.loopDevices:
             self.assertEqual(lvm.pvremove(dev), None)
 
         # fail
diff --git a/tests/devicelibs_test/mdraid_test.py b/tests/devicelibs_test/mdraid_test.py
index b0a100b..3b12aaf 100755
--- a/tests/devicelibs_test/mdraid_test.py
+++ b/tests/devicelibs_test/mdraid_test.py
@@ -59,7 +59,7 @@ class MDRaidAsRootTestCase(baseclass.DevicelibsTestCase):
     def tearDown(self):
         try:
             mdraid.mddeactivate(self._dev_name)
-            for dev in self._loopMap.values():
+            for dev in self.loopDevices:
                 mdraid.mddestroy(dev)
         except MDRaidError:
             pass
@@ -67,14 +67,11 @@ class MDRaidAsRootTestCase(baseclass.DevicelibsTestCase):
         super(MDRaidAsRootTestCase, self).tearDown()
 
     def testMDRaidAsRoot(self):
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-        _LOOP_DEV1 = self._loopMap[self._LOOP_DEVICES[1]]
-
         ##
         ## mdcreate
         ##
         # pass
-        self.assertEqual(mdraid.mdcreate(self._dev_name, raid.RAID1, [_LOOP_DEV0, _LOOP_DEV1]), None)
+        self.assertEqual(mdraid.mdcreate(self._dev_name, raid.RAID1, self.loopDevices), None)
         # wait for raid to settle
         time.sleep(2)
 
@@ -110,8 +107,8 @@ class MDRaidAsRootTestCase(baseclass.DevicelibsTestCase):
         ## mddestroy
         ##
         # pass
-        self.assertEqual(mdraid.mddestroy(_LOOP_DEV0), None)
-        self.assertEqual(mdraid.mddestroy(_LOOP_DEV1), None)
+        for dev in self.loopDevices:
+            self.assertEqual(mdraid.mddestroy(dev), None)
 
         # pass
         # Note that these should fail because mdadm is unable to locate the
diff --git a/tests/devicelibs_test/swap_test.py b/tests/devicelibs_test/swap_test.py
index 0784352..8f7d2bb 100755
--- a/tests/devicelibs_test/swap_test.py
+++ b/tests/devicelibs_test/swap_test.py
@@ -9,8 +9,8 @@ from tests.devicelibs_test import baseclass
 class SwapTestCase(baseclass.DevicelibsTestCase):
 
     def testSwap(self):
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-        _LOOP_DEV1 = self._loopMap[self._LOOP_DEVICES[1]]
+        _LOOP_DEV0 = self.loopDevices[0]
+        _LOOP_DEV1 = self.loopDevices[1]
 
 
         ##
diff --git a/tests/formats_test/fslabeling.py b/tests/formats_test/fslabeling.py
index f69ffa8..4cfa9f1 100644
--- a/tests/formats_test/fslabeling.py
+++ b/tests/formats_test/fslabeling.py
@@ -19,6 +19,9 @@ class LabelingAsRoot(baseclass.DevicelibsTestCase):
     _invalid_label = abc.abstractproperty(
        doc="A label which is invalid for this filesystem.")
 
+    def __init__(self, methodName='runTest'):
+        super(LabelingAsRoot, self).__init__(methodName=methodName, deviceSpec=[102400])
+
     def setUp(self):
         an_fs = self._fs_class()
         if not an_fs.utilsAvailable:
@@ -32,9 +35,7 @@ class LabelingAsRoot(baseclass.DevicelibsTestCase):
            * raise an exception when reading the filesystem
            * raise an exception when relabeling the filesystem
         """
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-
-        an_fs = self._fs_class(device=_LOOP_DEV0, label=self._invalid_label)
+        an_fs = self._fs_class(device=self.loopDevices[0], label=self._invalid_label)
         self.assertIsNone(an_fs.create())
 
         self.assertRaisesRegexp(FSError,
@@ -48,25 +49,19 @@ class LabelingAsRoot(baseclass.DevicelibsTestCase):
 
     def testCreating(self):
         """Create the filesystem when passing a valid label """
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-
-        an_fs = self._fs_class(device=_LOOP_DEV0, label="start")
+        an_fs = self._fs_class(device=self.loopDevices[0], label="start")
         self.assertIsNone(an_fs.create())
 
     def testCreatingNone(self):
         """Create the filesystem when passing None
            (indicates filesystem default)
         """
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-
-        an_fs = self._fs_class(device=_LOOP_DEV0, label=None)
+        an_fs = self._fs_class(device=self.loopDevices[0], label=None)
         self.assertIsNone(an_fs.create())
 
     def testCreatingEmpty(self):
         """Create the filesystem when passing the empty label."""
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-
-        an_fs = self._fs_class(device=_LOOP_DEV0, label="")
+        an_fs = self._fs_class(device=self.loopDevices[0], label="")
         self.assertIsNone(an_fs.create())
 
 class LabelingWithRelabeling(LabelingAsRoot):
@@ -83,9 +78,7 @@ class LabelingWithRelabeling(LabelingAsRoot):
            * raise an exception when relabeling when None is specified
            * raise an exception when relabeling with an invalid label
         """
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-
-        an_fs = self._fs_class(device=_LOOP_DEV0, label=self._invalid_label)
+        an_fs = self._fs_class(device=self.loopDevices[0], label=self._invalid_label)
         self.assertIsNone(an_fs.create())
 
         self.assertRaisesRegexp(FSError,
@@ -125,9 +118,7 @@ class CompleteLabelingAsRoot(LabelingAsRoot):
            * raise an exception when relabeling when None is specified
            * raise an exception when relabeling with an invalid label
         """
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-
-        an_fs = self._fs_class(device=_LOOP_DEV0, label=self._invalid_label)
+        an_fs = self._fs_class(device=self.loopDevices[0], label=self._invalid_label)
         self.assertIsNone(an_fs.create())
         self.assertEqual(an_fs.readLabel(), an_fs._labelfs.default_label)
 
@@ -153,9 +144,7 @@ class CompleteLabelingAsRoot(LabelingAsRoot):
         """Create the filesystem when passing a valid label.
            Verify that the filesystem has that label.
         """
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-
-        an_fs = self._fs_class(device=_LOOP_DEV0, label="start")
+        an_fs = self._fs_class(device=self.loopDevices[0], label="start")
         self.assertIsNone(an_fs.create())
         self.assertEqual(an_fs.readLabel(), "start")
 
@@ -163,9 +152,7 @@ class CompleteLabelingAsRoot(LabelingAsRoot):
         """Create a filesystem with the label None.
            Verify that the filesystem has the default label.
         """
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-
-        an_fs = self._fs_class(device=_LOOP_DEV0, label=None)
+        an_fs = self._fs_class(device=self.loopDevices[0], label=None)
         self.assertIsNone(an_fs.create())
         self.assertEqual(an_fs.readLabel(), an_fs._labelfs.default_label)
 
@@ -173,8 +160,6 @@ class CompleteLabelingAsRoot(LabelingAsRoot):
         """Create a filesystem with an empty label.
            Verify that the filesystem has the empty label.
         """
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-
-        an_fs = self._fs_class(device=_LOOP_DEV0, label="")
+        an_fs = self._fs_class(device=self.loopDevices[0], label="")
         self.assertIsNone(an_fs.create())
         self.assertEqual(an_fs.readLabel(), "")
diff --git a/tests/formats_test/labeling_test.py b/tests/formats_test/labeling_test.py
index 1444e2a..1519cca 100755
--- a/tests/formats_test/labeling_test.py
+++ b/tests/formats_test/labeling_test.py
@@ -129,22 +129,16 @@ class HFSTestCase(fslabeling.LabelingAsRoot):
 class LabelingSwapSpaceTestCase(baseclass.DevicelibsTestCase):
 
     def testLabeling(self):
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-
-        swp = swap.SwapSpace(device=_LOOP_DEV0)
+        swp = swap.SwapSpace(device=self.loopDevices[0])
         swp.label = "mkswap is really pretty permissive about labels"
         self.assertIsNone(swp.create())
 
     def testCreatingSwapSpaceNone(self):
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-
-        swp = swap.SwapSpace(device=_LOOP_DEV0, label=None)
+        swp = swap.SwapSpace(device=self.loopDevices[0], label=None)
         self.assertIsNone(swp.create())
 
     def testCreatingSwapSpaceEmpty(self):
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-
-        swp = swap.SwapSpace(device=_LOOP_DEV0, label="")
+        swp = swap.SwapSpace(device=self.loopDevices[0], label="")
         self.assertIsNone(swp.create())
 
 if __name__ == "__main__":
diff --git a/tests/formats_test/selinux_test.py b/tests/formats_test/selinux_test.py
index 4106312..9c48ea4 100755
--- a/tests/formats_test/selinux_test.py
+++ b/tests/formats_test/selinux_test.py
@@ -12,10 +12,11 @@ class SELinuxContextTestCase(baseclass.DevicelibsTestCase):
     """Testing SELinux contexts.
     """
 
-    def testMountingExt2FS(self):
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
+    def __init__(self, methodName='runTest'):
+        super(SELinuxContextTestCase, self).__init__(methodName=methodName, deviceSpec=[102400])
 
-        an_fs = fs.Ext2FS(device=_LOOP_DEV0, label="test")
+    def testMountingExt2FS(self):
+        an_fs = fs.Ext2FS(device=self.loopDevices[0], label="test")
         self.assertIsNone(an_fs.create())
 
         blivet.flags.installer_mode = False
@@ -57,9 +58,7 @@ class SELinuxContextTestCase(baseclass.DevicelibsTestCase):
            'system_u:object_r:lost_found_t:s0')
 
     def testMountingXFS(self):
-        _LOOP_DEV0 = self._loopMap[self._LOOP_DEVICES[0]]
-
-        an_fs = fs.XFS(device=_LOOP_DEV0, label="test")
+        an_fs = fs.XFS(device=self.loopDevices[0], label="test")
         self.assertIsNone(an_fs.create())
 
         blivet.flags.installer_mode = False
-- 
1.9.3



More information about the anaconda-patches mailing list