[blivet:master 11/11] Move DevicelibsTestCase up to the top level of the testing directory.

David Lehman dlehman at redhat.com
Wed Jun 18 14:06:11 UTC 2014


On 06/17/2014 10:08 AM, mulhern wrote:
> It is used by formats_test tests as well as tests in devicelibs_test.
> Change all uses appropriately.
>
> Signed-off-by: mulhern <amulhern at redhat.com>
> ---
>   tests/devicelibs_test/baseclass.py   | 102 -----------------------------------
>   tests/devicelibs_test/btrfs_test.py  |  12 ++---
>   tests/devicelibs_test/crypto_test.py |   6 +--
>   tests/devicelibs_test/lvm_test.py    |   4 +-
>   tests/devicelibs_test/mdraid_test.py |   4 +-
>   tests/devicelibs_test/swap_test.py   |   4 +-
>   tests/formats_test/fslabeling.py     |   4 +-
>   tests/formats_test/labeling_test.py  |   4 +-
>   tests/formats_test/selinux_test.py   |   4 +-
>   tests/loopbacktestcase.py            | 102 +++++++++++++++++++++++++++++++++++
>   10 files changed, 123 insertions(+), 123 deletions(-)
>   delete mode 100644 tests/devicelibs_test/baseclass.py
>   create mode 100644 tests/loopbacktestcase.py
>
> diff --git a/tests/devicelibs_test/baseclass.py b/tests/devicelibs_test/baseclass.py
> deleted file mode 100644
> index 5e6d51e..0000000
> --- a/tests/devicelibs_test/baseclass.py
> +++ /dev/null
> @@ -1,102 +0,0 @@
> -import unittest
> -import os
> -import subprocess
> -
> -
> -def makeLoopDev(device_name, file_name, num_blocks=102400):
> -    """ Set up a loop device with a backing store.
> -
> -        :param str device_name: the path of the loop device
> -        :param str file_name: the path of the backing file
> -        :param int num_blocks: the size of file_name in number of blocks
> -    """
> -    proc = subprocess.Popen(["dd", "if=/dev/zero", "of=%s" % file_name,
> -                             "bs=1024", "count=%d" % num_blocks],
> -                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
> -    while True:
> -        proc.communicate()
> -        if proc.returncode is not None:
> -            rc = proc.returncode
> -            break
> -    if rc:
> -        raise OSError("dd failed creating the file %s" % file_name)
> -
> -    proc = subprocess.Popen(["losetup", device_name, file_name],
> -                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
> -    while True:
> -        proc.communicate()
> -        if proc.returncode is not None:
> -            rc = proc.returncode
> -            break
> -    if rc:
> -        raise OSError("losetup failed setting up the loop device %s" % device_name)
> -
> -def removeLoopDev(device_name, file_name):
> -    proc = subprocess.Popen(["losetup", "-d", device_name],
> -                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
> -    while True:
> -        proc.communicate()
> -        if proc.returncode is not None:
> -            rc = proc.returncode
> -            break
> -    if rc:
> -        raise OSError("losetup failed removing the loop device %s" % device_name)
> -
> -    os.unlink(file_name)
> -
> -def getFreeLoopDev():
> -    # There's a race condition here where another process could grab the loop
> -    # device losetup gives us before we have time to set it up, but that's just
> -    # a chance we'll have to take.
> -    proc = subprocess.Popen(["losetup", "-f"],
> -                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
> -    out = None
> -
> -    while True:
> -        (out, _err) = proc.communicate()
> -        if proc.returncode is not None:
> -            rc = proc.returncode
> -            out = out.strip()
> -            break
> -
> -    if rc:
> -        raise OSError("losetup failed to find a free device")
> -
> -    return out
> -
> - at unittest.skipUnless(os.geteuid() == 0, "requires root privileges")
> -class DevicelibsTestCase(unittest.TestCase):
> -
> -    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', 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 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, num_blocks=size)
> -            self._loopMap[dev] = store
> -            self.loopDevices.append(dev)
> -
> -    def tearDown(self):
> -        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 40a26de..4957855 100755
> --- a/tests/devicelibs_test/btrfs_test.py
> +++ b/tests/devicelibs_test/btrfs_test.py
> @@ -8,9 +8,9 @@ import unittest
>   import blivet.devicelibs.btrfs as btrfs
>   from blivet.errors import BTRFSError
>
> -import tests.devicelibs_test.baseclass as baseclass
> +import tests.loopbacktestcase as loopbacktestcase
>
> -class BTRFSMountDevice(baseclass.DevicelibsTestCase):
> +class BTRFSMountDevice(loopbacktestcase.LoopBackTestCase):
>       """A superclass that mounts and unmounts the filesystem.
>          It must create the filesystem on its chosen devices before mounting.
>          It always mounts the filesystem using self.device at self.mountpoint.
> @@ -26,7 +26,7 @@ class BTRFSMountDevice(baseclass.DevicelibsTestCase):
>
>              Chooses the device to specify to mount command arbitrarily.
>           """
> -        baseclass.DevicelibsTestCase.setUp(self)
> +        loopbacktestcase.LoopBackTestCase.setUp(self)
>
>           btrfs.create_volume(self.loopDevices)
>           self.device = self.loopDevices[0]
> @@ -50,9 +50,9 @@ class BTRFSMountDevice(baseclass.DevicelibsTestCase):
>               raise OSError("failed to unmount device %s" % self.device)
>
>           os.rmdir(self.mountpoint)
> -        baseclass.DevicelibsTestCase.tearDown(self)
> +        loopbacktestcase.LoopBackTestCase.tearDown(self)
>
> -class BTRFSAsRootTestCase1(baseclass.DevicelibsTestCase):
> +class BTRFSAsRootTestCase1(loopbacktestcase.LoopBackTestCase):
>
>       def testUnmountedBTRFS(self):
>           """A series of simple tests on an unmounted file system.
> @@ -177,7 +177,7 @@ class BTRFSAsRootTestCase2(BTRFSMountDevice):
>           subvolumes = btrfs.list_subvolumes(self.mountpoint)
>           self.assertEqual(len([v for v in subvolumes if v['path'].find("SV1.1") != -1]), 1)
>
> -class BTRFSAsRootTestCase3(baseclass.DevicelibsTestCase):
> +class BTRFSAsRootTestCase3(loopbacktestcase.LoopBackTestCase):
>
>       def __init__(self, methodName='runTest'):
>           super(BTRFSAsRootTestCase3, self).__init__(methodName=methodName, deviceSpec=[8192])
> diff --git a/tests/devicelibs_test/crypto_test.py b/tests/devicelibs_test/crypto_test.py
> index 8316a21..dc12645 100755
> --- a/tests/devicelibs_test/crypto_test.py
> +++ b/tests/devicelibs_test/crypto_test.py
> @@ -6,7 +6,7 @@ import os
>
>   from blivet.devicelibs import crypto
>   from blivet.errors import CryptoError
> -from tests.devicelibs_test import baseclass
> +from tests import loopbacktestcase
>
>   #FIXME: some of these tests expect behavior which is not correct
>   #
> @@ -27,7 +27,7 @@ from tests.devicelibs_test import baseclass
>   # CryptSetup.status is informative and that it would be useful to preserve it.
>   # The numeric values are enumerated in libcryptsetup.h.
>
> -class CryptoTestCase(baseclass.DevicelibsTestCase):
> +class CryptoTestCase(loopbacktestcase.LoopBackTestCase):
>
>       def testCryptoMisc(self):
>           _LOOP_DEV0 = self.loopDevices[0]
> @@ -129,7 +129,7 @@ class CryptoTestCase(baseclass.DevicelibsTestCase):
>           # cleanup
>           os.unlink(keyfile)
>
> -class CryptoTestCase2(baseclass.DevicelibsTestCase):
> +class CryptoTestCase2(loopbacktestcase.LoopBackTestCase):
>
>       def __init__(self, methodName='runTest'):
>           """Set up the names by which luks knows these devices."""
> diff --git a/tests/devicelibs_test/lvm_test.py b/tests/devicelibs_test/lvm_test.py
> index a66c756..fa546f4 100755
> --- a/tests/devicelibs_test/lvm_test.py
> +++ b/tests/devicelibs_test/lvm_test.py
> @@ -5,7 +5,7 @@ import blivet.devicelibs.lvm as lvm
>   from blivet.size import Size
>   from blivet.errors import LVMError
>
> -from tests.devicelibs_test import baseclass
> +from tests import loopbacktestcase
>
>   class LVMTestCase(unittest.TestCase):
>
> @@ -31,7 +31,7 @@ class LVMTestCase(unittest.TestCase):
>   # call if the device is non-existant, and usually that exception is caught and
>   # an LVMError is then raised, but not always.
>
> -class LVMAsRootTestCase(baseclass.DevicelibsTestCase):
> +class LVMAsRootTestCase(loopbacktestcase.LoopBackTestCase):
>
>       def __init__(self, methodName='runTest'):
>           """Set up the structure of the volume group."""
> diff --git a/tests/devicelibs_test/mdraid_test.py b/tests/devicelibs_test/mdraid_test.py
> index 3b12aaf..8185132 100755
> --- a/tests/devicelibs_test/mdraid_test.py
> +++ b/tests/devicelibs_test/mdraid_test.py
> @@ -7,7 +7,7 @@ import blivet.devicelibs.mdraid as mdraid
>   from blivet.errors import MDRaidError
>   from blivet.size import Size
>
> -from tests.devicelibs_test import baseclass
> +from tests import loopbacktestcase
>
>   class MDRaidTestCase(unittest.TestCase):
>
> @@ -49,7 +49,7 @@ class MDRaidTestCase(unittest.TestCase):
>                            mdraid.MD_SUPERBLOCK_SIZE)
>
>
> -class MDRaidAsRootTestCase(baseclass.DevicelibsTestCase):
> +class MDRaidAsRootTestCase(loopbacktestcase.LoopBackTestCase):
>
>       def __init__(self, methodName='runTest'):
>           """Set up the structure of the mdraid array."""
> diff --git a/tests/devicelibs_test/swap_test.py b/tests/devicelibs_test/swap_test.py
> index 8f7d2bb..9fa8240 100755
> --- a/tests/devicelibs_test/swap_test.py
> +++ b/tests/devicelibs_test/swap_test.py
> @@ -4,9 +4,9 @@ import unittest
>   import blivet.devicelibs.swap as swap
>   from blivet.errors import SwapError
>
> -from tests.devicelibs_test import baseclass
> +from tests import loopbacktestcase
>
> -class SwapTestCase(baseclass.DevicelibsTestCase):
> +class SwapTestCase(loopbacktestcase.LoopBackTestCase):
>
>       def testSwap(self):
>           _LOOP_DEV0 = self.loopDevices[0]
> diff --git a/tests/formats_test/fslabeling.py b/tests/formats_test/fslabeling.py
> index 4cfa9f1..12624a3 100644
> --- a/tests/formats_test/fslabeling.py
> +++ b/tests/formats_test/fslabeling.py
> @@ -2,10 +2,10 @@
>
>   import abc
>
> -from tests.devicelibs_test import baseclass
> +from tests import loopbacktestcase
>   from blivet.errors import FSError
>
> -class LabelingAsRoot(baseclass.DevicelibsTestCase):
> +class LabelingAsRoot(loopbacktestcase.LoopBackTestCase):
>       """Tests various aspects of labeling a filesystem where there
>          is no easy way to read the filesystem's label once it has been
>          set and where the filesystem can not be relabeled.
> diff --git a/tests/formats_test/labeling_test.py b/tests/formats_test/labeling_test.py
> index 1519cca..b880ac9 100755
> --- a/tests/formats_test/labeling_test.py
> +++ b/tests/formats_test/labeling_test.py
> @@ -1,7 +1,7 @@
>   #!/usr/bin/python
>   import unittest
>
> -from tests.devicelibs_test import baseclass
> +from tests import loopbacktestcase
>   from blivet.formats import device_formats
>   import blivet.formats.fs as fs
>   import blivet.formats.swap as swap
> @@ -126,7 +126,7 @@ class HFSTestCase(fslabeling.LabelingAsRoot):
>       def setUp(self):
>           super(HFSTestCase, self).setUp()
>
> -class LabelingSwapSpaceTestCase(baseclass.DevicelibsTestCase):
> +class LabelingSwapSpaceTestCase(loopbacktestcase.LoopBackTestCase):
>
>       def testLabeling(self):
>           swp = swap.SwapSpace(device=self.loopDevices[0])
> diff --git a/tests/formats_test/selinux_test.py b/tests/formats_test/selinux_test.py
> index 9c48ea4..ce3bcb1 100755
> --- a/tests/formats_test/selinux_test.py
> +++ b/tests/formats_test/selinux_test.py
> @@ -5,10 +5,10 @@ import tempfile
>   import unittest
>
>   import blivet
> -from tests.devicelibs_test import baseclass
> +from tests import loopbacktestcase
>   import blivet.formats.fs as fs
>
> -class SELinuxContextTestCase(baseclass.DevicelibsTestCase):
> +class SELinuxContextTestCase(loopbacktestcase.LoopBackTestCase):
>       """Testing SELinux contexts.
>       """
>
> diff --git a/tests/loopbacktestcase.py b/tests/loopbacktestcase.py
> new file mode 100644
> index 0000000..de6cfb0
> --- /dev/null
> +++ b/tests/loopbacktestcase.py
> @@ -0,0 +1,102 @@
> +import unittest
> +import os
> +import subprocess
> +
> +
> +def makeLoopDev(device_name, file_name, num_blocks=102400):
> +    """ Set up a loop device with a backing store.
> +
> +        :param str device_name: the path of the loop device
> +        :param str file_name: the path of the backing file
> +        :param int num_blocks: the size of file_name in number of blocks
> +    """
> +    proc = subprocess.Popen(["dd", "if=/dev/zero", "of=%s" % file_name,
> +                             "bs=1024", "count=%d" % num_blocks],
> +                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
> +    while True:
> +        proc.communicate()
> +        if proc.returncode is not None:
> +            rc = proc.returncode
> +            break
> +    if rc:
> +        raise OSError("dd failed creating the file %s" % file_name)
> +
> +    proc = subprocess.Popen(["losetup", device_name, file_name],
> +                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
> +    while True:
> +        proc.communicate()
> +        if proc.returncode is not None:
> +            rc = proc.returncode
> +            break
> +    if rc:
> +        raise OSError("losetup failed setting up the loop device %s" % device_name)
> +
> +def removeLoopDev(device_name, file_name):
> +    proc = subprocess.Popen(["losetup", "-d", device_name],
> +                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
> +    while True:
> +        proc.communicate()
> +        if proc.returncode is not None:
> +            rc = proc.returncode
> +            break
> +    if rc:
> +        raise OSError("losetup failed removing the loop device %s" % device_name)
> +
> +    os.unlink(file_name)
> +
> +def getFreeLoopDev():
> +    # There's a race condition here where another process could grab the loop
> +    # device losetup gives us before we have time to set it up, but that's just
> +    # a chance we'll have to take.
> +    proc = subprocess.Popen(["losetup", "-f"],
> +                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
> +    out = None
> +
> +    while True:
> +        (out, _err) = proc.communicate()
> +        if proc.returncode is not None:
> +            rc = proc.returncode
> +            out = out.strip()
> +            break
> +
> +    if rc:
> +        raise OSError("losetup failed to find a free device")
> +
> +    return out
> +
> + at unittest.skipUnless(os.geteuid() == 0, "requires root privileges")
> +class LoopBackTestCase(unittest.TestCase):
> +
> +    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', 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 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, num_blocks=size)
> +            self._loopMap[dev] = store
> +            self.loopDevices.append(dev)
> +
> +    def tearDown(self):
> +        for (dev, store) in self._loopMap.iteritems():
> +            removeLoopDev(dev, store)
>

My only gripe is that it isn't "loopback" unless you're talking 
networking. /dev/loop* are "loop" devices. So it should either be 
LoopTestCase or maybe LoopBackedTestCase, with a corresponding filename.

David


More information about the anaconda-patches mailing list